s4-dsdb/schema: Implement multi-pass working schema creation function
[Samba.git] / source3 / libsmb / clirap.c
blob6af3d153150147f5576a63e6bc160d2d940fcb65
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 NTSTATUS cli_qfilename(struct cli_state *cli, uint16_t fnum, char *name,
1101 size_t namelen)
1103 uint8_t *rdata;
1104 uint32_t num_rdata;
1105 NTSTATUS status;
1107 status = cli_qfileinfo(talloc_tos(), cli, fnum,
1108 SMB_QUERY_FILE_NAME_INFO,
1109 4, cli->max_xmit,
1110 &rdata, &num_rdata);
1111 if (!NT_STATUS_IS_OK(status)) {
1112 return status;
1115 clistr_pull(cli->inbuf, name, rdata+4, namelen, IVAL(rdata, 0),
1116 STR_UNICODE);
1117 TALLOC_FREE(rdata);
1118 return NT_STATUS_OK;
1121 /****************************************************************************
1122 Send a qfileinfo call.
1123 ****************************************************************************/
1125 NTSTATUS cli_qfileinfo_basic(struct cli_state *cli, uint16_t fnum,
1126 uint16 *mode, SMB_OFF_T *size,
1127 struct timespec *create_time,
1128 struct timespec *access_time,
1129 struct timespec *write_time,
1130 struct timespec *change_time,
1131 SMB_INO_T *ino)
1133 uint8_t *rdata;
1134 uint32_t num_rdata;
1135 NTSTATUS status;
1137 /* if its a win95 server then fail this - win95 totally screws it
1138 up */
1139 if (cli->win95) {
1140 return NT_STATUS_NOT_SUPPORTED;
1143 status = cli_qfileinfo(talloc_tos(), cli, fnum,
1144 SMB_QUERY_FILE_ALL_INFO,
1145 68, MIN(cli->max_xmit, 0xffff),
1146 &rdata, &num_rdata);
1147 if (!NT_STATUS_IS_OK(status)) {
1148 return status;
1151 if (create_time) {
1152 *create_time = interpret_long_date((char *)rdata+0);
1154 if (access_time) {
1155 *access_time = interpret_long_date((char *)rdata+8);
1157 if (write_time) {
1158 *write_time = interpret_long_date((char *)rdata+16);
1160 if (change_time) {
1161 *change_time = interpret_long_date((char *)rdata+24);
1163 if (mode) {
1164 *mode = SVAL(rdata, 32);
1166 if (size) {
1167 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1169 if (ino) {
1170 *ino = IVAL(rdata, 64);
1173 TALLOC_FREE(rdata);
1174 return NT_STATUS_OK;
1177 /****************************************************************************
1178 Send a qpathinfo BASIC_INFO call.
1179 ****************************************************************************/
1181 struct cli_qpathinfo_basic_state {
1182 uint32_t num_data;
1183 uint8_t *data;
1186 static void cli_qpathinfo_basic_done(struct tevent_req *subreq);
1188 struct tevent_req *cli_qpathinfo_basic_send(TALLOC_CTX *mem_ctx,
1189 struct event_context *ev,
1190 struct cli_state *cli,
1191 const char *fname)
1193 struct tevent_req *req = NULL, *subreq = NULL;
1194 struct cli_qpathinfo_basic_state *state = NULL;
1196 req = tevent_req_create(mem_ctx, &state,
1197 struct cli_qpathinfo_basic_state);
1198 if (req == NULL) {
1199 return NULL;
1201 subreq = cli_qpathinfo_send(state, ev, cli, fname,
1202 SMB_QUERY_FILE_BASIC_INFO,
1203 36, cli->max_xmit);
1204 if (tevent_req_nomem(subreq, req)) {
1205 return tevent_req_post(req, ev);
1207 tevent_req_set_callback(subreq, cli_qpathinfo_basic_done, req);
1208 return req;
1211 static void cli_qpathinfo_basic_done(struct tevent_req *subreq)
1213 struct tevent_req *req = tevent_req_callback_data(
1214 subreq, struct tevent_req);
1215 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1216 req, struct cli_qpathinfo_basic_state);
1217 NTSTATUS status;
1219 status = cli_qpathinfo_recv(subreq, state, &state->data,
1220 &state->num_data);
1221 TALLOC_FREE(subreq);
1222 if (!NT_STATUS_IS_OK(status)) {
1223 tevent_req_nterror(req, status);
1224 return;
1226 tevent_req_done(req);
1229 NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
1230 SMB_STRUCT_STAT *sbuf, uint32 *attributes)
1232 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1233 req, struct cli_qpathinfo_basic_state);
1234 NTSTATUS status;
1236 if (tevent_req_is_nterror(req, &status)) {
1237 return status;
1240 sbuf->st_ex_atime = interpret_long_date((char *)state->data+8);
1241 sbuf->st_ex_mtime = interpret_long_date((char *)state->data+16);
1242 sbuf->st_ex_ctime = interpret_long_date((char *)state->data+24);
1243 *attributes = IVAL(state->data, 32);
1244 return NT_STATUS_OK;
1247 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
1248 SMB_STRUCT_STAT *sbuf, uint32 *attributes)
1250 TALLOC_CTX *frame = talloc_stackframe();
1251 struct event_context *ev;
1252 struct tevent_req *req;
1253 NTSTATUS status = NT_STATUS_NO_MEMORY;
1255 if (cli_has_async_calls(cli)) {
1257 * Can't use sync call while an async call is in flight
1259 status = NT_STATUS_INVALID_PARAMETER;
1260 goto fail;
1262 ev = event_context_init(frame);
1263 if (ev == NULL) {
1264 goto fail;
1266 req = cli_qpathinfo_basic_send(frame, ev, cli, name);
1267 if (req == NULL) {
1268 goto fail;
1270 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1271 goto fail;
1273 status = cli_qpathinfo_basic_recv(req, sbuf, attributes);
1274 fail:
1275 TALLOC_FREE(frame);
1276 if (!NT_STATUS_IS_OK(status)) {
1277 cli_set_error(cli, status);
1279 return status;
1282 /****************************************************************************
1283 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1284 ****************************************************************************/
1286 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1288 unsigned int data_len = 0;
1289 unsigned int param_len = 0;
1290 uint16 setup = TRANSACT2_QPATHINFO;
1291 char *param;
1292 char *rparam=NULL, *rdata=NULL;
1293 int count=8;
1294 char *p;
1295 bool ret;
1296 unsigned int len;
1297 size_t nlen = 2*(strlen(fname)+1);
1299 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
1300 if (!param) {
1301 return NT_STATUS_NO_MEMORY;
1303 p = param;
1304 memset(param, '\0', 6);
1305 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
1306 p += 6;
1307 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
1308 param_len = PTR_DIFF(p, param);
1310 do {
1311 ret = (cli_send_trans(cli, SMBtrans2,
1312 NULL, /* Name */
1313 -1, 0, /* fid, flags */
1314 &setup, 1, 0, /* setup, length, max */
1315 param, param_len, 10, /* param, length, max */
1316 NULL, data_len, cli->max_xmit /* data, length, max */
1317 ) &&
1318 cli_receive_trans(cli, SMBtrans2,
1319 &rparam, &param_len,
1320 &rdata, &data_len));
1321 if (!ret && cli_is_dos_error(cli)) {
1322 /* we need to work around a Win95 bug - sometimes
1323 it gives ERRSRV/ERRerror temprarily */
1324 uint8 eclass;
1325 uint32 ecode;
1326 cli_dos_error(cli, &eclass, &ecode);
1327 if (eclass != ERRSRV || ecode != ERRerror) break;
1328 smb_msleep(100);
1330 } while (count-- && ret==False);
1332 SAFE_FREE(param);
1334 if (!ret || !rdata || data_len < 4) {
1335 return NT_STATUS_UNSUCCESSFUL;
1338 len = IVAL(rdata, 0);
1340 if (len > data_len - 4) {
1341 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1344 clistr_pull(cli->inbuf, alt_name, rdata+4, sizeof(fstring), len,
1345 STR_UNICODE);
1347 SAFE_FREE(rdata);
1348 SAFE_FREE(rparam);
1350 return NT_STATUS_OK;