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

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

samba client: add the possibility to have logfiles per share

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