source: branches/samba-3.3.x/source/libsmb/clirap.c@ 411

Last change on this file since 411 was 411, checked in by Herwig Bauernfeind, 15 years ago

Update Samba 3.3.x to 3.3.11

File size: 34.8 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 client RAP calls
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Gerald (Jerry) Carter 2004
6 Copyright (C) James Peach 2007
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23
24/****************************************************************************
25 Call a remote api on an arbitrary pipe. takes param, data and setup buffers.
26****************************************************************************/
27
28bool cli_api_pipe(struct cli_state *cli, const char *pipe_name,
29 uint16 *setup, uint32 setup_count, uint32 max_setup_count,
30 char *params, uint32 param_count, uint32 max_param_count,
31 char *data, uint32 data_count, uint32 max_data_count,
32 char **rparam, uint32 *rparam_count,
33 char **rdata, uint32 *rdata_count)
34{
35 cli_send_trans(cli, SMBtrans,
36 pipe_name,
37 0,0, /* fid, flags */
38 setup, setup_count, max_setup_count,
39 params, param_count, max_param_count,
40 data, data_count, max_data_count);
41
42 return (cli_receive_trans(cli, SMBtrans,
43 rparam, (unsigned int *)rparam_count,
44 rdata, (unsigned int *)rdata_count));
45}
46
47/****************************************************************************
48 Call a remote api
49****************************************************************************/
50
51bool cli_api(struct cli_state *cli,
52 char *param, int prcnt, int mprcnt,
53 char *data, int drcnt, int mdrcnt,
54 char **rparam, unsigned int *rprcnt,
55 char **rdata, unsigned int *rdrcnt)
56{
57 cli_send_trans(cli,SMBtrans,
58 PIPE_LANMAN, /* Name */
59 0,0, /* fid, flags */
60 NULL,0,0, /* Setup, length, max */
61 param, prcnt, mprcnt, /* Params, length, max */
62 data, drcnt, mdrcnt /* Data, length, max */
63 );
64
65 return (cli_receive_trans(cli,SMBtrans,
66 rparam, rprcnt,
67 rdata, rdrcnt));
68}
69
70/****************************************************************************
71 Perform a NetWkstaUserLogon.
72****************************************************************************/
73
74bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
75{
76 char *rparam = NULL;
77 char *rdata = NULL;
78 char *p;
79 unsigned int rdrcnt,rprcnt;
80 char param[1024];
81
82 memset(param, 0, sizeof(param));
83
84 /* send a SMBtrans command with api NetWkstaUserLogon */
85 p = param;
86 SSVAL(p,0,132); /* api number */
87 p += 2;
88 strlcpy(p,"OOWb54WrLh",sizeof(param)-PTR_DIFF(p,param));
89 p = skip_string(param,sizeof(param),p);
90 strlcpy(p,"WB21BWDWWDDDDDDDzzzD",sizeof(param)-PTR_DIFF(p,param));
91 p = skip_string(param,sizeof(param),p);
92 SSVAL(p,0,1);
93 p += 2;
94 strlcpy(p,user,sizeof(param)-PTR_DIFF(p,param));
95 strupper_m(p);
96 p += 21;
97 p++;
98 p += 15;
99 p++;
100 strlcpy(p, workstation,sizeof(param)-PTR_DIFF(p,param));
101 strupper_m(p);
102 p += 16;
103 SSVAL(p, 0, CLI_BUFFER_SIZE);
104 p += 2;
105 SSVAL(p, 0, CLI_BUFFER_SIZE);
106 p += 2;
107
108 if (cli_api(cli,
109 param, PTR_DIFF(p,param),1024, /* param, length, max */
110 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
111 &rparam, &rprcnt, /* return params, return size */
112 &rdata, &rdrcnt /* return data, return size */
113 )) {
114 cli->rap_error = rparam? SVAL(rparam,0) : -1;
115 p = rdata;
116
117 if (cli->rap_error == 0) {
118 DEBUG(4,("NetWkstaUserLogon success\n"));
119 cli->privileges = SVAL(p, 24);
120 /* The cli->eff_name field used to be set here
121 but it wasn't used anywhere else. */
122 } else {
123 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
124 }
125 }
126
127 SAFE_FREE(rparam);
128 SAFE_FREE(rdata);
129 return (cli->rap_error == 0);
130}
131
132/****************************************************************************
133 Call a NetShareEnum - try and browse available connections on a host.
134****************************************************************************/
135
136int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
137{
138 char *rparam = NULL;
139 char *rdata = NULL;
140 char *p;
141 unsigned int rdrcnt,rprcnt;
142 char param[1024];
143 int count = -1;
144
145 /* now send a SMBtrans command with api RNetShareEnum */
146 p = param;
147 SSVAL(p,0,0); /* api number */
148 p += 2;
149 strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
150 p = skip_string(param,sizeof(param),p);
151 strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
152 p = skip_string(param,sizeof(param),p);
153 SSVAL(p,0,1);
154 /*
155 * Win2k needs a *smaller* buffer than 0xFFFF here -
156 * it returns "out of server memory" with 0xFFFF !!! JRA.
157 */
158 SSVAL(p,2,0xFFE0);
159 p += 4;
160
161 if (cli_api(cli,
162 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
163 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
164 &rparam, &rprcnt, /* return params, length */
165 &rdata, &rdrcnt)) /* return data, length */
166 {
167 int res = rparam? SVAL(rparam,0) : -1;
168
169 if (res == 0 || res == ERRmoredata) {
170 int converter=SVAL(rparam,2);
171 int i;
172 char *rdata_end = rdata + rdrcnt;
173
174 count=SVAL(rparam,4);
175 p = rdata;
176
177 for (i=0;i<count;i++,p+=20) {
178 char *sname;
179 int type;
180 int comment_offset;
181 const char *cmnt;
182 const char *p1;
183 char *s1, *s2;
184 size_t len;
185 TALLOC_CTX *frame = talloc_stackframe();
186
187 if (p + 20 > rdata_end) {
188 TALLOC_FREE(frame);
189 break;
190 }
191
192 sname = p;
193 type = SVAL(p,14);
194 comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
195 if (comment_offset < 0 ||
196 comment_offset > (int)rdrcnt) {
197 TALLOC_FREE(frame);
198 break;
199 }
200 cmnt = comment_offset?(rdata+comment_offset):"";
201
202 /* Work out the comment length. */
203 for (p1 = cmnt, len = 0; *p1 &&
204 p1 < rdata_end; len++)
205 p1++;
206 if (!*p1) {
207 len++;
208 }
209 pull_string_talloc(frame,rdata,0,
210 &s1,sname,14,STR_ASCII);
211 pull_string_talloc(frame,rdata,0,
212 &s2,cmnt,len,STR_ASCII);
213 if (!s1 || !s2) {
214 TALLOC_FREE(frame);
215 continue;
216 }
217
218 fn(s1, type, s2, state);
219
220 TALLOC_FREE(frame);
221 }
222 } else {
223 DEBUG(4,("NetShareEnum res=%d\n", res));
224 }
225 } else {
226 DEBUG(4,("NetShareEnum failed\n"));
227 }
228
229 SAFE_FREE(rparam);
230 SAFE_FREE(rdata);
231
232 return count;
233}
234
235/****************************************************************************
236 Call a NetServerEnum for the specified workgroup and servertype mask. This
237 function then calls the specified callback function for each name returned.
238
239 The callback function takes 4 arguments: the machine name, the server type,
240 the comment and a state pointer.
241****************************************************************************/
242
243bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
244 void (*fn)(const char *, uint32, const char *, void *),
245 void *state)
246{
247 char *rparam = NULL;
248 char *rdata = NULL;
249 char *rdata_end = NULL;
250 unsigned int rdrcnt,rprcnt;
251 char *p;
252 char param[1024];
253 int uLevel = 1;
254 size_t len;
255 uint32 func = RAP_NetServerEnum2;
256 char *last_entry = NULL;
257 int total_cnt = 0;
258 int return_cnt = 0;
259 int res;
260
261 errno = 0; /* reset */
262
263 /*
264 * This may take more than one transaction, so we should loop until
265 * we no longer get a more data to process or we have all of the
266 * items.
267 */
268 do {
269 /* send a SMBtrans command with api NetServerEnum */
270 p = param;
271 SIVAL(p,0,func); /* api number */
272 p += 2;
273
274 if (func == RAP_NetServerEnum3) {
275 strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
276 } else {
277 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
278 }
279
280 p = skip_string(param, sizeof(param), p);
281 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
282
283 p = skip_string(param, sizeof(param), p);
284 SSVAL(p,0,uLevel);
285 SSVAL(p,2,CLI_BUFFER_SIZE);
286 p += 4;
287 SIVAL(p,0,stype);
288 p += 4;
289
290 /* If we have more data, tell the server where
291 * to continue from.
292 */
293 len = push_ascii(p,
294 workgroup,
295 sizeof(param) - PTR_DIFF(p,param) - 1,
296 STR_TERMINATE|STR_UPPER);
297
298 if (len == (size_t)-1) {
299 SAFE_FREE(last_entry);
300 return false;
301 }
302 p += len;
303
304 if (func == RAP_NetServerEnum3) {
305 len = push_ascii(p,
306 last_entry ? last_entry : "",
307 sizeof(param) - PTR_DIFF(p,param) - 1,
308 STR_TERMINATE);
309
310 if (len == (size_t)-1) {
311 SAFE_FREE(last_entry);
312 return false;
313 }
314 p += len;
315 }
316
317 /* Next time through we need to use the continue api */
318 func = RAP_NetServerEnum3;
319
320 if (!cli_api(cli,
321 param, PTR_DIFF(p,param), 8, /* params, length, max */
322 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
323 &rparam, &rprcnt, /* return params, return size */
324 &rdata, &rdrcnt)) { /* return data, return size */
325
326 /* break out of the loop on error */
327 res = -1;
328 break;
329 }
330
331 rdata_end = rdata + rdrcnt;
332 res = rparam ? SVAL(rparam,0) : -1;
333
334 if (res == 0 || res == ERRmoredata ||
335 (res != -1 && cli_errno(cli) == 0)) {
336 char *sname = NULL;
337 int i, count;
338 int converter=SVAL(rparam,2);
339
340 /* Get the number of items returned in this buffer */
341 count = SVAL(rparam, 4);
342
343 /* The next field contains the number of items left,
344 * including those returned in this buffer. So the
345 * first time through this should contain all of the
346 * entries.
347 */
348 if (total_cnt == 0) {
349 total_cnt = SVAL(rparam, 6);
350 }
351
352 /* Keep track of how many we have read */
353 return_cnt += count;
354 p = rdata;
355
356 /* The last name in the previous NetServerEnum reply is
357 * sent back to server in the NetServerEnum3 request
358 * (last_entry). The next reply should repeat this entry
359 * as the first element. We have no proof that this is
360 * always true, but from traces that seems to be the
361 * behavior from Window Servers. So first lets do a lot
362 * of checking, just being paranoid. If the string
363 * matches then we already saw this entry so skip it.
364 *
365 * NOTE: sv1_name field must be null terminated and has
366 * a max size of 16 (NetBIOS Name).
367 */
368 if (last_entry && count && p &&
369 (strncmp(last_entry, p, 16) == 0)) {
370 count -= 1; /* Skip this entry */
371 return_cnt = -1; /* Not part of total, so don't count. */
372 p = rdata + 26; /* Skip the whole record */
373 }
374
375 for (i = 0; i < count; i++, p += 26) {
376 int comment_offset;
377 const char *cmnt;
378 const char *p1;
379 char *s1, *s2;
380 TALLOC_CTX *frame = talloc_stackframe();
381 uint32_t entry_stype;
382
383 if (p + 26 > rdata_end) {
384 TALLOC_FREE(frame);
385 break;
386 }
387
388 sname = p;
389 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
390 cmnt = comment_offset?(rdata+comment_offset):"";
391
392 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
393 TALLOC_FREE(frame);
394 continue;
395 }
396
397 /* Work out the comment length. */
398 for (p1 = cmnt, len = 0; *p1 &&
399 p1 < rdata_end; len++)
400 p1++;
401 if (!*p1) {
402 len++;
403 }
404
405 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
406
407 pull_string_talloc(frame,rdata,0,
408 &s1,sname,16,STR_ASCII);
409 pull_string_talloc(frame,rdata,0,
410 &s2,cmnt,len,STR_ASCII);
411
412 if (!s1 || !s2) {
413 TALLOC_FREE(frame);
414 continue;
415 }
416
417 fn(s1, entry_stype, s2, state);
418 TALLOC_FREE(frame);
419 }
420
421 /* We are done with the old last entry, so now we can free it */
422 if (last_entry) {
423 SAFE_FREE(last_entry); /* This will set it to null */
424 }
425
426 /* We always make a copy of the last entry if we have one */
427 if (sname) {
428 last_entry = smb_xstrdup(sname);
429 }
430
431 /* If we have more data, but no last entry then error out */
432 if (!last_entry && (res == ERRmoredata)) {
433 errno = EINVAL;
434 res = 0;
435 }
436
437 }
438
439 SAFE_FREE(rparam);
440 SAFE_FREE(rdata);
441 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
442
443 SAFE_FREE(rparam);
444 SAFE_FREE(rdata);
445 SAFE_FREE(last_entry);
446
447 if (res == -1) {
448 errno = cli_errno(cli);
449 } else {
450 if (!return_cnt) {
451 /* this is a very special case, when the domain master for the
452 work group isn't part of the work group itself, there is something
453 wild going on */
454 errno = ENOENT;
455 }
456 }
457
458 return(return_cnt > 0);
459}
460
461/****************************************************************************
462 Send a SamOEMChangePassword command.
463****************************************************************************/
464
465bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
466 const char *old_password)
467{
468 char param[1024];
469 unsigned char data[532];
470 char *p = param;
471 unsigned char old_pw_hash[16];
472 unsigned char new_pw_hash[16];
473 unsigned int data_len;
474 unsigned int param_len = 0;
475 char *rparam = NULL;
476 char *rdata = NULL;
477 unsigned int rprcnt, rdrcnt;
478
479 if (strlen(user) >= sizeof(fstring)-1) {
480 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
481 return False;
482 }
483
484 SSVAL(p,0,214); /* SamOEMChangePassword command. */
485 p += 2;
486 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
487 p = skip_string(param,sizeof(param),p);
488 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
489 p = skip_string(param,sizeof(param),p);
490 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
491 p = skip_string(param,sizeof(param),p);
492 SSVAL(p,0,532);
493 p += 2;
494
495 param_len = PTR_DIFF(p,param);
496
497 /*
498 * Get the Lanman hash of the old password, we
499 * use this as the key to make_oem_passwd_hash().
500 */
501 E_deshash(old_password, old_pw_hash);
502
503 encode_pw_buffer(data, new_password, STR_ASCII);
504
505#ifdef DEBUG_PASSWORD
506 DEBUG(100,("make_oem_passwd_hash\n"));
507 dump_data(100, data, 516);
508#endif
509 SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
510
511 /*
512 * Now place the old password hash in the data.
513 */
514 E_deshash(new_password, new_pw_hash);
515
516 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
517
518 data_len = 532;
519
520 if (cli_send_trans(cli,SMBtrans,
521 PIPE_LANMAN, /* name */
522 0,0, /* fid, flags */
523 NULL,0,0, /* setup, length, max */
524 param,param_len,2, /* param, length, max */
525 (char *)data,data_len,0 /* data, length, max */
526 ) == False) {
527 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
528 user ));
529 return False;
530 }
531
532 if (!cli_receive_trans(cli,SMBtrans,
533 &rparam, &rprcnt,
534 &rdata, &rdrcnt)) {
535 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
536 user ));
537 return False;
538 }
539
540 if (rparam) {
541 cli->rap_error = SVAL(rparam,0);
542 }
543
544 SAFE_FREE(rparam);
545 SAFE_FREE(rdata);
546
547 return (cli->rap_error == 0);
548}
549
550/****************************************************************************
551 Send a qpathinfo call.
552****************************************************************************/
553
554bool cli_qpathinfo(struct cli_state *cli,
555 const char *fname,
556 time_t *change_time,
557 time_t *access_time,
558 time_t *write_time,
559 SMB_OFF_T *size,
560 uint16 *mode)
561{
562 unsigned int data_len = 0;
563 unsigned int param_len = 0;
564 unsigned int rparam_len, rdata_len;
565 uint16 setup = TRANSACT2_QPATHINFO;
566 char *param;
567 char *rparam=NULL, *rdata=NULL;
568 int count=8;
569 bool ret;
570 time_t (*date_fn)(struct cli_state *, const void *);
571 char *p;
572 size_t nlen = 2*(strlen(fname)+1);
573
574 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
575 if (!param) {
576 return false;
577 }
578 p = param;
579 memset(p, '\0', 6);
580 SSVAL(p, 0, SMB_INFO_STANDARD);
581 p += 6;
582 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
583 param_len = PTR_DIFF(p, param);
584
585 do {
586 ret = (cli_send_trans(cli, SMBtrans2,
587 NULL, /* Name */
588 -1, 0, /* fid, flags */
589 &setup, 1, 0, /* setup, length, max */
590 param, param_len, 10, /* param, length, max */
591 NULL, data_len, cli->max_xmit /* data, length, max */
592 ) &&
593 cli_receive_trans(cli, SMBtrans2,
594 &rparam, &rparam_len,
595 &rdata, &rdata_len));
596 if (!cli_is_dos_error(cli)) break;
597 if (!ret) {
598 /* we need to work around a Win95 bug - sometimes
599 it gives ERRSRV/ERRerror temprarily */
600 uint8 eclass;
601 uint32 ecode;
602 cli_dos_error(cli, &eclass, &ecode);
603 if (eclass != ERRSRV || ecode != ERRerror) break;
604 smb_msleep(100);
605 }
606 } while (count-- && ret==False);
607
608 SAFE_FREE(param);
609 if (!ret || !rdata || rdata_len < 22) {
610 return False;
611 }
612
613 if (cli->win95) {
614 date_fn = cli_make_unix_date;
615 } else {
616 date_fn = cli_make_unix_date2;
617 }
618
619 if (change_time) {
620 *change_time = date_fn(cli, rdata+0);
621 }
622 if (access_time) {
623 *access_time = date_fn(cli, rdata+4);
624 }
625 if (write_time) {
626 *write_time = date_fn(cli, rdata+8);
627 }
628 if (size) {
629 *size = IVAL(rdata, 12);
630 }
631 if (mode) {
632 *mode = SVAL(rdata,l1_attrFile);
633 }
634
635 SAFE_FREE(rdata);
636 SAFE_FREE(rparam);
637 return True;
638}
639
640/****************************************************************************
641 Send a setpathinfo call.
642****************************************************************************/
643
644bool cli_setpathinfo(struct cli_state *cli, const char *fname,
645 time_t create_time,
646 time_t access_time,
647 time_t write_time,
648 time_t change_time,
649 uint16 mode)
650{
651 unsigned int data_len = 0;
652 unsigned int param_len = 0;
653 unsigned int rparam_len, rdata_len;
654 uint16 setup = TRANSACT2_SETPATHINFO;
655 char *param;
656 char data[40];
657 char *rparam=NULL, *rdata=NULL;
658 int count=8;
659 bool ret;
660 char *p;
661 size_t nlen = 2*(strlen(fname)+1);
662
663 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
664 if (!param) {
665 return false;
666 }
667 memset(param, '\0', 6);
668 memset(data, 0, sizeof(data));
669
670 p = param;
671
672 /* Add the information level */
673 SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
674
675 /* Skip reserved */
676 p += 6;
677
678 /* Add the file name */
679 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
680
681 param_len = PTR_DIFF(p, param);
682
683 p = data;
684
685 /*
686 * Add the create, last access, modification, and status change times
687 */
688 put_long_date(p, create_time);
689 p += 8;
690
691 put_long_date(p, access_time);
692 p += 8;
693
694 put_long_date(p, write_time);
695 p += 8;
696
697 put_long_date(p, change_time);
698 p += 8;
699
700 /* Add attributes */
701 SIVAL(p, 0, mode);
702 p += 4;
703
704 /* Add padding */
705 SIVAL(p, 0, 0);
706 p += 4;
707
708 data_len = PTR_DIFF(p, data);
709
710 do {
711 ret = (cli_send_trans(cli, SMBtrans2,
712 NULL, /* Name */
713 -1, 0, /* fid, flags */
714 &setup, 1, 0, /* setup, length, max */
715 param, param_len, 10, /* param, length, max */
716 data, data_len, cli->max_xmit /* data, length, max */
717 ) &&
718 cli_receive_trans(cli, SMBtrans2,
719 &rparam, &rparam_len,
720 &rdata, &rdata_len));
721 if (!cli_is_dos_error(cli)) break;
722 if (!ret) {
723 /* we need to work around a Win95 bug - sometimes
724 it gives ERRSRV/ERRerror temprarily */
725 uint8 eclass;
726 uint32 ecode;
727 cli_dos_error(cli, &eclass, &ecode);
728 if (eclass != ERRSRV || ecode != ERRerror) break;
729 smb_msleep(100);
730 }
731 } while (count-- && ret==False);
732
733 SAFE_FREE(param);
734 if (!ret) {
735 return False;
736 }
737
738 SAFE_FREE(rdata);
739 SAFE_FREE(rparam);
740 return True;
741}
742
743/****************************************************************************
744 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
745****************************************************************************/
746
747bool cli_qpathinfo2(struct cli_state *cli, const char *fname,
748 struct timespec *create_time,
749 struct timespec *access_time,
750 struct timespec *write_time,
751 struct timespec *change_time,
752 SMB_OFF_T *size, uint16 *mode,
753 SMB_INO_T *ino)
754{
755 unsigned int data_len = 0;
756 unsigned int param_len = 0;
757 uint16 setup = TRANSACT2_QPATHINFO;
758 char *param;
759 char *rparam=NULL, *rdata=NULL;
760 char *p;
761 size_t nlen = 2*(strlen(fname)+1);
762
763 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
764 if (!param) {
765 return false;
766 }
767 p = param;
768 memset(param, '\0', 6);
769 SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
770 p += 6;
771 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
772
773 param_len = PTR_DIFF(p, param);
774
775 if (!cli_send_trans(cli, SMBtrans2,
776 NULL, /* name */
777 -1, 0, /* fid, flags */
778 &setup, 1, 0, /* setup, length, max */
779 param, param_len, 10, /* param, length, max */
780 NULL, data_len, cli->max_xmit /* data, length, max */
781 )) {
782 SAFE_FREE(param);
783 return False;
784 }
785
786 SAFE_FREE(param);
787 if (!cli_receive_trans(cli, SMBtrans2,
788 &rparam, &param_len,
789 &rdata, &data_len)) {
790 return False;
791 }
792
793 if (!rdata || data_len < 22) {
794 return False;
795 }
796
797 if (create_time) {
798 *create_time = interpret_long_date(rdata+0);
799 }
800 if (access_time) {
801 *access_time = interpret_long_date(rdata+8);
802 }
803 if (write_time) {
804 *write_time = interpret_long_date(rdata+16);
805 }
806 if (change_time) {
807 *change_time = interpret_long_date(rdata+24);
808 }
809 if (mode) {
810 *mode = SVAL(rdata, 32);
811 }
812 if (size) {
813 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
814 }
815 if (ino) {
816 *ino = IVAL(rdata, 64);
817 }
818
819 SAFE_FREE(rdata);
820 SAFE_FREE(rparam);
821 return True;
822}
823
824/****************************************************************************
825 Get the stream info
826****************************************************************************/
827
828bool cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
829 TALLOC_CTX *mem_ctx,
830 unsigned int *pnum_streams,
831 struct stream_struct **pstreams)
832{
833 unsigned int data_len = 0;
834 unsigned int param_len = 0;
835 uint16 setup = TRANSACT2_QPATHINFO;
836 char *param;
837 char *rparam=NULL, *rdata=NULL;
838 char *p;
839 unsigned int num_streams;
840 struct stream_struct *streams;
841 unsigned int ofs;
842 size_t namelen = 2*(strlen(fname)+1);
843
844 param = SMB_MALLOC_ARRAY(char, 6+namelen+2);
845 if (param == NULL) {
846 return false;
847 }
848 p = param;
849 memset(p, 0, 6);
850 SSVAL(p, 0, SMB_FILE_STREAM_INFORMATION);
851 p += 6;
852 p += clistr_push(cli, p, fname, namelen, STR_TERMINATE);
853
854 param_len = PTR_DIFF(p, param);
855
856 if (!cli_send_trans(cli, SMBtrans2,
857 NULL, /* name */
858 -1, 0, /* fid, flags */
859 &setup, 1, 0, /* setup, len, max */
860 param, param_len, 10, /* param, len, max */
861 NULL, data_len, cli->max_xmit /* data, len, max */
862 )) {
863 return false;
864 }
865
866 if (!cli_receive_trans(cli, SMBtrans2,
867 &rparam, &param_len,
868 &rdata, &data_len)) {
869 return false;
870 }
871
872 if (!rdata) {
873 SAFE_FREE(rparam);
874 return false;
875 }
876
877 num_streams = 0;
878 streams = NULL;
879 ofs = 0;
880
881 while ((data_len > ofs) && (data_len - ofs >= 24)) {
882 uint32_t nlen, len;
883 size_t size;
884 void *vstr;
885 struct stream_struct *tmp;
886 uint8_t *tmp_buf;
887
888 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
889 struct stream_struct,
890 num_streams+1);
891
892 if (tmp == NULL) {
893 goto fail;
894 }
895 streams = tmp;
896
897 nlen = IVAL(rdata, ofs + 0x04);
898
899 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
900 rdata, ofs + 0x08);
901 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
902 rdata, ofs + 0x10);
903
904 if (nlen > data_len - (ofs + 24)) {
905 goto fail;
906 }
907
908 /*
909 * We need to null-terminate src, how do I do this with
910 * convert_string_talloc??
911 */
912
913 tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
914 if (tmp_buf == NULL) {
915 goto fail;
916 }
917
918 memcpy(tmp_buf, rdata+ofs+24, nlen);
919 tmp_buf[nlen] = 0;
920 tmp_buf[nlen+1] = 0;
921
922 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
923 nlen+2, &vstr, &size, false))
924 {
925 TALLOC_FREE(tmp_buf);
926 goto fail;
927 }
928
929 TALLOC_FREE(tmp_buf);
930 streams[num_streams].name = (char *)vstr;
931 num_streams++;
932
933 len = IVAL(rdata, ofs);
934 if (len > data_len - ofs) {
935 goto fail;
936 }
937 if (len == 0) break;
938 ofs += len;
939 }
940
941 SAFE_FREE(rdata);
942 SAFE_FREE(rparam);
943
944 *pnum_streams = num_streams;
945 *pstreams = streams;
946 return true;
947
948 fail:
949 TALLOC_FREE(streams);
950 SAFE_FREE(rdata);
951 SAFE_FREE(rparam);
952 return false;
953}
954
955/****************************************************************************
956 Send a qfileinfo QUERY_FILE_NAME_INFO call.
957****************************************************************************/
958
959bool cli_qfilename(struct cli_state *cli, int fnum, char *name, size_t namelen)
960{
961 unsigned int data_len = 0;
962 unsigned int param_len = 0;
963 uint16 setup = TRANSACT2_QFILEINFO;
964 char param[4];
965 char *rparam=NULL, *rdata=NULL;
966
967 param_len = 4;
968 SSVAL(param, 0, fnum);
969 SSVAL(param, 2, SMB_QUERY_FILE_NAME_INFO);
970
971 if (!cli_send_trans(cli, SMBtrans2,
972 NULL, /* name */
973 -1, 0, /* fid, flags */
974 &setup, 1, 0, /* setup, length, max */
975 param, param_len, 2, /* param, length, max */
976 NULL, data_len, cli->max_xmit /* data, length, max */
977 )) {
978 return False;
979 }
980
981 if (!cli_receive_trans(cli, SMBtrans2,
982 &rparam, &param_len,
983 &rdata, &data_len)) {
984 return False;
985 }
986
987 if (!rdata || data_len < 4) {
988 return False;
989 }
990
991 clistr_pull(cli, name, rdata+4, namelen, IVAL(rdata, 0), STR_UNICODE);
992
993 return True;
994}
995
996/****************************************************************************
997 Send a qfileinfo call.
998****************************************************************************/
999
1000bool cli_qfileinfo(struct cli_state *cli, int fnum,
1001 uint16 *mode, SMB_OFF_T *size,
1002 struct timespec *create_time,
1003 struct timespec *access_time,
1004 struct timespec *write_time,
1005 struct timespec *change_time,
1006 SMB_INO_T *ino)
1007{
1008 unsigned int data_len = 0;
1009 unsigned int param_len = 0;
1010 uint16 setup = TRANSACT2_QFILEINFO;
1011 char param[4];
1012 char *rparam=NULL, *rdata=NULL;
1013
1014 /* if its a win95 server then fail this - win95 totally screws it
1015 up */
1016 if (cli->win95) return False;
1017
1018 param_len = 4;
1019
1020 SSVAL(param, 0, fnum);
1021 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
1022
1023 if (!cli_send_trans(cli, SMBtrans2,
1024 NULL, /* name */
1025 -1, 0, /* fid, flags */
1026 &setup, 1, 0, /* setup, length, max */
1027 param, param_len, 2, /* param, length, max */
1028 NULL, data_len, cli->max_xmit /* data, length, max */
1029 )) {
1030 return False;
1031 }
1032
1033 if (!cli_receive_trans(cli, SMBtrans2,
1034 &rparam, &param_len,
1035 &rdata, &data_len)) {
1036 return False;
1037 }
1038
1039 if (!rdata || data_len < 68) {
1040 return False;
1041 }
1042
1043 if (create_time) {
1044 *create_time = interpret_long_date(rdata+0);
1045 }
1046 if (access_time) {
1047 *access_time = interpret_long_date(rdata+8);
1048 }
1049 if (write_time) {
1050 *write_time = interpret_long_date(rdata+16);
1051 }
1052 if (change_time) {
1053 *change_time = interpret_long_date(rdata+24);
1054 }
1055 if (mode) {
1056 *mode = SVAL(rdata, 32);
1057 }
1058 if (size) {
1059 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1060 }
1061 if (ino) {
1062 *ino = IVAL(rdata, 64);
1063 }
1064
1065 SAFE_FREE(rdata);
1066 SAFE_FREE(rparam);
1067 return True;
1068}
1069
1070/****************************************************************************
1071 Send a qpathinfo BASIC_INFO call.
1072****************************************************************************/
1073
1074bool cli_qpathinfo_basic( struct cli_state *cli, const char *name,
1075 SMB_STRUCT_STAT *sbuf, uint32 *attributes )
1076{
1077 unsigned int param_len = 0;
1078 unsigned int data_len = 0;
1079 uint16 setup = TRANSACT2_QPATHINFO;
1080 char *param;
1081 char *rparam=NULL, *rdata=NULL;
1082 char *p;
1083 char *path;
1084 int len;
1085 size_t nlen;
1086 TALLOC_CTX *frame = talloc_stackframe();
1087
1088 path = talloc_strdup(frame, name);
1089 if (!path) {
1090 TALLOC_FREE(frame);
1091 return false;
1092 }
1093 /* cleanup */
1094
1095 len = strlen(path);
1096 if ( path[len-1] == '\\' || path[len-1] == '/') {
1097 path[len-1] = '\0';
1098 }
1099 nlen = 2*(strlen(path)+1);
1100
1101 param = TALLOC_ARRAY(frame,char,6+nlen+2);
1102 if (!param) {
1103 return false;
1104 }
1105 p = param;
1106 memset(param, '\0', 6);
1107
1108 SSVAL(p, 0, SMB_QUERY_FILE_BASIC_INFO);
1109 p += 6;
1110 p += clistr_push(cli, p, path, nlen, STR_TERMINATE);
1111 param_len = PTR_DIFF(p, param);
1112
1113
1114 if (!cli_send_trans(cli, SMBtrans2,
1115 NULL, /* name */
1116 -1, 0, /* fid, flags */
1117 &setup, 1, 0, /* setup, length, max */
1118 param, param_len, 2, /* param, length, max */
1119 NULL, 0, cli->max_xmit /* data, length, max */
1120 )) {
1121 TALLOC_FREE(frame);
1122 return False;
1123 }
1124
1125 TALLOC_FREE(frame);
1126
1127 if (!cli_receive_trans(cli, SMBtrans2,
1128 &rparam, &param_len,
1129 &rdata, &data_len)) {
1130 return False;
1131 }
1132
1133 if (data_len < 36) {
1134 SAFE_FREE(rdata);
1135 SAFE_FREE(rparam);
1136 return False;
1137 }
1138
1139 set_atimespec(sbuf, interpret_long_date( rdata+8 )); /* Access time. */
1140 set_mtimespec(sbuf, interpret_long_date( rdata+16 )); /* Write time. */
1141 set_ctimespec(sbuf, interpret_long_date( rdata+24 )); /* Change time. */
1142
1143 *attributes = IVAL( rdata, 32 );
1144
1145 SAFE_FREE(rparam);
1146 SAFE_FREE(rdata);
1147
1148 return True;
1149}
1150
1151/****************************************************************************
1152 Send a qfileinfo call.
1153****************************************************************************/
1154
1155bool cli_qfileinfo_test(struct cli_state *cli, int fnum, int level, char **poutdata, uint32 *poutlen)
1156{
1157 unsigned int data_len = 0;
1158 unsigned int param_len = 0;
1159 uint16 setup = TRANSACT2_QFILEINFO;
1160 char param[4];
1161 char *rparam=NULL, *rdata=NULL;
1162
1163 *poutdata = NULL;
1164 *poutlen = 0;
1165
1166 /* if its a win95 server then fail this - win95 totally screws it
1167 up */
1168 if (cli->win95)
1169 return False;
1170
1171 param_len = 4;
1172
1173 SSVAL(param, 0, fnum);
1174 SSVAL(param, 2, level);
1175
1176 if (!cli_send_trans(cli, SMBtrans2,
1177 NULL, /* name */
1178 -1, 0, /* fid, flags */
1179 &setup, 1, 0, /* setup, length, max */
1180 param, param_len, 2, /* param, length, max */
1181 NULL, data_len, cli->max_xmit /* data, length, max */
1182 )) {
1183 return False;
1184 }
1185
1186 if (!cli_receive_trans(cli, SMBtrans2,
1187 &rparam, &param_len,
1188 &rdata, &data_len)) {
1189 return False;
1190 }
1191
1192 *poutdata = (char *)memdup(rdata, data_len);
1193 if (!*poutdata) {
1194 SAFE_FREE(rdata);
1195 SAFE_FREE(rparam);
1196 return False;
1197 }
1198
1199 *poutlen = data_len;
1200
1201 SAFE_FREE(rdata);
1202 SAFE_FREE(rparam);
1203 return True;
1204}
1205
1206/****************************************************************************
1207 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1208****************************************************************************/
1209
1210NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1211{
1212 unsigned int data_len = 0;
1213 unsigned int param_len = 0;
1214 uint16 setup = TRANSACT2_QPATHINFO;
1215 char *param;
1216 char *rparam=NULL, *rdata=NULL;
1217 int count=8;
1218 char *p;
1219 bool ret;
1220 unsigned int len;
1221 size_t nlen = 2*(strlen(fname)+1);
1222
1223 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
1224 if (!param) {
1225 return NT_STATUS_NO_MEMORY;
1226 }
1227 p = param;
1228 memset(param, '\0', 6);
1229 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
1230 p += 6;
1231 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
1232 param_len = PTR_DIFF(p, param);
1233
1234 do {
1235 ret = (cli_send_trans(cli, SMBtrans2,
1236 NULL, /* Name */
1237 -1, 0, /* fid, flags */
1238 &setup, 1, 0, /* setup, length, max */
1239 param, param_len, 10, /* param, length, max */
1240 NULL, data_len, cli->max_xmit /* data, length, max */
1241 ) &&
1242 cli_receive_trans(cli, SMBtrans2,
1243 &rparam, &param_len,
1244 &rdata, &data_len));
1245 if (!ret && cli_is_dos_error(cli)) {
1246 /* we need to work around a Win95 bug - sometimes
1247 it gives ERRSRV/ERRerror temprarily */
1248 uint8 eclass;
1249 uint32 ecode;
1250 cli_dos_error(cli, &eclass, &ecode);
1251 if (eclass != ERRSRV || ecode != ERRerror) break;
1252 smb_msleep(100);
1253 }
1254 } while (count-- && ret==False);
1255
1256 SAFE_FREE(param);
1257
1258 if (!ret || !rdata || data_len < 4) {
1259 return NT_STATUS_UNSUCCESSFUL;
1260 }
1261
1262 len = IVAL(rdata, 0);
1263
1264 if (len > data_len - 4) {
1265 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1266 }
1267
1268 clistr_pull(cli, alt_name, rdata+4, sizeof(fstring), len, STR_UNICODE);
1269
1270 SAFE_FREE(rdata);
1271 SAFE_FREE(rparam);
1272
1273 return NT_STATUS_OK;
1274}
Note: See TracBrowser for help on using the repository browser.