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

Last change on this file since 958 was 957, checked in by Silvan Scherrer, 9 years ago

samba client: adjust copyright

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