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

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

Correct some debug messages

  • Property svn:eol-style set to native
File size: 54.9 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 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1191 // we need to handle reconnection also here, because NdpQueryPathInfo
1192 // could be called with '*' and exit then immediately (without calling libsmb)
1193 if (rc == ERROR_REM_NOT_LIST)
1194 {
1195 // free current cli resources
1196 smbwrp_disconnect( pRes, pConn->cli);
1197 // reconnect
1198 smbwrp_connect( pRes, &pConn->cli);
1199 // try file list again next loop
1200 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1201 debuglocal(9,"NdpFindStart remote connection lost, rc = %d\n", rc);
1202 }
1203
1204 debuglocal(9,"NdpFindStart <%s> (%s) cnt %d %d\n", szPath, path, count, rc);
1205 LEAVE();
1206 return rc;
1207}
1208
1209int APIENTRY NdpDeletePathInfo (HRESOURCE resource, NDFILEINFOL *pfi)
1210{
1211// debuglocal(9,"NdpDeletePathInfo %d\n", 0);
1212 return NO_ERROR;
1213}
1214
1215int APIENTRY NdpRefresh (HCONNECTION conn, char *path, int tree)
1216{
1217 debuglocal(9,"NdpRefresh <%s> %d\n", path, 0);
1218 return NO_ERROR;
1219}
1220
1221int APIENTRY NdpDiscardResourceData (HRESOURCE resource, NDDATABUF *pdatabuf)
1222{
1223 // The plugin do not have to deallocate anything
1224 // because resource data did not contain any pointers
1225 // to plugins data.
1226 // Data stored by fsphSetResourceData will be
1227 // deallocated by NetDrive.
1228
1229 debuglocal(9,"NdpDicardresourceData %d\n", 0);
1230 return NO_ERROR;
1231}
1232
1233int APIENTRY NdpSetPathInfo (HCONNECTION conn, NDFILEINFOL *pfi, char *szPathName)
1234{
1235 Connection *pConn = (Connection *)conn;
1236 Resource *pRes = pConn->pRes;
1237 int rc = 0;
1238 unsigned long action;
1239 char path[CCHMAXPATH+1] = {0};
1240 smbwrp_fileinfo finfo;
1241
1242 ENTER();
1243
1244 debug_printf("NdpSetPathInfo in [%p]\n", pConn);
1245
1246 // delete the dir cache
1247 dircache_invalidate(szPathName, pRes->pdc, 1);
1248
1249 do {
1250 rc = pathparser(pRes, pConn, szPathName, path);
1251 if (rc)
1252 {
1253 break;
1254 }
1255
1256 memset(&finfo, 0, sizeof(finfo));
1257
1258 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1259 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(finfo.mtime));
1260 finfo.attr = pfi->stat.attrFile & 0x37;
1261 rc = smbwrp_setattr(pConn->cli, &finfo);
1262 } while (0);
1263 debuglocal(9,"NdpSetPathInfo <%s> (%s) %d\n", szPathName, path, rc);
1264 LEAVE();
1265 return rc;
1266}
1267
1268int buildFEALIST(FEALIST *pFEASrc, GEALIST *pGEAList, FEALIST *pFEAList)
1269{
1270 int rc = 0;
1271 FEA * pfea;
1272 FEA * pfeadest;
1273 unsigned long size, done = sizeof(pFEAList->cbList), dsize, ddone = sizeof(pFEAList->cbList);
1274
1275 size = pFEASrc->cbList;
1276 pfea = pFEASrc->list;
1277 pfeadest = pFEAList->list;
1278 dsize = pFEAList->cbList;
1279//debuglocal(9,"buildFEALIST in destsize %d srcsize %d pGEAList=%08x pGEAList->cbList=%d\n", dsize, ddone, size, pGEAList, pGEAList ? pGEAList->cbList : 0);
1280 while (done < size)
1281 {
1282 char * name = (char *)(pfea + 1);
1283 int insert = 1;
1284 if (pGEAList && pGEAList->cbList > sizeof(pGEAList->cbList))
1285 {
1286 GEA * pgea = pGEAList->list;
1287 unsigned long size = pGEAList->cbList - sizeof(pGEAList->cbList), done = 0;
1288 insert = 0;
1289 while (done < size)
1290 {
1291//debuglocal(9,"comp <%s> <%s>\n", name, pgea->szName);
1292 if (!ph->fsphStrNCmp(name, pgea->szName, pgea->cbName))
1293 {
1294 insert = 1;
1295 break;
1296 }
1297 done += pgea->cbName + 2;
1298 pgea = (GEA *)((char *)pgea + pgea->cbName + 2);
1299 }
1300 }
1301 if (insert)
1302 {
1303 ddone += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1304 if (ddone <= dsize)
1305 {
1306 pfeadest->cbName = pfea->cbName;
1307 pfeadest->cbValue = pfea->cbValue;
1308 pfeadest->fEA = 0;
1309 strcpy((char *)(pfeadest + 1), name);
1310 memcpy((char *)(pfeadest + 1) + pfea->cbName + 1, (char *)(pfea + 1) + pfea->cbName + 1, pfea->cbValue);
1311 pfeadest = (FEA *)((char *)pFEAList + ddone);
1312 }
1313 }
1314 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1315//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);
1316 pfea = (FEA *)((char *)pFEASrc + done);
1317 }
1318 pFEAList->cbList = ddone;
1319 if (ddone > dsize && dsize > sizeof(pFEAList->cbList))
1320 {
1321 rc = ERROR_BUFFER_OVERFLOW;
1322 }
1323 debuglocal(9,"buildFEALIST rc=%d destsize=%d destdone=%d srcsize=%d pGEAList=%08x\n", rc, dsize, ddone, size, pGEAList);
1324 return rc;
1325}
1326
1327int APIENTRY NdpEAQuery (HCONNECTION conn, GEALIST *pGEAList, NDFILEINFOL *pfi, FEALIST *pFEAList)
1328{
1329 Connection *pConn = (Connection *)conn;
1330 Resource *pRes = pConn->pRes;
1331 int rc = 0;
1332 unsigned long action;
1333 char * path = NULL;
1334 FEALIST * pFEASrc;
1335 NDDATABUF fdata = {0};
1336 smbwrp_fileinfo *finfo;
1337 const int cbBuffer = 64*1024;
1338
1339 if (!pfi || !pfi->pszName || !pFEAList)
1340 {
1341 return ERROR_EAS_NOT_SUPPORTED;
1342 }
1343 if (!pRes->easupport)
1344 {
1345 return ERROR_EAS_NOT_SUPPORTED;
1346 }
1347
1348 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1349 if (rc || !fdata.ulSize || !fdata.pData)
1350 {
1351 debuglocal(9,"NdpEAQuery: ph->fsphGetFileInfoData = %d/%d %08x\n", rc, fdata.ulSize, fdata.pData);
1352 return ERROR_EAS_NOT_SUPPORTED;
1353 }
1354
1355 ENTER();
1356
1357 finfo = (smbwrp_fileinfo *)fdata.pData;
1358 path = finfo->fname;
1359
1360 debuglocal(9,"NdpEAQuery in [%p] <%s> %08x %d\n", pConn, path, pGEAList, pGEAList ? pGEAList->cbList : 0);
1361
1362 char *pchBuffer = (char *)malloc(cbBuffer);
1363 if (!pchBuffer)
1364 {
1365 LEAVE();
1366 return ERROR_NOT_ENOUGH_MEMORY;
1367 }
1368
1369 do {
1370 rc = smbwrp_listea( pConn->cli, path, pchBuffer, cbBuffer);
1371 pFEASrc = (FEALIST*) pchBuffer;
1372 if (rc)
1373 {
1374 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1375 switch (rc)
1376 {
1377 case ERROR_FILE_NOT_FOUND :
1378 case ERROR_PATH_NOT_FOUND :
1379 {
1380 pFEAList->cbList = sizeof(pFEAList->cbList);
1381 rc = NO_ERROR;
1382 } break;
1383 case ERROR_BUFFER_OVERFLOW :
1384 {
1385 pFEAList->cbList = pFEASrc->cbList;
1386 } break;
1387 default :
1388 {
1389 rc = ERROR_EAS_NOT_SUPPORTED;
1390 }
1391 }
1392 }
1393 else
1394 {
1395 rc = buildFEALIST((FEALIST *)pFEASrc, pGEAList, pFEAList);
1396 }
1397 } while (0);
1398 free(pchBuffer);
1399 debuglocal(9,"NdpEAQuery <%s> %d %d %d\n", pfi->pszName, rc, pFEASrc->cbList, pFEAList->cbList);
1400 LEAVE();
1401 return rc;
1402}
1403
1404int APIENTRY NdpEASet (HCONNECTION conn, FEALIST *pFEAList, NDFILEINFOL *pfi)
1405{
1406 Connection *pConn = (Connection *)conn;
1407 Resource *pRes = pConn->pRes;
1408 int rc = 0;
1409 char * path;
1410 unsigned long action;
1411 NDDATABUF fdata = {0};
1412 smbwrp_fileinfo *finfo;
1413
1414 debuglocal(9,"NdpEASet in [%p]\n", pConn);
1415
1416 if (!pfi || !pfi->pszName)
1417 {
1418 return ERROR_EAS_NOT_SUPPORTED;
1419 }
1420 if (!pRes->easupport)
1421 {
1422 return ERROR_EAS_NOT_SUPPORTED;
1423 }
1424
1425 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1426 if (rc || !fdata.ulSize || !fdata.pData)
1427 {
1428 debuglocal(9,"NdpEASet: ph->fsphGetFileInfoData = %d/%d/%08x\n", rc, fdata.ulSize, fdata.pData);
1429 return ERROR_EAS_NOT_SUPPORTED;
1430 }
1431
1432 finfo = (smbwrp_fileinfo *)fdata.pData;
1433 path = finfo->fname;
1434
1435 rc = helperEASet(pConn->cli, pFEAList, path);
1436 debuglocal(9,"NdpEASet %d\n", rc);
1437 return rc;
1438}
1439
1440int APIENTRY NdpEASize (HCONNECTION conn, NDFILEINFOL *pfi, ULONG *pulEASize)
1441{
1442 Connection *pConn = (Connection *)conn;
1443 Resource *pRes = pConn->pRes;
1444 int rc = 0;
1445 unsigned long action;
1446 char * path = NULL;
1447 FEALIST * pfealist;
1448 NDDATABUF fdata = {0};
1449 smbwrp_fileinfo *finfo;
1450 const int cbBuffer = 64*1024;
1451 int easize;
1452
1453 if (!pfi || !pulEASize)
1454 {
1455 return ERROR_EAS_NOT_SUPPORTED;
1456 }
1457 if (!pRes->easupport)
1458 {
1459 return ERROR_EAS_NOT_SUPPORTED;
1460 }
1461
1462 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1463 if (rc || !fdata.ulSize || !fdata.pData)
1464 {
1465 debuglocal(9,"NdpEASize: ph->fsphGetFileInfoData = %d/%d/%08x\n", rc, fdata.ulSize, fdata.pData);
1466 return ERROR_EAS_NOT_SUPPORTED;
1467 }
1468
1469 ENTER();
1470
1471 finfo = (smbwrp_fileinfo *)fdata.pData;
1472 easize = finfo->easize;
1473 finfo->easize = -1;
1474 path = finfo->fname;
1475 if (easize >= 0)
1476 {
1477 *pulEASize = easize;
1478 debuglocal(9,"NdpEASize <%s> cached %d\n", path, easize);
1479 LEAVE();
1480 return NO_ERROR;
1481 }
1482
1483 debuglocal(9,"NdpEASize in [%p] <%s> \n", pConn, path);
1484
1485 char *pchBuffer = (char *)malloc(cbBuffer);
1486 if (!pchBuffer)
1487 {
1488 LEAVE();
1489 return ERROR_NOT_ENOUGH_MEMORY;
1490 }
1491
1492 do {
1493 rc = smbwrp_listea(pConn->cli, path, pchBuffer, cbBuffer);
1494 pfealist = (FEALIST*)pchBuffer;
1495 if (rc)
1496 {
1497 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1498 switch (rc)
1499 {
1500 case ERROR_FILE_NOT_FOUND :
1501 case ERROR_PATH_NOT_FOUND :
1502 {
1503 pfealist->cbList = sizeof(pfealist->cbList);
1504 } /* Fall through */
1505 case ERROR_BUFFER_OVERFLOW :
1506 {
1507 rc = NO_ERROR;
1508 } break;
1509 default :
1510 {
1511 rc = ERROR_EAS_NOT_SUPPORTED;
1512 }
1513 }
1514 }
1515 *pulEASize = pfealist->cbList;
1516 } while (0);
1517 free(pchBuffer);
1518 debuglocal(9,"NdpEASize <%s> %d %d\n", pfi->pszName, *pulEASize, rc);
1519 LEAVE();
1520 return rc;
1521}
1522
1523int APIENTRY NdpSetCurrentDir (HCONNECTION conn, NDFILEINFOL *pfi, char *szPath)
1524{
1525 Connection *pConn = (Connection *)conn;
1526 Resource *pRes = pConn->pRes;
1527 int rc = 0;
1528 unsigned long action;
1529 char path[CCHMAXPATH+1] = {0};
1530
1531 debuglocal(9,"NdpSetCurrentDir in [%p]\n", pConn);
1532
1533 ENTER();
1534
1535 do {
1536 rc = pathparser(pRes, pConn, szPath, path);
1537 if (rc)
1538 {
1539 break;
1540 }
1541
1542 rc = smbwrp_chdir(&pRes->srv, pConn->cli, path);
1543 } while (0);
1544 debuglocal(9,"NdpSetCurrentDir <%s> (%s) %d\n", szPath, path, rc);
1545 LEAVE();
1546 return rc;
1547}
1548
1549int APIENTRY NdpCopy (HCONNECTION conn, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, ULONG ulOption)
1550{
1551 debuglocal(9,"NdpCopy <%s> -> <%s> %d\n", szSrc, szDst, ERROR_CANNOT_COPY);
1552 return ERROR_CANNOT_COPY;
1553}
1554
1555int APIENTRY NdpCopy2 (HCONNECTION conn, HRESOURCE resDst, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, ULONG ulOption)
1556{
1557 debuglocal(9,"NdpCopy2 <%s> -> <%s> %d\n", szSrc, szDst, ERROR_CANNOT_COPY);
1558 return ERROR_CANNOT_COPY;
1559}
1560
1561int APIENTRY NdpForceDelete (HCONNECTION conn, NDFILEINFOL *pfi, char *szFile)
1562{
1563 Connection *pConn = (Connection *)conn;
1564 Resource *pRes = pConn->pRes;
1565 int rc = 0;
1566 unsigned long action;
1567 char path[CCHMAXPATH+1] = {0};
1568
1569 ENTER();
1570
1571 debuglocal(9,"NdpForceDelete in [%p]\n", pConn);
1572
1573 dircache_invalidate(szFile, pRes->pdc, 1);
1574
1575 do {
1576 rc = pathparser(pRes, pConn, szFile, path);
1577 if (rc)
1578 {
1579 break;
1580 }
1581
1582 rc = smbwrp_unlink(pConn->cli, path);
1583 } while (0);
1584 debuglocal(9,"NdpForceDelete <%s> (%s) %d\n", szFile, path, rc);
1585 LEAVE();
1586 return rc;
1587}
1588
1589int APIENTRY NdpCreateDir (HCONNECTION conn, NDFILEINFOL *pfiparent, char *szDirName, FEALIST *pFEAList)
1590{
1591 Connection *pConn = (Connection *)conn;
1592 Resource *pRes = pConn->pRes;
1593 int rc = 0;
1594 int rcEASet = 0;
1595 unsigned long action;
1596 char path[CCHMAXPATH+1] = {0};
1597
1598 ENTER();
1599
1600 debuglocal(9,"NdpCreateDir in [%p]\n", pConn);
1601
1602 dircache_invalidate(szDirName, pRes->pdc, 1);
1603
1604 do {
1605 rc = pathparser(pRes, pConn, szDirName, path);
1606 if (rc)
1607 {
1608 break;
1609 }
1610
1611 rc = smbwrp_mkdir(pConn->cli, path);
1612 } while (0);
1613 LEAVE();
1614
1615 if (path && pRes->easupport)
1616 {
1617 rcEASet = helperEASet(pConn->cli, pFEAList, path);
1618 }
1619 debuglocal(9,"NdpCreateDir <%s> (%s) rc=%d, EASupport=%s rc=%d\n", szDirName, path, rc, pRes->easupport?"yes":"no", rcEASet);
1620
1621 return rc;
1622}
1623
1624int APIENTRY NdpDeleteDir (HCONNECTION conn, NDFILEINFOL *pfi, char *szDir)
1625{
1626 Connection *pConn = (Connection *)conn;
1627 Resource *pRes = pConn->pRes;
1628 int rc = 0;
1629 unsigned long action;
1630 char path[CCHMAXPATH+1] = {0};
1631
1632 ENTER();
1633
1634 debuglocal(9,"NdpDeleteDir in [%p]\n", pConn);
1635
1636 dircache_invalidate(szDir, pRes->pdc, 1);
1637
1638 do {
1639 rc = pathparser(pRes, pConn, szDir, path);
1640 if (rc)
1641 {
1642 break;
1643 }
1644
1645 rc = smbwrp_rmdir(pConn->cli, path);
1646 } while (0);
1647 debuglocal(9,"NdpDeleteDir <%s> (%s) %d\n", szDir, path, rc);
1648 LEAVE();
1649 return rc;
1650}
1651
1652int APIENTRY NdpMove (HCONNECTION conn, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc)
1653{
1654 Connection *pConn = (Connection *)conn;
1655 Resource *pRes = pConn->pRes;
1656 int rc = 0;
1657 unsigned long action;
1658 char src[CCHMAXPATH+1] = {0};
1659 int l1, l2;
1660 char * p = szDst;
1661
1662 ENTER();
1663
1664 debuglocal(9,"NdpMove in [%p] from <%s> to <%s>\n", pConn, szSrc, szDst);
1665
1666 dircache_invalidate(szSrc, pRes->pdc, 1);
1667 dircache_invalidate(szDst, pRes->pdc, 1);
1668
1669 do
1670 {
1671 rc = pathparser(pRes, pConn, szSrc, src);
1672 if (rc)
1673 {
1674 break;
1675 }
1676 l1 = strlen(szSrc);
1677 l2 = strlen(src);
1678 if (l1 > l2)
1679 {
1680 if (ph->fsphStrNICmp(szSrc, szDst, l1 - l2))
1681 {
1682 // the file moved accross different shares or servers or workgroups
1683 rc = ERROR_WRITE_PROTECT;
1684 break;
1685 }
1686 p = szDst + l1 - l2 + 1;
1687 }
1688 //pConn->mem[CCHMAXPATH + 1] = '\\';
1689 rc = smbwrp_rename(pConn->cli, src, p);
1690 } while (0);
1691 debuglocal(9,"NdpMove <%s> -> <%s> (%s) %d\n", szSrc, szDst, src, rc);
1692 LEAVE();
1693 return rc;
1694}
1695
1696int APIENTRY NdpMove2 (HCONNECTION conn, HRESOURCE resDst, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc)
1697{
1698 debuglocal(9,"NdpMove2 <%s> -> <%s> %d\n", szSrc, szDst, ERROR_WRITE_PROTECT);
1699 return ERROR_WRITE_PROTECT;
1700}
1701
1702
1703int APIENTRY NdpChangeCase (HCONNECTION conn, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, char *szNewName, ULONG ulNameLen)
1704{
1705 return NdpMove (conn, pfiSrc, szDst, pfiSrc, szSrc);
1706}
1707
1708int APIENTRY NdpRename (HCONNECTION conn, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, char *szNewName, ULONG ulNameLen)
1709{
1710 return NdpMove (conn, pfiSrc, szDst, pfiSrc, szSrc);
1711}
1712
1713int smbopen(Connection *pConn, char *szFileName, int flags, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1714{
1715 Resource *pRes = pConn->pRes;
1716 unsigned long action;
1717 int rc = 0;
1718 char path[CCHMAXPATH+1] = {0};
1719
1720 ENTER();
1721
1722 debuglocal(9,"smbopen in [%p] %d\n", pConn, pConn->file.fd);
1723
1724 if (flags & O_CREAT)
1725 {
1726 dircache_invalidate(szFileName, pRes->pdc, 1);
1727 }
1728 do {
1729 if (pConn->file.fd > 0)
1730 {
1731 rc = ERROR_TOO_MANY_OPEN_FILES;
1732 break;
1733 }
1734
1735 rc = pathparser(pRes, pConn, szFileName, path);
1736 if (rc)
1737 {
1738 break;
1739 }
1740
1741 strncpy(pConn->file.fullname, szFileName, sizeof(pConn->file.fullname) - 1);
1742 strncpy(pConn->file.fname, path, sizeof(pConn->file.fname) - 1);
1743 flags |= O_BINARY;
1744 switch (ulOpenMode & 3)
1745 {
1746 case OPEN_ACCESS_READONLY : flags |= O_RDONLY; break;
1747 case OPEN_ACCESS_WRITEONLY : flags |= O_WRONLY; break;
1748 case OPEN_ACCESS_READWRITE : flags |= O_RDWR; break;
1749 default : flags |= O_RDWR;
1750 }
1751 pConn->file.openmode = flags;
1752 pConn->file.openattr = ulAttribute & 0x37;
1753 pConn->file.denymode = (ulOpenMode & 0x70) >> 4;
1754 rc = smbwrp_open(pConn->cli, &pConn->file);
1755 } while (0);
1756 debuglocal(9,"smbopen <%s> (%s) %08x %08x %08x %d. file = %d\n", szFileName, path, flags, ulOpenMode, ulAttribute, rc, pConn->file.fd);
1757 if (!rc && pFEAList)
1758 {
1759 int rc1 = NdpFileEASet((HCONNECTION)pConn, (NDFILEHANDLE)0, pFEAList);
1760 debuglocal(9,"smbopen NdpFileEASet %d. pFEAList->cbList %d\n", rc1, pFEAList->cbList);
1761 }
1762 LEAVE();
1763 return rc;
1764}
1765
1766int APIENTRY NdpOpenReplace (HCONNECTION conn, NDFILEINFOL *pfi, NDFILEHANDLE *phandle, char *szFileName, ULONG ulSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1767{
1768 return smbopen((Connection *)conn, szFileName, O_TRUNC, ulOpenMode, ulAttribute, pFEAList);
1769}
1770
1771int APIENTRY NdpOpenReplaceL(HCONNECTION conn, NDFILEINFO *pfi, NDFILEHANDLE *phandle, char *szFileName, LONGLONG llSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1772{
1773 return smbopen((Connection *)conn, szFileName, O_TRUNC, ulOpenMode, ulAttribute, pFEAList);
1774}
1775
1776int APIENTRY NdpOpenCreate (HCONNECTION conn, NDFILEINFOL *pfiparent, NDFILEHANDLE *phandle, char *szFileName, ULONG ulSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1777{
1778// return smbopen((Connection *)conn, szFileName, O_CREAT, ulOpenMode, ulAttribute);
1779 return smbopen((Connection *)conn, szFileName, O_CREAT | O_EXCL, ulOpenMode, ulAttribute, pFEAList);
1780}
1781
1782int APIENTRY NdpOpenCreateL(HCONNECTION conn, NDFILEINFO *pfiparent, NDFILEHANDLE *phandle, char *szFileName, LONGLONG llSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1783{
1784 return smbopen((Connection *)conn, szFileName, O_CREAT | O_EXCL, ulOpenMode, ulAttribute, pFEAList);
1785}
1786
1787int APIENTRY NdpOpenExisting (HCONNECTION conn, NDFILEINFOL *pfi, NDFILEHANDLE *phandle, char *szFileName, ULONG ulOpenMode, USHORT *pfNeedEA)
1788{
1789 if (pfNeedEA) *pfNeedEA = 0; // wtf is this ?
1790 return smbopen((Connection *)conn, szFileName, 0, ulOpenMode, 0, NULL);
1791}
1792
1793int APIENTRY NdpSetFileAttribute (HCONNECTION conn, NDFILEINFOL *pfi, char *szFileName, USHORT usAttr)
1794{
1795 Connection *pConn = (Connection *)conn;
1796 Resource *pRes = pConn->pRes;
1797 int rc = 0;
1798 unsigned long action;
1799
1800 smbwrp_fileinfo finfo;
1801 char path[CCHMAXPATH+1] = {0};
1802
1803 ENTER();
1804
1805 debuglocal(9,"NdpSetFileAttribute in [%p]\n", pConn);
1806 do {
1807 rc = pathparser(pRes, pConn, szFileName, path);
1808 if (rc)
1809 {
1810 break;
1811 }
1812
1813 memset(&finfo, 0, sizeof(finfo));
1814 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1815 finfo.attr = usAttr & 0x37;
1816 rc = smbwrp_setattr(pConn->cli, &finfo);
1817 } while (0);
1818 debuglocal(9,"NdpSetFileAttribute <%s> (%s) %04x %d\n", szFileName, path, usAttr, rc);
1819 LEAVE();
1820 return rc;
1821}
1822
1823int APIENTRY NdpFlush (HRESOURCE resource)
1824{
1825 debuglocal(9,"NdpFlush %d\n", ERROR_NOT_SUPPORTED);
1826 return ERROR_NOT_SUPPORTED;
1827}
1828
1829// Called when a new thread will call the plugin. Allows to do per thread init, like disable signals.
1830#define ND_PL_INIT_THREAD 0xFFFF0000
1831
1832int APIENTRY NdpIOCTL (int type, HRESOURCE resource, char *path, int function, void *in, ULONG insize, PULONG poutlen)
1833{
1834 if (function == ND_PL_INIT_THREAD)
1835 {
1836 smbwrp_initthread();
1837 debuglocal(9, "NdpIOCTL init thread\n");
1838 return NO_ERROR;
1839 }
1840
1841 debuglocal(9,"NdpIOCTL <%s> %d\n", path, function);
1842
1843 if (in && insize > 4096)
1844 {
1845 char out[4096];
1846 sprintf (out, "SAMBA IOCTL function = %d, parms [%s] insize = %d, *poutlen = %d", function, in, insize, *poutlen);
1847 *poutlen = strlen(out);
1848 strcpy (in, out);
1849 return NO_ERROR;
1850 }
1851
1852 return ERROR_NOT_SUPPORTED;
1853}
1854
1855int APIENTRY NdpFileQueryInfo (HCONNECTION conn, NDFILEHANDLE handle, void *plist)
1856{
1857 Connection *pConn = (Connection *)conn;
1858 Resource *pRes = pConn->pRes;
1859 int rc = 0;
1860 unsigned long action;
1861 smbwrp_fileinfo finfo;
1862
1863 ENTER();
1864
1865 debug_printf("NdpFileQueryInfo in [%p]\n", pConn);
1866 do {
1867 if (pConn->file.fd < 0 || !*pConn->file.fname)
1868 {
1869 rc = ERROR_INVALID_HANDLE;
1870 break;
1871 }
1872 strncpy(finfo.fname, pConn->file.fname, sizeof(finfo.fname) - 1);
1873 rc = smbwrp_fgetattr(pConn->cli, &pConn->file, &finfo);
1874 if (!rc)
1875 {
1876 finfo.easize = -1;
1877 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1878 }
1879 } while (0);
1880 debuglocal(9,"NdpFileQueryInfo <%s> %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, rc);
1881 LEAVE();
1882 return rc;
1883}
1884
1885int APIENTRY NdpFileEAQuery (HCONNECTION conn, NDFILEHANDLE handle, GEALIST *pGEAList, FEALIST *pFEAList)
1886{
1887 Connection *pConn = (Connection *)conn;
1888 Resource *pRes = pConn->pRes;
1889 int rc = 0;
1890 unsigned long action;
1891 const int cbBuffer = 64*1024;
1892 FEALIST * pFEASrc;
1893
1894 if (!pFEAList)
1895 {
1896 return ERROR_EAS_NOT_SUPPORTED;
1897 }
1898 if (!pRes->easupport)
1899 {
1900 return ERROR_EAS_NOT_SUPPORTED;
1901 }
1902
1903 debuglocal(9,"NdpFileEAQuery in [%p] <%s>/%d pGEAList=%08x\n", pConn, pConn->file.fname, pConn->file.fd, pGEAList);
1904
1905 char *pchBuffer = (char *)malloc(cbBuffer);
1906 if (!pchBuffer)
1907 return ERROR_NOT_ENOUGH_MEMORY;
1908
1909 ENTER();
1910
1911 do {
1912 if (pConn->file.fd < 0)
1913 {
1914 rc = ERROR_INVALID_HANDLE;
1915 break;
1916 }
1917 rc = smbwrp_flistea(pConn->cli, &pConn->file, pchBuffer, cbBuffer);
1918 pFEASrc = (FEALIST *)pchBuffer;
1919 if (rc)
1920 {
1921 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1922 switch (rc)
1923 {
1924 case ERROR_FILE_NOT_FOUND :
1925 case ERROR_PATH_NOT_FOUND :
1926 {
1927 pFEAList->cbList = sizeof(pFEAList->cbList);
1928 rc = NO_ERROR;
1929 } break;
1930 case ERROR_BUFFER_OVERFLOW :
1931 {
1932 pFEAList->cbList = pFEASrc->cbList;
1933 } break;
1934 default :
1935 {
1936 rc = ERROR_EAS_NOT_SUPPORTED;
1937 }
1938 }
1939 }
1940 else
1941 {
1942 rc = buildFEALIST(pFEASrc, pGEAList, pFEAList);
1943 }
1944 } while (0);
1945 free(pchBuffer);
1946 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);
1947 LEAVE();
1948 return rc;
1949}
1950
1951int APIENTRY NdpFileEASet (HCONNECTION conn, NDFILEHANDLE handle, FEALIST *pFEAList)
1952{
1953 Connection *pConn = (Connection *)conn;
1954 Resource *pRes = pConn->pRes;
1955 int rc = 0;
1956 unsigned long action;
1957
1958 debuglocal(9,"NdpFileEASet in [%p]\n", pConn);
1959
1960 if (!pFEAList || pFEAList->cbList <= sizeof(long))
1961 {
1962 return ERROR_EAS_NOT_SUPPORTED;
1963 }
1964 if (!pRes->easupport)
1965 {
1966 return ERROR_EAS_NOT_SUPPORTED;
1967 }
1968
1969 ENTER();
1970
1971 do {
1972 // got FEA there
1973 FEA * pfea;
1974 unsigned long done = sizeof(long);
1975 if (pConn->file.fd < 0)
1976 {
1977 rc = ERROR_INVALID_HANDLE;
1978 break;
1979 }
1980
1981 pfea = pFEAList->list;
1982 while (done < pFEAList->cbList)
1983 {
1984 rc = smbwrp_fsetea(pConn->cli, &pConn->file, (char *)(pfea + 1), pfea->cbValue ? (char *)(pfea + 1) + pfea->cbName + 1: NULL, pfea->cbValue);
1985 if (rc)
1986 {
1987 break;
1988 }
1989 pfea = (FEA *)((char *)(pfea + 1) + pfea->cbName + 1 + pfea->cbValue);
1990 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1991 }
1992
1993 } while (0);
1994 debuglocal(9,"NdpFileEASet %d\n", rc);
1995 LEAVE();
1996 return rc;
1997}
1998
1999int APIENTRY NdpFileEASize (HCONNECTION conn, NDFILEHANDLE handle, ULONG *pulEASize)
2000{
2001 Connection *pConn = (Connection *)conn;
2002 Resource *pRes = pConn->pRes;
2003 int rc = 0;
2004 unsigned long action;
2005 char path[CCHMAXPATH+1] = {0};
2006 FEALIST * pFEAList;
2007 const int cbBuffer = 64*1024;
2008
2009 if (!pulEASize)
2010 {
2011 return ERROR_EAS_NOT_SUPPORTED;
2012 }
2013 if (!pRes->easupport)
2014 {
2015 return ERROR_EAS_NOT_SUPPORTED;
2016 }
2017
2018 debuglocal(9,"NdpFileEASize in [%p] <%s>/%d \n", pConn, pConn->file.fname, pConn->file.fd);
2019
2020 char *pchBuffer = (char *)malloc(cbBuffer);
2021 if (!pchBuffer)
2022 return ERROR_NOT_ENOUGH_MEMORY;
2023
2024 ENTER();
2025
2026 do {
2027 if (pConn->file.fd < 0)
2028 {
2029 rc = ERROR_INVALID_HANDLE;
2030 break;
2031 }
2032 rc = smbwrp_flistea(pConn->cli, &pConn->file, pchBuffer, cbBuffer);
2033 pFEAList = (FEALIST*)pchBuffer;
2034 if (rc)
2035 {
2036 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
2037 switch (rc)
2038 {
2039 case ERROR_FILE_NOT_FOUND :
2040 case ERROR_PATH_NOT_FOUND :
2041 {
2042 pFEAList->cbList = sizeof(pFEAList->cbList);
2043 } /* Fall through */
2044 case ERROR_BUFFER_OVERFLOW :
2045 {
2046 rc = NO_ERROR;
2047 } break;
2048 default :
2049 {
2050 rc = ERROR_EAS_NOT_SUPPORTED;
2051 }
2052 }
2053 }
2054 *pulEASize = pFEAList->cbList;
2055 } while (0);
2056 free(pchBuffer);
2057 debuglocal(9,"NdpFileEASize %d %d\n", *pulEASize, rc);
2058 LEAVE();
2059 return rc;
2060}
2061
2062int APIENTRY NdpFileSetInfo (HCONNECTION conn, NDFILEHANDLE handle, NDFILEINFOL *pfi)
2063{
2064 Connection *pConn = (Connection *)conn;
2065 Resource *pRes = pConn->pRes;
2066 int rc = 0;
2067 unsigned long action, attrFile;
2068
2069 ENTER();
2070
2071 debug_printf("NdpFileSetInfo in [%p]\n", pConn);
2072
2073 // delete the dir cache
2074 dircache_invalidate(pConn->file.fullname, pRes->pdc, 1);
2075
2076 do {
2077 if (pConn->file.fd < 0 || !*pConn->file.fname)
2078 {
2079 rc = ERROR_INVALID_HANDLE;
2080 break;
2081 }
2082 attrFile = pfi->stat.attrFile;
2083 // deferred setinfo - on closing the file
2084 pConn->file.openattr = attrFile;
2085 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(pConn->file.mtime));
2086 fsphDosDateToUnixTime(pfi->stat.fdateCreation, pfi->stat.ftimeCreation, &(pConn->file.ctime));
2087 pConn->file.updatetime = 2;
2088 debug_printf("NdpFileSetInfo mtime %d\n", pConn->file.mtime);
2089 } while (0);
2090 debuglocal(9,"NdpFileSetInfo <%s> %08x %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, attrFile, rc);
2091 LEAVE();
2092 return NO_ERROR;
2093}
2094
2095int APIENTRY NdpFileSetFilePtrL(HCONNECTION conn, NDFILEHANDLE handle, LONGLONG llOffset, ULONG ulMethod, LONGLONG *pllActual)
2096{
2097 Connection *pConn = (Connection *)conn;
2098 Resource *pRes = pConn->pRes;
2099 int rc = 0;
2100 unsigned long action;
2101
2102 ENTER();
2103
2104 debuglocal(9,"NdpFileSetFilePtrL in [%p]\n", pConn);
2105
2106 do {
2107 if (pConn->file.fd < 0)
2108 {
2109 rc = ERROR_INVALID_HANDLE;
2110 break;
2111 }
2112
2113 rc = smbwrp_lseek(pConn->cli, &pConn->file, ulMethod, llOffset);
2114 if (!rc)
2115 *pllActual = pConn->file.offset;
2116
2117 } while (0);
2118 debuglocal(9,"NdpFileSetFilePtrL <%s> %lld %lu %lld %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, llOffset, ulMethod, *pllActual, rc);
2119 LEAVE();
2120 return rc;
2121}
2122
2123int APIENTRY NdpFileSetFilePtr (HCONNECTION conn, NDFILEHANDLE handle, LONG lOffset, ULONG ulMethod, ULONG *pulActual)
2124{
2125 LONGLONG llActual;
2126 int rc = NdpFileSetFilePtrL(conn, handle, lOffset, ulMethod, &llActual);
2127 *pulActual = llActual & 0xFFFFFFFF;
2128 debuglocal(9,"NdpFileSetFilePtr %ld %lu %ld %d\n", lOffset, ulMethod, *pulActual, rc);
2129 return rc;
2130}
2131
2132int APIENTRY NdpFileClose (HCONNECTION conn, NDFILEHANDLE handle)
2133{
2134 Connection *pConn = (Connection *)conn;
2135 Resource *pRes = pConn->pRes;
2136 int rc = 0;
2137 unsigned long action;
2138
2139 ENTER();
2140
2141 debuglocal(9,"NdpFileClose in [%p] %d <%s>\n", pConn, pConn->file.fd, pConn->file.fd < 0 ? "!null!" : pConn->file.fname);
2142
2143 do {
2144 if (pConn->file.fd < 0)
2145 {
2146 rc = ERROR_INVALID_HANDLE;
2147 break;
2148 }
2149
2150 rc = smbwrp_close(pConn->cli, &pConn->file);
2151
2152 } while (0);
2153 debuglocal(9,"NdpFileClose %d %d\n", pConn->file.fd, rc);
2154
2155 pConn->file.fd = -1;
2156 LEAVE();
2157 return rc;
2158}
2159
2160int APIENTRY NdpFileCommit (HCONNECTION conn, NDFILEHANDLE handle)
2161{
2162 debuglocal(9,"NdpFileCommit %d\n", NO_ERROR);
2163 return NO_ERROR;
2164}
2165
2166
2167int APIENTRY NdpFileNewSize (HCONNECTION conn, NDFILEHANDLE handle, ULONG ulLen)
2168{
2169 int rc = NdpFileNewSizeL(conn, handle, ulLen);
2170 debuglocal(9,"NdpFileNewSize %ld %d\n", ulLen, rc);
2171 return rc;
2172}
2173
2174int APIENTRY NdpFileNewSizeL(HCONNECTION conn, NDFILEHANDLE handle, LONGLONG llLen)
2175{
2176 Connection *pConn = (Connection *)conn;
2177 Resource *pRes = pConn->pRes;
2178 int rc = 0;
2179 unsigned long action;
2180
2181 ENTER();
2182
2183 debuglocal(9,"NdpFileNewSizeL in [%p]\n", pConn);
2184
2185 do {
2186 if (pConn->file.fd < 0)
2187 {
2188 rc = ERROR_INVALID_HANDLE;
2189 break;
2190 }
2191
2192 rc = smbwrp_setfilesize(pConn->cli, &pConn->file, llLen);
2193
2194 } while (0);
2195 debuglocal(9,"NdpFileNewSizeL <%s> %lld %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, llLen, rc);
2196 LEAVE();
2197 return rc;
2198}
2199
2200#define NDPSMB_READ_MAX_SIZE (65536 - 4096)
2201
2202int APIENTRY NdpFileRead (HCONNECTION conn, NDFILEHANDLE handle, void *pBuffer, ULONG ulRead, ULONG *pulActual)
2203{
2204 Connection *pConn = (Connection *)conn;
2205 Resource *pRes = pConn->pRes;
2206 int rc = 0;
2207 unsigned long done = 0;
2208 unsigned long onedone;
2209 unsigned long action;
2210 ULONG ulReadCompleted = 0;
2211
2212 ENTER();
2213
2214 debuglocal(9,"NdpFileRead in [%p]\n", pConn);
2215
2216 do {
2217 if (pConn->file.fd < 0)
2218 {
2219 rc = ERROR_INVALID_HANDLE;
2220 break;
2221 }
2222 while (ulReadCompleted < ulRead)
2223 {
2224 ULONG ulActual;
2225 ULONG ulToRead = ulRead - ulReadCompleted;
2226 debuglocal(9,"NdpFileRead completed %d, to read %d\n", ulReadCompleted, ulToRead);
2227 if (ulToRead > NDPSMB_READ_MAX_SIZE)
2228 {
2229 ulToRead = NDPSMB_READ_MAX_SIZE;
2230 }
2231 rc = smbwrp_read(pConn->cli, &pConn->file, (char *)pBuffer + ulReadCompleted, ulToRead, &ulActual);
2232 if (ulActual == 0 || rc != NO_ERROR)
2233 {
2234 break;
2235 }
2236 ulReadCompleted += ulActual;
2237 }
2238 //*pulActual = ulRead;
2239 //DosSleep(0);
2240
2241 } while (0);
2242
2243 if (ulReadCompleted > 0)
2244 {
2245 rc = NO_ERROR; /* Still were able to read some data. */
2246 }
2247
2248 if (rc == NO_ERROR)
2249 {
2250 *pulActual = ulReadCompleted;
2251 }
2252
2253 debuglocal(9,"NdpFileRead <%s> %lu %lu %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, ulRead, *pulActual, rc);
2254 LEAVE();
2255 return rc;
2256}
2257
2258int APIENTRY NdpFileWrite (HCONNECTION conn, NDFILEHANDLE handle, void *pBuffer, ULONG ulWrite, ULONG *pulActual)
2259{
2260 Connection *pConn = (Connection *)conn;
2261 Resource *pRes = pConn->pRes;
2262 int rc = 0;
2263 unsigned long done = 0;
2264 unsigned long onedone;
2265 unsigned long action;
2266
2267 ENTER();
2268
2269 debuglocal(9,"NdpFileWrite in [%p]\n", pConn);
2270
2271 /* delete the dir cache
2272 this was moved from NdpFileClose() becasue if there are a lot files in the tree all are reread
2273 the problem when moved to here is, that last accessed time is not refreshed
2274 if this is needed, a new function needs to be done to update only one file in the cache */
2275 dircache_invalidate(pConn->file.fullname, pRes->pdc, 1);
2276
2277 do {
2278 if (pConn->file.fd < 0)
2279 {
2280 rc = ERROR_INVALID_HANDLE;
2281 break;
2282 }
2283 rc = smbwrp_write(pConn->cli, &pConn->file, pBuffer, ulWrite, pulActual);
2284
2285 } while (0);
2286 debuglocal(9,"NdpFileWrite <%s> %lu %lu %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, ulWrite, *pulActual, rc);
2287 LEAVE();
2288 return rc;
2289}
2290
Note: See TracBrowser for help on using the repository browser.