Fix crash when get_gpo_info returns incorrect data.
[Samba.git] / source3 / libsmb / clirap.c
blobbe4cc0a8fb035ddc7a1a45a6b266949f5f07f597
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"
23 #include "../libcli/auth/libcli_auth.h"
24 #include "../librpc/gen_ndr/rap.h"
25 #include "../lib/crypto/arcfour.h"
27 /****************************************************************************
28 Call a remote api
29 ****************************************************************************/
31 bool cli_api(struct cli_state *cli,
32 char *param, int prcnt, int mprcnt,
33 char *data, int drcnt, int mdrcnt,
34 char **rparam, unsigned int *rprcnt,
35 char **rdata, unsigned int *rdrcnt)
37 cli_send_trans(cli,SMBtrans,
38 PIPE_LANMAN, /* Name */
39 0,0, /* fid, flags */
40 NULL,0,0, /* Setup, length, max */
41 param, prcnt, mprcnt, /* Params, length, max */
42 data, drcnt, mdrcnt /* Data, length, max */
45 return (cli_receive_trans(cli,SMBtrans,
46 rparam, rprcnt,
47 rdata, rdrcnt));
50 /****************************************************************************
51 Perform a NetWkstaUserLogon.
52 ****************************************************************************/
54 bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
56 char *rparam = NULL;
57 char *rdata = NULL;
58 char *p;
59 unsigned int rdrcnt,rprcnt;
60 char param[1024];
62 memset(param, 0, sizeof(param));
64 /* send a SMBtrans command with api NetWkstaUserLogon */
65 p = param;
66 SSVAL(p,0,132); /* api number */
67 p += 2;
68 strlcpy(p,"OOWb54WrLh",sizeof(param)-PTR_DIFF(p,param));
69 p = skip_string(param,sizeof(param),p);
70 strlcpy(p,"WB21BWDWWDDDDDDDzzzD",sizeof(param)-PTR_DIFF(p,param));
71 p = skip_string(param,sizeof(param),p);
72 SSVAL(p,0,1);
73 p += 2;
74 strlcpy(p,user,sizeof(param)-PTR_DIFF(p,param));
75 strupper_m(p);
76 p += 21;
77 p++;
78 p += 15;
79 p++;
80 strlcpy(p, workstation,sizeof(param)-PTR_DIFF(p,param));
81 strupper_m(p);
82 p += 16;
83 SSVAL(p, 0, CLI_BUFFER_SIZE);
84 p += 2;
85 SSVAL(p, 0, CLI_BUFFER_SIZE);
86 p += 2;
88 if (cli_api(cli,
89 param, PTR_DIFF(p,param),1024, /* param, length, max */
90 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
91 &rparam, &rprcnt, /* return params, return size */
92 &rdata, &rdrcnt /* return data, return size */
93 )) {
94 cli->rap_error = rparam? SVAL(rparam,0) : -1;
95 p = rdata;
97 if (cli->rap_error == 0) {
98 DEBUG(4,("NetWkstaUserLogon success\n"));
99 cli->privileges = SVAL(p, 24);
100 /* The cli->eff_name field used to be set here
101 but it wasn't used anywhere else. */
102 } else {
103 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
107 SAFE_FREE(rparam);
108 SAFE_FREE(rdata);
109 return (cli->rap_error == 0);
112 /****************************************************************************
113 Call a NetShareEnum - try and browse available connections on a host.
114 ****************************************************************************/
116 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
118 char *rparam = NULL;
119 char *rdata = NULL;
120 char *p;
121 unsigned int rdrcnt,rprcnt;
122 char param[1024];
123 int count = -1;
125 /* now send a SMBtrans command with api RNetShareEnum */
126 p = param;
127 SSVAL(p,0,0); /* api number */
128 p += 2;
129 strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
130 p = skip_string(param,sizeof(param),p);
131 strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
132 p = skip_string(param,sizeof(param),p);
133 SSVAL(p,0,1);
135 * Win2k needs a *smaller* buffer than 0xFFFF here -
136 * it returns "out of server memory" with 0xFFFF !!! JRA.
138 SSVAL(p,2,0xFFE0);
139 p += 4;
141 if (cli_api(cli,
142 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
143 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
144 &rparam, &rprcnt, /* return params, length */
145 &rdata, &rdrcnt)) /* return data, length */
147 int res = rparam? SVAL(rparam,0) : -1;
149 if (res == 0 || res == ERRmoredata) {
150 int converter=SVAL(rparam,2);
151 int i;
152 char *rdata_end = rdata + rdrcnt;
154 count=SVAL(rparam,4);
155 p = rdata;
157 for (i=0;i<count;i++,p+=20) {
158 char *sname;
159 int type;
160 int comment_offset;
161 const char *cmnt;
162 const char *p1;
163 char *s1, *s2;
164 size_t len;
165 TALLOC_CTX *frame = talloc_stackframe();
167 if (p + 20 > rdata_end) {
168 TALLOC_FREE(frame);
169 break;
172 sname = p;
173 type = SVAL(p,14);
174 comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
175 if (comment_offset < 0 ||
176 comment_offset > (int)rdrcnt) {
177 TALLOC_FREE(frame);
178 break;
180 cmnt = comment_offset?(rdata+comment_offset):"";
182 /* Work out the comment length. */
183 for (p1 = cmnt, len = 0; *p1 &&
184 p1 < rdata_end; len++)
185 p1++;
186 if (!*p1) {
187 len++;
189 pull_string_talloc(frame,rdata,0,
190 &s1,sname,14,STR_ASCII);
191 pull_string_talloc(frame,rdata,0,
192 &s2,cmnt,len,STR_ASCII);
193 if (!s1 || !s2) {
194 TALLOC_FREE(frame);
195 continue;
198 fn(s1, type, s2, state);
200 TALLOC_FREE(frame);
202 } else {
203 DEBUG(4,("NetShareEnum res=%d\n", res));
205 } else {
206 DEBUG(4,("NetShareEnum failed\n"));
209 SAFE_FREE(rparam);
210 SAFE_FREE(rdata);
212 return count;
215 /****************************************************************************
216 Call a NetServerEnum for the specified workgroup and servertype mask. This
217 function then calls the specified callback function for each name returned.
219 The callback function takes 4 arguments: the machine name, the server type,
220 the comment and a state pointer.
221 ****************************************************************************/
223 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
224 void (*fn)(const char *, uint32, const char *, void *),
225 void *state)
227 char *rparam = NULL;
228 char *rdata = NULL;
229 char *rdata_end = NULL;
230 unsigned int rdrcnt,rprcnt;
231 char *p;
232 char param[1024];
233 int uLevel = 1;
234 size_t len;
235 uint32 func = RAP_NetServerEnum2;
236 char *last_entry = NULL;
237 int total_cnt = 0;
238 int return_cnt = 0;
239 int res;
241 errno = 0; /* reset */
244 * This may take more than one transaction, so we should loop until
245 * we no longer get a more data to process or we have all of the
246 * items.
248 do {
249 /* send a SMBtrans command with api NetServerEnum */
250 p = param;
251 SIVAL(p,0,func); /* api number */
252 p += 2;
254 if (func == RAP_NetServerEnum3) {
255 strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
256 } else {
257 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
260 p = skip_string(param, sizeof(param), p);
261 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
263 p = skip_string(param, sizeof(param), p);
264 SSVAL(p,0,uLevel);
265 SSVAL(p,2,CLI_BUFFER_SIZE);
266 p += 4;
267 SIVAL(p,0,stype);
268 p += 4;
270 /* If we have more data, tell the server where
271 * to continue from.
273 len = push_ascii(p,
274 workgroup,
275 sizeof(param) - PTR_DIFF(p,param) - 1,
276 STR_TERMINATE|STR_UPPER);
278 if (len == (size_t)-1) {
279 SAFE_FREE(last_entry);
280 return false;
282 p += len;
284 if (func == RAP_NetServerEnum3) {
285 len = push_ascii(p,
286 last_entry ? last_entry : "",
287 sizeof(param) - PTR_DIFF(p,param) - 1,
288 STR_TERMINATE);
290 if (len == (size_t)-1) {
291 SAFE_FREE(last_entry);
292 return false;
294 p += len;
297 /* Next time through we need to use the continue api */
298 func = RAP_NetServerEnum3;
300 if (!cli_api(cli,
301 param, PTR_DIFF(p,param), 8, /* params, length, max */
302 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
303 &rparam, &rprcnt, /* return params, return size */
304 &rdata, &rdrcnt)) { /* return data, return size */
306 /* break out of the loop on error */
307 res = -1;
308 break;
311 rdata_end = rdata + rdrcnt;
312 res = rparam ? SVAL(rparam,0) : -1;
314 if (res == 0 || res == ERRmoredata ||
315 (res != -1 && cli_errno(cli) == 0)) {
316 char *sname = NULL;
317 int i, count;
318 int converter=SVAL(rparam,2);
320 /* Get the number of items returned in this buffer */
321 count = SVAL(rparam, 4);
323 /* The next field contains the number of items left,
324 * including those returned in this buffer. So the
325 * first time through this should contain all of the
326 * entries.
328 if (total_cnt == 0) {
329 total_cnt = SVAL(rparam, 6);
332 /* Keep track of how many we have read */
333 return_cnt += count;
334 p = rdata;
336 /* The last name in the previous NetServerEnum reply is
337 * sent back to server in the NetServerEnum3 request
338 * (last_entry). The next reply should repeat this entry
339 * as the first element. We have no proof that this is
340 * always true, but from traces that seems to be the
341 * behavior from Window Servers. So first lets do a lot
342 * of checking, just being paranoid. If the string
343 * matches then we already saw this entry so skip it.
345 * NOTE: sv1_name field must be null terminated and has
346 * a max size of 16 (NetBIOS Name).
348 if (last_entry && count && p &&
349 (strncmp(last_entry, p, 16) == 0)) {
350 count -= 1; /* Skip this entry */
351 return_cnt = -1; /* Not part of total, so don't count. */
352 p = rdata + 26; /* Skip the whole record */
355 for (i = 0; i < count; i++, p += 26) {
356 int comment_offset;
357 const char *cmnt;
358 const char *p1;
359 char *s1, *s2;
360 TALLOC_CTX *frame = talloc_stackframe();
361 uint32_t entry_stype;
363 if (p + 26 > rdata_end) {
364 TALLOC_FREE(frame);
365 break;
368 sname = p;
369 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
370 cmnt = comment_offset?(rdata+comment_offset):"";
372 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
373 TALLOC_FREE(frame);
374 continue;
377 /* Work out the comment length. */
378 for (p1 = cmnt, len = 0; *p1 &&
379 p1 < rdata_end; len++)
380 p1++;
381 if (!*p1) {
382 len++;
385 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
387 pull_string_talloc(frame,rdata,0,
388 &s1,sname,16,STR_ASCII);
389 pull_string_talloc(frame,rdata,0,
390 &s2,cmnt,len,STR_ASCII);
392 if (!s1 || !s2) {
393 TALLOC_FREE(frame);
394 continue;
397 fn(s1, entry_stype, s2, state);
398 TALLOC_FREE(frame);
401 /* We are done with the old last entry, so now we can free it */
402 if (last_entry) {
403 SAFE_FREE(last_entry); /* This will set it to null */
406 /* We always make a copy of the last entry if we have one */
407 if (sname) {
408 last_entry = smb_xstrdup(sname);
411 /* If we have more data, but no last entry then error out */
412 if (!last_entry && (res == ERRmoredata)) {
413 errno = EINVAL;
414 res = 0;
419 SAFE_FREE(rparam);
420 SAFE_FREE(rdata);
421 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
423 SAFE_FREE(rparam);
424 SAFE_FREE(rdata);
425 SAFE_FREE(last_entry);
427 if (res == -1) {
428 errno = cli_errno(cli);
429 } else {
430 if (!return_cnt) {
431 /* this is a very special case, when the domain master for the
432 work group isn't part of the work group itself, there is something
433 wild going on */
434 errno = ENOENT;
438 return(return_cnt > 0);
441 /****************************************************************************
442 Send a SamOEMChangePassword command.
443 ****************************************************************************/
445 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
446 const char *old_password)
448 char param[1024];
449 unsigned char data[532];
450 char *p = param;
451 unsigned char old_pw_hash[16];
452 unsigned char new_pw_hash[16];
453 unsigned int data_len;
454 unsigned int param_len = 0;
455 char *rparam = NULL;
456 char *rdata = NULL;
457 unsigned int rprcnt, rdrcnt;
459 if (strlen(user) >= sizeof(fstring)-1) {
460 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
461 return False;
464 SSVAL(p,0,214); /* SamOEMChangePassword command. */
465 p += 2;
466 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
467 p = skip_string(param,sizeof(param),p);
468 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
469 p = skip_string(param,sizeof(param),p);
470 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
471 p = skip_string(param,sizeof(param),p);
472 SSVAL(p,0,532);
473 p += 2;
475 param_len = PTR_DIFF(p,param);
478 * Get the Lanman hash of the old password, we
479 * use this as the key to make_oem_passwd_hash().
481 E_deshash(old_password, old_pw_hash);
483 encode_pw_buffer(data, new_password, STR_ASCII);
485 #ifdef DEBUG_PASSWORD
486 DEBUG(100,("make_oem_passwd_hash\n"));
487 dump_data(100, data, 516);
488 #endif
489 arcfour_crypt( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
492 * Now place the old password hash in the data.
494 E_deshash(new_password, new_pw_hash);
496 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
498 data_len = 532;
500 if (cli_send_trans(cli,SMBtrans,
501 PIPE_LANMAN, /* name */
502 0,0, /* fid, flags */
503 NULL,0,0, /* setup, length, max */
504 param,param_len,4, /* param, length, max */
505 (char *)data,data_len,0 /* data, length, max */
506 ) == False) {
507 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
508 user ));
509 return False;
512 if (!cli_receive_trans(cli,SMBtrans,
513 &rparam, &rprcnt,
514 &rdata, &rdrcnt)) {
515 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
516 user ));
517 return False;
520 if (rparam) {
521 cli->rap_error = SVAL(rparam,0);
524 SAFE_FREE(rparam);
525 SAFE_FREE(rdata);
527 return (cli->rap_error == 0);
530 /****************************************************************************
531 Send a qpathinfo call.
532 ****************************************************************************/
534 bool cli_qpathinfo(struct cli_state *cli,
535 const char *fname,
536 time_t *change_time,
537 time_t *access_time,
538 time_t *write_time,
539 SMB_OFF_T *size,
540 uint16 *mode)
542 unsigned int data_len = 0;
543 unsigned int param_len = 0;
544 unsigned int rparam_len, rdata_len;
545 uint16 setup = TRANSACT2_QPATHINFO;
546 char *param;
547 char *rparam=NULL, *rdata=NULL;
548 int count=8;
549 bool ret;
550 time_t (*date_fn)(struct cli_state *, const void *);
551 char *p;
552 size_t nlen = 2*(strlen(fname)+1);
554 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
555 if (!param) {
556 return false;
558 p = param;
559 memset(p, '\0', 6);
560 SSVAL(p, 0, SMB_INFO_STANDARD);
561 p += 6;
562 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
563 param_len = PTR_DIFF(p, param);
565 do {
566 ret = (cli_send_trans(cli, SMBtrans2,
567 NULL, /* Name */
568 -1, 0, /* fid, flags */
569 &setup, 1, 0, /* setup, length, max */
570 param, param_len, 10, /* param, length, max */
571 NULL, data_len, cli->max_xmit /* data, length, max */
572 ) &&
573 cli_receive_trans(cli, SMBtrans2,
574 &rparam, &rparam_len,
575 &rdata, &rdata_len));
576 if (!cli_is_dos_error(cli)) break;
577 if (!ret) {
578 /* we need to work around a Win95 bug - sometimes
579 it gives ERRSRV/ERRerror temprarily */
580 uint8 eclass;
581 uint32 ecode;
582 cli_dos_error(cli, &eclass, &ecode);
583 if (eclass != ERRSRV || ecode != ERRerror) break;
584 smb_msleep(100);
586 } while (count-- && ret==False);
588 SAFE_FREE(param);
589 if (!ret || !rdata || rdata_len < 22) {
590 return False;
593 if (cli->win95) {
594 date_fn = cli_make_unix_date;
595 } else {
596 date_fn = cli_make_unix_date2;
599 if (change_time) {
600 *change_time = date_fn(cli, rdata+0);
602 if (access_time) {
603 *access_time = date_fn(cli, rdata+4);
605 if (write_time) {
606 *write_time = date_fn(cli, rdata+8);
608 if (size) {
609 *size = IVAL(rdata, 12);
611 if (mode) {
612 *mode = SVAL(rdata,l1_attrFile);
615 SAFE_FREE(rdata);
616 SAFE_FREE(rparam);
617 return True;
620 /****************************************************************************
621 Send a setpathinfo call.
622 ****************************************************************************/
624 bool cli_setpathinfo(struct cli_state *cli, const char *fname,
625 time_t create_time,
626 time_t access_time,
627 time_t write_time,
628 time_t change_time,
629 uint16 mode)
631 unsigned int data_len = 0;
632 unsigned int param_len = 0;
633 unsigned int rparam_len, rdata_len;
634 uint16 setup = TRANSACT2_SETPATHINFO;
635 char *param;
636 char data[40];
637 char *rparam=NULL, *rdata=NULL;
638 int count=8;
639 bool ret;
640 char *p;
641 size_t nlen = 2*(strlen(fname)+1);
643 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
644 if (!param) {
645 return false;
647 memset(param, '\0', 6);
648 memset(data, 0, sizeof(data));
650 p = param;
652 /* Add the information level */
653 SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
655 /* Skip reserved */
656 p += 6;
658 /* Add the file name */
659 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
661 param_len = PTR_DIFF(p, param);
663 p = data;
666 * Add the create, last access, modification, and status change times
668 put_long_date(p, create_time);
669 p += 8;
671 put_long_date(p, access_time);
672 p += 8;
674 put_long_date(p, write_time);
675 p += 8;
677 put_long_date(p, change_time);
678 p += 8;
680 /* Add attributes */
681 SIVAL(p, 0, mode);
682 p += 4;
684 /* Add padding */
685 SIVAL(p, 0, 0);
686 p += 4;
688 data_len = PTR_DIFF(p, data);
690 do {
691 ret = (cli_send_trans(cli, SMBtrans2,
692 NULL, /* Name */
693 -1, 0, /* fid, flags */
694 &setup, 1, 0, /* setup, length, max */
695 param, param_len, 10, /* param, length, max */
696 data, data_len, cli->max_xmit /* data, length, max */
697 ) &&
698 cli_receive_trans(cli, SMBtrans2,
699 &rparam, &rparam_len,
700 &rdata, &rdata_len));
701 if (!cli_is_dos_error(cli)) break;
702 if (!ret) {
703 /* we need to work around a Win95 bug - sometimes
704 it gives ERRSRV/ERRerror temprarily */
705 uint8 eclass;
706 uint32 ecode;
707 cli_dos_error(cli, &eclass, &ecode);
708 if (eclass != ERRSRV || ecode != ERRerror) break;
709 smb_msleep(100);
711 } while (count-- && ret==False);
713 SAFE_FREE(param);
714 if (!ret) {
715 return False;
718 SAFE_FREE(rdata);
719 SAFE_FREE(rparam);
720 return True;
723 /****************************************************************************
724 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
725 ****************************************************************************/
727 bool cli_qpathinfo2(struct cli_state *cli, const char *fname,
728 struct timespec *create_time,
729 struct timespec *access_time,
730 struct timespec *write_time,
731 struct timespec *change_time,
732 SMB_OFF_T *size, uint16 *mode,
733 SMB_INO_T *ino)
735 unsigned int data_len = 0;
736 unsigned int param_len = 0;
737 uint16 setup = TRANSACT2_QPATHINFO;
738 char *param;
739 char *rparam=NULL, *rdata=NULL;
740 char *p;
741 size_t nlen = 2*(strlen(fname)+1);
743 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
744 if (!param) {
745 return false;
747 p = param;
748 memset(param, '\0', 6);
749 SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
750 p += 6;
751 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
753 param_len = PTR_DIFF(p, param);
755 if (!cli_send_trans(cli, SMBtrans2,
756 NULL, /* name */
757 -1, 0, /* fid, flags */
758 &setup, 1, 0, /* setup, length, max */
759 param, param_len, 10, /* param, length, max */
760 NULL, data_len, cli->max_xmit /* data, length, max */
761 )) {
762 SAFE_FREE(param);
763 return False;
766 SAFE_FREE(param);
767 if (!cli_receive_trans(cli, SMBtrans2,
768 &rparam, &param_len,
769 &rdata, &data_len)) {
770 return False;
773 if (!rdata || data_len < 22) {
774 return False;
777 if (create_time) {
778 *create_time = interpret_long_date(rdata+0);
780 if (access_time) {
781 *access_time = interpret_long_date(rdata+8);
783 if (write_time) {
784 *write_time = interpret_long_date(rdata+16);
786 if (change_time) {
787 *change_time = interpret_long_date(rdata+24);
789 if (mode) {
790 *mode = SVAL(rdata, 32);
792 if (size) {
793 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
795 if (ino) {
796 *ino = IVAL(rdata, 64);
799 SAFE_FREE(rdata);
800 SAFE_FREE(rparam);
801 return True;
804 /****************************************************************************
805 Get the stream info
806 ****************************************************************************/
808 bool cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
809 TALLOC_CTX *mem_ctx,
810 unsigned int *pnum_streams,
811 struct stream_struct **pstreams)
813 unsigned int data_len = 0;
814 unsigned int param_len = 0;
815 uint16 setup = TRANSACT2_QPATHINFO;
816 char *param;
817 char *rparam=NULL, *rdata=NULL;
818 char *p;
819 unsigned int num_streams;
820 struct stream_struct *streams;
821 unsigned int ofs;
822 size_t namelen = 2*(strlen(fname)+1);
824 param = SMB_MALLOC_ARRAY(char, 6+namelen+2);
825 if (param == NULL) {
826 return false;
828 p = param;
829 memset(p, 0, 6);
830 SSVAL(p, 0, SMB_FILE_STREAM_INFORMATION);
831 p += 6;
832 p += clistr_push(cli, p, fname, namelen, STR_TERMINATE);
834 param_len = PTR_DIFF(p, param);
836 if (!cli_send_trans(cli, SMBtrans2,
837 NULL, /* name */
838 -1, 0, /* fid, flags */
839 &setup, 1, 0, /* setup, len, max */
840 param, param_len, 10, /* param, len, max */
841 NULL, data_len, cli->max_xmit /* data, len, max */
842 )) {
843 return false;
846 if (!cli_receive_trans(cli, SMBtrans2,
847 &rparam, &param_len,
848 &rdata, &data_len)) {
849 return false;
852 if (!rdata) {
853 SAFE_FREE(rparam);
854 return false;
857 num_streams = 0;
858 streams = NULL;
859 ofs = 0;
861 while ((data_len > ofs) && (data_len - ofs >= 24)) {
862 uint32_t nlen, len;
863 size_t size;
864 void *vstr;
865 struct stream_struct *tmp;
866 uint8_t *tmp_buf;
868 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
869 struct stream_struct,
870 num_streams+1);
872 if (tmp == NULL) {
873 goto fail;
875 streams = tmp;
877 nlen = IVAL(rdata, ofs + 0x04);
879 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
880 rdata, ofs + 0x08);
881 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
882 rdata, ofs + 0x10);
884 if (nlen > data_len - (ofs + 24)) {
885 goto fail;
889 * We need to null-terminate src, how do I do this with
890 * convert_string_talloc??
893 tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
894 if (tmp_buf == NULL) {
895 goto fail;
898 memcpy(tmp_buf, rdata+ofs+24, nlen);
899 tmp_buf[nlen] = 0;
900 tmp_buf[nlen+1] = 0;
902 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
903 nlen+2, &vstr, &size, false))
905 TALLOC_FREE(tmp_buf);
906 goto fail;
909 TALLOC_FREE(tmp_buf);
910 streams[num_streams].name = (char *)vstr;
911 num_streams++;
913 len = IVAL(rdata, ofs);
914 if (len > data_len - ofs) {
915 goto fail;
917 if (len == 0) break;
918 ofs += len;
921 SAFE_FREE(rdata);
922 SAFE_FREE(rparam);
924 *pnum_streams = num_streams;
925 *pstreams = streams;
926 return true;
928 fail:
929 TALLOC_FREE(streams);
930 SAFE_FREE(rdata);
931 SAFE_FREE(rparam);
932 return false;
935 /****************************************************************************
936 Send a qfileinfo QUERY_FILE_NAME_INFO call.
937 ****************************************************************************/
939 bool cli_qfilename(struct cli_state *cli, uint16_t fnum, char *name, size_t namelen)
941 unsigned int data_len = 0;
942 unsigned int param_len = 0;
943 uint16 setup = TRANSACT2_QFILEINFO;
944 char param[4];
945 char *rparam=NULL, *rdata=NULL;
947 param_len = 4;
948 SSVAL(param, 0, fnum);
949 SSVAL(param, 2, SMB_QUERY_FILE_NAME_INFO);
951 if (!cli_send_trans(cli, SMBtrans2,
952 NULL, /* name */
953 -1, 0, /* fid, flags */
954 &setup, 1, 0, /* setup, length, max */
955 param, param_len, 2, /* param, length, max */
956 NULL, data_len, cli->max_xmit /* data, length, max */
957 )) {
958 return False;
961 if (!cli_receive_trans(cli, SMBtrans2,
962 &rparam, &param_len,
963 &rdata, &data_len)) {
964 return False;
967 if (!rdata || data_len < 4) {
968 SAFE_FREE(rparam);
969 SAFE_FREE(rdata);
970 return False;
973 clistr_pull(cli->inbuf, name, rdata+4, namelen, IVAL(rdata, 0),
974 STR_UNICODE);
976 SAFE_FREE(rparam);
977 SAFE_FREE(rdata);
979 return True;
982 /****************************************************************************
983 Send a qfileinfo call.
984 ****************************************************************************/
986 bool cli_qfileinfo(struct cli_state *cli, uint16_t fnum,
987 uint16 *mode, SMB_OFF_T *size,
988 struct timespec *create_time,
989 struct timespec *access_time,
990 struct timespec *write_time,
991 struct timespec *change_time,
992 SMB_INO_T *ino)
994 uint32_t data_len = 0;
995 uint16 setup;
996 uint8_t param[4];
997 uint8_t *rdata=NULL;
998 NTSTATUS status;
1000 /* if its a win95 server then fail this - win95 totally screws it
1001 up */
1002 if (cli->win95) return False;
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, 4, 2, /* param, length, max */
1013 NULL, 0, MIN(cli->max_xmit, 0xffff), /* data, length, max */
1014 NULL, 0, NULL, /* rsetup, length */
1015 NULL, 0, NULL, /* rparam, length */
1016 &rdata, 68, &data_len);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 return false;
1022 if (create_time) {
1023 *create_time = interpret_long_date((char *)rdata+0);
1025 if (access_time) {
1026 *access_time = interpret_long_date((char *)rdata+8);
1028 if (write_time) {
1029 *write_time = interpret_long_date((char *)rdata+16);
1031 if (change_time) {
1032 *change_time = interpret_long_date((char *)rdata+24);
1034 if (mode) {
1035 *mode = SVAL(rdata, 32);
1037 if (size) {
1038 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1040 if (ino) {
1041 *ino = IVAL(rdata, 64);
1044 TALLOC_FREE(rdata);
1045 return True;
1048 /****************************************************************************
1049 Send a qpathinfo BASIC_INFO call.
1050 ****************************************************************************/
1052 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
1053 SMB_STRUCT_STAT *sbuf, uint32 *attributes)
1055 unsigned int param_len = 0;
1056 uint32_t rdata_len;
1057 uint16_t setup[1];
1058 uint8_t *param, *rdata;
1059 uint8_t *p;
1060 char *path;
1061 int len;
1062 size_t nlen;
1063 TALLOC_CTX *frame = talloc_stackframe();
1064 NTSTATUS status;
1066 path = talloc_strdup(frame, name);
1067 if (!path) {
1068 TALLOC_FREE(frame);
1069 return NT_STATUS_NO_MEMORY;
1071 /* cleanup */
1073 len = strlen(path);
1074 if ( path[len-1] == '\\' || path[len-1] == '/') {
1075 path[len-1] = '\0';
1077 nlen = 2*(strlen(path)+1);
1079 param = TALLOC_ARRAY(frame, uint8_t, 6+nlen+2);
1080 if (!param) {
1081 TALLOC_FREE(frame);
1082 return NT_STATUS_NO_MEMORY;
1084 p = param;
1085 memset(param, '\0', 6);
1087 SSVAL(setup, 0, TRANSACT2_QPATHINFO);
1088 SSVAL(p, 0, SMB_QUERY_FILE_BASIC_INFO);
1089 p += 6;
1090 p += clistr_push(cli, p, path, nlen, STR_TERMINATE);
1091 param_len = PTR_DIFF(p, param);
1093 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
1094 setup, 1, 0,
1095 param, param_len, 2,
1096 NULL, 0, cli->max_xmit,
1097 NULL, 0, NULL,
1098 NULL, 0, NULL,
1099 &rdata, 36, &rdata_len);
1100 if (!NT_STATUS_IS_OK(status)) {
1101 TALLOC_FREE(frame);
1102 return status;
1105 sbuf->st_ex_atime = interpret_long_date((char *)rdata+8);
1106 sbuf->st_ex_mtime = interpret_long_date((char *)rdata+16);
1107 sbuf->st_ex_ctime = interpret_long_date((char *)rdata+24);
1109 *attributes = IVAL( rdata, 32 );
1111 TALLOC_FREE(rdata);
1113 return NT_STATUS_OK;
1116 /****************************************************************************
1117 Send a qfileinfo call.
1118 ****************************************************************************/
1120 bool cli_qfileinfo_test(struct cli_state *cli, uint16_t fnum, int level, char **poutdata, uint32 *poutlen)
1122 unsigned int data_len = 0;
1123 unsigned int param_len = 0;
1124 uint16 setup = TRANSACT2_QFILEINFO;
1125 char param[4];
1126 char *rparam=NULL, *rdata=NULL;
1128 *poutdata = NULL;
1129 *poutlen = 0;
1131 /* if its a win95 server then fail this - win95 totally screws it
1132 up */
1133 if (cli->win95)
1134 return False;
1136 param_len = 4;
1138 SSVAL(param, 0, fnum);
1139 SSVAL(param, 2, level);
1141 if (!cli_send_trans(cli, SMBtrans2,
1142 NULL, /* name */
1143 -1, 0, /* fid, flags */
1144 &setup, 1, 0, /* setup, length, max */
1145 param, param_len, 2, /* param, length, max */
1146 NULL, data_len, cli->max_xmit /* data, length, max */
1147 )) {
1148 return False;
1151 if (!cli_receive_trans(cli, SMBtrans2,
1152 &rparam, &param_len,
1153 &rdata, &data_len)) {
1154 return False;
1157 *poutdata = (char *)memdup(rdata, data_len);
1158 if (!*poutdata) {
1159 SAFE_FREE(rdata);
1160 SAFE_FREE(rparam);
1161 return False;
1164 *poutlen = data_len;
1166 SAFE_FREE(rdata);
1167 SAFE_FREE(rparam);
1168 return True;
1171 /****************************************************************************
1172 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1173 ****************************************************************************/
1175 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1177 unsigned int data_len = 0;
1178 unsigned int param_len = 0;
1179 uint16 setup = TRANSACT2_QPATHINFO;
1180 char *param;
1181 char *rparam=NULL, *rdata=NULL;
1182 int count=8;
1183 char *p;
1184 bool ret;
1185 unsigned int len;
1186 size_t nlen = 2*(strlen(fname)+1);
1188 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
1189 if (!param) {
1190 return NT_STATUS_NO_MEMORY;
1192 p = param;
1193 memset(param, '\0', 6);
1194 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
1195 p += 6;
1196 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
1197 param_len = PTR_DIFF(p, param);
1199 do {
1200 ret = (cli_send_trans(cli, SMBtrans2,
1201 NULL, /* Name */
1202 -1, 0, /* fid, flags */
1203 &setup, 1, 0, /* setup, length, max */
1204 param, param_len, 10, /* param, length, max */
1205 NULL, data_len, cli->max_xmit /* data, length, max */
1206 ) &&
1207 cli_receive_trans(cli, SMBtrans2,
1208 &rparam, &param_len,
1209 &rdata, &data_len));
1210 if (!ret && cli_is_dos_error(cli)) {
1211 /* we need to work around a Win95 bug - sometimes
1212 it gives ERRSRV/ERRerror temprarily */
1213 uint8 eclass;
1214 uint32 ecode;
1215 cli_dos_error(cli, &eclass, &ecode);
1216 if (eclass != ERRSRV || ecode != ERRerror) break;
1217 smb_msleep(100);
1219 } while (count-- && ret==False);
1221 SAFE_FREE(param);
1223 if (!ret || !rdata || data_len < 4) {
1224 return NT_STATUS_UNSUCCESSFUL;
1227 len = IVAL(rdata, 0);
1229 if (len > data_len - 4) {
1230 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1233 clistr_pull(cli->inbuf, alt_name, rdata+4, sizeof(fstring), len,
1234 STR_UNICODE);
1236 SAFE_FREE(rdata);
1237 SAFE_FREE(rparam);
1239 return NT_STATUS_OK;