s4-smbtorture: use struct torture_context in RPC-LSA.
[Samba.git] / source3 / libsmb / clirap.c
blob3f95e77aeec31a0ff227f15321de17ddb9945241
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
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.
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.
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/>.
22 #include "includes.h"
24 /****************************************************************************
25 Call a remote api
26 ****************************************************************************/
28 bool cli_api(struct cli_state *cli,
29 char *param, int prcnt, int mprcnt,
30 char *data, int drcnt, int mdrcnt,
31 char **rparam, unsigned int *rprcnt,
32 char **rdata, unsigned int *rdrcnt)
34 cli_send_trans(cli,SMBtrans,
35 PIPE_LANMAN, /* Name */
36 0,0, /* fid, flags */
37 NULL,0,0, /* Setup, length, max */
38 param, prcnt, mprcnt, /* Params, length, max */
39 data, drcnt, mdrcnt /* Data, length, max */
42 return (cli_receive_trans(cli,SMBtrans,
43 rparam, rprcnt,
44 rdata, rdrcnt));
47 /****************************************************************************
48 Perform a NetWkstaUserLogon.
49 ****************************************************************************/
51 bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
53 char *rparam = NULL;
54 char *rdata = NULL;
55 char *p;
56 unsigned int rdrcnt,rprcnt;
57 char param[1024];
59 memset(param, 0, sizeof(param));
61 /* send a SMBtrans command with api NetWkstaUserLogon */
62 p = param;
63 SSVAL(p,0,132); /* api number */
64 p += 2;
65 strlcpy(p,"OOWb54WrLh",sizeof(param)-PTR_DIFF(p,param));
66 p = skip_string(param,sizeof(param),p);
67 strlcpy(p,"WB21BWDWWDDDDDDDzzzD",sizeof(param)-PTR_DIFF(p,param));
68 p = skip_string(param,sizeof(param),p);
69 SSVAL(p,0,1);
70 p += 2;
71 strlcpy(p,user,sizeof(param)-PTR_DIFF(p,param));
72 strupper_m(p);
73 p += 21;
74 p++;
75 p += 15;
76 p++;
77 strlcpy(p, workstation,sizeof(param)-PTR_DIFF(p,param));
78 strupper_m(p);
79 p += 16;
80 SSVAL(p, 0, CLI_BUFFER_SIZE);
81 p += 2;
82 SSVAL(p, 0, CLI_BUFFER_SIZE);
83 p += 2;
85 if (cli_api(cli,
86 param, PTR_DIFF(p,param),1024, /* param, length, max */
87 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
88 &rparam, &rprcnt, /* return params, return size */
89 &rdata, &rdrcnt /* return data, return size */
90 )) {
91 cli->rap_error = rparam? SVAL(rparam,0) : -1;
92 p = rdata;
94 if (cli->rap_error == 0) {
95 DEBUG(4,("NetWkstaUserLogon success\n"));
96 cli->privileges = SVAL(p, 24);
97 /* The cli->eff_name field used to be set here
98 but it wasn't used anywhere else. */
99 } else {
100 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
104 SAFE_FREE(rparam);
105 SAFE_FREE(rdata);
106 return (cli->rap_error == 0);
109 /****************************************************************************
110 Call a NetShareEnum - try and browse available connections on a host.
111 ****************************************************************************/
113 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
115 char *rparam = NULL;
116 char *rdata = NULL;
117 char *p;
118 unsigned int rdrcnt,rprcnt;
119 char param[1024];
120 int count = -1;
122 /* now send a SMBtrans command with api RNetShareEnum */
123 p = param;
124 SSVAL(p,0,0); /* api number */
125 p += 2;
126 strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
127 p = skip_string(param,sizeof(param),p);
128 strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
129 p = skip_string(param,sizeof(param),p);
130 SSVAL(p,0,1);
132 * Win2k needs a *smaller* buffer than 0xFFFF here -
133 * it returns "out of server memory" with 0xFFFF !!! JRA.
135 SSVAL(p,2,0xFFE0);
136 p += 4;
138 if (cli_api(cli,
139 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
140 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
141 &rparam, &rprcnt, /* return params, length */
142 &rdata, &rdrcnt)) /* return data, length */
144 int res = rparam? SVAL(rparam,0) : -1;
146 if (res == 0 || res == ERRmoredata) {
147 int converter=SVAL(rparam,2);
148 int i;
149 char *rdata_end = rdata + rdrcnt;
151 count=SVAL(rparam,4);
152 p = rdata;
154 for (i=0;i<count;i++,p+=20) {
155 char *sname;
156 int type;
157 int comment_offset;
158 const char *cmnt;
159 const char *p1;
160 char *s1, *s2;
161 size_t len;
162 TALLOC_CTX *frame = talloc_stackframe();
164 if (p + 20 > rdata_end) {
165 TALLOC_FREE(frame);
166 break;
169 sname = p;
170 type = SVAL(p,14);
171 comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
172 if (comment_offset < 0 ||
173 comment_offset > (int)rdrcnt) {
174 TALLOC_FREE(frame);
175 break;
177 cmnt = comment_offset?(rdata+comment_offset):"";
179 /* Work out the comment length. */
180 for (p1 = cmnt, len = 0; *p1 &&
181 p1 < rdata_end; len++)
182 p1++;
183 if (!*p1) {
184 len++;
186 pull_string_talloc(frame,rdata,0,
187 &s1,sname,14,STR_ASCII);
188 pull_string_talloc(frame,rdata,0,
189 &s2,cmnt,len,STR_ASCII);
190 if (!s1 || !s2) {
191 TALLOC_FREE(frame);
192 continue;
195 fn(s1, type, s2, state);
197 TALLOC_FREE(frame);
199 } else {
200 DEBUG(4,("NetShareEnum res=%d\n", res));
202 } else {
203 DEBUG(4,("NetShareEnum failed\n"));
206 SAFE_FREE(rparam);
207 SAFE_FREE(rdata);
209 return count;
212 /****************************************************************************
213 Call a NetServerEnum for the specified workgroup and servertype mask. This
214 function then calls the specified callback function for each name returned.
216 The callback function takes 4 arguments: the machine name, the server type,
217 the comment and a state pointer.
218 ****************************************************************************/
220 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
221 void (*fn)(const char *, uint32, const char *, void *),
222 void *state)
224 char *rparam = NULL;
225 char *rdata = NULL;
226 char *rdata_end = NULL;
227 unsigned int rdrcnt,rprcnt;
228 char *p;
229 char param[1024];
230 int uLevel = 1;
231 size_t len;
232 uint32 func = RAP_NetServerEnum2;
233 char *last_entry = NULL;
234 int total_cnt = 0;
235 int return_cnt = 0;
236 int res;
238 errno = 0; /* reset */
241 * This may take more than one transaction, so we should loop until
242 * we no longer get a more data to process or we have all of the
243 * items.
245 do {
246 /* send a SMBtrans command with api NetServerEnum */
247 p = param;
248 SIVAL(p,0,func); /* api number */
249 p += 2;
250 /* Next time through we need to use the continue api */
251 func = RAP_NetServerEnum3;
253 if (last_entry) {
254 strlcpy(p,"WrLehDOz", sizeof(param)-PTR_DIFF(p,param));
255 } else {
256 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
259 p = skip_string(param, sizeof(param), p);
260 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
262 p = skip_string(param, sizeof(param), p);
263 SSVAL(p,0,uLevel);
264 SSVAL(p,2,CLI_BUFFER_SIZE);
265 p += 4;
266 SIVAL(p,0,stype);
267 p += 4;
269 /* If we have more data, tell the server where
270 * to continue from.
272 len = push_ascii(p,
273 last_entry ? last_entry : workgroup,
274 sizeof(param) - PTR_DIFF(p,param) - 1,
275 STR_TERMINATE|STR_UPPER);
277 if (len == (size_t)-1) {
278 SAFE_FREE(last_entry);
279 return false;
281 p += len;
283 if (!cli_api(cli,
284 param, PTR_DIFF(p,param), 8, /* params, length, max */
285 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
286 &rparam, &rprcnt, /* return params, return size */
287 &rdata, &rdrcnt)) { /* return data, return size */
289 /* break out of the loop on error */
290 res = -1;
291 break;
294 rdata_end = rdata + rdrcnt;
295 res = rparam ? SVAL(rparam,0) : -1;
297 if (res == 0 || res == ERRmoredata ||
298 (res != -1 && cli_errno(cli) == 0)) {
299 char *sname = NULL;
300 int i, count;
301 int converter=SVAL(rparam,2);
303 /* Get the number of items returned in this buffer */
304 count = SVAL(rparam, 4);
306 /* The next field contains the number of items left,
307 * including those returned in this buffer. So the
308 * first time through this should contain all of the
309 * entries.
311 if (total_cnt == 0) {
312 total_cnt = SVAL(rparam, 6);
315 /* Keep track of how many we have read */
316 return_cnt += count;
317 p = rdata;
319 /* The last name in the previous NetServerEnum reply is
320 * sent back to server in the NetServerEnum3 request
321 * (last_entry). The next reply should repeat this entry
322 * as the first element. We have no proof that this is
323 * always true, but from traces that seems to be the
324 * behavior from Window Servers. So first lets do a lot
325 * of checking, just being paranoid. If the string
326 * matches then we already saw this entry so skip it.
328 * NOTE: sv1_name field must be null terminated and has
329 * a max size of 16 (NetBIOS Name).
331 if (last_entry && count && p &&
332 (strncmp(last_entry, p, 16) == 0)) {
333 count -= 1; /* Skip this entry */
334 return_cnt = -1; /* Not part of total, so don't count. */
335 p = rdata + 26; /* Skip the whole record */
338 for (i = 0; i < count; i++, p += 26) {
339 int comment_offset;
340 const char *cmnt;
341 const char *p1;
342 char *s1, *s2;
343 TALLOC_CTX *frame = talloc_stackframe();
345 if (p + 26 > rdata_end) {
346 TALLOC_FREE(frame);
347 break;
350 sname = p;
351 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
352 cmnt = comment_offset?(rdata+comment_offset):"";
354 if (comment_offset < 0 || comment_offset > (int)rdrcnt) {
355 TALLOC_FREE(frame);
356 continue;
359 /* Work out the comment length. */
360 for (p1 = cmnt, len = 0; *p1 &&
361 p1 < rdata_end; len++)
362 p1++;
363 if (!*p1) {
364 len++;
367 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
369 pull_string_talloc(frame,rdata,0,
370 &s1,sname,16,STR_ASCII);
371 pull_string_talloc(frame,rdata,0,
372 &s2,cmnt,len,STR_ASCII);
374 if (!s1 || !s2) {
375 TALLOC_FREE(frame);
376 continue;
379 fn(s1, stype, s2, state);
380 TALLOC_FREE(frame);
383 /* We are done with the old last entry, so now we can free it */
384 if (last_entry) {
385 SAFE_FREE(last_entry); /* This will set it to null */
388 /* We always make a copy of the last entry if we have one */
389 if (sname) {
390 last_entry = smb_xstrdup(sname);
393 /* If we have more data, but no last entry then error out */
394 if (!last_entry && (res == ERRmoredata)) {
395 errno = EINVAL;
396 res = 0;
401 SAFE_FREE(rparam);
402 SAFE_FREE(rdata);
403 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
405 SAFE_FREE(rparam);
406 SAFE_FREE(rdata);
407 SAFE_FREE(last_entry);
409 if (res == -1) {
410 errno = cli_errno(cli);
411 } else {
412 if (!return_cnt) {
413 /* this is a very special case, when the domain master for the
414 work group isn't part of the work group itself, there is something
415 wild going on */
416 errno = ENOENT;
420 return(return_cnt > 0);
423 /****************************************************************************
424 Send a SamOEMChangePassword command.
425 ****************************************************************************/
427 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
428 const char *old_password)
430 char param[1024];
431 unsigned char data[532];
432 char *p = param;
433 unsigned char old_pw_hash[16];
434 unsigned char new_pw_hash[16];
435 unsigned int data_len;
436 unsigned int param_len = 0;
437 char *rparam = NULL;
438 char *rdata = NULL;
439 unsigned int rprcnt, rdrcnt;
441 if (strlen(user) >= sizeof(fstring)-1) {
442 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
443 return False;
446 SSVAL(p,0,214); /* SamOEMChangePassword command. */
447 p += 2;
448 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
449 p = skip_string(param,sizeof(param),p);
450 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
451 p = skip_string(param,sizeof(param),p);
452 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
453 p = skip_string(param,sizeof(param),p);
454 SSVAL(p,0,532);
455 p += 2;
457 param_len = PTR_DIFF(p,param);
460 * Get the Lanman hash of the old password, we
461 * use this as the key to make_oem_passwd_hash().
463 E_deshash(old_password, old_pw_hash);
465 encode_pw_buffer(data, new_password, STR_ASCII);
467 #ifdef DEBUG_PASSWORD
468 DEBUG(100,("make_oem_passwd_hash\n"));
469 dump_data(100, data, 516);
470 #endif
471 SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
474 * Now place the old password hash in the data.
476 E_deshash(new_password, new_pw_hash);
478 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
480 data_len = 532;
482 if (cli_send_trans(cli,SMBtrans,
483 PIPE_LANMAN, /* name */
484 0,0, /* fid, flags */
485 NULL,0,0, /* setup, length, max */
486 param,param_len,2, /* param, length, max */
487 (char *)data,data_len,0 /* data, length, max */
488 ) == False) {
489 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
490 user ));
491 return False;
494 if (!cli_receive_trans(cli,SMBtrans,
495 &rparam, &rprcnt,
496 &rdata, &rdrcnt)) {
497 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
498 user ));
499 return False;
502 if (rparam) {
503 cli->rap_error = SVAL(rparam,0);
506 SAFE_FREE(rparam);
507 SAFE_FREE(rdata);
509 return (cli->rap_error == 0);
512 /****************************************************************************
513 Send a qpathinfo call.
514 ****************************************************************************/
516 bool cli_qpathinfo(struct cli_state *cli,
517 const char *fname,
518 time_t *change_time,
519 time_t *access_time,
520 time_t *write_time,
521 SMB_OFF_T *size,
522 uint16 *mode)
524 unsigned int data_len = 0;
525 unsigned int param_len = 0;
526 unsigned int rparam_len, rdata_len;
527 uint16 setup = TRANSACT2_QPATHINFO;
528 char *param;
529 char *rparam=NULL, *rdata=NULL;
530 int count=8;
531 bool ret;
532 time_t (*date_fn)(struct cli_state *, const void *);
533 char *p;
534 size_t nlen = 2*(strlen(fname)+1);
536 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
537 if (!param) {
538 return false;
540 p = param;
541 memset(p, '\0', 6);
542 SSVAL(p, 0, SMB_INFO_STANDARD);
543 p += 6;
544 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
545 param_len = PTR_DIFF(p, param);
547 do {
548 ret = (cli_send_trans(cli, SMBtrans2,
549 NULL, /* Name */
550 -1, 0, /* fid, flags */
551 &setup, 1, 0, /* setup, length, max */
552 param, param_len, 10, /* param, length, max */
553 NULL, data_len, cli->max_xmit /* data, length, max */
554 ) &&
555 cli_receive_trans(cli, SMBtrans2,
556 &rparam, &rparam_len,
557 &rdata, &rdata_len));
558 if (!cli_is_dos_error(cli)) break;
559 if (!ret) {
560 /* we need to work around a Win95 bug - sometimes
561 it gives ERRSRV/ERRerror temprarily */
562 uint8 eclass;
563 uint32 ecode;
564 cli_dos_error(cli, &eclass, &ecode);
565 if (eclass != ERRSRV || ecode != ERRerror) break;
566 smb_msleep(100);
568 } while (count-- && ret==False);
570 SAFE_FREE(param);
571 if (!ret || !rdata || rdata_len < 22) {
572 return False;
575 if (cli->win95) {
576 date_fn = cli_make_unix_date;
577 } else {
578 date_fn = cli_make_unix_date2;
581 if (change_time) {
582 *change_time = date_fn(cli, rdata+0);
584 if (access_time) {
585 *access_time = date_fn(cli, rdata+4);
587 if (write_time) {
588 *write_time = date_fn(cli, rdata+8);
590 if (size) {
591 *size = IVAL(rdata, 12);
593 if (mode) {
594 *mode = SVAL(rdata,l1_attrFile);
597 SAFE_FREE(rdata);
598 SAFE_FREE(rparam);
599 return True;
602 /****************************************************************************
603 Send a setpathinfo call.
604 ****************************************************************************/
606 bool cli_setpathinfo(struct cli_state *cli, const char *fname,
607 time_t create_time,
608 time_t access_time,
609 time_t write_time,
610 time_t change_time,
611 uint16 mode)
613 unsigned int data_len = 0;
614 unsigned int param_len = 0;
615 unsigned int rparam_len, rdata_len;
616 uint16 setup = TRANSACT2_SETPATHINFO;
617 char *param;
618 char data[40];
619 char *rparam=NULL, *rdata=NULL;
620 int count=8;
621 bool ret;
622 char *p;
623 size_t nlen = 2*(strlen(fname)+1);
625 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
626 if (!param) {
627 return false;
629 memset(param, '\0', 6);
630 memset(data, 0, sizeof(data));
632 p = param;
634 /* Add the information level */
635 SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
637 /* Skip reserved */
638 p += 6;
640 /* Add the file name */
641 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
643 param_len = PTR_DIFF(p, param);
645 p = data;
648 * Add the create, last access, modification, and status change times
650 put_long_date(p, create_time);
651 p += 8;
653 put_long_date(p, access_time);
654 p += 8;
656 put_long_date(p, write_time);
657 p += 8;
659 put_long_date(p, change_time);
660 p += 8;
662 /* Add attributes */
663 SIVAL(p, 0, mode);
664 p += 4;
666 /* Add padding */
667 SIVAL(p, 0, 0);
668 p += 4;
670 data_len = PTR_DIFF(p, data);
672 do {
673 ret = (cli_send_trans(cli, SMBtrans2,
674 NULL, /* Name */
675 -1, 0, /* fid, flags */
676 &setup, 1, 0, /* setup, length, max */
677 param, param_len, 10, /* param, length, max */
678 data, data_len, cli->max_xmit /* data, length, max */
679 ) &&
680 cli_receive_trans(cli, SMBtrans2,
681 &rparam, &rparam_len,
682 &rdata, &rdata_len));
683 if (!cli_is_dos_error(cli)) break;
684 if (!ret) {
685 /* we need to work around a Win95 bug - sometimes
686 it gives ERRSRV/ERRerror temprarily */
687 uint8 eclass;
688 uint32 ecode;
689 cli_dos_error(cli, &eclass, &ecode);
690 if (eclass != ERRSRV || ecode != ERRerror) break;
691 smb_msleep(100);
693 } while (count-- && ret==False);
695 SAFE_FREE(param);
696 if (!ret) {
697 return False;
700 SAFE_FREE(rdata);
701 SAFE_FREE(rparam);
702 return True;
705 /****************************************************************************
706 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
707 ****************************************************************************/
709 bool cli_qpathinfo2(struct cli_state *cli, const char *fname,
710 struct timespec *create_time,
711 struct timespec *access_time,
712 struct timespec *write_time,
713 struct timespec *change_time,
714 SMB_OFF_T *size, uint16 *mode,
715 SMB_INO_T *ino)
717 unsigned int data_len = 0;
718 unsigned int param_len = 0;
719 uint16 setup = TRANSACT2_QPATHINFO;
720 char *param;
721 char *rparam=NULL, *rdata=NULL;
722 char *p;
723 size_t nlen = 2*(strlen(fname)+1);
725 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
726 if (!param) {
727 return false;
729 p = param;
730 memset(param, '\0', 6);
731 SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
732 p += 6;
733 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
735 param_len = PTR_DIFF(p, param);
737 if (!cli_send_trans(cli, SMBtrans2,
738 NULL, /* name */
739 -1, 0, /* fid, flags */
740 &setup, 1, 0, /* setup, length, max */
741 param, param_len, 10, /* param, length, max */
742 NULL, data_len, cli->max_xmit /* data, length, max */
743 )) {
744 SAFE_FREE(param);
745 return False;
748 SAFE_FREE(param);
749 if (!cli_receive_trans(cli, SMBtrans2,
750 &rparam, &param_len,
751 &rdata, &data_len)) {
752 return False;
755 if (!rdata || data_len < 22) {
756 return False;
759 if (create_time) {
760 *create_time = interpret_long_date(rdata+0);
762 if (access_time) {
763 *access_time = interpret_long_date(rdata+8);
765 if (write_time) {
766 *write_time = interpret_long_date(rdata+16);
768 if (change_time) {
769 *change_time = interpret_long_date(rdata+24);
771 if (mode) {
772 *mode = SVAL(rdata, 32);
774 if (size) {
775 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
777 if (ino) {
778 *ino = IVAL(rdata, 64);
781 SAFE_FREE(rdata);
782 SAFE_FREE(rparam);
783 return True;
786 /****************************************************************************
787 Get the stream info
788 ****************************************************************************/
790 bool cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
791 TALLOC_CTX *mem_ctx,
792 unsigned int *pnum_streams,
793 struct stream_struct **pstreams)
795 unsigned int data_len = 0;
796 unsigned int param_len = 0;
797 uint16 setup = TRANSACT2_QPATHINFO;
798 char *param;
799 char *rparam=NULL, *rdata=NULL;
800 char *p;
801 unsigned int num_streams;
802 struct stream_struct *streams;
803 unsigned int ofs;
804 size_t namelen = 2*(strlen(fname)+1);
806 param = SMB_MALLOC_ARRAY(char, 6+namelen+2);
807 if (param == NULL) {
808 return false;
810 p = param;
811 memset(p, 0, 6);
812 SSVAL(p, 0, SMB_FILE_STREAM_INFORMATION);
813 p += 6;
814 p += clistr_push(cli, p, fname, namelen, STR_TERMINATE);
816 param_len = PTR_DIFF(p, param);
818 if (!cli_send_trans(cli, SMBtrans2,
819 NULL, /* name */
820 -1, 0, /* fid, flags */
821 &setup, 1, 0, /* setup, len, max */
822 param, param_len, 10, /* param, len, max */
823 NULL, data_len, cli->max_xmit /* data, len, max */
824 )) {
825 return false;
828 if (!cli_receive_trans(cli, SMBtrans2,
829 &rparam, &param_len,
830 &rdata, &data_len)) {
831 return false;
834 if (!rdata) {
835 SAFE_FREE(rparam);
836 return false;
839 num_streams = 0;
840 streams = NULL;
841 ofs = 0;
843 while ((data_len > ofs) && (data_len - ofs >= 24)) {
844 uint32_t nlen, len;
845 size_t size;
846 void *vstr;
847 struct stream_struct *tmp;
848 uint8_t *tmp_buf;
850 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
851 struct stream_struct,
852 num_streams+1);
854 if (tmp == NULL) {
855 goto fail;
857 streams = tmp;
859 nlen = IVAL(rdata, ofs + 0x04);
861 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
862 rdata, ofs + 0x08);
863 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
864 rdata, ofs + 0x10);
866 if (nlen > data_len - (ofs + 24)) {
867 goto fail;
871 * We need to null-terminate src, how do I do this with
872 * convert_string_talloc??
875 tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
876 if (tmp_buf == NULL) {
877 goto fail;
880 memcpy(tmp_buf, rdata+ofs+24, nlen);
881 tmp_buf[nlen] = 0;
882 tmp_buf[nlen+1] = 0;
884 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
885 nlen+2, &vstr, &size, false))
887 TALLOC_FREE(tmp_buf);
888 goto fail;
891 TALLOC_FREE(tmp_buf);
892 streams[num_streams].name = (char *)vstr;
893 num_streams++;
895 len = IVAL(rdata, ofs);
896 if (len > data_len - ofs) {
897 goto fail;
899 if (len == 0) break;
900 ofs += len;
903 SAFE_FREE(rdata);
904 SAFE_FREE(rparam);
906 *pnum_streams = num_streams;
907 *pstreams = streams;
908 return true;
910 fail:
911 TALLOC_FREE(streams);
912 SAFE_FREE(rdata);
913 SAFE_FREE(rparam);
914 return false;
917 /****************************************************************************
918 Send a qfileinfo QUERY_FILE_NAME_INFO call.
919 ****************************************************************************/
921 bool cli_qfilename(struct cli_state *cli, int fnum, char *name, size_t namelen)
923 unsigned int data_len = 0;
924 unsigned int param_len = 0;
925 uint16 setup = TRANSACT2_QFILEINFO;
926 char param[4];
927 char *rparam=NULL, *rdata=NULL;
929 param_len = 4;
930 SSVAL(param, 0, fnum);
931 SSVAL(param, 2, SMB_QUERY_FILE_NAME_INFO);
933 if (!cli_send_trans(cli, SMBtrans2,
934 NULL, /* name */
935 -1, 0, /* fid, flags */
936 &setup, 1, 0, /* setup, length, max */
937 param, param_len, 2, /* param, length, max */
938 NULL, data_len, cli->max_xmit /* data, length, max */
939 )) {
940 return False;
943 if (!cli_receive_trans(cli, SMBtrans2,
944 &rparam, &param_len,
945 &rdata, &data_len)) {
946 return False;
949 if (!rdata || data_len < 4) {
950 SAFE_FREE(rparam);
951 SAFE_FREE(rdata);
952 return False;
955 clistr_pull(cli->inbuf, name, rdata+4, namelen, IVAL(rdata, 0),
956 STR_UNICODE);
958 SAFE_FREE(rparam);
959 SAFE_FREE(rdata);
961 return True;
964 /****************************************************************************
965 Send a qfileinfo call.
966 ****************************************************************************/
968 bool cli_qfileinfo(struct cli_state *cli, int fnum,
969 uint16 *mode, SMB_OFF_T *size,
970 struct timespec *create_time,
971 struct timespec *access_time,
972 struct timespec *write_time,
973 struct timespec *change_time,
974 SMB_INO_T *ino)
976 unsigned int data_len = 0;
977 unsigned int param_len = 0;
978 uint16 setup;
979 uint8_t param[4];
980 uint8_t *rparam=NULL, *rdata=NULL;
981 NTSTATUS status;
983 /* if its a win95 server then fail this - win95 totally screws it
984 up */
985 if (cli->win95) return False;
987 param_len = 4;
989 SSVAL(param, 0, fnum);
990 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
992 SSVAL(&setup, 0, TRANSACT2_QFILEINFO);
994 status = cli_trans(talloc_tos(), cli, SMBtrans2,
995 NULL, -1, 0, 0, /* name, fid, function, flags */
996 &setup, 1, 0, /* setup, length, max */
997 param, param_len, 2, /* param, length, max */
998 NULL, 0, MIN(cli->max_xmit, 0xffff), /* data, length, max */
999 NULL, NULL, /* rsetup, length */
1000 &rparam, &param_len, /* rparam, length */
1001 &rdata, &data_len);
1003 if (!NT_STATUS_IS_OK(status)) {
1004 return false;
1007 if (!rdata || data_len < 68) {
1008 return False;
1011 if (create_time) {
1012 *create_time = interpret_long_date((char *)rdata+0);
1014 if (access_time) {
1015 *access_time = interpret_long_date((char *)rdata+8);
1017 if (write_time) {
1018 *write_time = interpret_long_date((char *)rdata+16);
1020 if (change_time) {
1021 *change_time = interpret_long_date((char *)rdata+24);
1023 if (mode) {
1024 *mode = SVAL(rdata, 32);
1026 if (size) {
1027 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1029 if (ino) {
1030 *ino = IVAL(rdata, 64);
1033 TALLOC_FREE(rdata);
1034 TALLOC_FREE(rparam);
1035 return True;
1038 /****************************************************************************
1039 Send a qpathinfo BASIC_INFO call.
1040 ****************************************************************************/
1042 bool cli_qpathinfo_basic( struct cli_state *cli, const char *name,
1043 SMB_STRUCT_STAT *sbuf, uint32 *attributes )
1045 unsigned int param_len = 0;
1046 unsigned int data_len = 0;
1047 uint16 setup = TRANSACT2_QPATHINFO;
1048 char *param;
1049 char *rparam=NULL, *rdata=NULL;
1050 char *p;
1051 char *path;
1052 int len;
1053 size_t nlen;
1054 TALLOC_CTX *frame = talloc_stackframe();
1056 path = talloc_strdup(frame, name);
1057 if (!path) {
1058 TALLOC_FREE(frame);
1059 return false;
1061 /* cleanup */
1063 len = strlen(path);
1064 if ( path[len-1] == '\\' || path[len-1] == '/') {
1065 path[len-1] = '\0';
1067 nlen = 2*(strlen(path)+1);
1069 param = TALLOC_ARRAY(frame,char,6+nlen+2);
1070 if (!param) {
1071 return false;
1073 p = param;
1074 memset(param, '\0', 6);
1076 SSVAL(p, 0, SMB_QUERY_FILE_BASIC_INFO);
1077 p += 6;
1078 p += clistr_push(cli, p, path, nlen, STR_TERMINATE);
1079 param_len = PTR_DIFF(p, param);
1082 if (!cli_send_trans(cli, SMBtrans2,
1083 NULL, /* name */
1084 -1, 0, /* fid, flags */
1085 &setup, 1, 0, /* setup, length, max */
1086 param, param_len, 2, /* param, length, max */
1087 NULL, 0, cli->max_xmit /* data, length, max */
1088 )) {
1089 TALLOC_FREE(frame);
1090 return False;
1093 TALLOC_FREE(frame);
1095 if (!cli_receive_trans(cli, SMBtrans2,
1096 &rparam, &param_len,
1097 &rdata, &data_len)) {
1098 return False;
1101 if (data_len < 36) {
1102 SAFE_FREE(rdata);
1103 SAFE_FREE(rparam);
1104 return False;
1107 set_atimespec(sbuf, interpret_long_date( rdata+8 )); /* Access time. */
1108 set_mtimespec(sbuf, interpret_long_date( rdata+16 )); /* Write time. */
1109 set_ctimespec(sbuf, interpret_long_date( rdata+24 )); /* Change time. */
1111 *attributes = IVAL( rdata, 32 );
1113 SAFE_FREE(rparam);
1114 SAFE_FREE(rdata);
1116 return True;
1119 /****************************************************************************
1120 Send a qfileinfo call.
1121 ****************************************************************************/
1123 bool cli_qfileinfo_test(struct cli_state *cli, int fnum, int level, char **poutdata, uint32 *poutlen)
1125 unsigned int data_len = 0;
1126 unsigned int param_len = 0;
1127 uint16 setup = TRANSACT2_QFILEINFO;
1128 char param[4];
1129 char *rparam=NULL, *rdata=NULL;
1131 *poutdata = NULL;
1132 *poutlen = 0;
1134 /* if its a win95 server then fail this - win95 totally screws it
1135 up */
1136 if (cli->win95)
1137 return False;
1139 param_len = 4;
1141 SSVAL(param, 0, fnum);
1142 SSVAL(param, 2, level);
1144 if (!cli_send_trans(cli, SMBtrans2,
1145 NULL, /* name */
1146 -1, 0, /* fid, flags */
1147 &setup, 1, 0, /* setup, length, max */
1148 param, param_len, 2, /* param, length, max */
1149 NULL, data_len, cli->max_xmit /* data, length, max */
1150 )) {
1151 return False;
1154 if (!cli_receive_trans(cli, SMBtrans2,
1155 &rparam, &param_len,
1156 &rdata, &data_len)) {
1157 return False;
1160 *poutdata = (char *)memdup(rdata, data_len);
1161 if (!*poutdata) {
1162 SAFE_FREE(rdata);
1163 SAFE_FREE(rparam);
1164 return False;
1167 *poutlen = data_len;
1169 SAFE_FREE(rdata);
1170 SAFE_FREE(rparam);
1171 return True;
1174 /****************************************************************************
1175 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1176 ****************************************************************************/
1178 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1180 unsigned int data_len = 0;
1181 unsigned int param_len = 0;
1182 uint16 setup = TRANSACT2_QPATHINFO;
1183 char *param;
1184 char *rparam=NULL, *rdata=NULL;
1185 int count=8;
1186 char *p;
1187 bool ret;
1188 unsigned int len;
1189 size_t nlen = 2*(strlen(fname)+1);
1191 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
1192 if (!param) {
1193 return NT_STATUS_NO_MEMORY;
1195 p = param;
1196 memset(param, '\0', 6);
1197 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
1198 p += 6;
1199 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
1200 param_len = PTR_DIFF(p, param);
1202 do {
1203 ret = (cli_send_trans(cli, SMBtrans2,
1204 NULL, /* Name */
1205 -1, 0, /* fid, flags */
1206 &setup, 1, 0, /* setup, length, max */
1207 param, param_len, 10, /* param, length, max */
1208 NULL, data_len, cli->max_xmit /* data, length, max */
1209 ) &&
1210 cli_receive_trans(cli, SMBtrans2,
1211 &rparam, &param_len,
1212 &rdata, &data_len));
1213 if (!ret && cli_is_dos_error(cli)) {
1214 /* we need to work around a Win95 bug - sometimes
1215 it gives ERRSRV/ERRerror temprarily */
1216 uint8 eclass;
1217 uint32 ecode;
1218 cli_dos_error(cli, &eclass, &ecode);
1219 if (eclass != ERRSRV || ecode != ERRerror) break;
1220 smb_msleep(100);
1222 } while (count-- && ret==False);
1224 SAFE_FREE(param);
1226 if (!ret || !rdata || data_len < 4) {
1227 return NT_STATUS_UNSUCCESSFUL;
1230 len = IVAL(rdata, 0);
1232 if (len > data_len - 4) {
1233 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1236 clistr_pull(cli->inbuf, alt_name, rdata+4, sizeof(fstring), len,
1237 STR_UNICODE);
1239 SAFE_FREE(rdata);
1240 SAFE_FREE(rparam);
1242 return NT_STATUS_OK;