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

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

Introduce smb_echo, used to confirm server connection is still active before trying a connection

  • Property svn:eol-style set to native
File size: 31.4 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 if (!pRes->krb5support)
248 debuglocal(1,"Connecting to \\\\%s:*********@%s:%s\\%s. Master %s:%d\n", srv->username, workgroup, server, share, srv->master, srv->ifmastergroup);
249 else
250 debuglocal(1,"Connecting to \\\\%s:%s\\%s using kerberos authentication. Master %s:%d\n", workgroup, server, share, srv->master, srv->ifmastergroup);
251
252 if (pRes->krb5support) {
253 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
254 }
255
256 if (pRes->ntlmv1support) {
257 lp_set_cmdline("client ntlmv2 auth","no");
258 }
259
260 status = cli_connect_nb(
261 server, NULL, port, name_type, NULL,
262 SMB_SIGNING_DEFAULT, flags, &c);
263
264 if (!NT_STATUS_IS_OK(status)) {
265 debuglocal(4,"Connection to %s failed (Error %s)\n",
266 server,
267 nt_errstr(status));
268 return 3;
269 }
270
271 if (max_protocol == PROTOCOL_DEFAULT) {
272 max_protocol = PROTOCOL_LATEST;
273 }
274 DEBUG(4,(" session request ok, c->timeout = %d\n",c->timeout));
275
276 status = smbXcli_negprot(c->conn, c->timeout,
277 lp_client_min_protocol(),
278 max_protocol);
279
280 if (!NT_STATUS_IS_OK(status)) {
281 debuglocal(4,"protocol negotiation failed: %s\n",
282 nt_errstr(status));
283 cli_shutdown(c);
284 return status;
285 }
286
287 if (!NT_STATUS_IS_OK(cli_session_setup(c, srv->username,
288 srv->password, strlen(srv->password),
289 srv->password, strlen(srv->password),
290 workgroup))) {
291 debuglocal(4,"%s/******** login failed\n", srv->username);
292 loginerror = 1; // save the login error
293
294 /* try an anonymous login if it failed */
295
296 /* If a password was not supplied then
297 * try again with a null username. */
298 if (srv->password[0] || !srv->username[0] ||
299 pRes->krb5support ||
300 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
301 "", 0,
302 "", 0,
303 workgroup))) {
304 debuglocal(1,"session setup failed: %s\n",
305 nt_errstr(status));
306
307 if ((NT_STATUS_EQUAL(status,
308 NT_STATUS_MORE_PROCESSING_REQUIRED)) ||
309 NT_STATUS_EQUAL(status,
310 NT_STATUS_INTERNAL_ERROR)){
311 debuglocal(4,"did you forget to run kinit?\n");
312 } else
313 debuglocal(4,"Anonymous login failed\n");
314
315 cli_shutdown(c);
316 return 6;
317 }
318 debuglocal(4,"Anonymous login successful\n");
319 }
320
321 if (!NT_STATUS_IS_OK(status)) {
322 debuglocal(4,"cli_init_creds() failed\n");
323 cli_shutdown(c);
324 // if loginerror is != 0 means normal login failed, but anonymous login worked
325 if (loginerror !=0)
326 return 6;
327 else
328 return 7;
329 }
330
331 debuglocal(4," session setup ok. Sending tconx <%s> <********>\n", share);
332
333 // YD ticket:58 we need to check resource type to avoid connecting to printers.
334 // dev type is set to IPC for IPC$, A: for everything else (printers use LPT1:)
335 if (!strcmp( share, "IPC$"))
336 dev_type = "IPC";
337 else
338 dev_type = "A:";
339
340 if (!NT_STATUS_IS_OK(cli_tcon_andx(c, share, dev_type,
341 srv->password, strlen(srv->password)+1))) {
342 cli_shutdown(c);
343 // if loginerror is != 0 means normal login failed, but anonymous login worked
344 if (loginerror !=0)
345 return 6;
346 else
347 return 7;
348 }
349
350 debuglocal(4," tconx ok.\n");
351
352 // save cli_state pointer
353 *cli = c;
354
355 return 0;
356}
357
358/*****************************************************
359close a connection to a server
360*******************************************************/
361void _System smbwrp_disconnect( Resource* pRes, cli_state * cli)
362{
363 if (pRes && cli)
364 {
365 // this call will free all buffers, close handles and free cli mem
366 cli_shutdown( cli);
367 }
368}
369
370
371
372/*****************************************************
373a wrapper for open()
374*******************************************************/
375int _System smbwrp_open(cli_state * cli, smbwrp_file * file)
376{
377 uint16_t fd = 0;
378
379 if (!cli || !file || !*file->fname)
380 {
381 return maperror(EINVAL);
382 }
383 if (file->denymode < DENY_ALL || file->denymode > DENY_NONE)
384 {
385 file->denymode = DENY_NONE;
386 }
387
388 debuglocal(4,"cli_open(%s) attr %08x mode %02x denymode %02x\n", file->fname, file->openattr, file->openmode, file->denymode);
389 if (!NT_STATUS_IS_OK(cli_open(cli, file->fname, file->openmode, file->denymode, &fd)))
390 {
391 return os2cli_errno(cli);
392 }
393 file->fd = fd;
394 file->updatetime = 0;
395 file->offset = 0;
396 return 0;
397}
398
399/*****************************************************
400a wrapper for read()
401*******************************************************/
402int _System smbwrp_read(cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result)
403{
404 int ret;
405
406 if (!cli || !file || !buf || !result)
407 {
408 return maperror(EINVAL);
409 }
410 size_t nread;
411 *result = 0;
412 ret = cli_read(cli, file->fd, buf, file->offset, count, &nread);
413 if (ret == -1)
414 {
415 debuglocal(4," smbwrp_read - cli_read ret = %d\n",ret);
416 return os2cli_errno(cli);
417 }
418
419 file->offset += nread;
420 *result = nread;
421 debuglocal(4," smbwrp_read successful, nread = %d, ret = %d\n",nread,ret);
422 return 0;
423}
424
425
426
427/*****************************************************
428a wrapper for write()
429*******************************************************/
430int _System smbwrp_write(cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result)
431{
432 NTSTATUS status;
433 size_t ret;
434
435 if (!cli || !file || !buf || !result)
436 {
437 return maperror(EINVAL);
438 }
439
440 *result = 0;
441//debuglocal(1,("Write %x %d %lld %d", cli, file->fd, file->offset, count));
442 status = cli_writeall(cli, file->fd, 0, buf, file->offset, count, &ret);
443 if (!NT_STATUS_IS_OK(status)) {
444 return os2cli_errno(cli);
445 }
446
447 file->updatetime = 1;
448 file->offset += ret;
449 *result = ret;
450 return 0;
451}
452
453/*****************************************************
454a wrapper for close()
455*******************************************************/
456int _System smbwrp_close(cli_state * cli, smbwrp_file * file)
457{
458 int rc = 0;
459 if (!cli || !file)
460 {
461 return maperror(EINVAL);
462 }
463
464 debuglocal(4,"smpwrp_close updatetime: %d\n", file->updatetime);
465
466 if (file->updatetime == 1)
467 {
468 file->mtime = time(NULL);
469 debuglocal(4,"cli_close new mtime %lu\n", file->mtime);
470 }
471
472 if (!NT_STATUS_IS_OK(cli_close(cli, file->fd)))
473 {
474 rc = os2cli_errno(cli);
475 }
476
477 if (!rc && (file->openattr || file->mtime || file->ctime))
478 {
479 debuglocal(4,"Set pathinfo on close %s %08x %d %d\n", file->fname, file->openattr, file->mtime, file->ctime);
480 if (!NT_STATUS_IS_OK(cli_setpathinfo_basic(cli, file->fname, file->ctime, 0, file->mtime, 0, file->openattr)))
481 {
482 debuglocal(4,"Set pathinfo on close failed %d\n", os2cli_errno(cli));
483 //rc = os2cli_errno(cli);
484 }
485 }
486
487 file->openattr = 0;
488 file->mtime = 0;
489 file->ctime = 0;
490 file->updatetime = 0;
491 file->fd = -1;
492 file->offset = 0;
493 *file->fname = 0;
494 return rc;
495}
496
497int _System smbwrp_setfilesize(cli_state * cli, smbwrp_file * file, long long newsize)
498{
499 int rc = 0;
500 if (!cli || !file)
501 {
502 return maperror(EINVAL);
503 }
504
505 debuglocal(4,"cli_setnewfilesize(%s) %lld\n", file->fname, newsize);
506 if (!NT_STATUS_IS_OK(cli_ftruncate(cli, file->fd, newsize)))
507 {
508 if (newsize)
509 {
510 rc = os2cli_errno(cli);
511 }
512
513 if (!NT_STATUS_IS_OK(cli_close(cli, file->fd)))
514 {
515 return os2cli_errno(cli);
516 }
517 uint16_t fd = 0;
518 file->fd = -1;
519 file->offset = 0;
520 file->openmode &= ~(O_CREAT | O_EXCL);
521 file->openmode |= O_TRUNC;
522 debuglocal(4,"cli_setnewfileszie : cli_open(%s) attr %08x mode %02x denymode %02x\n", file->fname, file->openattr, file->openmode, file->denymode);
523 if (!NT_STATUS_IS_OK(cli_open(cli, file->fname, file->openmode, file->denymode, &fd)))
524 {
525 return os2cli_errno(cli);
526 }
527 file->fd = fd;
528 }
529 return 0;
530}
531
532/*****************************************************
533a wrapper for rename()
534*******************************************************/
535int _System smbwrp_rename(cli_state * cli, char *oldname, char *newname)
536{
537 if (!cli || !oldname || !newname)
538 {
539 return maperror(EINVAL);
540 }
541
542 debuglocal(1,"Rename <%s> -> <%s>\n", oldname, newname);
543 if (!NT_STATUS_IS_OK(cli_rename(cli, oldname, newname)))
544 {
545 return os2cli_errno(cli);
546 }
547 return 0;
548}
549
550
551/*****************************************************
552a wrapper for chmod()
553*******************************************************/
554int _System smbwrp_setattr(cli_state * cli, smbwrp_fileinfo *finfo)
555{
556 if (!cli || !finfo || !*finfo->fname)
557 {
558 return maperror(EINVAL);
559 }
560
561debuglocal(4,"Setting on <%s> attr %04x, time %lu (timezone /%lu\n", finfo->fname, finfo->attr, finfo->mtime, finfo->mtime + get_time_zone(finfo->mtime));
562 // we already have gmt time, so no need to add timezone
563 // if (!cli_setatr(cli, finfo->fname, finfo->attr, finfo->mtime + (finfo->mtime == 0 ? 0 : get_time_zone(finfo->mtime)))
564 if (!NT_STATUS_IS_OK(cli_setatr(cli, finfo->fname, finfo->attr, finfo->mtime))
565 && !NT_STATUS_IS_OK(cli_setatr(cli, finfo->fname, finfo->attr, 0)))
566 {
567 return os2cli_errno(cli);
568 }
569 return 0;
570}
571
572/*****************************************************
573a wrapper for unlink()
574*******************************************************/
575int _System smbwrp_unlink(cli_state * cli, const char *fname)
576{
577 if (!cli || !fname)
578 {
579 return maperror(EINVAL);
580 }
581#if 0
582 if (strncmp(cli->dev, "LPT", 3) == 0)
583 {
584 int job = smbw_stat_printjob(cli, fname, NULL, NULL);
585 if (job == -1)
586 {
587 goto failed;
588 }
589 if (cli_printjob_del(cli, job) != 0)
590 {
591 goto failed;
592 }
593 } else
594#endif
595 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN)))
596 {
597 return os2cli_errno(cli);
598 }
599 return 0;
600}
601
602/*****************************************************
603a wrapper for lseek()
604*******************************************************/
605int _System smbwrp_lseek(cli_state * cli, smbwrp_file * file, int whence, long long offset)
606{
607 off_t size;
608 if (!cli || !file)
609 {
610 return maperror(EINVAL);
611 }
612
613 debuglocal(4,"lseek %d %lld %lld\n", whence, offset, file->offset);
614
615 switch (whence) {
616 case SEEK_SET:
617 if (offset < 0)
618 {
619 return maperror(EINVAL);
620 }
621 file->offset = offset;
622 break;
623 case SEEK_CUR:
624 file->offset += offset;
625 break;
626 case SEEK_END:
627 if (offset > 0)
628 {
629 return maperror(EINVAL);
630 }
631 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(cli, file->fd,
632 NULL, &size, NULL, NULL, NULL,
633 NULL, NULL)) &&
634 !NT_STATUS_IS_OK(cli_getattrE(cli, file->fd,
635 NULL, &size, NULL, NULL, NULL)))
636 {
637 return os2cli_errno(cli);
638 }
639 file->offset = size + offset;
640 break;
641 default: return maperror(EINVAL);
642 }
643
644 return 0;
645}
646
647/*****************************************************
648try to do a QPATHINFO and if that fails then do a getatr
649this is needed because win95 sometimes refuses the qpathinfo
650*******************************************************/
651int _System smbwrp_getattr(smbwrp_server *srv, cli_state * cli, smbwrp_fileinfo *finfo)
652{
653 SMB_INO_T ino = 0;
654 struct timespec ctime;
655 struct timespec mtime;
656 struct timespec atime;
657
658 if (!cli || !finfo || !*finfo->fname)
659 {
660 return maperror(EINVAL);
661 }
662 debuglocal(4,"getattr %d %d <%s>\n", smb1cli_conn_capabilities(cli->conn) & CAP_NOPATHINFO2, smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS, finfo->fname);
663
664 if (NT_STATUS_IS_OK(cli_qpathinfo2(cli, finfo->fname, &ctime, &atime, &mtime, NULL,
665 (off_t *)&finfo->size, (unsigned short *)&finfo->attr, &ino)))
666 {
667 finfo->attr &= 0x7F;
668 finfo->ctime = convert_timespec_to_time_t(ctime);
669 finfo->atime = convert_timespec_to_time_t(atime);
670 finfo->mtime = convert_timespec_to_time_t(mtime);
671 return 0;
672 }
673
674#if 0 // cli->fd not available in Samba 4.x
675 if (cli->fd == -1)
676 {
677 /* fd == -1 means the connection is broken */
678 return maperror(ENOTCONN);
679 }
680#endif
681
682 debuglocal(4, "smbwrp_getattr, calling cli_qpathinfo3\n");
683 if (NT_STATUS_IS_OK(cli_qpathinfo3(cli, finfo->fname, &ctime, &atime, &mtime, NULL,
684 (off_t *)&finfo->size, (unsigned short *)&finfo->attr, &ino)))
685 {
686 finfo->attr &= 0x7F;
687 finfo->ctime = convert_timespec_to_time_t(ctime);
688 finfo->atime = convert_timespec_to_time_t(atime);
689 finfo->mtime = convert_timespec_to_time_t(mtime);
690 return 0;
691 }
692
693 /* If the path is not on a share (it is a workgroup or a server),
694 * then cli_qpathinfo2 obviously fails. Return some fake information
695 * about the directory.
696 */
697 if ( *srv->server_name == 0
698 || (strcmp(cli->dev,"IPC") == 0)
699 || *srv->share_name == 0
700 || (stricmp(srv->share_name,"IPC$") == 0)
701 || (strncmp(cli->dev,"LPT",3) == 0)
702 )
703 {
704 debuglocal(4,"getattr not a share.\n");
705 *(time_t *)&finfo->ctime = time (NULL);
706 *(time_t *)&finfo->atime = time (NULL);
707 *(time_t *)&finfo->mtime = time (NULL);
708 finfo->size = 0;
709 finfo->easize = 0;
710 finfo->attr = aDIR;
711 return 0;
712 }
713
714 /* if this is NT then don't bother with the getatr */
715 if (smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS/* && !(smb1cli_conn_capabilities(cli->conn) & CAP_NOPATHINFO2)*/)
716 {
717 int ret = cli_errno(cli);
718 // cli_qpathinfo* reports EINVAL when path of given file not exists
719 // thus there is no real situation when EINVAL should be returned to
720 // client at this point, we just replace it to ENOTDIR
721 if (ret == EINVAL)
722 {
723 ret = ENOTDIR;
724 }
725 return maperror(ret);
726 }
727
728 if (NT_STATUS_IS_OK(cli_getatr(cli, finfo->fname, (unsigned short *)&finfo->attr, &finfo->size, (time_t *)&finfo->mtime)))
729 {
730//debuglocal(2,("gotattr1 %08x <%s>\n", finfo->attr, finfo->fname));
731 finfo->mtime -= get_time_zone(finfo->mtime);
732 finfo->atime = finfo->atime; //was mtime
733 finfo->ctime = finfo->ctime; //was mtime
734 return 0;
735 }
736 return os2cli_errno(cli);
737}
738
739/*****************************************************
740try to do a QPATHINFO and if that fails then do a getatr
741this is needed because win95 sometimes refuses the qpathinfo
742*******************************************************/
743int _System smbwrp_fgetattr(cli_state * cli, smbwrp_file *file, smbwrp_fileinfo *finfo)
744{
745 struct timespec ctime;
746 struct timespec mtime;
747 struct timespec atime;
748 SMB_INO_T ino = 0;
749
750 if (!cli || !file || !finfo)
751 {
752 return maperror(EINVAL);
753 }
754
755 strncpy(finfo->fname, file->fname, sizeof(finfo->fname) - 1);
756 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(cli, file->fd,
757 (unsigned short *)&finfo->attr, (off_t *)&finfo->size, &ctime, &atime, &mtime, NULL,
758 &ino)))
759 {
760 if (!NT_STATUS_IS_OK(cli_getattrE(cli, file->fd,
761 (unsigned short *)&finfo->attr, (&finfo->size), (time_t *)&finfo->ctime, (time_t *)&finfo->atime, (time_t *)&finfo->mtime)))
762 {
763 return os2cli_errno(cli);
764 }
765 else
766 {
767 finfo->ctime -= get_time_zone(finfo->ctime);
768 finfo->atime -= get_time_zone(finfo->atime);
769 finfo->mtime -= get_time_zone(finfo->mtime);
770 }
771 }
772 else
773 {
774 finfo->ctime = convert_timespec_to_time_t(ctime);
775 finfo->atime = convert_timespec_to_time_t(atime);
776 finfo->mtime = convert_timespec_to_time_t(mtime);
777 }
778
779 return 0;
780}
781
782// =============================DIRECTORY ROUTINES============================
783
784/*****************************************************
785add a entry to a directory listing
786*******************************************************/
787static NTSTATUS smbwrp_dir_add(const char* mnt, smbwrp_fileinfo *finfo, const char *mask, void *state)
788{
789 if (state && finfo)
790 {
791 filelist_state * st = (filelist_state *)state;
792 char fullname[ _MAX_PATH] = {0};
793 debuglocal(8,"adding <%s> %d %d %d\n", finfo->fname, sizeof(st->finfo), st->datalen, sizeof(st->finfo.fname));
794 memcpy(&st->finfo, finfo, sizeof(st->finfo));
795 strncpy(fullname, st->dir, strlen(st->dir));
796 strncat(fullname, finfo->fname, sizeof(fullname) - strlen(fullname) -1);
797 strncpy(st->finfo.fname, fullname, sizeof(st->finfo.fname));
798 getfindinfoL( st->pConn, st->plist, &st->finfo, st->ulAttribute, st->dir_mask);
799 }
800}
801
802static void smbwrp_special_add(const char * name, void * state)
803{
804 smbwrp_fileinfo finfo = {0};
805
806 if (!name)
807 {
808 return;
809 }
810
811 ZERO_STRUCT(finfo);
812
813 strncpy(finfo.fname, name, sizeof(finfo.fname) - 1);
814 finfo.attr = aRONLY | aDIR;
815
816 smbwrp_dir_add("", &finfo, NULL, state);
817}
818
819static NTSTATUS smbwrp_printjob_add(struct print_job_info *job, void * state)
820{
821 smbwrp_fileinfo finfo = {0};
822
823 ZERO_STRUCT(finfo);
824
825//printf("Printjob <%s>\n", job->name);
826
827 strncpy(finfo.fname, job->name, sizeof(finfo.fname) - 1);
828 finfo.mtime = job->t - get_time_zone(job->t);
829 finfo.atime = finfo.atime; //was mtime
830 finfo.ctime = finfo.ctime; //was mtime
831 finfo.attr = aRONLY;
832 finfo.size = job->size;
833
834 smbwrp_dir_add("", &finfo, NULL, state);
835}
836
837static void smbwrp_share_add(const char *share, uint32_t type,
838 const char *comment, void *state)
839{
840 smbwrp_fileinfo finfo = {0};
841
842 // strip administrative names and printers from list
843 if (type == STYPE_PRINTQ || strcmp(share,"IPC$") == 0) return;
844
845 ZERO_STRUCT(finfo);
846
847 strncpy(finfo.fname, share, sizeof(finfo.fname) - 1);
848 finfo.attr = aRONLY | aDIR;
849
850 smbwrp_dir_add("", &finfo, NULL, state);
851}
852
853/****************************************************************************
854 Do a directory listing, calling fn on each file found.
855 Modified from cli_list
856****************************************************************************/
857static int list_files(struct cli_state *cli, const char *mask, uint16_t attribute,
858 void (*fn)(const char *, smbwrp_fileinfo *, const char *,
859 void *), void *state)
860{
861 TALLOC_CTX *frame = talloc_stackframe();
862 struct event_context *ev;
863 struct tevent_req *req;
864 NTSTATUS status = NT_STATUS_NO_MEMORY;
865 struct file_info *finfo;
866 size_t i, num_finfo;
867 uint16_t info_level;
868 void *dircachectx = NULL;
869 smbwrp_fileinfo wrpfinfo;
870
871 /* Try to get the listing from cache. */
872 if (dircache_list_files(fn, state, &num_finfo))
873 {
874 /* Got from cache. */
875 return(num_finfo);
876 }
877
878 if (smbXcli_conn_has_async_calls(cli->conn)) {
879 /*
880 * Can't use sync call while an async call is in flight
881 */
882 status = NT_STATUS_INVALID_PARAMETER;
883 goto fail;
884 }
885 ev = samba_tevent_context_init(frame);
886 if (ev == NULL) {
887 goto fail;
888 }
889
890 info_level = (smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS)
891 ? SMB_FIND_FILE_BOTH_DIRECTORY_INFO : SMB_FIND_EA_SIZE;
892
893 debuglocal(4,"list_files level %d. mask <%s>\n", info_level, mask);
894
895 req = cli_list_send(frame, ev, cli, mask, attribute, info_level);
896 if (req == NULL) {
897 goto fail;
898 }
899 if (!tevent_req_poll(req, ev)) {
900 status = map_nt_error_from_unix(errno);
901 goto fail;
902 }
903
904 status = cli_list_recv(req, frame, &finfo, &num_finfo);
905 if (!NT_STATUS_IS_OK(status)) {
906 goto fail;
907 }
908
909 dircachectx = dircache_write_begin(state, num_finfo);
910
911 debuglocal(4,"list_files: got %d files\n", num_finfo);
912
913
914 for (i=0; i<num_finfo; i++) {
915 //as samba and this client have different finfo, we need to convert
916 memset(&wrpfinfo, 0, sizeof(wrpfinfo));
917 wrpfinfo.size = finfo[i].size;
918 wrpfinfo.attr = finfo[i].mode;
919 wrpfinfo.ctime = convert_timespec_to_time_t(finfo[i].ctime_ts);
920 wrpfinfo.mtime = convert_timespec_to_time_t(finfo[i].mtime_ts);
921 wrpfinfo.atime = convert_timespec_to_time_t(finfo[i].atime_ts);
922 wrpfinfo.easize = finfo[i].easize;
923 strncpy(wrpfinfo.fname, finfo[i].name, sizeof(wrpfinfo.fname) -1);
924
925 fn(cli->dfs_mountpoint, &wrpfinfo, mask, state);
926 // Also add the entry to the cache.
927 dircache_write_entry(dircachectx, &wrpfinfo);
928 }
929
930 dircache_write_end(dircachectx);
931 fail:
932 TALLOC_FREE(frame);
933 return num_finfo;
934}
935
936/*****************************************************
937open a directory on the server
938*******************************************************/
939int _System smbwrp_filelist(smbwrp_server *srv, cli_state * cli, filelist_state * state)
940{
941 if (!srv || !cli || !state || !*state->mask)
942 {
943 return maperror(EINVAL);
944 }
945 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);
946 if (*srv->workgroup == 0 && *srv->server_name == 0)
947 {
948 smbwrp_special_add(".", state);
949 smbwrp_special_add("..", state);
950 cli_NetServerEnum(cli, srv->master, SV_TYPE_DOMAIN_ENUM,
951 smbwrp_share_add, state);
952 } else
953 if (*srv->server_name == 0)
954 {
955 smbwrp_special_add(".", state);
956 smbwrp_special_add("..", state);
957
958 cli_NetServerEnum(cli, srv->workgroup, SV_TYPE_ALL,
959 smbwrp_share_add, state);
960 } else
961 if ((strcmp(cli->dev,"IPC") == 0) || *srv->share_name == 0 || (stricmp(srv->share_name,"IPC$") == 0))
962 {
963 smbwrp_special_add(".", state);
964 smbwrp_special_add("..", state);
965
966 if (net_share_enum_rpc(cli, smbwrp_share_add, state) < 0 &&
967 cli_RNetShareEnum(cli,smbwrp_share_add, state) < 0)
968 {
969 return os2cli_errno(cli);
970 }
971 } else
972 if (strncmp(cli->dev,"LPT",3) == 0)
973 {
974 smbwrp_special_add(".", state);
975 smbwrp_special_add("..", state);
976 if (cli_print_queue_state(cli, smbwrp_printjob_add, state) < 0)
977 {
978 return os2cli_errno(cli);
979 }
980 }
981 else
982 {
983 if (list_files(cli, state->mask, aHIDDEN|aSYSTEM|aDIR,
984 smbwrp_dir_add, state) < 0)
985 {
986 return os2cli_errno(cli);
987 }
988 }
989
990 return 0;
991}
992
993/*****************************************************
994a wrapper for chdir()
995*******************************************************/
996int _System smbwrp_chdir(smbwrp_server *srv, cli_state * cli, char *fname)
997{
998 unsigned short mode = aDIR;
999 smbwrp_fileinfo finfo = {0};
1000 if (!cli || !fname)
1001 {
1002 return maperror(EINVAL);
1003 }
1004
1005 strncpy(finfo.fname, fname, sizeof(finfo.fname) - 1);
1006 if (smbwrp_getattr(srv, cli, &finfo))
1007 {
1008 return os2cli_errno(cli);
1009 }
1010
1011 if (!(finfo.attr & aDIR)) {
1012 return maperror(ENOTDIR);
1013 }
1014
1015 return 0;
1016}
1017
1018
1019/*****************************************************
1020a wrapper for mkdir()
1021*******************************************************/
1022int _System smbwrp_mkdir(cli_state * cli, char *fname)
1023{
1024 if (!cli || !fname)
1025 {
1026 return maperror(EINVAL);
1027 }
1028
1029 if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname)))
1030 {
1031 return os2cli_errno(cli);
1032 }
1033 return 0;
1034}
1035
1036/*****************************************************
1037a wrapper for rmdir()
1038*******************************************************/
1039int _System smbwrp_rmdir(cli_state * cli, char *fname)
1040{
1041 if (!cli || !fname)
1042 {
1043 return maperror(EINVAL);
1044 }
1045
1046 if (!NT_STATUS_IS_OK(cli_rmdir(cli, fname)))
1047 {
1048 return os2cli_errno(cli);
1049 }
1050 return 0;
1051}
1052
1053/*****************************************************
1054set EA for a path
1055*******************************************************/
1056int _System smbwrp_setea(cli_state * cli, char *fname, char * name, unsigned char * value, int size)
1057{
1058 if (!cli || !fname || !name)
1059 {
1060 return maperror(EINVAL);
1061 }
1062 if (!NT_STATUS_IS_OK(cli_set_ea_path(cli, fname, name, value, size)))
1063 {
1064 return os2cli_errno(cli);
1065 }
1066 return 0;
1067}
1068
1069/*****************************************************
1070set EA for a file
1071*******************************************************/
1072int _System smbwrp_fsetea(cli_state * cli, smbwrp_file *file, char * name, unsigned char * value, int size)
1073{
1074 if (!cli || !file || !name)
1075 {
1076 return maperror(EINVAL);
1077 }
1078 if (!NT_STATUS_IS_OK(cli_set_ea_fnum(cli, file->fd, name, value, size)))
1079 {
1080 return os2cli_errno(cli);
1081 }
1082 return 0;
1083}
1084
1085
1086#pragma pack(1)
1087typedef struct _FEA /* fea */
1088{
1089 unsigned char fEA; /* flags */
1090 unsigned char cbName; /* name length not including NULL */
1091 unsigned short cbValue; /* value length */
1092} FEA;
1093
1094typedef struct _FEALIST /* feal */
1095{
1096 unsigned long cbList; /* total bytes of structure including full list */
1097 FEA list[1]; /* variable length FEA structures */
1098} FEALIST;
1099#pragma pack()
1100
1101static int unilistea(cli_state * cli, char *fname, void * buffer, unsigned long size)
1102{
1103 int fnum, i;
1104 int gotsize = sizeof(unsigned long);
1105 size_t num_eas;
1106 struct ea_struct *ea_list = NULL;
1107 TALLOC_CTX *mem_ctx;
1108 FEA * p;
1109 FEALIST * pfealist;
1110 char * q;
1111
1112 mem_ctx = talloc_init("%d: ealist", _gettid());
1113 pfealist = (FEALIST *)buffer;
1114 pfealist->cbList = 0;
1115
1116 if (!NT_STATUS_IS_OK(cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list)))
1117 {
1118 debuglocal(4,"ea_get_file list failed - %s\n", cli_errstr(cli));
1119 talloc_destroy(mem_ctx);
1120 return os2cli_errno(cli);
1121 }
1122
1123 debuglocal(4,"num_eas = %d\n", num_eas);
1124
1125 // we will count that os/2 max EA size for file is 64kb
1126 p = pfealist->list;
1127 for (i = 0; i < num_eas; i++)
1128 {
1129 int namelen = strlen(ea_list[i].name);
1130 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);
1131 if (namelen > 0xFF || ea_list[i].value.length > 0xFFFF)
1132 {
1133 debuglocal(4, "Skip EA <%s> with namelen %d, size %d\n", ea_list[i].name, namelen, ea_list[i].value.length);
1134 continue;
1135 }
1136 gotsize += sizeof(FEA) + namelen + ea_list[i].value.length + 1;
1137 if (size >= gotsize)
1138 {
1139 p->fEA = 0;
1140 p->cbName = namelen;
1141 p->cbValue = ea_list[i].value.length;
1142 q = (char *)(p + 1);
1143 strncpy(q, ea_list[i].name, namelen + 1);
1144 q += namelen + 1;
1145 memcpy(q, ea_list[i].value.data, ea_list[i].value.length);
1146 p = (FEA *)(q + ea_list[i].value.length);
1147 }
1148 }
1149 pfealist->cbList = gotsize;
1150 debuglocal(4,"ret size = %d\n", gotsize);
1151
1152 talloc_destroy(mem_ctx);
1153 return 0;
1154}
1155
1156/*****************************************************
1157lists EA of a path
1158*******************************************************/
1159int _System smbwrp_listea(cli_state * cli, char *fname, void * buffer, unsigned long size)
1160{
1161 if (!cli || !fname || !buffer)
1162 {
1163 return maperror(EINVAL);
1164 }
1165
1166 debuglocal(4,"EALIst for <%s>\n", fname);
1167 return unilistea(cli, fname, buffer, size);
1168}
1169
1170/*****************************************************
1171lists EA of a file
1172*******************************************************/
1173int _System smbwrp_flistea(cli_state * cli, smbwrp_file *file, void * buffer, unsigned long size)
1174{
1175 if (!cli || !file || !buffer)
1176 {
1177 return maperror(EINVAL);
1178 }
1179
1180 debuglocal(4,"FEALIst for <%s>\n", file->fname);
1181 return unilistea(cli, file->fname, buffer, size);
1182}
1183
1184/****************************************************************************
1185Check the space on a device.
1186****************************************************************************/
1187int _System smbwrp_dskattr(cli_state * cli, FSALLOCATE *pfsa)
1188{
1189 int total, bsize, avail;
1190
1191 if (!cli || !pfsa)
1192 {
1193 return maperror(EINVAL);
1194 }
1195
1196 if (!NT_STATUS_IS_OK(cli_dskattr(cli, &bsize, &total, &avail)))
1197 {
1198 debuglocal(4,"Error in dskattr: %s\n",cli_errstr(cli));
1199 return os2cli_errno(cli);
1200 }
1201
1202 debuglocal(4,"\n\t\t%d blocks of size %d. %d blocks available\n",
1203 total, bsize, avail);
1204
1205 // YD currently Samba return it in MB!
1206 pfsa->cSectorUnit = 1;
1207 if (bsize >= 65536)
1208 {
1209 pfsa->cUnit = total*1024;
1210 pfsa->cUnitAvail = avail*1024;
1211 pfsa->cbSector = bsize/1024;
1212 }
1213 else
1214 {
1215 pfsa->cUnit = total;
1216 pfsa->cUnitAvail = avail;
1217 pfsa->cbSector = bsize;
1218 }
1219
1220 return 0;
1221}
1222
1223/*****************************************************
1224Send an echo to the server to confirm it is still alive
1225*******************************************************/
1226int _System smbwrp_echo(cli_state * cli)
1227{
1228 debuglocal(4," smbwrp_echo\n");
1229 unsigned char garbage[16];
1230 NTSTATUS status;
1231 if (!cli)
1232 {
1233 return maperror(EINVAL);
1234 }
1235 /* Ping the server to keep the connection alive using SMBecho. */
1236 memset(garbage, 0xf0, sizeof(garbage));
1237 unsigned int old_timeout = cli->timeout;
1238 cli->timeout = 2000;// we don't want to wait 20 seconds
1239 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
1240 cli->timeout = old_timeout; // reset back to previous value
1241 if (NT_STATUS_IS_OK(status)) {
1242 return 0;
1243 } else {
1244 return -1;
1245 }
1246}
Note: See TracBrowser for help on using the repository browser.