s3: Add an async smbsock_connect
[Samba.git] / source3 / libsmb / clirap.c
blob2abb550ab2997f1bb280c5b62e64ddcf9aded8cc
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;
251 if (func == RAP_NetServerEnum3) {
252 strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
253 } else {
254 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
257 p = skip_string(param, sizeof(param), p);
258 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
260 p = skip_string(param, sizeof(param), p);
261 SSVAL(p,0,uLevel);
262 SSVAL(p,2,CLI_BUFFER_SIZE);
263 p += 4;
264 SIVAL(p,0,stype);
265 p += 4;
267 /* If we have more data, tell the server where
268 * to continue from.
270 len = push_ascii(p,
271 workgroup,
272 sizeof(param) - PTR_DIFF(p,param) - 1,
273 STR_TERMINATE|STR_UPPER);
275 if (len == (size_t)-1) {
276 SAFE_FREE(last_entry);
277 return false;
279 p += len;
281 if (func == RAP_NetServerEnum3) {
282 len = push_ascii(p,
283 last_entry ? last_entry : "",
284 sizeof(param) - PTR_DIFF(p,param) - 1,
285 STR_TERMINATE);
287 if (len == (size_t)-1) {
288 SAFE_FREE(last_entry);
289 return false;
291 p += len;
294 /* Next time through we need to use the continue api */
295 func = RAP_NetServerEnum3;
297 if (!cli_api(cli,
298 param, PTR_DIFF(p,param), 8, /* params, length, max */
299 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
300 &rparam, &rprcnt, /* return params, return size */
301 &rdata, &rdrcnt)) { /* return data, return size */
303 /* break out of the loop on error */
304 res = -1;
305 break;
308 rdata_end = rdata + rdrcnt;
309 res = rparam ? SVAL(rparam,0) : -1;
311 if (res == 0 || res == ERRmoredata ||
312 (res != -1 && cli_errno(cli) == 0)) {
313 char *sname = NULL;
314 int i, count;
315 int converter=SVAL(rparam,2);
317 /* Get the number of items returned in this buffer */
318 count = SVAL(rparam, 4);
320 /* The next field contains the number of items left,
321 * including those returned in this buffer. So the
322 * first time through this should contain all of the
323 * entries.
325 if (total_cnt == 0) {
326 total_cnt = SVAL(rparam, 6);
329 /* Keep track of how many we have read */
330 return_cnt += count;
331 p = rdata;
333 /* The last name in the previous NetServerEnum reply is
334 * sent back to server in the NetServerEnum3 request
335 * (last_entry). The next reply should repeat this entry
336 * as the first element. We have no proof that this is
337 * always true, but from traces that seems to be the
338 * behavior from Window Servers. So first lets do a lot
339 * of checking, just being paranoid. If the string
340 * matches then we already saw this entry so skip it.
342 * NOTE: sv1_name field must be null terminated and has
343 * a max size of 16 (NetBIOS Name).
345 if (last_entry && count && p &&
346 (strncmp(last_entry, p, 16) == 0)) {
347 count -= 1; /* Skip this entry */
348 return_cnt = -1; /* Not part of total, so don't count. */
349 p = rdata + 26; /* Skip the whole record */
352 for (i = 0; i < count; i++, p += 26) {
353 int comment_offset;
354 const char *cmnt;
355 const char *p1;
356 char *s1, *s2;
357 TALLOC_CTX *frame = talloc_stackframe();
358 uint32_t entry_stype;
360 if (p + 26 > rdata_end) {
361 TALLOC_FREE(frame);
362 break;
365 sname = p;
366 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
367 cmnt = comment_offset?(rdata+comment_offset):"";
369 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
370 TALLOC_FREE(frame);
371 continue;
374 /* Work out the comment length. */
375 for (p1 = cmnt, len = 0; *p1 &&
376 p1 < rdata_end; len++)
377 p1++;
378 if (!*p1) {
379 len++;
382 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
384 pull_string_talloc(frame,rdata,0,
385 &s1,sname,16,STR_ASCII);
386 pull_string_talloc(frame,rdata,0,
387 &s2,cmnt,len,STR_ASCII);
389 if (!s1 || !s2) {
390 TALLOC_FREE(frame);
391 continue;
394 fn(s1, entry_stype, s2, state);
395 TALLOC_FREE(frame);
398 /* We are done with the old last entry, so now we can free it */
399 if (last_entry) {
400 SAFE_FREE(last_entry); /* This will set it to null */
403 /* We always make a copy of the last entry if we have one */
404 if (sname) {
405 last_entry = smb_xstrdup(sname);
408 /* If we have more data, but no last entry then error out */
409 if (!last_entry && (res == ERRmoredata)) {
410 errno = EINVAL;
411 res = 0;
416 SAFE_FREE(rparam);
417 SAFE_FREE(rdata);
418 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
420 SAFE_FREE(rparam);
421 SAFE_FREE(rdata);
422 SAFE_FREE(last_entry);
424 if (res == -1) {
425 errno = cli_errno(cli);
426 } else {
427 if (!return_cnt) {
428 /* this is a very special case, when the domain master for the
429 work group isn't part of the work group itself, there is something
430 wild going on */
431 errno = ENOENT;
435 return(return_cnt > 0);
438 /****************************************************************************
439 Send a SamOEMChangePassword command.
440 ****************************************************************************/
442 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
443 const char *old_password)
445 char param[1024];
446 unsigned char data[532];
447 char *p = param;
448 unsigned char old_pw_hash[16];
449 unsigned char new_pw_hash[16];
450 unsigned int data_len;
451 unsigned int param_len = 0;
452 char *rparam = NULL;
453 char *rdata = NULL;
454 unsigned int rprcnt, rdrcnt;
456 if (strlen(user) >= sizeof(fstring)-1) {
457 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
458 return False;
461 SSVAL(p,0,214); /* SamOEMChangePassword command. */
462 p += 2;
463 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
464 p = skip_string(param,sizeof(param),p);
465 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
466 p = skip_string(param,sizeof(param),p);
467 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
468 p = skip_string(param,sizeof(param),p);
469 SSVAL(p,0,532);
470 p += 2;
472 param_len = PTR_DIFF(p,param);
475 * Get the Lanman hash of the old password, we
476 * use this as the key to make_oem_passwd_hash().
478 E_deshash(old_password, old_pw_hash);
480 encode_pw_buffer(data, new_password, STR_ASCII);
482 #ifdef DEBUG_PASSWORD
483 DEBUG(100,("make_oem_passwd_hash\n"));
484 dump_data(100, data, 516);
485 #endif
486 SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
489 * Now place the old password hash in the data.
491 E_deshash(new_password, new_pw_hash);
493 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
495 data_len = 532;
497 if (cli_send_trans(cli,SMBtrans,
498 PIPE_LANMAN, /* name */
499 0,0, /* fid, flags */
500 NULL,0,0, /* setup, length, max */
501 param,param_len,2, /* param, length, max */
502 (char *)data,data_len,0 /* data, length, max */
503 ) == False) {
504 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
505 user ));
506 return False;
509 if (!cli_receive_trans(cli,SMBtrans,
510 &rparam, &rprcnt,
511 &rdata, &rdrcnt)) {
512 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
513 user ));
514 return False;
517 if (rparam) {
518 cli->rap_error = SVAL(rparam,0);
521 SAFE_FREE(rparam);
522 SAFE_FREE(rdata);
524 return (cli->rap_error == 0);
527 /****************************************************************************
528 Send a qpathinfo call.
529 ****************************************************************************/
531 bool cli_qpathinfo(struct cli_state *cli,
532 const char *fname,
533 time_t *change_time,
534 time_t *access_time,
535 time_t *write_time,
536 SMB_OFF_T *size,
537 uint16 *mode)
539 unsigned int data_len = 0;
540 unsigned int param_len = 0;
541 unsigned int rparam_len, rdata_len;
542 uint16 setup = TRANSACT2_QPATHINFO;
543 char *param;
544 char *rparam=NULL, *rdata=NULL;
545 int count=8;
546 bool ret;
547 time_t (*date_fn)(struct cli_state *, const void *);
548 char *p;
549 size_t nlen = 2*(strlen(fname)+1);
551 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
552 if (!param) {
553 return false;
555 p = param;
556 memset(p, '\0', 6);
557 SSVAL(p, 0, SMB_INFO_STANDARD);
558 p += 6;
559 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
560 param_len = PTR_DIFF(p, param);
562 do {
563 ret = (cli_send_trans(cli, SMBtrans2,
564 NULL, /* Name */
565 -1, 0, /* fid, flags */
566 &setup, 1, 0, /* setup, length, max */
567 param, param_len, 10, /* param, length, max */
568 NULL, data_len, cli->max_xmit /* data, length, max */
569 ) &&
570 cli_receive_trans(cli, SMBtrans2,
571 &rparam, &rparam_len,
572 &rdata, &rdata_len));
573 if (!cli_is_dos_error(cli)) break;
574 if (!ret) {
575 /* we need to work around a Win95 bug - sometimes
576 it gives ERRSRV/ERRerror temprarily */
577 uint8 eclass;
578 uint32 ecode;
579 cli_dos_error(cli, &eclass, &ecode);
580 if (eclass != ERRSRV || ecode != ERRerror) break;
581 smb_msleep(100);
583 } while (count-- && ret==False);
585 SAFE_FREE(param);
586 if (!ret || !rdata || rdata_len < 22) {
587 return False;
590 if (cli->win95) {
591 date_fn = cli_make_unix_date;
592 } else {
593 date_fn = cli_make_unix_date2;
596 if (change_time) {
597 *change_time = date_fn(cli, rdata+0);
599 if (access_time) {
600 *access_time = date_fn(cli, rdata+4);
602 if (write_time) {
603 *write_time = date_fn(cli, rdata+8);
605 if (size) {
606 *size = IVAL(rdata, 12);
608 if (mode) {
609 *mode = SVAL(rdata,l1_attrFile);
612 SAFE_FREE(rdata);
613 SAFE_FREE(rparam);
614 return True;
617 /****************************************************************************
618 Send a setpathinfo call.
619 ****************************************************************************/
621 bool cli_setpathinfo(struct cli_state *cli, const char *fname,
622 time_t create_time,
623 time_t access_time,
624 time_t write_time,
625 time_t change_time,
626 uint16 mode)
628 unsigned int data_len = 0;
629 unsigned int param_len = 0;
630 unsigned int rparam_len, rdata_len;
631 uint16 setup = TRANSACT2_SETPATHINFO;
632 char *param;
633 char data[40];
634 char *rparam=NULL, *rdata=NULL;
635 int count=8;
636 bool ret;
637 char *p;
638 size_t nlen = 2*(strlen(fname)+1);
640 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
641 if (!param) {
642 return false;
644 memset(param, '\0', 6);
645 memset(data, 0, sizeof(data));
647 p = param;
649 /* Add the information level */
650 SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
652 /* Skip reserved */
653 p += 6;
655 /* Add the file name */
656 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
658 param_len = PTR_DIFF(p, param);
660 p = data;
663 * Add the create, last access, modification, and status change times
665 put_long_date(p, create_time);
666 p += 8;
668 put_long_date(p, access_time);
669 p += 8;
671 put_long_date(p, write_time);
672 p += 8;
674 put_long_date(p, change_time);
675 p += 8;
677 /* Add attributes */
678 SIVAL(p, 0, mode);
679 p += 4;
681 /* Add padding */
682 SIVAL(p, 0, 0);
683 p += 4;
685 data_len = PTR_DIFF(p, data);
687 do {
688 ret = (cli_send_trans(cli, SMBtrans2,
689 NULL, /* Name */
690 -1, 0, /* fid, flags */
691 &setup, 1, 0, /* setup, length, max */
692 param, param_len, 10, /* param, length, max */
693 data, data_len, cli->max_xmit /* data, length, max */
694 ) &&
695 cli_receive_trans(cli, SMBtrans2,
696 &rparam, &rparam_len,
697 &rdata, &rdata_len));
698 if (!cli_is_dos_error(cli)) break;
699 if (!ret) {
700 /* we need to work around a Win95 bug - sometimes
701 it gives ERRSRV/ERRerror temprarily */
702 uint8 eclass;
703 uint32 ecode;
704 cli_dos_error(cli, &eclass, &ecode);
705 if (eclass != ERRSRV || ecode != ERRerror) break;
706 smb_msleep(100);
708 } while (count-- && ret==False);
710 SAFE_FREE(param);
711 if (!ret) {
712 return False;
715 SAFE_FREE(rdata);
716 SAFE_FREE(rparam);
717 return True;
720 /****************************************************************************
721 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
722 ****************************************************************************/
724 bool cli_qpathinfo2(struct cli_state *cli, const char *fname,
725 struct timespec *create_time,
726 struct timespec *access_time,
727 struct timespec *write_time,
728 struct timespec *change_time,
729 SMB_OFF_T *size, uint16 *mode,
730 SMB_INO_T *ino)
732 unsigned int data_len = 0;
733 unsigned int param_len = 0;
734 uint16 setup = TRANSACT2_QPATHINFO;
735 char *param;
736 char *rparam=NULL, *rdata=NULL;
737 char *p;
738 size_t nlen = 2*(strlen(fname)+1);
740 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
741 if (!param) {
742 return false;
744 p = param;
745 memset(param, '\0', 6);
746 SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
747 p += 6;
748 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
750 param_len = PTR_DIFF(p, param);
752 if (!cli_send_trans(cli, SMBtrans2,
753 NULL, /* name */
754 -1, 0, /* fid, flags */
755 &setup, 1, 0, /* setup, length, max */
756 param, param_len, 10, /* param, length, max */
757 NULL, data_len, cli->max_xmit /* data, length, max */
758 )) {
759 SAFE_FREE(param);
760 return False;
763 SAFE_FREE(param);
764 if (!cli_receive_trans(cli, SMBtrans2,
765 &rparam, &param_len,
766 &rdata, &data_len)) {
767 return False;
770 if (!rdata || data_len < 22) {
771 return False;
774 if (create_time) {
775 *create_time = interpret_long_date(rdata+0);
777 if (access_time) {
778 *access_time = interpret_long_date(rdata+8);
780 if (write_time) {
781 *write_time = interpret_long_date(rdata+16);
783 if (change_time) {
784 *change_time = interpret_long_date(rdata+24);
786 if (mode) {
787 *mode = SVAL(rdata, 32);
789 if (size) {
790 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
792 if (ino) {
793 *ino = IVAL(rdata, 64);
796 SAFE_FREE(rdata);
797 SAFE_FREE(rparam);
798 return True;
801 /****************************************************************************
802 Get the stream info
803 ****************************************************************************/
805 bool cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
806 TALLOC_CTX *mem_ctx,
807 unsigned int *pnum_streams,
808 struct stream_struct **pstreams)
810 unsigned int data_len = 0;
811 unsigned int param_len = 0;
812 uint16 setup = TRANSACT2_QPATHINFO;
813 char *param;
814 char *rparam=NULL, *rdata=NULL;
815 char *p;
816 unsigned int num_streams;
817 struct stream_struct *streams;
818 unsigned int ofs;
819 size_t namelen = 2*(strlen(fname)+1);
821 param = SMB_MALLOC_ARRAY(char, 6+namelen+2);
822 if (param == NULL) {
823 return false;
825 p = param;
826 memset(p, 0, 6);
827 SSVAL(p, 0, SMB_FILE_STREAM_INFORMATION);
828 p += 6;
829 p += clistr_push(cli, p, fname, namelen, STR_TERMINATE);
831 param_len = PTR_DIFF(p, param);
833 if (!cli_send_trans(cli, SMBtrans2,
834 NULL, /* name */
835 -1, 0, /* fid, flags */
836 &setup, 1, 0, /* setup, len, max */
837 param, param_len, 10, /* param, len, max */
838 NULL, data_len, cli->max_xmit /* data, len, max */
839 )) {
840 return false;
843 if (!cli_receive_trans(cli, SMBtrans2,
844 &rparam, &param_len,
845 &rdata, &data_len)) {
846 return false;
849 if (!rdata) {
850 SAFE_FREE(rparam);
851 return false;
854 num_streams = 0;
855 streams = NULL;
856 ofs = 0;
858 while ((data_len > ofs) && (data_len - ofs >= 24)) {
859 uint32_t nlen, len;
860 size_t size;
861 void *vstr;
862 struct stream_struct *tmp;
863 uint8_t *tmp_buf;
865 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
866 struct stream_struct,
867 num_streams+1);
869 if (tmp == NULL) {
870 goto fail;
872 streams = tmp;
874 nlen = IVAL(rdata, ofs + 0x04);
876 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
877 rdata, ofs + 0x08);
878 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
879 rdata, ofs + 0x10);
881 if (nlen > data_len - (ofs + 24)) {
882 goto fail;
886 * We need to null-terminate src, how do I do this with
887 * convert_string_talloc??
890 tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
891 if (tmp_buf == NULL) {
892 goto fail;
895 memcpy(tmp_buf, rdata+ofs+24, nlen);
896 tmp_buf[nlen] = 0;
897 tmp_buf[nlen+1] = 0;
899 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
900 nlen+2, &vstr, &size, false))
902 TALLOC_FREE(tmp_buf);
903 goto fail;
906 TALLOC_FREE(tmp_buf);
907 streams[num_streams].name = (char *)vstr;
908 num_streams++;
910 len = IVAL(rdata, ofs);
911 if (len > data_len - ofs) {
912 goto fail;
914 if (len == 0) break;
915 ofs += len;
918 SAFE_FREE(rdata);
919 SAFE_FREE(rparam);
921 *pnum_streams = num_streams;
922 *pstreams = streams;
923 return true;
925 fail:
926 TALLOC_FREE(streams);
927 SAFE_FREE(rdata);
928 SAFE_FREE(rparam);
929 return false;
932 /****************************************************************************
933 Send a qfileinfo QUERY_FILE_NAME_INFO call.
934 ****************************************************************************/
936 bool cli_qfilename(struct cli_state *cli, int fnum, char *name, size_t namelen)
938 unsigned int data_len = 0;
939 unsigned int param_len = 0;
940 uint16 setup = TRANSACT2_QFILEINFO;
941 char param[4];
942 char *rparam=NULL, *rdata=NULL;
944 param_len = 4;
945 SSVAL(param, 0, fnum);
946 SSVAL(param, 2, SMB_QUERY_FILE_NAME_INFO);
948 if (!cli_send_trans(cli, SMBtrans2,
949 NULL, /* name */
950 -1, 0, /* fid, flags */
951 &setup, 1, 0, /* setup, length, max */
952 param, param_len, 2, /* param, length, max */
953 NULL, data_len, cli->max_xmit /* data, length, max */
954 )) {
955 return False;
958 if (!cli_receive_trans(cli, SMBtrans2,
959 &rparam, &param_len,
960 &rdata, &data_len)) {
961 return False;
964 if (!rdata || data_len < 4) {
965 SAFE_FREE(rparam);
966 SAFE_FREE(rdata);
967 return False;
970 clistr_pull(cli->inbuf, name, rdata+4, namelen, IVAL(rdata, 0),
971 STR_UNICODE);
973 SAFE_FREE(rparam);
974 SAFE_FREE(rdata);
976 return True;
979 /****************************************************************************
980 Send a qfileinfo call.
981 ****************************************************************************/
983 bool cli_qfileinfo(struct cli_state *cli, int fnum,
984 uint16 *mode, SMB_OFF_T *size,
985 struct timespec *create_time,
986 struct timespec *access_time,
987 struct timespec *write_time,
988 struct timespec *change_time,
989 SMB_INO_T *ino)
991 unsigned int data_len = 0;
992 unsigned int param_len = 0;
993 uint16 setup;
994 uint8_t param[4];
995 uint8_t *rparam=NULL, *rdata=NULL;
996 NTSTATUS status;
998 /* if its a win95 server then fail this - win95 totally screws it
999 up */
1000 if (cli->win95) return False;
1002 param_len = 4;
1004 SSVAL(param, 0, fnum);
1005 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
1007 SSVAL(&setup, 0, TRANSACT2_QFILEINFO);
1009 status = cli_trans(talloc_tos(), cli, SMBtrans2,
1010 NULL, -1, 0, 0, /* name, fid, function, flags */
1011 &setup, 1, 0, /* setup, length, max */
1012 param, param_len, 2, /* param, length, max */
1013 NULL, 0, MIN(cli->max_xmit, 0xffff), /* data, length, max */
1014 NULL, NULL, /* rsetup, length */
1015 &rparam, &param_len, /* rparam, length */
1016 &rdata, &data_len);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 return false;
1022 if (!rdata || data_len < 68) {
1023 return False;
1026 if (create_time) {
1027 *create_time = interpret_long_date((char *)rdata+0);
1029 if (access_time) {
1030 *access_time = interpret_long_date((char *)rdata+8);
1032 if (write_time) {
1033 *write_time = interpret_long_date((char *)rdata+16);
1035 if (change_time) {
1036 *change_time = interpret_long_date((char *)rdata+24);
1038 if (mode) {
1039 *mode = SVAL(rdata, 32);
1041 if (size) {
1042 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1044 if (ino) {
1045 *ino = IVAL(rdata, 64);
1048 TALLOC_FREE(rdata);
1049 TALLOC_FREE(rparam);
1050 return True;
1053 /****************************************************************************
1054 Send a qpathinfo BASIC_INFO call.
1055 ****************************************************************************/
1057 bool cli_qpathinfo_basic( struct cli_state *cli, const char *name,
1058 SMB_STRUCT_STAT *sbuf, uint32 *attributes )
1060 unsigned int param_len = 0;
1061 unsigned int data_len = 0;
1062 uint16 setup = TRANSACT2_QPATHINFO;
1063 char *param;
1064 char *rparam=NULL, *rdata=NULL;
1065 char *p;
1066 char *path;
1067 int len;
1068 size_t nlen;
1069 TALLOC_CTX *frame = talloc_stackframe();
1071 path = talloc_strdup(frame, name);
1072 if (!path) {
1073 TALLOC_FREE(frame);
1074 return false;
1076 /* cleanup */
1078 len = strlen(path);
1079 if ( path[len-1] == '\\' || path[len-1] == '/') {
1080 path[len-1] = '\0';
1082 nlen = 2*(strlen(path)+1);
1084 param = TALLOC_ARRAY(frame,char,6+nlen+2);
1085 if (!param) {
1086 return false;
1088 p = param;
1089 memset(param, '\0', 6);
1091 SSVAL(p, 0, SMB_QUERY_FILE_BASIC_INFO);
1092 p += 6;
1093 p += clistr_push(cli, p, path, nlen, STR_TERMINATE);
1094 param_len = PTR_DIFF(p, param);
1097 if (!cli_send_trans(cli, SMBtrans2,
1098 NULL, /* name */
1099 -1, 0, /* fid, flags */
1100 &setup, 1, 0, /* setup, length, max */
1101 param, param_len, 2, /* param, length, max */
1102 NULL, 0, cli->max_xmit /* data, length, max */
1103 )) {
1104 TALLOC_FREE(frame);
1105 return False;
1108 TALLOC_FREE(frame);
1110 if (!cli_receive_trans(cli, SMBtrans2,
1111 &rparam, &param_len,
1112 &rdata, &data_len)) {
1113 return False;
1116 if (data_len < 36) {
1117 SAFE_FREE(rdata);
1118 SAFE_FREE(rparam);
1119 return False;
1122 set_atimespec(sbuf, interpret_long_date( rdata+8 )); /* Access time. */
1123 set_mtimespec(sbuf, interpret_long_date( rdata+16 )); /* Write time. */
1124 set_ctimespec(sbuf, interpret_long_date( rdata+24 )); /* Change time. */
1126 *attributes = IVAL( rdata, 32 );
1128 SAFE_FREE(rparam);
1129 SAFE_FREE(rdata);
1131 return True;
1134 /****************************************************************************
1135 Send a qfileinfo call.
1136 ****************************************************************************/
1138 bool cli_qfileinfo_test(struct cli_state *cli, int fnum, int level, char **poutdata, uint32 *poutlen)
1140 unsigned int data_len = 0;
1141 unsigned int param_len = 0;
1142 uint16 setup = TRANSACT2_QFILEINFO;
1143 char param[4];
1144 char *rparam=NULL, *rdata=NULL;
1146 *poutdata = NULL;
1147 *poutlen = 0;
1149 /* if its a win95 server then fail this - win95 totally screws it
1150 up */
1151 if (cli->win95)
1152 return False;
1154 param_len = 4;
1156 SSVAL(param, 0, fnum);
1157 SSVAL(param, 2, level);
1159 if (!cli_send_trans(cli, SMBtrans2,
1160 NULL, /* name */
1161 -1, 0, /* fid, flags */
1162 &setup, 1, 0, /* setup, length, max */
1163 param, param_len, 2, /* param, length, max */
1164 NULL, data_len, cli->max_xmit /* data, length, max */
1165 )) {
1166 return False;
1169 if (!cli_receive_trans(cli, SMBtrans2,
1170 &rparam, &param_len,
1171 &rdata, &data_len)) {
1172 return False;
1175 *poutdata = (char *)memdup(rdata, data_len);
1176 if (!*poutdata) {
1177 SAFE_FREE(rdata);
1178 SAFE_FREE(rparam);
1179 return False;
1182 *poutlen = data_len;
1184 SAFE_FREE(rdata);
1185 SAFE_FREE(rparam);
1186 return True;
1189 /****************************************************************************
1190 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1191 ****************************************************************************/
1193 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1195 unsigned int data_len = 0;
1196 unsigned int param_len = 0;
1197 uint16 setup = TRANSACT2_QPATHINFO;
1198 char *param;
1199 char *rparam=NULL, *rdata=NULL;
1200 int count=8;
1201 char *p;
1202 bool ret;
1203 unsigned int len;
1204 size_t nlen = 2*(strlen(fname)+1);
1206 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
1207 if (!param) {
1208 return NT_STATUS_NO_MEMORY;
1210 p = param;
1211 memset(param, '\0', 6);
1212 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
1213 p += 6;
1214 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
1215 param_len = PTR_DIFF(p, param);
1217 do {
1218 ret = (cli_send_trans(cli, SMBtrans2,
1219 NULL, /* Name */
1220 -1, 0, /* fid, flags */
1221 &setup, 1, 0, /* setup, length, max */
1222 param, param_len, 10, /* param, length, max */
1223 NULL, data_len, cli->max_xmit /* data, length, max */
1224 ) &&
1225 cli_receive_trans(cli, SMBtrans2,
1226 &rparam, &param_len,
1227 &rdata, &data_len));
1228 if (!ret && cli_is_dos_error(cli)) {
1229 /* we need to work around a Win95 bug - sometimes
1230 it gives ERRSRV/ERRerror temprarily */
1231 uint8 eclass;
1232 uint32 ecode;
1233 cli_dos_error(cli, &eclass, &ecode);
1234 if (eclass != ERRSRV || ecode != ERRerror) break;
1235 smb_msleep(100);
1237 } while (count-- && ret==False);
1239 SAFE_FREE(param);
1241 if (!ret || !rdata || data_len < 4) {
1242 return NT_STATUS_UNSUCCESSFUL;
1245 len = IVAL(rdata, 0);
1247 if (len > data_len - 4) {
1248 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1251 clistr_pull(cli->inbuf, alt_name, rdata+4, sizeof(fstring), len,
1252 STR_UNICODE);
1254 SAFE_FREE(rdata);
1255 SAFE_FREE(rparam);
1257 return NT_STATUS_OK;