Be more forgiving on client oplock break failure (as Windows does). Remove a global.
[Samba/ekacnet.git] / source3 / libsmb / clirap.c
blob80e5a41893158c53ff4e2c8db734ee31e0a32fab
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"
26 /****************************************************************************
27 Call a remote api
28 ****************************************************************************/
30 bool cli_api(struct cli_state *cli,
31 char *param, int prcnt, int mprcnt,
32 char *data, int drcnt, int mdrcnt,
33 char **rparam, unsigned int *rprcnt,
34 char **rdata, unsigned int *rdrcnt)
36 cli_send_trans(cli,SMBtrans,
37 PIPE_LANMAN, /* Name */
38 0,0, /* fid, flags */
39 NULL,0,0, /* Setup, length, max */
40 param, prcnt, mprcnt, /* Params, length, max */
41 data, drcnt, mdrcnt /* Data, length, max */
44 return (cli_receive_trans(cli,SMBtrans,
45 rparam, rprcnt,
46 rdata, rdrcnt));
49 /****************************************************************************
50 Perform a NetWkstaUserLogon.
51 ****************************************************************************/
53 bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
55 char *rparam = NULL;
56 char *rdata = NULL;
57 char *p;
58 unsigned int rdrcnt,rprcnt;
59 char param[1024];
61 memset(param, 0, sizeof(param));
63 /* send a SMBtrans command with api NetWkstaUserLogon */
64 p = param;
65 SSVAL(p,0,132); /* api number */
66 p += 2;
67 strlcpy(p,"OOWb54WrLh",sizeof(param)-PTR_DIFF(p,param));
68 p = skip_string(param,sizeof(param),p);
69 strlcpy(p,"WB21BWDWWDDDDDDDzzzD",sizeof(param)-PTR_DIFF(p,param));
70 p = skip_string(param,sizeof(param),p);
71 SSVAL(p,0,1);
72 p += 2;
73 strlcpy(p,user,sizeof(param)-PTR_DIFF(p,param));
74 strupper_m(p);
75 p += 21;
76 p++;
77 p += 15;
78 p++;
79 strlcpy(p, workstation,sizeof(param)-PTR_DIFF(p,param));
80 strupper_m(p);
81 p += 16;
82 SSVAL(p, 0, CLI_BUFFER_SIZE);
83 p += 2;
84 SSVAL(p, 0, CLI_BUFFER_SIZE);
85 p += 2;
87 if (cli_api(cli,
88 param, PTR_DIFF(p,param),1024, /* param, length, max */
89 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
90 &rparam, &rprcnt, /* return params, return size */
91 &rdata, &rdrcnt /* return data, return size */
92 )) {
93 cli->rap_error = rparam? SVAL(rparam,0) : -1;
94 p = rdata;
96 if (cli->rap_error == 0) {
97 DEBUG(4,("NetWkstaUserLogon success\n"));
98 cli->privileges = SVAL(p, 24);
99 /* The cli->eff_name field used to be set here
100 but it wasn't used anywhere else. */
101 } else {
102 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
106 SAFE_FREE(rparam);
107 SAFE_FREE(rdata);
108 return (cli->rap_error == 0);
111 /****************************************************************************
112 Call a NetShareEnum - try and browse available connections on a host.
113 ****************************************************************************/
115 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
117 char *rparam = NULL;
118 char *rdata = NULL;
119 char *p;
120 unsigned int rdrcnt,rprcnt;
121 char param[1024];
122 int count = -1;
124 /* now send a SMBtrans command with api RNetShareEnum */
125 p = param;
126 SSVAL(p,0,0); /* api number */
127 p += 2;
128 strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
129 p = skip_string(param,sizeof(param),p);
130 strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
131 p = skip_string(param,sizeof(param),p);
132 SSVAL(p,0,1);
134 * Win2k needs a *smaller* buffer than 0xFFFF here -
135 * it returns "out of server memory" with 0xFFFF !!! JRA.
137 SSVAL(p,2,0xFFE0);
138 p += 4;
140 if (cli_api(cli,
141 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
142 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
143 &rparam, &rprcnt, /* return params, length */
144 &rdata, &rdrcnt)) /* return data, length */
146 int res = rparam? SVAL(rparam,0) : -1;
148 if (res == 0 || res == ERRmoredata) {
149 int converter=SVAL(rparam,2);
150 int i;
151 char *rdata_end = rdata + rdrcnt;
153 count=SVAL(rparam,4);
154 p = rdata;
156 for (i=0;i<count;i++,p+=20) {
157 char *sname;
158 int type;
159 int comment_offset;
160 const char *cmnt;
161 const char *p1;
162 char *s1, *s2;
163 size_t len;
164 TALLOC_CTX *frame = talloc_stackframe();
166 if (p + 20 > rdata_end) {
167 TALLOC_FREE(frame);
168 break;
171 sname = p;
172 type = SVAL(p,14);
173 comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
174 if (comment_offset < 0 ||
175 comment_offset > (int)rdrcnt) {
176 TALLOC_FREE(frame);
177 break;
179 cmnt = comment_offset?(rdata+comment_offset):"";
181 /* Work out the comment length. */
182 for (p1 = cmnt, len = 0; *p1 &&
183 p1 < rdata_end; len++)
184 p1++;
185 if (!*p1) {
186 len++;
188 pull_string_talloc(frame,rdata,0,
189 &s1,sname,14,STR_ASCII);
190 pull_string_talloc(frame,rdata,0,
191 &s2,cmnt,len,STR_ASCII);
192 if (!s1 || !s2) {
193 TALLOC_FREE(frame);
194 continue;
197 fn(s1, type, s2, state);
199 TALLOC_FREE(frame);
201 } else {
202 DEBUG(4,("NetShareEnum res=%d\n", res));
204 } else {
205 DEBUG(4,("NetShareEnum failed\n"));
208 SAFE_FREE(rparam);
209 SAFE_FREE(rdata);
211 return count;
214 /****************************************************************************
215 Call a NetServerEnum for the specified workgroup and servertype mask. This
216 function then calls the specified callback function for each name returned.
218 The callback function takes 4 arguments: the machine name, the server type,
219 the comment and a state pointer.
220 ****************************************************************************/
222 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
223 void (*fn)(const char *, uint32, const char *, void *),
224 void *state)
226 char *rparam = NULL;
227 char *rdata = NULL;
228 char *rdata_end = NULL;
229 unsigned int rdrcnt,rprcnt;
230 char *p;
231 char param[1024];
232 int uLevel = 1;
233 size_t len;
234 uint32 func = RAP_NetServerEnum2;
235 char *last_entry = NULL;
236 int total_cnt = 0;
237 int return_cnt = 0;
238 int res;
240 errno = 0; /* reset */
243 * This may take more than one transaction, so we should loop until
244 * we no longer get a more data to process or we have all of the
245 * items.
247 do {
248 /* send a SMBtrans command with api NetServerEnum */
249 p = param;
250 SIVAL(p,0,func); /* api number */
251 p += 2;
253 if (func == RAP_NetServerEnum3) {
254 strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
255 } else {
256 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
259 p = skip_string(param, sizeof(param), p);
260 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
262 p = skip_string(param, sizeof(param), p);
263 SSVAL(p,0,uLevel);
264 SSVAL(p,2,CLI_BUFFER_SIZE);
265 p += 4;
266 SIVAL(p,0,stype);
267 p += 4;
269 /* If we have more data, tell the server where
270 * to continue from.
272 len = push_ascii(p,
273 workgroup,
274 sizeof(param) - PTR_DIFF(p,param) - 1,
275 STR_TERMINATE|STR_UPPER);
277 if (len == (size_t)-1) {
278 SAFE_FREE(last_entry);
279 return false;
281 p += len;
283 if (func == RAP_NetServerEnum3) {
284 len = push_ascii(p,
285 last_entry ? last_entry : "",
286 sizeof(param) - PTR_DIFF(p,param) - 1,
287 STR_TERMINATE);
289 if (len == (size_t)-1) {
290 SAFE_FREE(last_entry);
291 return false;
293 p += len;
296 /* Next time through we need to use the continue api */
297 func = RAP_NetServerEnum3;
299 if (!cli_api(cli,
300 param, PTR_DIFF(p,param), 8, /* params, length, max */
301 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
302 &rparam, &rprcnt, /* return params, return size */
303 &rdata, &rdrcnt)) { /* return data, return size */
305 /* break out of the loop on error */
306 res = -1;
307 break;
310 rdata_end = rdata + rdrcnt;
311 res = rparam ? SVAL(rparam,0) : -1;
313 if (res == 0 || res == ERRmoredata ||
314 (res != -1 && cli_errno(cli) == 0)) {
315 char *sname = NULL;
316 int i, count;
317 int converter=SVAL(rparam,2);
319 /* Get the number of items returned in this buffer */
320 count = SVAL(rparam, 4);
322 /* The next field contains the number of items left,
323 * including those returned in this buffer. So the
324 * first time through this should contain all of the
325 * entries.
327 if (total_cnt == 0) {
328 total_cnt = SVAL(rparam, 6);
331 /* Keep track of how many we have read */
332 return_cnt += count;
333 p = rdata;
335 /* The last name in the previous NetServerEnum reply is
336 * sent back to server in the NetServerEnum3 request
337 * (last_entry). The next reply should repeat this entry
338 * as the first element. We have no proof that this is
339 * always true, but from traces that seems to be the
340 * behavior from Window Servers. So first lets do a lot
341 * of checking, just being paranoid. If the string
342 * matches then we already saw this entry so skip it.
344 * NOTE: sv1_name field must be null terminated and has
345 * a max size of 16 (NetBIOS Name).
347 if (last_entry && count && p &&
348 (strncmp(last_entry, p, 16) == 0)) {
349 count -= 1; /* Skip this entry */
350 return_cnt = -1; /* Not part of total, so don't count. */
351 p = rdata + 26; /* Skip the whole record */
354 for (i = 0; i < count; i++, p += 26) {
355 int comment_offset;
356 const char *cmnt;
357 const char *p1;
358 char *s1, *s2;
359 TALLOC_CTX *frame = talloc_stackframe();
360 uint32_t entry_stype;
362 if (p + 26 > rdata_end) {
363 TALLOC_FREE(frame);
364 break;
367 sname = p;
368 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
369 cmnt = comment_offset?(rdata+comment_offset):"";
371 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
372 TALLOC_FREE(frame);
373 continue;
376 /* Work out the comment length. */
377 for (p1 = cmnt, len = 0; *p1 &&
378 p1 < rdata_end; len++)
379 p1++;
380 if (!*p1) {
381 len++;
384 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
386 pull_string_talloc(frame,rdata,0,
387 &s1,sname,16,STR_ASCII);
388 pull_string_talloc(frame,rdata,0,
389 &s2,cmnt,len,STR_ASCII);
391 if (!s1 || !s2) {
392 TALLOC_FREE(frame);
393 continue;
396 fn(s1, entry_stype, s2, state);
397 TALLOC_FREE(frame);
400 /* We are done with the old last entry, so now we can free it */
401 if (last_entry) {
402 SAFE_FREE(last_entry); /* This will set it to null */
405 /* We always make a copy of the last entry if we have one */
406 if (sname) {
407 last_entry = smb_xstrdup(sname);
410 /* If we have more data, but no last entry then error out */
411 if (!last_entry && (res == ERRmoredata)) {
412 errno = EINVAL;
413 res = 0;
418 SAFE_FREE(rparam);
419 SAFE_FREE(rdata);
420 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
422 SAFE_FREE(rparam);
423 SAFE_FREE(rdata);
424 SAFE_FREE(last_entry);
426 if (res == -1) {
427 errno = cli_errno(cli);
428 } else {
429 if (!return_cnt) {
430 /* this is a very special case, when the domain master for the
431 work group isn't part of the work group itself, there is something
432 wild going on */
433 errno = ENOENT;
437 return(return_cnt > 0);
440 /****************************************************************************
441 Send a SamOEMChangePassword command.
442 ****************************************************************************/
444 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
445 const char *old_password)
447 char param[1024];
448 unsigned char data[532];
449 char *p = param;
450 unsigned char old_pw_hash[16];
451 unsigned char new_pw_hash[16];
452 unsigned int data_len;
453 unsigned int param_len = 0;
454 char *rparam = NULL;
455 char *rdata = NULL;
456 unsigned int rprcnt, rdrcnt;
458 if (strlen(user) >= sizeof(fstring)-1) {
459 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
460 return False;
463 SSVAL(p,0,214); /* SamOEMChangePassword command. */
464 p += 2;
465 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
466 p = skip_string(param,sizeof(param),p);
467 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
468 p = skip_string(param,sizeof(param),p);
469 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
470 p = skip_string(param,sizeof(param),p);
471 SSVAL(p,0,532);
472 p += 2;
474 param_len = PTR_DIFF(p,param);
477 * Get the Lanman hash of the old password, we
478 * use this as the key to make_oem_passwd_hash().
480 E_deshash(old_password, old_pw_hash);
482 encode_pw_buffer(data, new_password, STR_ASCII);
484 #ifdef DEBUG_PASSWORD
485 DEBUG(100,("make_oem_passwd_hash\n"));
486 dump_data(100, data, 516);
487 #endif
488 arcfour_crypt( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
491 * Now place the old password hash in the data.
493 E_deshash(new_password, new_pw_hash);
495 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
497 data_len = 532;
499 if (cli_send_trans(cli,SMBtrans,
500 PIPE_LANMAN, /* name */
501 0,0, /* fid, flags */
502 NULL,0,0, /* setup, length, max */
503 param,param_len,4, /* param, length, max */
504 (char *)data,data_len,0 /* data, length, max */
505 ) == False) {
506 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
507 user ));
508 return False;
511 if (!cli_receive_trans(cli,SMBtrans,
512 &rparam, &rprcnt,
513 &rdata, &rdrcnt)) {
514 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
515 user ));
516 return False;
519 if (rparam) {
520 cli->rap_error = SVAL(rparam,0);
523 SAFE_FREE(rparam);
524 SAFE_FREE(rdata);
526 return (cli->rap_error == 0);
529 /****************************************************************************
530 Send a qpathinfo call.
531 ****************************************************************************/
533 bool cli_qpathinfo(struct cli_state *cli,
534 const char *fname,
535 time_t *change_time,
536 time_t *access_time,
537 time_t *write_time,
538 SMB_OFF_T *size,
539 uint16 *mode)
541 unsigned int data_len = 0;
542 unsigned int param_len = 0;
543 unsigned int rparam_len, rdata_len;
544 uint16 setup = TRANSACT2_QPATHINFO;
545 char *param;
546 char *rparam=NULL, *rdata=NULL;
547 int count=8;
548 bool ret;
549 time_t (*date_fn)(struct cli_state *, const void *);
550 char *p;
551 size_t nlen = 2*(strlen(fname)+1);
553 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
554 if (!param) {
555 return false;
557 p = param;
558 memset(p, '\0', 6);
559 SSVAL(p, 0, SMB_INFO_STANDARD);
560 p += 6;
561 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
562 param_len = PTR_DIFF(p, param);
564 do {
565 ret = (cli_send_trans(cli, SMBtrans2,
566 NULL, /* Name */
567 -1, 0, /* fid, flags */
568 &setup, 1, 0, /* setup, length, max */
569 param, param_len, 10, /* param, length, max */
570 NULL, data_len, cli->max_xmit /* data, length, max */
571 ) &&
572 cli_receive_trans(cli, SMBtrans2,
573 &rparam, &rparam_len,
574 &rdata, &rdata_len));
575 if (!cli_is_dos_error(cli)) break;
576 if (!ret) {
577 /* we need to work around a Win95 bug - sometimes
578 it gives ERRSRV/ERRerror temprarily */
579 uint8 eclass;
580 uint32 ecode;
581 cli_dos_error(cli, &eclass, &ecode);
582 if (eclass != ERRSRV || ecode != ERRerror) break;
583 smb_msleep(100);
585 } while (count-- && ret==False);
587 SAFE_FREE(param);
588 if (!ret || !rdata || rdata_len < 22) {
589 return False;
592 if (cli->win95) {
593 date_fn = cli_make_unix_date;
594 } else {
595 date_fn = cli_make_unix_date2;
598 if (change_time) {
599 *change_time = date_fn(cli, rdata+0);
601 if (access_time) {
602 *access_time = date_fn(cli, rdata+4);
604 if (write_time) {
605 *write_time = date_fn(cli, rdata+8);
607 if (size) {
608 *size = IVAL(rdata, 12);
610 if (mode) {
611 *mode = SVAL(rdata,l1_attrFile);
614 SAFE_FREE(rdata);
615 SAFE_FREE(rparam);
616 return True;
619 /****************************************************************************
620 Send a setpathinfo call.
621 ****************************************************************************/
623 bool cli_setpathinfo(struct cli_state *cli, const char *fname,
624 time_t create_time,
625 time_t access_time,
626 time_t write_time,
627 time_t change_time,
628 uint16 mode)
630 unsigned int data_len = 0;
631 unsigned int param_len = 0;
632 unsigned int rparam_len, rdata_len;
633 uint16 setup = TRANSACT2_SETPATHINFO;
634 char *param;
635 char data[40];
636 char *rparam=NULL, *rdata=NULL;
637 int count=8;
638 bool ret;
639 char *p;
640 size_t nlen = 2*(strlen(fname)+1);
642 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
643 if (!param) {
644 return false;
646 memset(param, '\0', 6);
647 memset(data, 0, sizeof(data));
649 p = param;
651 /* Add the information level */
652 SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
654 /* Skip reserved */
655 p += 6;
657 /* Add the file name */
658 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
660 param_len = PTR_DIFF(p, param);
662 p = data;
665 * Add the create, last access, modification, and status change times
667 put_long_date(p, create_time);
668 p += 8;
670 put_long_date(p, access_time);
671 p += 8;
673 put_long_date(p, write_time);
674 p += 8;
676 put_long_date(p, change_time);
677 p += 8;
679 /* Add attributes */
680 SIVAL(p, 0, mode);
681 p += 4;
683 /* Add padding */
684 SIVAL(p, 0, 0);
685 p += 4;
687 data_len = PTR_DIFF(p, data);
689 do {
690 ret = (cli_send_trans(cli, SMBtrans2,
691 NULL, /* Name */
692 -1, 0, /* fid, flags */
693 &setup, 1, 0, /* setup, length, max */
694 param, param_len, 10, /* param, length, max */
695 data, data_len, cli->max_xmit /* data, length, max */
696 ) &&
697 cli_receive_trans(cli, SMBtrans2,
698 &rparam, &rparam_len,
699 &rdata, &rdata_len));
700 if (!cli_is_dos_error(cli)) break;
701 if (!ret) {
702 /* we need to work around a Win95 bug - sometimes
703 it gives ERRSRV/ERRerror temprarily */
704 uint8 eclass;
705 uint32 ecode;
706 cli_dos_error(cli, &eclass, &ecode);
707 if (eclass != ERRSRV || ecode != ERRerror) break;
708 smb_msleep(100);
710 } while (count-- && ret==False);
712 SAFE_FREE(param);
713 if (!ret) {
714 return False;
717 SAFE_FREE(rdata);
718 SAFE_FREE(rparam);
719 return True;
722 /****************************************************************************
723 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
724 ****************************************************************************/
726 bool cli_qpathinfo2(struct cli_state *cli, const char *fname,
727 struct timespec *create_time,
728 struct timespec *access_time,
729 struct timespec *write_time,
730 struct timespec *change_time,
731 SMB_OFF_T *size, uint16 *mode,
732 SMB_INO_T *ino)
734 unsigned int data_len = 0;
735 unsigned int param_len = 0;
736 uint16 setup = TRANSACT2_QPATHINFO;
737 char *param;
738 char *rparam=NULL, *rdata=NULL;
739 char *p;
740 size_t nlen = 2*(strlen(fname)+1);
742 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
743 if (!param) {
744 return false;
746 p = param;
747 memset(param, '\0', 6);
748 SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
749 p += 6;
750 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
752 param_len = PTR_DIFF(p, param);
754 if (!cli_send_trans(cli, SMBtrans2,
755 NULL, /* name */
756 -1, 0, /* fid, flags */
757 &setup, 1, 0, /* setup, length, max */
758 param, param_len, 10, /* param, length, max */
759 NULL, data_len, cli->max_xmit /* data, length, max */
760 )) {
761 SAFE_FREE(param);
762 return False;
765 SAFE_FREE(param);
766 if (!cli_receive_trans(cli, SMBtrans2,
767 &rparam, &param_len,
768 &rdata, &data_len)) {
769 return False;
772 if (!rdata || data_len < 22) {
773 return False;
776 if (create_time) {
777 *create_time = interpret_long_date(rdata+0);
779 if (access_time) {
780 *access_time = interpret_long_date(rdata+8);
782 if (write_time) {
783 *write_time = interpret_long_date(rdata+16);
785 if (change_time) {
786 *change_time = interpret_long_date(rdata+24);
788 if (mode) {
789 *mode = SVAL(rdata, 32);
791 if (size) {
792 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
794 if (ino) {
795 *ino = IVAL(rdata, 64);
798 SAFE_FREE(rdata);
799 SAFE_FREE(rparam);
800 return True;
803 /****************************************************************************
804 Get the stream info
805 ****************************************************************************/
807 bool cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
808 TALLOC_CTX *mem_ctx,
809 unsigned int *pnum_streams,
810 struct stream_struct **pstreams)
812 unsigned int data_len = 0;
813 unsigned int param_len = 0;
814 uint16 setup = TRANSACT2_QPATHINFO;
815 char *param;
816 char *rparam=NULL, *rdata=NULL;
817 char *p;
818 unsigned int num_streams;
819 struct stream_struct *streams;
820 unsigned int ofs;
821 size_t namelen = 2*(strlen(fname)+1);
823 param = SMB_MALLOC_ARRAY(char, 6+namelen+2);
824 if (param == NULL) {
825 return false;
827 p = param;
828 memset(p, 0, 6);
829 SSVAL(p, 0, SMB_FILE_STREAM_INFORMATION);
830 p += 6;
831 p += clistr_push(cli, p, fname, namelen, STR_TERMINATE);
833 param_len = PTR_DIFF(p, param);
835 if (!cli_send_trans(cli, SMBtrans2,
836 NULL, /* name */
837 -1, 0, /* fid, flags */
838 &setup, 1, 0, /* setup, len, max */
839 param, param_len, 10, /* param, len, max */
840 NULL, data_len, cli->max_xmit /* data, len, max */
841 )) {
842 return false;
845 if (!cli_receive_trans(cli, SMBtrans2,
846 &rparam, &param_len,
847 &rdata, &data_len)) {
848 return false;
851 if (!rdata) {
852 SAFE_FREE(rparam);
853 return false;
856 num_streams = 0;
857 streams = NULL;
858 ofs = 0;
860 while ((data_len > ofs) && (data_len - ofs >= 24)) {
861 uint32_t nlen, len;
862 size_t size;
863 void *vstr;
864 struct stream_struct *tmp;
865 uint8_t *tmp_buf;
867 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
868 struct stream_struct,
869 num_streams+1);
871 if (tmp == NULL) {
872 goto fail;
874 streams = tmp;
876 nlen = IVAL(rdata, ofs + 0x04);
878 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
879 rdata, ofs + 0x08);
880 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
881 rdata, ofs + 0x10);
883 if (nlen > data_len - (ofs + 24)) {
884 goto fail;
888 * We need to null-terminate src, how do I do this with
889 * convert_string_talloc??
892 tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
893 if (tmp_buf == NULL) {
894 goto fail;
897 memcpy(tmp_buf, rdata+ofs+24, nlen);
898 tmp_buf[nlen] = 0;
899 tmp_buf[nlen+1] = 0;
901 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
902 nlen+2, &vstr, &size, false))
904 TALLOC_FREE(tmp_buf);
905 goto fail;
908 TALLOC_FREE(tmp_buf);
909 streams[num_streams].name = (char *)vstr;
910 num_streams++;
912 len = IVAL(rdata, ofs);
913 if (len > data_len - ofs) {
914 goto fail;
916 if (len == 0) break;
917 ofs += len;
920 SAFE_FREE(rdata);
921 SAFE_FREE(rparam);
923 *pnum_streams = num_streams;
924 *pstreams = streams;
925 return true;
927 fail:
928 TALLOC_FREE(streams);
929 SAFE_FREE(rdata);
930 SAFE_FREE(rparam);
931 return false;
934 /****************************************************************************
935 Send a qfileinfo QUERY_FILE_NAME_INFO call.
936 ****************************************************************************/
938 bool cli_qfilename(struct cli_state *cli, uint16_t fnum, char *name, size_t namelen)
940 unsigned int data_len = 0;
941 unsigned int param_len = 0;
942 uint16 setup = TRANSACT2_QFILEINFO;
943 char param[4];
944 char *rparam=NULL, *rdata=NULL;
946 param_len = 4;
947 SSVAL(param, 0, fnum);
948 SSVAL(param, 2, SMB_QUERY_FILE_NAME_INFO);
950 if (!cli_send_trans(cli, SMBtrans2,
951 NULL, /* name */
952 -1, 0, /* fid, flags */
953 &setup, 1, 0, /* setup, length, max */
954 param, param_len, 2, /* param, length, max */
955 NULL, data_len, cli->max_xmit /* data, length, max */
956 )) {
957 return False;
960 if (!cli_receive_trans(cli, SMBtrans2,
961 &rparam, &param_len,
962 &rdata, &data_len)) {
963 return False;
966 if (!rdata || data_len < 4) {
967 SAFE_FREE(rparam);
968 SAFE_FREE(rdata);
969 return False;
972 clistr_pull(cli->inbuf, name, rdata+4, namelen, IVAL(rdata, 0),
973 STR_UNICODE);
975 SAFE_FREE(rparam);
976 SAFE_FREE(rdata);
978 return True;
981 /****************************************************************************
982 Send a qfileinfo call.
983 ****************************************************************************/
985 bool cli_qfileinfo(struct cli_state *cli, uint16_t fnum,
986 uint16 *mode, SMB_OFF_T *size,
987 struct timespec *create_time,
988 struct timespec *access_time,
989 struct timespec *write_time,
990 struct timespec *change_time,
991 SMB_INO_T *ino)
993 uint32_t data_len = 0;
994 uint16 setup;
995 uint8_t param[4];
996 uint8_t *rdata=NULL;
997 NTSTATUS status;
999 /* if its a win95 server then fail this - win95 totally screws it
1000 up */
1001 if (cli->win95) return False;
1003 SSVAL(param, 0, fnum);
1004 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
1006 SSVAL(&setup, 0, TRANSACT2_QFILEINFO);
1008 status = cli_trans(talloc_tos(), cli, SMBtrans2,
1009 NULL, -1, 0, 0, /* name, fid, function, flags */
1010 &setup, 1, 0, /* setup, length, max */
1011 param, 4, 2, /* param, length, max */
1012 NULL, 0, MIN(cli->max_xmit, 0xffff), /* data, length, max */
1013 NULL, 0, NULL, /* rsetup, length */
1014 NULL, 0, NULL, /* rparam, length */
1015 &rdata, 68, &data_len);
1017 if (!NT_STATUS_IS_OK(status)) {
1018 return false;
1021 if (create_time) {
1022 *create_time = interpret_long_date((char *)rdata+0);
1024 if (access_time) {
1025 *access_time = interpret_long_date((char *)rdata+8);
1027 if (write_time) {
1028 *write_time = interpret_long_date((char *)rdata+16);
1030 if (change_time) {
1031 *change_time = interpret_long_date((char *)rdata+24);
1033 if (mode) {
1034 *mode = SVAL(rdata, 32);
1036 if (size) {
1037 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1039 if (ino) {
1040 *ino = IVAL(rdata, 64);
1043 TALLOC_FREE(rdata);
1044 return True;
1047 /****************************************************************************
1048 Send a qpathinfo BASIC_INFO call.
1049 ****************************************************************************/
1051 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
1052 SMB_STRUCT_STAT *sbuf, uint32 *attributes)
1054 unsigned int param_len = 0;
1055 uint32_t rdata_len;
1056 uint16_t setup[1];
1057 uint8_t *param, *rdata;
1058 uint8_t *p;
1059 char *path;
1060 int len;
1061 size_t nlen;
1062 TALLOC_CTX *frame = talloc_stackframe();
1063 NTSTATUS status;
1065 path = talloc_strdup(frame, name);
1066 if (!path) {
1067 TALLOC_FREE(frame);
1068 return NT_STATUS_NO_MEMORY;
1070 /* cleanup */
1072 len = strlen(path);
1073 if ( path[len-1] == '\\' || path[len-1] == '/') {
1074 path[len-1] = '\0';
1076 nlen = 2*(strlen(path)+1);
1078 param = TALLOC_ARRAY(frame, uint8_t, 6+nlen+2);
1079 if (!param) {
1080 TALLOC_FREE(frame);
1081 return NT_STATUS_NO_MEMORY;
1083 p = param;
1084 memset(param, '\0', 6);
1086 SSVAL(setup, 0, TRANSACT2_QPATHINFO);
1087 SSVAL(p, 0, SMB_QUERY_FILE_BASIC_INFO);
1088 p += 6;
1089 p += clistr_push(cli, p, path, nlen, STR_TERMINATE);
1090 param_len = PTR_DIFF(p, param);
1092 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
1093 setup, 1, 0,
1094 param, param_len, 2,
1095 NULL, 0, cli->max_xmit,
1096 NULL, 0, NULL,
1097 NULL, 0, NULL,
1098 &rdata, 36, &rdata_len);
1099 if (!NT_STATUS_IS_OK(status)) {
1100 TALLOC_FREE(frame);
1101 return status;
1104 sbuf->st_ex_atime = interpret_long_date((char *)rdata+8);
1105 sbuf->st_ex_mtime = interpret_long_date((char *)rdata+16);
1106 sbuf->st_ex_ctime = interpret_long_date((char *)rdata+24);
1108 *attributes = IVAL( rdata, 32 );
1110 TALLOC_FREE(rdata);
1112 return NT_STATUS_OK;
1115 /****************************************************************************
1116 Send a qfileinfo call.
1117 ****************************************************************************/
1119 bool cli_qfileinfo_test(struct cli_state *cli, uint16_t fnum, int level, char **poutdata, uint32 *poutlen)
1121 unsigned int data_len = 0;
1122 unsigned int param_len = 0;
1123 uint16 setup = TRANSACT2_QFILEINFO;
1124 char param[4];
1125 char *rparam=NULL, *rdata=NULL;
1127 *poutdata = NULL;
1128 *poutlen = 0;
1130 /* if its a win95 server then fail this - win95 totally screws it
1131 up */
1132 if (cli->win95)
1133 return False;
1135 param_len = 4;
1137 SSVAL(param, 0, fnum);
1138 SSVAL(param, 2, level);
1140 if (!cli_send_trans(cli, SMBtrans2,
1141 NULL, /* name */
1142 -1, 0, /* fid, flags */
1143 &setup, 1, 0, /* setup, length, max */
1144 param, param_len, 2, /* param, length, max */
1145 NULL, data_len, cli->max_xmit /* data, length, max */
1146 )) {
1147 return False;
1150 if (!cli_receive_trans(cli, SMBtrans2,
1151 &rparam, &param_len,
1152 &rdata, &data_len)) {
1153 return False;
1156 *poutdata = (char *)memdup(rdata, data_len);
1157 if (!*poutdata) {
1158 SAFE_FREE(rdata);
1159 SAFE_FREE(rparam);
1160 return False;
1163 *poutlen = data_len;
1165 SAFE_FREE(rdata);
1166 SAFE_FREE(rparam);
1167 return True;
1170 /****************************************************************************
1171 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1172 ****************************************************************************/
1174 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1176 unsigned int data_len = 0;
1177 unsigned int param_len = 0;
1178 uint16 setup = TRANSACT2_QPATHINFO;
1179 char *param;
1180 char *rparam=NULL, *rdata=NULL;
1181 int count=8;
1182 char *p;
1183 bool ret;
1184 unsigned int len;
1185 size_t nlen = 2*(strlen(fname)+1);
1187 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
1188 if (!param) {
1189 return NT_STATUS_NO_MEMORY;
1191 p = param;
1192 memset(param, '\0', 6);
1193 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
1194 p += 6;
1195 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
1196 param_len = PTR_DIFF(p, param);
1198 do {
1199 ret = (cli_send_trans(cli, SMBtrans2,
1200 NULL, /* Name */
1201 -1, 0, /* fid, flags */
1202 &setup, 1, 0, /* setup, length, max */
1203 param, param_len, 10, /* param, length, max */
1204 NULL, data_len, cli->max_xmit /* data, length, max */
1205 ) &&
1206 cli_receive_trans(cli, SMBtrans2,
1207 &rparam, &param_len,
1208 &rdata, &data_len));
1209 if (!ret && cli_is_dos_error(cli)) {
1210 /* we need to work around a Win95 bug - sometimes
1211 it gives ERRSRV/ERRerror temprarily */
1212 uint8 eclass;
1213 uint32 ecode;
1214 cli_dos_error(cli, &eclass, &ecode);
1215 if (eclass != ERRSRV || ecode != ERRerror) break;
1216 smb_msleep(100);
1218 } while (count-- && ret==False);
1220 SAFE_FREE(param);
1222 if (!ret || !rdata || data_len < 4) {
1223 return NT_STATUS_UNSUCCESSFUL;
1226 len = IVAL(rdata, 0);
1228 if (len > data_len - 4) {
1229 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1232 clistr_pull(cli->inbuf, alt_name, rdata+4, sizeof(fstring), len,
1233 STR_UNICODE);
1235 SAFE_FREE(rdata);
1236 SAFE_FREE(rparam);
1238 return NT_STATUS_OK;