s3:libsmb: fix NetServerEnum3 rap calls.
[Samba.git] / source / libsmb / clirap.c
blob116e0a9175916374e9daaecb1950973e25db29c1
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 on an arbitrary pipe. takes param, data and setup buffers.
26 ****************************************************************************/
28 bool 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)
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);
42 return (cli_receive_trans(cli, SMBtrans,
43 rparam, (unsigned int *)rparam_count,
44 rdata, (unsigned int *)rdata_count));
47 /****************************************************************************
48 Call a remote api
49 ****************************************************************************/
51 bool 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)
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 */
65 return (cli_receive_trans(cli,SMBtrans,
66 rparam, rprcnt,
67 rdata, rdrcnt));
70 /****************************************************************************
71 Perform a NetWkstaUserLogon.
72 ****************************************************************************/
74 bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
76 char *rparam = NULL;
77 char *rdata = NULL;
78 char *p;
79 unsigned int rdrcnt,rprcnt;
80 char param[1024];
82 memset(param, 0, sizeof(param));
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;
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;
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));
127 SAFE_FREE(rparam);
128 SAFE_FREE(rdata);
129 return (cli->rap_error == 0);
132 /****************************************************************************
133 Call a NetShareEnum - try and browse available connections on a host.
134 ****************************************************************************/
136 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
138 char *rparam = NULL;
139 char *rdata = NULL;
140 char *p;
141 unsigned int rdrcnt,rprcnt;
142 char param[1024];
143 int count = -1;
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);
155 * Win2k needs a *smaller* buffer than 0xFFFF here -
156 * it returns "out of server memory" with 0xFFFF !!! JRA.
158 SSVAL(p,2,0xFFE0);
159 p += 4;
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 */
167 int res = rparam? SVAL(rparam,0) : -1;
169 if (res == 0 || res == ERRmoredata) {
170 int converter=SVAL(rparam,2);
171 int i;
172 char *rdata_end = rdata + rdrcnt;
174 count=SVAL(rparam,4);
175 p = rdata;
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();
187 if (p + 20 > rdata_end) {
188 TALLOC_FREE(frame);
189 break;
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;
200 cmnt = comment_offset?(rdata+comment_offset):"";
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++;
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;
218 fn(s1, type, s2, state);
220 TALLOC_FREE(frame);
222 } else {
223 DEBUG(4,("NetShareEnum res=%d\n", res));
225 } else {
226 DEBUG(4,("NetShareEnum failed\n"));
229 SAFE_FREE(rparam);
230 SAFE_FREE(rdata);
232 return count;
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.
239 The callback function takes 4 arguments: the machine name, the server type,
240 the comment and a state pointer.
241 ****************************************************************************/
243 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
244 void (*fn)(const char *, uint32, const char *, void *),
245 void *state)
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;
261 errno = 0; /* reset */
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.
268 do {
269 /* send a SMBtrans command with api NetServerEnum */
270 p = param;
271 SIVAL(p,0,func); /* api number */
272 p += 2;
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));
280 p = skip_string(param, sizeof(param), p);
281 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
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;
290 /* If we have more data, tell the server where
291 * to continue from.
293 len = push_ascii(p,
294 workgroup,
295 sizeof(param) - PTR_DIFF(p,param) - 1,
296 STR_TERMINATE|STR_UPPER);
298 if (len == (size_t)-1) {
299 SAFE_FREE(last_entry);
300 return false;
302 p += len;
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);
310 if (len == (size_t)-1) {
311 SAFE_FREE(last_entry);
312 return false;
314 p += len;
317 /* Next time through we need to use the continue api */
318 func = RAP_NetServerEnum3;
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 */
326 /* break out of the loop on error */
327 res = -1;
328 break;
331 rdata_end = rdata + rdrcnt;
332 res = rparam ? SVAL(rparam,0) : -1;
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);
340 /* Get the number of items returned in this buffer */
341 count = SVAL(rparam, 4);
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.
348 if (total_cnt == 0) {
349 total_cnt = SVAL(rparam, 6);
352 /* Keep track of how many we have read */
353 return_cnt += count;
354 p = rdata;
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.
365 * NOTE: sv1_name field must be null terminated and has
366 * a max size of 16 (NetBIOS Name).
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 */
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;
383 if (p + 26 > rdata_end) {
384 TALLOC_FREE(frame);
385 break;
388 sname = p;
389 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
390 cmnt = comment_offset?(rdata+comment_offset):"";
392 if (comment_offset < 0 || comment_offset > (int)rdrcnt) {
393 TALLOC_FREE(frame);
394 continue;
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++;
405 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
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);
412 if (!s1 || !s2) {
413 TALLOC_FREE(frame);
414 continue;
417 fn(s1, entry_stype, s2, state);
418 TALLOC_FREE(frame);
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 */
426 /* We always make a copy of the last entry if we have one */
427 if (sname) {
428 last_entry = smb_xstrdup(sname);
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;
439 SAFE_FREE(rparam);
440 SAFE_FREE(rdata);
441 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
443 SAFE_FREE(rparam);
444 SAFE_FREE(rdata);
445 SAFE_FREE(last_entry);
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;
458 return(return_cnt > 0);
461 /****************************************************************************
462 Send a SamOEMChangePassword command.
463 ****************************************************************************/
465 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
466 const char *old_password)
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;
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;
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;
495 param_len = PTR_DIFF(p,param);
498 * Get the Lanman hash of the old password, we
499 * use this as the key to make_oem_passwd_hash().
501 E_deshash(old_password, old_pw_hash);
503 encode_pw_buffer(data, new_password, STR_ASCII);
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);
512 * Now place the old password hash in the data.
514 E_deshash(new_password, new_pw_hash);
516 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
518 data_len = 532;
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;
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;
540 if (rparam) {
541 cli->rap_error = SVAL(rparam,0);
544 SAFE_FREE(rparam);
545 SAFE_FREE(rdata);
547 return (cli->rap_error == 0);
550 /****************************************************************************
551 Send a qpathinfo call.
552 ****************************************************************************/
554 bool 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)
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);
574 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
575 if (!param) {
576 return false;
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);
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);
606 } while (count-- && ret==False);
608 SAFE_FREE(param);
609 if (!ret || !rdata || rdata_len < 22) {
610 return False;
613 if (cli->win95) {
614 date_fn = cli_make_unix_date;
615 } else {
616 date_fn = cli_make_unix_date2;
619 if (change_time) {
620 *change_time = date_fn(cli, rdata+0);
622 if (access_time) {
623 *access_time = date_fn(cli, rdata+4);
625 if (write_time) {
626 *write_time = date_fn(cli, rdata+8);
628 if (size) {
629 *size = IVAL(rdata, 12);
631 if (mode) {
632 *mode = SVAL(rdata,l1_attrFile);
635 SAFE_FREE(rdata);
636 SAFE_FREE(rparam);
637 return True;
640 /****************************************************************************
641 Send a setpathinfo call.
642 ****************************************************************************/
644 bool 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)
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);
663 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
664 if (!param) {
665 return false;
667 memset(param, '\0', 6);
668 memset(data, 0, sizeof(data));
670 p = param;
672 /* Add the information level */
673 SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
675 /* Skip reserved */
676 p += 6;
678 /* Add the file name */
679 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
681 param_len = PTR_DIFF(p, param);
683 p = data;
686 * Add the create, last access, modification, and status change times
688 put_long_date(p, create_time);
689 p += 8;
691 put_long_date(p, access_time);
692 p += 8;
694 put_long_date(p, write_time);
695 p += 8;
697 put_long_date(p, change_time);
698 p += 8;
700 /* Add attributes */
701 SIVAL(p, 0, mode);
702 p += 4;
704 /* Add padding */
705 SIVAL(p, 0, 0);
706 p += 4;
708 data_len = PTR_DIFF(p, data);
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);
731 } while (count-- && ret==False);
733 SAFE_FREE(param);
734 if (!ret) {
735 return False;
738 SAFE_FREE(rdata);
739 SAFE_FREE(rparam);
740 return True;
743 /****************************************************************************
744 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
745 ****************************************************************************/
747 bool 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)
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);
763 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
764 if (!param) {
765 return false;
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);
773 param_len = PTR_DIFF(p, param);
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;
786 SAFE_FREE(param);
787 if (!cli_receive_trans(cli, SMBtrans2,
788 &rparam, &param_len,
789 &rdata, &data_len)) {
790 return False;
793 if (!rdata || data_len < 22) {
794 return False;
797 if (create_time) {
798 *create_time = interpret_long_date(rdata+0);
800 if (access_time) {
801 *access_time = interpret_long_date(rdata+8);
803 if (write_time) {
804 *write_time = interpret_long_date(rdata+16);
806 if (change_time) {
807 *change_time = interpret_long_date(rdata+24);
809 if (mode) {
810 *mode = SVAL(rdata, 32);
812 if (size) {
813 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
815 if (ino) {
816 *ino = IVAL(rdata, 64);
819 SAFE_FREE(rdata);
820 SAFE_FREE(rparam);
821 return True;
824 /****************************************************************************
825 Get the stream info
826 ****************************************************************************/
828 bool 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)
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);
844 param = SMB_MALLOC_ARRAY(char, 6+namelen+2);
845 if (param == NULL) {
846 return false;
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);
854 param_len = PTR_DIFF(p, param);
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;
866 if (!cli_receive_trans(cli, SMBtrans2,
867 &rparam, &param_len,
868 &rdata, &data_len)) {
869 return false;
872 if (!rdata) {
873 SAFE_FREE(rparam);
874 return false;
877 num_streams = 0;
878 streams = NULL;
879 ofs = 0;
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;
888 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
889 struct stream_struct,
890 num_streams+1);
892 if (tmp == NULL) {
893 goto fail;
895 streams = tmp;
897 nlen = IVAL(rdata, ofs + 0x04);
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);
904 if (nlen > data_len - (ofs + 24)) {
905 goto fail;
909 * We need to null-terminate src, how do I do this with
910 * convert_string_talloc??
913 tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
914 if (tmp_buf == NULL) {
915 goto fail;
918 memcpy(tmp_buf, rdata+ofs+24, nlen);
919 tmp_buf[nlen] = 0;
920 tmp_buf[nlen+1] = 0;
922 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
923 nlen+2, &vstr, &size, false))
925 TALLOC_FREE(tmp_buf);
926 goto fail;
929 TALLOC_FREE(tmp_buf);
930 streams[num_streams].name = (char *)vstr;
931 num_streams++;
933 len = IVAL(rdata, ofs);
934 if (len > data_len - ofs) {
935 goto fail;
937 if (len == 0) break;
938 ofs += len;
941 SAFE_FREE(rdata);
942 SAFE_FREE(rparam);
944 *pnum_streams = num_streams;
945 *pstreams = streams;
946 return true;
948 fail:
949 TALLOC_FREE(streams);
950 SAFE_FREE(rdata);
951 SAFE_FREE(rparam);
952 return false;
955 /****************************************************************************
956 Send a qfileinfo QUERY_FILE_NAME_INFO call.
957 ****************************************************************************/
959 bool cli_qfilename(struct cli_state *cli, int fnum, char *name, size_t namelen)
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;
967 param_len = 4;
968 SSVAL(param, 0, fnum);
969 SSVAL(param, 2, SMB_QUERY_FILE_NAME_INFO);
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;
981 if (!cli_receive_trans(cli, SMBtrans2,
982 &rparam, &param_len,
983 &rdata, &data_len)) {
984 return False;
987 if (!rdata || data_len < 4) {
988 return False;
991 clistr_pull(cli, name, rdata+4, namelen, IVAL(rdata, 0), STR_UNICODE);
993 return True;
996 /****************************************************************************
997 Send a qfileinfo call.
998 ****************************************************************************/
1000 bool 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)
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;
1014 /* if its a win95 server then fail this - win95 totally screws it
1015 up */
1016 if (cli->win95) return False;
1018 param_len = 4;
1020 SSVAL(param, 0, fnum);
1021 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
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;
1033 if (!cli_receive_trans(cli, SMBtrans2,
1034 &rparam, &param_len,
1035 &rdata, &data_len)) {
1036 return False;
1039 if (!rdata || data_len < 68) {
1040 return False;
1043 if (create_time) {
1044 *create_time = interpret_long_date(rdata+0);
1046 if (access_time) {
1047 *access_time = interpret_long_date(rdata+8);
1049 if (write_time) {
1050 *write_time = interpret_long_date(rdata+16);
1052 if (change_time) {
1053 *change_time = interpret_long_date(rdata+24);
1055 if (mode) {
1056 *mode = SVAL(rdata, 32);
1058 if (size) {
1059 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1061 if (ino) {
1062 *ino = IVAL(rdata, 64);
1065 SAFE_FREE(rdata);
1066 SAFE_FREE(rparam);
1067 return True;
1070 /****************************************************************************
1071 Send a qpathinfo BASIC_INFO call.
1072 ****************************************************************************/
1074 bool cli_qpathinfo_basic( struct cli_state *cli, const char *name,
1075 SMB_STRUCT_STAT *sbuf, uint32 *attributes )
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();
1088 path = talloc_strdup(frame, name);
1089 if (!path) {
1090 TALLOC_FREE(frame);
1091 return false;
1093 /* cleanup */
1095 len = strlen(path);
1096 if ( path[len-1] == '\\' || path[len-1] == '/') {
1097 path[len-1] = '\0';
1099 nlen = 2*(strlen(path)+1);
1101 param = TALLOC_ARRAY(frame,char,6+nlen+2);
1102 if (!param) {
1103 return false;
1105 p = param;
1106 memset(param, '\0', 6);
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);
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;
1125 TALLOC_FREE(frame);
1127 if (!cli_receive_trans(cli, SMBtrans2,
1128 &rparam, &param_len,
1129 &rdata, &data_len)) {
1130 return False;
1133 if (data_len < 36) {
1134 SAFE_FREE(rdata);
1135 SAFE_FREE(rparam);
1136 return False;
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. */
1143 *attributes = IVAL( rdata, 32 );
1145 SAFE_FREE(rparam);
1146 SAFE_FREE(rdata);
1148 return True;
1151 /****************************************************************************
1152 Send a qfileinfo call.
1153 ****************************************************************************/
1155 bool cli_qfileinfo_test(struct cli_state *cli, int fnum, int level, char **poutdata, uint32 *poutlen)
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;
1163 *poutdata = NULL;
1164 *poutlen = 0;
1166 /* if its a win95 server then fail this - win95 totally screws it
1167 up */
1168 if (cli->win95)
1169 return False;
1171 param_len = 4;
1173 SSVAL(param, 0, fnum);
1174 SSVAL(param, 2, level);
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;
1186 if (!cli_receive_trans(cli, SMBtrans2,
1187 &rparam, &param_len,
1188 &rdata, &data_len)) {
1189 return False;
1192 *poutdata = (char *)memdup(rdata, data_len);
1193 if (!*poutdata) {
1194 SAFE_FREE(rdata);
1195 SAFE_FREE(rparam);
1196 return False;
1199 *poutlen = data_len;
1201 SAFE_FREE(rdata);
1202 SAFE_FREE(rparam);
1203 return True;
1206 /****************************************************************************
1207 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1208 ****************************************************************************/
1210 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
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);
1223 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
1224 if (!param) {
1225 return NT_STATUS_NO_MEMORY;
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);
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);
1254 } while (count-- && ret==False);
1256 SAFE_FREE(param);
1258 if (!ret || !rdata || data_len < 4) {
1259 return NT_STATUS_UNSUCCESSFUL;
1262 len = IVAL(rdata, 0);
1264 if (len > data_len - 4) {
1265 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1268 clistr_pull(cli, alt_name, rdata+4, sizeof(fstring), len, STR_UNICODE);
1270 SAFE_FREE(rdata);
1271 SAFE_FREE(rparam);
1273 return NT_STATUS_OK;