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

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

Add support to enable NTLMv1 authentication

  • 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 debug_printf("checkMountResource in tid#%d\n", _gettid());
866 rc = smbwrp_connect( pRes, &cli);
867 if (rc)
868 {
869 debuglocal(9,"NdpCreateConnection failed rc=%d\n", rc);
870 pfsa->cSectorUnit = 1;
871 pfsa->cUnit = 123456;
872 pfsa->cUnitAvail = 123456;
873 pfsa->cbSector = 2048;
874 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_ACCESS_DENIED);
875 LEAVE();
876 return rc;
877 }
878
879 rc = smbwrp_dskattr( cli, &fsa);
880 if (rc)
881 {
882 pfsa->cSectorUnit = 1;
883 pfsa->cUnit = 123456;
884 pfsa->cUnitAvail = 123456;
885 pfsa->cbSector = 2048;
886 //rc = rc ? rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
887 }
888 else
889 {
890 pfsa->cSectorUnit = fsa.cSectorUnit;
891 pfsa->cUnit = fsa.cUnit;
892 pfsa->cUnitAvail = fsa.cUnitAvail;
893 pfsa->cbSector = fsa.cbSector;
894 }
895
896 smbwrp_disconnect( pRes, cli);
897
898 debuglocal(9,"NdpRsrcQueryFSAllocate %d/%d (cUnit = %d/cUnitAvail = %d/cbSector = %d)\n", rc, rc1, pfsa->cUnit, pfsa->cUnitAvail, pfsa->cbSector);
899 LEAVE();
900 return rc;
901}
902
903// -------------------------------------------------------------
904
905int APIENTRY NdpCreateConnection (HRESOURCE resource, HCONNECTION *pconn)
906{
907 int rc = 0;
908 Resource * pRes = (Resource *)resource;
909 unsigned long action;
910 Connection *pConn = NULL;
911
912 ENTER();
913
914 debuglocal(9,"NdpCreateConnection in\n");
915
916 pConn = malloc( sizeof(Connection));
917 if (pConn == NULL)
918 {
919 rc = ERROR_NOT_ENOUGH_MEMORY;
920 }
921 if (rc)
922 {
923 debuglocal(9,"NdpCreateConnection ERROR_NOT_ENOUGH_MEMORY %d\n", rc);
924 LEAVE();
925 return rc;
926 }
927 memset(pConn, 0, sizeof(Connection));
928 pConn->pRes = pRes;
929 pConn->file.fd = -1;
930
931 debuglocal(9,"NdpCreateConnection send CONNECT\n");
932 rc = smbwrp_connect( pRes, &pConn->cli);
933 if (rc)
934 {
935 free(pConn);
936 pConn = NULL;
937 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_INVALID_PARAMETER);
938 }
939
940 *pconn = (HCONNECTION)pConn;
941 debuglocal(9,"NdpCreateConnection [%p] %d\n", pConn, rc);
942 LEAVE();
943 return rc;
944}
945
946// -------------------------------------------------------------
947
948int APIENTRY NdpFreeConnection (HCONNECTION conn)
949{
950 Connection *pConn = (Connection *)conn;
951 Resource *pRes = pConn->pRes;
952 int rc;
953
954 ENTER();
955
956 debuglocal(9,"NdpFreeConnection in [%p]\n", pConn);
957 if (pConn->file.fd >= 0)
958 {
959 rc = smbwrp_close( pConn->cli, &pConn->file);
960 pConn->file.fd = -1;
961 }
962
963 smbwrp_disconnect( pRes, pConn->cli);
964
965 free(pConn);
966 debuglocal(9,"NdpFreeConnection %d\n", NO_ERROR);
967 LEAVE();
968 return NO_ERROR;
969}
970
971// -------------------------------------------------------------
972
973/*
974 * NdpQueryPathInfo is the most important function :) netdrive always calls
975 * the function before every operation to find out the path status:
976 * does it exist, is it a file, does a parent directory exist, etc.
977 * Plugin must return one of the following error codes:
978 * NO_ERROR - path exists and the path information have been successfully
979 * retrieved.
980 * ERROR_FILE_NOT_FOUND - all but the last component of the path exist and the
981 * path without the last component is a directory. dir1_ok\dir2_ok\does_not_exist.
982 * the wildcard can not exist, so the plugin returns FILE_NOT_FOUND, if the parent
983 * directory exist.
984 * ERROR_PATH_NOT_FOUND - any of not last path components does not exist, or all
985 * but the last component exist and is a file: \dir_ok\dir2_ok\file_ok\non_existing.
986 * ERROR_REM_NOT_LIST - resource is temporarily unavailable for some reasons.
987 * Any other error codes means an internal plugin error, not related to the status
988 * of the path queried.
989 */
990int APIENTRY NdpQueryPathInfo (HCONNECTION conn, void *plist, char *szPath)
991{
992 Connection *pConn = (Connection *)conn;
993 Resource *pRes = pConn->pRes;
994 smbwrp_fileinfo finfo;
995 int rc = 0;
996 int rcCon = 0;
997 unsigned long action;
998 char path[CCHMAXPATH+1] = {0};
999
1000 ENTER();
1001
1002 debuglocal(9,"NdpQueryPathInfo in [%p] <%s>\n", pConn, szPath);
1003
1004 // is wildcard is specified, we suppose parent dir exist, so exit immediately
1005 if (ph->fsphStrChr(szPath, '*') || ph->fsphStrChr(szPath, '?'))
1006 {
1007 LEAVE();
1008 return ERROR_FILE_NOT_FOUND;
1009 }
1010
1011
1012 do {
1013 /* First check if there is information in the directory cache. */
1014 unsigned long ulAge = 0;
1015 if (dircache_find_path(pRes->pdc, szPath, &finfo, &ulAge))
1016 {
1017 if (ulAge <= 15) /* @todo configurable. */
1018 {
1019 rc = NO_ERROR;
1020 finfo.easize = -1;
1021 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1022 break;
1023 }
1024 }
1025
1026 rc = pathparser(pRes, pConn, szPath, path);
1027 debuglocal(9,"NdpQueryPathInfo pathparser for <%s> rc=%d\n", path, rc);
1028 switch (rc)
1029 {
1030 case NO_ERROR :
1031 case ERROR_FILE_NOT_FOUND:
1032 case ERROR_PATH_NOT_FOUND:
1033 case ERROR_ACCESS_DENIED:
1034 case ERROR_INVALID_PARAMETER:
1035 {
1036 break;
1037 }
1038 default :
1039 {
1040 rc = ERROR_PATH_NOT_FOUND;
1041 }
1042 }
1043 if (rc)
1044 {
1045 break;
1046 }
1047 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1048 debuglocal(9,"NdpQueryPathInfo smbwrp_getattr for <%s>\n", path);
1049 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
1050 if (rc)
1051 {
1052 // remote server not available for first time?
1053 if (rc == ERROR_REM_NOT_LIST)
1054 {
1055 // free current cli resources
1056 smbwrp_disconnect( pRes, pConn->cli);
1057 // reconnect
1058 rcCon = smbwrp_connect( pRes, &pConn->cli);
1059 if (rcCon != NO_ERROR)
1060 debuglocal(9,"NdpQueryPathInfo smbwrp_connect rc = %d\n", rcCon);
1061
1062 // try file list again if reconnecting worked
1063 if (rcCon == NO_ERROR)
1064 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
1065 }
1066 debuglocal(9,"NdpQueryPathInfo smbwrp_getattr, rc = %d\n", rc);
1067 switch (rc)
1068 {
1069 case NO_ERROR :
1070 case ERROR_FILE_NOT_FOUND:
1071 case ERROR_PATH_NOT_FOUND:
1072 case ERROR_ACCESS_DENIED:
1073 case ERROR_INVALID_PARAMETER:
1074 case ERROR_REM_NOT_LIST:
1075 break;
1076 default :
1077 {
1078 rc = ERROR_PATH_NOT_FOUND;
1079 }
1080 }
1081 }
1082
1083 if (rc == NO_ERROR) {
1084 finfo.easize = -1;
1085 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1086 }
1087 else if (rc == ERROR_FILE_NOT_FOUND)
1088 {
1089 // now try the upper path
1090 char * p = ph->fsphStrRChr(finfo.fname, '\\');
1091 if (p && p > finfo.fname)
1092 {
1093 *p = 0;
1094 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
1095 debuglocal(9,"NdpQueryPathInfo upper path in <%s>, rc = %d\n", finfo.fname, rc);
1096 if (rc == NO_ERROR)
1097 {
1098 rc = (finfo.attr & FILE_DIRECTORY) !=0 ?
1099 ERROR_FILE_NOT_FOUND:ERROR_PATH_NOT_FOUND;
1100 }
1101 else if (rc != ERROR_REM_NOT_LIST)
1102 {
1103 rc = ERROR_PATH_NOT_FOUND;
1104 }
1105 }
1106 }
1107 } while (0);
1108 debuglocal(9,"NdpQueryPathInfo <%s> (%s) %d\n", szPath, path, rc);
1109
1110 LEAVE();
1111 return rc;
1112}
1113
1114// -------------------------------------------------------------
1115
1116int APIENTRY NdpFindStart (HCONNECTION conn, void *plist, NDFILEINFOL *pfiparent, char *szPath, ULONG ulAttribute)
1117{
1118 Connection *pConn = (Connection *)conn;
1119 Resource *pRes = pConn->pRes;
1120 int rc = NO_ERROR, count = 0;
1121 unsigned long action;
1122 char *mask = "*";
1123 char dir[CCHMAXPATH+1] = {0};
1124 char path[CCHMAXPATH+1] = {0};
1125 smbwrp_fileinfo * data;
1126 NDPATHELEMENT *pel = ph->fsphNameElem(0);
1127 filelist_state state;
1128 char * p;
1129
1130 ENTER();
1131
1132 debug_printf("NdpFindStart in [%p]\n", pConn);
1133
1134 strncpy(dir, szPath, sizeof(dir) - 1);
1135 if (pel)
1136 {
1137 mask = pel->name;
1138 dir[strlen(szPath) - pel->length] = 0;
1139 }
1140 action = strlen(dir) - 1;
1141 if (dir[action] == '\\')
1142 {
1143 dir[action] = 0;
1144 }
1145 rc = pathparser(pRes, pConn, dir, path);
1146 if (rc)
1147 {
1148 return rc;
1149 }
1150 action = strlen(path) - 1;
1151 if (path[action] != '\\')
1152 {
1153 strncat(path, "\\", sizeof(path) - 1);
1154 }
1155 strcpy(dir, path);
1156 strncat(path, mask, sizeof(path) - 1);
1157
1158 // this structure will be used by libsmb callbacks, so we store here all we need
1159 // to fill netdrive structures
1160 state.pConn = pConn;
1161 state.plist = plist;
1162 state.ulAttribute = ulAttribute;
1163 strcpy( state.dir, dir);
1164 strcpy( state.dir_mask, mask);
1165 strcpy( state.mask, path);
1166 state.fullpath = szPath;
1167 /* This plugin always reads a complete directory listing and filters results
1168 * using actual mask (state.dir_mask) in getfindinfoL.
1169 * May be this was a workaround for some server bug.
1170 * If this will be changed, then directory listing cache must be changed too,
1171 * and must remember the mask, which was used to obtain a listing.
1172 * Now the directory cache saves complete directory listings and then uses them to find
1173 * information about single files.
1174 * However, with a directory cache, it is probably faster to get a full listing and
1175 * then use it to obtain info about separate files than to perform a network
1176 * list query operation using actual wild cards for each file. Some application,
1177 * for example OpenOffice, do this.
1178 */
1179 p = getlastslash(state.mask);
1180 if (p)
1181 {
1182 *(p + 1) = '*';
1183 *(p + 2) = 0;
1184 }
1185 else
1186 {
1187 strcpy(state.mask, "\\*");
1188 }
1189 debuglocal(9,"NdpFindStart: dir [%s], dir_mask [%s], mask [%s], szPath [%s]\n",
1190 state.dir, state.dir_mask, state.mask, state.fullpath);
1191 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1192 // we need to handle reconnection also here, because NdpQueryPathInfo
1193 // could be called with '*' and exit then immediately (without calling libsmb)
1194 if (rc == ERROR_REM_NOT_LIST)
1195 {
1196 // free current cli resources
1197 smbwrp_disconnect( pRes, pConn->cli);
1198 // reconnect
1199 smbwrp_connect( pRes, &pConn->cli);
1200 // try file list again next loop
1201 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1202 debuglocal(9,"NdpFindStart remote connection lost, rc = %d\n", rc);
1203 }
1204
1205 debuglocal(9,"NdpFindStart <%s> (%s) cnt %d %d\n", szPath, path, count, rc);
1206 LEAVE();
1207 return rc;
1208}
1209
1210int APIENTRY NdpDeletePathInfo (HRESOURCE resource, NDFILEINFOL *pfi)
1211{
1212// debuglocal(9,"NdpDeletePathInfo %d\n", 0);
1213 return NO_ERROR;
1214}
1215
1216int APIENTRY NdpRefresh (HCONNECTION conn, char *path, int tree)
1217{
1218 debuglocal(9,"NdpRefresh <%s> %d\n", path, 0);
1219 return NO_ERROR;
1220}
1221
1222int APIENTRY NdpDiscardResourceData (HRESOURCE resource, NDDATABUF *pdatabuf)
1223{
1224 // The plugin do not have to deallocate anything
1225 // because resource data did not contain any pointers
1226 // to plugins data.
1227 // Data stored by fsphSetResourceData will be
1228 // deallocated by NetDrive.
1229
1230 debuglocal(9,"NdpDicardresourceData %d\n", 0);
1231 return NO_ERROR;
1232}
1233
1234int APIENTRY NdpSetPathInfo (HCONNECTION conn, NDFILEINFOL *pfi, char *szPathName)
1235{
1236 Connection *pConn = (Connection *)conn;
1237 Resource *pRes = pConn->pRes;
1238 int rc = 0;
1239 unsigned long action;
1240 char path[CCHMAXPATH+1] = {0};
1241 smbwrp_fileinfo finfo;
1242
1243 ENTER();
1244
1245 debug_printf("NdpSetPathInfo in [%p]\n", pConn);
1246
1247 // delete the dir cache
1248 dircache_invalidate(szPathName, pRes->pdc, 1);
1249
1250 do {
1251 rc = pathparser(pRes, pConn, szPathName, path);
1252 if (rc)
1253 {
1254 break;
1255 }
1256
1257 memset(&finfo, 0, sizeof(finfo));
1258
1259 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1260 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(finfo.mtime));
1261 finfo.attr = pfi->stat.attrFile & 0x37;
1262 rc = smbwrp_setattr(pConn->cli, &finfo);
1263 } while (0);
1264 debuglocal(9,"NdpSetPathInfo <%s> (%s) %d\n", szPathName, path, rc);
1265 LEAVE();
1266 return rc;
1267}
1268
1269int buildFEALIST(FEALIST *pFEASrc, GEALIST *pGEAList, FEALIST *pFEAList)
1270{
1271 int rc = 0;
1272 FEA * pfea;
1273 FEA * pfeadest;
1274 unsigned long size, done = sizeof(pFEAList->cbList), dsize, ddone = sizeof(pFEAList->cbList);
1275
1276 size = pFEASrc->cbList;
1277 pfea = pFEASrc->list;
1278 pfeadest = pFEAList->list;
1279 dsize = pFEAList->cbList;
1280//debuglocal(9,"buildFEALIST in destsize %d srcsize %d pGEAList=%08x pGEAList->cbList=%d\n", dsize, ddone, size, pGEAList, pGEAList ? pGEAList->cbList : 0);
1281 while (done < size)
1282 {
1283 char * name = (char *)(pfea + 1);
1284 int insert = 1;
1285 if (pGEAList && pGEAList->cbList > sizeof(pGEAList->cbList))
1286 {
1287 GEA * pgea = pGEAList->list;
1288 unsigned long size = pGEAList->cbList - sizeof(pGEAList->cbList), done = 0;
1289 insert = 0;
1290 while (done < size)
1291 {
1292//debuglocal(9,"comp <%s> <%s>\n", name, pgea->szName);
1293 if (!ph->fsphStrNCmp(name, pgea->szName, pgea->cbName))
1294 {
1295 insert = 1;
1296 break;
1297 }
1298 done += pgea->cbName + 2;
1299 pgea = (GEA *)((char *)pgea + pgea->cbName + 2);
1300 }
1301 }
1302 if (insert)
1303 {
1304 ddone += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1305 if (ddone <= dsize)
1306 {
1307 pfeadest->cbName = pfea->cbName;
1308 pfeadest->cbValue = pfea->cbValue;
1309 pfeadest->fEA = 0;
1310 strcpy((char *)(pfeadest + 1), name);
1311 memcpy((char *)(pfeadest + 1) + pfea->cbName + 1, (char *)(pfea + 1) + pfea->cbName + 1, pfea->cbValue);
1312 pfeadest = (FEA *)((char *)pFEAList + ddone);
1313 }
1314 }
1315 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1316//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);
1317 pfea = (FEA *)((char *)pFEASrc + done);
1318 }
1319 pFEAList->cbList = ddone;
1320 if (ddone > dsize && dsize > sizeof(pFEAList->cbList))
1321 {
1322 rc = ERROR_BUFFER_OVERFLOW;
1323 }
1324 debuglocal(9,"buildFEALIST rc=%d destsize=%d destdone=%d srcsize=%d pGEAList=%08x\n", rc, dsize, ddone, size, pGEAList);
1325 return rc;
1326}
1327
1328int APIENTRY NdpEAQuery (HCONNECTION conn, GEALIST *pGEAList, NDFILEINFOL *pfi, FEALIST *pFEAList)
1329{
1330 Connection *pConn = (Connection *)conn;
1331 Resource *pRes = pConn->pRes;
1332 int rc = 0;
1333 unsigned long action;
1334 char * path = NULL;
1335 FEALIST * pFEASrc;
1336 NDDATABUF fdata = {0};
1337 smbwrp_fileinfo *finfo;
1338 const int cbBuffer = 64*1024;
1339
1340 if (!pfi || !pfi->pszName || !pFEAList)
1341 {
1342 return ERROR_EAS_NOT_SUPPORTED;
1343 }
1344 if (!pRes->easupport)
1345 {
1346 return ERROR_EAS_NOT_SUPPORTED;
1347 }
1348
1349 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1350 if (rc || !fdata.ulSize || !fdata.pData)
1351 {
1352 debuglocal(9,"NdpEAQuery: ph->fsphGetFileInfoData = %d/%d %08x\n", rc, fdata.ulSize, fdata.pData);
1353 return ERROR_EAS_NOT_SUPPORTED;
1354 }
1355
1356 ENTER();
1357
1358 finfo = (smbwrp_fileinfo *)fdata.pData;
1359 path = finfo->fname;
1360
1361 debuglocal(9,"NdpEAQuery in [%p] <%s> %08x %d\n", pConn, path, pGEAList, pGEAList ? pGEAList->cbList : 0);
1362
1363 char *pchBuffer = (char *)malloc(cbBuffer);
1364 if (!pchBuffer)
1365 {
1366 LEAVE();
1367 return ERROR_NOT_ENOUGH_MEMORY;
1368 }
1369
1370 do {
1371 rc = smbwrp_listea( pConn->cli, path, pchBuffer, cbBuffer);
1372 pFEASrc = (FEALIST*) pchBuffer;
1373 if (rc)
1374 {
1375 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1376 switch (rc)
1377 {
1378 case ERROR_FILE_NOT_FOUND :
1379 case ERROR_PATH_NOT_FOUND :
1380 {
1381 pFEAList->cbList = sizeof(pFEAList->cbList);
1382 rc = NO_ERROR;
1383 } break;
1384 case ERROR_BUFFER_OVERFLOW :
1385 {
1386 pFEAList->cbList = pFEASrc->cbList;
1387 } break;
1388 default :
1389 {
1390 rc = ERROR_EAS_NOT_SUPPORTED;
1391 }
1392 }
1393 }
1394 else
1395 {
1396 rc = buildFEALIST((FEALIST *)pFEASrc, pGEAList, pFEAList);
1397 }
1398 } while (0);
1399 free(pchBuffer);
1400 debuglocal(9,"NdpEAQuery <%s> %d %d %d\n", pfi->pszName, rc, pFEASrc->cbList, pFEAList->cbList);
1401 LEAVE();
1402 return rc;
1403}
1404
1405int APIENTRY NdpEASet (HCONNECTION conn, FEALIST *pFEAList, NDFILEINFOL *pfi)
1406{
1407 Connection *pConn = (Connection *)conn;
1408 Resource *pRes = pConn->pRes;
1409 int rc = 0;
1410 char * path;
1411 unsigned long action;
1412 NDDATABUF fdata = {0};
1413 smbwrp_fileinfo *finfo;
1414
1415 debuglocal(9,"NdpEASet in [%p]\n", pConn);
1416
1417 if (!pfi || !pfi->pszName)
1418 {
1419 return ERROR_EAS_NOT_SUPPORTED;
1420 }
1421 if (!pRes->easupport)
1422 {
1423 return ERROR_EAS_NOT_SUPPORTED;
1424 }
1425
1426 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1427 if (rc || !fdata.ulSize || !fdata.pData)
1428 {
1429 debuglocal(9,"NdpEASet: ph->fsphGetFileInfoData = %d/%d/%08x\n", rc, fdata.ulSize, fdata.pData);
1430 return ERROR_EAS_NOT_SUPPORTED;
1431 }
1432
1433 finfo = (smbwrp_fileinfo *)fdata.pData;
1434 path = finfo->fname;
1435
1436 rc = helperEASet(pConn->cli, pFEAList, path);
1437 debuglocal(9,"NdpEASet %d\n", rc);
1438 return rc;
1439}
1440
1441int APIENTRY NdpEASize (HCONNECTION conn, NDFILEINFOL *pfi, ULONG *pulEASize)
1442{
1443 Connection *pConn = (Connection *)conn;
1444 Resource *pRes = pConn->pRes;
1445 int rc = 0;
1446 unsigned long action;
1447 char * path = NULL;
1448 FEALIST * pfealist;
1449 NDDATABUF fdata = {0};
1450 smbwrp_fileinfo *finfo;
1451 const int cbBuffer = 64*1024;
1452 int easize;
1453
1454 if (!pfi || !pulEASize)
1455 {
1456 return ERROR_EAS_NOT_SUPPORTED;
1457 }
1458 if (!pRes->easupport)
1459 {
1460 return ERROR_EAS_NOT_SUPPORTED;
1461 }
1462
1463 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1464 if (rc || !fdata.ulSize || !fdata.pData)
1465 {
1466 debuglocal(9,"NdpEASize: ph->fsphGetFileInfoData = %d/%d/%08x\n", rc, fdata.ulSize, fdata.pData);
1467 return ERROR_EAS_NOT_SUPPORTED;
1468 }
1469
1470 ENTER();
1471
1472 finfo = (smbwrp_fileinfo *)fdata.pData;
1473 easize = finfo->easize;
1474 finfo->easize = -1;
1475 path = finfo->fname;
1476 if (easize >= 0)
1477 {
1478 *pulEASize = easize;
1479 debuglocal(9,"NdpEASize <%s> cached %d\n", path, easize);
1480 LEAVE();
1481 return NO_ERROR;
1482 }
1483
1484 debuglocal(9,"NdpEASize in [%p] <%s> \n", pConn, path);
1485
1486 char *pchBuffer = (char *)malloc(cbBuffer);
1487 if (!pchBuffer)
1488 {
1489 LEAVE();
1490 return ERROR_NOT_ENOUGH_MEMORY;
1491 }
1492
1493 do {
1494 rc = smbwrp_listea(pConn->cli, path, pchBuffer, cbBuffer);
1495 pfealist = (FEALIST*)pchBuffer;
1496 if (rc)
1497 {
1498 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1499 switch (rc)
1500 {
1501 case ERROR_FILE_NOT_FOUND :
1502 case ERROR_PATH_NOT_FOUND :
1503 {
1504 pfealist->cbList = sizeof(pfealist->cbList);
1505 } /* Fall through */
1506 case ERROR_BUFFER_OVERFLOW :
1507 {
1508 rc = NO_ERROR;
1509 } break;
1510 default :
1511 {
1512 rc = ERROR_EAS_NOT_SUPPORTED;
1513 }
1514 }
1515 }
1516 *pulEASize = pfealist->cbList;
1517 } while (0);
1518 free(pchBuffer);
1519 debuglocal(9,"NdpEASize <%s> %d %d\n", pfi->pszName, *pulEASize, rc);
1520 LEAVE();
1521 return rc;
1522}
1523
1524int APIENTRY NdpSetCurrentDir (HCONNECTION conn, NDFILEINFOL *pfi, char *szPath)
1525{
1526 Connection *pConn = (Connection *)conn;
1527 Resource *pRes = pConn->pRes;
1528 int rc = 0;
1529 unsigned long action;
1530 char path[CCHMAXPATH+1] = {0};
1531
1532 debuglocal(9,"NdpSetCurrentDir in [%p]\n", pConn);
1533
1534 ENTER();
1535
1536 do {
1537 rc = pathparser(pRes, pConn, szPath, path);
1538 if (rc)
1539 {
1540 break;
1541 }
1542
1543 rc = smbwrp_chdir(&pRes->srv, pConn->cli, path);
1544 } while (0);
1545 debuglocal(9,"NdpSetCurrentDir <%s> (%s) %d\n", szPath, path, rc);
1546 LEAVE();
1547 return rc;
1548}
1549
1550int APIENTRY NdpCopy (HCONNECTION conn, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, ULONG ulOption)
1551{
1552 debuglocal(9,"NdpCopy <%s> -> <%s> %d\n", szSrc, szDst, ERROR_CANNOT_COPY);
1553 return ERROR_CANNOT_COPY;
1554}
1555
1556int APIENTRY NdpCopy2 (HCONNECTION conn, HRESOURCE resDst, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, ULONG ulOption)
1557{
1558 debuglocal(9,"NdpCopy2 <%s> -> <%s> %d\n", szSrc, szDst, ERROR_CANNOT_COPY);
1559 return ERROR_CANNOT_COPY;
1560}
1561
1562int APIENTRY NdpForceDelete (HCONNECTION conn, NDFILEINFOL *pfi, char *szFile)
1563{
1564 Connection *pConn = (Connection *)conn;
1565 Resource *pRes = pConn->pRes;
1566 int rc = 0;
1567 unsigned long action;
1568 char path[CCHMAXPATH+1] = {0};
1569
1570 ENTER();
1571
1572 debuglocal(9,"NdpForceDelete in [%p]\n", pConn);
1573
1574 dircache_invalidate(szFile, pRes->pdc, 1);
1575
1576 do {
1577 rc = pathparser(pRes, pConn, szFile, path);
1578 if (rc)
1579 {
1580 break;
1581 }
1582
1583 rc = smbwrp_unlink(pConn->cli, path);
1584 } while (0);
1585 debuglocal(9,"NdpForceDelete <%s> (%s) %d\n", szFile, path, rc);
1586 LEAVE();
1587 return rc;
1588}
1589
1590int APIENTRY NdpCreateDir (HCONNECTION conn, NDFILEINFOL *pfiparent, char *szDirName, FEALIST *pFEAList)
1591{
1592 Connection *pConn = (Connection *)conn;
1593 Resource *pRes = pConn->pRes;
1594 int rc = 0;
1595 int rcEASet = 0;
1596 unsigned long action;
1597 char path[CCHMAXPATH+1] = {0};
1598
1599 ENTER();
1600
1601 debuglocal(9,"NdpCreateDir in [%p]\n", pConn);
1602
1603 dircache_invalidate(szDirName, pRes->pdc, 1);
1604
1605 do {
1606 rc = pathparser(pRes, pConn, szDirName, path);
1607 if (rc)
1608 {
1609 break;
1610 }
1611
1612 rc = smbwrp_mkdir(pConn->cli, path);
1613 } while (0);
1614 LEAVE();
1615
1616 if (path && pRes->easupport)
1617 {
1618 rcEASet = helperEASet(pConn->cli, pFEAList, path);
1619 }
1620 debuglocal(9,"NdpCreateDir <%s> (%s) rc=%d, EASupport=%s rc=%d\n", szDirName, path, rc, pRes->easupport?"yes":"no", rcEASet);
1621
1622 return rc;
1623}
1624
1625int APIENTRY NdpDeleteDir (HCONNECTION conn, NDFILEINFOL *pfi, char *szDir)
1626{
1627 Connection *pConn = (Connection *)conn;
1628 Resource *pRes = pConn->pRes;
1629 int rc = 0;
1630 unsigned long action;
1631 char path[CCHMAXPATH+1] = {0};
1632
1633 ENTER();
1634
1635 debuglocal(9,"NdpDeleteDir in [%p]\n", pConn);
1636
1637 dircache_invalidate(szDir, pRes->pdc, 1);
1638
1639 do {
1640 rc = pathparser(pRes, pConn, szDir, path);
1641 if (rc)
1642 {
1643 break;
1644 }
1645
1646 rc = smbwrp_rmdir(pConn->cli, path);
1647 } while (0);
1648 debuglocal(9,"NdpDeleteDir <%s> (%s) %d\n", szDir, path, rc);
1649 LEAVE();
1650 return rc;
1651}
1652
1653int APIENTRY NdpMove (HCONNECTION conn, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc)
1654{
1655 Connection *pConn = (Connection *)conn;
1656 Resource *pRes = pConn->pRes;
1657 int rc = 0;
1658 unsigned long action;
1659 char src[CCHMAXPATH+1] = {0};
1660 int l1, l2;
1661 char * p = szDst;
1662
1663 ENTER();
1664
1665 debuglocal(9,"NdpMove in [%p] from <%s> to <%s>\n", pConn, szSrc, szDst);
1666
1667 dircache_invalidate(szSrc, pRes->pdc, 1);
1668 dircache_invalidate(szDst, pRes->pdc, 1);
1669
1670 do
1671 {
1672 rc = pathparser(pRes, pConn, szSrc, src);
1673 if (rc)
1674 {
1675 break;
1676 }
1677 l1 = strlen(szSrc);
1678 l2 = strlen(src);
1679 if (l1 > l2)
1680 {
1681 if (ph->fsphStrNICmp(szSrc, szDst, l1 - l2))
1682 {
1683 // the file moved accross different shares or servers or workgroups
1684 rc = ERROR_WRITE_PROTECT;
1685 break;
1686 }
1687 p = szDst + l1 - l2 + 1;
1688 }
1689 //pConn->mem[CCHMAXPATH + 1] = '\\';
1690 rc = smbwrp_rename(pConn->cli, src, p);
1691 } while (0);
1692 debuglocal(9,"NdpMove <%s> -> <%s> (%s) %d\n", szSrc, szDst, src, rc);
1693 LEAVE();
1694 return rc;
1695}
1696
1697int APIENTRY NdpMove2 (HCONNECTION conn, HRESOURCE resDst, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc)
1698{
1699 debuglocal(9,"NdpMove2 <%s> -> <%s> %d\n", szSrc, szDst, ERROR_WRITE_PROTECT);
1700 return ERROR_WRITE_PROTECT;
1701}
1702
1703
1704int APIENTRY NdpChangeCase (HCONNECTION conn, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, char *szNewName, ULONG ulNameLen)
1705{
1706 return NdpMove (conn, pfiSrc, szDst, pfiSrc, szSrc);
1707}
1708
1709int APIENTRY NdpRename (HCONNECTION conn, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, char *szNewName, ULONG ulNameLen)
1710{
1711 return NdpMove (conn, pfiSrc, szDst, pfiSrc, szSrc);
1712}
1713
1714int smbopen(Connection *pConn, char *szFileName, int flags, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1715{
1716 Resource *pRes = pConn->pRes;
1717 unsigned long action;
1718 int rc = 0;
1719 char path[CCHMAXPATH+1] = {0};
1720
1721 ENTER();
1722
1723 debuglocal(9,"smbopen in [%p] %d\n", pConn, pConn->file.fd);
1724
1725 if (flags & O_CREAT)
1726 {
1727 dircache_invalidate(szFileName, pRes->pdc, 1);
1728 }
1729 do {
1730 if (pConn->file.fd > 0)
1731 {
1732 rc = ERROR_TOO_MANY_OPEN_FILES;
1733 break;
1734 }
1735
1736 rc = pathparser(pRes, pConn, szFileName, path);
1737 if (rc)
1738 {
1739 break;
1740 }
1741
1742 strncpy(pConn->file.fullname, szFileName, sizeof(pConn->file.fullname) - 1);
1743 strncpy(pConn->file.fname, path, sizeof(pConn->file.fname) - 1);
1744 flags |= O_BINARY;
1745 switch (ulOpenMode & 3)
1746 {
1747 case OPEN_ACCESS_READONLY : flags |= O_RDONLY; break;
1748 case OPEN_ACCESS_WRITEONLY : flags |= O_WRONLY; break;
1749 case OPEN_ACCESS_READWRITE : flags |= O_RDWR; break;
1750 default : flags |= O_RDWR;
1751 }
1752 pConn->file.openmode = flags;
1753 pConn->file.openattr = ulAttribute & 0x37;
1754 pConn->file.denymode = (ulOpenMode & 0x70) >> 4;
1755 rc = smbwrp_open(pConn->cli, &pConn->file);
1756 } while (0);
1757 debuglocal(9,"smbopen <%s> (%s) %08x %08x %08x %d. file = %d\n", szFileName, path, flags, ulOpenMode, ulAttribute, rc, pConn->file.fd);
1758 if (!rc && pFEAList)
1759 {
1760 int rc1 = NdpFileEASet((HCONNECTION)pConn, (NDFILEHANDLE)0, pFEAList);
1761 debuglocal(9,"smbopen NdpFileEASet %d. pFEAList->cbList %d\n", rc1, pFEAList->cbList);
1762 }
1763 LEAVE();
1764 return rc;
1765}
1766
1767int APIENTRY NdpOpenReplace (HCONNECTION conn, NDFILEINFOL *pfi, NDFILEHANDLE *phandle, char *szFileName, ULONG ulSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1768{
1769 return smbopen((Connection *)conn, szFileName, O_TRUNC, ulOpenMode, ulAttribute, pFEAList);
1770}
1771
1772int APIENTRY NdpOpenReplaceL(HCONNECTION conn, NDFILEINFO *pfi, NDFILEHANDLE *phandle, char *szFileName, LONGLONG llSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1773{
1774 return smbopen((Connection *)conn, szFileName, O_TRUNC, ulOpenMode, ulAttribute, pFEAList);
1775}
1776
1777int APIENTRY NdpOpenCreate (HCONNECTION conn, NDFILEINFOL *pfiparent, NDFILEHANDLE *phandle, char *szFileName, ULONG ulSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1778{
1779// return smbopen((Connection *)conn, szFileName, O_CREAT, ulOpenMode, ulAttribute);
1780 return smbopen((Connection *)conn, szFileName, O_CREAT | O_EXCL, ulOpenMode, ulAttribute, pFEAList);
1781}
1782
1783int APIENTRY NdpOpenCreateL(HCONNECTION conn, NDFILEINFO *pfiparent, NDFILEHANDLE *phandle, char *szFileName, LONGLONG llSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1784{
1785 return smbopen((Connection *)conn, szFileName, O_CREAT | O_EXCL, ulOpenMode, ulAttribute, pFEAList);
1786}
1787
1788int APIENTRY NdpOpenExisting (HCONNECTION conn, NDFILEINFOL *pfi, NDFILEHANDLE *phandle, char *szFileName, ULONG ulOpenMode, USHORT *pfNeedEA)
1789{
1790 if (pfNeedEA) *pfNeedEA = 0; // wtf is this ?
1791 return smbopen((Connection *)conn, szFileName, 0, ulOpenMode, 0, NULL);
1792}
1793
1794int APIENTRY NdpSetFileAttribute (HCONNECTION conn, NDFILEINFOL *pfi, char *szFileName, USHORT usAttr)
1795{
1796 Connection *pConn = (Connection *)conn;
1797 Resource *pRes = pConn->pRes;
1798 int rc = 0;
1799 unsigned long action;
1800
1801 smbwrp_fileinfo finfo;
1802 char path[CCHMAXPATH+1] = {0};
1803
1804 ENTER();
1805
1806 debuglocal(9,"NdpSetFileAttribute in [%p]\n", pConn);
1807 do {
1808 rc = pathparser(pRes, pConn, szFileName, path);
1809 if (rc)
1810 {
1811 break;
1812 }
1813
1814 memset(&finfo, 0, sizeof(finfo));
1815 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1816 finfo.attr = usAttr & 0x37;
1817 rc = smbwrp_setattr(pConn->cli, &finfo);
1818 } while (0);
1819 debuglocal(9,"NdpSetFileAttribute <%s> (%s) %04x %d\n", szFileName, path, usAttr, rc);
1820 LEAVE();
1821 return rc;
1822}
1823
1824int APIENTRY NdpFlush (HRESOURCE resource)
1825{
1826 debuglocal(9,"NdpFlush %d\n", ERROR_NOT_SUPPORTED);
1827 return ERROR_NOT_SUPPORTED;
1828}
1829
1830// Called when a new thread will call the plugin. Allows to do per thread init, like disable signals.
1831#define ND_PL_INIT_THREAD 0xFFFF0000
1832
1833int APIENTRY NdpIOCTL (int type, HRESOURCE resource, char *path, int function, void *in, ULONG insize, PULONG poutlen)
1834{
1835 if (function == ND_PL_INIT_THREAD)
1836 {
1837 smbwrp_initthread();
1838 debuglocal(9, "NdpIOCTL init thread\n");
1839 return NO_ERROR;
1840 }
1841
1842 debuglocal(9,"NdpIOCTL <%s> %d\n", path, function);
1843
1844 if (in && insize > 4096)
1845 {
1846 char out[4096];
1847 sprintf (out, "SAMBA IOCTL function = %d, parms [%s] insize = %d, *poutlen = %d", function, in, insize, *poutlen);
1848 *poutlen = strlen(out);
1849 strcpy (in, out);
1850 return NO_ERROR;
1851 }
1852
1853 return ERROR_NOT_SUPPORTED;
1854}
1855
1856int APIENTRY NdpFileQueryInfo (HCONNECTION conn, NDFILEHANDLE handle, void *plist)
1857{
1858 Connection *pConn = (Connection *)conn;
1859 Resource *pRes = pConn->pRes;
1860 int rc = 0;
1861 unsigned long action;
1862 smbwrp_fileinfo finfo;
1863
1864 ENTER();
1865
1866 debug_printf("NdpFileQueryInfo in [%p]\n", pConn);
1867 do {
1868 if (pConn->file.fd < 0 || !*pConn->file.fname)
1869 {
1870 rc = ERROR_INVALID_HANDLE;
1871 break;
1872 }
1873 strncpy(finfo.fname, pConn->file.fname, sizeof(finfo.fname) - 1);
1874 rc = smbwrp_fgetattr(pConn->cli, &pConn->file, &finfo);
1875 if (!rc)
1876 {
1877 finfo.easize = -1;
1878 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1879 }
1880 } while (0);
1881 debuglocal(9,"NdpFileQueryInfo <%s> %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, rc);
1882 LEAVE();
1883 return rc;
1884}
1885
1886int APIENTRY NdpFileEAQuery (HCONNECTION conn, NDFILEHANDLE handle, GEALIST *pGEAList, FEALIST *pFEAList)
1887{
1888 Connection *pConn = (Connection *)conn;
1889 Resource *pRes = pConn->pRes;
1890 int rc = 0;
1891 unsigned long action;
1892 const int cbBuffer = 64*1024;
1893 FEALIST * pFEASrc;
1894
1895 if (!pFEAList)
1896 {
1897 return ERROR_EAS_NOT_SUPPORTED;
1898 }
1899 if (!pRes->easupport)
1900 {
1901 return ERROR_EAS_NOT_SUPPORTED;
1902 }
1903
1904 debuglocal(9,"NdpFileEAQuery in [%p] <%s>/%d pGEAList=%08x\n", pConn, pConn->file.fname, pConn->file.fd, pGEAList);
1905
1906 char *pchBuffer = (char *)malloc(cbBuffer);
1907 if (!pchBuffer)
1908 return ERROR_NOT_ENOUGH_MEMORY;
1909
1910 ENTER();
1911
1912 do {
1913 if (pConn->file.fd < 0)
1914 {
1915 rc = ERROR_INVALID_HANDLE;
1916 break;
1917 }
1918 rc = smbwrp_flistea(pConn->cli, &pConn->file, pchBuffer, cbBuffer);
1919 pFEASrc = (FEALIST *)pchBuffer;
1920 if (rc)
1921 {
1922 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1923 switch (rc)
1924 {
1925 case ERROR_FILE_NOT_FOUND :
1926 case ERROR_PATH_NOT_FOUND :
1927 {
1928 pFEAList->cbList = sizeof(pFEAList->cbList);
1929 rc = NO_ERROR;
1930 } break;
1931 case ERROR_BUFFER_OVERFLOW :
1932 {
1933 pFEAList->cbList = pFEASrc->cbList;
1934 } break;
1935 default :
1936 {
1937 rc = ERROR_EAS_NOT_SUPPORTED;
1938 }
1939 }
1940 }
1941 else
1942 {
1943 rc = buildFEALIST(pFEASrc, pGEAList, pFEAList);
1944 }
1945 } while (0);
1946 free(pchBuffer);
1947 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);
1948 LEAVE();
1949 return rc;
1950}
1951
1952int APIENTRY NdpFileEASet (HCONNECTION conn, NDFILEHANDLE handle, FEALIST *pFEAList)
1953{
1954 Connection *pConn = (Connection *)conn;
1955 Resource *pRes = pConn->pRes;
1956 int rc = 0;
1957 unsigned long action;
1958
1959 debuglocal(9,"NdpFileEASet in [%p]\n", pConn);
1960
1961 if (!pFEAList || pFEAList->cbList <= sizeof(long))
1962 {
1963 return ERROR_EAS_NOT_SUPPORTED;
1964 }
1965 if (!pRes->easupport)
1966 {
1967 return ERROR_EAS_NOT_SUPPORTED;
1968 }
1969
1970 ENTER();
1971
1972 do {
1973 // got FEA there
1974 FEA * pfea;
1975 unsigned long done = sizeof(long);
1976 if (pConn->file.fd < 0)
1977 {
1978 rc = ERROR_INVALID_HANDLE;
1979 break;
1980 }
1981
1982 pfea = pFEAList->list;
1983 while (done < pFEAList->cbList)
1984 {
1985 rc = smbwrp_fsetea(pConn->cli, &pConn->file, (char *)(pfea + 1), pfea->cbValue ? (char *)(pfea + 1) + pfea->cbName + 1: NULL, pfea->cbValue);
1986 if (rc)
1987 {
1988 break;
1989 }
1990 pfea = (FEA *)((char *)(pfea + 1) + pfea->cbName + 1 + pfea->cbValue);
1991 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1992 }
1993
1994 } while (0);
1995 debuglocal(9,"NdpFileEASet %d\n", rc);
1996 LEAVE();
1997 return rc;
1998}
1999
2000int APIENTRY NdpFileEASize (HCONNECTION conn, NDFILEHANDLE handle, ULONG *pulEASize)
2001{
2002 Connection *pConn = (Connection *)conn;
2003 Resource *pRes = pConn->pRes;
2004 int rc = 0;
2005 unsigned long action;
2006 char path[CCHMAXPATH+1] = {0};
2007 FEALIST * pFEAList;
2008 const int cbBuffer = 64*1024;
2009
2010 if (!pulEASize)
2011 {
2012 return ERROR_EAS_NOT_SUPPORTED;
2013 }
2014 if (!pRes->easupport)
2015 {
2016 return ERROR_EAS_NOT_SUPPORTED;
2017 }
2018
2019 debuglocal(9,"NdpFileEASize in [%p] <%s>/%d \n", pConn, pConn->file.fname, pConn->file.fd);
2020
2021 char *pchBuffer = (char *)malloc(cbBuffer);
2022 if (!pchBuffer)
2023 return ERROR_NOT_ENOUGH_MEMORY;
2024
2025 ENTER();
2026
2027 do {
2028 if (pConn->file.fd < 0)
2029 {
2030 rc = ERROR_INVALID_HANDLE;
2031 break;
2032 }
2033 rc = smbwrp_flistea(pConn->cli, &pConn->file, pchBuffer, cbBuffer);
2034 pFEAList = (FEALIST*)pchBuffer;
2035 if (rc)
2036 {
2037 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
2038 switch (rc)
2039 {
2040 case ERROR_FILE_NOT_FOUND :
2041 case ERROR_PATH_NOT_FOUND :
2042 {
2043 pFEAList->cbList = sizeof(pFEAList->cbList);
2044 } /* Fall through */
2045 case ERROR_BUFFER_OVERFLOW :
2046 {
2047 rc = NO_ERROR;
2048 } break;
2049 default :
2050 {
2051 rc = ERROR_EAS_NOT_SUPPORTED;
2052 }
2053 }
2054 }
2055 *pulEASize = pFEAList->cbList;
2056 } while (0);
2057 free(pchBuffer);
2058 debuglocal(9,"NdpFileEASize %d %d\n", *pulEASize, rc);
2059 LEAVE();
2060 return rc;
2061}
2062
2063int APIENTRY NdpFileSetInfo (HCONNECTION conn, NDFILEHANDLE handle, NDFILEINFOL *pfi)
2064{
2065 Connection *pConn = (Connection *)conn;
2066 Resource *pRes = pConn->pRes;
2067 int rc = 0;
2068 unsigned long action, attrFile;
2069
2070 ENTER();
2071
2072 debug_printf("NdpFileSetInfo in [%p]\n", pConn);
2073
2074 // delete the dir cache
2075 dircache_invalidate(pConn->file.fullname, pRes->pdc, 1);
2076
2077 do {
2078 if (pConn->file.fd < 0 || !*pConn->file.fname)
2079 {
2080 rc = ERROR_INVALID_HANDLE;
2081 break;
2082 }
2083 attrFile = pfi->stat.attrFile;
2084 // deferred setinfo - on closing the file
2085 pConn->file.openattr = attrFile;
2086 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(pConn->file.mtime));
2087 fsphDosDateToUnixTime(pfi->stat.fdateCreation, pfi->stat.ftimeCreation, &(pConn->file.ctime));
2088 pConn->file.updatetime = 2;
2089 debug_printf("NdpFileSetInfo mtime %d\n", pConn->file.mtime);
2090 } while (0);
2091 debuglocal(9,"NdpFileSetInfo <%s> %08x %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, attrFile, rc);
2092 LEAVE();
2093 return NO_ERROR;
2094}
2095
2096int APIENTRY NdpFileSetFilePtrL(HCONNECTION conn, NDFILEHANDLE handle, LONGLONG llOffset, ULONG ulMethod, LONGLONG *pllActual)
2097{
2098 Connection *pConn = (Connection *)conn;
2099 Resource *pRes = pConn->pRes;
2100 int rc = 0;
2101 unsigned long action;
2102
2103 ENTER();
2104
2105 debuglocal(9,"NdpFileSetFilePtrL in [%p]\n", pConn);
2106
2107 do {
2108 if (pConn->file.fd < 0)
2109 {
2110 rc = ERROR_INVALID_HANDLE;
2111 break;
2112 }
2113
2114 rc = smbwrp_lseek(pConn->cli, &pConn->file, ulMethod, llOffset);
2115 if (!rc)
2116 *pllActual = pConn->file.offset;
2117
2118 } while (0);
2119 debuglocal(9,"NdpFileSetFilePtrL <%s> %lld %lu %lld %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, llOffset, ulMethod, *pllActual, rc);
2120 LEAVE();
2121 return rc;
2122}
2123
2124int APIENTRY NdpFileSetFilePtr (HCONNECTION conn, NDFILEHANDLE handle, LONG lOffset, ULONG ulMethod, ULONG *pulActual)
2125{
2126 LONGLONG llActual;
2127 int rc = NdpFileSetFilePtrL(conn, handle, lOffset, ulMethod, &llActual);
2128 *pulActual = llActual & 0xFFFFFFFF;
2129 debuglocal(9,"NdpFileSetFilePtr %ld %lu %ld %d\n", lOffset, ulMethod, *pulActual, rc);
2130 return rc;
2131}
2132
2133int APIENTRY NdpFileClose (HCONNECTION conn, NDFILEHANDLE handle)
2134{
2135 Connection *pConn = (Connection *)conn;
2136 Resource *pRes = pConn->pRes;
2137 int rc = 0;
2138 unsigned long action;
2139
2140 ENTER();
2141
2142 debuglocal(9,"NdpFileClose in [%p] %d <%s>\n", pConn, pConn->file.fd, pConn->file.fd < 0 ? "!null!" : pConn->file.fname);
2143
2144 do {
2145 if (pConn->file.fd < 0)
2146 {
2147 rc = ERROR_INVALID_HANDLE;
2148 break;
2149 }
2150
2151 rc = smbwrp_close(pConn->cli, &pConn->file);
2152
2153 } while (0);
2154 debuglocal(9,"NdpFileClose %d %d\n", pConn->file.fd, rc);
2155
2156 pConn->file.fd = -1;
2157 LEAVE();
2158 return rc;
2159}
2160
2161int APIENTRY NdpFileCommit (HCONNECTION conn, NDFILEHANDLE handle)
2162{
2163 debuglocal(9,"NdpFileCommit %d\n", NO_ERROR);
2164 return NO_ERROR;
2165}
2166
2167
2168int APIENTRY NdpFileNewSize (HCONNECTION conn, NDFILEHANDLE handle, ULONG ulLen)
2169{
2170 int rc = NdpFileNewSizeL(conn, handle, ulLen);
2171 debuglocal(9,"NdpFileNewSize %ld %d\n", ulLen, rc);
2172 return rc;
2173}
2174
2175int APIENTRY NdpFileNewSizeL(HCONNECTION conn, NDFILEHANDLE handle, LONGLONG llLen)
2176{
2177 Connection *pConn = (Connection *)conn;
2178 Resource *pRes = pConn->pRes;
2179 int rc = 0;
2180 unsigned long action;
2181
2182 ENTER();
2183
2184 debuglocal(9,"NdpFileNewSizeL in [%p]\n", pConn);
2185
2186 do {
2187 if (pConn->file.fd < 0)
2188 {
2189 rc = ERROR_INVALID_HANDLE;
2190 break;
2191 }
2192
2193 rc = smbwrp_setfilesize(pConn->cli, &pConn->file, llLen);
2194
2195 } while (0);
2196 debuglocal(9,"NdpFileNewSizeL <%s> %lld %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, llLen, rc);
2197 LEAVE();
2198 return rc;
2199}
2200
2201#define NDPSMB_READ_MAX_SIZE (65536 - 4096)
2202
2203int APIENTRY NdpFileRead (HCONNECTION conn, NDFILEHANDLE handle, void *pBuffer, ULONG ulRead, ULONG *pulActual)
2204{
2205 Connection *pConn = (Connection *)conn;
2206 Resource *pRes = pConn->pRes;
2207 int rc = 0;
2208 unsigned long done = 0;
2209 unsigned long onedone;
2210 unsigned long action;
2211 ULONG ulReadCompleted = 0;
2212
2213 ENTER();
2214
2215 debuglocal(9,"NdpFileRead in [%p]\n", pConn);
2216
2217 do {
2218 if (pConn->file.fd < 0)
2219 {
2220 rc = ERROR_INVALID_HANDLE;
2221 break;
2222 }
2223 while (ulReadCompleted < ulRead)
2224 {
2225 ULONG ulActual;
2226 ULONG ulToRead = ulRead - ulReadCompleted;
2227 debuglocal(9,"NdpFileRead completed %d, to read %d\n", ulReadCompleted, ulToRead);
2228 if (ulToRead > NDPSMB_READ_MAX_SIZE)
2229 {
2230 ulToRead = NDPSMB_READ_MAX_SIZE;
2231 }
2232 rc = smbwrp_read(pConn->cli, &pConn->file, (char *)pBuffer + ulReadCompleted, ulToRead, &ulActual);
2233 if (ulActual == 0 || rc != NO_ERROR)
2234 {
2235 break;
2236 }
2237 ulReadCompleted += ulActual;
2238 }
2239 //*pulActual = ulRead;
2240 //DosSleep(0);
2241
2242 } while (0);
2243
2244 if (ulReadCompleted > 0)
2245 {
2246 rc = NO_ERROR; /* Still were able to read some data. */
2247 }
2248
2249 if (rc == NO_ERROR)
2250 {
2251 *pulActual = ulReadCompleted;
2252 }
2253
2254 debuglocal(9,"NdpFileRead <%s> %lu %lu %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, ulRead, *pulActual, rc);
2255 LEAVE();
2256 return rc;
2257}
2258
2259int APIENTRY NdpFileWrite (HCONNECTION conn, NDFILEHANDLE handle, void *pBuffer, ULONG ulWrite, ULONG *pulActual)
2260{
2261 Connection *pConn = (Connection *)conn;
2262 Resource *pRes = pConn->pRes;
2263 int rc = 0;
2264 unsigned long done = 0;
2265 unsigned long onedone;
2266 unsigned long action;
2267
2268 ENTER();
2269
2270 debuglocal(9,"NdpFileWrite in [%p]\n", pConn);
2271
2272 /* delete the dir cache
2273 this was moved from NdpFileClose() becasue if there are a lot files in the tree all are reread
2274 the problem when moved to here is, that last accessed time is not refreshed
2275 if this is needed, a new function needs to be done to update only one file in the cache */
2276 dircache_invalidate(pConn->file.fullname, pRes->pdc, 1);
2277
2278 do {
2279 if (pConn->file.fd < 0)
2280 {
2281 rc = ERROR_INVALID_HANDLE;
2282 break;
2283 }
2284 rc = smbwrp_write(pConn->cli, &pConn->file, pBuffer, ulWrite, pulActual);
2285
2286 } while (0);
2287 debuglocal(9,"NdpFileWrite <%s> %lu %lu %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, ulWrite, *pulActual, rc);
2288 LEAVE();
2289 return rc;
2290}
2291
Note: See TracBrowser for help on using the repository browser.