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

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

Support file listings on SMB2+ connections - add missing definition and missing }

  • Property svn:eol-style set to native
File size: 37.5 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
32struct smb2_hnd {
33 uint64_t fid_persistent;
34 uint64_t fid_volatile;
35};
36
37/*
38 * Wrapper for cli_errno to return not connected error on negative fd
39 * Now returns an OS/2 return code instead of lerrno.
40 */
41int os2cli_errno(cli_state * cli)
42{
43#if 0 // cli->fd not available in Samba 4.x
44 if (cli->fd == -1)
45 {
46 return maperror( ENOTCONN);
47 }
48#endif
49 return maperror(cli_errno(cli));
50}
51
52void smbwrp_Logging()
53{
54 char slogfile[_MAX_PATH +1] = {0};
55 char slogfilename[] = "log.smbc";
56 char *env = getenv("LOGFILES");
57 if (env != NULL)
58 {
59 strncpy(slogfile, env, sizeof(slogfile) -1);
60 strncat(slogfile, "\\", sizeof(slogfile) - strlen(slogfile) -1);
61 strncat(slogfile, slogfilename, sizeof(slogfile) - strlen(slogfile) -1);
62 }
63 else
64 {
65 strncpy(slogfile, slogfilename, sizeof(slogfile) -1);
66 }
67
68 // init samba for debug messages
69 setup_logging(slogfile, DEBUG_FILE);
70 lp_set_logfile(slogfile);
71 reopen_logs();
72}
73
74const char * smbwrp_getVersion()
75{
76 return samba_version_string();
77}
78
79int _System smbwrp_getclisize(void)
80{
81 return sizeof(struct cli_state);
82}
83
84/*****************************************************
85initialise structures
86*******************************************************/
87int _System smbwrp_init(void)
88{
89 static int initialised = 0;
90 char *p;
91
92 struct loadparm_context *lp_ctx;
93 TALLOC_CTX *frame = talloc_stackframe();
94
95 if (initialised)
96 {
97 return 0;
98 }
99 initialised = 1;
100
101 smb_init_locale();
102
103 if (!lp_load_client(get_dyn_CONFIGFILE())) {
104 debuglocal(0,("Can't load %s, defaults are used!\n",get_dyn_CONFIGFILE()));
105 }
106 load_interfaces();
107
108 if (!init_names())
109 {
110 return 1;
111 }
112
113 if (writeLog())
114 {
115 smbwrp_Logging();
116 }
117
118/*
119 if ((p=smbw_getshared("RESOLVE_ORDER"))) {
120 lp_set_name_resolve_order(p);
121 }
122*/
123 return 0;
124
125}
126
127void smbwrp_initthread(void)
128{
129 /*
130 Block SIGPIPE (from lib/util_sock.c: write())
131 It is not needed and should not stop execution
132 */
133 BlockSignals(True, SIGPIPE);
134}
135
136#if 0
137/*****************************************************
138remove redundent stuff from a filename
139*******************************************************/
140void clean_fname(char *name)
141{
142 char *p, *p2;
143 int l;
144 int modified = 1;
145
146 if (!name) return;
147
148 while (modified) {
149 modified = 0;
150
151 if ((p=strstr(name,"/./"))) {
152 modified = 1;
153 while (*p) {
154 p[0] = p[2];
155 p++;
156 }
157 }
158
159 if ((p=strstr(name,"//"))) {
160 modified = 1;
161 while (*p) {
162 p[0] = p[1];
163 p++;
164 }
165 }
166
167 if (strcmp(name,"/../")==0) {
168 modified = 1;
169 name[1] = 0;
170 }
171
172 if ((p=strstr(name,"/../"))) {
173 modified = 1;
174 for (p2=(p>name?p-1:p);p2>name;p2--) {
175 if (p2[0] == '/') break;
176 }
177 while (*p2) {
178 p2[0] = p2[3];
179 p2++;
180 }
181 }
182
183 if (strcmp(name,"/..")==0) {
184 modified = 1;
185 name[1] = 0;
186 }
187
188 l = strlen(name);
189 p = l>=3?(name+l-3):name;
190 if (strcmp(p,"/..")==0) {
191 modified = 1;
192 for (p2=p-1;p2>name;p2--) {
193 if (p2[0] == '/') break;
194 }
195 if (p2==name) {
196 p[0] = '/';
197 p[1] = 0;
198 } else {
199 p2[0] = 0;
200 }
201 }
202
203 l = strlen(name);
204 p = l>=2?(name+l-2):name;
205 if (strcmp(p,"/.")==0) {
206 if (p == name) {
207 p[1] = 0;
208 } else {
209 p[0] = 0;
210 }
211 }
212
213 if (strncmp(p=name,"./",2) == 0) {
214 modified = 1;
215 do {
216 p[0] = p[2];
217 } while (*p++);
218 }
219
220 l = strlen(p=name);
221 if (l > 1 && p[l-1] == '/') {
222 modified = 1;
223 p[l-1] = 0;
224 }
225 }
226}
227#endif
228
229/*****************************************************
230return a connection to a server
231loosely based on do_connect() from libsmb/clidfs.c
232*******************************************************/
233int _System smbwrp_connect( Resource* pRes, cli_state ** cli)
234{
235 smbwrp_server * srv = &pRes->srv;
236 char * server = srv->server_name;
237 char * share = *(srv->share_name) ? srv->share_name : "IPC$";
238 char * workgroup = srv->workgroup;
239 struct nmb_name called, calling;
240 char *p, *server_n = server;
241 struct cli_state * c;
242 char* dev_type;
243 int loginerror = 0;
244 NTSTATUS status;
245 int max_protocol = lp__client_max_protocol();
246 int port = 0; //NBT_SMB_PORT;
247 int name_type= 0x20;
248 int flags = 0;
249 enum protocol_types protocol;
250 const char *name = NULL;
251
252 if (!pRes->krb5support)
253 debuglocal(1,"Connecting to \\\\%s:*********@%s:%s\\%s. Master %s:%d\n", srv->username, workgroup, server, share, srv->master, srv->ifmastergroup);
254 else
255 debuglocal(1,"Connecting to \\\\%s:%s\\%s using kerberos authentication. Master %s:%d\n", workgroup, server, share, srv->master, srv->ifmastergroup);
256
257 if (pRes->krb5support) {
258 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
259 }
260
261 if (pRes->ntlmv1support) {
262 lp_set_cmdline("client ntlmv2 auth","no");
263 }
264
265 status = cli_connect_nb(
266 server, NULL, port, name_type, NULL,
267 SMB_SIGNING_DEFAULT, flags, &c);
268
269 if (!NT_STATUS_IS_OK(status)) {
270 debuglocal(4,"Connection to %s failed (Error %s)\n",
271 server,
272 nt_errstr(status));
273 return 3;
274 }
275
276 if (max_protocol == PROTOCOL_DEFAULT) {
277 max_protocol = PROTOCOL_LATEST;
278 }
279 DEBUG(4,(" session request ok, c->timeout = %d\n",c->timeout));
280
281 status = smbXcli_negprot(c->conn, c->timeout,
282 lp_client_min_protocol(),
283 max_protocol);
284
285 if (!NT_STATUS_IS_OK(status)) {
286 debuglocal(4,"protocol negotiation failed: %s\n",
287 nt_errstr(status));
288 cli_shutdown(c);
289 return status;
290 }
291
292 protocol = smbXcli_conn_protocol(c->conn);
293
294 switch (protocol) {
295 case PROTOCOL_CORE:
296 name = "CORE";
297 break;
298 case PROTOCOL_COREPLUS:
299 name = "COREPLUS";
300 break;
301 case PROTOCOL_LANMAN1:
302 name = "LANMAN1";
303 break;
304 case PROTOCOL_LANMAN2:
305 name = "LANMAN2";
306 break;
307 case PROTOCOL_NT1:
308 name = "NT1";
309 break;
310 case PROTOCOL_SMB2_02:
311 name = "SMB2_02";
312 break;
313 case PROTOCOL_SMB2_10:
314 name = "SMB2_10";
315 break;
316 case PROTOCOL_SMB2_22:
317 name = "SMB2_22";
318 break;
319 case PROTOCOL_SMB2_24:
320 name = "SMB2_24";
321 break;
322 case PROTOCOL_SMB3_00:
323 name = "SMB3_00";
324 break;
325 case PROTOCOL_SMB3_02:
326 name = "SMB3_02";
327 break;
328 case PROTOCOL_SMB3_10:
329 name = "SMB3_10";
330 break;
331 case PROTOCOL_SMB3_11:
332 name = "SMB3_11";
333 break;
334 default:
335 name = "Unknown";
336 break;
337 }
338
339 debuglocal(4,"Server supports %s protocol\n", name);
340
341 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
342 /* Ensure we ask for some initial credits. */
343 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
344 }
345
346 if (!NT_STATUS_IS_OK(cli_session_setup(c, srv->username,
347 srv->password, strlen(srv->password),
348 srv->password, strlen(srv->password),
349 workgroup))) {
350 debuglocal(4,"%s/******** login failed\n", srv->username);
351 loginerror = 1; // save the login error
352
353 /* try an anonymous login if it failed */
354
355 /* If a password was not supplied then
356 * try again with a null username. */
357 if (srv->password[0] || !srv->username[0] ||
358 pRes->krb5support ||
359 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
360 "", 0,
361 "", 0,
362 workgroup))) {
363 debuglocal(1,"session setup failed: %s\n",
364 nt_errstr(status));
365
366 if ((NT_STATUS_EQUAL(status,
367 NT_STATUS_MORE_PROCESSING_REQUIRED)) ||
368 NT_STATUS_EQUAL(status,
369 NT_STATUS_INTERNAL_ERROR)){
370 debuglocal(4,"did you forget to run kinit?\n");
371 } else
372 debuglocal(4,"Anonymous login failed\n");
373
374 cli_shutdown(c);
375 return 6;
376 }
377 debuglocal(4,"Anonymous login successful\n");
378 }
379
380 if (!NT_STATUS_IS_OK(status)) {
381 debuglocal(4,"cli_init_creds() failed\n");
382 cli_shutdown(c);
383 // if loginerror is != 0 means normal login failed, but anonymous login worked
384 if (loginerror !=0)
385 return 6;
386 else
387 return 7;
388 }
389
390 debuglocal(4," session setup ok. Sending tconx <%s> <********>\n", share);
391
392 // YD ticket:58 we need to check resource type to avoid connecting to printers.
393 // dev type is set to IPC for IPC$, A: for everything else (printers use LPT1:)
394 if (!strcmp( share, "IPC$"))
395 dev_type = "IPC";
396 else
397 dev_type = "A:";
398
399 if (!NT_STATUS_IS_OK(cli_tcon_andx(c, share, dev_type,
400 srv->password, strlen(srv->password)+1))) {
401 cli_shutdown(c);
402 // if loginerror is != 0 means normal login failed, but anonymous login worked
403 if (loginerror !=0)
404 return 6;
405 else
406 return 7;
407 }
408
409 /* must be a normal share */
410
411 status = cli_tree_connect(c, share, "?????",
412 srv->password, strlen(srv->password)+1);
413 if (!NT_STATUS_IS_OK(status)) {
414 debuglocal(4,"tree connect failed: %s\n", nt_errstr(status));
415 cli_shutdown(c);
416 return status;
417 }
418
419 debuglocal(4," tconx ok.\n");
420
421 // save cli_state pointer
422 *cli = c;
423
424 return 0;
425}
426
427/*****************************************************
428close a connection to a server
429*******************************************************/
430void _System smbwrp_disconnect( Resource* pRes, cli_state * cli)
431{
432 if (pRes && cli)
433 {
434 // this call will free all buffers, close handles and free cli mem
435 cli_shutdown( cli);
436 }
437}
438
439
440
441/*****************************************************
442a wrapper for open()
443*******************************************************/
444int _System smbwrp_open(cli_state * cli, smbwrp_file * file)
445{
446 uint16_t fd = 0;
447
448 if (!cli || !file || !*file->fname)
449 {
450 return maperror(EINVAL);
451 }
452 if (file->denymode < DENY_ALL || file->denymode > DENY_NONE)
453 {
454 file->denymode = DENY_NONE;
455 }
456
457 debuglocal(4,"cli_open(%s) attr %08x mode %02x denymode %02x\n", file->fname, file->openattr, file->openmode, file->denymode);
458 if (!NT_STATUS_IS_OK(cli_open(cli, file->fname, file->openmode, file->denymode, &fd)))
459 {
460 return os2cli_errno(cli);
461 }
462 file->fd = fd;
463 file->updatetime = 0;
464 file->offset = 0;
465 return 0;
466}
467
468/*****************************************************
469a wrapper for read()
470*******************************************************/
471int _System smbwrp_read(cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result)
472{
473 int ret;
474
475 if (!cli || !file || !buf || !result)
476 {
477 return maperror(EINVAL);
478 }
479 size_t nread;
480 *result = 0;
481 ret = cli_read(cli, file->fd, buf, file->offset, count, &nread);
482 if (ret == -1)
483 {
484 debuglocal(4," smbwrp_read - cli_read ret = %d\n",ret);
485 return os2cli_errno(cli);
486 }
487
488 file->offset += nread;
489 *result = nread;
490 debuglocal(4," smbwrp_read successful, nread = %d, ret = %d\n",nread,ret);
491 return 0;
492}
493
494
495
496/*****************************************************
497a wrapper for write()
498*******************************************************/
499int _System smbwrp_write(cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result)
500{
501 NTSTATUS status;
502 size_t ret;
503
504 if (!cli || !file || !buf || !result)
505 {
506 return maperror(EINVAL);
507 }
508
509 *result = 0;
510//debuglocal(1,("Write %x %d %lld %d", cli, file->fd, file->offset, count));
511 status = cli_writeall(cli, file->fd, 0, buf, file->offset, count, &ret);
512 if (!NT_STATUS_IS_OK(status)) {
513 return os2cli_errno(cli);
514 }
515
516 file->updatetime = 1;
517 file->offset += ret;
518 *result = ret;
519 return 0;
520}
521
522/*****************************************************
523a wrapper for close()
524*******************************************************/
525int _System smbwrp_close(cli_state * cli, smbwrp_file * file)
526{
527 int rc = 0;
528 if (!cli || !file)
529 {
530 return maperror(EINVAL);
531 }
532
533 debuglocal(4,"smpwrp_close updatetime: %d\n", file->updatetime);
534
535 if (file->updatetime == 1)
536 {
537 file->mtime = time(NULL);
538 debuglocal(4,"cli_close new mtime %lu\n", file->mtime);
539 }
540
541 if (!NT_STATUS_IS_OK(cli_close(cli, file->fd)))
542 {
543 rc = os2cli_errno(cli);
544 }
545
546 if (!rc && (file->openattr || file->mtime || file->ctime))
547 {
548 debuglocal(4,"Set pathinfo on close %s %08x %d %d\n", file->fname, file->openattr, file->mtime, file->ctime);
549 if (!NT_STATUS_IS_OK(cli_setpathinfo_basic(cli, file->fname, file->ctime, 0, file->mtime, 0, file->openattr)))
550 {
551 debuglocal(4,"Set pathinfo on close failed %d\n", os2cli_errno(cli));
552 //rc = os2cli_errno(cli);
553 }
554 }
555
556 file->openattr = 0;
557 file->mtime = 0;
558 file->ctime = 0;
559 file->updatetime = 0;
560 file->fd = -1;
561 file->offset = 0;
562 *file->fname = 0;
563 return rc;
564}
565
566int _System smbwrp_setfilesize(cli_state * cli, smbwrp_file * file, long long newsize)
567{
568 int rc = 0;
569 if (!cli || !file)
570 {
571 return maperror(EINVAL);
572 }
573
574 debuglocal(4,"cli_setnewfilesize(%s) %lld\n", file->fname, newsize);
575 if (!NT_STATUS_IS_OK(cli_ftruncate(cli, file->fd, newsize)))
576 {
577 if (newsize)
578 {
579 rc = os2cli_errno(cli);
580 }
581
582 if (!NT_STATUS_IS_OK(cli_close(cli, file->fd)))
583 {
584 return os2cli_errno(cli);
585 }
586 uint16_t fd = 0;
587 file->fd = -1;
588 file->offset = 0;
589 file->openmode &= ~(O_CREAT | O_EXCL);
590 file->openmode |= O_TRUNC;
591 debuglocal(4,"cli_setnewfileszie : cli_open(%s) attr %08x mode %02x denymode %02x\n", file->fname, file->openattr, file->openmode, file->denymode);
592 if (!NT_STATUS_IS_OK(cli_open(cli, file->fname, file->openmode, file->denymode, &fd)))
593 {
594 return os2cli_errno(cli);
595 }
596 file->fd = fd;
597 }
598 return 0;
599}
600
601/*****************************************************
602a wrapper for rename()
603*******************************************************/
604int _System smbwrp_rename(cli_state * cli, char *oldname, char *newname)
605{
606 if (!cli || !oldname || !newname)
607 {
608 return maperror(EINVAL);
609 }
610
611 debuglocal(1,"Rename <%s> -> <%s>\n", oldname, newname);
612 if (!NT_STATUS_IS_OK(cli_rename(cli, oldname, newname)))
613 {
614 return os2cli_errno(cli);
615 }
616 return 0;
617}
618
619
620/*****************************************************
621a wrapper for chmod()
622*******************************************************/
623int _System smbwrp_setattr(cli_state * cli, smbwrp_fileinfo *finfo)
624{
625 if (!cli || !finfo || !*finfo->fname)
626 {
627 return maperror(EINVAL);
628 }
629
630debuglocal(4,"Setting on <%s> attr %04x, time %lu (timezone /%lu\n", finfo->fname, finfo->attr, finfo->mtime, finfo->mtime + get_time_zone(finfo->mtime));
631 // we already have gmt time, so no need to add timezone
632 // if (!cli_setatr(cli, finfo->fname, finfo->attr, finfo->mtime + (finfo->mtime == 0 ? 0 : get_time_zone(finfo->mtime)))
633 if (!NT_STATUS_IS_OK(cli_setatr(cli, finfo->fname, finfo->attr, finfo->mtime))
634 && !NT_STATUS_IS_OK(cli_setatr(cli, finfo->fname, finfo->attr, 0)))
635 {
636 return os2cli_errno(cli);
637 }
638 return 0;
639}
640
641/*****************************************************
642a wrapper for unlink()
643*******************************************************/
644int _System smbwrp_unlink(cli_state * cli, const char *fname)
645{
646 if (!cli || !fname)
647 {
648 return maperror(EINVAL);
649 }
650#if 0
651 if (strncmp(cli->dev, "LPT", 3) == 0)
652 {
653 int job = smbw_stat_printjob(cli, fname, NULL, NULL);
654 if (job == -1)
655 {
656 goto failed;
657 }
658 if (cli_printjob_del(cli, job) != 0)
659 {
660 goto failed;
661 }
662 } else
663#endif
664 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN)))
665 {
666 return os2cli_errno(cli);
667 }
668 return 0;
669}
670
671/*****************************************************
672a wrapper for lseek()
673*******************************************************/
674int _System smbwrp_lseek(cli_state * cli, smbwrp_file * file, int whence, long long offset)
675{
676 off_t size;
677 if (!cli || !file)
678 {
679 return maperror(EINVAL);
680 }
681
682 debuglocal(4,"lseek %d %lld %lld\n", whence, offset, file->offset);
683
684 switch (whence) {
685 case SEEK_SET:
686 if (offset < 0)
687 {
688 return maperror(EINVAL);
689 }
690 file->offset = offset;
691 break;
692 case SEEK_CUR:
693 file->offset += offset;
694 break;
695 case SEEK_END:
696 if (offset > 0)
697 {
698 return maperror(EINVAL);
699 }
700 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(cli, file->fd,
701 NULL, &size, NULL, NULL, NULL,
702 NULL, NULL)) &&
703 !NT_STATUS_IS_OK(cli_getattrE(cli, file->fd,
704 NULL, &size, NULL, NULL, NULL)))
705 {
706 return os2cli_errno(cli);
707 }
708 file->offset = size + offset;
709 break;
710 default: return maperror(EINVAL);
711 }
712
713 return 0;
714}
715
716/*****************************************************
717try to do a QPATHINFO and if that fails then do a getatr
718this is needed because win95 sometimes refuses the qpathinfo
719loosely based on SMBC_getatr() from source3/libsmb/libsmb_file.c
720*******************************************************/
721int _System smbwrp_getattr(smbwrp_server *srv, cli_state * cli, smbwrp_fileinfo *finfo)
722{
723 SMB_INO_T ino = 0;
724 struct timespec ctime;
725 struct timespec mtime;
726 struct timespec atime;
727
728 if (!cli || !finfo || !*finfo->fname)
729 {
730 return maperror(EINVAL);
731 }
732 debuglocal(4,"getattr %d %d <%s>\n", smb1cli_conn_capabilities(cli->conn) & CAP_NOPATHINFO2, smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS, finfo->fname);
733
734 if (NT_STATUS_IS_OK(cli_qpathinfo2(cli, finfo->fname, &ctime, &atime, &mtime, NULL,
735 (off_t *)&finfo->size, (unsigned short *)&finfo->attr, &ino)))
736 {
737 finfo->attr &= 0x7F;
738 finfo->ctime = convert_timespec_to_time_t(ctime);
739 finfo->atime = convert_timespec_to_time_t(atime);
740 finfo->mtime = convert_timespec_to_time_t(mtime);
741 return 0;
742 }
743
744#if 0 // cli->fd not available in Samba 4.x
745 if (cli->fd == -1)
746 {
747 /* fd == -1 means the connection is broken */
748 return maperror(ENOTCONN);
749 }
750#endif
751
752 debuglocal(4, "smbwrp_getattr, calling cli_qpathinfo3\n");
753 if (NT_STATUS_IS_OK(cli_qpathinfo3(cli, finfo->fname, &ctime, &atime, &mtime, NULL,
754 (off_t *)&finfo->size, (unsigned short *)&finfo->attr, &ino)))
755 {
756 finfo->attr &= 0x7F;
757 finfo->ctime = convert_timespec_to_time_t(ctime);
758 finfo->atime = convert_timespec_to_time_t(atime);
759 finfo->mtime = convert_timespec_to_time_t(mtime);
760 return 0;
761 }
762
763 /* If the path is not on a share (it is a workgroup or a server),
764 * then cli_qpathinfo2 obviously fails. Return some fake information
765 * about the directory.
766 */
767 if ( *srv->server_name == 0
768 || (strcmp(cli->dev,"IPC") == 0)
769 || *srv->share_name == 0
770 || (stricmp(srv->share_name,"IPC$") == 0)
771 || (strncmp(cli->dev,"LPT",3) == 0)
772 )
773 {
774 debuglocal(4,"getattr not a share.\n");
775 *(time_t *)&finfo->ctime = time (NULL);
776 *(time_t *)&finfo->atime = time (NULL);
777 *(time_t *)&finfo->mtime = time (NULL);
778 finfo->size = 0;
779 finfo->easize = 0;
780 finfo->attr = aDIR;
781 return 0;
782 }
783
784 /* if this is NT then don't bother with the getatr */
785 if (smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS/* && !(smb1cli_conn_capabilities(cli->conn) & CAP_NOPATHINFO2)*/)
786 {
787 int ret = cli_errno(cli);
788 // cli_qpathinfo* reports EINVAL when path of given file not exists
789 // thus there is no real situation when EINVAL should be returned to
790 // client at this point, we just replace it to ENOTDIR
791 if (ret == EINVAL)
792 {
793 ret = ENOTDIR;
794 }
795 return maperror(ret);
796 }
797
798 if (NT_STATUS_IS_OK(cli_getatr(cli, finfo->fname, (unsigned short *)&finfo->attr, &finfo->size, (time_t *)&finfo->mtime)))
799 {
800//debuglocal(2,("gotattr1 %08x <%s>\n", finfo->attr, finfo->fname));
801 finfo->mtime -= get_time_zone(finfo->mtime);
802 finfo->atime = finfo->atime; //was mtime
803 finfo->ctime = finfo->ctime; //was mtime
804 return 0;
805 }
806 return os2cli_errno(cli);
807}
808
809/*****************************************************
810try to do a QPATHINFO and if that fails then do a getatr
811this is needed because win95 sometimes refuses the qpathinfo
812*******************************************************/
813int _System smbwrp_fgetattr(cli_state * cli, smbwrp_file *file, smbwrp_fileinfo *finfo)
814{
815 struct timespec ctime;
816 struct timespec mtime;
817 struct timespec atime;
818 SMB_INO_T ino = 0;
819
820 if (!cli || !file || !finfo)
821 {
822 return maperror(EINVAL);
823 }
824
825 strncpy(finfo->fname, file->fname, sizeof(finfo->fname) - 1);
826 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(cli, file->fd,
827 (unsigned short *)&finfo->attr, (off_t *)&finfo->size, &ctime, &atime, &mtime, NULL,
828 &ino)))
829 {
830 if (!NT_STATUS_IS_OK(cli_getattrE(cli, file->fd,
831 (unsigned short *)&finfo->attr, (&finfo->size), (time_t *)&finfo->ctime, (time_t *)&finfo->atime, (time_t *)&finfo->mtime)))
832 {
833 return os2cli_errno(cli);
834 }
835 else
836 {
837 finfo->ctime -= get_time_zone(finfo->ctime);
838 finfo->atime -= get_time_zone(finfo->atime);
839 finfo->mtime -= get_time_zone(finfo->mtime);
840 }
841 }
842 else
843 {
844 finfo->ctime = convert_timespec_to_time_t(ctime);
845 finfo->atime = convert_timespec_to_time_t(atime);
846 finfo->mtime = convert_timespec_to_time_t(mtime);
847 }
848
849 return 0;
850}
851
852// =============================DIRECTORY ROUTINES============================
853
854/*****************************************************
855add a entry to a directory listing
856*******************************************************/
857static NTSTATUS smbwrp_dir_add(const char* mnt, smbwrp_fileinfo *finfo, const char *mask, void *state)
858{
859 if (state && finfo)
860 {
861 filelist_state * st = (filelist_state *)state;
862 char fullname[ _MAX_PATH] = {0};
863 debuglocal(8,"adding <%s> %d %d %d\n", finfo->fname, sizeof(st->finfo), st->datalen, sizeof(st->finfo.fname));
864 memcpy(&st->finfo, finfo, sizeof(st->finfo));
865 strncpy(fullname, st->dir, strlen(st->dir));
866 strncat(fullname, finfo->fname, sizeof(fullname) - strlen(fullname) -1);
867 strncpy(st->finfo.fname, fullname, sizeof(st->finfo.fname));
868 getfindinfoL( st->pConn, st->plist, &st->finfo, st->ulAttribute, st->dir_mask);
869 }
870}
871
872static void smbwrp_special_add(const char * name, void * state)
873{
874 smbwrp_fileinfo finfo = {0};
875
876 if (!name)
877 {
878 return;
879 }
880
881 ZERO_STRUCT(finfo);
882
883 strncpy(finfo.fname, name, sizeof(finfo.fname) - 1);
884 finfo.attr = aRONLY | aDIR;
885
886 smbwrp_dir_add("", &finfo, NULL, state);
887}
888
889static void smbwrp_printjob_add(struct print_job_info *job, void * state)
890{
891 smbwrp_fileinfo finfo = {0};
892
893 ZERO_STRUCT(finfo);
894
895//printf("Printjob <%s>\n", job->name);
896
897 strncpy(finfo.fname, job->name, sizeof(finfo.fname) - 1);
898 finfo.mtime = job->t - get_time_zone(job->t);
899 finfo.atime = finfo.atime; //was mtime
900 finfo.ctime = finfo.ctime; //was mtime
901 finfo.attr = aRONLY;
902 finfo.size = job->size;
903
904 smbwrp_dir_add("", &finfo, NULL, state);
905}
906
907static void smbwrp_share_add(const char *share, uint32_t type,
908 const char *comment, void *state)
909{
910 smbwrp_fileinfo finfo = {0};
911
912 // strip administrative names and printers from list
913 if (type == STYPE_PRINTQ || strcmp(share,"IPC$") == 0) return;
914
915 ZERO_STRUCT(finfo);
916
917 strncpy(finfo.fname, share, sizeof(finfo.fname) - 1);
918 finfo.attr = aRONLY | aDIR;
919
920 smbwrp_dir_add("", &finfo, NULL, state);
921}
922
923/***************************************************************
924 Wrapper that allows SMB2 to list a directory.
925 Synchronous only.
926 Based on cli_smb2_list
927***************************************************************/
928
929NTSTATUS list_files_smb2(struct cli_state *cli,
930 const char *pathname,
931 uint16_t attribute,
932 NTSTATUS (*fn)(const char *,
933 struct smbwrp_fileinfo *,
934 const char *,
935 void *),
936 void *state)
937{
938 NTSTATUS status;
939 uint16_t fnum = 0xffff;
940 char *parent_dir = NULL;
941 const char *mask = NULL;
942 struct smb2_hnd *ph = NULL;
943 bool processed_file = false;
944 TALLOC_CTX *frame = talloc_stackframe();
945 TALLOC_CTX *subframe = NULL;
946 bool mask_has_wild;
947 void *dircachectx = NULL;
948 smbwrp_fileinfo wrpfinfo;
949
950 if (smbXcli_conn_has_async_calls(cli->conn)) {
951 /*
952 * Can't use sync call while an async call is in flight
953 */
954 status = NT_STATUS_INVALID_PARAMETER;
955 goto fail;
956 }
957
958 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
959 status = NT_STATUS_INVALID_PARAMETER;
960 goto fail;
961 }
962
963 /* Get the directory name. */
964 if (!windows_parent_dirname(frame,
965 pathname,
966 &parent_dir,
967 &mask)) {
968 status = NT_STATUS_NO_MEMORY;
969 goto fail;
970 }
971
972 mask_has_wild = ms_has_wild(mask);
973
974 status = cli_smb2_create_fnum(cli,
975 parent_dir,
976 0, /* create_flags */
977 SEC_DIR_LIST|SEC_DIR_READ_ATTRIBUTE,/* desired_access */
978 FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
979 FILE_SHARE_READ|FILE_SHARE_WRITE, /* share_access */
980 FILE_OPEN, /* create_disposition */
981 FILE_DIRECTORY_FILE, /* create_options */
982 &fnum,
983 NULL);
984
985 if (!NT_STATUS_IS_OK(status)) {
986 goto fail;
987 }
988
989 status = map_fnum_to_smb2_handle(cli,
990 fnum,
991 &ph);
992 if (!NT_STATUS_IS_OK(status)) {
993 goto fail;
994 }
995
996 do {
997 uint8_t *dir_data = NULL;
998 uint32_t dir_data_length = 0;
999 uint32_t next_offset = 0;
1000 subframe = talloc_stackframe();
1001
1002 status = smb2cli_query_directory(cli->conn,
1003 cli->timeout,
1004 cli->smb2.session,
1005 cli->smb2.tcon,
1006 SMB2_FIND_ID_BOTH_DIRECTORY_INFO,
1007 0, /* flags */
1008 0, /* file_index */
1009 ph->fid_persistent,
1010 ph->fid_volatile,
1011 mask,
1012 0xffff,
1013 subframe,
1014 &dir_data,
1015 &dir_data_length);
1016
1017 if (!NT_STATUS_IS_OK(status)) {
1018 if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES)) {
1019 break;
1020 }
1021 goto fail;
1022 }
1023
1024 do {
1025 struct file_info *finfo = talloc_zero(subframe,
1026 struct file_info);
1027
1028 if (finfo == NULL) {
1029 status = NT_STATUS_NO_MEMORY;
1030 goto fail;
1031 }
1032
1033 status = parse_finfo_id_both_directory_info(dir_data,
1034 dir_data_length,
1035 finfo,
1036 &next_offset);
1037
1038 if (!NT_STATUS_IS_OK(status)) {
1039 goto fail;
1040 }
1041
1042 if (dir_check_ftype((uint32_t)finfo->mode,
1043 (uint32_t)attribute)) {
1044 /*
1045 * Only process if attributes match.
1046 * On SMB1 server does this, so on
1047 * SMB2 we need to emulate in the
1048 * client.
1049 *
1050 * https://bugzilla.samba.org/show_bug.cgi?id=10260
1051 */
1052 processed_file = true;
1053
1054 //as samba and this client have different finfo, we need to convert
1055 memset(&wrpfinfo, 0, sizeof(wrpfinfo));
1056 wrpfinfo.size = finfo[0].size;
1057 wrpfinfo.attr = finfo[0].mode;
1058 wrpfinfo.ctime = convert_timespec_to_time_t(finfo[0].ctime_ts);
1059 wrpfinfo.mtime = convert_timespec_to_time_t(finfo[0].mtime_ts);
1060 wrpfinfo.atime = convert_timespec_to_time_t(finfo[0].atime_ts);
1061 wrpfinfo.easize = finfo[0].easize;
1062 strncpy(wrpfinfo.fname, finfo[0].name, sizeof(wrpfinfo.fname) -1);
1063
1064 status = fn(cli->dfs_mountpoint,
1065 &wrpfinfo,
1066 pathname,
1067 state);
1068
1069 if (!NT_STATUS_IS_OK(status)) {
1070 /* not sure why this is required on OS/2 */
1071 if (status != NT_STATUS_WAIT_1)
1072 break;
1073 }
1074 }
1075
1076 TALLOC_FREE(finfo);
1077
1078 /* Move to next entry. */
1079 if (next_offset) {
1080 dir_data += next_offset;
1081 dir_data_length -= next_offset;
1082 }
1083 } while (next_offset != 0);
1084
1085 TALLOC_FREE(subframe);
1086
1087 if (!mask_has_wild) {
1088 /*
1089 * MacOSX 10 doesn't set STATUS_NO_MORE_FILES
1090 * when handed a non-wildcard path. Do it
1091 * for the server (with a non-wildcard path
1092 * there should only ever be one file returned.
1093 */
1094 status = STATUS_NO_MORE_FILES;
1095 break;
1096 }
1097
1098 } while (NT_STATUS_IS_OK(status));
1099
1100 if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES)) {
1101 status = NT_STATUS_OK;
1102 }
1103
1104 if (NT_STATUS_IS_OK(status) && !processed_file) {
1105 /*
1106 * In SMB1 findfirst returns NT_STATUS_NO_SUCH_FILE
1107 * if no files match. Emulate this in the client.
1108 */
1109 status = NT_STATUS_NO_SUCH_FILE;
1110 }
1111
1112 fail:
1113
1114 if (fnum != 0xffff) {
1115 cli_smb2_close_fnum(cli, fnum);
1116 }
1117 TALLOC_FREE(subframe);
1118 TALLOC_FREE(frame);
1119 return status;
1120}
1121
1122
1123/****************************************************************************
1124 Do a directory listing, calling fn on each file found.
1125 Modified from cli_list
1126****************************************************************************/
1127static int list_files(struct cli_state *cli, const char *mask, uint16_t attribute,
1128 void (*fn)(const char *, smbwrp_fileinfo *, const char *,
1129 void *), void *state)
1130{
1131 TALLOC_CTX *frame = talloc_stackframe();
1132 struct event_context *ev;
1133 struct tevent_req *req;
1134 NTSTATUS status = NT_STATUS_NO_MEMORY;
1135 struct file_info *finfo;
1136 size_t i, num_finfo;
1137 uint16_t info_level;
1138 void *dircachectx = NULL;
1139 smbwrp_fileinfo wrpfinfo;
1140
1141 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1142 debuglocal(4,"SMB2 detected, calling list_files_smb2()\n");
1143 return list_files_smb2(cli, mask, attribute, fn, state);
1144 }
1145
1146 /* Try to get the listing from cache. */
1147 if (dircache_list_files(fn, state, &num_finfo))
1148 {
1149 /* Got from cache. */
1150 return(num_finfo);
1151 }
1152
1153 if (smbXcli_conn_has_async_calls(cli->conn)) {
1154 /*
1155 * Can't use sync call while an async call is in flight
1156 */
1157 status = NT_STATUS_INVALID_PARAMETER;
1158 goto fail;
1159 }
1160 ev = samba_tevent_context_init(frame);
1161 if (ev == NULL) {
1162 goto fail;
1163 }
1164
1165 info_level = (smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS)
1166 ? SMB_FIND_FILE_BOTH_DIRECTORY_INFO : SMB_FIND_EA_SIZE;
1167
1168 debuglocal(4,"list_files level %d. mask <%s>\n", info_level, mask);
1169
1170 req = cli_list_send(frame, ev, cli, mask, attribute, info_level);
1171 if (req == NULL) {
1172 goto fail;
1173 }
1174 if (!tevent_req_poll(req, ev)) {
1175 status = map_nt_error_from_unix(errno);
1176 goto fail;
1177 }
1178
1179 status = cli_list_recv(req, frame, &finfo, &num_finfo);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 goto fail;
1182 }
1183
1184 dircachectx = dircache_write_begin(state, num_finfo);
1185
1186 debuglocal(4,"list_files: got %d files\n", num_finfo);
1187
1188
1189 for (i=0; i<num_finfo; i++) {
1190 //as samba and this client have different finfo, we need to convert
1191 memset(&wrpfinfo, 0, sizeof(wrpfinfo));
1192 wrpfinfo.size = finfo[i].size;
1193 wrpfinfo.attr = finfo[i].mode;
1194 wrpfinfo.ctime = convert_timespec_to_time_t(finfo[i].ctime_ts);
1195 wrpfinfo.mtime = convert_timespec_to_time_t(finfo[i].mtime_ts);
1196 wrpfinfo.atime = convert_timespec_to_time_t(finfo[i].atime_ts);
1197 wrpfinfo.easize = finfo[i].easize;
1198 strncpy(wrpfinfo.fname, finfo[i].name, sizeof(wrpfinfo.fname) -1);
1199
1200 fn(cli->dfs_mountpoint, &wrpfinfo, mask, state);
1201 // Also add the entry to the cache.
1202 dircache_write_entry(dircachectx, &wrpfinfo);
1203 }
1204
1205 dircache_write_end(dircachectx);
1206 fail:
1207 TALLOC_FREE(frame);
1208 return num_finfo;
1209}
1210
1211/*****************************************************
1212open a directory on the server
1213*******************************************************/
1214int _System smbwrp_filelist(smbwrp_server *srv, cli_state * cli, filelist_state * state)
1215{
1216 if (!srv || !cli || !state || !*state->mask)
1217 {
1218 return maperror(EINVAL);
1219 }
1220 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);
1221 if (*srv->workgroup == 0 && *srv->server_name == 0)
1222 {
1223 smbwrp_special_add(".", state);
1224 smbwrp_special_add("..", state);
1225 cli_NetServerEnum(cli, srv->master, SV_TYPE_DOMAIN_ENUM,
1226 smbwrp_share_add, state);
1227 } else
1228 if (*srv->server_name == 0)
1229 {
1230 smbwrp_special_add(".", state);
1231 smbwrp_special_add("..", state);
1232
1233 cli_NetServerEnum(cli, srv->workgroup, SV_TYPE_ALL,
1234 smbwrp_share_add, state);
1235 } else
1236 if ((strcmp(cli->dev,"IPC") == 0) || *srv->share_name == 0 || (stricmp(srv->share_name,"IPC$") == 0))
1237 {
1238 smbwrp_special_add(".", state);
1239 smbwrp_special_add("..", state);
1240
1241 if (net_share_enum_rpc(cli, smbwrp_share_add, state) < 0 &&
1242 cli_RNetShareEnum(cli,smbwrp_share_add, state) < 0)
1243 {
1244 return os2cli_errno(cli);
1245 }
1246 } else
1247 if (strncmp(cli->dev,"LPT",3) == 0)
1248 {
1249 smbwrp_special_add(".", state);
1250 smbwrp_special_add("..", state);
1251 if (cli_print_queue_state(cli, smbwrp_printjob_add, state) < 0)
1252 {
1253 return os2cli_errno(cli);
1254 }
1255 }
1256 else
1257 {
1258 if (list_files(cli, state->mask, aHIDDEN|aSYSTEM|aDIR,
1259 smbwrp_dir_add, state) < 0)
1260 {
1261 return os2cli_errno(cli);
1262 }
1263 }
1264
1265 return 0;
1266}
1267
1268/*****************************************************
1269a wrapper for chdir()
1270*******************************************************/
1271int _System smbwrp_chdir(smbwrp_server *srv, cli_state * cli, char *fname)
1272{
1273 unsigned short mode = aDIR;
1274 smbwrp_fileinfo finfo = {0};
1275 if (!cli || !fname)
1276 {
1277 return maperror(EINVAL);
1278 }
1279
1280 strncpy(finfo.fname, fname, sizeof(finfo.fname) - 1);
1281 if (smbwrp_getattr(srv, cli, &finfo))
1282 {
1283 return os2cli_errno(cli);
1284 }
1285
1286 if (!(finfo.attr & aDIR)) {
1287 return maperror(ENOTDIR);
1288 }
1289
1290 return 0;
1291}
1292
1293
1294/*****************************************************
1295a wrapper for mkdir()
1296*******************************************************/
1297int _System smbwrp_mkdir(cli_state * cli, char *fname)
1298{
1299 if (!cli || !fname)
1300 {
1301 return maperror(EINVAL);
1302 }
1303
1304 if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname)))
1305 {
1306 return os2cli_errno(cli);
1307 }
1308 return 0;
1309}
1310
1311/*****************************************************
1312a wrapper for rmdir()
1313*******************************************************/
1314int _System smbwrp_rmdir(cli_state * cli, char *fname)
1315{
1316 if (!cli || !fname)
1317 {
1318 return maperror(EINVAL);
1319 }
1320
1321 if (!NT_STATUS_IS_OK(cli_rmdir(cli, fname)))
1322 {
1323 return os2cli_errno(cli);
1324 }
1325 return 0;
1326}
1327
1328/*****************************************************
1329set EA for a path
1330*******************************************************/
1331int _System smbwrp_setea(cli_state * cli, char *fname, char * name, unsigned char * value, int size)
1332{
1333 if (!cli || !fname || !name)
1334 {
1335 return maperror(EINVAL);
1336 }
1337 if (!NT_STATUS_IS_OK(cli_set_ea_path(cli, fname, name, value, size)))
1338 {
1339 return os2cli_errno(cli);
1340 }
1341 return 0;
1342}
1343
1344/*****************************************************
1345set EA for a file
1346*******************************************************/
1347int _System smbwrp_fsetea(cli_state * cli, smbwrp_file *file, char * name, unsigned char * value, int size)
1348{
1349 if (!cli || !file || !name)
1350 {
1351 return maperror(EINVAL);
1352 }
1353 if (!NT_STATUS_IS_OK(cli_set_ea_fnum(cli, file->fd, name, value, size)))
1354 {
1355 return os2cli_errno(cli);
1356 }
1357 return 0;
1358}
1359
1360
1361#pragma pack(1)
1362typedef struct _FEA /* fea */
1363{
1364 unsigned char fEA; /* flags */
1365 unsigned char cbName; /* name length not including NULL */
1366 unsigned short cbValue; /* value length */
1367} FEA;
1368
1369typedef struct _FEALIST /* feal */
1370{
1371 unsigned long cbList; /* total bytes of structure including full list */
1372 FEA list[1]; /* variable length FEA structures */
1373} FEALIST;
1374#pragma pack()
1375
1376static int unilistea(cli_state * cli, char *fname, void * buffer, unsigned long size)
1377{
1378 int fnum, i;
1379 int gotsize = sizeof(unsigned long);
1380 size_t num_eas;
1381 struct ea_struct *ea_list = NULL;
1382 TALLOC_CTX *mem_ctx;
1383 FEA * p;
1384 FEALIST * pfealist;
1385 char * q;
1386
1387 mem_ctx = talloc_init("%d: ealist", _gettid());
1388 pfealist = (FEALIST *)buffer;
1389 pfealist->cbList = 0;
1390
1391 if (!NT_STATUS_IS_OK(cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list)))
1392 {
1393 debuglocal(4,"ea_get_file list failed - %s\n", cli_errstr(cli));
1394 talloc_destroy(mem_ctx);
1395 return os2cli_errno(cli);
1396 }
1397
1398 debuglocal(4,"num_eas = %d\n", num_eas);
1399
1400 // we will count that os/2 max EA size for file is 64kb
1401 p = pfealist->list;
1402 for (i = 0; i < num_eas; i++)
1403 {
1404 int namelen = strlen(ea_list[i].name);
1405 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);
1406 if (namelen > 0xFF || ea_list[i].value.length > 0xFFFF)
1407 {
1408 debuglocal(4, "Skip EA <%s> with namelen %d, size %d\n", ea_list[i].name, namelen, ea_list[i].value.length);
1409 continue;
1410 }
1411 gotsize += sizeof(FEA) + namelen + ea_list[i].value.length + 1;
1412 if (size >= gotsize)
1413 {
1414 p->fEA = 0;
1415 p->cbName = namelen;
1416 p->cbValue = ea_list[i].value.length;
1417 q = (char *)(p + 1);
1418 strncpy(q, ea_list[i].name, namelen + 1);
1419 q += namelen + 1;
1420 memcpy(q, ea_list[i].value.data, ea_list[i].value.length);
1421 p = (FEA *)(q + ea_list[i].value.length);
1422 }
1423 }
1424 pfealist->cbList = gotsize;
1425 debuglocal(4,"ret size = %d\n", gotsize);
1426
1427 talloc_destroy(mem_ctx);
1428 return 0;
1429}
1430
1431/*****************************************************
1432lists EA of a path
1433*******************************************************/
1434int _System smbwrp_listea(cli_state * cli, char *fname, void * buffer, unsigned long size)
1435{
1436 if (!cli || !fname || !buffer)
1437 {
1438 return maperror(EINVAL);
1439 }
1440
1441 debuglocal(4,"EALIst for <%s>\n", fname);
1442 return unilistea(cli, fname, buffer, size);
1443}
1444
1445/*****************************************************
1446lists EA of a file
1447*******************************************************/
1448int _System smbwrp_flistea(cli_state * cli, smbwrp_file *file, void * buffer, unsigned long size)
1449{
1450 if (!cli || !file || !buffer)
1451 {
1452 return maperror(EINVAL);
1453 }
1454
1455 debuglocal(4,"FEALIst for <%s>\n", file->fname);
1456 return unilistea(cli, file->fname, buffer, size);
1457}
1458
1459/****************************************************************************
1460Check the space on a device.
1461****************************************************************************/
1462int _System smbwrp_dskattr(cli_state * cli, FSALLOCATE *pfsa)
1463{
1464 int total, bsize, avail;
1465
1466 if (!cli || !pfsa)
1467 {
1468 return maperror(EINVAL);
1469 }
1470
1471 if (!NT_STATUS_IS_OK(cli_dskattr(cli, &bsize, &total, &avail)))
1472 {
1473 debuglocal(4,"Error in dskattr: %s\n",cli_errstr(cli));
1474 return os2cli_errno(cli);
1475 }
1476
1477 debuglocal(4,"\n\t\t%d blocks of size %d. %d blocks available\n",
1478 total, bsize, avail);
1479
1480 // YD currently Samba return it in MB!
1481 pfsa->cSectorUnit = 1;
1482 if (bsize >= 65536)
1483 {
1484 pfsa->cUnit = total*1024;
1485 pfsa->cUnitAvail = avail*1024;
1486 pfsa->cbSector = bsize/1024;
1487 }
1488 else
1489 {
1490 pfsa->cUnit = total;
1491 pfsa->cUnitAvail = avail;
1492 pfsa->cbSector = bsize;
1493 }
1494
1495 return 0;
1496}
1497
1498/*****************************************************
1499Send an echo to the server to confirm it is still alive
1500*******************************************************/
1501int _System smbwrp_echo(cli_state * cli)
1502{
1503 debuglocal(4," smbwrp_echo\n");
1504 unsigned char garbage[16];
1505 NTSTATUS status;
1506 if (!cli)
1507 {
1508 return maperror(EINVAL);
1509 }
1510 /* Ping the server to keep the connection alive using SMBecho. */
1511 memset(garbage, 0xf0, sizeof(garbage));
1512 unsigned int old_timeout = cli->timeout;
1513 cli->timeout = 2000;// we don't want to wait 20 seconds
1514 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
1515 cli->timeout = old_timeout; // reset back to previous value
1516 if (NT_STATUS_IS_OK(status)) {
1517 return 0;
1518 } else {
1519 return -1;
1520 }
1521}
Note: See TracBrowser for help on using the repository browser.