source: trunk/client/src/smbwrp.c@ 884

Last change on this file since 884 was 883, checked in by Silvan Scherrer, 10 years ago

Samba client: update trunk to 3.6 server level, change client version to 2.3

  • Property svn:eol-style set to native
File size: 30.0 KB
Line 
1/*
2 Netdrive Samba client plugin
3 samba library wrappers
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 "includes.h"
22#include "rpc_client/cli_pipe.h"
23#include "librpc/gen_ndr/ndr_srvsvc_c.h"
24#include "libsmb/libsmb.h"
25#include "libsmb/clirap.h"
26#include "trans2.h"
27#include "smbwrp.h"
28
29/*
30 * Wrapper for cli_errno to return not connected error on negative fd
31 * Now returns an OS/2 return code instead of lerrno.
32 */
33int os2cli_errno(cli_state * cli)
34{
35 if (cli->fd == -1)
36 {
37 return maperror( ENOTCONN);
38 }
39 return maperror(cli_errno(cli));
40}
41
42void smbwrp_Logging()
43{
44 char slogfile[_MAX_PATH +1] = {0};
45 char slogfilename[] = "log.smbc";
46 char *env = getenv("LOGFILES");
47 if (env != NULL)
48 {
49 strncpy(slogfile, env, sizeof(slogfile) -1);
50 strncat(slogfile, "\\", sizeof(slogfile) - strlen(slogfile) -1);
51 strncat(slogfile, slogfilename, sizeof(slogfile) - strlen(slogfile) -1);
52 }
53 else
54 {
55 strncpy(slogfile, slogfilename, sizeof(slogfile) -1);
56 }
57
58 // init samba for debug messages
59 setup_logging(slogfile, false);
60 lp_set_logfile(slogfile);
61 debug_parse_levels("10");
62
63}
64const char * smbwrp_getVersion()
65{
66 return samba_version_string();
67}
68
69int _System smbwrp_getclisize(void)
70{
71 return sizeof(struct cli_state);
72}
73
74/*****************************************************
75initialise structures
76*******************************************************/
77int _System smbwrp_init(void)
78{
79 static int initialised = 0;
80 char *p;
81
82 if (initialised)
83 {
84 return 0;
85 }
86 initialised = 1;
87
88 lp_set_in_client(true); /* Make sure that we tell lp_load we are client */
89
90 load_case_tables();
91
92 if (!lp_load(get_dyn_CONFIGFILE(),true,false,false,true)) {
93 debuglocal(0,("The initial smb.conf is missing, defaults are used!\n"));
94 }
95
96 load_interfaces();
97
98 if (!init_names())
99 {
100 return 1;
101 }
102
103 if (writeLog())
104 {
105 smbwrp_Logging();
106 }
107
108/*
109 if ((p=smbw_getshared("RESOLVE_ORDER"))) {
110 lp_set_name_resolve_order(p);
111 }
112*/
113 return 0;
114
115}
116
117void smbwrp_initthread(void)
118{
119 /*
120 Block SIGPIPE (from lib/util_sock.c: write())
121 It is not needed and should not stop execution
122 */
123 BlockSignals(True, SIGPIPE);
124}
125
126#if 0
127/*****************************************************
128remove redundent stuff from a filename
129*******************************************************/
130void clean_fname(char *name)
131{
132 char *p, *p2;
133 int l;
134 int modified = 1;
135
136 if (!name) return;
137
138 while (modified) {
139 modified = 0;
140
141 if ((p=strstr(name,"/./"))) {
142 modified = 1;
143 while (*p) {
144 p[0] = p[2];
145 p++;
146 }
147 }
148
149 if ((p=strstr(name,"//"))) {
150 modified = 1;
151 while (*p) {
152 p[0] = p[1];
153 p++;
154 }
155 }
156
157 if (strcmp(name,"/../")==0) {
158 modified = 1;
159 name[1] = 0;
160 }
161
162 if ((p=strstr(name,"/../"))) {
163 modified = 1;
164 for (p2=(p>name?p-1:p);p2>name;p2--) {
165 if (p2[0] == '/') break;
166 }
167 while (*p2) {
168 p2[0] = p2[3];
169 p2++;
170 }
171 }
172
173 if (strcmp(name,"/..")==0) {
174 modified = 1;
175 name[1] = 0;
176 }
177
178 l = strlen(name);
179 p = l>=3?(name+l-3):name;
180 if (strcmp(p,"/..")==0) {
181 modified = 1;
182 for (p2=p-1;p2>name;p2--) {
183 if (p2[0] == '/') break;
184 }
185 if (p2==name) {
186 p[0] = '/';
187 p[1] = 0;
188 } else {
189 p2[0] = 0;
190 }
191 }
192
193 l = strlen(name);
194 p = l>=2?(name+l-2):name;
195 if (strcmp(p,"/.")==0) {
196 if (p == name) {
197 p[1] = 0;
198 } else {
199 p[0] = 0;
200 }
201 }
202
203 if (strncmp(p=name,"./",2) == 0) {
204 modified = 1;
205 do {
206 p[0] = p[2];
207 } while (*p++);
208 }
209
210 l = strlen(p=name);
211 if (l > 1 && p[l-1] == '/') {
212 modified = 1;
213 p[l-1] = 0;
214 }
215 }
216}
217#endif
218
219/*****************************************************
220return a connection to a server
221*******************************************************/
222int _System smbwrp_connect( Resource* pRes, cli_state ** cli)
223{
224 smbwrp_server * srv = &pRes->srv;
225 char * server = srv->server_name;
226 char * share = *(srv->share_name) ? srv->share_name : "IPC$";
227 char * workgroup = srv->workgroup;
228 struct nmb_name called, calling;
229 char *p, *server_n = server;
230 fstring group;
231 struct sockaddr_storage ss;
232 NTSTATUS rc;
233 struct cli_state * c;
234 char* dev_type;
235 int loginerror = 0;
236 NTSTATUS status;
237
238 zero_sockaddr(&ss);
239
240 debuglocal(1,"Connecting to \\\\%s:*********@%s:%s\\%s. Master %s:%d\n", srv->username, workgroup, server, share, srv->master, srv->ifmastergroup);
241
242 if (!*server) {
243 struct sockaddr_storage sip;
244
245 if (*workgroup)
246 {
247 if (!find_master_ip(workgroup, &sip)) {
248 return 1;
249 }
250 fstrcpy(group, inet_ntoa(sip.sin_addr));
251 server_n = group;
252 } else
253 if (*srv->master)
254 {
255 if (srv->ifmastergroup)
256 {
257 if (!find_master_ip(srv->master, &sip)) {
258 return 11;
259 }
260 strncpy(srv->master, inet_ntoa(sip.sin_addr), sizeof(srv->master) - 1);
261 srv->ifmastergroup = 0;
262 }
263 server_n = srv->master;
264 } else
265 {
266 return 10;
267 }
268 }
269
270 make_nmb_name(&calling, global_myname(), 0x0);
271// make_nmb_name(&calling, "WORK", 0x0); // this machine name
272 make_nmb_name(&called , server_n, 0x20);
273
274 again:
275 zero_sockaddr(&ss);
276
277 /* have to open a new connection */
278 if (!(c=cli_initialise()))
279 {
280 return 2;
281 }
282
283 cli_set_timeout(c, 10000); /* 10 seconds. */
284
285 if (pRes->krb5support == 1)
286 {
287 debuglocal(1,"Kerberos support enabled\n");
288 c->use_kerberos = True;
289 }
290
291 if (!NT_STATUS_IS_OK(cli_connect(c, server_n, &ss)))
292 {
293 return 3;
294 }
295
296 if (!cli_session_request(c, &calling, &called)) {
297 cli_shutdown(c);
298 if (strcmp(called.name, "*SMBSERVER")) {
299 make_nmb_name(&called , "*SMBSERVER", 0x20);
300 goto again;
301 }
302 return 4;
303 }
304
305 debuglocal(4," session request ok\n");
306
307 if (!NT_STATUS_IS_OK(cli_negprot(c))) {
308 cli_shutdown(c);
309 return 5;
310 }
311
312 debuglocal(4," session setuping for <%s>/<********> in <%s> %08x %08x %08x\n", srv->username, workgroup, c->protocol, c->sec_mode, c->capabilities);
313
314 if (!NT_STATUS_IS_OK(cli_session_setup(c, srv->username,
315 srv->password, strlen(srv->password),
316 srv->password, strlen(srv->password),
317 workgroup))) {
318 debuglocal(4,"%s/******** login failed\n", srv->username);
319 loginerror = 1; // save the login error
320
321 /* try an anonymous login if it failed */
322 if (!NT_STATUS_IS_OK(cli_session_setup(c, "", "", 0,"", 0, workgroup))) {
323 debuglocal(4,"Anonymous login failed\n");
324 cli_shutdown(c);
325 return 6;
326 }
327 debuglocal(4,"Anonymous login successful\n");
328 status = cli_init_creds(c, "", lp_workgroup(), "");
329 } else {
330 status = cli_init_creds(c, srv->username, workgroup, srv->password);
331 }
332
333 if (!NT_STATUS_IS_OK(status)) {
334 debuglocal(4,"cli_init_creds() failed\n");
335 cli_shutdown(c);
336 // if loginerror is != 0 means normal login failed, but anonymous login worked
337 if (loginerror !=0)
338 return 6;
339 else
340 return 7;
341 }
342
343 debuglocal(4," session setup ok. Sending tconx <%s> <********>\n", share);
344
345 // YD ticket:58 we need to check resource type to avoid connecting to printers.
346 // dev type is set to IPC for IPC$, A: for everything else (printers use LPT1:)
347 if (!strcmp( share, "IPC$"))
348 dev_type = "IPC";
349 else
350 dev_type = "A:";
351
352 if (!NT_STATUS_IS_OK(cli_tcon_andx(c, share, dev_type,
353 srv->password, strlen(srv->password)+1))) {
354 cli_shutdown(c);
355 // if loginerror is != 0 means normal login failed, but anonymous login worked
356 if (loginerror !=0)
357 return 6;
358 else
359 return 7;
360 }
361
362 debuglocal(4," tconx ok. cli caps %08x\n", c->capabilities);
363
364 // save cli_state pointer
365 *cli = c;
366
367 return 0;
368}
369
370/*****************************************************
371close a connection to a server
372*******************************************************/
373void _System smbwrp_disconnect( Resource* pRes, cli_state * cli)
374{
375 if (pRes && cli)
376 {
377 // this call will free all buffers, close handles and free cli mem
378 cli_shutdown( cli);
379 }
380}
381
382
383
384/*****************************************************
385a wrapper for open()
386*******************************************************/
387int _System smbwrp_open(cli_state * cli, smbwrp_file * file)
388{
389 uint16_t fd = 0;
390
391 if (!cli || !file || !*file->fname)
392 {
393 return maperror(EINVAL);
394 }
395 if (file->denymode < DENY_ALL || file->denymode > DENY_NONE)
396 {
397 file->denymode = DENY_NONE;
398 }
399
400 debuglocal(4,"cli_open(%s) attr %08x mode %02x denymode %02x\n", file->fname, file->openattr, file->openmode, file->denymode);
401 if (!NT_STATUS_IS_OK(cli_open(cli, file->fname, file->openmode, file->denymode, &fd)))
402 {
403 return os2cli_errno(cli);
404 }
405 file->fd = fd;
406 file->updatetime = 0;
407 file->offset = 0;
408 return 0;
409}
410
411/*****************************************************
412a wrapper for read()
413*******************************************************/
414int _System smbwrp_read(cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result)
415{
416 int ret;
417
418 if (!cli || !file || !buf || !result)
419 {
420 return maperror(EINVAL);
421 }
422
423 *result = 0;
424 ret = cli_read(cli, file->fd, buf, file->offset, count);
425 if (ret == -1)
426 {
427 return os2cli_errno(cli);
428 }
429
430 file->offset += ret;
431 *result = ret;
432 return 0;
433}
434
435
436
437/*****************************************************
438a wrapper for write()
439*******************************************************/
440int _System smbwrp_write(cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result)
441{
442 NTSTATUS status;
443 size_t ret;
444
445 if (!cli || !file || !buf || !result)
446 {
447 return maperror(EINVAL);
448 }
449
450 *result = 0;
451//debuglocal(1,("Write %x %d %lld %d", cli, file->fd, file->offset, count));
452 status = cli_writeall(cli, file->fd, 0, buf, file->offset, count, &ret);
453 if (!NT_STATUS_IS_OK(status)) {
454 return os2cli_errno(cli);
455 }
456
457 file->updatetime = 1;
458 file->offset += ret;
459 *result = ret;
460 return 0;
461}
462
463/*****************************************************
464a wrapper for close()
465*******************************************************/
466int _System smbwrp_close(cli_state * cli, smbwrp_file * file)
467{
468 int rc = 0;
469 if (!cli || !file)
470 {
471 return maperror(EINVAL);
472 }
473
474 debuglocal(4,"smpwrp_close updatetime: %d\n", file->updatetime);
475
476 if (file->updatetime == 1)
477 {
478 file->mtime = time(NULL);
479 debuglocal(4,"cli_close new mtime %lu\n", file->mtime);
480 }
481
482 if (!NT_STATUS_IS_OK(cli_close(cli, file->fd)))
483 {
484 rc = os2cli_errno(cli);
485 }
486
487 if (!rc && (file->openattr || file->mtime || file->ctime))
488 {
489 debuglocal(4,"Set pathinfo on close %s %08x %d %d\n", file->fname, file->openattr, file->mtime, file->ctime);
490 if (!NT_STATUS_IS_OK(cli_setpathinfo_basic(cli, file->fname, file->ctime, 0, file->mtime, 0, file->openattr)))
491 {
492 debuglocal(4,"Set pathinfo on close failed %d\n", os2cli_errno(cli));
493 //rc = os2cli_errno(cli);
494 }
495 }
496
497 file->openattr = 0;
498 file->mtime = 0;
499 file->ctime = 0;
500 file->updatetime = 0;
501 file->fd = -1;
502 file->offset = 0;
503 *file->fname = 0;
504 return rc;
505}
506
507int _System smbwrp_setfilesize(cli_state * cli, smbwrp_file * file, long long newsize)
508{
509 int rc = 0;
510 if (!cli || !file)
511 {
512 return maperror(EINVAL);
513 }
514
515 debuglocal(4,"cli_setnewfilesize(%s) %lld\n", file->fname, newsize);
516 if (!NT_STATUS_IS_OK(cli_ftruncate(cli, file->fd, newsize)))
517 {
518 if (newsize)
519 {
520 rc = os2cli_errno(cli);
521 }
522
523 if (!NT_STATUS_IS_OK(cli_close(cli, file->fd)))
524 {
525 return os2cli_errno(cli);
526 }
527 uint16_t fd = 0;
528 file->fd = -1;
529 file->offset = 0;
530 file->openmode &= ~(O_CREAT | O_EXCL);
531 file->openmode |= O_TRUNC;
532 debuglocal(4,"cli_setnewfileszie : cli_open(%s) attr %08x mode %02x denymode %02x\n", file->fname, file->openattr, file->openmode, file->denymode);
533 if (!NT_STATUS_IS_OK(cli_open(cli, file->fname, file->openmode, file->denymode, &fd)))
534 {
535 return os2cli_errno(cli);
536 }
537 file->fd = fd;
538 }
539 return 0;
540}
541
542/*****************************************************
543a wrapper for rename()
544*******************************************************/
545int _System smbwrp_rename(cli_state * cli, char *oldname, char *newname)
546{
547 if (!cli || !oldname || !newname)
548 {
549 return maperror(EINVAL);
550 }
551
552 debuglocal(1,"Rename <%s> -> <%s>\n", oldname, newname);
553 if (!NT_STATUS_IS_OK(cli_rename(cli, oldname, newname)))
554 {
555 return os2cli_errno(cli);
556 }
557 return 0;
558}
559
560
561/*****************************************************
562a wrapper for chmod()
563*******************************************************/
564int _System smbwrp_setattr(cli_state * cli, smbwrp_fileinfo *finfo)
565{
566 if (!cli || !finfo || !*finfo->fname)
567 {
568 return maperror(EINVAL);
569 }
570
571debuglocal(4,"Setting on <%s> attr %04x, time %lu (timezone /%lu\n", finfo->fname, finfo->attr, finfo->mtime, finfo->mtime + get_time_zone(finfo->mtime));
572 // we already have gmt time, so no need to add timezone
573 // if (!cli_setatr(cli, finfo->fname, finfo->attr, finfo->mtime + (finfo->mtime == 0 ? 0 : get_time_zone(finfo->mtime)))
574 if (!NT_STATUS_IS_OK(cli_setatr(cli, finfo->fname, finfo->attr, finfo->mtime))
575 && !NT_STATUS_IS_OK(cli_setatr(cli, finfo->fname, finfo->attr, 0)))
576 {
577 return os2cli_errno(cli);
578 }
579 return 0;
580}
581
582/*****************************************************
583a wrapper for unlink()
584*******************************************************/
585int _System smbwrp_unlink(cli_state * cli, const char *fname)
586{
587 if (!cli || !fname)
588 {
589 return maperror(EINVAL);
590 }
591#if 0
592 if (strncmp(cli->dev, "LPT", 3) == 0)
593 {
594 int job = smbw_stat_printjob(cli, fname, NULL, NULL);
595 if (job == -1)
596 {
597 goto failed;
598 }
599 if (cli_printjob_del(cli, job) != 0)
600 {
601 goto failed;
602 }
603 } else
604#endif
605 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN)))
606 {
607 return os2cli_errno(cli);
608 }
609 return 0;
610}
611
612/*****************************************************
613a wrapper for lseek()
614*******************************************************/
615int _System smbwrp_lseek(cli_state * cli, smbwrp_file * file, int whence, long long offset)
616{
617 off_t size;
618 if (!cli || !file)
619 {
620 return maperror(EINVAL);
621 }
622
623 debuglocal(4,"lseek %d %lld %lld\n", whence, offset, file->offset);
624
625 switch (whence) {
626 case SEEK_SET:
627 if (offset < 0)
628 {
629 return maperror(EINVAL);
630 }
631 file->offset = offset;
632 break;
633 case SEEK_CUR:
634 file->offset += offset;
635 break;
636 case SEEK_END:
637 if (offset > 0)
638 {
639 return maperror(EINVAL);
640 }
641 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(cli, file->fd,
642 NULL, &size, NULL, NULL, NULL,
643 NULL, NULL)) &&
644 !NT_STATUS_IS_OK(cli_getattrE(cli, file->fd,
645 NULL, &size, NULL, NULL, NULL)))
646 {
647 return os2cli_errno(cli);
648 }
649 file->offset = size + offset;
650 break;
651 default: return maperror(EINVAL);
652 }
653
654 return 0;
655}
656
657/*****************************************************
658try to do a QPATHINFO and if that fails then do a getatr
659this is needed because win95 sometimes refuses the qpathinfo
660*******************************************************/
661int _System smbwrp_getattr(smbwrp_server *srv, cli_state * cli, smbwrp_fileinfo *finfo)
662{
663 SMB_INO_T ino = 0;
664 struct timespec ctime;
665 struct timespec mtime;
666 struct timespec atime;
667
668 if (!cli || !finfo || !*finfo->fname)
669 {
670 return maperror(EINVAL);
671 }
672 debuglocal(4,"getattr %d %d <%s>\n", cli->capabilities & CAP_NOPATHINFO2, cli->capabilities & CAP_NT_SMBS, finfo->fname);
673 if (!(cli->capabilities & CAP_NOPATHINFO2) &&
674 cli_qpathinfo2(cli, finfo->fname, &ctime, &atime, &mtime, NULL,
675 (off_t *)&finfo->size, (unsigned short *)&finfo->attr, &ino))
676 {
677 finfo->attr &= 0x7F;
678 finfo->ctime = convert_timespec_to_time_t(ctime);
679 finfo->atime = convert_timespec_to_time_t(atime);
680 finfo->mtime = convert_timespec_to_time_t(mtime);
681 return 0;
682 }
683
684 if (cli->fd == -1)
685 {
686 /* fd == -1 means the connection is broken */
687 return maperror(ENOTCONN);
688 }
689
690 /* If the path is not on a share (it is a workgroup or a server),
691 * then cli_qpathinfo2 obviously fails. Return some fake information
692 * about the directory.
693 */
694 if ( *srv->server_name == 0
695 || (strcmp(cli->dev,"IPC") == 0)
696 || *srv->share_name == 0
697 || (stricmp(srv->share_name,"IPC$") == 0)
698 || (strncmp(cli->dev,"LPT",3) == 0)
699 )
700 {
701 debuglocal(4,"getattr not a share.\n");
702 *(time_t *)&finfo->ctime = time (NULL);
703 *(time_t *)&finfo->atime = time (NULL);
704 *(time_t *)&finfo->mtime = time (NULL);
705 finfo->size = 0;
706 finfo->easize = 0;
707 finfo->attr = aDIR;
708 return 0;
709 }
710
711 /* if this is NT then don't bother with the getatr */
712 if (cli->capabilities & CAP_NT_SMBS && !(cli->capabilities & CAP_NOPATHINFO2))
713 {
714 int ret = cli_errno(cli);
715 // cli_qpathinfo* reports EINVAL when path of given file not exists
716 // thus there is no real situation when EINVAL should be returned to
717 // client at this point, we just replace it to ENOTDIR
718 if (ret == EINVAL)
719 {
720 ret = ENOTDIR;
721 }
722 return maperror(ret);
723 }
724
725 if (NT_STATUS_IS_OK(cli_getatr(cli, finfo->fname, (unsigned short *)&finfo->attr, &finfo->size, (time_t *)&finfo->mtime)))
726 {
727//debuglocal(2,("gotattr1 %08x <%s>\n", finfo->attr, finfo->fname));
728 finfo->mtime -= get_time_zone(finfo->mtime);
729 finfo->atime = finfo->atime; //was mtime
730 finfo->ctime = finfo->ctime; //was mtime
731 cli->capabilities &= CAP_NOPATHINFO2;
732 return 0;
733 }
734 return os2cli_errno(cli);
735}
736
737/*****************************************************
738try to do a QPATHINFO and if that fails then do a getatr
739this is needed because win95 sometimes refuses the qpathinfo
740*******************************************************/
741int _System smbwrp_fgetattr(cli_state * cli, smbwrp_file *file, smbwrp_fileinfo *finfo)
742{
743 struct timespec ctime;
744 struct timespec mtime;
745 struct timespec atime;
746 SMB_INO_T ino = 0;
747
748 if (!cli || !file || !finfo)
749 {
750 return maperror(EINVAL);
751 }
752
753 strncpy(finfo->fname, file->fname, sizeof(finfo->fname) - 1);
754 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(cli, file->fd,
755 (unsigned short *)&finfo->attr, (off_t *)&finfo->size, &ctime, &atime, &mtime, NULL,
756 &ino)))
757 {
758 if (!NT_STATUS_IS_OK(cli_getattrE(cli, file->fd,
759 (unsigned short *)&finfo->attr, (&finfo->size), (time_t *)&finfo->ctime, (time_t *)&finfo->atime, (time_t *)&finfo->mtime)))
760 {
761 return os2cli_errno(cli);
762 }
763 else
764 {
765 finfo->ctime -= get_time_zone(finfo->ctime);
766 finfo->atime -= get_time_zone(finfo->atime);
767 finfo->mtime -= get_time_zone(finfo->mtime);
768 }
769 }
770 else
771 {
772 finfo->ctime = convert_timespec_to_time_t(ctime);
773 finfo->atime = convert_timespec_to_time_t(atime);
774 finfo->mtime = convert_timespec_to_time_t(mtime);
775 }
776
777 return 0;
778}
779
780// =============================DIRECTORY ROUTINES============================
781
782/*****************************************************
783add a entry to a directory listing
784*******************************************************/
785static void smbwrp_dir_add(const char* mnt, smbwrp_fileinfo *finfo, const char *mask, void *state)
786{
787 if (state && finfo)
788 {
789 filelist_state * st = (filelist_state *)state;
790 char fullname[ _MAX_PATH] = {0};
791 debuglocal(8,"adding <%s> %d %d %d\n", finfo->fname, sizeof(st->finfo), st->datalen, sizeof(st->finfo.fname));
792 memcpy(&st->finfo, finfo, sizeof(st->finfo));
793 strncpy(fullname, st->dir, strlen(st->dir));
794 strncat(fullname, finfo->fname, sizeof(fullname) - strlen(fullname) -1);
795 strncpy(st->finfo.fname, fullname, sizeof(st->finfo.fname));
796 getfindinfoL( st->pConn, st->plist, &st->finfo, st->ulAttribute, st->dir_mask);
797 }
798}
799
800static void smbwrp_special_add(const char * name, void * state)
801{
802 smbwrp_fileinfo finfo = {0};
803
804 if (!name)
805 {
806 return;
807 }
808
809 ZERO_STRUCT(finfo);
810
811 strncpy(finfo.fname, name, sizeof(finfo.fname) - 1);
812 finfo.attr = aRONLY | aDIR;
813
814 smbwrp_dir_add("", &finfo, NULL, state);
815}
816
817static void smbwrp_printjob_add(struct print_job_info *job, void * state)
818{
819 smbwrp_fileinfo finfo = {0};
820
821 ZERO_STRUCT(finfo);
822
823//printf("Printjob <%s>\n", job->name);
824
825 strncpy(finfo.fname, job->name, sizeof(finfo.fname) - 1);
826 finfo.mtime = job->t - get_time_zone(job->t);
827 finfo.atime = finfo.atime; //was mtime
828 finfo.ctime = finfo.ctime; //was mtime
829 finfo.attr = aRONLY;
830 finfo.size = job->size;
831
832 smbwrp_dir_add("", &finfo, NULL, state);
833}
834
835static void smbwrp_share_add(const char *share, uint32 type,
836 const char *comment, void *state)
837{
838 smbwrp_fileinfo finfo = {0};
839
840 // strip administrative names and printers from list
841 if (type == STYPE_PRINTQ || strcmp(share,"IPC$") == 0) return;
842
843 ZERO_STRUCT(finfo);
844
845 strncpy(finfo.fname, share, sizeof(finfo.fname) - 1);
846 finfo.attr = aRONLY | aDIR;
847
848 smbwrp_dir_add("", &finfo, NULL, state);
849}
850
851/****************************************************************************
852 Do a directory listing, calling fn on each file found.
853 Modified from cli_list
854****************************************************************************/
855static int list_files(struct cli_state *cli, const char *mask, uint16 attribute,
856 void (*fn)(const char *, smbwrp_fileinfo *, const char *,
857 void *), void *state)
858{
859 TALLOC_CTX *frame = talloc_stackframe();
860 struct event_context *ev;
861 struct tevent_req *req;
862 NTSTATUS status = NT_STATUS_NO_MEMORY;
863 struct file_info *finfo;
864 size_t i, num_finfo;
865 uint16_t info_level;
866 void *dircachectx = NULL;
867 smbwrp_fileinfo wrpfinfo;
868
869 /* Try to get the listing from cache. */
870 if (dircache_list_files(fn, state, &num_finfo))
871 {
872 /* Got from cache. */
873 return(num_finfo);
874 }
875
876 if (cli_has_async_calls(cli)) {
877 /*
878 * Can't use sync call while an async call is in flight
879 */
880 status = NT_STATUS_INVALID_PARAMETER;
881 goto fail;
882 }
883 ev = event_context_init(frame);
884 if (ev == NULL) {
885 goto fail;
886 }
887
888 info_level = (cli->capabilities & CAP_NT_SMBS)
889 ? SMB_FIND_FILE_BOTH_DIRECTORY_INFO : SMB_FIND_EA_SIZE;
890
891 debuglocal(4,"list_files level %d. mask <%s>\n", info_level, mask);
892
893 req = cli_list_send(frame, ev, cli, mask, attribute, info_level);
894 if (req == NULL) {
895 goto fail;
896 }
897 if (!tevent_req_poll(req, ev)) {
898 status = map_nt_error_from_unix(errno);
899 goto fail;
900 }
901
902 status = cli_list_recv(req, frame, &finfo, &num_finfo);
903 if (!NT_STATUS_IS_OK(status)) {
904 goto fail;
905 }
906
907 dircachectx = dircache_write_begin(state, num_finfo);
908
909 debuglocal(4,"list_files: got %d files\n", num_finfo);
910
911
912 for (i=0; i<num_finfo; i++) {
913 //as samba and this client have different finfo, we need to convert
914 memset(&wrpfinfo, 0, sizeof(wrpfinfo));
915 wrpfinfo.size = finfo[i].size;
916 wrpfinfo.attr = finfo[i].mode;
917 wrpfinfo.ctime = convert_timespec_to_time_t(finfo[i].ctime_ts);
918 wrpfinfo.mtime = convert_timespec_to_time_t(finfo[i].mtime_ts);
919 wrpfinfo.atime = convert_timespec_to_time_t(finfo[i].atime_ts);
920 wrpfinfo.easize = finfo[i].easize;
921 strncpy(wrpfinfo.fname, finfo[i].name, sizeof(wrpfinfo.fname) -1);
922
923 fn(cli->dfs_mountpoint, &wrpfinfo, mask, state);
924 // Also add the entry to the cache.
925 dircache_write_entry(dircachectx, &wrpfinfo);
926 }
927
928 dircache_write_end(dircachectx);
929 fail:
930 TALLOC_FREE(frame);
931 return num_finfo;
932}
933
934/*****************************************************
935open a directory on the server
936*******************************************************/
937int _System smbwrp_filelist(smbwrp_server *srv, cli_state * cli, filelist_state * state)
938{
939 if (!srv || !cli || !state || !*state->mask)
940 {
941 return maperror(EINVAL);
942 }
943 debuglocal(1,"Filelist <%s> on master <%s> wgrp <%s> server <%s> share <%s> clidev <%s>\n", state->mask, srv->master, srv->workgroup, srv->server_name, srv->share_name, cli->dev);
944 if (*srv->workgroup == 0 && *srv->server_name == 0)
945 {
946 smbwrp_special_add(".", state);
947 smbwrp_special_add("..", state);
948 cli_NetServerEnum(cli, srv->master, SV_TYPE_DOMAIN_ENUM,
949 smbwrp_share_add, state);
950 } else
951 if (*srv->server_name == 0)
952 {
953 smbwrp_special_add(".", state);
954 smbwrp_special_add("..", state);
955
956 cli_NetServerEnum(cli, srv->workgroup, SV_TYPE_ALL,
957 smbwrp_share_add, state);
958 } else
959 if ((strcmp(cli->dev,"IPC") == 0) || *srv->share_name == 0 || (stricmp(srv->share_name,"IPC$") == 0))
960 {
961 smbwrp_special_add(".", state);
962 smbwrp_special_add("..", state);
963
964 if (net_share_enum_rpc(cli, smbwrp_share_add, state) < 0 &&
965 cli_RNetShareEnum(cli,smbwrp_share_add, state) < 0)
966 {
967 return os2cli_errno(cli);
968 }
969 } else
970 if (strncmp(cli->dev,"LPT",3) == 0)
971 {
972 smbwrp_special_add(".", state);
973 smbwrp_special_add("..", state);
974 if (cli_print_queue_state(cli, smbwrp_printjob_add, state) < 0)
975 {
976 return os2cli_errno(cli);
977 }
978 }
979 else
980 {
981 if (list_files(cli, state->mask, aHIDDEN|aSYSTEM|aDIR,
982 smbwrp_dir_add, state) < 0)
983 {
984 return os2cli_errno(cli);
985 }
986 }
987
988 return 0;
989}
990
991/*****************************************************
992a wrapper for chdir()
993*******************************************************/
994int _System smbwrp_chdir(smbwrp_server *srv, cli_state * cli, char *fname)
995{
996 unsigned short mode = aDIR;
997 smbwrp_fileinfo finfo = {0};
998 if (!cli || !fname)
999 {
1000 return maperror(EINVAL);
1001 }
1002
1003 strncpy(finfo.fname, fname, sizeof(finfo.fname) - 1);
1004 if (smbwrp_getattr(srv, cli, &finfo))
1005 {
1006 return os2cli_errno(cli);
1007 }
1008
1009 if (!(finfo.attr & aDIR)) {
1010 return maperror(ENOTDIR);
1011 }
1012
1013 return 0;
1014}
1015
1016
1017/*****************************************************
1018a wrapper for mkdir()
1019*******************************************************/
1020int _System smbwrp_mkdir(cli_state * cli, char *fname)
1021{
1022 if (!cli || !fname)
1023 {
1024 return maperror(EINVAL);
1025 }
1026
1027 if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname)))
1028 {
1029 return os2cli_errno(cli);
1030 }
1031 return 0;
1032}
1033
1034/*****************************************************
1035a wrapper for rmdir()
1036*******************************************************/
1037int _System smbwrp_rmdir(cli_state * cli, char *fname)
1038{
1039 if (!cli || !fname)
1040 {
1041 return maperror(EINVAL);
1042 }
1043
1044 if (!NT_STATUS_IS_OK(cli_rmdir(cli, fname)))
1045 {
1046 return os2cli_errno(cli);
1047 }
1048 return 0;
1049}
1050
1051/*****************************************************
1052set EA for a path
1053*******************************************************/
1054int _System smbwrp_setea(cli_state * cli, char *fname, char * name, unsigned char * value, int size)
1055{
1056 if (!cli || !fname || !name)
1057 {
1058 return maperror(EINVAL);
1059 }
1060 if (!cli_set_ea_path(cli, fname, name, value, size))
1061 {
1062 return os2cli_errno(cli);
1063 }
1064 return 0;
1065}
1066
1067/*****************************************************
1068set EA for a file
1069*******************************************************/
1070int _System smbwrp_fsetea(cli_state * cli, smbwrp_file *file, char * name, unsigned char * value, int size)
1071{
1072 if (!cli || !file || !name)
1073 {
1074 return maperror(EINVAL);
1075 }
1076 if (!cli_set_ea_fnum(cli, file->fd, name, value, size))
1077 {
1078 return os2cli_errno(cli);
1079 }
1080 return 0;
1081}
1082
1083
1084#pragma pack(1)
1085typedef struct _FEA /* fea */
1086{
1087 unsigned char fEA; /* flags */
1088 unsigned char cbName; /* name length not including NULL */
1089 unsigned short cbValue; /* value length */
1090} FEA;
1091
1092typedef struct _FEALIST /* feal */
1093{
1094 unsigned long cbList; /* total bytes of structure including full list */
1095 FEA list[1]; /* variable length FEA structures */
1096} FEALIST;
1097#pragma pack()
1098
1099static int unilistea(cli_state * cli, char *fname, void * buffer, unsigned long size)
1100{
1101 int fnum, i;
1102 int gotsize = sizeof(unsigned long);
1103 size_t num_eas;
1104 struct ea_struct *ea_list = NULL;
1105 TALLOC_CTX *mem_ctx;
1106 FEA * p;
1107 FEALIST * pfealist;
1108 char * q;
1109
1110 mem_ctx = talloc_init("%d: ealist", _gettid());
1111 pfealist = (FEALIST *)buffer;
1112 pfealist->cbList = 0;
1113
1114 if (!cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list))
1115 {
1116 debuglocal(4,"ea_get_file list failed - %s\n", cli_errstr(cli));
1117 talloc_destroy(mem_ctx);
1118 return os2cli_errno(cli);
1119 }
1120
1121 debuglocal(4,"num_eas = %d\n", num_eas);
1122
1123 // we will count that os/2 max EA size for file is 64kb
1124 p = pfealist->list;
1125 for (i = 0; i < num_eas; i++)
1126 {
1127 int namelen = strlen(ea_list[i].name);
1128 debuglocal(4, "%d Got EA <%s> with namelen %d, size %d. Gross %d. Buf %d\n", i, ea_list[i].name, namelen, ea_list[i].value.length, gotsize, size);
1129 if (namelen > 0xFF || ea_list[i].value.length > 0xFFFF)
1130 {
1131 debuglocal(4, "Skip EA <%s> with namelen %d, size %d\n", ea_list[i].name, namelen, ea_list[i].value.length);
1132 continue;
1133 }
1134 gotsize += sizeof(FEA) + namelen + ea_list[i].value.length + 1;
1135 if (size >= gotsize)
1136 {
1137 p->fEA = 0;
1138 p->cbName = namelen;
1139 p->cbValue = ea_list[i].value.length;
1140 q = (char *)(p + 1);
1141 strncpy(q, ea_list[i].name, namelen + 1);
1142 q += namelen + 1;
1143 memcpy(q, ea_list[i].value.data, ea_list[i].value.length);
1144 p = (FEA *)(q + ea_list[i].value.length);
1145 }
1146 }
1147 pfealist->cbList = gotsize;
1148 debuglocal(4,"ret size = %d\n", gotsize);
1149
1150 talloc_destroy(mem_ctx);
1151 return 0;
1152}
1153
1154/*****************************************************
1155lists EA of a path
1156*******************************************************/
1157int _System smbwrp_listea(cli_state * cli, char *fname, void * buffer, unsigned long size)
1158{
1159 if (!cli || !fname || !buffer)
1160 {
1161 return maperror(EINVAL);
1162 }
1163
1164 debuglocal(4,"EALIst for <%s>\n", fname);
1165 return unilistea(cli, fname, buffer, size);
1166}
1167
1168/*****************************************************
1169lists EA of a file
1170*******************************************************/
1171int _System smbwrp_flistea(cli_state * cli, smbwrp_file *file, void * buffer, unsigned long size)
1172{
1173 if (!cli || !file || !buffer)
1174 {
1175 return maperror(EINVAL);
1176 }
1177
1178 debuglocal(4,"FEALIst for <%s>\n", file->fname);
1179 return unilistea(cli, file->fname, buffer, size);
1180}
1181
1182/****************************************************************************
1183Check the space on a device.
1184****************************************************************************/
1185int _System smbwrp_dskattr(cli_state * cli, FSALLOCATE *pfsa)
1186{
1187 int total, bsize, avail;
1188
1189 if (!cli || !pfsa)
1190 {
1191 return maperror(EINVAL);
1192 }
1193
1194 if (!NT_STATUS_IS_OK(cli_dskattr(cli, &bsize, &total, &avail)))
1195 {
1196 debuglocal(4,"Error in dskattr: %s\n",cli_errstr(cli));
1197 return os2cli_errno(cli);
1198 }
1199
1200 debuglocal(4,"\n\t\t%d blocks of size %d. %d blocks available\n",
1201 total, bsize, avail);
1202
1203 // YD currently Samba return it in MB!
1204 pfsa->cSectorUnit = 1;
1205 if (bsize >= 65536)
1206 {
1207 pfsa->cUnit = total*1024;
1208 pfsa->cUnitAvail = avail*1024;
1209 pfsa->cbSector = bsize/1024;
1210 }
1211 else
1212 {
1213 pfsa->cUnit = total;
1214 pfsa->cUnitAvail = avail;
1215 pfsa->cbSector = bsize;
1216 }
1217
1218 return 0;
1219}
1220
Note: See TracBrowser for help on using the repository browser.