source: branches/client-3.0/src/ndpsmb.c@ 933

Last change on this file since 933 was 933, checked in by Paul Smedley, 9 years ago

Introduce smb_echo, used to confirm server connection is still active before trying a connection

  • Property svn:eol-style set to native
File size: 55.1 KB
Line 
1/*
2 Netdrive Samba client plugin
3 plugin API
4 Copyright (C) netlabs.org 2003-2008
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <stdarg.h>
24#include <string.h>
25#include <time.h>
26
27#define NDPL_LARGEFILES
28#define INCL_LONGLONG
29#include <ndextpl2.h>
30#include "smbwrp.h"
31#include "util.h"
32
33#define debug_printf(...) debuglocal(9, __VA_ARGS__)
34
35// -------------------------------------------------------------
36
37/* time conversion functions: SMB protocol sends timestamps in GMT time,
38* os2 api uses localtime,
39* emx/klibc uses timezone and daylight saving to convert GMT timestamps,
40* so only the timezone must be counted in conversion.
41*/
42void fsphUnixTimeToDosDate( time_t time, FDATE* fdate, FTIME *ftime)
43{
44 struct tm* gmt = localtime( &time);
45#if 0 // as localtime() already does dst we don't need to add something
46 if (gmt->tm_isdst>0) {
47 debug_printf( "daylight saving in effect %d, timezone %d\n",gmt->tm_isdst, timezone);
48 time -= 3600;
49 gmt = localtime( &time);
50 }
51#endif
52 fdate->day = gmt->tm_mday;
53 fdate->month = gmt->tm_mon+1;
54 fdate->year = gmt->tm_year + 1900 - 1980;
55 ftime->twosecs = gmt->tm_sec/2;
56 ftime->minutes = gmt->tm_min;
57 ftime->hours = gmt->tm_hour;
58}
59
60void fsphDosDateToUnixTime( FDATE fdate, FTIME ftime, ULONG* time)
61{
62 struct tm gmtime = { 0 };
63
64 debug_printf( "fsphDosDateToUnixTime time %02d:%02d:%02d\n", ftime.hours, ftime.minutes, ftime.twosecs*2);
65 gmtime.tm_mday = fdate.day;
66 gmtime.tm_mon = fdate.month-1;
67 gmtime.tm_year = fdate.year + 1980 - 1900;
68 gmtime.tm_sec = ftime.twosecs*2;
69 gmtime.tm_min = ftime.minutes;
70 gmtime.tm_hour = ftime.hours;
71 gmtime.tm_isdst = -1; // force libc to check dst saving
72
73 *time = mktime( &gmtime);
74 debug_printf( "fsphDosDateToUnixTime time1 %d %s", *time, ctime( (time_t*)time));
75#if 0 // as mktime() already does dst we don't need to add something
76 struct tm* gmt;
77 gmt = localtime( (time_t*) time);
78 if (gmt->tm_isdst>0) {
79 debug_printf( "fsphDosDateToUnixTime daylight saving in effect %d, timezone %d\n",gmt->tm_isdst, timezone);
80 *time += 3600;
81 }
82 debug_printf( "fsphDosDateToUnixTime time2 %d %s", *time, ctime( (time_t*)time));
83#endif
84}
85
86// -------------------------------------------------------------
87
88/* uppercased type of resource */
89const char *NdpTypes[] =
90{
91 "SMBFS",
92 NULL
93}
94;
95
96/* Properties of supported resource types */
97
98/* Properties of resource */
99static const NDPROPERTYINFO smbProperties[] =
100{
101 {ND_PROP_STRING, 0, "WORKGROUP", ""},
102 {ND_PROP_STRING, 0, "SERVER", ""},
103 {ND_PROP_STRING, 0, "SHARE", ""},
104 {ND_PROP_STRING, 0, "USER", "guest"},
105 {ND_PROP_STRING, 0, "PASSWORD", ""},
106 {ND_PROP_STRING, 0, "SPASSWORD", ""},
107 {ND_PROP_STRING, 0, "MASTER", "WORKGROUP"},
108 {ND_PROP_ULONG, 0, "MASTERTYPE", "1"},
109 {ND_PROP_ULONG, 0, "CTO", "10"},
110 {ND_PROP_ULONG, 0, "CLD", "32"},
111 {ND_PROP_ULONG, 0, "EASUPPORT", "1"},
112 {ND_PROP_ULONG, 0, "KRB5SUPPORT", "0"},
113 {ND_PROP_ULONG, 0, "NTLMv1SUPPORT", "0"},
114 {ND_PROP_STRING, 0, NULL, NULL}
115};
116
117
118/* Exported array of properties */
119const NDPROPERTYINFO *NdpPropertiesInfo[] =
120{
121 smbProperties
122};
123
124
125PLUGINHELPERTABLE2L *ph;
126static int ifL;
127
128/* A mutex to serialize plugin calls because libsmb may not be thread safe. */
129static NDMUTEX mutex;
130
131static int lockInit (void)
132{
133 return ph->fsphCreateMutex (&mutex);
134}
135
136static void lockClose (void)
137{
138 ph->fsphCloseMutex (mutex);
139}
140
141static int lockRequest (void)
142{
143 return ph->fsphRequestMutex (mutex, SEM_INDEFINITE_WAIT);
144}
145
146static void lockRelease (void)
147{
148 ph->fsphReleaseMutex (mutex);
149}
150
151#if LIBSMB_THREAD_SAFE==0
152
153#define ENTER() do { \
154 int rcLock = lockRequest(); \
155 if (rcLock != NO_ERROR) \
156 return rcLock; \
157} while (0)
158
159#define LEAVE() do { \
160 lockRelease(); \
161} while (0)
162
163#else
164#define ENTER() do { /* nothing */ } while (0)
165#define LEAVE() do { /* nothing */ } while (0)
166#endif
167
168int helperEASet (cli_state *cli, FEALIST *pFEAList, char *path)
169{
170 int rc = 0;
171
172 if (!path || !pFEAList || pFEAList->cbList <= sizeof(long))
173 {
174 return ERROR_EAS_NOT_SUPPORTED;
175 }
176
177 ENTER();
178
179 do {
180 // got FEA there
181 FEA * pfea;
182 unsigned long done = sizeof(long);
183 pfea = pFEAList->list;
184 while (done < pFEAList->cbList)
185 {
186 rc = smbwrp_setea(cli, path, (char*)(pfea + 1), pfea->cbValue ? (char *)(pfea + 1) + pfea->cbName + 1: NULL, pfea->cbValue);
187 if (rc)
188 {
189 break;
190 }
191 pfea = (FEA *)((char *)(pfea + 1) + pfea->cbName + 1 + pfea->cbValue);
192 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
193 }
194 } while (0);
195 LEAVE();
196 return rc;
197}
198
199int APIENTRY NdpPluginLoad (PLUGINHELPERTABLE2L *pPHT)
200{
201 int rc;
202 HPIPE pipe;
203 unsigned long action;
204 ph = pPHT;
205 ifL = 0;
206/*
207 if (ph->cb < sizeof (PLUGINHELPERTABLE2))
208 {
209 return ERROR_INVALID_FUNCTION;
210 }
211*/
212 if (ph->cb >= sizeof (PLUGINHELPERTABLE2L))
213 {
214 ifL = 1;
215 }
216 lockInit();
217 debugInit();
218 debuglocal(9,"Working with %s bit fileio NDFS\n", ifL ? "64" : "32");
219 return NO_ERROR;
220}
221
222
223int APIENTRY NdpPluginFree (void)
224{
225 debugDelete();
226 lockClose();
227 return NO_ERROR;
228}
229
230
231void getfindinfo(Connection * pConn, FILEFINDBUF3 * stat, smbwrp_fileinfo * finfo)
232{
233 char * name = ph->fsphStrRChr(finfo->fname, '\\');
234 if (name)
235 {
236 name++;
237 }
238 else
239 {
240 name = finfo->fname;
241 }
242 if (!*name)
243 {
244 name = pConn->pRes->srv.share_name;
245 }
246 strncpy(stat->achName, name, CCHMAXPATHCOMP - 1);
247 stat->cbFile = finfo->size;
248 stat->cbFileAlloc = stat->cbFile;
249 stat->oNextEntryOffset = 0ul;
250 stat->cchName = strlen(stat->achName);
251 stat->attrFile = (finfo->attr & 0x37);
252
253 fsphUnixTimeToDosDate(finfo->mtime, &stat->fdateLastWrite, &stat->ftimeLastWrite);
254 fsphUnixTimeToDosDate(finfo->ctime, &stat->fdateCreation, &stat->ftimeCreation);
255 fsphUnixTimeToDosDate(finfo->atime, &stat->fdateLastAccess, &stat->ftimeLastAccess);
256}
257
258int getfindinfoL(Connection * pConn, void * plist, smbwrp_fileinfo * finfo, ULONG ulAttribute, char * mask)
259{
260 FILESTATUS3L stat = {0};
261 char * name = ph->fsphStrRChr(finfo->fname, '\\');
262 if (name)
263 {
264 name++;
265 }
266 else
267 {
268 name = finfo->fname;
269 }
270 if (!*name)
271 {
272 name = pConn->pRes->srv.share_name;
273 }
274 if (mask && (!ph->fsphAttrMatch(ulAttribute, finfo->attr & 0x37) || !ph->fsphWildMatch(mask, name, ND_IGNORE_CASE)))
275 {
276 return 0;
277 }
278
279 stat.cbFile = finfo->size;
280 stat.cbFileAlloc = stat.cbFile;
281 stat.attrFile = (finfo->attr & 0x37);
282
283 fsphUnixTimeToDosDate(finfo->mtime, &stat.fdateLastWrite, &stat.ftimeLastWrite);
284 fsphUnixTimeToDosDate(finfo->ctime, &stat.fdateCreation, &stat.ftimeCreation);
285 fsphUnixTimeToDosDate(finfo->atime, &stat.fdateLastAccess, &stat.ftimeLastAccess);
286 debug_printf( "fname %s\n", finfo->fname);
287 debug_printf( "mtime %d %s", finfo->mtime, ctime( (time_t*)&finfo->mtime));
288 debug_printf( "ftimeLastAccess %02d:%02d:%02d\n", stat.ftimeLastWrite.hours, stat.ftimeLastWrite.minutes, stat.ftimeLastWrite.twosecs*2);
289
290 ph->fsphAddFile32L(plist, &stat, name, strlen(name), finfo, sizeof(*finfo), 0);
291 return 1;
292}
293
294static unsigned char fromhex (char c)
295{
296 if ('0' <= c && c <= '9')
297 {
298 return c - '0';
299 }
300
301 if ('A' <= c && c <= 'F')
302 {
303 return c - 'A' + 0xA;
304 }
305
306 if ('a' <= c && c <= 'f')
307 {
308 return c - 'a' + 0xA;
309 }
310
311 return 0;
312}
313
314static char tohex (unsigned char b)
315{
316 b &= 0xF;
317
318 if (b <= 9)
319 {
320 return b + '0';
321 }
322
323 return 'A' + (b - 0xA);
324}
325
326static void decryptPassword (const char *pszCrypt, char *pszPlain)
327{
328 /* A simple "decryption", character from the hex value. */
329 const char *s = pszCrypt;
330 char *d = pszPlain;
331
332 while (*s)
333 {
334 *d++ = (char)((fromhex (*s++) << 4) + fromhex (*s++));
335 }
336
337 *d++ = 0;
338}
339
340static void encryptPassword (const char *pszPlain, char *pszCrypt)
341{
342 /* A simple "encryption" encode each character as hex value. */
343 const char *s = pszPlain;
344 char *d = pszCrypt;
345
346 while (*s)
347 {
348 *d++ = tohex ((*s) >> 4);
349 *d++ = tohex (*s);
350 s++;
351 }
352
353 *d++ = 0;
354}
355
356/* accept parameters in form
357 * [filename][;name=filename]
358 */
359int initResource (Resource *pRes, NDPROPERTYHANDLE properties)
360{
361 int rc = NO_ERROR;
362 unsigned long t;
363 const CHAR * q = NULL;
364 int defaultPassword = 1;
365
366 pRes->rootlevel = 0;
367 pRes->easupport = 1;
368 pRes->krb5support = 0;
369 pRes->ntlmv1support = 0;
370 pRes->pdc = NULL;
371
372 t = 0, q = NULL;
373 rc = ph->fsphQueryStringProperty (properties, "WORKGROUP", &q, &t);
374 if (!rc && t && *q)
375 {
376 strncpy(pRes->srv.workgroup, q, sizeof(pRes->srv.workgroup) - 1);
377 pRes->rootlevel = 1;
378 }
379
380 t = 0, q = NULL;
381 rc = ph->fsphQueryStringProperty (properties, "SERVER", &q, &t);
382 if (!rc && t && *q)
383 {
384 strncpy(pRes->srv.server_name, q, sizeof(pRes->srv.server_name) - 1);
385 pRes->rootlevel = 2;
386 }
387
388 t = 0, q = NULL;
389 rc = ph->fsphQueryStringProperty (properties, "SHARE", &q, &t);
390 if (!rc && t && *q)
391 {
392 strncpy(pRes->srv.share_name, q, sizeof(pRes->srv.share_name) - 1);
393 pRes->rootlevel = 3;
394 }
395
396 t = 0, q = NULL;
397 rc = ph->fsphQueryStringProperty (properties, "USER", &q, &t);
398 if (!rc && t && *q)
399 {
400 strncpy(pRes->srv.username, q, sizeof(pRes->srv.username) - 1);
401 }
402
403 t = 0, q = NULL;
404 rc = ph->fsphQueryStringProperty (properties, "PASSWORD", &q, &t);
405 if (!rc && t && *q)
406 {
407 strncpy(pRes->srv.password, q, sizeof(pRes->srv.password) - 1);
408 defaultPassword = 0;
409 }
410
411 t = 0, q = NULL;
412 rc = ph->fsphQueryStringProperty (properties, "SPASSWORD", &q, &t);
413 if ( rc == NO_ERROR && *q != '\0' && defaultPassword)
414 {
415 char p[1024];
416 p[0] = 0;
417
418 decryptPassword (q, p);
419
420 if (*p)
421 {
422 strncpy(pRes->srv.password, p, sizeof(pRes->srv.password) - 1);
423
424 /* clear the plain password */
425 ph->fsphSetProperty (properties, "PASSWORD", "");
426 }
427 }
428 else
429 {
430 char c[1024];
431 encryptPassword (pRes->srv.password, c);
432
433 ph->fsphSetProperty (properties, "SPASSWORD", c);
434
435 // clear the plain password
436 ph->fsphSetProperty (properties, "PASSWORD", "");
437 }
438
439 t = 0, q = NULL;
440 rc = ph->fsphQueryStringProperty (properties, "MASTER", &q, &t);
441 if (!rc && t && *q)
442 {
443 strncpy(pRes->srv.master, q, sizeof(pRes->srv.master) - 1);
444 }
445
446 t = 0;
447 rc = ph->fsphQueryUlongProperty (properties, "MASTERTYPE", &t);
448 if (!rc)
449 {
450 if (t > 1)
451 {
452 rc = ERROR_INVALID_PARAMETER;
453 }
454 else
455 {
456 pRes->srv.ifmastergroup = t;
457 }
458 }
459
460 t = 0;
461 rc = ph->fsphQueryUlongProperty (properties, "EASUPPORT", &t);
462 if (!rc)
463 {
464 if (t > 1)
465 {
466 rc = ERROR_INVALID_PARAMETER;
467 }
468 else
469 {
470 pRes->easupport = t;
471 }
472 }
473
474 t = 0;
475 rc = ph->fsphQueryUlongProperty (properties, "KRB5SUPPORT", &t);
476 if (!rc)
477 {
478 if (t > 1)
479 {
480 rc = ERROR_INVALID_PARAMETER;
481 }
482 else
483 {
484 pRes->krb5support = t;
485 }
486 }
487 t = 0;
488 rc = ph->fsphQueryUlongProperty (properties, "NTLMv1SUPPORT", &t);
489 if (!rc)
490 {
491 if (t > 1)
492 {
493 rc = ERROR_INVALID_PARAMETER;
494 }
495 else
496 {
497 pRes->ntlmv1support = t;
498 }
499 }
500
501 t = 0;
502 rc = ph->fsphQueryUlongProperty (properties, "CTO", &t);
503 if (!rc)
504 {
505 if (t > 600)
506 {
507 rc = ERROR_INVALID_PARAMETER;
508 }
509 else
510 {
511 pRes->cachetimeout = t;
512 }
513 }
514
515 t = 0;
516 rc = ph->fsphQueryUlongProperty (properties, "CLD", &t);
517 if (!rc)
518 {
519 if (t > 96)
520 {
521 rc = ERROR_INVALID_PARAMETER;
522 }
523 else
524 {
525 pRes->cachedepth = t;
526 }
527 }
528
529 /*
530 * Create a directory cache with expiration time and cache listings
531 * the above values come from the gui. default: timeout 10; listings: 32
532 */
533 dircache_create(&pRes->pdc, pRes->cachetimeout, pRes->cachedepth);
534
535 return rc;
536}
537
538int iftestpath(char * path)
539{
540 char * p = path;
541 if (!path)
542 {
543 return 0;
544 }
545 while ((p = ph->fsphStrChr(p, 'A')) != NULL)
546 {
547 if (ph->fsphStrNCmp(p, "A.+,;=[].B", 10) == 0)
548 {
549 return 1;
550 }
551 p++;
552 }
553 return 0;
554}
555
556int pathparser(Resource *pRes, Connection * pConn, char * path, char * result)
557{
558 int rootlevel;
559 int rc = NO_ERROR;
560 if (!pRes || !path || !result)
561 {
562 return ERROR_INVALID_PARAMETER;
563 }
564 // handle special case when someone wants to test support of LFN or smth similar
565 if (iftestpath(path))
566 {
567 strcpy(result, "\\A.+,;=[].B");
568 return NO_ERROR;
569 }
570
571 rootlevel = pRes->rootlevel;
572 if (*path == '\\') path++;
573
574 if (rootlevel < 3)
575 {
576 char * p;
577 // flag: 1 parameters changed, reconnection required, 0 do nothing
578 int newlevel = 0;
579 // use a temporary resource to test disconnection/reconnection
580 Resource tmpRes;
581 // copy exising data
582 memcpy( &tmpRes, pRes, sizeof( tmpRes));
583 // pointer to new connection fields
584 smbwrp_server * tmp = &tmpRes.srv;
585 if (rootlevel == 0)
586 {
587 p = ph->fsphStrChr(path, '\\');
588 if (!p)
589 {
590 p = path + strlen(path);
591 }
592 if (strlen(tmp->workgroup) != p - path
593 || (p == path || ph->fsphStrNICmp(path, tmp->workgroup, p - path)))
594 {
595 strncpy(tmp->workgroup, path, p - path);
596 tmp->workgroup[p - path] = 0;
597 newlevel = 1;
598 }
599 path = *p == '\\' ? p + 1 : p;
600 rootlevel = 1;
601 }
602 if (rootlevel == 1) // root path starts from server name
603 {
604 p = ph->fsphStrChr(path, '\\');
605 if (!p)
606 {
607 p = path + strlen(path);
608 }
609 if (strlen(tmp->server_name) != p - path
610 || (p == path || ph->fsphStrNICmp(path, tmp->server_name, p - path)))
611 {
612 strncpy(tmp->server_name, path, p - path);
613 tmp->server_name[p - path] = 0;
614 newlevel = 1;
615 }
616 path = *p == '\\' ? p + 1 : p;
617 rootlevel = 2;
618 }
619 if (rootlevel == 2) // root path starts from share name
620 {
621 p = ph->fsphStrChr(path, '\\');
622 if (!p)
623 {
624 p = path + strlen(path);
625 }
626 if (strlen(tmp->share_name) != (p - path)
627 || (p == path || ph->fsphStrNICmp(path, tmp->share_name, p - path)))
628 {
629 strncpy(tmp->share_name, path, p - path);
630 tmp->share_name[p - path] = 0;
631 newlevel = 1;
632 }
633 path = *p == '\\' ? p + 1 : p;
634 }
635 if (newlevel)
636 {
637 // reconnect to server here, first test new connection
638 cli_state* tmp_cli = NULL;
639 rc = smbwrp_connect( &tmpRes, &tmp_cli);
640 if (!rc)
641 {
642 // new connection is ok, disconnect old one
643 cli_state* cli = pConn->cli;
644 smbwrp_disconnect( pRes, cli);
645 // save tmp data structure
646 memcpy( pRes, &tmpRes, sizeof( tmpRes));
647 // save new connection handle
648 pConn->cli = tmp_cli;
649 }
650 }
651 }
652
653 strcpy(result, "\\");
654 strncat(result, path, CCHMAXPATH);
655
656 return rc;
657}
658
659
660// -------------------------------------------------------------
661
662/* check if the requested resource is available */
663static int checkMountResource( Resource* pRes)
664{
665 int rc;
666 unsigned long action;
667 cli_state* cli = NULL;
668
669 debug_printf("checkMountResource in tid#%d\n", _gettid());
670 rc = smbwrp_connect( pRes, &cli);
671/* changed to real error codes SCS
672 if (rc)
673 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_ACCESS_DENIED); */
674 switch (rc) {
675 case 0:
676 rc = NO_ERROR;
677 break;
678 case 1:
679 case 10:
680 case 11:
681 rc = ERROR_BAD_NET_NAME;
682 break;
683 case 2:
684 rc = ERROR_INIT_ROUTINE_FAILED;
685 break;
686 case 3:
687 rc = ERROR_BAD_NET_RESP;
688 break;
689 case 4:
690 rc = ERROR_NETWORK_BUSY;
691 break;
692 case 6:
693 rc = ERROR_NETWORK_ACCESS_DENIED;
694 break;
695 case 7:
696 rc = ERROR_BAD_NETPATH;
697 break;
698 default:
699 rc = ERROR_UNEXP_NET_ERR;
700 break;
701 } /* endswitch */
702
703 smbwrp_disconnect( pRes, cli);
704
705 return rc;
706}
707
708int APIENTRY NdpMountResource (HRESOURCE *presource, int type, NDPROPERTYHANDLE properties)
709{
710 int rc = NO_ERROR;
711 unsigned long objany = OBJ_ANY;
712 Resource *pRes = NULL;
713
714 ENTER();
715
716 debuglocal(9,"NdpMountResource in\n");
717
718 // init code
719 smbwrp_init();
720
721 /* since samba plugin support only 1 type of resources we do not need */
722 /* to check what the found type really is */
723 pRes = malloc( sizeof(Resource));
724 if (pRes == NULL)
725 {
726 rc = ERROR_NOT_ENOUGH_MEMORY;
727 }
728 else
729 {
730 memset(pRes, 0, sizeof(Resource));
731 //pRes->objany = objany;
732 // parse init string
733 rc = initResource (pRes, properties);
734 // try to connect to resource (check type) only if thread!=1, so ndctl startup
735 // is not slowed down by network connections.
736 // ndctl does mounting on main thread (#1)
737 // nd/ndpm do not use main thread
738 if (!rc && _gettid()!=1)
739 rc = checkMountResource( pRes);
740 if (!rc)
741 {
742 // store resource data
743 *presource = (HRESOURCE)pRes;
744 }
745 else
746 {
747 free(pRes);
748 }
749 }
750 debuglocal(9,"NdpMountResource rc=%d\n", rc);
751 LEAVE();
752 return rc;
753}
754
755// -------------------------------------------------------------
756
757int APIENTRY NdpFreeResource (HRESOURCE resource)
758{
759 Resource *pRes = (Resource *)resource;
760 ENTER();
761 dircache_delete(pRes->pdc);
762 memset(&pRes->srv, 0, sizeof(pRes->srv));
763 free(pRes);
764 debuglocal(9,"NdpFreeResource %d\n", NO_ERROR);
765 LEAVE();
766 return NO_ERROR;
767}
768
769// -------------------------------------------------------------
770
771int APIENTRY NdpRsrcCompare (HRESOURCE resource, HRESOURCE resource2)
772{
773 Resource *pRes = (Resource *)resource;
774 Resource *pRes2 = (Resource *)resource2;
775 int rc = ND_RSRC_DIFFERENT;
776
777 debuglocal(9,"NdpRsrcCompare in\n");
778 if (ph->fsphStrICmp(pRes->srv.server_name, pRes2->srv.server_name) == 0
779 && ph->fsphStrICmp(pRes->srv.share_name, pRes2->srv.share_name) == 0
780 && ph->fsphStrICmp(pRes->srv.username, pRes2->srv.username) == 0
781 && ph->fsphStrICmp(pRes->srv.workgroup, pRes2->srv.workgroup) == 0)
782 {
783 // resources are equal
784 rc = ND_RSRC_EQUAL;
785 }
786
787 debuglocal(9,"NdpRsrcCompare %d\n", rc);
788
789 return rc;
790}
791
792int APIENTRY NdpRsrcUpdate (HRESOURCE resource, HRESOURCE resource2)
793{
794 // do nothing
795 debuglocal(9,"NdpRsrcUpdate %d\n", NO_ERROR);
796 return NO_ERROR;
797}
798
799int APIENTRY NdpRsrcQueryInfo (HRESOURCE resource, ULONG *pulFlags, void *pdata, ULONG insize, ULONG *poutlen)
800{
801 Resource *pRes = (Resource *)resource;
802 int rc = NO_ERROR;
803 char s[4096];
804
805 debuglocal(9,"NdpRsrcQueryInfo in\n");
806
807 switch (pRes->rootlevel)
808 {
809 case 0:
810 {
811 ph->fsph_snprintf(s, sizeof(s) - 1, "SMBFS%s \\\\@%s", ifL ? "64" : "32", pRes->srv.username);
812 } break;
813 case 1:
814 {
815 ph->fsph_snprintf(s, sizeof(s) - 1, "SMBFS%s %s: \\\\@%s", ifL ? "64" : "32", pRes->srv.workgroup, pRes->srv.username);
816 } break;
817 case 2:
818 {
819 ph->fsph_snprintf(s, sizeof(s) - 1, "SMBFS%s \\\\%s%s%s@%s", ifL ? "64" : "32", *pRes->srv.workgroup ? pRes->srv.workgroup : "", *pRes->srv.workgroup ? ":" : "", pRes->srv.server_name, pRes->srv.username);
820 } break;
821 default:
822 {
823 ph->fsph_snprintf(s, sizeof(s) - 1, "SMBFS%s \\\\%s%s%s\\%s@%s", ifL ? "64" : "32", *pRes->srv.workgroup ? pRes->srv.workgroup : "", *pRes->srv.workgroup ? ":" : "", pRes->srv.server_name, pRes->srv.share_name, pRes->srv.username);
824 } break;
825 }
826 *poutlen = strlen(s) + 1;
827 if (*poutlen > insize)
828 {
829 rc = ERROR_BUFFER_OVERFLOW;
830 }
831 else
832 {
833 memcpy(pdata, s, *poutlen);
834 }
835
836 debuglocal(9,"NdpRsrcQueryInfo %d\n", rc);
837
838 return rc;
839}
840
841int APIENTRY NdpRsrcQueryFSAttach (HRESOURCE resource, void *pdata, ULONG insize, ULONG *poutlen)
842{
843 ULONG ulDummy = 0;
844 /* just return the resource info string */
845 return NdpRsrcQueryInfo (resource, &ulDummy, pdata, insize, poutlen);
846}
847
848int APIENTRY NdpRsrcQueryFSAllocate (HRESOURCE resource, NDFSALLOCATE *pfsa)
849{
850 Resource *pRes = (Resource *)resource;
851 int rc = NO_ERROR, rc1;
852 unsigned long action = 0;
853 cli_state* cli = NULL;
854 FSALLOCATE fsa;
855
856 ENTER();
857 debuglocal(9,"NdpRsrcQueryFSAllocate %08x\n", pfsa);
858
859 if (!pfsa)
860 {
861 LEAVE();
862 return NO_ERROR;
863 }
864
865 rc = smbwrp_connect( pRes, &cli);
866 if (rc)
867 {
868 debuglocal(9,"smbwrp_connect failed rc=%d\n", rc);
869 pfsa->cSectorUnit = 1;
870 pfsa->cUnit = 123456;
871 pfsa->cUnitAvail = 123456;
872 pfsa->cbSector = 2048;
873 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_ACCESS_DENIED);
874 LEAVE();
875 return rc;
876 }
877
878 rc = smbwrp_dskattr( cli, &fsa);
879 if (rc)
880 {
881 pfsa->cSectorUnit = 1;
882 pfsa->cUnit = 123456;
883 pfsa->cUnitAvail = 123456;
884 pfsa->cbSector = 2048;
885 //rc = rc ? rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
886 }
887 else
888 {
889 pfsa->cSectorUnit = fsa.cSectorUnit;
890 pfsa->cUnit = fsa.cUnit;
891 pfsa->cUnitAvail = fsa.cUnitAvail;
892 pfsa->cbSector = fsa.cbSector;
893 }
894
895 smbwrp_disconnect( pRes, cli);
896
897 debuglocal(9,"NdpRsrcQueryFSAllocate %d/%d (cUnit = %d/cUnitAvail = %d/cbSector = %d)\n", rc, rc1, pfsa->cUnit, pfsa->cUnitAvail, pfsa->cbSector);
898 LEAVE();
899 return rc;
900}
901
902// -------------------------------------------------------------
903
904int APIENTRY NdpCreateConnection (HRESOURCE resource, HCONNECTION *pconn)
905{
906 int rc = 0;
907 Resource * pRes = (Resource *)resource;
908 unsigned long action;
909 Connection *pConn = NULL;
910
911 ENTER();
912
913 debuglocal(9,"NdpCreateConnection in\n");
914
915 pConn = malloc( sizeof(Connection));
916 if (pConn == NULL)
917 {
918 rc = ERROR_NOT_ENOUGH_MEMORY;
919 }
920 if (rc)
921 {
922 debuglocal(9,"NdpCreateConnection ERROR_NOT_ENOUGH_MEMORY %d\n", rc);
923 LEAVE();
924 return rc;
925 }
926 memset(pConn, 0, sizeof(Connection));
927 pConn->pRes = pRes;
928 pConn->file.fd = -1;
929
930 debuglocal(9,"NdpCreateConnection send CONNECT\n");
931 rc = smbwrp_connect( pRes, &pConn->cli);
932 if (rc)
933 {
934 free(pConn);
935 pConn = NULL;
936 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_INVALID_PARAMETER);
937 }
938
939 *pconn = (HCONNECTION)pConn;
940 debuglocal(9,"NdpCreateConnection [%p] %d\n", pConn, rc);
941 LEAVE();
942 return rc;
943}
944
945// -------------------------------------------------------------
946
947int APIENTRY NdpFreeConnection (HCONNECTION conn)
948{
949 Connection *pConn = (Connection *)conn;
950 Resource *pRes = pConn->pRes;
951 int rc;
952
953 ENTER();
954
955 debuglocal(9,"NdpFreeConnection in [%p]\n", pConn);
956 if (pConn->file.fd >= 0)
957 {
958 rc = smbwrp_close( pConn->cli, &pConn->file);
959 pConn->file.fd = -1;
960 }
961
962 smbwrp_disconnect( pRes, pConn->cli);
963
964 free(pConn);
965 debuglocal(9,"NdpFreeConnection %d\n", NO_ERROR);
966 LEAVE();
967 return NO_ERROR;
968}
969
970// -------------------------------------------------------------
971
972/*
973 * NdpQueryPathInfo is the most important function :) netdrive always calls
974 * the function before every operation to find out the path status:
975 * does it exist, is it a file, does a parent directory exist, etc.
976 * Plugin must return one of the following error codes:
977 * NO_ERROR - path exists and the path information have been successfully
978 * retrieved.
979 * ERROR_FILE_NOT_FOUND - all but the last component of the path exist and the
980 * path without the last component is a directory. dir1_ok\dir2_ok\does_not_exist.
981 * the wildcard can not exist, so the plugin returns FILE_NOT_FOUND, if the parent
982 * directory exist.
983 * ERROR_PATH_NOT_FOUND - any of not last path components does not exist, or all
984 * but the last component exist and is a file: \dir_ok\dir2_ok\file_ok\non_existing.
985 * ERROR_REM_NOT_LIST - resource is temporarily unavailable for some reasons.
986 * Any other error codes means an internal plugin error, not related to the status
987 * of the path queried.
988 */
989int APIENTRY NdpQueryPathInfo (HCONNECTION conn, void *plist, char *szPath)
990{
991 Connection *pConn = (Connection *)conn;
992 Resource *pRes = pConn->pRes;
993 smbwrp_fileinfo finfo;
994 int rc = 0;
995 int rcCon = 0;
996 unsigned long action;
997 char path[CCHMAXPATH+1] = {0};
998
999 ENTER();
1000
1001 debuglocal(9,"NdpQueryPathInfo in [%p] <%s>\n", pConn, szPath);
1002
1003 // is wildcard is specified, we suppose parent dir exist, so exit immediately
1004 if (ph->fsphStrChr(szPath, '*') || ph->fsphStrChr(szPath, '?'))
1005 {
1006 LEAVE();
1007 return ERROR_FILE_NOT_FOUND;
1008 }
1009
1010
1011 do {
1012 /* First check if there is information in the directory cache. */
1013 unsigned long ulAge = 0;
1014 if (dircache_find_path(pRes->pdc, szPath, &finfo, &ulAge))
1015 {
1016 if (ulAge <= 15) /* @todo configurable. */
1017 {
1018 rc = NO_ERROR;
1019 finfo.easize = -1;
1020 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1021 break;
1022 }
1023 }
1024
1025 rc = pathparser(pRes, pConn, szPath, path);
1026 debuglocal(9,"NdpQueryPathInfo pathparser for <%s> rc=%d\n", path, rc);
1027 switch (rc)
1028 {
1029 case NO_ERROR :
1030 case ERROR_FILE_NOT_FOUND:
1031 case ERROR_PATH_NOT_FOUND:
1032 case ERROR_ACCESS_DENIED:
1033 case ERROR_INVALID_PARAMETER:
1034 {
1035 break;
1036 }
1037 default :
1038 {
1039 rc = ERROR_PATH_NOT_FOUND;
1040 }
1041 }
1042 if (rc)
1043 {
1044 break;
1045 }
1046 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1047 debuglocal(9,"NdpQueryPathInfo smbwrp_getattr for <%s>\n", path);
1048 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
1049 if (rc)
1050 {
1051 // remote server not available for first time?
1052 if (rc == ERROR_REM_NOT_LIST)
1053 {
1054 // free current cli resources
1055 smbwrp_disconnect( pRes, pConn->cli);
1056 // reconnect
1057 rcCon = smbwrp_connect( pRes, &pConn->cli);
1058 if (rcCon != NO_ERROR)
1059 debuglocal(9,"NdpQueryPathInfo smbwrp_connect rc = %d\n", rcCon);
1060
1061 // try file list again if reconnecting worked
1062 if (rcCon == NO_ERROR)
1063 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
1064 }
1065 debuglocal(9,"NdpQueryPathInfo smbwrp_getattr, rc = %d\n", rc);
1066 switch (rc)
1067 {
1068 case NO_ERROR :
1069 case ERROR_FILE_NOT_FOUND:
1070 case ERROR_PATH_NOT_FOUND:
1071 case ERROR_ACCESS_DENIED:
1072 case ERROR_INVALID_PARAMETER:
1073 case ERROR_REM_NOT_LIST:
1074 break;
1075 default :
1076 {
1077 rc = ERROR_PATH_NOT_FOUND;
1078 }
1079 }
1080 }
1081
1082 if (rc == NO_ERROR) {
1083 finfo.easize = -1;
1084 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1085 }
1086 else if (rc == ERROR_FILE_NOT_FOUND)
1087 {
1088 // now try the upper path
1089 char * p = ph->fsphStrRChr(finfo.fname, '\\');
1090 if (p && p > finfo.fname)
1091 {
1092 *p = 0;
1093 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
1094 debuglocal(9,"NdpQueryPathInfo upper path in <%s>, rc = %d\n", finfo.fname, rc);
1095 if (rc == NO_ERROR)
1096 {
1097 rc = (finfo.attr & FILE_DIRECTORY) !=0 ?
1098 ERROR_FILE_NOT_FOUND:ERROR_PATH_NOT_FOUND;
1099 }
1100 else if (rc != ERROR_REM_NOT_LIST)
1101 {
1102 rc = ERROR_PATH_NOT_FOUND;
1103 }
1104 }
1105 }
1106 } while (0);
1107 debuglocal(9,"NdpQueryPathInfo <%s> (%s) %d\n", szPath, path, rc);
1108
1109 LEAVE();
1110 return rc;
1111}
1112
1113// -------------------------------------------------------------
1114
1115int APIENTRY NdpFindStart (HCONNECTION conn, void *plist, NDFILEINFOL *pfiparent, char *szPath, ULONG ulAttribute)
1116{
1117 Connection *pConn = (Connection *)conn;
1118 Resource *pRes = pConn->pRes;
1119 int rc = NO_ERROR, count = 0;
1120 unsigned long action;
1121 char *mask = "*";
1122 char dir[CCHMAXPATH+1] = {0};
1123 char path[CCHMAXPATH+1] = {0};
1124 smbwrp_fileinfo * data;
1125 NDPATHELEMENT *pel = ph->fsphNameElem(0);
1126 filelist_state state;
1127 char * p;
1128
1129 ENTER();
1130
1131 debug_printf("NdpFindStart in [%p]\n", pConn);
1132
1133 strncpy(dir, szPath, sizeof(dir) - 1);
1134 if (pel)
1135 {
1136 mask = pel->name;
1137 dir[strlen(szPath) - pel->length] = 0;
1138 }
1139 action = strlen(dir) - 1;
1140 if (dir[action] == '\\')
1141 {
1142 dir[action] = 0;
1143 }
1144 rc = pathparser(pRes, pConn, dir, path);
1145 if (rc)
1146 {
1147 return rc;
1148 }
1149 action = strlen(path) - 1;
1150 if (path[action] != '\\')
1151 {
1152 strncat(path, "\\", sizeof(path) - 1);
1153 }
1154 strcpy(dir, path);
1155 strncat(path, mask, sizeof(path) - 1);
1156
1157 // this structure will be used by libsmb callbacks, so we store here all we need
1158 // to fill netdrive structures
1159 state.pConn = pConn;
1160 state.plist = plist;
1161 state.ulAttribute = ulAttribute;
1162 strcpy( state.dir, dir);
1163 strcpy( state.dir_mask, mask);
1164 strcpy( state.mask, path);
1165 state.fullpath = szPath;
1166 /* This plugin always reads a complete directory listing and filters results
1167 * using actual mask (state.dir_mask) in getfindinfoL.
1168 * May be this was a workaround for some server bug.
1169 * If this will be changed, then directory listing cache must be changed too,
1170 * and must remember the mask, which was used to obtain a listing.
1171 * Now the directory cache saves complete directory listings and then uses them to find
1172 * information about single files.
1173 * However, with a directory cache, it is probably faster to get a full listing and
1174 * then use it to obtain info about separate files than to perform a network
1175 * list query operation using actual wild cards for each file. Some application,
1176 * for example OpenOffice, do this.
1177 */
1178 p = getlastslash(state.mask);
1179 if (p)
1180 {
1181 *(p + 1) = '*';
1182 *(p + 2) = 0;
1183 }
1184 else
1185 {
1186 strcpy(state.mask, "\\*");
1187 }
1188 debuglocal(9,"NdpFindStart: dir [%s], dir_mask [%s], mask [%s], szPath [%s]\n",
1189 state.dir, state.dir_mask, state.mask, state.fullpath);
1190
1191 /* use cli_echo to check the connection is alive - if not, reconnect */
1192 rc = smbwrp_echo(pConn->cli);
1193 if (rc)
1194 {
1195 debuglocal(4,"NdpFindStart, smbwrp_echo rc = %d, reconnecting.....\n",rc);
1196 int rcCon = smbwrp_connect( pRes, &pConn->cli);
1197 if (rcCon)
1198 return ERROR_PATH_NOT_FOUND;
1199 }
1200 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1201 // we need to handle reconnection also here, because NdpQueryPathInfo
1202 // could be called with '*' and exit then immediately (without calling libsmb)
1203 if (rc == ERROR_REM_NOT_LIST)
1204 {
1205 // free current cli resources
1206 smbwrp_disconnect( pRes, pConn->cli);
1207 // reconnect
1208 smbwrp_connect( pRes, &pConn->cli);
1209 // try file list again next loop
1210 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1211 debuglocal(9,"NdpFindStart remote connection lost, rc = %d\n", rc);
1212 }
1213
1214 debuglocal(9,"NdpFindStart <%s> (%s) cnt %d %d\n", szPath, path, count, rc);
1215 LEAVE();
1216 return rc;
1217}
1218
1219int APIENTRY NdpDeletePathInfo (HRESOURCE resource, NDFILEINFOL *pfi)
1220{
1221// debuglocal(9,"NdpDeletePathInfo %d\n", 0);
1222 return NO_ERROR;
1223}
1224
1225int APIENTRY NdpRefresh (HCONNECTION conn, char *path, int tree)
1226{
1227 debuglocal(9,"NdpRefresh <%s> %d\n", path, 0);
1228 return NO_ERROR;
1229}
1230
1231int APIENTRY NdpDiscardResourceData (HRESOURCE resource, NDDATABUF *pdatabuf)
1232{
1233 // The plugin do not have to deallocate anything
1234 // because resource data did not contain any pointers
1235 // to plugins data.
1236 // Data stored by fsphSetResourceData will be
1237 // deallocated by NetDrive.
1238
1239 debuglocal(9,"NdpDicardresourceData %d\n", 0);
1240 return NO_ERROR;
1241}
1242
1243int APIENTRY NdpSetPathInfo (HCONNECTION conn, NDFILEINFOL *pfi, char *szPathName)
1244{
1245 Connection *pConn = (Connection *)conn;
1246 Resource *pRes = pConn->pRes;
1247 int rc = 0;
1248 unsigned long action;
1249 char path[CCHMAXPATH+1] = {0};
1250 smbwrp_fileinfo finfo;
1251
1252 ENTER();
1253
1254 debug_printf("NdpSetPathInfo in [%p]\n", pConn);
1255
1256 // delete the dir cache
1257 dircache_invalidate(szPathName, pRes->pdc, 1);
1258
1259 do {
1260 rc = pathparser(pRes, pConn, szPathName, path);
1261 if (rc)
1262 {
1263 break;
1264 }
1265
1266 memset(&finfo, 0, sizeof(finfo));
1267
1268 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1269 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(finfo.mtime));
1270 finfo.attr = pfi->stat.attrFile & 0x37;
1271 rc = smbwrp_setattr(pConn->cli, &finfo);
1272 } while (0);
1273 debuglocal(9,"NdpSetPathInfo <%s> (%s) %d\n", szPathName, path, rc);
1274 LEAVE();
1275 return rc;
1276}
1277
1278int buildFEALIST(FEALIST *pFEASrc, GEALIST *pGEAList, FEALIST *pFEAList)
1279{
1280 int rc = 0;
1281 FEA * pfea;
1282 FEA * pfeadest;
1283 unsigned long size, done = sizeof(pFEAList->cbList), dsize, ddone = sizeof(pFEAList->cbList);
1284
1285 size = pFEASrc->cbList;
1286 pfea = pFEASrc->list;
1287 pfeadest = pFEAList->list;
1288 dsize = pFEAList->cbList;
1289//debuglocal(9,"buildFEALIST in destsize %d srcsize %d pGEAList=%08x pGEAList->cbList=%d\n", dsize, ddone, size, pGEAList, pGEAList ? pGEAList->cbList : 0);
1290 while (done < size)
1291 {
1292 char * name = (char *)(pfea + 1);
1293 int insert = 1;
1294 if (pGEAList && pGEAList->cbList > sizeof(pGEAList->cbList))
1295 {
1296 GEA * pgea = pGEAList->list;
1297 unsigned long size = pGEAList->cbList - sizeof(pGEAList->cbList), done = 0;
1298 insert = 0;
1299 while (done < size)
1300 {
1301//debuglocal(9,"comp <%s> <%s>\n", name, pgea->szName);
1302 if (!ph->fsphStrNCmp(name, pgea->szName, pgea->cbName))
1303 {
1304 insert = 1;
1305 break;
1306 }
1307 done += pgea->cbName + 2;
1308 pgea = (GEA *)((char *)pgea + pgea->cbName + 2);
1309 }
1310 }
1311 if (insert)
1312 {
1313 ddone += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1314 if (ddone <= dsize)
1315 {
1316 pfeadest->cbName = pfea->cbName;
1317 pfeadest->cbValue = pfea->cbValue;
1318 pfeadest->fEA = 0;
1319 strcpy((char *)(pfeadest + 1), name);
1320 memcpy((char *)(pfeadest + 1) + pfea->cbName + 1, (char *)(pfea + 1) + pfea->cbName + 1, pfea->cbValue);
1321 pfeadest = (FEA *)((char *)pFEAList + ddone);
1322 }
1323 }
1324 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1325//debuglocal(9,"buuildfea <%s> insert=%d pfea->cbName=%d pfea->cbValue=%d srcdone=%d destdone=%d pfeadest=%08x pfea=%08x\n", name, insert, pfea->cbName, pfea->cbValue, done, ddone, pfeadest, pfea);
1326 pfea = (FEA *)((char *)pFEASrc + done);
1327 }
1328 pFEAList->cbList = ddone;
1329 if (ddone > dsize && dsize > sizeof(pFEAList->cbList))
1330 {
1331 rc = ERROR_BUFFER_OVERFLOW;
1332 }
1333 debuglocal(9,"buildFEALIST rc=%d destsize=%d destdone=%d srcsize=%d pGEAList=%08x\n", rc, dsize, ddone, size, pGEAList);
1334 return rc;
1335}
1336
1337int APIENTRY NdpEAQuery (HCONNECTION conn, GEALIST *pGEAList, NDFILEINFOL *pfi, FEALIST *pFEAList)
1338{
1339 Connection *pConn = (Connection *)conn;
1340 Resource *pRes = pConn->pRes;
1341 int rc = 0;
1342 unsigned long action;
1343 char * path = NULL;
1344 FEALIST * pFEASrc;
1345 NDDATABUF fdata = {0};
1346 smbwrp_fileinfo *finfo;
1347 const int cbBuffer = 64*1024;
1348
1349 if (!pfi || !pfi->pszName || !pFEAList)
1350 {
1351 return ERROR_EAS_NOT_SUPPORTED;
1352 }
1353 if (!pRes->easupport)
1354 {
1355 return ERROR_EAS_NOT_SUPPORTED;
1356 }
1357
1358 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1359 if (rc || !fdata.ulSize || !fdata.pData)
1360 {
1361 debuglocal(9,"NdpEAQuery: ph->fsphGetFileInfoData = %d/%d %08x\n", rc, fdata.ulSize, fdata.pData);
1362 return ERROR_EAS_NOT_SUPPORTED;
1363 }
1364
1365 ENTER();
1366
1367 finfo = (smbwrp_fileinfo *)fdata.pData;
1368 path = finfo->fname;
1369
1370 debuglocal(9,"NdpEAQuery in [%p] <%s> %08x %d\n", pConn, path, pGEAList, pGEAList ? pGEAList->cbList : 0);
1371
1372 char *pchBuffer = (char *)malloc(cbBuffer);
1373 if (!pchBuffer)
1374 {
1375 LEAVE();
1376 return ERROR_NOT_ENOUGH_MEMORY;
1377 }
1378
1379 do {
1380 rc = smbwrp_listea( pConn->cli, path, pchBuffer, cbBuffer);
1381 pFEASrc = (FEALIST*) pchBuffer;
1382 if (rc)
1383 {
1384 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1385 switch (rc)
1386 {
1387 case ERROR_FILE_NOT_FOUND :
1388 case ERROR_PATH_NOT_FOUND :
1389 {
1390 pFEAList->cbList = sizeof(pFEAList->cbList);
1391 rc = NO_ERROR;
1392 } break;
1393 case ERROR_BUFFER_OVERFLOW :
1394 {
1395 pFEAList->cbList = pFEASrc->cbList;
1396 } break;
1397 default :
1398 {
1399 rc = ERROR_EAS_NOT_SUPPORTED;
1400 }
1401 }
1402 }
1403 else
1404 {
1405 rc = buildFEALIST((FEALIST *)pFEASrc, pGEAList, pFEAList);
1406 }
1407 } while (0);
1408 free(pchBuffer);
1409 debuglocal(9,"NdpEAQuery <%s> %d %d %d\n", pfi->pszName, rc, pFEASrc->cbList, pFEAList->cbList);
1410 LEAVE();
1411 return rc;
1412}
1413
1414int APIENTRY NdpEASet (HCONNECTION conn, FEALIST *pFEAList, NDFILEINFOL *pfi)
1415{
1416 Connection *pConn = (Connection *)conn;
1417 Resource *pRes = pConn->pRes;
1418 int rc = 0;
1419 char * path;
1420 unsigned long action;
1421 NDDATABUF fdata = {0};
1422 smbwrp_fileinfo *finfo;
1423
1424 debuglocal(9,"NdpEASet in [%p]\n", pConn);
1425
1426 if (!pfi || !pfi->pszName)
1427 {
1428 return ERROR_EAS_NOT_SUPPORTED;
1429 }
1430 if (!pRes->easupport)
1431 {
1432 return ERROR_EAS_NOT_SUPPORTED;
1433 }
1434
1435 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1436 if (rc || !fdata.ulSize || !fdata.pData)
1437 {
1438 debuglocal(9,"NdpEASet: ph->fsphGetFileInfoData = %d/%d/%08x\n", rc, fdata.ulSize, fdata.pData);
1439 return ERROR_EAS_NOT_SUPPORTED;
1440 }
1441
1442 finfo = (smbwrp_fileinfo *)fdata.pData;
1443 path = finfo->fname;
1444
1445 rc = helperEASet(pConn->cli, pFEAList, path);
1446 debuglocal(9,"NdpEASet %d\n", rc);
1447 return rc;
1448}
1449
1450int APIENTRY NdpEASize (HCONNECTION conn, NDFILEINFOL *pfi, ULONG *pulEASize)
1451{
1452 Connection *pConn = (Connection *)conn;
1453 Resource *pRes = pConn->pRes;
1454 int rc = 0;
1455 unsigned long action;
1456 char * path = NULL;
1457 FEALIST * pfealist;
1458 NDDATABUF fdata = {0};
1459 smbwrp_fileinfo *finfo;
1460 const int cbBuffer = 64*1024;
1461 int easize;
1462
1463 if (!pfi || !pulEASize)
1464 {
1465 return ERROR_EAS_NOT_SUPPORTED;
1466 }
1467 if (!pRes->easupport)
1468 {
1469 return ERROR_EAS_NOT_SUPPORTED;
1470 }
1471
1472 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1473 if (rc || !fdata.ulSize || !fdata.pData)
1474 {
1475 debuglocal(9,"NdpEASize: ph->fsphGetFileInfoData = %d/%d/%08x\n", rc, fdata.ulSize, fdata.pData);
1476 return ERROR_EAS_NOT_SUPPORTED;
1477 }
1478
1479 ENTER();
1480
1481 finfo = (smbwrp_fileinfo *)fdata.pData;
1482 easize = finfo->easize;
1483 finfo->easize = -1;
1484 path = finfo->fname;
1485 if (easize >= 0)
1486 {
1487 *pulEASize = easize;
1488 debuglocal(9,"NdpEASize <%s> cached %d\n", path, easize);
1489 LEAVE();
1490 return NO_ERROR;
1491 }
1492
1493 debuglocal(9,"NdpEASize in [%p] <%s> \n", pConn, path);
1494
1495 char *pchBuffer = (char *)malloc(cbBuffer);
1496 if (!pchBuffer)
1497 {
1498 LEAVE();
1499 return ERROR_NOT_ENOUGH_MEMORY;
1500 }
1501
1502 do {
1503 rc = smbwrp_listea(pConn->cli, path, pchBuffer, cbBuffer);
1504 pfealist = (FEALIST*)pchBuffer;
1505 if (rc)
1506 {
1507 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1508 switch (rc)
1509 {
1510 case ERROR_FILE_NOT_FOUND :
1511 case ERROR_PATH_NOT_FOUND :
1512 {
1513 pfealist->cbList = sizeof(pfealist->cbList);
1514 } /* Fall through */
1515 case ERROR_BUFFER_OVERFLOW :
1516 {
1517 rc = NO_ERROR;
1518 } break;
1519 default :
1520 {
1521 rc = ERROR_EAS_NOT_SUPPORTED;
1522 }
1523 }
1524 }
1525 *pulEASize = pfealist->cbList;
1526 } while (0);
1527 free(pchBuffer);
1528 debuglocal(9,"NdpEASize <%s> %d %d\n", pfi->pszName, *pulEASize, rc);
1529 LEAVE();
1530 return rc;
1531}
1532
1533int APIENTRY NdpSetCurrentDir (HCONNECTION conn, NDFILEINFOL *pfi, char *szPath)
1534{
1535 Connection *pConn = (Connection *)conn;
1536 Resource *pRes = pConn->pRes;
1537 int rc = 0;
1538 unsigned long action;
1539 char path[CCHMAXPATH+1] = {0};
1540
1541 debuglocal(9,"NdpSetCurrentDir in [%p]\n", pConn);
1542
1543 ENTER();
1544
1545 do {
1546 rc = pathparser(pRes, pConn, szPath, path);
1547 if (rc)
1548 {
1549 break;
1550 }
1551
1552 rc = smbwrp_chdir(&pRes->srv, pConn->cli, path);
1553 } while (0);
1554 debuglocal(9,"NdpSetCurrentDir <%s> (%s) %d\n", szPath, path, rc);
1555 LEAVE();
1556 return rc;
1557}
1558
1559int APIENTRY NdpCopy (HCONNECTION conn, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, ULONG ulOption)
1560{
1561 debuglocal(9,"NdpCopy <%s> -> <%s> %d\n", szSrc, szDst, ERROR_CANNOT_COPY);
1562 return ERROR_CANNOT_COPY;
1563}
1564
1565int APIENTRY NdpCopy2 (HCONNECTION conn, HRESOURCE resDst, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, ULONG ulOption)
1566{
1567 debuglocal(9,"NdpCopy2 <%s> -> <%s> %d\n", szSrc, szDst, ERROR_CANNOT_COPY);
1568 return ERROR_CANNOT_COPY;
1569}
1570
1571int APIENTRY NdpForceDelete (HCONNECTION conn, NDFILEINFOL *pfi, char *szFile)
1572{
1573 Connection *pConn = (Connection *)conn;
1574 Resource *pRes = pConn->pRes;
1575 int rc = 0;
1576 unsigned long action;
1577 char path[CCHMAXPATH+1] = {0};
1578
1579 ENTER();
1580
1581 debuglocal(9,"NdpForceDelete in [%p]\n", pConn);
1582
1583 dircache_invalidate(szFile, pRes->pdc, 1);
1584
1585 do {
1586 rc = pathparser(pRes, pConn, szFile, path);
1587 if (rc)
1588 {
1589 break;
1590 }
1591
1592 rc = smbwrp_unlink(pConn->cli, path);
1593 } while (0);
1594 debuglocal(9,"NdpForceDelete <%s> (%s) %d\n", szFile, path, rc);
1595 LEAVE();
1596 return rc;
1597}
1598
1599int APIENTRY NdpCreateDir (HCONNECTION conn, NDFILEINFOL *pfiparent, char *szDirName, FEALIST *pFEAList)
1600{
1601 Connection *pConn = (Connection *)conn;
1602 Resource *pRes = pConn->pRes;
1603 int rc = 0;
1604 int rcEASet = 0;
1605 unsigned long action;
1606 char path[CCHMAXPATH+1] = {0};
1607
1608 ENTER();
1609
1610 debuglocal(9,"NdpCreateDir in [%p]\n", pConn);
1611
1612 dircache_invalidate(szDirName, pRes->pdc, 1);
1613
1614 do {
1615 rc = pathparser(pRes, pConn, szDirName, path);
1616 if (rc)
1617 {
1618 break;
1619 }
1620
1621 rc = smbwrp_mkdir(pConn->cli, path);
1622 } while (0);
1623 LEAVE();
1624
1625 if (path && pRes->easupport)
1626 {
1627 rcEASet = helperEASet(pConn->cli, pFEAList, path);
1628 }
1629 debuglocal(9,"NdpCreateDir <%s> (%s) rc=%d, EASupport=%s rc=%d\n", szDirName, path, rc, pRes->easupport?"yes":"no", rcEASet);
1630
1631 return rc;
1632}
1633
1634int APIENTRY NdpDeleteDir (HCONNECTION conn, NDFILEINFOL *pfi, char *szDir)
1635{
1636 Connection *pConn = (Connection *)conn;
1637 Resource *pRes = pConn->pRes;
1638 int rc = 0;
1639 unsigned long action;
1640 char path[CCHMAXPATH+1] = {0};
1641
1642 ENTER();
1643
1644 debuglocal(9,"NdpDeleteDir in [%p]\n", pConn);
1645
1646 dircache_invalidate(szDir, pRes->pdc, 1);
1647
1648 do {
1649 rc = pathparser(pRes, pConn, szDir, path);
1650 if (rc)
1651 {
1652 break;
1653 }
1654
1655 rc = smbwrp_rmdir(pConn->cli, path);
1656 } while (0);
1657 debuglocal(9,"NdpDeleteDir <%s> (%s) %d\n", szDir, path, rc);
1658 LEAVE();
1659 return rc;
1660}
1661
1662int APIENTRY NdpMove (HCONNECTION conn, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc)
1663{
1664 Connection *pConn = (Connection *)conn;
1665 Resource *pRes = pConn->pRes;
1666 int rc = 0;
1667 unsigned long action;
1668 char src[CCHMAXPATH+1] = {0};
1669 int l1, l2;
1670 char * p = szDst;
1671
1672 ENTER();
1673
1674 debuglocal(9,"NdpMove in [%p] from <%s> to <%s>\n", pConn, szSrc, szDst);
1675
1676 dircache_invalidate(szSrc, pRes->pdc, 1);
1677 dircache_invalidate(szDst, pRes->pdc, 1);
1678
1679 do
1680 {
1681 rc = pathparser(pRes, pConn, szSrc, src);
1682 if (rc)
1683 {
1684 break;
1685 }
1686 l1 = strlen(szSrc);
1687 l2 = strlen(src);
1688 if (l1 > l2)
1689 {
1690 if (ph->fsphStrNICmp(szSrc, szDst, l1 - l2))
1691 {
1692 // the file moved accross different shares or servers or workgroups
1693 rc = ERROR_WRITE_PROTECT;
1694 break;
1695 }
1696 p = szDst + l1 - l2 + 1;
1697 }
1698 //pConn->mem[CCHMAXPATH + 1] = '\\';
1699 rc = smbwrp_rename(pConn->cli, src, p);
1700 } while (0);
1701 debuglocal(9,"NdpMove <%s> -> <%s> (%s) %d\n", szSrc, szDst, src, rc);
1702 LEAVE();
1703 return rc;
1704}
1705
1706int APIENTRY NdpMove2 (HCONNECTION conn, HRESOURCE resDst, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc)
1707{
1708 debuglocal(9,"NdpMove2 <%s> -> <%s> %d\n", szSrc, szDst, ERROR_WRITE_PROTECT);
1709 return ERROR_WRITE_PROTECT;
1710}
1711
1712
1713int APIENTRY NdpChangeCase (HCONNECTION conn, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, char *szNewName, ULONG ulNameLen)
1714{
1715 return NdpMove (conn, pfiSrc, szDst, pfiSrc, szSrc);
1716}
1717
1718int APIENTRY NdpRename (HCONNECTION conn, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, char *szNewName, ULONG ulNameLen)
1719{
1720 return NdpMove (conn, pfiSrc, szDst, pfiSrc, szSrc);
1721}
1722
1723int smbopen(Connection *pConn, char *szFileName, int flags, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1724{
1725 Resource *pRes = pConn->pRes;
1726 unsigned long action;
1727 int rc = 0;
1728 char path[CCHMAXPATH+1] = {0};
1729
1730 ENTER();
1731
1732 debuglocal(9,"smbopen in [%p] %d\n", pConn, pConn->file.fd);
1733
1734 if (flags & O_CREAT)
1735 {
1736 dircache_invalidate(szFileName, pRes->pdc, 1);
1737 }
1738 do {
1739 if (pConn->file.fd > 0)
1740 {
1741 rc = ERROR_TOO_MANY_OPEN_FILES;
1742 break;
1743 }
1744
1745 rc = pathparser(pRes, pConn, szFileName, path);
1746 if (rc)
1747 {
1748 break;
1749 }
1750
1751 strncpy(pConn->file.fullname, szFileName, sizeof(pConn->file.fullname) - 1);
1752 strncpy(pConn->file.fname, path, sizeof(pConn->file.fname) - 1);
1753 flags |= O_BINARY;
1754 switch (ulOpenMode & 3)
1755 {
1756 case OPEN_ACCESS_READONLY : flags |= O_RDONLY; break;
1757 case OPEN_ACCESS_WRITEONLY : flags |= O_WRONLY; break;
1758 case OPEN_ACCESS_READWRITE : flags |= O_RDWR; break;
1759 default : flags |= O_RDWR;
1760 }
1761 pConn->file.openmode = flags;
1762 pConn->file.openattr = ulAttribute & 0x37;
1763 pConn->file.denymode = (ulOpenMode & 0x70) >> 4;
1764 rc = smbwrp_open(pConn->cli, &pConn->file);
1765 } while (0);
1766 debuglocal(9,"smbopen <%s> (%s) %08x %08x %08x %d. file = %d\n", szFileName, path, flags, ulOpenMode, ulAttribute, rc, pConn->file.fd);
1767 if (!rc && pFEAList)
1768 {
1769 int rc1 = NdpFileEASet((HCONNECTION)pConn, (NDFILEHANDLE)0, pFEAList);
1770 debuglocal(9,"smbopen NdpFileEASet %d. pFEAList->cbList %d\n", rc1, pFEAList->cbList);
1771 }
1772 LEAVE();
1773 return rc;
1774}
1775
1776int APIENTRY NdpOpenReplace (HCONNECTION conn, NDFILEINFOL *pfi, NDFILEHANDLE *phandle, char *szFileName, ULONG ulSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1777{
1778 return smbopen((Connection *)conn, szFileName, O_TRUNC, ulOpenMode, ulAttribute, pFEAList);
1779}
1780
1781int APIENTRY NdpOpenReplaceL(HCONNECTION conn, NDFILEINFO *pfi, NDFILEHANDLE *phandle, char *szFileName, LONGLONG llSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1782{
1783 return smbopen((Connection *)conn, szFileName, O_TRUNC, ulOpenMode, ulAttribute, pFEAList);
1784}
1785
1786int APIENTRY NdpOpenCreate (HCONNECTION conn, NDFILEINFOL *pfiparent, NDFILEHANDLE *phandle, char *szFileName, ULONG ulSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1787{
1788// return smbopen((Connection *)conn, szFileName, O_CREAT, ulOpenMode, ulAttribute);
1789 return smbopen((Connection *)conn, szFileName, O_CREAT | O_EXCL, ulOpenMode, ulAttribute, pFEAList);
1790}
1791
1792int APIENTRY NdpOpenCreateL(HCONNECTION conn, NDFILEINFO *pfiparent, NDFILEHANDLE *phandle, char *szFileName, LONGLONG llSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1793{
1794 return smbopen((Connection *)conn, szFileName, O_CREAT | O_EXCL, ulOpenMode, ulAttribute, pFEAList);
1795}
1796
1797int APIENTRY NdpOpenExisting (HCONNECTION conn, NDFILEINFOL *pfi, NDFILEHANDLE *phandle, char *szFileName, ULONG ulOpenMode, USHORT *pfNeedEA)
1798{
1799 if (pfNeedEA) *pfNeedEA = 0; // wtf is this ?
1800 return smbopen((Connection *)conn, szFileName, 0, ulOpenMode, 0, NULL);
1801}
1802
1803int APIENTRY NdpSetFileAttribute (HCONNECTION conn, NDFILEINFOL *pfi, char *szFileName, USHORT usAttr)
1804{
1805 Connection *pConn = (Connection *)conn;
1806 Resource *pRes = pConn->pRes;
1807 int rc = 0;
1808 unsigned long action;
1809
1810 smbwrp_fileinfo finfo;
1811 char path[CCHMAXPATH+1] = {0};
1812
1813 ENTER();
1814
1815 debuglocal(9,"NdpSetFileAttribute in [%p]\n", pConn);
1816 do {
1817 rc = pathparser(pRes, pConn, szFileName, path);
1818 if (rc)
1819 {
1820 break;
1821 }
1822
1823 memset(&finfo, 0, sizeof(finfo));
1824 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1825 finfo.attr = usAttr & 0x37;
1826 rc = smbwrp_setattr(pConn->cli, &finfo);
1827 } while (0);
1828 debuglocal(9,"NdpSetFileAttribute <%s> (%s) %04x %d\n", szFileName, path, usAttr, rc);
1829 LEAVE();
1830 return rc;
1831}
1832
1833int APIENTRY NdpFlush (HRESOURCE resource)
1834{
1835 debuglocal(9,"NdpFlush %d\n", ERROR_NOT_SUPPORTED);
1836 return ERROR_NOT_SUPPORTED;
1837}
1838
1839// Called when a new thread will call the plugin. Allows to do per thread init, like disable signals.
1840#define ND_PL_INIT_THREAD 0xFFFF0000
1841
1842int APIENTRY NdpIOCTL (int type, HRESOURCE resource, char *path, int function, void *in, ULONG insize, PULONG poutlen)
1843{
1844 if (function == ND_PL_INIT_THREAD)
1845 {
1846 smbwrp_initthread();
1847 debuglocal(9, "NdpIOCTL init thread\n");
1848 return NO_ERROR;
1849 }
1850
1851 debuglocal(9,"NdpIOCTL <%s> %d\n", path, function);
1852
1853 if (in && insize > 4096)
1854 {
1855 char out[4096];
1856 sprintf (out, "SAMBA IOCTL function = %d, parms [%s] insize = %d, *poutlen = %d", function, in, insize, *poutlen);
1857 *poutlen = strlen(out);
1858 strcpy (in, out);
1859 return NO_ERROR;
1860 }
1861
1862 return ERROR_NOT_SUPPORTED;
1863}
1864
1865int APIENTRY NdpFileQueryInfo (HCONNECTION conn, NDFILEHANDLE handle, void *plist)
1866{
1867 Connection *pConn = (Connection *)conn;
1868 Resource *pRes = pConn->pRes;
1869 int rc = 0;
1870 unsigned long action;
1871 smbwrp_fileinfo finfo;
1872
1873 ENTER();
1874
1875 debug_printf("NdpFileQueryInfo in [%p]\n", pConn);
1876 do {
1877 if (pConn->file.fd < 0 || !*pConn->file.fname)
1878 {
1879 rc = ERROR_INVALID_HANDLE;
1880 break;
1881 }
1882 strncpy(finfo.fname, pConn->file.fname, sizeof(finfo.fname) - 1);
1883 rc = smbwrp_fgetattr(pConn->cli, &pConn->file, &finfo);
1884 if (!rc)
1885 {
1886 finfo.easize = -1;
1887 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1888 }
1889 } while (0);
1890 debuglocal(9,"NdpFileQueryInfo <%s> %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, rc);
1891 LEAVE();
1892 return rc;
1893}
1894
1895int APIENTRY NdpFileEAQuery (HCONNECTION conn, NDFILEHANDLE handle, GEALIST *pGEAList, FEALIST *pFEAList)
1896{
1897 Connection *pConn = (Connection *)conn;
1898 Resource *pRes = pConn->pRes;
1899 int rc = 0;
1900 unsigned long action;
1901 const int cbBuffer = 64*1024;
1902 FEALIST * pFEASrc;
1903
1904 if (!pFEAList)
1905 {
1906 return ERROR_EAS_NOT_SUPPORTED;
1907 }
1908 if (!pRes->easupport)
1909 {
1910 return ERROR_EAS_NOT_SUPPORTED;
1911 }
1912
1913 debuglocal(9,"NdpFileEAQuery in [%p] <%s>/%d pGEAList=%08x\n", pConn, pConn->file.fname, pConn->file.fd, pGEAList);
1914
1915 char *pchBuffer = (char *)malloc(cbBuffer);
1916 if (!pchBuffer)
1917 return ERROR_NOT_ENOUGH_MEMORY;
1918
1919 ENTER();
1920
1921 do {
1922 if (pConn->file.fd < 0)
1923 {
1924 rc = ERROR_INVALID_HANDLE;
1925 break;
1926 }
1927 rc = smbwrp_flistea(pConn->cli, &pConn->file, pchBuffer, cbBuffer);
1928 pFEASrc = (FEALIST *)pchBuffer;
1929 if (rc)
1930 {
1931 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1932 switch (rc)
1933 {
1934 case ERROR_FILE_NOT_FOUND :
1935 case ERROR_PATH_NOT_FOUND :
1936 {
1937 pFEAList->cbList = sizeof(pFEAList->cbList);
1938 rc = NO_ERROR;
1939 } break;
1940 case ERROR_BUFFER_OVERFLOW :
1941 {
1942 pFEAList->cbList = pFEASrc->cbList;
1943 } break;
1944 default :
1945 {
1946 rc = ERROR_EAS_NOT_SUPPORTED;
1947 }
1948 }
1949 }
1950 else
1951 {
1952 rc = buildFEALIST(pFEASrc, pGEAList, pFEAList);
1953 }
1954 } while (0);
1955 free(pchBuffer);
1956 debuglocal(9,"NdpFileEAQuery out <%s>/%d pFEASrc->cbList=%d pFEAList->cbList=%d rc=%d\n", pConn->file.fname, pConn->file.fd, pFEASrc->cbList, pFEAList->cbList, rc);
1957 LEAVE();
1958 return rc;
1959}
1960
1961int APIENTRY NdpFileEASet (HCONNECTION conn, NDFILEHANDLE handle, FEALIST *pFEAList)
1962{
1963 Connection *pConn = (Connection *)conn;
1964 Resource *pRes = pConn->pRes;
1965 int rc = 0;
1966 unsigned long action;
1967
1968 debuglocal(9,"NdpFileEASet in [%p]\n", pConn);
1969
1970 if (!pFEAList || pFEAList->cbList <= sizeof(long))
1971 {
1972 return ERROR_EAS_NOT_SUPPORTED;
1973 }
1974 if (!pRes->easupport)
1975 {
1976 return ERROR_EAS_NOT_SUPPORTED;
1977 }
1978
1979 ENTER();
1980
1981 do {
1982 // got FEA there
1983 FEA * pfea;
1984 unsigned long done = sizeof(long);
1985 if (pConn->file.fd < 0)
1986 {
1987 rc = ERROR_INVALID_HANDLE;
1988 break;
1989 }
1990
1991 pfea = pFEAList->list;
1992 while (done < pFEAList->cbList)
1993 {
1994 rc = smbwrp_fsetea(pConn->cli, &pConn->file, (char *)(pfea + 1), pfea->cbValue ? (char *)(pfea + 1) + pfea->cbName + 1: NULL, pfea->cbValue);
1995 if (rc)
1996 {
1997 break;
1998 }
1999 pfea = (FEA *)((char *)(pfea + 1) + pfea->cbName + 1 + pfea->cbValue);
2000 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
2001 }
2002
2003 } while (0);
2004 debuglocal(9,"NdpFileEASet %d\n", rc);
2005 LEAVE();
2006 return rc;
2007}
2008
2009int APIENTRY NdpFileEASize (HCONNECTION conn, NDFILEHANDLE handle, ULONG *pulEASize)
2010{
2011 Connection *pConn = (Connection *)conn;
2012 Resource *pRes = pConn->pRes;
2013 int rc = 0;
2014 unsigned long action;
2015 char path[CCHMAXPATH+1] = {0};
2016 FEALIST * pFEAList;
2017 const int cbBuffer = 64*1024;
2018
2019 if (!pulEASize)
2020 {
2021 return ERROR_EAS_NOT_SUPPORTED;
2022 }
2023 if (!pRes->easupport)
2024 {
2025 return ERROR_EAS_NOT_SUPPORTED;
2026 }
2027
2028 debuglocal(9,"NdpFileEASize in [%p] <%s>/%d \n", pConn, pConn->file.fname, pConn->file.fd);
2029
2030 char *pchBuffer = (char *)malloc(cbBuffer);
2031 if (!pchBuffer)
2032 return ERROR_NOT_ENOUGH_MEMORY;
2033
2034 ENTER();
2035
2036 do {
2037 if (pConn->file.fd < 0)
2038 {
2039 rc = ERROR_INVALID_HANDLE;
2040 break;
2041 }
2042 rc = smbwrp_flistea(pConn->cli, &pConn->file, pchBuffer, cbBuffer);
2043 pFEAList = (FEALIST*)pchBuffer;
2044 if (rc)
2045 {
2046 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
2047 switch (rc)
2048 {
2049 case ERROR_FILE_NOT_FOUND :
2050 case ERROR_PATH_NOT_FOUND :
2051 {
2052 pFEAList->cbList = sizeof(pFEAList->cbList);
2053 } /* Fall through */
2054 case ERROR_BUFFER_OVERFLOW :
2055 {
2056 rc = NO_ERROR;
2057 } break;
2058 default :
2059 {
2060 rc = ERROR_EAS_NOT_SUPPORTED;
2061 }
2062 }
2063 }
2064 *pulEASize = pFEAList->cbList;
2065 } while (0);
2066 free(pchBuffer);
2067 debuglocal(9,"NdpFileEASize %d %d\n", *pulEASize, rc);
2068 LEAVE();
2069 return rc;
2070}
2071
2072int APIENTRY NdpFileSetInfo (HCONNECTION conn, NDFILEHANDLE handle, NDFILEINFOL *pfi)
2073{
2074 Connection *pConn = (Connection *)conn;
2075 Resource *pRes = pConn->pRes;
2076 int rc = 0;
2077 unsigned long action, attrFile;
2078
2079 ENTER();
2080
2081 debug_printf("NdpFileSetInfo in [%p]\n", pConn);
2082
2083 // delete the dir cache
2084 dircache_invalidate(pConn->file.fullname, pRes->pdc, 1);
2085
2086 do {
2087 if (pConn->file.fd < 0 || !*pConn->file.fname)
2088 {
2089 rc = ERROR_INVALID_HANDLE;
2090 break;
2091 }
2092 attrFile = pfi->stat.attrFile;
2093 // deferred setinfo - on closing the file
2094 pConn->file.openattr = attrFile;
2095 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(pConn->file.mtime));
2096 fsphDosDateToUnixTime(pfi->stat.fdateCreation, pfi->stat.ftimeCreation, &(pConn->file.ctime));
2097 pConn->file.updatetime = 2;
2098 debug_printf("NdpFileSetInfo mtime %d\n", pConn->file.mtime);
2099 } while (0);
2100 debuglocal(9,"NdpFileSetInfo <%s> %08x %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, attrFile, rc);
2101 LEAVE();
2102 return NO_ERROR;
2103}
2104
2105int APIENTRY NdpFileSetFilePtrL(HCONNECTION conn, NDFILEHANDLE handle, LONGLONG llOffset, ULONG ulMethod, LONGLONG *pllActual)
2106{
2107 Connection *pConn = (Connection *)conn;
2108 Resource *pRes = pConn->pRes;
2109 int rc = 0;
2110 unsigned long action;
2111
2112 ENTER();
2113
2114 debuglocal(9,"NdpFileSetFilePtrL in [%p]\n", pConn);
2115
2116 do {
2117 if (pConn->file.fd < 0)
2118 {
2119 rc = ERROR_INVALID_HANDLE;
2120 break;
2121 }
2122
2123 rc = smbwrp_lseek(pConn->cli, &pConn->file, ulMethod, llOffset);
2124 if (!rc)
2125 *pllActual = pConn->file.offset;
2126
2127 } while (0);
2128 debuglocal(9,"NdpFileSetFilePtrL <%s> %lld %lu %lld %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, llOffset, ulMethod, *pllActual, rc);
2129 LEAVE();
2130 return rc;
2131}
2132
2133int APIENTRY NdpFileSetFilePtr (HCONNECTION conn, NDFILEHANDLE handle, LONG lOffset, ULONG ulMethod, ULONG *pulActual)
2134{
2135 LONGLONG llActual;
2136 int rc = NdpFileSetFilePtrL(conn, handle, lOffset, ulMethod, &llActual);
2137 *pulActual = llActual & 0xFFFFFFFF;
2138 debuglocal(9,"NdpFileSetFilePtr %ld %lu %ld %d\n", lOffset, ulMethod, *pulActual, rc);
2139 return rc;
2140}
2141
2142int APIENTRY NdpFileClose (HCONNECTION conn, NDFILEHANDLE handle)
2143{
2144 Connection *pConn = (Connection *)conn;
2145 Resource *pRes = pConn->pRes;
2146 int rc = 0;
2147 unsigned long action;
2148
2149 ENTER();
2150
2151 debuglocal(9,"NdpFileClose in [%p] %d <%s>\n", pConn, pConn->file.fd, pConn->file.fd < 0 ? "!null!" : pConn->file.fname);
2152
2153 do {
2154 if (pConn->file.fd < 0)
2155 {
2156 rc = ERROR_INVALID_HANDLE;
2157 break;
2158 }
2159
2160 rc = smbwrp_close(pConn->cli, &pConn->file);
2161
2162 } while (0);
2163 debuglocal(9,"NdpFileClose %d %d\n", pConn->file.fd, rc);
2164
2165 pConn->file.fd = -1;
2166 LEAVE();
2167 return rc;
2168}
2169
2170int APIENTRY NdpFileCommit (HCONNECTION conn, NDFILEHANDLE handle)
2171{
2172 debuglocal(9,"NdpFileCommit %d\n", NO_ERROR);
2173 return NO_ERROR;
2174}
2175
2176
2177int APIENTRY NdpFileNewSize (HCONNECTION conn, NDFILEHANDLE handle, ULONG ulLen)
2178{
2179 int rc = NdpFileNewSizeL(conn, handle, ulLen);
2180 debuglocal(9,"NdpFileNewSize %ld %d\n", ulLen, rc);
2181 return rc;
2182}
2183
2184int APIENTRY NdpFileNewSizeL(HCONNECTION conn, NDFILEHANDLE handle, LONGLONG llLen)
2185{
2186 Connection *pConn = (Connection *)conn;
2187 Resource *pRes = pConn->pRes;
2188 int rc = 0;
2189 unsigned long action;
2190
2191 ENTER();
2192
2193 debuglocal(9,"NdpFileNewSizeL in [%p]\n", pConn);
2194
2195 do {
2196 if (pConn->file.fd < 0)
2197 {
2198 rc = ERROR_INVALID_HANDLE;
2199 break;
2200 }
2201
2202 rc = smbwrp_setfilesize(pConn->cli, &pConn->file, llLen);
2203
2204 } while (0);
2205 debuglocal(9,"NdpFileNewSizeL <%s> %lld %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, llLen, rc);
2206 LEAVE();
2207 return rc;
2208}
2209
2210#define NDPSMB_READ_MAX_SIZE (65536 - 4096)
2211
2212int APIENTRY NdpFileRead (HCONNECTION conn, NDFILEHANDLE handle, void *pBuffer, ULONG ulRead, ULONG *pulActual)
2213{
2214 Connection *pConn = (Connection *)conn;
2215 Resource *pRes = pConn->pRes;
2216 int rc = 0;
2217 unsigned long done = 0;
2218 unsigned long onedone;
2219 unsigned long action;
2220 ULONG ulReadCompleted = 0;
2221
2222 ENTER();
2223
2224 debuglocal(9,"NdpFileRead in [%p]\n", pConn);
2225
2226 do {
2227 if (pConn->file.fd < 0)
2228 {
2229 rc = ERROR_INVALID_HANDLE;
2230 break;
2231 }
2232 while (ulReadCompleted < ulRead)
2233 {
2234 ULONG ulActual;
2235 ULONG ulToRead = ulRead - ulReadCompleted;
2236 debuglocal(9,"NdpFileRead completed %d, to read %d\n", ulReadCompleted, ulToRead);
2237 if (ulToRead > NDPSMB_READ_MAX_SIZE)
2238 {
2239 ulToRead = NDPSMB_READ_MAX_SIZE;
2240 }
2241 rc = smbwrp_read(pConn->cli, &pConn->file, (char *)pBuffer + ulReadCompleted, ulToRead, &ulActual);
2242 if (ulActual == 0 || rc != NO_ERROR)
2243 {
2244 break;
2245 }
2246 ulReadCompleted += ulActual;
2247 }
2248 //*pulActual = ulRead;
2249 //DosSleep(0);
2250
2251 } while (0);
2252
2253 if (ulReadCompleted > 0)
2254 {
2255 rc = NO_ERROR; /* Still were able to read some data. */
2256 }
2257
2258 if (rc == NO_ERROR)
2259 {
2260 *pulActual = ulReadCompleted;
2261 }
2262
2263 debuglocal(9,"NdpFileRead <%s> %lu %lu %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, ulRead, *pulActual, rc);
2264 LEAVE();
2265 return rc;
2266}
2267
2268int APIENTRY NdpFileWrite (HCONNECTION conn, NDFILEHANDLE handle, void *pBuffer, ULONG ulWrite, ULONG *pulActual)
2269{
2270 Connection *pConn = (Connection *)conn;
2271 Resource *pRes = pConn->pRes;
2272 int rc = 0;
2273 unsigned long done = 0;
2274 unsigned long onedone;
2275 unsigned long action;
2276
2277 ENTER();
2278
2279 debuglocal(9,"NdpFileWrite in [%p]\n", pConn);
2280
2281 /* delete the dir cache
2282 this was moved from NdpFileClose() becasue if there are a lot files in the tree all are reread
2283 the problem when moved to here is, that last accessed time is not refreshed
2284 if this is needed, a new function needs to be done to update only one file in the cache */
2285 dircache_invalidate(pConn->file.fullname, pRes->pdc, 1);
2286
2287 do {
2288 if (pConn->file.fd < 0)
2289 {
2290 rc = ERROR_INVALID_HANDLE;
2291 break;
2292 }
2293 rc = smbwrp_write(pConn->cli, &pConn->file, pBuffer, ulWrite, pulActual);
2294
2295 } while (0);
2296 debuglocal(9,"NdpFileWrite <%s> %lu %lu %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, ulWrite, *pulActual, rc);
2297 LEAVE();
2298 return rc;
2299}
2300
Note: See TracBrowser for help on using the repository browser.