source: branches/client-3.0/src/smbwrp.c@ 929

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

make it build using Samba 4.4.x

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