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 | struct 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 | */
|
---|
41 | int 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 |
|
---|
52 | void 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 |
|
---|
74 | const char * smbwrp_getVersion()
|
---|
75 | {
|
---|
76 | return samba_version_string();
|
---|
77 | }
|
---|
78 |
|
---|
79 | int _System smbwrp_getclisize(void)
|
---|
80 | {
|
---|
81 | return sizeof(struct cli_state);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /*****************************************************
|
---|
85 | initialise structures
|
---|
86 | *******************************************************/
|
---|
87 | int _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 |
|
---|
127 | void 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 | /*****************************************************
|
---|
138 | remove redundent stuff from a filename
|
---|
139 | *******************************************************/
|
---|
140 | void 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 | /*****************************************************
|
---|
230 | return a connection to a server
|
---|
231 | loosely based on do_connect() from libsmb/clidfs.c
|
---|
232 | *******************************************************/
|
---|
233 | int _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 | #if 0 /* Causes connections to fail with Samba 4.x */
|
---|
393 | // YD ticket:58 we need to check resource type to avoid connecting to printers.
|
---|
394 | // dev type is set to IPC for IPC$, A: for everything else (printers use LPT1:)
|
---|
395 | if (!strcmp( share, "IPC$"))
|
---|
396 | dev_type = "IPC";
|
---|
397 | else
|
---|
398 | dev_type = "A:";
|
---|
399 |
|
---|
400 | if (!NT_STATUS_IS_OK(cli_tcon_andx(c, share, dev_type,
|
---|
401 | srv->password, strlen(srv->password)+1))) {
|
---|
402 | cli_shutdown(c);
|
---|
403 | // if loginerror is != 0 means normal login failed, but anonymous login worked
|
---|
404 | if (loginerror !=0)
|
---|
405 | return 6;
|
---|
406 | else
|
---|
407 | return 7;
|
---|
408 | }
|
---|
409 | #endif
|
---|
410 |
|
---|
411 | /* must be a normal share */
|
---|
412 |
|
---|
413 | status = cli_tree_connect(c, share, "?????",
|
---|
414 | srv->password, strlen(srv->password)+1);
|
---|
415 | if (!NT_STATUS_IS_OK(status)) {
|
---|
416 | debuglocal(4,"tree connect failed: %s\n", nt_errstr(status));
|
---|
417 | cli_shutdown(c);
|
---|
418 | return status;
|
---|
419 | }
|
---|
420 |
|
---|
421 | debuglocal(4," tconx ok.\n");
|
---|
422 |
|
---|
423 | // save cli_state pointer
|
---|
424 | *cli = c;
|
---|
425 |
|
---|
426 | return 0;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /*****************************************************
|
---|
430 | close a connection to a server
|
---|
431 | *******************************************************/
|
---|
432 | void _System smbwrp_disconnect( Resource* pRes, cli_state * cli)
|
---|
433 | {
|
---|
434 | if (pRes && cli)
|
---|
435 | {
|
---|
436 | // this call will free all buffers, close handles and free cli mem
|
---|
437 | cli_shutdown( cli);
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 |
|
---|
442 |
|
---|
443 | /*****************************************************
|
---|
444 | a wrapper for open()
|
---|
445 | *******************************************************/
|
---|
446 | int _System smbwrp_open(cli_state * cli, smbwrp_file * file)
|
---|
447 | {
|
---|
448 | uint16_t fd = 0;
|
---|
449 |
|
---|
450 | if (!cli || !file || !*file->fname)
|
---|
451 | {
|
---|
452 | return maperror(EINVAL);
|
---|
453 | }
|
---|
454 | if (file->denymode < DENY_ALL || file->denymode > DENY_NONE)
|
---|
455 | {
|
---|
456 | file->denymode = DENY_NONE;
|
---|
457 | }
|
---|
458 |
|
---|
459 | debuglocal(4,"cli_open(%s) attr %08x mode %02x denymode %02x\n", file->fname, file->openattr, file->openmode, file->denymode);
|
---|
460 | if (!NT_STATUS_IS_OK(cli_open(cli, file->fname, file->openmode, file->denymode, &fd)))
|
---|
461 | {
|
---|
462 | return os2cli_errno(cli);
|
---|
463 | }
|
---|
464 | file->fd = fd;
|
---|
465 | file->updatetime = 0;
|
---|
466 | file->offset = 0;
|
---|
467 | return 0;
|
---|
468 | }
|
---|
469 |
|
---|
470 | /*****************************************************
|
---|
471 | a wrapper for read()
|
---|
472 | *******************************************************/
|
---|
473 | int _System smbwrp_read(cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result)
|
---|
474 | {
|
---|
475 | int ret;
|
---|
476 |
|
---|
477 | if (!cli || !file || !buf || !result)
|
---|
478 | {
|
---|
479 | return maperror(EINVAL);
|
---|
480 | }
|
---|
481 | size_t nread;
|
---|
482 | *result = 0;
|
---|
483 | ret = cli_read(cli, file->fd, buf, file->offset, count, &nread);
|
---|
484 | if (ret == -1)
|
---|
485 | {
|
---|
486 | debuglocal(4," smbwrp_read - cli_read ret = %d\n",ret);
|
---|
487 | return os2cli_errno(cli);
|
---|
488 | }
|
---|
489 |
|
---|
490 | file->offset += nread;
|
---|
491 | *result = nread;
|
---|
492 | debuglocal(4," smbwrp_read successful, nread = %d, ret = %d\n",nread,ret);
|
---|
493 | return 0;
|
---|
494 | }
|
---|
495 |
|
---|
496 |
|
---|
497 |
|
---|
498 | /*****************************************************
|
---|
499 | a wrapper for write()
|
---|
500 | *******************************************************/
|
---|
501 | int _System smbwrp_write(cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result)
|
---|
502 | {
|
---|
503 | NTSTATUS status;
|
---|
504 | size_t ret;
|
---|
505 |
|
---|
506 | if (!cli || !file || !buf || !result)
|
---|
507 | {
|
---|
508 | return maperror(EINVAL);
|
---|
509 | }
|
---|
510 |
|
---|
511 | *result = 0;
|
---|
512 | //debuglocal(1,("Write %x %d %lld %d", cli, file->fd, file->offset, count));
|
---|
513 | status = cli_writeall(cli, file->fd, 0, buf, file->offset, count, &ret);
|
---|
514 | if (!NT_STATUS_IS_OK(status)) {
|
---|
515 | return os2cli_errno(cli);
|
---|
516 | }
|
---|
517 |
|
---|
518 | file->updatetime = 1;
|
---|
519 | file->offset += ret;
|
---|
520 | *result = ret;
|
---|
521 | return 0;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /*****************************************************
|
---|
525 | a wrapper for close()
|
---|
526 | *******************************************************/
|
---|
527 | int _System smbwrp_close(cli_state * cli, smbwrp_file * file)
|
---|
528 | {
|
---|
529 | int rc = 0;
|
---|
530 | if (!cli || !file)
|
---|
531 | {
|
---|
532 | return maperror(EINVAL);
|
---|
533 | }
|
---|
534 |
|
---|
535 | debuglocal(4,"smpwrp_close updatetime: %d\n", file->updatetime);
|
---|
536 |
|
---|
537 | if (file->updatetime == 1)
|
---|
538 | {
|
---|
539 | file->mtime = time(NULL);
|
---|
540 | debuglocal(4,"cli_close new mtime %lu\n", file->mtime);
|
---|
541 | }
|
---|
542 |
|
---|
543 | if (!NT_STATUS_IS_OK(cli_close(cli, file->fd)))
|
---|
544 | {
|
---|
545 | rc = os2cli_errno(cli);
|
---|
546 | }
|
---|
547 |
|
---|
548 | if (!rc && (file->openattr || file->mtime || file->ctime))
|
---|
549 | {
|
---|
550 | debuglocal(4,"Set pathinfo on close %s %08x %d %d\n", file->fname, file->openattr, file->mtime, file->ctime);
|
---|
551 | if (!NT_STATUS_IS_OK(cli_setpathinfo_basic(cli, file->fname, file->ctime, 0, file->mtime, 0, file->openattr)))
|
---|
552 | {
|
---|
553 | debuglocal(4,"Set pathinfo on close failed %d\n", os2cli_errno(cli));
|
---|
554 | //rc = os2cli_errno(cli);
|
---|
555 | }
|
---|
556 | }
|
---|
557 |
|
---|
558 | file->openattr = 0;
|
---|
559 | file->mtime = 0;
|
---|
560 | file->ctime = 0;
|
---|
561 | file->updatetime = 0;
|
---|
562 | file->fd = -1;
|
---|
563 | file->offset = 0;
|
---|
564 | *file->fname = 0;
|
---|
565 | return rc;
|
---|
566 | }
|
---|
567 |
|
---|
568 | int _System smbwrp_setfilesize(cli_state * cli, smbwrp_file * file, long long newsize)
|
---|
569 | {
|
---|
570 | int rc = 0;
|
---|
571 | if (!cli || !file)
|
---|
572 | {
|
---|
573 | return maperror(EINVAL);
|
---|
574 | }
|
---|
575 |
|
---|
576 | debuglocal(4,"smbwrp_setnewfilesize(%s) %lld\n", file->fname, newsize);
|
---|
577 | if (!NT_STATUS_IS_OK(cli_ftruncate(cli, file->fd, newsize)))
|
---|
578 | {
|
---|
579 | debuglocal(4,"smbwrp_setnewfilesize - cli_ftruncate errno = %d\n",cli_errno(cli));
|
---|
580 | #if 0 /* This is all sorts of bad - if cli_ftruncate fails, it creates a new file in it's place */
|
---|
581 | if (newsize)
|
---|
582 | {
|
---|
583 | rc = os2cli_errno(cli);
|
---|
584 | }
|
---|
585 |
|
---|
586 | if (!NT_STATUS_IS_OK(cli_close(cli, file->fd)))
|
---|
587 | {
|
---|
588 | return os2cli_errno(cli);
|
---|
589 | }
|
---|
590 | uint16_t fd = 0;
|
---|
591 | file->fd = -1;
|
---|
592 | file->offset = 0;
|
---|
593 | file->openmode &= ~(O_CREAT | O_EXCL);
|
---|
594 | file->openmode |= O_TRUNC;
|
---|
595 | debuglocal(4,"smbwrp_setnewfilesize : cli_open(%s) attr %08x mode %02x denymode %02x\n", file->fname, file->openattr, file->openmode, file->denymode);
|
---|
596 | if (!NT_STATUS_IS_OK(cli_open(cli, file->fname, file->openmode, file->denymode, &fd)))
|
---|
597 | {
|
---|
598 | return os2cli_errno(cli);
|
---|
599 | }
|
---|
600 | file->fd = fd;
|
---|
601 | #endif
|
---|
602 | }
|
---|
603 | return 0;
|
---|
604 | }
|
---|
605 |
|
---|
606 | /*****************************************************
|
---|
607 | a wrapper for rename()
|
---|
608 | *******************************************************/
|
---|
609 | int _System smbwrp_rename(cli_state * cli, char *oldname, char *newname)
|
---|
610 | {
|
---|
611 | if (!cli || !oldname || !newname)
|
---|
612 | {
|
---|
613 | return maperror(EINVAL);
|
---|
614 | }
|
---|
615 |
|
---|
616 | debuglocal(1,"Rename <%s> -> <%s>\n", oldname, newname);
|
---|
617 | if (!NT_STATUS_IS_OK(cli_rename(cli, oldname, newname)))
|
---|
618 | {
|
---|
619 | return os2cli_errno(cli);
|
---|
620 | }
|
---|
621 | return 0;
|
---|
622 | }
|
---|
623 |
|
---|
624 |
|
---|
625 | /*****************************************************
|
---|
626 | a wrapper for chmod()
|
---|
627 | *******************************************************/
|
---|
628 | int _System smbwrp_setattr(cli_state * cli, smbwrp_fileinfo *finfo)
|
---|
629 | {
|
---|
630 | if (!cli || !finfo || !*finfo->fname)
|
---|
631 | {
|
---|
632 | return maperror(EINVAL);
|
---|
633 | }
|
---|
634 |
|
---|
635 | debuglocal(4,"Setting on <%s> attr %04x, time %lu (timezone /%lu\n", finfo->fname, finfo->attr, finfo->mtime, finfo->mtime + get_time_zone(finfo->mtime));
|
---|
636 | // we already have gmt time, so no need to add timezone
|
---|
637 | // if (!cli_setatr(cli, finfo->fname, finfo->attr, finfo->mtime + (finfo->mtime == 0 ? 0 : get_time_zone(finfo->mtime)))
|
---|
638 | if (!NT_STATUS_IS_OK(cli_setatr(cli, finfo->fname, finfo->attr, finfo->mtime))
|
---|
639 | && !NT_STATUS_IS_OK(cli_setatr(cli, finfo->fname, finfo->attr, 0)))
|
---|
640 | {
|
---|
641 | return os2cli_errno(cli);
|
---|
642 | }
|
---|
643 | return 0;
|
---|
644 | }
|
---|
645 |
|
---|
646 | /*****************************************************
|
---|
647 | a wrapper for unlink()
|
---|
648 | *******************************************************/
|
---|
649 | int _System smbwrp_unlink(cli_state * cli, const char *fname)
|
---|
650 | {
|
---|
651 | if (!cli || !fname)
|
---|
652 | {
|
---|
653 | return maperror(EINVAL);
|
---|
654 | }
|
---|
655 | #if 0
|
---|
656 | if (strncmp(cli->dev, "LPT", 3) == 0)
|
---|
657 | {
|
---|
658 | int job = smbw_stat_printjob(cli, fname, NULL, NULL);
|
---|
659 | if (job == -1)
|
---|
660 | {
|
---|
661 | goto failed;
|
---|
662 | }
|
---|
663 | if (cli_printjob_del(cli, job) != 0)
|
---|
664 | {
|
---|
665 | goto failed;
|
---|
666 | }
|
---|
667 | } else
|
---|
668 | #endif
|
---|
669 | if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN)))
|
---|
670 | {
|
---|
671 | return os2cli_errno(cli);
|
---|
672 | }
|
---|
673 | return 0;
|
---|
674 | }
|
---|
675 |
|
---|
676 | /*****************************************************
|
---|
677 | a wrapper for lseek()
|
---|
678 | *******************************************************/
|
---|
679 | int _System smbwrp_lseek(cli_state * cli, smbwrp_file * file, int whence, long long offset)
|
---|
680 | {
|
---|
681 | off_t size;
|
---|
682 | if (!cli || !file)
|
---|
683 | {
|
---|
684 | return maperror(EINVAL);
|
---|
685 | }
|
---|
686 |
|
---|
687 | debuglocal(4,"lseek %d %lld %lld\n", whence, offset, file->offset);
|
---|
688 |
|
---|
689 | switch (whence) {
|
---|
690 | case SEEK_SET:
|
---|
691 | if (offset < 0)
|
---|
692 | {
|
---|
693 | return maperror(EINVAL);
|
---|
694 | }
|
---|
695 | file->offset = offset;
|
---|
696 | break;
|
---|
697 | case SEEK_CUR:
|
---|
698 | file->offset += offset;
|
---|
699 | break;
|
---|
700 | case SEEK_END:
|
---|
701 | if (offset > 0)
|
---|
702 | {
|
---|
703 | return maperror(EINVAL);
|
---|
704 | }
|
---|
705 | if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(cli, file->fd,
|
---|
706 | NULL, &size, NULL, NULL, NULL,
|
---|
707 | NULL, NULL)) &&
|
---|
708 | !NT_STATUS_IS_OK(cli_getattrE(cli, file->fd,
|
---|
709 | NULL, &size, NULL, NULL, NULL)))
|
---|
710 | {
|
---|
711 | return os2cli_errno(cli);
|
---|
712 | }
|
---|
713 | file->offset = size + offset;
|
---|
714 | break;
|
---|
715 | default: return maperror(EINVAL);
|
---|
716 | }
|
---|
717 |
|
---|
718 | return 0;
|
---|
719 | }
|
---|
720 |
|
---|
721 | /*****************************************************
|
---|
722 | try to do a QPATHINFO and if that fails then do a getatr
|
---|
723 | this is needed because win95 sometimes refuses the qpathinfo
|
---|
724 | loosely based on SMBC_getatr() from source3/libsmb/libsmb_file.c
|
---|
725 | *******************************************************/
|
---|
726 | int _System smbwrp_getattr(smbwrp_server *srv, cli_state * cli, smbwrp_fileinfo *finfo)
|
---|
727 | {
|
---|
728 | SMB_INO_T ino = 0;
|
---|
729 | struct timespec ctime;
|
---|
730 | struct timespec mtime;
|
---|
731 | struct timespec atime;
|
---|
732 |
|
---|
733 | if (!cli || !finfo || !*finfo->fname)
|
---|
734 | {
|
---|
735 | return maperror(EINVAL);
|
---|
736 | }
|
---|
737 | debuglocal(4,"getattr %d %d <%s>\n", smb1cli_conn_capabilities(cli->conn) & CAP_NOPATHINFO2, smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS, finfo->fname);
|
---|
738 |
|
---|
739 | if (NT_STATUS_IS_OK(cli_qpathinfo2(cli, finfo->fname, &ctime, &atime, &mtime, NULL,
|
---|
740 | (off_t *)&finfo->size, (unsigned short *)&finfo->attr, &ino)))
|
---|
741 | {
|
---|
742 | finfo->attr &= 0x7F;
|
---|
743 | finfo->ctime = convert_timespec_to_time_t(ctime);
|
---|
744 | finfo->atime = convert_timespec_to_time_t(atime);
|
---|
745 | finfo->mtime = convert_timespec_to_time_t(mtime);
|
---|
746 | return 0;
|
---|
747 | }
|
---|
748 |
|
---|
749 | #if 0 // cli->fd not available in Samba 4.x
|
---|
750 | if (cli->fd == -1)
|
---|
751 | {
|
---|
752 | /* fd == -1 means the connection is broken */
|
---|
753 | return maperror(ENOTCONN);
|
---|
754 | }
|
---|
755 | #endif
|
---|
756 |
|
---|
757 | debuglocal(4, "smbwrp_getattr, calling cli_qpathinfo3\n");
|
---|
758 | if (NT_STATUS_IS_OK(cli_qpathinfo3(cli, finfo->fname, &ctime, &atime, &mtime, NULL,
|
---|
759 | (off_t *)&finfo->size, (unsigned short *)&finfo->attr, &ino)))
|
---|
760 | {
|
---|
761 | finfo->attr &= 0x7F;
|
---|
762 | finfo->ctime = convert_timespec_to_time_t(ctime);
|
---|
763 | finfo->atime = convert_timespec_to_time_t(atime);
|
---|
764 | finfo->mtime = convert_timespec_to_time_t(mtime);
|
---|
765 | return 0;
|
---|
766 | }
|
---|
767 |
|
---|
768 | /* If the path is not on a share (it is a workgroup or a server),
|
---|
769 | * then cli_qpathinfo2 obviously fails. Return some fake information
|
---|
770 | * about the directory.
|
---|
771 | */
|
---|
772 | if ( *srv->server_name == 0
|
---|
773 | #if 0 /* Causes crashes with Samba 4.x */
|
---|
774 | || (strcmp(cli->dev,"IPC") == 0)
|
---|
775 | #endif
|
---|
776 | || *srv->share_name == 0
|
---|
777 | || (stricmp(srv->share_name,"IPC$") == 0)
|
---|
778 | #if 0 /* Causes crashes with Samba 4.x */
|
---|
779 | || (strncmp(cli->dev,"LPT",3) == 0)
|
---|
780 | #endif
|
---|
781 | )
|
---|
782 | {
|
---|
783 | debuglocal(4,"getattr not a share.\n");
|
---|
784 | *(time_t *)&finfo->ctime = time (NULL);
|
---|
785 | *(time_t *)&finfo->atime = time (NULL);
|
---|
786 | *(time_t *)&finfo->mtime = time (NULL);
|
---|
787 | finfo->size = 0;
|
---|
788 | finfo->easize = 0;
|
---|
789 | finfo->attr = aDIR;
|
---|
790 | return 0;
|
---|
791 | }
|
---|
792 |
|
---|
793 | /* if this is NT then don't bother with the getatr */
|
---|
794 | if (smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS/* && !(smb1cli_conn_capabilities(cli->conn) & CAP_NOPATHINFO2)*/)
|
---|
795 | {
|
---|
796 | int ret = cli_errno(cli);
|
---|
797 | // cli_qpathinfo* reports EINVAL when path of given file not exists
|
---|
798 | // thus there is no real situation when EINVAL should be returned to
|
---|
799 | // client at this point, we just replace it to ENOTDIR
|
---|
800 | if (ret == EINVAL)
|
---|
801 | {
|
---|
802 | ret = ENOTDIR;
|
---|
803 | }
|
---|
804 | return maperror(ret);
|
---|
805 | }
|
---|
806 |
|
---|
807 | if (NT_STATUS_IS_OK(cli_getatr(cli, finfo->fname, (unsigned short *)&finfo->attr, &finfo->size, (time_t *)&finfo->mtime)))
|
---|
808 | {
|
---|
809 | //debuglocal(2,("gotattr1 %08x <%s>\n", finfo->attr, finfo->fname));
|
---|
810 | finfo->mtime -= get_time_zone(finfo->mtime);
|
---|
811 | finfo->atime = finfo->atime; //was mtime
|
---|
812 | finfo->ctime = finfo->ctime; //was mtime
|
---|
813 | return 0;
|
---|
814 | }
|
---|
815 | return os2cli_errno(cli);
|
---|
816 | }
|
---|
817 |
|
---|
818 | /*****************************************************
|
---|
819 | try to do a QPATHINFO and if that fails then do a getatr
|
---|
820 | this is needed because win95 sometimes refuses the qpathinfo
|
---|
821 | *******************************************************/
|
---|
822 | int _System smbwrp_fgetattr(cli_state * cli, smbwrp_file *file, smbwrp_fileinfo *finfo)
|
---|
823 | {
|
---|
824 | struct timespec ctime;
|
---|
825 | struct timespec mtime;
|
---|
826 | struct timespec atime;
|
---|
827 | SMB_INO_T ino = 0;
|
---|
828 |
|
---|
829 | if (!cli || !file || !finfo)
|
---|
830 | {
|
---|
831 | return maperror(EINVAL);
|
---|
832 | }
|
---|
833 |
|
---|
834 | strncpy(finfo->fname, file->fname, sizeof(finfo->fname) - 1);
|
---|
835 | if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(cli, file->fd,
|
---|
836 | (unsigned short *)&finfo->attr, (off_t *)&finfo->size, &ctime, &atime, &mtime, NULL,
|
---|
837 | &ino)))
|
---|
838 | {
|
---|
839 | if (!NT_STATUS_IS_OK(cli_getattrE(cli, file->fd,
|
---|
840 | (unsigned short *)&finfo->attr, (&finfo->size), (time_t *)&finfo->ctime, (time_t *)&finfo->atime, (time_t *)&finfo->mtime)))
|
---|
841 | {
|
---|
842 | return os2cli_errno(cli);
|
---|
843 | }
|
---|
844 | else
|
---|
845 | {
|
---|
846 | finfo->ctime -= get_time_zone(finfo->ctime);
|
---|
847 | finfo->atime -= get_time_zone(finfo->atime);
|
---|
848 | finfo->mtime -= get_time_zone(finfo->mtime);
|
---|
849 | }
|
---|
850 | }
|
---|
851 | else
|
---|
852 | {
|
---|
853 | finfo->ctime = convert_timespec_to_time_t(ctime);
|
---|
854 | finfo->atime = convert_timespec_to_time_t(atime);
|
---|
855 | finfo->mtime = convert_timespec_to_time_t(mtime);
|
---|
856 | }
|
---|
857 |
|
---|
858 | return 0;
|
---|
859 | }
|
---|
860 |
|
---|
861 | // =============================DIRECTORY ROUTINES============================
|
---|
862 |
|
---|
863 | /*****************************************************
|
---|
864 | add a entry to a directory listing
|
---|
865 | *******************************************************/
|
---|
866 | static NTSTATUS smbwrp_dir_add(const char* mnt, smbwrp_fileinfo *finfo, const char *mask, void *state)
|
---|
867 | {
|
---|
868 | if (state && finfo)
|
---|
869 | {
|
---|
870 | filelist_state * st = (filelist_state *)state;
|
---|
871 | char fullname[ _MAX_PATH] = {0};
|
---|
872 | debuglocal(8,"adding <%s> %d %d %d\n", finfo->fname, sizeof(st->finfo), st->datalen, sizeof(st->finfo.fname));
|
---|
873 | memcpy(&st->finfo, finfo, sizeof(st->finfo));
|
---|
874 | strncpy(fullname, st->dir, strlen(st->dir));
|
---|
875 | strncat(fullname, finfo->fname, sizeof(fullname) - strlen(fullname) -1);
|
---|
876 | strncpy(st->finfo.fname, fullname, sizeof(st->finfo.fname));
|
---|
877 | getfindinfoL( st->pConn, st->plist, &st->finfo, st->ulAttribute, st->dir_mask);
|
---|
878 | }
|
---|
879 | }
|
---|
880 |
|
---|
881 | static void smbwrp_special_add(const char * name, void * state)
|
---|
882 | {
|
---|
883 | smbwrp_fileinfo finfo = {0};
|
---|
884 |
|
---|
885 | if (!name)
|
---|
886 | {
|
---|
887 | return;
|
---|
888 | }
|
---|
889 |
|
---|
890 | ZERO_STRUCT(finfo);
|
---|
891 |
|
---|
892 | strncpy(finfo.fname, name, sizeof(finfo.fname) - 1);
|
---|
893 | finfo.attr = aRONLY | aDIR;
|
---|
894 |
|
---|
895 | smbwrp_dir_add("", &finfo, NULL, state);
|
---|
896 | }
|
---|
897 |
|
---|
898 | static void smbwrp_printjob_add(struct print_job_info *job, void * state)
|
---|
899 | {
|
---|
900 | smbwrp_fileinfo finfo = {0};
|
---|
901 |
|
---|
902 | ZERO_STRUCT(finfo);
|
---|
903 |
|
---|
904 | //printf("Printjob <%s>\n", job->name);
|
---|
905 |
|
---|
906 | strncpy(finfo.fname, job->name, sizeof(finfo.fname) - 1);
|
---|
907 | finfo.mtime = job->t - get_time_zone(job->t);
|
---|
908 | finfo.atime = finfo.atime; //was mtime
|
---|
909 | finfo.ctime = finfo.ctime; //was mtime
|
---|
910 | finfo.attr = aRONLY;
|
---|
911 | finfo.size = job->size;
|
---|
912 |
|
---|
913 | smbwrp_dir_add("", &finfo, NULL, state);
|
---|
914 | }
|
---|
915 |
|
---|
916 | static void smbwrp_share_add(const char *share, uint32_t type,
|
---|
917 | const char *comment, void *state)
|
---|
918 | {
|
---|
919 | smbwrp_fileinfo finfo = {0};
|
---|
920 |
|
---|
921 | // strip administrative names and printers from list
|
---|
922 | if (type == STYPE_PRINTQ || strcmp(share,"IPC$") == 0) return;
|
---|
923 |
|
---|
924 | ZERO_STRUCT(finfo);
|
---|
925 |
|
---|
926 | strncpy(finfo.fname, share, sizeof(finfo.fname) - 1);
|
---|
927 | finfo.attr = aRONLY | aDIR;
|
---|
928 |
|
---|
929 | smbwrp_dir_add("", &finfo, NULL, state);
|
---|
930 | }
|
---|
931 |
|
---|
932 | /***************************************************************
|
---|
933 | Wrapper that allows SMB2 to list a directory.
|
---|
934 | Synchronous only.
|
---|
935 | Based on cli_smb2_list
|
---|
936 | ***************************************************************/
|
---|
937 |
|
---|
938 | NTSTATUS list_files_smb2(struct cli_state *cli,
|
---|
939 | const char *pathname,
|
---|
940 | uint16_t attribute,
|
---|
941 | NTSTATUS (*fn)(const char *,
|
---|
942 | struct smbwrp_fileinfo *,
|
---|
943 | const char *,
|
---|
944 | void *),
|
---|
945 | void *state)
|
---|
946 | {
|
---|
947 | NTSTATUS status;
|
---|
948 | uint16_t fnum = 0xffff;
|
---|
949 | char *parent_dir = NULL;
|
---|
950 | const char *mask = NULL;
|
---|
951 | struct smb2_hnd *ph = NULL;
|
---|
952 | bool processed_file = false;
|
---|
953 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
954 | TALLOC_CTX *subframe = NULL;
|
---|
955 | bool mask_has_wild;
|
---|
956 | void *dircachectx = NULL;
|
---|
957 | smbwrp_fileinfo wrpfinfo;
|
---|
958 |
|
---|
959 | if (smbXcli_conn_has_async_calls(cli->conn)) {
|
---|
960 | /*
|
---|
961 | * Can't use sync call while an async call is in flight
|
---|
962 | */
|
---|
963 | status = NT_STATUS_INVALID_PARAMETER;
|
---|
964 | goto fail;
|
---|
965 | }
|
---|
966 |
|
---|
967 | if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
|
---|
968 | status = NT_STATUS_INVALID_PARAMETER;
|
---|
969 | goto fail;
|
---|
970 | }
|
---|
971 |
|
---|
972 | /* Get the directory name. */
|
---|
973 | if (!windows_parent_dirname(frame,
|
---|
974 | pathname,
|
---|
975 | &parent_dir,
|
---|
976 | &mask)) {
|
---|
977 | status = NT_STATUS_NO_MEMORY;
|
---|
978 | goto fail;
|
---|
979 | }
|
---|
980 |
|
---|
981 | mask_has_wild = ms_has_wild(mask);
|
---|
982 |
|
---|
983 | status = cli_smb2_create_fnum(cli,
|
---|
984 | parent_dir,
|
---|
985 | 0, /* create_flags */
|
---|
986 | SEC_DIR_LIST|SEC_DIR_READ_ATTRIBUTE,/* desired_access */
|
---|
987 | FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
|
---|
988 | FILE_SHARE_READ|FILE_SHARE_WRITE, /* share_access */
|
---|
989 | FILE_OPEN, /* create_disposition */
|
---|
990 | FILE_DIRECTORY_FILE, /* create_options */
|
---|
991 | &fnum,
|
---|
992 | NULL);
|
---|
993 |
|
---|
994 | if (!NT_STATUS_IS_OK(status)) {
|
---|
995 | goto fail;
|
---|
996 | }
|
---|
997 |
|
---|
998 | status = map_fnum_to_smb2_handle(cli,
|
---|
999 | fnum,
|
---|
1000 | &ph);
|
---|
1001 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1002 | goto fail;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | do {
|
---|
1006 | uint8_t *dir_data = NULL;
|
---|
1007 | uint32_t dir_data_length = 0;
|
---|
1008 | uint32_t next_offset = 0;
|
---|
1009 | subframe = talloc_stackframe();
|
---|
1010 |
|
---|
1011 | status = smb2cli_query_directory(cli->conn,
|
---|
1012 | cli->timeout,
|
---|
1013 | cli->smb2.session,
|
---|
1014 | cli->smb2.tcon,
|
---|
1015 | SMB2_FIND_ID_BOTH_DIRECTORY_INFO,
|
---|
1016 | 0, /* flags */
|
---|
1017 | 0, /* file_index */
|
---|
1018 | ph->fid_persistent,
|
---|
1019 | ph->fid_volatile,
|
---|
1020 | mask,
|
---|
1021 | 0xffff,
|
---|
1022 | subframe,
|
---|
1023 | &dir_data,
|
---|
1024 | &dir_data_length);
|
---|
1025 |
|
---|
1026 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1027 | if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES)) {
|
---|
1028 | break;
|
---|
1029 | }
|
---|
1030 | goto fail;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | do {
|
---|
1034 | struct file_info *finfo = talloc_zero(subframe,
|
---|
1035 | struct file_info);
|
---|
1036 |
|
---|
1037 | if (finfo == NULL) {
|
---|
1038 | status = NT_STATUS_NO_MEMORY;
|
---|
1039 | goto fail;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | status = parse_finfo_id_both_directory_info(dir_data,
|
---|
1043 | dir_data_length,
|
---|
1044 | finfo,
|
---|
1045 | &next_offset);
|
---|
1046 |
|
---|
1047 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1048 | goto fail;
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | if (dir_check_ftype((uint32_t)finfo->mode,
|
---|
1052 | (uint32_t)attribute)) {
|
---|
1053 | /*
|
---|
1054 | * Only process if attributes match.
|
---|
1055 | * On SMB1 server does this, so on
|
---|
1056 | * SMB2 we need to emulate in the
|
---|
1057 | * client.
|
---|
1058 | *
|
---|
1059 | * https://bugzilla.samba.org/show_bug.cgi?id=10260
|
---|
1060 | */
|
---|
1061 | processed_file = true;
|
---|
1062 |
|
---|
1063 | //as samba and this client have different finfo, we need to convert
|
---|
1064 | memset(&wrpfinfo, 0, sizeof(wrpfinfo));
|
---|
1065 | wrpfinfo.size = finfo[0].size;
|
---|
1066 | wrpfinfo.attr = finfo[0].mode;
|
---|
1067 | wrpfinfo.ctime = convert_timespec_to_time_t(finfo[0].ctime_ts);
|
---|
1068 | wrpfinfo.mtime = convert_timespec_to_time_t(finfo[0].mtime_ts);
|
---|
1069 | wrpfinfo.atime = convert_timespec_to_time_t(finfo[0].atime_ts);
|
---|
1070 | wrpfinfo.easize = finfo[0].easize;
|
---|
1071 | strncpy(wrpfinfo.fname, finfo[0].name, sizeof(wrpfinfo.fname) -1);
|
---|
1072 |
|
---|
1073 | status = fn(cli->dfs_mountpoint,
|
---|
1074 | &wrpfinfo,
|
---|
1075 | pathname,
|
---|
1076 | state);
|
---|
1077 |
|
---|
1078 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1079 | /* not sure why this is required on OS/2 */
|
---|
1080 | if (status != NT_STATUS_WAIT_1)
|
---|
1081 | break;
|
---|
1082 | }
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | TALLOC_FREE(finfo);
|
---|
1086 |
|
---|
1087 | /* Move to next entry. */
|
---|
1088 | if (next_offset) {
|
---|
1089 | dir_data += next_offset;
|
---|
1090 | dir_data_length -= next_offset;
|
---|
1091 | }
|
---|
1092 | } while (next_offset != 0);
|
---|
1093 |
|
---|
1094 | TALLOC_FREE(subframe);
|
---|
1095 |
|
---|
1096 | if (!mask_has_wild) {
|
---|
1097 | /*
|
---|
1098 | * MacOSX 10 doesn't set STATUS_NO_MORE_FILES
|
---|
1099 | * when handed a non-wildcard path. Do it
|
---|
1100 | * for the server (with a non-wildcard path
|
---|
1101 | * there should only ever be one file returned.
|
---|
1102 | */
|
---|
1103 | status = STATUS_NO_MORE_FILES;
|
---|
1104 | break;
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | } while (NT_STATUS_IS_OK(status));
|
---|
1108 |
|
---|
1109 | if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES)) {
|
---|
1110 | status = NT_STATUS_OK;
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | if (NT_STATUS_IS_OK(status) && !processed_file) {
|
---|
1114 | /*
|
---|
1115 | * In SMB1 findfirst returns NT_STATUS_NO_SUCH_FILE
|
---|
1116 | * if no files match. Emulate this in the client.
|
---|
1117 | */
|
---|
1118 | status = NT_STATUS_NO_SUCH_FILE;
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | fail:
|
---|
1122 |
|
---|
1123 | if (fnum != 0xffff) {
|
---|
1124 | cli_smb2_close_fnum(cli, fnum);
|
---|
1125 | }
|
---|
1126 | TALLOC_FREE(subframe);
|
---|
1127 | TALLOC_FREE(frame);
|
---|
1128 | return status;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 |
|
---|
1132 | /****************************************************************************
|
---|
1133 | Do a directory listing, calling fn on each file found.
|
---|
1134 | Modified from cli_list
|
---|
1135 | ****************************************************************************/
|
---|
1136 | static int list_files(struct cli_state *cli, const char *mask, uint16_t attribute,
|
---|
1137 | void (*fn)(const char *, smbwrp_fileinfo *, const char *,
|
---|
1138 | void *), void *state)
|
---|
1139 | {
|
---|
1140 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
1141 | struct event_context *ev;
|
---|
1142 | struct tevent_req *req;
|
---|
1143 | NTSTATUS status = NT_STATUS_NO_MEMORY;
|
---|
1144 | struct file_info *finfo;
|
---|
1145 | size_t i, num_finfo;
|
---|
1146 | uint16_t info_level;
|
---|
1147 | void *dircachectx = NULL;
|
---|
1148 | smbwrp_fileinfo wrpfinfo;
|
---|
1149 |
|
---|
1150 | if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
|
---|
1151 | debuglocal(4,"SMB2 detected, calling list_files_smb2()\n");
|
---|
1152 | return list_files_smb2(cli, mask, attribute, fn, state);
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | /* Try to get the listing from cache. */
|
---|
1156 | if (dircache_list_files(fn, state, &num_finfo))
|
---|
1157 | {
|
---|
1158 | /* Got from cache. */
|
---|
1159 | return(num_finfo);
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | if (smbXcli_conn_has_async_calls(cli->conn)) {
|
---|
1163 | /*
|
---|
1164 | * Can't use sync call while an async call is in flight
|
---|
1165 | */
|
---|
1166 | status = NT_STATUS_INVALID_PARAMETER;
|
---|
1167 | goto fail;
|
---|
1168 | }
|
---|
1169 | ev = samba_tevent_context_init(frame);
|
---|
1170 | if (ev == NULL) {
|
---|
1171 | goto fail;
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | info_level = (smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS)
|
---|
1175 | ? SMB_FIND_FILE_BOTH_DIRECTORY_INFO : SMB_FIND_EA_SIZE;
|
---|
1176 |
|
---|
1177 | debuglocal(4,"list_files level %d. mask <%s>\n", info_level, mask);
|
---|
1178 |
|
---|
1179 | req = cli_list_send(frame, ev, cli, mask, attribute, info_level);
|
---|
1180 | if (req == NULL) {
|
---|
1181 | goto fail;
|
---|
1182 | }
|
---|
1183 | if (!tevent_req_poll(req, ev)) {
|
---|
1184 | status = map_nt_error_from_unix(errno);
|
---|
1185 | goto fail;
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | status = cli_list_recv(req, frame, &finfo, &num_finfo);
|
---|
1189 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1190 | goto fail;
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | dircachectx = dircache_write_begin(state, num_finfo);
|
---|
1194 |
|
---|
1195 | debuglocal(4,"list_files: got %d files\n", num_finfo);
|
---|
1196 |
|
---|
1197 |
|
---|
1198 | for (i=0; i<num_finfo; i++) {
|
---|
1199 | //as samba and this client have different finfo, we need to convert
|
---|
1200 | memset(&wrpfinfo, 0, sizeof(wrpfinfo));
|
---|
1201 | wrpfinfo.size = finfo[i].size;
|
---|
1202 | wrpfinfo.attr = finfo[i].mode;
|
---|
1203 | wrpfinfo.ctime = convert_timespec_to_time_t(finfo[i].ctime_ts);
|
---|
1204 | wrpfinfo.mtime = convert_timespec_to_time_t(finfo[i].mtime_ts);
|
---|
1205 | wrpfinfo.atime = convert_timespec_to_time_t(finfo[i].atime_ts);
|
---|
1206 | wrpfinfo.easize = finfo[i].easize;
|
---|
1207 | strncpy(wrpfinfo.fname, finfo[i].name, sizeof(wrpfinfo.fname) -1);
|
---|
1208 |
|
---|
1209 | fn(cli->dfs_mountpoint, &wrpfinfo, mask, state);
|
---|
1210 | // Also add the entry to the cache.
|
---|
1211 | dircache_write_entry(dircachectx, &wrpfinfo);
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | dircache_write_end(dircachectx);
|
---|
1215 | fail:
|
---|
1216 | TALLOC_FREE(frame);
|
---|
1217 | return num_finfo;
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | /*****************************************************
|
---|
1221 | open a directory on the server
|
---|
1222 | *******************************************************/
|
---|
1223 | int _System smbwrp_filelist(smbwrp_server *srv, cli_state * cli, filelist_state * state)
|
---|
1224 | {
|
---|
1225 | if (!srv || !cli || !state || !*state->mask)
|
---|
1226 | {
|
---|
1227 | return maperror(EINVAL);
|
---|
1228 | }
|
---|
1229 | 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);
|
---|
1230 | if (*srv->workgroup == 0 && *srv->server_name == 0)
|
---|
1231 | {
|
---|
1232 | smbwrp_special_add(".", state);
|
---|
1233 | smbwrp_special_add("..", state);
|
---|
1234 | cli_NetServerEnum(cli, srv->master, SV_TYPE_DOMAIN_ENUM,
|
---|
1235 | smbwrp_share_add, state);
|
---|
1236 | } else
|
---|
1237 | if (*srv->server_name == 0)
|
---|
1238 | {
|
---|
1239 | smbwrp_special_add(".", state);
|
---|
1240 | smbwrp_special_add("..", state);
|
---|
1241 |
|
---|
1242 | cli_NetServerEnum(cli, srv->workgroup, SV_TYPE_ALL,
|
---|
1243 | smbwrp_share_add, state);
|
---|
1244 | } else
|
---|
1245 | #if 0 /* Causes crashes with Samba 4.x */
|
---|
1246 | if ((strcmp(cli->dev,"IPC") == 0) || *srv->share_name == 0 || (stricmp(srv->share_name,"IPC$") == 0))
|
---|
1247 | #else
|
---|
1248 | if (/*(strcmp(cli->dev,"IPC") == 0) ||*/ *srv->share_name == 0 || (stricmp(srv->share_name,"IPC$") == 0))
|
---|
1249 | #endif
|
---|
1250 | {
|
---|
1251 | smbwrp_special_add(".", state);
|
---|
1252 | smbwrp_special_add("..", state);
|
---|
1253 |
|
---|
1254 | if (net_share_enum_rpc(cli, smbwrp_share_add, state) < 0 &&
|
---|
1255 | cli_RNetShareEnum(cli,smbwrp_share_add, state) < 0)
|
---|
1256 | {
|
---|
1257 | return os2cli_errno(cli);
|
---|
1258 | }
|
---|
1259 | } else
|
---|
1260 | #if 0 /* Causes crashes with Samba 4.x */
|
---|
1261 | if (strncmp(cli->dev,"LPT",3) == 0)
|
---|
1262 | {
|
---|
1263 | smbwrp_special_add(".", state);
|
---|
1264 | smbwrp_special_add("..", state);
|
---|
1265 | if (cli_print_queue_state(cli, smbwrp_printjob_add, state) < 0)
|
---|
1266 | {
|
---|
1267 | return os2cli_errno(cli);
|
---|
1268 | }
|
---|
1269 | }
|
---|
1270 | else
|
---|
1271 | #endif
|
---|
1272 | {
|
---|
1273 | if (list_files(cli, state->mask, aHIDDEN|aSYSTEM|aDIR,
|
---|
1274 | smbwrp_dir_add, state) < 0)
|
---|
1275 | {
|
---|
1276 | return os2cli_errno(cli);
|
---|
1277 | }
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | return 0;
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | /*****************************************************
|
---|
1284 | a wrapper for chdir()
|
---|
1285 | *******************************************************/
|
---|
1286 | int _System smbwrp_chdir(smbwrp_server *srv, cli_state * cli, char *fname)
|
---|
1287 | {
|
---|
1288 | unsigned short mode = aDIR;
|
---|
1289 | smbwrp_fileinfo finfo = {0};
|
---|
1290 | if (!cli || !fname)
|
---|
1291 | {
|
---|
1292 | return maperror(EINVAL);
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | strncpy(finfo.fname, fname, sizeof(finfo.fname) - 1);
|
---|
1296 | if (smbwrp_getattr(srv, cli, &finfo))
|
---|
1297 | {
|
---|
1298 | return os2cli_errno(cli);
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | if (!(finfo.attr & aDIR)) {
|
---|
1302 | return maperror(ENOTDIR);
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | return 0;
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 |
|
---|
1309 | /*****************************************************
|
---|
1310 | a wrapper for mkdir()
|
---|
1311 | *******************************************************/
|
---|
1312 | int _System smbwrp_mkdir(cli_state * cli, char *fname)
|
---|
1313 | {
|
---|
1314 | if (!cli || !fname)
|
---|
1315 | {
|
---|
1316 | return maperror(EINVAL);
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname)))
|
---|
1320 | {
|
---|
1321 | return os2cli_errno(cli);
|
---|
1322 | }
|
---|
1323 | return 0;
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | /*****************************************************
|
---|
1327 | a wrapper for rmdir()
|
---|
1328 | *******************************************************/
|
---|
1329 | int _System smbwrp_rmdir(cli_state * cli, char *fname)
|
---|
1330 | {
|
---|
1331 | if (!cli || !fname)
|
---|
1332 | {
|
---|
1333 | return maperror(EINVAL);
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 | if (!NT_STATUS_IS_OK(cli_rmdir(cli, fname)))
|
---|
1337 | {
|
---|
1338 | return os2cli_errno(cli);
|
---|
1339 | }
|
---|
1340 | return 0;
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 | /*****************************************************
|
---|
1344 | set EA for a path
|
---|
1345 | *******************************************************/
|
---|
1346 | int _System smbwrp_setea(cli_state * cli, char *fname, char * name, unsigned char * value, int size)
|
---|
1347 | {
|
---|
1348 | if (!cli || !fname || !name)
|
---|
1349 | {
|
---|
1350 | return maperror(EINVAL);
|
---|
1351 | }
|
---|
1352 | if (!NT_STATUS_IS_OK(cli_set_ea_path(cli, fname, name, value, size)))
|
---|
1353 | {
|
---|
1354 | return os2cli_errno(cli);
|
---|
1355 | }
|
---|
1356 | return 0;
|
---|
1357 | }
|
---|
1358 |
|
---|
1359 | /*****************************************************
|
---|
1360 | set EA for a file
|
---|
1361 | *******************************************************/
|
---|
1362 | int _System smbwrp_fsetea(cli_state * cli, smbwrp_file *file, char * name, unsigned char * value, int size)
|
---|
1363 | {
|
---|
1364 | if (!cli || !file || !name)
|
---|
1365 | {
|
---|
1366 | return maperror(EINVAL);
|
---|
1367 | }
|
---|
1368 | if (!NT_STATUS_IS_OK(cli_set_ea_fnum(cli, file->fd, name, value, size)))
|
---|
1369 | {
|
---|
1370 | return os2cli_errno(cli);
|
---|
1371 | }
|
---|
1372 | return 0;
|
---|
1373 | }
|
---|
1374 |
|
---|
1375 |
|
---|
1376 | #pragma pack(1)
|
---|
1377 | typedef struct _FEA /* fea */
|
---|
1378 | {
|
---|
1379 | unsigned char fEA; /* flags */
|
---|
1380 | unsigned char cbName; /* name length not including NULL */
|
---|
1381 | unsigned short cbValue; /* value length */
|
---|
1382 | } FEA;
|
---|
1383 |
|
---|
1384 | typedef struct _FEALIST /* feal */
|
---|
1385 | {
|
---|
1386 | unsigned long cbList; /* total bytes of structure including full list */
|
---|
1387 | FEA list[1]; /* variable length FEA structures */
|
---|
1388 | } FEALIST;
|
---|
1389 | #pragma pack()
|
---|
1390 |
|
---|
1391 | static int unilistea(cli_state * cli, char *fname, void * buffer, unsigned long size)
|
---|
1392 | {
|
---|
1393 | int fnum, i;
|
---|
1394 | int gotsize = sizeof(unsigned long);
|
---|
1395 | size_t num_eas;
|
---|
1396 | struct ea_struct *ea_list = NULL;
|
---|
1397 | TALLOC_CTX *mem_ctx;
|
---|
1398 | FEA * p;
|
---|
1399 | FEALIST * pfealist;
|
---|
1400 | char * q;
|
---|
1401 |
|
---|
1402 | mem_ctx = talloc_init("%d: ealist", _gettid());
|
---|
1403 | pfealist = (FEALIST *)buffer;
|
---|
1404 | pfealist->cbList = 0;
|
---|
1405 |
|
---|
1406 | if (!NT_STATUS_IS_OK(cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list)))
|
---|
1407 | {
|
---|
1408 | debuglocal(4,"ea_get_file list failed - %s\n", cli_errstr(cli));
|
---|
1409 | talloc_destroy(mem_ctx);
|
---|
1410 | return os2cli_errno(cli);
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | debuglocal(4,"num_eas = %d\n", num_eas);
|
---|
1414 |
|
---|
1415 | // we will count that os/2 max EA size for file is 64kb
|
---|
1416 | p = pfealist->list;
|
---|
1417 | for (i = 0; i < num_eas; i++)
|
---|
1418 | {
|
---|
1419 | int namelen = strlen(ea_list[i].name);
|
---|
1420 | 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);
|
---|
1421 | if (namelen > 0xFF || ea_list[i].value.length > 0xFFFF)
|
---|
1422 | {
|
---|
1423 | debuglocal(4, "Skip EA <%s> with namelen %d, size %d\n", ea_list[i].name, namelen, ea_list[i].value.length);
|
---|
1424 | continue;
|
---|
1425 | }
|
---|
1426 | gotsize += sizeof(FEA) + namelen + ea_list[i].value.length + 1;
|
---|
1427 | if (size >= gotsize)
|
---|
1428 | {
|
---|
1429 | p->fEA = 0;
|
---|
1430 | p->cbName = namelen;
|
---|
1431 | p->cbValue = ea_list[i].value.length;
|
---|
1432 | q = (char *)(p + 1);
|
---|
1433 | strncpy(q, ea_list[i].name, namelen + 1);
|
---|
1434 | q += namelen + 1;
|
---|
1435 | memcpy(q, ea_list[i].value.data, ea_list[i].value.length);
|
---|
1436 | p = (FEA *)(q + ea_list[i].value.length);
|
---|
1437 | }
|
---|
1438 | }
|
---|
1439 | pfealist->cbList = gotsize;
|
---|
1440 | debuglocal(4,"ret size = %d\n", gotsize);
|
---|
1441 |
|
---|
1442 | talloc_destroy(mem_ctx);
|
---|
1443 | return 0;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | /*****************************************************
|
---|
1447 | lists EA of a path
|
---|
1448 | *******************************************************/
|
---|
1449 | int _System smbwrp_listea(cli_state * cli, char *fname, void * buffer, unsigned long size)
|
---|
1450 | {
|
---|
1451 | if (!cli || !fname || !buffer)
|
---|
1452 | {
|
---|
1453 | return maperror(EINVAL);
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | debuglocal(4,"EALIst for <%s>\n", fname);
|
---|
1457 | return unilistea(cli, fname, buffer, size);
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | /*****************************************************
|
---|
1461 | lists EA of a file
|
---|
1462 | *******************************************************/
|
---|
1463 | int _System smbwrp_flistea(cli_state * cli, smbwrp_file *file, void * buffer, unsigned long size)
|
---|
1464 | {
|
---|
1465 | if (!cli || !file || !buffer)
|
---|
1466 | {
|
---|
1467 | return maperror(EINVAL);
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | debuglocal(4,"FEALIst for <%s>\n", file->fname);
|
---|
1471 | return unilistea(cli, file->fname, buffer, size);
|
---|
1472 | }
|
---|
1473 |
|
---|
1474 | /****************************************************************************
|
---|
1475 | Check the space on a device.
|
---|
1476 | ****************************************************************************/
|
---|
1477 | int _System smbwrp_dskattr(cli_state * cli, FSALLOCATE *pfsa)
|
---|
1478 | {
|
---|
1479 | uint64_t total, bsize, avail;
|
---|
1480 |
|
---|
1481 | if (!cli || !pfsa)
|
---|
1482 | {
|
---|
1483 | return maperror(EINVAL);
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | if (!NT_STATUS_IS_OK(cli_disk_size(cli, "", &bsize, &total, &avail)))
|
---|
1487 | {
|
---|
1488 | debuglocal(4,"Error in cli_disk_size: %s\n",cli_errstr(cli));
|
---|
1489 | return os2cli_errno(cli);
|
---|
1490 | }
|
---|
1491 |
|
---|
1492 | debuglocal(4,"\n\t\t%" PRIu64
|
---|
1493 | " blocks of size %" PRIu64
|
---|
1494 | ". %" PRIu64 " blocks available\n",
|
---|
1495 | total, bsize, avail);
|
---|
1496 |
|
---|
1497 | // YD currently Samba return it in MB!
|
---|
1498 | pfsa->cSectorUnit = 1;
|
---|
1499 | if (bsize >= 65536)
|
---|
1500 | {
|
---|
1501 | pfsa->cUnit = total*1024;
|
---|
1502 | pfsa->cUnitAvail = avail*1024;
|
---|
1503 | pfsa->cbSector = bsize/1024;
|
---|
1504 | }
|
---|
1505 | else
|
---|
1506 | {
|
---|
1507 | pfsa->cUnit = total;
|
---|
1508 | pfsa->cUnitAvail = avail;
|
---|
1509 | pfsa->cbSector = bsize;
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 | return 0;
|
---|
1513 | }
|
---|
1514 |
|
---|
1515 | /*****************************************************
|
---|
1516 | Send an echo to the server to confirm it is still alive
|
---|
1517 | *******************************************************/
|
---|
1518 | int _System smbwrp_echo(cli_state * cli)
|
---|
1519 | {
|
---|
1520 | debuglocal(4," smbwrp_echo\n");
|
---|
1521 | unsigned char garbage[16];
|
---|
1522 | NTSTATUS status;
|
---|
1523 | if (!cli)
|
---|
1524 | {
|
---|
1525 | return maperror(EINVAL);
|
---|
1526 | }
|
---|
1527 | /* Ping the server to keep the connection alive using SMBecho. */
|
---|
1528 | memset(garbage, 0xf0, sizeof(garbage));
|
---|
1529 | unsigned int old_timeout = cli->timeout;
|
---|
1530 | cli->timeout = 2000;// we don't want to wait 20 seconds
|
---|
1531 | status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
|
---|
1532 | cli->timeout = old_timeout; // reset back to previous value
|
---|
1533 | if (NT_STATUS_IS_OK(status)) {
|
---|
1534 | return 0;
|
---|
1535 | } else {
|
---|
1536 | return -1;
|
---|
1537 | }
|
---|
1538 | }
|
---|