source: branches/client-2.0/src/ndpsmb.c@ 522

Last change on this file since 522 was 520, checked in by Silvan Scherrer, 15 years ago

Samba Client 2.0: another bunch of updates

  • Property svn:eol-style set to native
File size: 54.1 KB
Line 
1/*
2 Netdrive Samba client plugin
3 plugin API
4 Copyright (C) netlabs.org 2003-2008
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <stdarg.h>
24#include <string.h>
25#include <time.h>
26
27#define NDPL_LARGEFILES
28#define INCL_LONGLONG
29#include <ndextpl2.h>
30#include "smbwrp.h"
31#include "util.h"
32
33#if 0
34
35#ifndef DEBUG_PRINTF
36#define debug_printf( ...)
37#endif
38
39#define log debug_printf
40#endif
41
42#define debug_printf(...) debuglocal(9, __VA_ARGS__)
43
44// -------------------------------------------------------------
45
46/* time conversion functions: SMB protocol sends timestamps in GMT time,
47* os2 api uses localtime,
48* emx/klibc uses timezone and daylight saving to convert GMT timestamps,
49* so only the timezone must be counted in conversion.
50*/
51void fsphUnixTimeToDosDate( time_t time, FDATE* fdate, FTIME *ftime)
52{
53 struct tm* gmt = localtime( &time);
54#if 0 // as localtime() already does dst we don't need to add something
55 if (gmt->tm_isdst>0) {
56 debug_printf( "daylight saving in effect %d, timezone %d\n",gmt->tm_isdst, timezone);
57 time -= 3600;
58 gmt = localtime( &time);
59 }
60#endif
61 fdate->day = gmt->tm_mday;
62 fdate->month = gmt->tm_mon+1;
63 fdate->year = gmt->tm_year + 1900 - 1980;
64 ftime->twosecs = gmt->tm_sec/2;
65 ftime->minutes = gmt->tm_min;
66 ftime->hours = gmt->tm_hour;
67 debug_printf("fsphUnixDateToDosDate\n");
68}
69
70void fsphDosDateToUnixTime( FDATE fdate, FTIME ftime, ULONG* time)
71{
72 struct tm gmtime = { 0 };
73
74 debug_printf( "fsphDosDateToUnixTime time %02d:%02d\n", ftime.hours, ftime.minutes);
75 gmtime.tm_mday = fdate.day;
76 gmtime.tm_mon = fdate.month-1;
77 gmtime.tm_year = fdate.year + 1980 - 1900;
78 gmtime.tm_sec = ftime.twosecs*2;
79 gmtime.tm_min = ftime.minutes;
80 gmtime.tm_hour = ftime.hours;
81 gmtime.tm_isdst = -1; // force libc to check dst saving
82
83 *time = mktime( &gmtime);
84 debug_printf( "fsphDosDateToUnixTime time1 %d %s", *time, ctime( (time_t*)time));
85#if 0 // as mktime() already does dst we don't need to add something
86 struct tm* gmt;
87 gmt = localtime( (time_t*) time);
88 if (gmt->tm_isdst>0) {
89 debug_printf( "fsphDosDateToUnixTime daylight saving in effect %d, timezone %d\n",gmt->tm_isdst, timezone);
90 *time += 3600;
91 }
92#endif
93 debug_printf( "fsphDosDateToUnixTime time2 %d %s", *time, ctime( (time_t*)time));
94}
95
96// -------------------------------------------------------------
97
98int StrLen(char * s)
99{
100 char * p;
101 if (!s)
102 {
103 return 0;
104 }
105 for (p = s; *p; p++);
106 return (p - s);
107}
108
109char * StrNCat(char *dst, const char *src, int count)
110{
111 int i;
112 if (!dst || !src || count <= 0)
113 {
114 return dst;
115 }
116 for (i = 0; dst[i]; i++);
117 for (;i < count && *src; i++, src++)
118 {
119 dst[i] = *src;
120 }
121 dst[i] = 0;
122 return dst;
123}
124
125char * StrNCpy(char *dst, const char *src, int count)
126{
127 if (!dst || !src || count <= 0)
128 {
129 return dst;
130 }
131 *dst = 0;
132 return StrNCat(dst, src, count);
133}
134
135char * StrCpy(char *dst, const char *src)
136{
137 char * p;
138 if (!dst || !src)
139 {
140 return dst;
141 }
142 p = dst;
143 while (*p++ = *src++);
144 return dst;
145}
146
147char * StrCat(char *dst, const char *src)
148{
149 int i;
150 if (!dst || !src)
151 {
152 return dst;
153 }
154 for (i = 0; dst[i]; i++);
155 for (; *src; i++, src++)
156 {
157 dst[i] = *src;
158 }
159 dst[i] = 0;
160 return dst;
161}
162
163void * MemCpy(void * dst, const void * src, int len)
164{
165 int i;
166 if (!src || !dst || len <= 0)
167 {
168 return dst;
169 }
170 for (i = 0; i < len; i++)
171 {
172 ((char *)dst)[i] = ((char *)src)[i];
173 }
174 return dst;
175}
176
177void *MemSet(void *dst, char c, int len)
178{
179 int i;
180 if (!dst || len <= 0)
181 {
182 return dst;
183 }
184 for (i = 0; i < len; i++)
185 {
186 ((char *)dst)[i] = c;
187 }
188 return dst;
189}
190
191// -------------------------------------------------------------
192
193/* uppercased type of resource */
194const char *NdpTypes[] =
195{
196 "SMBFS",
197 NULL
198}
199;
200
201/* Properties of supported resource types */
202
203/* Properties of resource */
204static const NDPROPERTYINFO smbProperties[] =
205{
206 {ND_PROP_STRING, 0, "WORKGROUP", ""},
207 {ND_PROP_STRING, 0, "SERVER", ""},
208 {ND_PROP_STRING, 0, "SHARE", ""},
209 {ND_PROP_STRING, 0, "USER", "guest"},
210 {ND_PROP_STRING, 0, "PASSWORD", ""},
211 {ND_PROP_STRING, 0, "SPASSWORD", ""},
212 {ND_PROP_STRING, 0, "MASTER", "WORKGROUP"},
213 {ND_PROP_ULONG, 0, "MASTERTYPE", "1"},
214 {ND_PROP_ULONG, 0, "CTO", "10"},
215 {ND_PROP_ULONG, 0, "CLD", "32"},
216 {ND_PROP_ULONG, 0, "EASUPPORT", "1"},
217 {ND_PROP_STRING, 0, NULL, NULL}
218};
219
220
221/* Exported array of properties */
222const NDPROPERTYINFO *NdpPropertiesInfo[] =
223{
224 smbProperties
225};
226
227
228PLUGINHELPERTABLE2L *ph;
229static int ifL;
230
231int APIENTRY NdpPluginLoad (PLUGINHELPERTABLE2L *pPHT)
232{
233 int rc;
234 HPIPE pipe;
235 unsigned long action;
236 ph = pPHT;
237 ifL = 0;
238/*
239 if (ph->cb < sizeof (PLUGINHELPERTABLE2))
240 {
241 return ERROR_INVALID_FUNCTION;
242 }
243*/
244 if (ph->cb >= sizeof (PLUGINHELPERTABLE2L))
245 {
246 ifL = 1;
247 }
248 debuglocal(9,"Working with %s bit fileio NDFS\n", ifL ? "64" : "32");
249 return NO_ERROR;
250}
251
252
253int APIENTRY NdpPluginFree (void)
254{
255 return NO_ERROR;
256}
257
258
259void getfindinfo(Connection * pConn, FILEFINDBUF3 * stat, smbwrp_fileinfo * finfo)
260{
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 StrNCpy(stat->achName, name, CCHMAXPATHCOMP - 1);
275 stat->cbFile = finfo->size;
276 stat->cbFileAlloc = stat->cbFile;
277 stat->oNextEntryOffset = 0ul;
278 stat->cchName = StrLen(stat->achName);
279 stat->attrFile = (finfo->attr & 0x37);
280
281 fsphUnixTimeToDosDate(finfo->mtime, &stat->fdateLastWrite, &stat->ftimeLastWrite);
282 fsphUnixTimeToDosDate(finfo->ctime, &stat->fdateCreation, &stat->ftimeCreation);
283 fsphUnixTimeToDosDate(finfo->atime, &stat->fdateLastAccess, &stat->ftimeLastAccess);
284}
285
286int getfindinfoL(Connection * pConn, void * plist, smbwrp_fileinfo * finfo, ULONG ulAttribute, char * mask)
287{
288 FILESTATUS3L stat = {0};
289 char * name = ph->fsphStrRChr(finfo->fname, '\\');
290 if (name)
291 {
292 name++;
293 }
294 else
295 {
296 name = finfo->fname;
297 }
298 if (!*name)
299 {
300 name = pConn->pRes->srv.share_name;
301 }
302 if (mask && (!ph->fsphAttrMatch(ulAttribute, finfo->attr & 0x37) || !ph->fsphWildMatch(mask, name, ND_IGNORE_CASE)))
303 {
304 return 0;
305 }
306
307 stat.cbFile = finfo->size;
308 stat.cbFileAlloc = stat.cbFile;
309 stat.attrFile = (finfo->attr & 0x37);
310
311 fsphUnixTimeToDosDate(finfo->mtime, &stat.fdateLastWrite, &stat.ftimeLastWrite);
312 fsphUnixTimeToDosDate(finfo->ctime, &stat.fdateCreation, &stat.ftimeCreation);
313 fsphUnixTimeToDosDate(finfo->atime, &stat.fdateLastAccess, &stat.ftimeLastAccess);
314 debug_printf( "fname %s\n", finfo->fname);
315 debug_printf( "mtime %d %s", finfo->mtime, ctime( (time_t*)&finfo->mtime));
316 debug_printf( "ftimeLastAccess %02d:%02d:%02d\n", stat.ftimeLastWrite.hours, stat.ftimeLastWrite.minutes, stat.ftimeLastWrite.twosecs*2);
317
318 ph->fsphAddFile32L(plist, &stat, name, StrLen(name), finfo, sizeof(*finfo), 0);
319 return 1;
320}
321
322static unsigned char fromhex (char c)
323{
324 if ('0' <= c && c <= '9')
325 {
326 return c - '0';
327 }
328
329 if ('A' <= c && c <= 'F')
330 {
331 return c - 'A' + 0xA;
332 }
333
334 if ('a' <= c && c <= 'f')
335 {
336 return c - 'a' + 0xA;
337 }
338
339 return 0;
340}
341
342static char tohex (unsigned char b)
343{
344 b &= 0xF;
345
346 if (b <= 9)
347 {
348 return b + '0';
349 }
350
351 return 'A' + (b - 0xA);
352}
353
354static void decryptPassword (const char *pszCrypt, char *pszPlain)
355{
356 /* A simple "decryption", character from the hex value. */
357 const char *s = pszCrypt;
358 char *d = pszPlain;
359
360 while (*s)
361 {
362 *d++ = (char)((fromhex (*s++) << 4) + fromhex (*s++));
363 }
364
365 *d++ = 0;
366}
367
368static void encryptPassword (const char *pszPlain, char *pszCrypt)
369{
370 /* A simple "encryption" encode each character as hex value. */
371 const char *s = pszPlain;
372 char *d = pszCrypt;
373
374 while (*s)
375 {
376 *d++ = tohex ((*s) >> 4);
377 *d++ = tohex (*s);
378 s++;
379 }
380
381 *d++ = 0;
382}
383
384/* accept parameters in form
385 * [filename][;name=filename]
386 */
387int initResource (Resource *pRes, NDPROPERTYHANDLE properties)
388{
389 int rc = NO_ERROR;
390 unsigned long t;
391 const CHAR * q = NULL;
392 int defaultPassword = 1;
393
394 pRes->rootlevel = 0;
395 pRes->easupport = 1;
396#ifdef HAVE_KRB5_H
397 pRes->krb5support = 1;
398#else
399 pRes->krb5support = 0;
400#endif
401 pRes->pdc = NULL;
402
403 t = 0, q = NULL;
404 rc = ph->fsphQueryStringProperty (properties, "WORKGROUP", &q, &t);
405 if (!rc && t && *q)
406 {
407 StrNCpy(pRes->srv.workgroup, q, sizeof(pRes->srv.workgroup) - 1);
408 pRes->rootlevel = 1;
409 }
410
411 t = 0, q = NULL;
412 rc = ph->fsphQueryStringProperty (properties, "SERVER", &q, &t);
413 if (!rc && t && *q)
414 {
415 StrNCpy(pRes->srv.server_name, q, sizeof(pRes->srv.server_name) - 1);
416 pRes->rootlevel = 2;
417 }
418
419 t = 0, q = NULL;
420 rc = ph->fsphQueryStringProperty (properties, "SHARE", &q, &t);
421 if (!rc && t && *q)
422 {
423 StrNCpy(pRes->srv.share_name, q, sizeof(pRes->srv.share_name) - 1);
424 pRes->rootlevel = 3;
425 }
426
427 t = 0, q = NULL;
428 rc = ph->fsphQueryStringProperty (properties, "USER", &q, &t);
429 if (!rc && t && *q)
430 {
431 StrNCpy(pRes->srv.username, q, sizeof(pRes->srv.username) - 1);
432 }
433
434 t = 0, q = NULL;
435 rc = ph->fsphQueryStringProperty (properties, "PASSWORD", &q, &t);
436 if (!rc && t && *q)
437 {
438 StrNCpy(pRes->srv.password, q, sizeof(pRes->srv.password) - 1);
439 defaultPassword = 0;
440 }
441
442 t = 0, q = NULL;
443 rc = ph->fsphQueryStringProperty (properties, "SPASSWORD", &q, &t);
444 if ( rc == NO_ERROR
445 && *q != '\0'
446 && defaultPassword)
447 {
448 char p[1024];
449 p[0] = 0;
450
451 decryptPassword (q, p);
452
453 if (*p)
454 {
455 StrNCpy(pRes->srv.password, p, sizeof(pRes->srv.password) - 1);
456
457 /* clear the plain password */
458 ph->fsphSetProperty (properties, "PASSWORD", "");
459 }
460 }
461 else
462 {
463 char c[1024];
464 encryptPassword (pRes->srv.password, c);
465
466 ph->fsphSetProperty (properties, "SPASSWORD", c);
467
468 // clear the plain password
469 ph->fsphSetProperty (properties, "PASSWORD", "");
470 }
471
472 t = 0, q = NULL;
473 rc = ph->fsphQueryStringProperty (properties, "MASTER", &q, &t);
474 if (!rc && t && *q)
475 {
476 StrNCpy(pRes->srv.master, q, sizeof(pRes->srv.master) - 1);
477 }
478
479 t = 0, q = NULL;
480
481 t = 0;
482 rc = ph->fsphQueryUlongProperty (properties, "MASTERTYPE", &t);
483 if (!rc)
484 {
485 if (t > 1)
486 {
487 rc = ERROR_INVALID_PARAMETER;
488 }
489 else
490 {
491 pRes->srv.ifmastergroup = t;
492 }
493 }
494
495 t = 0;
496 rc = ph->fsphQueryUlongProperty (properties, "EASUPPORT", &t);
497 if (!rc)
498 {
499 if (t > 1)
500 {
501 rc = ERROR_INVALID_PARAMETER;
502 }
503 else
504 {
505 pRes->easupport = t;
506 }
507 }
508
509 t = 0;
510 rc = ph->fsphQueryUlongProperty (properties, "CTO", &t);
511 if (!rc)
512 {
513 if (t > 600)
514 {
515 rc = ERROR_INVALID_PARAMETER;
516 }
517 else
518 {
519 pRes->cachetimeout = t;
520 }
521 }
522
523 t = 0;
524 rc = ph->fsphQueryUlongProperty (properties, "CLD", &t);
525 if (!rc)
526 {
527 if (t > 96)
528 {
529 rc = ERROR_INVALID_PARAMETER;
530 }
531 else
532 {
533 pRes->cachedepth = t;
534 }
535 }
536
537 /*
538 * Create a directory cache with expiration time and cache listings
539 * the above values come from the gui. default: timeout 10; listings: 32
540 */
541 dircache_create(&pRes->pdc, pRes->cachetimeout, pRes->cachedepth);
542
543 return rc;
544}
545
546int iftestpath(char * path)
547{
548 char * p = path;
549 if (!path)
550 {
551 return 0;
552 }
553 while ((p = ph->fsphStrChr(p, 'A')) != NULL)
554 {
555 if (ph->fsphStrNCmp(p, "A.+,;=[].B", 10) == 0)
556 {
557 return 1;
558 }
559 p++;
560 }
561 return 0;
562}
563
564int pathparser(Resource *pRes, Connection * pConn, char * path, char * result)
565{
566 int rootlevel;
567 int rc = NO_ERROR;
568 if (!pRes || !path || !result)
569 {
570 return ERROR_INVALID_PARAMETER;
571 }
572 // handle special case when someone wants to test support of LFN or smth similar
573 if (iftestpath(path))
574 {
575 StrCpy(result, "\\A.+,;=[].B");
576 return NO_ERROR;
577 }
578
579 rootlevel = pRes->rootlevel;
580 if (*path == '\\') path++;
581
582 if (rootlevel < 3)
583 {
584 char * p;
585 // flag: 1 parameters changed, reconnection required, 0 do nothing
586 int newlevel = 0;
587 // use a temporary resource to test disconnection/reconnection
588 Resource tmpRes;
589 // copy exising data
590 memcpy( &tmpRes, pRes, sizeof( tmpRes));
591 // pointer to new connection fields
592 smbwrp_server * tmp = &tmpRes.srv;
593 if (rootlevel == 0)
594 {
595 p = ph->fsphStrChr(path, '\\');
596 if (!p)
597 {
598 p = path + StrLen(path);
599 }
600 if (StrLen(tmp->workgroup) != p - path
601 || (p == path || ph->fsphStrNICmp(path, tmp->workgroup, p - path)))
602 {
603 StrNCpy(tmp->workgroup, path, p - path);
604 tmp->workgroup[p - path] = 0;
605 newlevel = 1;
606 }
607 path = *p == '\\' ? p + 1 : p;
608 rootlevel = 1;
609 }
610 if (rootlevel == 1) // root path starts from server name
611 {
612 p = ph->fsphStrChr(path, '\\');
613 if (!p)
614 {
615 p = path + StrLen(path);
616 }
617 if (StrLen(tmp->server_name) != p - path
618 || (p == path || ph->fsphStrNICmp(path, tmp->server_name, p - path)))
619 {
620 StrNCpy(tmp->server_name, path, p - path);
621 tmp->server_name[p - path] = 0;
622 newlevel = 1;
623 }
624 path = *p == '\\' ? p + 1 : p;
625 rootlevel = 2;
626 }
627 if (rootlevel == 2) // root path starts from share name
628 {
629 p = ph->fsphStrChr(path, '\\');
630 if (!p)
631 {
632 p = path + StrLen(path);
633 }
634 if (StrLen(tmp->share_name) != (p - path)
635 || (p == path || ph->fsphStrNICmp(path, tmp->share_name, p - path)))
636 {
637 StrNCpy(tmp->share_name, path, p - path);
638 tmp->share_name[p - path] = 0;
639 newlevel = 1;
640 }
641 path = *p == '\\' ? p + 1 : p;
642 }
643 if (newlevel)
644 {
645 // reconnect to server here, first test new connection
646 cli_state* tmp_cli = NULL;
647 rc = smbwrp_connect( &tmpRes, &tmp_cli);
648 if (!rc)
649 {
650 // new connection is ok, disconnect old one
651 cli_state* cli = pConn->cli;
652 smbwrp_disconnect( pRes, cli);
653 // save tmp data structure
654 memcpy( pRes, &tmpRes, sizeof( tmpRes));
655 // save new connection handle
656 pConn->cli = tmp_cli;
657 }
658 }
659 }
660
661 StrCpy(result, "\\");
662 StrNCat(result, path, CCHMAXPATH);
663
664 return rc;
665}
666
667
668// -------------------------------------------------------------
669
670/* check if the requested resource is available */
671static int checkMountResource( Resource* pRes)
672{
673 int rc;
674 unsigned long action;
675 cli_state* cli = NULL;
676 smbwrp_file file;
677
678 debug_printf("checkMountResource in tid#%d\n", _gettid());
679 rc = smbwrp_connect( pRes, &cli);
680/* changed to real error codes SCS
681 if (rc)
682 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_ACCESS_DENIED); */
683 switch (rc) {
684 case 0:
685 rc = NO_ERROR;
686 break;
687 case 1:
688 case 10:
689 case 11:
690 rc = ERROR_BAD_NET_NAME;
691 break;
692 case 2:
693 rc = ERROR_INIT_ROUTINE_FAILED;
694 break;
695 case 3:
696 rc = ERROR_BAD_NET_RESP;
697 break;
698 case 4:
699 rc = ERROR_NETWORK_BUSY;
700 break;
701 case 6:
702 rc = ERROR_NETWORK_ACCESS_DENIED;
703 break;
704 case 7:
705 rc = ERROR_BAD_NETPATH;
706 break;
707 default:
708 rc = ERROR_UNEXP_NET_ERR;
709 break;
710 } /* endswitch */
711
712 smbwrp_disconnect( pRes, cli);
713
714 return rc;
715}
716
717int APIENTRY NdpMountResource (HRESOURCE *presource, int type, NDPROPERTYHANDLE properties)
718{
719 int rc = NO_ERROR;
720 unsigned long objany = OBJ_ANY;
721 Resource *pRes = NULL;
722
723 debuglocal(9,"NdpMountResource in\n");
724
725 // init code
726 smbwrp_init();
727
728 /* since samba plugin support only 1 type of resources we do not need */
729 /* to check what the found type really is */
730 pRes = malloc( sizeof(Resource));
731 if (pRes == NULL)
732 {
733 rc = ERROR_NOT_ENOUGH_MEMORY;
734 }
735 else
736 {
737 MemSet(pRes, 0, sizeof(Resource));
738 //pRes->objany = objany;
739 // parse init string
740 rc = initResource (pRes, properties);
741 // try to connect to resource (check type) only if thread!=1, so ndctl startup
742 // is not slowed down by network connections.
743 // ndctl does mounting on main thread (#1)
744 // nd/ndpm do not use main thread
745 if (!rc && _gettid()!=1)
746 rc = checkMountResource( pRes);
747 if (!rc)
748 {
749 // store resource data
750 *presource = (HRESOURCE)pRes;
751 }
752 else
753 {
754 free(pRes);
755 }
756 }
757 debuglocal(9,"NdpMountResource rc=%d\n", rc);
758 return rc;
759}
760
761// -------------------------------------------------------------
762
763int APIENTRY NdpFreeResource (HRESOURCE resource)
764{
765 Resource *pRes = (Resource *)resource;
766 dircache_delete(pRes->pdc);
767 MemSet(&pRes->srv, 0, sizeof(pRes->srv));
768 free(pRes);
769 debuglocal(9,"NdpFreeResource %d\n", NO_ERROR);
770 return NO_ERROR;
771}
772
773// -------------------------------------------------------------
774
775int APIENTRY NdpRsrcCompare (HRESOURCE resource, HRESOURCE resource2)
776{
777 Resource *pRes = (Resource *)resource;
778 Resource *pRes2 = (Resource *)resource2;
779 int rc = ND_RSRC_DIFFERENT;
780
781 debuglocal(9,"NdpRsrcCompare in\n");
782 if (ph->fsphStrICmp(pRes->srv.server_name, pRes2->srv.server_name) == 0
783 && ph->fsphStrICmp(pRes->srv.share_name, pRes2->srv.share_name) == 0
784 && ph->fsphStrICmp(pRes->srv.username, pRes2->srv.username) == 0
785 && ph->fsphStrICmp(pRes->srv.workgroup, pRes2->srv.workgroup) == 0)
786 {
787 // resources are equal
788 rc = ND_RSRC_EQUAL;
789 }
790
791 debuglocal(9,"NdpRsrcCompare %d\n", rc);
792
793 return rc;
794}
795
796int APIENTRY NdpRsrcUpdate (HRESOURCE resource, HRESOURCE resource2)
797{
798 // do nothing
799 debuglocal(9,"NdpRsrcUpdate %d\n", NO_ERROR);
800 return NO_ERROR;
801}
802
803int APIENTRY NdpRsrcQueryInfo (HRESOURCE resource, ULONG *pulFlags, void *pdata, ULONG insize, ULONG *poutlen)
804{
805 Resource *pRes = (Resource *)resource;
806 int rc = NO_ERROR;
807 char s[4096];
808
809 debuglocal(9,"NdpRsrcQueryInfo in\n");
810
811 switch (pRes->rootlevel)
812 {
813 case 0:
814 {
815 ph->fsph_snprintf(s, sizeof(s) - 1, "SMBFS%s \\\\@%s", ifL ? "64" : "32", pRes->srv.username);
816 } break;
817 case 1:
818 {
819 ph->fsph_snprintf(s, sizeof(s) - 1, "SMBFS%s %s: \\\\@%s", ifL ? "64" : "32", pRes->srv.workgroup, pRes->srv.username);
820 } break;
821 case 2:
822 {
823 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);
824 } break;
825 default:
826 {
827 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);
828 } break;
829 }
830 *poutlen = StrLen(s) + 1;
831 if (*poutlen > insize)
832 {
833 rc = ERROR_BUFFER_OVERFLOW;
834 }
835 else
836 {
837 MemCpy(pdata, s, *poutlen);
838 }
839
840 debuglocal(9,"NdpRsrcQueryInfo %d\n", rc);
841
842 return rc;
843}
844
845int APIENTRY NdpRsrcQueryFSAttach (HRESOURCE resource, void *pdata, ULONG insize, ULONG *poutlen)
846{
847 ULONG ulDummy = 0;
848 /* just return the resource info string */
849 return NdpRsrcQueryInfo (resource, &ulDummy, pdata, insize, poutlen);
850}
851
852int APIENTRY NdpRsrcQueryFSAllocate (HRESOURCE resource, NDFSALLOCATE *pfsa)
853{
854 Resource *pRes = (Resource *)resource;
855 int rc = NO_ERROR, rc1;
856 unsigned long action = 0;
857 smbwrp_file file;
858 cli_state* cli = NULL;
859 FSALLOCATE fsa;
860
861 debuglocal(9,"NdpRsrcQueryFSAllocate %08x\n", pfsa);
862
863 if (!pfsa)
864 {
865 return NO_ERROR;
866 }
867
868 debug_printf("checkMountResource in tid#%d\n", _gettid());
869 rc = smbwrp_connect( pRes, &cli);
870 if (rc)
871 {
872 debuglocal(9,"NdpCreateConnection failed rc=%d\n", rc);
873 pfsa->cSectorUnit = 1;
874 pfsa->cUnit = 123456;
875 pfsa->cUnitAvail = 123456;
876 pfsa->cbSector = 2048;
877 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_ACCESS_DENIED);
878 return rc;
879 }
880
881 rc = smbwrp_dskattr( cli, &fsa);
882 if (rc)
883 {
884 pfsa->cSectorUnit = 1;
885 pfsa->cUnit = 123456;
886 pfsa->cUnitAvail = 123456;
887 pfsa->cbSector = 2048;
888 //rc = rc ? rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
889 }
890 else
891 {
892 pfsa->cSectorUnit = fsa.cSectorUnit;
893 pfsa->cUnit = fsa.cUnit;
894 pfsa->cUnitAvail = fsa.cUnitAvail;
895 pfsa->cbSector = fsa.cbSector;
896 }
897
898 smbwrp_disconnect( pRes, cli);
899
900 debuglocal(9,"NdpRsrcQueryFSAllocate %d/%d (cUnit = %d/cUnitAvail = %d/cbSector = %d)\n", rc, rc1, pfsa->cUnit, pfsa->cUnitAvail, pfsa->cbSector);
901 return rc;
902}
903
904// -------------------------------------------------------------
905
906int APIENTRY NdpCreateConnection (HRESOURCE resource, HCONNECTION *pconn)
907{
908 int rc = 0;
909 Resource * pRes = (Resource *)resource;
910 unsigned long action;
911 Connection *pConn = NULL;
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 return rc;
924 }
925 MemSet(pConn, 0, sizeof(Connection));
926 pConn->pRes = pRes;
927 pConn->file.fd = -1;
928
929 debuglocal(9,"NdpCreateConnection send CONNECT\n");
930 rc = smbwrp_connect( pRes, &pConn->cli);
931 if (rc)
932 {
933 free(pConn);
934 pConn = NULL;
935 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_INVALID_PARAMETER);
936 }
937
938 *pconn = (HCONNECTION)pConn;
939 debuglocal(9,"NdpCreateConnection [%p] %d\n", pConn, rc);
940 return rc;
941}
942
943// -------------------------------------------------------------
944
945int APIENTRY NdpFreeConnection (HCONNECTION conn)
946{
947 Connection *pConn = (Connection *)conn;
948 Resource *pRes = pConn->pRes;
949 int rc;
950
951 debuglocal(9,"NdpFreeConnection in [%p]\n", pConn);
952 if (pConn->file.fd >= 0)
953 {
954 rc = smbwrp_close( pConn->cli, &pConn->file);
955 pConn->file.fd = -1;
956 }
957
958 smbwrp_disconnect( pRes, pConn->cli);
959
960 free(pConn);
961 debuglocal(9,"NdpFreeConnection %d\n", NO_ERROR);
962 return NO_ERROR;
963}
964
965// -------------------------------------------------------------
966
967/*
968 * NdpQueryPathInfo is the most important function :) netdrive always calls
969 * the function before every operation to find out the path status: does it exist, is it a file, does a
970 * parent directory exist, etc.
971 * Plugin must return one of the following error codes:
972 * NO_ERROR - path exists and the path information have been successfully retrieved.
973 * ERROR_FILE_NOT_FOUND - all but the last component of the path exist and the
974 * path without the last component is a directory. dir1_ok\dir2_ok\does_not_exist.
975 * the wildcard can not exist, so the plugin returns FILE_NOT_FOUND, if the parent
976 * directory exist.
977 * ERROR_PATH_NOT_FOUND - any of not last path components does not exist, or all
978 * but the last component exist and is a file: \dir_ok\dir2_ok\file_ok\non_existing.
979 * ERROR_REM_NOT_LIST - resource is temporarily unavailable for some reasons.
980 * Any other error codes means an internal plugin error, not related to the status
981 * of the path queried.
982 */
983int APIENTRY NdpQueryPathInfo (HCONNECTION conn, void *plist, char *szPath)
984{
985 Connection *pConn = (Connection *)conn;
986 Resource *pRes = pConn->pRes;
987 smbwrp_fileinfo finfo;
988 int rc = 0;
989 unsigned long action;
990 char path[CCHMAXPATH+1] = {0};
991 int retry = 0;
992
993 debuglocal(9,"NdpQueryPathInfo in [%p] <%s>, retry = %d\n", pConn, szPath, retry);
994
995 // is wildcard is specified, we suppose parent dir exist, so exit immediately
996 if (ph->fsphStrChr(szPath, '*') || ph->fsphStrChr(szPath, '?'))
997 {
998 return ERROR_FILE_NOT_FOUND;
999 }
1000
1001
1002 do {
1003 /* First check if there is information in the directory cache. */
1004 unsigned long ulAge = 0;
1005 if (dircache_find_path(pRes->pdc, szPath, &finfo, &ulAge))
1006 {
1007 if (ulAge <= 15) /* @todo configurable. */
1008 {
1009 rc = NO_ERROR;
1010 finfo.easize = -1;
1011 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1012 break;
1013 }
1014 }
1015
1016 rc = pathparser(pRes, pConn, szPath, path);
1017 debuglocal(9,"NdpQueryPathInfo pathparser for <%s> rc=%d\n", path, rc);
1018 switch (rc)
1019 {
1020 case NO_ERROR :
1021 case ERROR_FILE_NOT_FOUND:
1022 case ERROR_PATH_NOT_FOUND:
1023 case ERROR_ACCESS_DENIED:
1024 case ERROR_INVALID_PARAMETER:
1025 {
1026 break;
1027 }
1028 default :
1029 {
1030 rc = ERROR_PATH_NOT_FOUND;
1031 }
1032 }
1033 if (rc)
1034 {
1035 break;
1036 }
1037 StrNCpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1038 debuglocal(9,"NdpQueryPathInfo smbwrp_getattr for <%s>\n", path);
1039 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
1040 if (rc)
1041 {
1042 // remote server not available for first time?
1043 if (rc == ERROR_REM_NOT_LIST && retry == 0)
1044 {
1045 // free current cli resources
1046 smbwrp_disconnect( pRes, pConn->cli);
1047 // reconnect
1048 smbwrp_connect( pRes, &pConn->cli);
1049 // try file list again
1050 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
1051 debuglocal(9,"NdpQueryPathInfo remote connection lost, retry rc = %d\n", rc);
1052 }
1053 switch (rc)
1054 {
1055 case NO_ERROR :
1056 case ERROR_FILE_NOT_FOUND:
1057 case ERROR_PATH_NOT_FOUND:
1058 case ERROR_ACCESS_DENIED:
1059 case ERROR_INVALID_PARAMETER:
1060 case ERROR_REM_NOT_LIST:
1061 break;
1062 default :
1063 {
1064 rc = ERROR_PATH_NOT_FOUND;
1065 }
1066 }
1067 }
1068 else
1069 {
1070 finfo.easize = -1;
1071 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1072 }
1073 if (rc == ERROR_FILE_NOT_FOUND)
1074 {
1075 // now try the upper path
1076 char * p = ph->fsphStrChr(finfo.fname, '\\');
1077 if (p && p > finfo.fname)
1078 {
1079 *p = 0;
1080 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
1081 if (rc)
1082 {
1083 debuglocal(9,"NdpQueryPathInfo upper path in <%s>, retry = %d\n", finfo.fname, retry);
1084 rc = rc ? ERROR_PATH_NOT_FOUND : ERROR_INVALID_PARAMETER;
1085 }
1086 }
1087 }
1088 } while (0);
1089 debuglocal(9,"NdpQueryPathInfo <%s> (%s) %d\n", szPath, path, rc);
1090
1091 return rc;
1092}
1093
1094// -------------------------------------------------------------
1095
1096int APIENTRY NdpFindStart (HCONNECTION conn, void *plist, NDFILEINFOL *pfiparent, char *szPath, ULONG ulAttribute)
1097{
1098 Connection *pConn = (Connection *)conn;
1099 Resource *pRes = pConn->pRes;
1100 int rc = NO_ERROR, count = 0;
1101 unsigned long action;
1102 char *mask = "*";
1103 char dir[CCHMAXPATH+1] = {0};
1104 char path[CCHMAXPATH+1] = {0};
1105 smbwrp_fileinfo * data;
1106 NDPATHELEMENT *pel = ph->fsphNameElem(0);
1107 filelist_state state;
1108 char * p;
1109
1110 debug_printf("NdpFindStart in [%p]\n", pConn);
1111
1112 StrNCpy(dir, szPath, sizeof(dir) - 1);
1113 if (pel)
1114 {
1115 mask = pel->name;
1116 dir[StrLen(szPath) - pel->length] = 0;
1117 }
1118 action = StrLen(dir) - 1;
1119 if (dir[action] == '\\')
1120 {
1121 dir[action] = 0;
1122 }
1123 rc = pathparser(pRes, pConn, dir, path);
1124 if (rc)
1125 {
1126 return rc;
1127 }
1128 action = StrLen(path) - 1;
1129 if (path[action] != '\\')
1130 {
1131 StrNCat(path, "\\", sizeof(path) - 1);
1132 }
1133 StrCpy(dir, path);
1134 StrNCat(path, mask, sizeof(path) - 1);
1135
1136 // this structure will be used by libsmb callbacks, so we store here all we need
1137 // to fill netdrive structures
1138 state.pConn = pConn;
1139 state.plist = plist;
1140 state.ulAttribute = ulAttribute;
1141 strcpy( state.dir, dir);
1142 strcpy( state.dir_mask, mask);
1143 strcpy( state.mask, path);
1144 state.fullpath = szPath;
1145 /* This plugin always reads a complete directory listing and filters results
1146 * using actual mask (state.dir_mask) in getfindinfoL.
1147 * May be this was a workaround for some server bug.
1148 * If this will be changed, then directory listing cache must be changed too,
1149 * and must remember the mask, which was used to obtain a listing.
1150 * Now the directory cache saves complete directory listings and then uses them to find
1151 * information about single files.
1152 * However, with a directory cache, it is probably faster to get a full listing and
1153 * then use it to obtain info about separate files than to perform a network
1154 * list query operation using actual wild cards for each file. Some application,
1155 * for example OpenOffice, do this.
1156 */
1157 p = getlastslash(state.mask);
1158 if (p)
1159 {
1160 *(p + 1) = '*';
1161 *(p + 2) = 0;
1162 }
1163 else
1164 {
1165 strcpy(state.mask, "\\*");
1166 }
1167 debuglocal(9,"NdpFindStart: dir [%s], dir_mask [%s], mask [%s], szPath [%s]\n",
1168 state.dir, state.dir_mask, state.mask, state.fullpath);
1169 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1170 // we need to handle reconnection also here, because NdpQueryPathInfo
1171 // could be called with '*' and exit then immediately (without calling libsmb)
1172 if (rc == ERROR_REM_NOT_LIST)
1173 {
1174 // free current cli resources
1175 smbwrp_disconnect( pRes, pConn->cli);
1176 // reconnect
1177 smbwrp_connect( pRes, &pConn->cli);
1178 // try file list again next loop
1179 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1180 debuglocal(9,"NdpFindStart remote connection lost, retry rc = %d\n", rc);
1181 }
1182
1183 debuglocal(9,"NdpFindStart <%s> (%s) cnt %d %d\n", szPath, path, count, rc);
1184
1185 return rc;
1186}
1187
1188int APIENTRY NdpDeletePathInfo (HRESOURCE resource, NDFILEINFOL *pfi)
1189{
1190// debuglocal(9,"NdpDeletePathInfo %d\n", 0);
1191 return NO_ERROR;
1192}
1193
1194int APIENTRY NdpRefresh (HCONNECTION conn, char *path, int tree)
1195{
1196 debuglocal(9,"NdpRefresh <%s> %d\n", path, 0);
1197 return NO_ERROR;
1198}
1199
1200int APIENTRY NdpDiscardResourceData (HRESOURCE resource, NDDATABUF *pdatabuf)
1201{
1202 // The plugin do not have to deallocate anything
1203 // because resource data did not contain any pointers
1204 // to plugins data.
1205 // Data stored by fsphSetResourceData will be
1206 // deallocated by NetDrive.
1207
1208 debuglocal(9,"NdpDicardresourceData %d\n", 0);
1209 return NO_ERROR;
1210}
1211
1212int APIENTRY NdpSetPathInfo (HCONNECTION conn, NDFILEINFOL *pfi, char *szPathName)
1213{
1214 Connection *pConn = (Connection *)conn;
1215 Resource *pRes = pConn->pRes;
1216 int rc = 0;
1217 unsigned long action;
1218 char path[CCHMAXPATH+1] = {0};
1219 smbwrp_fileinfo finfo;
1220
1221 debug_printf("NdpSetPathInfo in [%p]\n", pConn);
1222
1223 // delete the dir cache
1224 dircache_invalidate(szPathName, pRes->pdc, 1);
1225
1226 do {
1227 rc = pathparser(pRes, pConn, szPathName, path);
1228 if (rc)
1229 {
1230 break;
1231 }
1232
1233 MemSet(&finfo, 0, sizeof(finfo));
1234
1235 StrNCpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1236 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(finfo.mtime));
1237 finfo.attr = pfi->stat.attrFile & 0x37;
1238 rc = smbwrp_setattr(pConn->cli, &finfo);
1239 } while (0);
1240 debuglocal(9,"NdpSetPathInfo <%s> (%s) %d\n", szPathName, path, rc);
1241
1242 return rc;
1243}
1244
1245int buildFEALIST(FEALIST *pFEASrc, GEALIST *pGEAList, FEALIST *pFEAList)
1246{
1247 int rc = 0;
1248 FEA * pfea;
1249 FEA * pfeadest;
1250 unsigned long size, done = sizeof(pFEAList->cbList), dsize, ddone = sizeof(pFEAList->cbList);
1251
1252 size = pFEASrc->cbList;
1253 pfea = pFEASrc->list;
1254 pfeadest = pFEAList->list;
1255 dsize = pFEAList->cbList;
1256//debuglocal(9,"buildFEALIST in destsize %d srcsize %d pGEAList=%08x pGEAList->cbList=%d\n", dsize, ddone, size, pGEAList, pGEAList ? pGEAList->cbList : 0);
1257 while (done < size)
1258 {
1259 char * name = (char *)(pfea + 1);
1260 int insert = 1;
1261 if (pGEAList && pGEAList->cbList > sizeof(pGEAList->cbList))
1262 {
1263 GEA * pgea = pGEAList->list;
1264 unsigned long size = pGEAList->cbList - sizeof(pGEAList->cbList), done = 0;
1265 insert = 0;