s3-net: let rpccli_winreg_Connect optionally return WERROR
[Samba.git] / source3 / libsmb / clirap.c
blob3ac9f4454fb6662ab1b081d60b834be0205e9dc5
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"
26 #include "async_smb.h"
28 /****************************************************************************
29 Call a remote api
30 ****************************************************************************/
32 bool cli_api(struct cli_state *cli,
33 char *param, int prcnt, int mprcnt,
34 char *data, int drcnt, int mdrcnt,
35 char **rparam, unsigned int *rprcnt,
36 char **rdata, unsigned int *rdrcnt)
38 cli_send_trans(cli,SMBtrans,
39 PIPE_LANMAN, /* Name */
40 0,0, /* fid, flags */
41 NULL,0,0, /* Setup, length, max */
42 param, prcnt, mprcnt, /* Params, length, max */
43 data, drcnt, mdrcnt /* Data, length, max */
46 return (cli_receive_trans(cli,SMBtrans,
47 rparam, rprcnt,
48 rdata, rdrcnt));
51 /****************************************************************************
52 Perform a NetWkstaUserLogon.
53 ****************************************************************************/
55 bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
57 char *rparam = NULL;
58 char *rdata = NULL;
59 char *p;
60 unsigned int rdrcnt,rprcnt;
61 char param[1024];
63 memset(param, 0, sizeof(param));
65 /* send a SMBtrans command with api NetWkstaUserLogon */
66 p = param;
67 SSVAL(p,0,132); /* api number */
68 p += 2;
69 strlcpy(p,"OOWb54WrLh",sizeof(param)-PTR_DIFF(p,param));
70 p = skip_string(param,sizeof(param),p);
71 strlcpy(p,"WB21BWDWWDDDDDDDzzzD",sizeof(param)-PTR_DIFF(p,param));
72 p = skip_string(param,sizeof(param),p);
73 SSVAL(p,0,1);
74 p += 2;
75 strlcpy(p,user,sizeof(param)-PTR_DIFF(p,param));
76 strupper_m(p);
77 p += 21;
78 p++;
79 p += 15;
80 p++;
81 strlcpy(p, workstation,sizeof(param)-PTR_DIFF(p,param));
82 strupper_m(p);
83 p += 16;
84 SSVAL(p, 0, CLI_BUFFER_SIZE);
85 p += 2;
86 SSVAL(p, 0, CLI_BUFFER_SIZE);
87 p += 2;
89 if (cli_api(cli,
90 param, PTR_DIFF(p,param),1024, /* param, length, max */
91 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
92 &rparam, &rprcnt, /* return params, return size */
93 &rdata, &rdrcnt /* return data, return size */
94 )) {
95 cli->rap_error = rparam? SVAL(rparam,0) : -1;
96 p = rdata;
98 if (cli->rap_error == 0) {
99 DEBUG(4,("NetWkstaUserLogon success\n"));
100 cli->privileges = SVAL(p, 24);
101 /* The cli->eff_name field used to be set here
102 but it wasn't used anywhere else. */
103 } else {
104 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
108 SAFE_FREE(rparam);
109 SAFE_FREE(rdata);
110 return (cli->rap_error == 0);
113 /****************************************************************************
114 Call a NetShareEnum - try and browse available connections on a host.
115 ****************************************************************************/
117 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
119 char *rparam = NULL;
120 char *rdata = NULL;
121 char *p;
122 unsigned int rdrcnt,rprcnt;
123 char param[1024];
124 int count = -1;
126 /* now send a SMBtrans command with api RNetShareEnum */
127 p = param;
128 SSVAL(p,0,0); /* api number */
129 p += 2;
130 strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
131 p = skip_string(param,sizeof(param),p);
132 strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
133 p = skip_string(param,sizeof(param),p);
134 SSVAL(p,0,1);
136 * Win2k needs a *smaller* buffer than 0xFFFF here -
137 * it returns "out of server memory" with 0xFFFF !!! JRA.
139 SSVAL(p,2,0xFFE0);
140 p += 4;
142 if (cli_api(cli,
143 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
144 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
145 &rparam, &rprcnt, /* return params, length */
146 &rdata, &rdrcnt)) /* return data, length */
148 int res = rparam? SVAL(rparam,0) : -1;
150 if (res == 0 || res == ERRmoredata) {
151 int converter=SVAL(rparam,2);
152 int i;
153 char *rdata_end = rdata + rdrcnt;
155 count=SVAL(rparam,4);
156 p = rdata;
158 for (i=0;i<count;i++,p+=20) {
159 char *sname;
160 int type;
161 int comment_offset;
162 const char *cmnt;
163 const char *p1;
164 char *s1, *s2;
165 size_t len;
166 TALLOC_CTX *frame = talloc_stackframe();
168 if (p + 20 > rdata_end) {
169 TALLOC_FREE(frame);
170 break;
173 sname = p;
174 type = SVAL(p,14);
175 comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
176 if (comment_offset < 0 ||
177 comment_offset > (int)rdrcnt) {
178 TALLOC_FREE(frame);
179 break;
181 cmnt = comment_offset?(rdata+comment_offset):"";
183 /* Work out the comment length. */
184 for (p1 = cmnt, len = 0; *p1 &&
185 p1 < rdata_end; len++)
186 p1++;
187 if (!*p1) {
188 len++;
190 pull_string_talloc(frame,rdata,0,
191 &s1,sname,14,STR_ASCII);
192 pull_string_talloc(frame,rdata,0,
193 &s2,cmnt,len,STR_ASCII);
194 if (!s1 || !s2) {
195 TALLOC_FREE(frame);
196 continue;
199 fn(s1, type, s2, state);
201 TALLOC_FREE(frame);
203 } else {
204 DEBUG(4,("NetShareEnum res=%d\n", res));
206 } else {
207 DEBUG(4,("NetShareEnum failed\n"));
210 SAFE_FREE(rparam);
211 SAFE_FREE(rdata);
213 return count;
216 /****************************************************************************
217 Call a NetServerEnum for the specified workgroup and servertype mask. This
218 function then calls the specified callback function for each name returned.
220 The callback function takes 4 arguments: the machine name, the server type,
221 the comment and a state pointer.
222 ****************************************************************************/
224 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
225 void (*fn)(const char *, uint32, const char *, void *),
226 void *state)
228 char *rparam = NULL;
229 char *rdata = NULL;
230 char *rdata_end = NULL;
231 unsigned int rdrcnt,rprcnt;
232 char *p;
233 char param[1024];
234 int uLevel = 1;
235 size_t len;
236 uint32 func = RAP_NetServerEnum2;
237 char *last_entry = NULL;
238 int total_cnt = 0;
239 int return_cnt = 0;
240 int res;
242 errno = 0; /* reset */
245 * This may take more than one transaction, so we should loop until
246 * we no longer get a more data to process or we have all of the
247 * items.
249 do {
250 /* send a SMBtrans command with api NetServerEnum */
251 p = param;
252 SIVAL(p,0,func); /* api number */
253 p += 2;
255 if (func == RAP_NetServerEnum3) {
256 strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
257 } else {
258 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
261 p = skip_string(param, sizeof(param), p);
262 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
264 p = skip_string(param, sizeof(param), p);
265 SSVAL(p,0,uLevel);
266 SSVAL(p,2,CLI_BUFFER_SIZE);
267 p += 4;
268 SIVAL(p,0,stype);
269 p += 4;
271 /* If we have more data, tell the server where
272 * to continue from.
274 len = push_ascii(p,
275 workgroup,
276 sizeof(param) - PTR_DIFF(p,param) - 1,
277 STR_TERMINATE|STR_UPPER);
279 if (len == (size_t)-1) {
280 SAFE_FREE(last_entry);
281 return false;
283 p += len;
285 if (func == RAP_NetServerEnum3) {
286 len = push_ascii(p,
287 last_entry ? last_entry : "",
288 sizeof(param) - PTR_DIFF(p,param) - 1,
289 STR_TERMINATE);
291 if (len == (size_t)-1) {
292 SAFE_FREE(last_entry);
293 return false;
295 p += len;
298 /* Next time through we need to use the continue api */
299 func = RAP_NetServerEnum3;
301 if (!cli_api(cli,
302 param, PTR_DIFF(p,param), 8, /* params, length, max */
303 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
304 &rparam, &rprcnt, /* return params, return size */
305 &rdata, &rdrcnt)) { /* return data, return size */
307 /* break out of the loop on error */
308 res = -1;
309 break;
312 rdata_end = rdata + rdrcnt;
313 res = rparam ? SVAL(rparam,0) : -1;
315 if (res == 0 || res == ERRmoredata ||
316 (res != -1 && cli_errno(cli) == 0)) {
317 char *sname = NULL;
318 int i, count;
319 int converter=SVAL(rparam,2);
321 /* Get the number of items returned in this buffer */
322 count = SVAL(rparam, 4);
324 /* The next field contains the number of items left,
325 * including those returned in this buffer. So the
326 * first time through this should contain all of the
327 * entries.
329 if (total_cnt == 0) {
330 total_cnt = SVAL(rparam, 6);
333 /* Keep track of how many we have read */
334 return_cnt += count;
335 p = rdata;
337 /* The last name in the previous NetServerEnum reply is
338 * sent back to server in the NetServerEnum3 request
339 * (last_entry). The next reply should repeat this entry
340 * as the first element. We have no proof that this is
341 * always true, but from traces that seems to be the
342 * behavior from Window Servers. So first lets do a lot
343 * of checking, just being paranoid. If the string
344 * matches then we already saw this entry so skip it.
346 * NOTE: sv1_name field must be null terminated and has
347 * a max size of 16 (NetBIOS Name).
349 if (last_entry && count && p &&
350 (strncmp(last_entry, p, 16) == 0)) {
351 count -= 1; /* Skip this entry */
352 return_cnt = -1; /* Not part of total, so don't count. */
353 p = rdata + 26; /* Skip the whole record */
356 for (i = 0; i < count; i++, p += 26) {
357 int comment_offset;
358 const char *cmnt;
359 const char *p1;
360 char *s1, *s2;
361 TALLOC_CTX *frame = talloc_stackframe();
362 uint32_t entry_stype;
364 if (p + 26 > rdata_end) {
365 TALLOC_FREE(frame);
366 break;
369 sname = p;
370 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
371 cmnt = comment_offset?(rdata+comment_offset):"";
373 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
374 TALLOC_FREE(frame);
375 continue;
378 /* Work out the comment length. */
379 for (p1 = cmnt, len = 0; *p1 &&
380 p1 < rdata_end; len++)
381 p1++;
382 if (!*p1) {
383 len++;
386 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
388 pull_string_talloc(frame,rdata,0,
389 &s1,sname,16,STR_ASCII);
390 pull_string_talloc(frame,rdata,0,
391 &s2,cmnt,len,STR_ASCII);
393 if (!s1 || !s2) {
394 TALLOC_FREE(frame);
395 continue;
398 fn(s1, entry_stype, s2, state);
399 TALLOC_FREE(frame);
402 /* We are done with the old last entry, so now we can free it */
403 if (last_entry) {
404 SAFE_FREE(last_entry); /* This will set it to null */
407 /* We always make a copy of the last entry if we have one */
408 if (sname) {
409 last_entry = smb_xstrdup(sname);
412 /* If we have more data, but no last entry then error out */
413 if (!last_entry && (res == ERRmoredata)) {
414 errno = EINVAL;
415 res = 0;
420 SAFE_FREE(rparam);
421 SAFE_FREE(rdata);
422 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
424 SAFE_FREE(rparam);
425 SAFE_FREE(rdata);
426 SAFE_FREE(last_entry);
428 if (res == -1) {
429 errno = cli_errno(cli);
430 } else {
431 if (!return_cnt) {
432 /* this is a very special case, when the domain master for the
433 work group isn't part of the work group itself, there is something
434 wild going on */
435 errno = ENOENT;
439 return(return_cnt > 0);
442 /****************************************************************************
443 Send a SamOEMChangePassword command.
444 ****************************************************************************/
446 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
447 const char *old_password)
449 char param[1024];
450 unsigned char data[532];
451 char *p = param;
452 unsigned char old_pw_hash[16];
453 unsigned char new_pw_hash[16];
454 unsigned int data_len;
455 unsigned int param_len = 0;
456 char *rparam = NULL;
457 char *rdata = NULL;
458 unsigned int rprcnt, rdrcnt;
460 if (strlen(user) >= sizeof(fstring)-1) {
461 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
462 return False;
465 SSVAL(p,0,214); /* SamOEMChangePassword command. */
466 p += 2;
467 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
468 p = skip_string(param,sizeof(param),p);
469 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
470 p = skip_string(param,sizeof(param),p);
471 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
472 p = skip_string(param,sizeof(param),p);
473 SSVAL(p,0,532);
474 p += 2;
476 param_len = PTR_DIFF(p,param);
479 * Get the Lanman hash of the old password, we
480 * use this as the key to make_oem_passwd_hash().
482 E_deshash(old_password, old_pw_hash);
484 encode_pw_buffer(data, new_password, STR_ASCII);
486 #ifdef DEBUG_PASSWORD
487 DEBUG(100,("make_oem_passwd_hash\n"));
488 dump_data(100, data, 516);
489 #endif
490 arcfour_crypt( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
493 * Now place the old password hash in the data.
495 E_deshash(new_password, new_pw_hash);
497 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
499 data_len = 532;
501 if (cli_send_trans(cli,SMBtrans,
502 PIPE_LANMAN, /* name */
503 0,0, /* fid, flags */
504 NULL,0,0, /* setup, length, max */
505 param,param_len,4, /* param, length, max */
506 (char *)data,data_len,0 /* data, length, max */
507 ) == False) {
508 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
509 user ));
510 return False;
513 if (!cli_receive_trans(cli,SMBtrans,
514 &rparam, &rprcnt,
515 &rdata, &rdrcnt)) {
516 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
517 user ));
518 return False;
521 if (rparam) {
522 cli->rap_error = SVAL(rparam,0);
525 SAFE_FREE(rparam);
526 SAFE_FREE(rdata);
528 return (cli->rap_error == 0);
531 /****************************************************************************
532 Send a qpathinfo call.
533 ****************************************************************************/
535 struct cli_qpathinfo1_state {
536 struct cli_state *cli;
537 uint32_t num_data;
538 uint8_t *data;
541 static void cli_qpathinfo1_done(struct tevent_req *subreq);
543 struct tevent_req *cli_qpathinfo1_send(TALLOC_CTX *mem_ctx,
544 struct event_context *ev,
545 struct cli_state *cli,
546 const char *fname)
548 struct tevent_req *req = NULL, *subreq = NULL;
549 struct cli_qpathinfo1_state *state = NULL;
551 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo1_state);
552 if (req == NULL) {
553 return NULL;
555 state->cli = cli;
556 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_INFO_STANDARD,
557 22, cli->max_xmit);
558 if (tevent_req_nomem(subreq, req)) {
559 return tevent_req_post(req, ev);
561 tevent_req_set_callback(subreq, cli_qpathinfo1_done, req);
562 return req;
565 static void cli_qpathinfo1_done(struct tevent_req *subreq)
567 struct tevent_req *req = tevent_req_callback_data(
568 subreq, struct tevent_req);
569 struct cli_qpathinfo1_state *state = tevent_req_data(
570 req, struct cli_qpathinfo1_state);
571 NTSTATUS status;
573 status = cli_qpathinfo_recv(subreq, state, &state->data,
574 &state->num_data);
575 TALLOC_FREE(subreq);
576 if (!NT_STATUS_IS_OK(status)) {
577 tevent_req_nterror(req, status);
578 return;
580 tevent_req_done(req);
583 NTSTATUS cli_qpathinfo1_recv(struct tevent_req *req,
584 time_t *change_time,
585 time_t *access_time,
586 time_t *write_time,
587 SMB_OFF_T *size,
588 uint16 *mode)
590 struct cli_qpathinfo1_state *state = tevent_req_data(
591 req, struct cli_qpathinfo1_state);
592 NTSTATUS status;
594 time_t (*date_fn)(const void *buf, int serverzone);
596 if (tevent_req_is_nterror(req, &status)) {
597 return status;
600 if (state->cli->win95) {
601 date_fn = make_unix_date;
602 } else {
603 date_fn = make_unix_date2;
606 if (change_time) {
607 *change_time = date_fn(state->data+0, state->cli->serverzone);
609 if (access_time) {
610 *access_time = date_fn(state->data+4, state->cli->serverzone);
612 if (write_time) {
613 *write_time = date_fn(state->data+8, state->cli->serverzone);
615 if (size) {
616 *size = IVAL(state->data, 12);
618 if (mode) {
619 *mode = SVAL(state->data, l1_attrFile);
621 return NT_STATUS_OK;
624 NTSTATUS cli_qpathinfo1(struct cli_state *cli,
625 const char *fname,
626 time_t *change_time,
627 time_t *access_time,
628 time_t *write_time,
629 SMB_OFF_T *size,
630 uint16 *mode)
632 TALLOC_CTX *frame = talloc_stackframe();
633 struct event_context *ev;
634 struct tevent_req *req;
635 NTSTATUS status = NT_STATUS_NO_MEMORY;
637 if (cli_has_async_calls(cli)) {
639 * Can't use sync call while an async call is in flight
641 status = NT_STATUS_INVALID_PARAMETER;
642 goto fail;
644 ev = event_context_init(frame);
645 if (ev == NULL) {
646 goto fail;
648 req = cli_qpathinfo1_send(frame, ev, cli, fname);
649 if (req == NULL) {
650 goto fail;
652 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
653 goto fail;
655 status = cli_qpathinfo1_recv(req, change_time, access_time,
656 write_time, size, mode);
657 fail:
658 TALLOC_FREE(frame);
659 if (!NT_STATUS_IS_OK(status)) {
660 cli_set_error(cli, status);
662 return status;
665 /****************************************************************************
666 Send a setpathinfo call.
667 ****************************************************************************/
669 bool cli_setpathinfo(struct cli_state *cli, const char *fname,
670 time_t create_time,
671 time_t access_time,
672 time_t write_time,
673 time_t change_time,
674 uint16 mode)
676 unsigned int data_len = 0;
677 unsigned int param_len = 0;
678 unsigned int rparam_len, rdata_len;
679 uint16 setup = TRANSACT2_SETPATHINFO;
680 char *param;
681 char data[40];
682 char *rparam=NULL, *rdata=NULL;
683 int count=8;
684 bool ret;
685 char *p;
686 size_t nlen = 2*(strlen(fname)+1);
688 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
689 if (!param) {
690 return false;
692 memset(param, '\0', 6);
693 memset(data, 0, sizeof(data));
695 p = param;
697 /* Add the information level */
698 SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
700 /* Skip reserved */
701 p += 6;
703 /* Add the file name */
704 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
706 param_len = PTR_DIFF(p, param);
708 p = data;
711 * Add the create, last access, modification, and status change times
713 put_long_date(p, create_time);
714 p += 8;
716 put_long_date(p, access_time);
717 p += 8;
719 put_long_date(p, write_time);
720 p += 8;
722 put_long_date(p, change_time);
723 p += 8;
725 /* Add attributes */
726 SIVAL(p, 0, mode);
727 p += 4;
729 /* Add padding */
730 SIVAL(p, 0, 0);
731 p += 4;
733 data_len = PTR_DIFF(p, data);
735 do {
736 ret = (cli_send_trans(cli, SMBtrans2,
737 NULL, /* Name */
738 -1, 0, /* fid, flags */
739 &setup, 1, 0, /* setup, length, max */
740 param, param_len, 10, /* param, length, max */
741 data, data_len, cli->max_xmit /* data, length, max */
742 ) &&
743 cli_receive_trans(cli, SMBtrans2,
744 &rparam, &rparam_len,
745 &rdata, &rdata_len));
746 if (!cli_is_dos_error(cli)) break;
747 if (!ret) {
748 /* we need to work around a Win95 bug - sometimes
749 it gives ERRSRV/ERRerror temprarily */
750 uint8 eclass;
751 uint32 ecode;
752 cli_dos_error(cli, &eclass, &ecode);
753 if (eclass != ERRSRV || ecode != ERRerror) break;
754 smb_msleep(100);
756 } while (count-- && ret==False);
758 SAFE_FREE(param);
759 if (!ret) {
760 return False;
763 SAFE_FREE(rdata);
764 SAFE_FREE(rparam);
765 return True;
768 /****************************************************************************
769 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
770 ****************************************************************************/
772 struct cli_qpathinfo2_state {
773 uint32_t num_data;
774 uint8_t *data;
777 static void cli_qpathinfo2_done(struct tevent_req *subreq);
779 struct tevent_req *cli_qpathinfo2_send(TALLOC_CTX *mem_ctx,
780 struct event_context *ev,
781 struct cli_state *cli,
782 const char *fname)
784 struct tevent_req *req = NULL, *subreq = NULL;
785 struct cli_qpathinfo2_state *state = NULL;
787 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo2_state);
788 if (req == NULL) {
789 return NULL;
791 subreq = cli_qpathinfo_send(state, ev, cli, fname,
792 SMB_QUERY_FILE_ALL_INFO,
793 68, cli->max_xmit);
794 if (tevent_req_nomem(subreq, req)) {
795 return tevent_req_post(req, ev);
797 tevent_req_set_callback(subreq, cli_qpathinfo2_done, req);
798 return req;
801 static void cli_qpathinfo2_done(struct tevent_req *subreq)
803 struct tevent_req *req = tevent_req_callback_data(
804 subreq, struct tevent_req);
805 struct cli_qpathinfo2_state *state = tevent_req_data(
806 req, struct cli_qpathinfo2_state);
807 NTSTATUS status;
809 status = cli_qpathinfo_recv(subreq, state, &state->data,
810 &state->num_data);
811 TALLOC_FREE(subreq);
812 if (!NT_STATUS_IS_OK(status)) {
813 tevent_req_nterror(req, status);
814 return;
816 tevent_req_done(req);
819 NTSTATUS cli_qpathinfo2_recv(struct tevent_req *req,
820 struct timespec *create_time,
821 struct timespec *access_time,
822 struct timespec *write_time,
823 struct timespec *change_time,
824 SMB_OFF_T *size, uint16 *mode,
825 SMB_INO_T *ino)
827 struct cli_qpathinfo2_state *state = tevent_req_data(
828 req, struct cli_qpathinfo2_state);
829 NTSTATUS status;
831 if (tevent_req_is_nterror(req, &status)) {
832 return status;
835 if (create_time) {
836 *create_time = interpret_long_date((char *)state->data+0);
838 if (access_time) {
839 *access_time = interpret_long_date((char *)state->data+8);
841 if (write_time) {
842 *write_time = interpret_long_date((char *)state->data+16);
844 if (change_time) {
845 *change_time = interpret_long_date((char *)state->data+24);
847 if (mode) {
848 *mode = SVAL(state->data, 32);
850 if (size) {
851 *size = IVAL2_TO_SMB_BIG_UINT(state->data,48);
853 if (ino) {
854 *ino = IVAL(state->data, 64);
856 return NT_STATUS_OK;
859 NTSTATUS cli_qpathinfo2(struct cli_state *cli, const char *fname,
860 struct timespec *create_time,
861 struct timespec *access_time,
862 struct timespec *write_time,
863 struct timespec *change_time,
864 SMB_OFF_T *size, uint16 *mode,
865 SMB_INO_T *ino)
867 TALLOC_CTX *frame = talloc_stackframe();
868 struct event_context *ev;
869 struct tevent_req *req;
870 NTSTATUS status = NT_STATUS_NO_MEMORY;
872 if (cli_has_async_calls(cli)) {
874 * Can't use sync call while an async call is in flight
876 status = NT_STATUS_INVALID_PARAMETER;
877 goto fail;
879 ev = event_context_init(frame);
880 if (ev == NULL) {
881 goto fail;
883 req = cli_qpathinfo2_send(frame, ev, cli, fname);
884 if (req == NULL) {
885 goto fail;
887 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
888 goto fail;
890 status = cli_qpathinfo2_recv(req, create_time, access_time,
891 write_time, change_time, size, mode, ino);
892 fail:
893 TALLOC_FREE(frame);
894 if (!NT_STATUS_IS_OK(status)) {
895 cli_set_error(cli, status);
897 return status;
900 /****************************************************************************
901 Get the stream info
902 ****************************************************************************/
904 static bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *data,
905 size_t data_len,
906 unsigned int *pnum_streams,
907 struct stream_struct **pstreams);
909 struct cli_qpathinfo_streams_state {
910 uint32_t num_data;
911 uint8_t *data;
914 static void cli_qpathinfo_streams_done(struct tevent_req *subreq);
916 struct tevent_req *cli_qpathinfo_streams_send(TALLOC_CTX *mem_ctx,
917 struct tevent_context *ev,
918 struct cli_state *cli,
919 const char *fname)
921 struct tevent_req *req = NULL, *subreq = NULL;
922 struct cli_qpathinfo_streams_state *state = NULL;
924 req = tevent_req_create(mem_ctx, &state,
925 struct cli_qpathinfo_streams_state);
926 if (req == NULL) {
927 return NULL;
929 subreq = cli_qpathinfo_send(state, ev, cli, fname,
930 SMB_FILE_STREAM_INFORMATION,
931 0, cli->max_xmit);
932 if (tevent_req_nomem(subreq, req)) {
933 return tevent_req_post(req, ev);
935 tevent_req_set_callback(subreq, cli_qpathinfo_streams_done, req);
936 return req;
939 static void cli_qpathinfo_streams_done(struct tevent_req *subreq)
941 struct tevent_req *req = tevent_req_callback_data(
942 subreq, struct tevent_req);
943 struct cli_qpathinfo_streams_state *state = tevent_req_data(
944 req, struct cli_qpathinfo_streams_state);
945 NTSTATUS status;
947 status = cli_qpathinfo_recv(subreq, state, &state->data,
948 &state->num_data);
949 TALLOC_FREE(subreq);
950 if (!NT_STATUS_IS_OK(status)) {
951 tevent_req_nterror(req, status);
952 return;
954 tevent_req_done(req);
957 NTSTATUS cli_qpathinfo_streams_recv(struct tevent_req *req,
958 TALLOC_CTX *mem_ctx,
959 unsigned int *pnum_streams,
960 struct stream_struct **pstreams)
962 struct cli_qpathinfo_streams_state *state = tevent_req_data(
963 req, struct cli_qpathinfo_streams_state);
964 NTSTATUS status;
966 if (tevent_req_is_nterror(req, &status)) {
967 return status;
969 if (!parse_streams_blob(mem_ctx, state->data, state->num_data,
970 pnum_streams, pstreams)) {
971 return NT_STATUS_INVALID_NETWORK_RESPONSE;
973 return NT_STATUS_OK;
976 NTSTATUS cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
977 TALLOC_CTX *mem_ctx,
978 unsigned int *pnum_streams,
979 struct stream_struct **pstreams)
981 TALLOC_CTX *frame = talloc_stackframe();
982 struct event_context *ev;
983 struct tevent_req *req;
984 NTSTATUS status = NT_STATUS_NO_MEMORY;
986 if (cli_has_async_calls(cli)) {
988 * Can't use sync call while an async call is in flight
990 status = NT_STATUS_INVALID_PARAMETER;
991 goto fail;
993 ev = event_context_init(frame);
994 if (ev == NULL) {
995 goto fail;
997 req = cli_qpathinfo_streams_send(frame, ev, cli, fname);
998 if (req == NULL) {
999 goto fail;
1001 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1002 goto fail;
1004 status = cli_qpathinfo_streams_recv(req, mem_ctx, pnum_streams,
1005 pstreams);
1006 fail:
1007 TALLOC_FREE(frame);
1008 if (!NT_STATUS_IS_OK(status)) {
1009 cli_set_error(cli, status);
1011 return status;
1014 static bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
1015 size_t data_len,
1016 unsigned int *pnum_streams,
1017 struct stream_struct **pstreams)
1019 unsigned int num_streams;
1020 struct stream_struct *streams;
1021 unsigned int ofs;
1023 num_streams = 0;
1024 streams = NULL;
1025 ofs = 0;
1027 while ((data_len > ofs) && (data_len - ofs >= 24)) {
1028 uint32_t nlen, len;
1029 size_t size;
1030 void *vstr;
1031 struct stream_struct *tmp;
1032 uint8_t *tmp_buf;
1034 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
1035 struct stream_struct,
1036 num_streams+1);
1038 if (tmp == NULL) {
1039 goto fail;
1041 streams = tmp;
1043 nlen = IVAL(rdata, ofs + 0x04);
1045 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
1046 rdata, ofs + 0x08);
1047 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
1048 rdata, ofs + 0x10);
1050 if (nlen > data_len - (ofs + 24)) {
1051 goto fail;
1055 * We need to null-terminate src, how do I do this with
1056 * convert_string_talloc??
1059 tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
1060 if (tmp_buf == NULL) {
1061 goto fail;
1064 memcpy(tmp_buf, rdata+ofs+24, nlen);
1065 tmp_buf[nlen] = 0;
1066 tmp_buf[nlen+1] = 0;
1068 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
1069 nlen+2, &vstr, &size, false))
1071 TALLOC_FREE(tmp_buf);
1072 goto fail;
1075 TALLOC_FREE(tmp_buf);
1076 streams[num_streams].name = (char *)vstr;
1077 num_streams++;
1079 len = IVAL(rdata, ofs);
1080 if (len > data_len - ofs) {
1081 goto fail;
1083 if (len == 0) break;
1084 ofs += len;
1087 *pnum_streams = num_streams;
1088 *pstreams = streams;
1089 return true;
1091 fail:
1092 TALLOC_FREE(streams);
1093 return false;
1096 /****************************************************************************
1097 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1098 ****************************************************************************/
1100 bool cli_qfilename(struct cli_state *cli, uint16_t fnum, char *name, size_t namelen)
1102 unsigned int data_len = 0;
1103 unsigned int param_len = 0;
1104 uint16 setup = TRANSACT2_QFILEINFO;
1105 char param[4];
1106 char *rparam=NULL, *rdata=NULL;
1108 param_len = 4;
1109 SSVAL(param, 0, fnum);
1110 SSVAL(param, 2, SMB_QUERY_FILE_NAME_INFO);
1112 if (!cli_send_trans(cli, SMBtrans2,
1113 NULL, /* name */
1114 -1, 0, /* fid, flags */
1115 &setup, 1, 0, /* setup, length, max */
1116 param, param_len, 2, /* param, length, max */
1117 NULL, data_len, cli->max_xmit /* data, length, max */
1118 )) {
1119 return False;
1122 if (!cli_receive_trans(cli, SMBtrans2,
1123 &rparam, &param_len,
1124 &rdata, &data_len)) {
1125 return False;
1128 if (!rdata || data_len < 4) {
1129 SAFE_FREE(rparam);
1130 SAFE_FREE(rdata);
1131 return False;
1134 clistr_pull(cli->inbuf, name, rdata+4, namelen, IVAL(rdata, 0),
1135 STR_UNICODE);
1137 SAFE_FREE(rparam);
1138 SAFE_FREE(rdata);
1140 return True;
1143 /****************************************************************************
1144 Send a qfileinfo call.
1145 ****************************************************************************/
1147 bool cli_qfileinfo(struct cli_state *cli, uint16_t fnum,
1148 uint16 *mode, SMB_OFF_T *size,
1149 struct timespec *create_time,
1150 struct timespec *access_time,
1151 struct timespec *write_time,
1152 struct timespec *change_time,
1153 SMB_INO_T *ino)
1155 uint32_t data_len = 0;
1156 uint16 setup;
1157 uint8_t param[4];
1158 uint8_t *rdata=NULL;
1159 NTSTATUS status;
1161 /* if its a win95 server then fail this - win95 totally screws it
1162 up */
1163 if (cli->win95) return False;
1165 SSVAL(param, 0, fnum);
1166 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
1168 SSVAL(&setup, 0, TRANSACT2_QFILEINFO);
1170 status = cli_trans(talloc_tos(), cli, SMBtrans2,
1171 NULL, -1, 0, 0, /* name, fid, function, flags */
1172 &setup, 1, 0, /* setup, length, max */
1173 param, 4, 2, /* param, length, max */
1174 NULL, 0, MIN(cli->max_xmit, 0xffff), /* data, length, max */
1175 NULL, /* recv_flags2 */
1176 NULL, 0, NULL, /* rsetup, length */
1177 NULL, 0, NULL, /* rparam, length */
1178 &rdata, 68, &data_len);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 return false;
1184 if (create_time) {
1185 *create_time = interpret_long_date((char *)rdata+0);
1187 if (access_time) {
1188 *access_time = interpret_long_date((char *)rdata+8);
1190 if (write_time) {
1191 *write_time = interpret_long_date((char *)rdata+16);
1193 if (change_time) {
1194 *change_time = interpret_long_date((char *)rdata+24);
1196 if (mode) {
1197 *mode = SVAL(rdata, 32);
1199 if (size) {
1200 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1202 if (ino) {
1203 *ino = IVAL(rdata, 64);
1206 TALLOC_FREE(rdata);
1207 return True;
1210 /****************************************************************************
1211 Send a qpathinfo BASIC_INFO call.
1212 ****************************************************************************/
1214 struct cli_qpathinfo_basic_state {
1215 uint32_t num_data;
1216 uint8_t *data;
1219 static void cli_qpathinfo_basic_done(struct tevent_req *subreq);
1221 struct tevent_req *cli_qpathinfo_basic_send(TALLOC_CTX *mem_ctx,
1222 struct event_context *ev,
1223 struct cli_state *cli,
1224 const char *fname)
1226 struct tevent_req *req = NULL, *subreq = NULL;
1227 struct cli_qpathinfo_basic_state *state = NULL;
1229 req = tevent_req_create(mem_ctx, &state,
1230 struct cli_qpathinfo_basic_state);
1231 if (req == NULL) {
1232 return NULL;
1234 subreq = cli_qpathinfo_send(state, ev, cli, fname,
1235 SMB_QUERY_FILE_BASIC_INFO,
1236 36, cli->max_xmit);
1237 if (tevent_req_nomem(subreq, req)) {
1238 return tevent_req_post(req, ev);
1240 tevent_req_set_callback(subreq, cli_qpathinfo_basic_done, req);
1241 return req;
1244 static void cli_qpathinfo_basic_done(struct tevent_req *subreq)
1246 struct tevent_req *req = tevent_req_callback_data(
1247 subreq, struct tevent_req);
1248 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1249 req, struct cli_qpathinfo_basic_state);
1250 NTSTATUS status;
1252 status = cli_qpathinfo_recv(subreq, state, &state->data,
1253 &state->num_data);
1254 TALLOC_FREE(subreq);
1255 if (!NT_STATUS_IS_OK(status)) {
1256 tevent_req_nterror(req, status);
1257 return;
1259 tevent_req_done(req);
1262 NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
1263 SMB_STRUCT_STAT *sbuf, uint32 *attributes)
1265 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1266 req, struct cli_qpathinfo_basic_state);
1267 NTSTATUS status;
1269 if (tevent_req_is_nterror(req, &status)) {
1270 return status;
1273 sbuf->st_ex_atime = interpret_long_date((char *)state->data+8);
1274 sbuf->st_ex_mtime = interpret_long_date((char *)state->data+16);
1275 sbuf->st_ex_ctime = interpret_long_date((char *)state->data+24);
1276 *attributes = IVAL(state->data, 32);
1277 return NT_STATUS_OK;
1280 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
1281 SMB_STRUCT_STAT *sbuf, uint32 *attributes)
1283 TALLOC_CTX *frame = talloc_stackframe();
1284 struct event_context *ev;
1285 struct tevent_req *req;
1286 NTSTATUS status = NT_STATUS_NO_MEMORY;
1288 if (cli_has_async_calls(cli)) {
1290 * Can't use sync call while an async call is in flight
1292 status = NT_STATUS_INVALID_PARAMETER;
1293 goto fail;
1295 ev = event_context_init(frame);
1296 if (ev == NULL) {
1297 goto fail;
1299 req = cli_qpathinfo_basic_send(frame, ev, cli, name);
1300 if (req == NULL) {
1301 goto fail;
1303 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1304 goto fail;
1306 status = cli_qpathinfo_basic_recv(req, sbuf, attributes);
1307 fail:
1308 TALLOC_FREE(frame);
1309 if (!NT_STATUS_IS_OK(status)) {
1310 cli_set_error(cli, status);
1312 return status;
1315 /****************************************************************************
1316 Send a qfileinfo call.
1317 ****************************************************************************/
1319 bool cli_qfileinfo_test(struct cli_state *cli, uint16_t fnum, int level, char **poutdata, uint32 *poutlen)
1321 unsigned int data_len = 0;
1322 unsigned int param_len = 0;
1323 uint16 setup = TRANSACT2_QFILEINFO;
1324 char param[4];
1325 char *rparam=NULL, *rdata=NULL;
1327 *poutdata = NULL;
1328 *poutlen = 0;
1330 /* if its a win95 server then fail this - win95 totally screws it
1331 up */
1332 if (cli->win95)
1333 return False;
1335 param_len = 4;
1337 SSVAL(param, 0, fnum);
1338 SSVAL(param, 2, level);
1340 if (!cli_send_trans(cli, SMBtrans2,
1341 NULL, /* name */
1342 -1, 0, /* fid, flags */
1343 &setup, 1, 0, /* setup, length, max */
1344 param, param_len, 2, /* param, length, max */
1345 NULL, data_len, cli->max_xmit /* data, length, max */
1346 )) {
1347 return False;
1350 if (!cli_receive_trans(cli, SMBtrans2,
1351 &rparam, &param_len,
1352 &rdata, &data_len)) {
1353 return False;
1356 *poutdata = (char *)memdup(rdata, data_len);
1357 if (!*poutdata) {
1358 SAFE_FREE(rdata);
1359 SAFE_FREE(rparam);
1360 return False;
1363 *poutlen = data_len;
1365 SAFE_FREE(rdata);
1366 SAFE_FREE(rparam);
1367 return True;
1370 /****************************************************************************
1371 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1372 ****************************************************************************/
1374 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1376 unsigned int data_len = 0;
1377 unsigned int param_len = 0;
1378 uint16 setup = TRANSACT2_QPATHINFO;
1379 char *param;
1380 char *rparam=NULL, *rdata=NULL;
1381 int count=8;
1382 char *p;
1383 bool ret;
1384 unsigned int len;
1385 size_t nlen = 2*(strlen(fname)+1);
1387 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
1388 if (!param) {
1389 return NT_STATUS_NO_MEMORY;
1391 p = param;
1392 memset(param, '\0', 6);
1393 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
1394 p += 6;
1395 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
1396 param_len = PTR_DIFF(p, param);
1398 do {
1399 ret = (cli_send_trans(cli, SMBtrans2,
1400 NULL, /* Name */
1401 -1, 0, /* fid, flags */
1402 &setup, 1, 0, /* setup, length, max */
1403 param, param_len, 10, /* param, length, max */
1404 NULL, data_len, cli->max_xmit /* data, length, max */
1405 ) &&
1406 cli_receive_trans(cli, SMBtrans2,
1407 &rparam, &param_len,
1408 &rdata, &data_len));
1409 if (!ret && cli_is_dos_error(cli)) {
1410 /* we need to work around a Win95 bug - sometimes
1411 it gives ERRSRV/ERRerror temprarily */
1412 uint8 eclass;
1413 uint32 ecode;
1414 cli_dos_error(cli, &eclass, &ecode);
1415 if (eclass != ERRSRV || ecode != ERRerror) break;
1416 smb_msleep(100);
1418 } while (count-- && ret==False);
1420 SAFE_FREE(param);
1422 if (!ret || !rdata || data_len < 4) {
1423 return NT_STATUS_UNSUCCESSFUL;
1426 len = IVAL(rdata, 0);
1428 if (len > data_len - 4) {
1429 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1432 clistr_pull(cli->inbuf, alt_name, rdata+4, sizeof(fstring), len,
1433 STR_UNICODE);
1435 SAFE_FREE(rdata);
1436 SAFE_FREE(rparam);
1438 return NT_STATUS_OK;