s3: Fix an uninitialized variable reference
[Samba.git] / source / libsmb / clirap.c
blobd248d0cdd1bec15070db8c81fb49ccd4f5266ebb
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;
273 /* Next time through we need to use the continue api */
274 func = RAP_NetServerEnum3;
276 if (last_entry) {
277 strlcpy(p,"WrLehDOz", sizeof(param)-PTR_DIFF(p,param));
278 } else {
279 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
282 p = skip_string(param, sizeof(param), p);
283 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
285 p = skip_string(param, sizeof(param), p);
286 SSVAL(p,0,uLevel);
287 SSVAL(p,2,CLI_BUFFER_SIZE);
288 p += 4;
289 SIVAL(p,0,stype);
290 p += 4;
292 /* If we have more data, tell the server where
293 * to continue from.
295 len = push_ascii(p,
296 last_entry ? last_entry : workgroup,
297 sizeof(param) - PTR_DIFF(p,param) - 1,
298 STR_TERMINATE|STR_UPPER);
300 if (len == (size_t)-1) {
301 SAFE_FREE(last_entry);
302 return false;
304 p += len;
306 if (!cli_api(cli,
307 param, PTR_DIFF(p,param), 8, /* params, length, max */
308 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
309 &rparam, &rprcnt, /* return params, return size */
310 &rdata, &rdrcnt)) { /* return data, return size */
312 /* break out of the loop on error */
313 res = -1;
314 break;
317 rdata_end = rdata + rdrcnt;
318 res = rparam ? SVAL(rparam,0) : -1;
320 if (res == 0 || res == ERRmoredata ||
321 (res != -1 && cli_errno(cli) == 0)) {
322 char *sname = NULL;
323 int i, count;
324 int converter=SVAL(rparam,2);
326 /* Get the number of items returned in this buffer */
327 count = SVAL(rparam, 4);
329 /* The next field contains the number of items left,
330 * including those returned in this buffer. So the
331 * first time through this should contain all of the
332 * entries.
334 if (total_cnt == 0) {
335 total_cnt = SVAL(rparam, 6);
338 /* Keep track of how many we have read */
339 return_cnt += count;
340 p = rdata;
342 /* The last name in the previous NetServerEnum reply is
343 * sent back to server in the NetServerEnum3 request
344 * (last_entry). The next reply should repeat this entry
345 * as the first element. We have no proof that this is
346 * always true, but from traces that seems to be the
347 * behavior from Window Servers. So first lets do a lot
348 * of checking, just being paranoid. If the string
349 * matches then we already saw this entry so skip it.
351 * NOTE: sv1_name field must be null terminated and has
352 * a max size of 16 (NetBIOS Name).
354 if (last_entry && count && p &&
355 (strncmp(last_entry, p, 16) == 0)) {
356 count -= 1; /* Skip this entry */
357 return_cnt = -1; /* Not part of total, so don't count. */
358 p = rdata + 26; /* Skip the whole record */
361 for (i = 0; i < count; i++, p += 26) {
362 int comment_offset;
363 const char *cmnt;
364 const char *p1;
365 char *s1, *s2;
366 TALLOC_CTX *frame = talloc_stackframe();
367 uint32_t entry_stype;
369 if (p + 26 > rdata_end) {
370 TALLOC_FREE(frame);
371 break;
374 sname = p;
375 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
376 cmnt = comment_offset?(rdata+comment_offset):"";
378 if (comment_offset < 0 || comment_offset > (int)rdrcnt) {
379 TALLOC_FREE(frame);
380 continue;
383 /* Work out the comment length. */
384 for (p1 = cmnt, len = 0; *p1 &&
385 p1 < rdata_end; len++)
386 p1++;
387 if (!*p1) {
388 len++;
391 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
393 pull_string_talloc(frame,rdata,0,
394 &s1,sname,16,STR_ASCII);
395 pull_string_talloc(frame,rdata,0,
396 &s2,cmnt,len,STR_ASCII);
398 if (!s1 || !s2) {
399 TALLOC_FREE(frame);
400 continue;
403 fn(s1, entry_stype, s2, state);
404 TALLOC_FREE(frame);
407 /* We are done with the old last entry, so now we can free it */
408 if (last_entry) {
409 SAFE_FREE(last_entry); /* This will set it to null */
412 /* We always make a copy of the last entry if we have one */
413 if (sname) {
414 last_entry = smb_xstrdup(sname);
417 /* If we have more data, but no last entry then error out */
418 if (!last_entry && (res == ERRmoredata)) {
419 errno = EINVAL;
420 res = 0;
425 SAFE_FREE(rparam);
426 SAFE_FREE(rdata);
427 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
429 SAFE_FREE(rparam);
430 SAFE_FREE(rdata);
431 SAFE_FREE(last_entry);
433 if (res == -1) {
434 errno = cli_errno(cli);
435 } else {
436 if (!return_cnt) {
437 /* this is a very special case, when the domain master for the
438 work group isn't part of the work group itself, there is something
439 wild going on */
440 errno = ENOENT;
444 return(return_cnt > 0);
447 /****************************************************************************
448 Send a SamOEMChangePassword command.
449 ****************************************************************************/
451 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
452 const char *old_password)
454 char param[1024];
455 unsigned char data[532];
456 char *p = param;
457 unsigned char old_pw_hash[16];
458 unsigned char new_pw_hash[16];
459 unsigned int data_len;
460 unsigned int param_len = 0;
461 char *rparam = NULL;
462 char *rdata = NULL;
463 unsigned int rprcnt, rdrcnt;
465 if (strlen(user) >= sizeof(fstring)-1) {
466 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
467 return False;
470 SSVAL(p,0,214); /* SamOEMChangePassword command. */
471 p += 2;
472 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
473 p = skip_string(param,sizeof(param),p);
474 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
475 p = skip_string(param,sizeof(param),p);
476 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
477 p = skip_string(param,sizeof(param),p);
478 SSVAL(p,0,532);
479 p += 2;
481 param_len = PTR_DIFF(p,param);
484 * Get the Lanman hash of the old password, we
485 * use this as the key to make_oem_passwd_hash().
487 E_deshash(old_password, old_pw_hash);
489 encode_pw_buffer(data, new_password, STR_ASCII);
491 #ifdef DEBUG_PASSWORD
492 DEBUG(100,("make_oem_passwd_hash\n"));
493 dump_data(100, data, 516);
494 #endif
495 SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
498 * Now place the old password hash in the data.
500 E_deshash(new_password, new_pw_hash);
502 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
504 data_len = 532;
506 if (cli_send_trans(cli,SMBtrans,
507 PIPE_LANMAN, /* name */
508 0,0, /* fid, flags */
509 NULL,0,0, /* setup, length, max */
510 param,param_len,2, /* param, length, max */
511 (char *)data,data_len,0 /* data, length, max */
512 ) == False) {
513 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
514 user ));
515 return False;
518 if (!cli_receive_trans(cli,SMBtrans,
519 &rparam, &rprcnt,
520 &rdata, &rdrcnt)) {
521 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
522 user ));
523 return False;
526 if (rparam) {
527 cli->rap_error = SVAL(rparam,0);
530 SAFE_FREE(rparam);
531 SAFE_FREE(rdata);
533 return (cli->rap_error == 0);
536 /****************************************************************************
537 Send a qpathinfo call.
538 ****************************************************************************/
540 bool cli_qpathinfo(struct cli_state *cli,
541 const char *fname,
542 time_t *change_time,
543 time_t *access_time,
544 time_t *write_time,
545 SMB_OFF_T *size,
546 uint16 *mode)
548 unsigned int data_len = 0;
549 unsigned int param_len = 0;
550 unsigned int rparam_len, rdata_len;
551 uint16 setup = TRANSACT2_QPATHINFO;
552 char *param;
553 char *rparam=NULL, *rdata=NULL;
554 int count=8;
555 bool ret;
556 time_t (*date_fn)(struct cli_state *, const void *);
557 char *p;
558 size_t nlen = 2*(strlen(fname)+1);
560 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
561 if (!param) {
562 return false;
564 p = param;
565 memset(p, '\0', 6);
566 SSVAL(p, 0, SMB_INFO_STANDARD);
567 p += 6;
568 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
569 param_len = PTR_DIFF(p, param);
571 do {
572 ret = (cli_send_trans(cli, SMBtrans2,
573 NULL, /* Name */
574 -1, 0, /* fid, flags */
575 &setup, 1, 0, /* setup, length, max */
576 param, param_len, 10, /* param, length, max */
577 NULL, data_len, cli->max_xmit /* data, length, max */
578 ) &&
579 cli_receive_trans(cli, SMBtrans2,
580 &rparam, &rparam_len,
581 &rdata, &rdata_len));
582 if (!cli_is_dos_error(cli)) break;
583 if (!ret) {
584 /* we need to work around a Win95 bug - sometimes
585 it gives ERRSRV/ERRerror temprarily */
586 uint8 eclass;
587 uint32 ecode;
588 cli_dos_error(cli, &eclass, &ecode);
589 if (eclass != ERRSRV || ecode != ERRerror) break;
590 smb_msleep(100);
592 } while (count-- && ret==False);
594 SAFE_FREE(param);
595 if (!ret || !rdata || rdata_len < 22) {
596 return False;
599 if (cli->win95) {
600 date_fn = cli_make_unix_date;
601 } else {
602 date_fn = cli_make_unix_date2;
605 if (change_time) {
606 *change_time = date_fn(cli, rdata+0);
608 if (access_time) {
609 *access_time = date_fn(cli, rdata+4);
611 if (write_time) {
612 *write_time = date_fn(cli, rdata+8);
614 if (size) {
615 *size = IVAL(rdata, 12);
617 if (mode) {
618 *mode = SVAL(rdata,l1_attrFile);
621 SAFE_FREE(rdata);
622 SAFE_FREE(rparam);
623 return True;
626 /****************************************************************************
627 Send a setpathinfo call.
628 ****************************************************************************/
630 bool cli_setpathinfo(struct cli_state *cli, const char *fname,
631 time_t create_time,
632 time_t access_time,
633 time_t write_time,
634 time_t change_time,
635 uint16 mode)
637 unsigned int data_len = 0;
638 unsigned int param_len = 0;
639 unsigned int rparam_len, rdata_len;
640 uint16 setup = TRANSACT2_SETPATHINFO;
641 char *param;
642 char data[40];
643 char *rparam=NULL, *rdata=NULL;
644 int count=8;
645 bool ret;
646 char *p;
647 size_t nlen = 2*(strlen(fname)+1);
649 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
650 if (!param) {
651 return false;
653 memset(param, '\0', 6);
654 memset(data, 0, sizeof(data));
656 p = param;
658 /* Add the information level */
659 SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
661 /* Skip reserved */
662 p += 6;
664 /* Add the file name */
665 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
667 param_len = PTR_DIFF(p, param);
669 p = data;
672 * Add the create, last access, modification, and status change times
674 put_long_date(p, create_time);
675 p += 8;
677 put_long_date(p, access_time);
678 p += 8;
680 put_long_date(p, write_time);
681 p += 8;
683 put_long_date(p, change_time);
684 p += 8;
686 /* Add attributes */
687 SIVAL(p, 0, mode);
688 p += 4;
690 /* Add padding */
691 SIVAL(p, 0, 0);
692 p += 4;
694 data_len = PTR_DIFF(p, data);
696 do {
697 ret = (cli_send_trans(cli, SMBtrans2,
698 NULL, /* Name */
699 -1, 0, /* fid, flags */
700 &setup, 1, 0, /* setup, length, max */
701 param, param_len, 10, /* param, length, max */
702 data, data_len, cli->max_xmit /* data, length, max */
703 ) &&
704 cli_receive_trans(cli, SMBtrans2,
705 &rparam, &rparam_len,
706 &rdata, &rdata_len));
707 if (!cli_is_dos_error(cli)) break;
708 if (!ret) {
709 /* we need to work around a Win95 bug - sometimes
710 it gives ERRSRV/ERRerror temprarily */
711 uint8 eclass;
712 uint32 ecode;
713 cli_dos_error(cli, &eclass, &ecode);
714 if (eclass != ERRSRV || ecode != ERRerror) break;
715 smb_msleep(100);
717 } while (count-- && ret==False);
719 SAFE_FREE(param);
720 if (!ret) {
721 return False;
724 SAFE_FREE(rdata);
725 SAFE_FREE(rparam);
726 return True;
729 /****************************************************************************
730 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
731 ****************************************************************************/
733 bool cli_qpathinfo2(struct cli_state *cli, const char *fname,
734 struct timespec *create_time,
735 struct timespec *access_time,
736 struct timespec *write_time,
737 struct timespec *change_time,
738 SMB_OFF_T *size, uint16 *mode,
739 SMB_INO_T *ino)
741 unsigned int data_len = 0;
742 unsigned int param_len = 0;
743 uint16 setup = TRANSACT2_QPATHINFO;
744 char *param;
745 char *rparam=NULL, *rdata=NULL;
746 char *p;
747 size_t nlen = 2*(strlen(fname)+1);
749 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
750 if (!param) {
751 return false;
753 p = param;
754 memset(param, '\0', 6);
755 SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
756 p += 6;
757 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
759 param_len = PTR_DIFF(p, param);
761 if (!cli_send_trans(cli, SMBtrans2,
762 NULL, /* name */
763 -1, 0, /* fid, flags */
764 &setup, 1, 0, /* setup, length, max */
765 param, param_len, 10, /* param, length, max */
766 NULL, data_len, cli->max_xmit /* data, length, max */
767 )) {
768 SAFE_FREE(param);
769 return False;
772 SAFE_FREE(param);
773 if (!cli_receive_trans(cli, SMBtrans2,
774 &rparam, &param_len,
775 &rdata, &data_len)) {
776 return False;
779 if (!rdata || data_len < 22) {
780 return False;
783 if (create_time) {
784 *create_time = interpret_long_date(rdata+0);
786 if (access_time) {
787 *access_time = interpret_long_date(rdata+8);
789 if (write_time) {
790 *write_time = interpret_long_date(rdata+16);
792 if (change_time) {
793 *change_time = interpret_long_date(rdata+24);
795 if (mode) {
796 *mode = SVAL(rdata, 32);
798 if (size) {
799 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
801 if (ino) {
802 *ino = IVAL(rdata, 64);
805 SAFE_FREE(rdata);
806 SAFE_FREE(rparam);
807 return True;
810 /****************************************************************************
811 Get the stream info
812 ****************************************************************************/
814 bool cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
815 TALLOC_CTX *mem_ctx,
816 unsigned int *pnum_streams,
817 struct stream_struct **pstreams)
819 unsigned int data_len = 0;
820 unsigned int param_len = 0;
821 uint16 setup = TRANSACT2_QPATHINFO;
822 char *param;
823 char *rparam=NULL, *rdata=NULL;
824 char *p;
825 unsigned int num_streams;
826 struct stream_struct *streams;
827 unsigned int ofs;
828 size_t namelen = 2*(strlen(fname)+1);
830 param = SMB_MALLOC_ARRAY(char, 6+namelen+2);
831 if (param == NULL) {
832 return false;
834 p = param;
835 memset(p, 0, 6);
836 SSVAL(p, 0, SMB_FILE_STREAM_INFORMATION);
837 p += 6;
838 p += clistr_push(cli, p, fname, namelen, STR_TERMINATE);
840 param_len = PTR_DIFF(p, param);
842 if (!cli_send_trans(cli, SMBtrans2,
843 NULL, /* name */
844 -1, 0, /* fid, flags */
845 &setup, 1, 0, /* setup, len, max */
846 param, param_len, 10, /* param, len, max */
847 NULL, data_len, cli->max_xmit /* data, len, max */
848 )) {
849 return false;
852 if (!cli_receive_trans(cli, SMBtrans2,
853 &rparam, &param_len,
854 &rdata, &data_len)) {
855 return false;
858 if (!rdata) {
859 SAFE_FREE(rparam);
860 return false;
863 num_streams = 0;
864 streams = NULL;
865 ofs = 0;
867 while ((data_len > ofs) && (data_len - ofs >= 24)) {
868 uint32_t nlen, len;
869 size_t size;
870 void *vstr;
871 struct stream_struct *tmp;
872 uint8_t *tmp_buf;
874 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
875 struct stream_struct,
876 num_streams+1);
878 if (tmp == NULL) {
879 goto fail;
881 streams = tmp;
883 nlen = IVAL(rdata, ofs + 0x04);
885 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
886 rdata, ofs + 0x08);
887 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
888 rdata, ofs + 0x10);
890 if (nlen > data_len - (ofs + 24)) {
891 goto fail;
895 * We need to null-terminate src, how do I do this with
896 * convert_string_talloc??
899 tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
900 if (tmp_buf == NULL) {
901 goto fail;
904 memcpy(tmp_buf, rdata+ofs+24, nlen);
905 tmp_buf[nlen] = 0;
906 tmp_buf[nlen+1] = 0;
908 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
909 nlen+2, &vstr, &size, false))
911 TALLOC_FREE(tmp_buf);
912 goto fail;
915 TALLOC_FREE(tmp_buf);
916 streams[num_streams].name = (char *)vstr;
917 num_streams++;
919 len = IVAL(rdata, ofs);
920 if (len > data_len - ofs) {
921 goto fail;
923 if (len == 0) break;
924 ofs += len;
927 SAFE_FREE(rdata);
928 SAFE_FREE(rparam);
930 *pnum_streams = num_streams;
931 *pstreams = streams;
932 return true;
934 fail:
935 TALLOC_FREE(streams);
936 SAFE_FREE(rdata);
937 SAFE_FREE(rparam);
938 return false;
941 /****************************************************************************
942 Send a qfileinfo QUERY_FILE_NAME_INFO call.
943 ****************************************************************************/
945 bool cli_qfilename(struct cli_state *cli, int fnum, char *name, size_t namelen)
947 unsigned int data_len = 0;
948 unsigned int param_len = 0;
949 uint16 setup = TRANSACT2_QFILEINFO;
950 char param[4];
951 char *rparam=NULL, *rdata=NULL;
953 param_len = 4;
954 SSVAL(param, 0, fnum);
955 SSVAL(param, 2, SMB_QUERY_FILE_NAME_INFO);
957 if (!cli_send_trans(cli, SMBtrans2,
958 NULL, /* name */
959 -1, 0, /* fid, flags */
960 &setup, 1, 0, /* setup, length, max */
961 param, param_len, 2, /* param, length, max */
962 NULL, data_len, cli->max_xmit /* data, length, max */
963 )) {
964 return False;
967 if (!cli_receive_trans(cli, SMBtrans2,
968 &rparam, &param_len,
969 &rdata, &data_len)) {
970 return False;
973 if (!rdata || data_len < 4) {
974 return False;
977 clistr_pull(cli, name, rdata+4, namelen, IVAL(rdata, 0), STR_UNICODE);
979 return True;
982 /****************************************************************************
983 Send a qfileinfo call.
984 ****************************************************************************/
986 bool cli_qfileinfo(struct cli_state *cli, int 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 unsigned int data_len = 0;
995 unsigned int param_len = 0;
996 uint16 setup = TRANSACT2_QFILEINFO;
997 char param[4];
998 char *rparam=NULL, *rdata=NULL;
1000 /* if its a win95 server then fail this - win95 totally screws it
1001 up */
1002 if (cli->win95) return False;
1004 param_len = 4;
1006 SSVAL(param, 0, fnum);
1007 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
1009 if (!cli_send_trans(cli, SMBtrans2,
1010 NULL, /* name */
1011 -1, 0, /* fid, flags */
1012 &setup, 1, 0, /* setup, length, max */
1013 param, param_len, 2, /* param, length, max */
1014 NULL, data_len, cli->max_xmit /* data, length, max */
1015 )) {
1016 return False;
1019 if (!cli_receive_trans(cli, SMBtrans2,
1020 &rparam, &param_len,
1021 &rdata, &data_len)) {
1022 return False;
1025 if (!rdata || data_len < 68) {
1026 return False;
1029 if (create_time) {
1030 *create_time = interpret_long_date(rdata+0);
1032 if (access_time) {
1033 *access_time = interpret_long_date(rdata+8);
1035 if (write_time) {
1036 *write_time = interpret_long_date(rdata+16);
1038 if (change_time) {
1039 *change_time = interpret_long_date(rdata+24);
1041 if (mode) {
1042 *mode = SVAL(rdata, 32);
1044 if (size) {
1045 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1047 if (ino) {
1048 *ino = IVAL(rdata, 64);
1051 SAFE_FREE(rdata);
1052 SAFE_FREE(rparam);
1053 return True;
1056 /****************************************************************************
1057 Send a qpathinfo BASIC_INFO call.
1058 ****************************************************************************/
1060 bool cli_qpathinfo_basic( struct cli_state *cli, const char *name,
1061 SMB_STRUCT_STAT *sbuf, uint32 *attributes )
1063 unsigned int param_len = 0;
1064 unsigned int data_len = 0;
1065 uint16 setup = TRANSACT2_QPATHINFO;
1066 char *param;
1067 char *rparam=NULL, *rdata=NULL;
1068 char *p;
1069 char *path;
1070 int len;
1071 size_t nlen;
1072 TALLOC_CTX *frame = talloc_stackframe();
1074 path = talloc_strdup(frame, name);
1075 if (!path) {
1076 TALLOC_FREE(frame);
1077 return false;
1079 /* cleanup */
1081 len = strlen(path);
1082 if ( path[len-1] == '\\' || path[len-1] == '/') {
1083 path[len-1] = '\0';
1085 nlen = 2*(strlen(path)+1);
1087 param = TALLOC_ARRAY(frame,char,6+nlen+2);
1088 if (!param) {
1089 return false;
1091 p = param;
1092 memset(param, '\0', 6);
1094 SSVAL(p, 0, SMB_QUERY_FILE_BASIC_INFO);
1095 p += 6;
1096 p += clistr_push(cli, p, path, nlen, STR_TERMINATE);
1097 param_len = PTR_DIFF(p, param);
1100 if (!cli_send_trans(cli, SMBtrans2,
1101 NULL, /* name */
1102 -1, 0, /* fid, flags */
1103 &setup, 1, 0, /* setup, length, max */
1104 param, param_len, 2, /* param, length, max */
1105 NULL, 0, cli->max_xmit /* data, length, max */
1106 )) {
1107 TALLOC_FREE(frame);
1108 return False;
1111 TALLOC_FREE(frame);
1113 if (!cli_receive_trans(cli, SMBtrans2,
1114 &rparam, &param_len,
1115 &rdata, &data_len)) {
1116 return False;
1119 if (data_len < 36) {
1120 SAFE_FREE(rdata);
1121 SAFE_FREE(rparam);
1122 return False;
1125 set_atimespec(sbuf, interpret_long_date( rdata+8 )); /* Access time. */
1126 set_mtimespec(sbuf, interpret_long_date( rdata+16 )); /* Write time. */
1127 set_ctimespec(sbuf, interpret_long_date( rdata+24 )); /* Change time. */
1129 *attributes = IVAL( rdata, 32 );
1131 SAFE_FREE(rparam);
1132 SAFE_FREE(rdata);
1134 return True;
1137 /****************************************************************************
1138 Send a qfileinfo call.
1139 ****************************************************************************/
1141 bool cli_qfileinfo_test(struct cli_state *cli, int fnum, int level, char **poutdata, uint32 *poutlen)
1143 unsigned int data_len = 0;
1144 unsigned int param_len = 0;
1145 uint16 setup = TRANSACT2_QFILEINFO;
1146 char param[4];
1147 char *rparam=NULL, *rdata=NULL;
1149 *poutdata = NULL;
1150 *poutlen = 0;
1152 /* if its a win95 server then fail this - win95 totally screws it
1153 up */
1154 if (cli->win95)
1155 return False;
1157 param_len = 4;
1159 SSVAL(param, 0, fnum);
1160 SSVAL(param, 2, level);
1162 if (!cli_send_trans(cli, SMBtrans2,
1163 NULL, /* name */
1164 -1, 0, /* fid, flags */
1165 &setup, 1, 0, /* setup, length, max */
1166 param, param_len, 2, /* param, length, max */
1167 NULL, data_len, cli->max_xmit /* data, length, max */
1168 )) {
1169 return False;
1172 if (!cli_receive_trans(cli, SMBtrans2,
1173 &rparam, &param_len,
1174 &rdata, &data_len)) {
1175 return False;
1178 *poutdata = (char *)memdup(rdata, data_len);
1179 if (!*poutdata) {
1180 SAFE_FREE(rdata);
1181 SAFE_FREE(rparam);
1182 return False;
1185 *poutlen = data_len;
1187 SAFE_FREE(rdata);
1188 SAFE_FREE(rparam);
1189 return True;
1192 /****************************************************************************
1193 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1194 ****************************************************************************/
1196 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1198 unsigned int data_len = 0;
1199 unsigned int param_len = 0;
1200 uint16 setup = TRANSACT2_QPATHINFO;
1201 char *param;
1202 char *rparam=NULL, *rdata=NULL;
1203 int count=8;
1204 char *p;
1205 bool ret;
1206 unsigned int len;
1207 size_t nlen = 2*(strlen(fname)+1);
1209 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
1210 if (!param) {
1211 return NT_STATUS_NO_MEMORY;
1213 p = param;
1214 memset(param, '\0', 6);
1215 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
1216 p += 6;
1217 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
1218 param_len = PTR_DIFF(p, param);
1220 do {
1221 ret = (cli_send_trans(cli, SMBtrans2,
1222 NULL, /* Name */
1223 -1, 0, /* fid, flags */
1224 &setup, 1, 0, /* setup, length, max */
1225 param, param_len, 10, /* param, length, max */
1226 NULL, data_len, cli->max_xmit /* data, length, max */
1227 ) &&
1228 cli_receive_trans(cli, SMBtrans2,
1229 &rparam, &param_len,
1230 &rdata, &data_len));
1231 if (!ret && cli_is_dos_error(cli)) {
1232 /* we need to work around a Win95 bug - sometimes
1233 it gives ERRSRV/ERRerror temprarily */
1234 uint8 eclass;
1235 uint32 ecode;
1236 cli_dos_error(cli, &eclass, &ecode);
1237 if (eclass != ERRSRV || ecode != ERRerror) break;
1238 smb_msleep(100);
1240 } while (count-- && ret==False);
1242 SAFE_FREE(param);
1244 if (!ret || !rdata || data_len < 4) {
1245 return NT_STATUS_UNSUCCESSFUL;
1248 len = IVAL(rdata, 0);
1250 if (len > data_len - 4) {
1251 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1254 clistr_pull(cli, alt_name, rdata+4, sizeof(fstring), len, STR_UNICODE);
1256 SAFE_FREE(rdata);
1257 SAFE_FREE(rparam);
1259 return NT_STATUS_OK;