s3: add functions to flush the idmap memcache
[Samba.git] / source3 / libsmb / clirap.c
blob781dbe64afec857a49189a5f2584d291a96fb951
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 #define PIPE_LANMAN "\\PIPE\\LANMAN"
30 /****************************************************************************
31 Call a remote api
32 ****************************************************************************/
34 bool cli_api(struct cli_state *cli,
35 char *param, int prcnt, int mprcnt,
36 char *data, int drcnt, int mdrcnt,
37 char **rparam, unsigned int *rprcnt,
38 char **rdata, unsigned int *rdrcnt)
40 NTSTATUS status;
42 uint8_t *my_rparam, *my_rdata;
43 uint32_t num_my_rparam, num_my_rdata;
45 status = cli_trans(talloc_tos(), cli, SMBtrans,
46 PIPE_LANMAN, 0, /* name, fid */
47 0, 0, /* function, flags */
48 NULL, 0, 0, /* setup */
49 (uint8_t *)param, prcnt, mprcnt, /* Params, length, max */
50 (uint8_t *)data, drcnt, mdrcnt, /* Data, length, max */
51 NULL, /* recv_flags2 */
52 NULL, 0, NULL, /* rsetup */
53 &my_rparam, 0, &num_my_rparam,
54 &my_rdata, 0, &num_my_rdata);
55 if (!NT_STATUS_IS_OK(status)) {
56 return false;
60 * I know this memcpy massively hurts, but there are just tons
61 * of callers of cli_api that eventually need changing to
62 * talloc
65 *rparam = (char *)memdup(my_rparam, num_my_rparam);
66 if (*rparam == NULL) {
67 goto fail;
69 *rprcnt = num_my_rparam;
70 TALLOC_FREE(my_rparam);
72 *rdata = (char *)memdup(my_rdata, num_my_rdata);
73 if (*rdata == NULL) {
74 goto fail;
76 *rdrcnt = num_my_rdata;
77 TALLOC_FREE(my_rdata);
79 return true;
80 fail:
81 TALLOC_FREE(my_rdata);
82 TALLOC_FREE(my_rparam);
83 *rparam = NULL;
84 *rprcnt = 0;
85 *rdata = NULL;
86 *rdrcnt = 0;
87 return false;
90 /****************************************************************************
91 Perform a NetWkstaUserLogon.
92 ****************************************************************************/
94 bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
96 char *rparam = NULL;
97 char *rdata = NULL;
98 char *p;
99 unsigned int rdrcnt,rprcnt;
100 char param[1024];
102 memset(param, 0, sizeof(param));
104 /* send a SMBtrans command with api NetWkstaUserLogon */
105 p = param;
106 SSVAL(p,0,132); /* api number */
107 p += 2;
108 strlcpy(p,"OOWb54WrLh",sizeof(param)-PTR_DIFF(p,param));
109 p = skip_string(param,sizeof(param),p);
110 strlcpy(p,"WB21BWDWWDDDDDDDzzzD",sizeof(param)-PTR_DIFF(p,param));
111 p = skip_string(param,sizeof(param),p);
112 SSVAL(p,0,1);
113 p += 2;
114 strlcpy(p,user,sizeof(param)-PTR_DIFF(p,param));
115 strupper_m(p);
116 p += 21;
117 p++;
118 p += 15;
119 p++;
120 strlcpy(p, workstation,sizeof(param)-PTR_DIFF(p,param));
121 strupper_m(p);
122 p += 16;
123 SSVAL(p, 0, CLI_BUFFER_SIZE);
124 p += 2;
125 SSVAL(p, 0, CLI_BUFFER_SIZE);
126 p += 2;
128 if (cli_api(cli,
129 param, PTR_DIFF(p,param),1024, /* param, length, max */
130 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
131 &rparam, &rprcnt, /* return params, return size */
132 &rdata, &rdrcnt /* return data, return size */
133 )) {
134 cli->rap_error = rparam? SVAL(rparam,0) : -1;
135 p = rdata;
137 if (cli->rap_error == 0) {
138 DEBUG(4,("NetWkstaUserLogon success\n"));
139 cli->privileges = SVAL(p, 24);
140 /* The cli->eff_name field used to be set here
141 but it wasn't used anywhere else. */
142 } else {
143 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
147 SAFE_FREE(rparam);
148 SAFE_FREE(rdata);
149 return (cli->rap_error == 0);
152 /****************************************************************************
153 Call a NetShareEnum - try and browse available connections on a host.
154 ****************************************************************************/
156 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
158 char *rparam = NULL;
159 char *rdata = NULL;
160 char *p;
161 unsigned int rdrcnt,rprcnt;
162 char param[1024];
163 int count = -1;
165 /* now send a SMBtrans command with api RNetShareEnum */
166 p = param;
167 SSVAL(p,0,0); /* api number */
168 p += 2;
169 strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
170 p = skip_string(param,sizeof(param),p);
171 strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
172 p = skip_string(param,sizeof(param),p);
173 SSVAL(p,0,1);
175 * Win2k needs a *smaller* buffer than 0xFFFF here -
176 * it returns "out of server memory" with 0xFFFF !!! JRA.
178 SSVAL(p,2,0xFFE0);
179 p += 4;
181 if (cli_api(cli,
182 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
183 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
184 &rparam, &rprcnt, /* return params, length */
185 &rdata, &rdrcnt)) /* return data, length */
187 int res = rparam? SVAL(rparam,0) : -1;
189 if (res == 0 || res == ERRmoredata) {
190 int converter=SVAL(rparam,2);
191 int i;
192 char *rdata_end = rdata + rdrcnt;
194 count=SVAL(rparam,4);
195 p = rdata;
197 for (i=0;i<count;i++,p+=20) {
198 char *sname;
199 int type;
200 int comment_offset;
201 const char *cmnt;
202 const char *p1;
203 char *s1, *s2;
204 size_t len;
205 TALLOC_CTX *frame = talloc_stackframe();
207 if (p + 20 > rdata_end) {
208 TALLOC_FREE(frame);
209 break;
212 sname = p;
213 type = SVAL(p,14);
214 comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
215 if (comment_offset < 0 ||
216 comment_offset > (int)rdrcnt) {
217 TALLOC_FREE(frame);
218 break;
220 cmnt = comment_offset?(rdata+comment_offset):"";
222 /* Work out the comment length. */
223 for (p1 = cmnt, len = 0; *p1 &&
224 p1 < rdata_end; len++)
225 p1++;
226 if (!*p1) {
227 len++;
229 pull_string_talloc(frame,rdata,0,
230 &s1,sname,14,STR_ASCII);
231 pull_string_talloc(frame,rdata,0,
232 &s2,cmnt,len,STR_ASCII);
233 if (!s1 || !s2) {
234 TALLOC_FREE(frame);
235 continue;
238 fn(s1, type, s2, state);
240 TALLOC_FREE(frame);
242 } else {
243 DEBUG(4,("NetShareEnum res=%d\n", res));
245 } else {
246 DEBUG(4,("NetShareEnum failed\n"));
249 SAFE_FREE(rparam);
250 SAFE_FREE(rdata);
252 return count;
255 /****************************************************************************
256 Call a NetServerEnum for the specified workgroup and servertype mask. This
257 function then calls the specified callback function for each name returned.
259 The callback function takes 4 arguments: the machine name, the server type,
260 the comment and a state pointer.
261 ****************************************************************************/
263 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
264 void (*fn)(const char *, uint32, const char *, void *),
265 void *state)
267 char *rparam = NULL;
268 char *rdata = NULL;
269 char *rdata_end = NULL;
270 unsigned int rdrcnt,rprcnt;
271 char *p;
272 char param[1024];
273 int uLevel = 1;
274 size_t len;
275 uint32 func = RAP_NetServerEnum2;
276 char *last_entry = NULL;
277 int total_cnt = 0;
278 int return_cnt = 0;
279 int res;
281 errno = 0; /* reset */
284 * This may take more than one transaction, so we should loop until
285 * we no longer get a more data to process or we have all of the
286 * items.
288 do {
289 /* send a SMBtrans command with api NetServerEnum */
290 p = param;
291 SIVAL(p,0,func); /* api number */
292 p += 2;
294 if (func == RAP_NetServerEnum3) {
295 strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
296 } else {
297 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
300 p = skip_string(param, sizeof(param), p);
301 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
303 p = skip_string(param, sizeof(param), p);
304 SSVAL(p,0,uLevel);
305 SSVAL(p,2,CLI_BUFFER_SIZE);
306 p += 4;
307 SIVAL(p,0,stype);
308 p += 4;
310 /* If we have more data, tell the server where
311 * to continue from.
313 len = push_ascii(p,
314 workgroup,
315 sizeof(param) - PTR_DIFF(p,param) - 1,
316 STR_TERMINATE|STR_UPPER);
318 if (len == (size_t)-1) {
319 SAFE_FREE(last_entry);
320 return false;
322 p += len;
324 if (func == RAP_NetServerEnum3) {
325 len = push_ascii(p,
326 last_entry ? last_entry : "",
327 sizeof(param) - PTR_DIFF(p,param) - 1,
328 STR_TERMINATE);
330 if (len == (size_t)-1) {
331 SAFE_FREE(last_entry);
332 return false;
334 p += len;
337 /* Next time through we need to use the continue api */
338 func = RAP_NetServerEnum3;
340 if (!cli_api(cli,
341 param, PTR_DIFF(p,param), 8, /* params, length, max */
342 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
343 &rparam, &rprcnt, /* return params, return size */
344 &rdata, &rdrcnt)) { /* return data, return size */
346 /* break out of the loop on error */
347 res = -1;
348 break;
351 rdata_end = rdata + rdrcnt;
352 res = rparam ? SVAL(rparam,0) : -1;
354 if (res == 0 || res == ERRmoredata ||
355 (res != -1 && cli_errno(cli) == 0)) {
356 char *sname = NULL;
357 int i, count;
358 int converter=SVAL(rparam,2);
360 /* Get the number of items returned in this buffer */
361 count = SVAL(rparam, 4);
363 /* The next field contains the number of items left,
364 * including those returned in this buffer. So the
365 * first time through this should contain all of the
366 * entries.
368 if (total_cnt == 0) {
369 total_cnt = SVAL(rparam, 6);
372 /* Keep track of how many we have read */
373 return_cnt += count;
374 p = rdata;
376 /* The last name in the previous NetServerEnum reply is
377 * sent back to server in the NetServerEnum3 request
378 * (last_entry). The next reply should repeat this entry
379 * as the first element. We have no proof that this is
380 * always true, but from traces that seems to be the
381 * behavior from Window Servers. So first lets do a lot
382 * of checking, just being paranoid. If the string
383 * matches then we already saw this entry so skip it.
385 * NOTE: sv1_name field must be null terminated and has
386 * a max size of 16 (NetBIOS Name).
388 if (last_entry && count && p &&
389 (strncmp(last_entry, p, 16) == 0)) {
390 count -= 1; /* Skip this entry */
391 return_cnt = -1; /* Not part of total, so don't count. */
392 p = rdata + 26; /* Skip the whole record */
395 for (i = 0; i < count; i++, p += 26) {
396 int comment_offset;
397 const char *cmnt;
398 const char *p1;
399 char *s1, *s2;
400 TALLOC_CTX *frame = talloc_stackframe();
401 uint32_t entry_stype;
403 if (p + 26 > rdata_end) {
404 TALLOC_FREE(frame);
405 break;
408 sname = p;
409 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
410 cmnt = comment_offset?(rdata+comment_offset):"";
412 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
413 TALLOC_FREE(frame);
414 continue;
417 /* Work out the comment length. */
418 for (p1 = cmnt, len = 0; *p1 &&
419 p1 < rdata_end; len++)
420 p1++;
421 if (!*p1) {
422 len++;
425 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
427 pull_string_talloc(frame,rdata,0,
428 &s1,sname,16,STR_ASCII);
429 pull_string_talloc(frame,rdata,0,
430 &s2,cmnt,len,STR_ASCII);
432 if (!s1 || !s2) {
433 TALLOC_FREE(frame);
434 continue;
437 fn(s1, entry_stype, s2, state);
438 TALLOC_FREE(frame);
441 /* We are done with the old last entry, so now we can free it */
442 if (last_entry) {
443 SAFE_FREE(last_entry); /* This will set it to null */
446 /* We always make a copy of the last entry if we have one */
447 if (sname) {
448 last_entry = smb_xstrdup(sname);
451 /* If we have more data, but no last entry then error out */
452 if (!last_entry && (res == ERRmoredata)) {
453 errno = EINVAL;
454 res = 0;
459 SAFE_FREE(rparam);
460 SAFE_FREE(rdata);
461 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
463 SAFE_FREE(rparam);
464 SAFE_FREE(rdata);
465 SAFE_FREE(last_entry);
467 if (res == -1) {
468 errno = cli_errno(cli);
469 } else {
470 if (!return_cnt) {
471 /* this is a very special case, when the domain master for the
472 work group isn't part of the work group itself, there is something
473 wild going on */
474 errno = ENOENT;
478 return(return_cnt > 0);
481 /****************************************************************************
482 Send a SamOEMChangePassword command.
483 ****************************************************************************/
485 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
486 const char *old_password)
488 char param[1024];
489 unsigned char data[532];
490 char *p = param;
491 unsigned char old_pw_hash[16];
492 unsigned char new_pw_hash[16];
493 unsigned int data_len;
494 unsigned int param_len = 0;
495 char *rparam = NULL;
496 char *rdata = NULL;
497 unsigned int rprcnt, rdrcnt;
499 if (strlen(user) >= sizeof(fstring)-1) {
500 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
501 return False;
504 SSVAL(p,0,214); /* SamOEMChangePassword command. */
505 p += 2;
506 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
507 p = skip_string(param,sizeof(param),p);
508 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
509 p = skip_string(param,sizeof(param),p);
510 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
511 p = skip_string(param,sizeof(param),p);
512 SSVAL(p,0,532);
513 p += 2;
515 param_len = PTR_DIFF(p,param);
518 * Get the Lanman hash of the old password, we
519 * use this as the key to make_oem_passwd_hash().
521 E_deshash(old_password, old_pw_hash);
523 encode_pw_buffer(data, new_password, STR_ASCII);
525 #ifdef DEBUG_PASSWORD
526 DEBUG(100,("make_oem_passwd_hash\n"));
527 dump_data(100, data, 516);
528 #endif
529 arcfour_crypt( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
532 * Now place the old password hash in the data.
534 E_deshash(new_password, new_pw_hash);
536 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
538 data_len = 532;
540 if (!cli_api(cli,
541 param, param_len, 4, /* param, length, max */
542 (char *)data, data_len, 0, /* data, length, max */
543 &rparam, &rprcnt,
544 &rdata, &rdrcnt)) {
545 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
546 user ));
547 return False;
550 if (rparam) {
551 cli->rap_error = SVAL(rparam,0);
554 SAFE_FREE(rparam);
555 SAFE_FREE(rdata);
557 return (cli->rap_error == 0);
560 /****************************************************************************
561 Send a qpathinfo call.
562 ****************************************************************************/
564 struct cli_qpathinfo1_state {
565 struct cli_state *cli;
566 uint32_t num_data;
567 uint8_t *data;
570 static void cli_qpathinfo1_done(struct tevent_req *subreq);
572 struct tevent_req *cli_qpathinfo1_send(TALLOC_CTX *mem_ctx,
573 struct event_context *ev,
574 struct cli_state *cli,
575 const char *fname)
577 struct tevent_req *req = NULL, *subreq = NULL;
578 struct cli_qpathinfo1_state *state = NULL;
580 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo1_state);
581 if (req == NULL) {
582 return NULL;
584 state->cli = cli;
585 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_INFO_STANDARD,
586 22, cli->max_xmit);
587 if (tevent_req_nomem(subreq, req)) {
588 return tevent_req_post(req, ev);
590 tevent_req_set_callback(subreq, cli_qpathinfo1_done, req);
591 return req;
594 static void cli_qpathinfo1_done(struct tevent_req *subreq)
596 struct tevent_req *req = tevent_req_callback_data(
597 subreq, struct tevent_req);
598 struct cli_qpathinfo1_state *state = tevent_req_data(
599 req, struct cli_qpathinfo1_state);
600 NTSTATUS status;
602 status = cli_qpathinfo_recv(subreq, state, &state->data,
603 &state->num_data);
604 TALLOC_FREE(subreq);
605 if (!NT_STATUS_IS_OK(status)) {
606 tevent_req_nterror(req, status);
607 return;
609 tevent_req_done(req);
612 NTSTATUS cli_qpathinfo1_recv(struct tevent_req *req,
613 time_t *change_time,
614 time_t *access_time,
615 time_t *write_time,
616 SMB_OFF_T *size,
617 uint16 *mode)
619 struct cli_qpathinfo1_state *state = tevent_req_data(
620 req, struct cli_qpathinfo1_state);
621 NTSTATUS status;
623 time_t (*date_fn)(const void *buf, int serverzone);
625 if (tevent_req_is_nterror(req, &status)) {
626 return status;
629 if (state->cli->win95) {
630 date_fn = make_unix_date;
631 } else {
632 date_fn = make_unix_date2;
635 if (change_time) {
636 *change_time = date_fn(state->data+0, state->cli->serverzone);
638 if (access_time) {
639 *access_time = date_fn(state->data+4, state->cli->serverzone);
641 if (write_time) {
642 *write_time = date_fn(state->data+8, state->cli->serverzone);
644 if (size) {
645 *size = IVAL(state->data, 12);
647 if (mode) {
648 *mode = SVAL(state->data, l1_attrFile);
650 return NT_STATUS_OK;
653 NTSTATUS cli_qpathinfo1(struct cli_state *cli,
654 const char *fname,
655 time_t *change_time,
656 time_t *access_time,
657 time_t *write_time,
658 SMB_OFF_T *size,
659 uint16 *mode)
661 TALLOC_CTX *frame = talloc_stackframe();
662 struct event_context *ev;
663 struct tevent_req *req;
664 NTSTATUS status = NT_STATUS_NO_MEMORY;
666 if (cli_has_async_calls(cli)) {
668 * Can't use sync call while an async call is in flight
670 status = NT_STATUS_INVALID_PARAMETER;
671 goto fail;
673 ev = event_context_init(frame);
674 if (ev == NULL) {
675 goto fail;
677 req = cli_qpathinfo1_send(frame, ev, cli, fname);
678 if (req == NULL) {
679 goto fail;
681 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
682 goto fail;
684 status = cli_qpathinfo1_recv(req, change_time, access_time,
685 write_time, size, mode);
686 fail:
687 TALLOC_FREE(frame);
688 if (!NT_STATUS_IS_OK(status)) {
689 cli_set_error(cli, status);
691 return status;
694 /****************************************************************************
695 Send a setpathinfo call.
696 ****************************************************************************/
698 NTSTATUS cli_setpathinfo_basic(struct cli_state *cli, const char *fname,
699 time_t create_time,
700 time_t access_time,
701 time_t write_time,
702 time_t change_time,
703 uint16 mode)
705 unsigned int data_len = 0;
706 char data[40];
707 char *p;
709 p = data;
712 * Add the create, last access, modification, and status change times
714 put_long_date(p, create_time);
715 p += 8;
717 put_long_date(p, access_time);
718 p += 8;
720 put_long_date(p, write_time);
721 p += 8;
723 put_long_date(p, change_time);
724 p += 8;
726 /* Add attributes */
727 SIVAL(p, 0, mode);
728 p += 4;
730 /* Add padding */
731 SIVAL(p, 0, 0);
732 p += 4;
734 data_len = PTR_DIFF(p, data);
736 return cli_setpathinfo(cli, SMB_FILE_BASIC_INFORMATION, fname,
737 (uint8_t *)data, data_len);
740 /****************************************************************************
741 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
742 ****************************************************************************/
744 struct cli_qpathinfo2_state {
745 uint32_t num_data;
746 uint8_t *data;
749 static void cli_qpathinfo2_done(struct tevent_req *subreq);
751 struct tevent_req *cli_qpathinfo2_send(TALLOC_CTX *mem_ctx,
752 struct event_context *ev,
753 struct cli_state *cli,
754 const char *fname)
756 struct tevent_req *req = NULL, *subreq = NULL;
757 struct cli_qpathinfo2_state *state = NULL;
759 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo2_state);
760 if (req == NULL) {
761 return NULL;
763 subreq = cli_qpathinfo_send(state, ev, cli, fname,
764 SMB_QUERY_FILE_ALL_INFO,
765 68, cli->max_xmit);
766 if (tevent_req_nomem(subreq, req)) {
767 return tevent_req_post(req, ev);
769 tevent_req_set_callback(subreq, cli_qpathinfo2_done, req);
770 return req;
773 static void cli_qpathinfo2_done(struct tevent_req *subreq)
775 struct tevent_req *req = tevent_req_callback_data(
776 subreq, struct tevent_req);
777 struct cli_qpathinfo2_state *state = tevent_req_data(
778 req, struct cli_qpathinfo2_state);
779 NTSTATUS status;
781 status = cli_qpathinfo_recv(subreq, state, &state->data,
782 &state->num_data);
783 TALLOC_FREE(subreq);
784 if (!NT_STATUS_IS_OK(status)) {
785 tevent_req_nterror(req, status);
786 return;
788 tevent_req_done(req);
791 NTSTATUS cli_qpathinfo2_recv(struct tevent_req *req,
792 struct timespec *create_time,
793 struct timespec *access_time,
794 struct timespec *write_time,
795 struct timespec *change_time,
796 SMB_OFF_T *size, uint16 *mode,
797 SMB_INO_T *ino)
799 struct cli_qpathinfo2_state *state = tevent_req_data(
800 req, struct cli_qpathinfo2_state);
801 NTSTATUS status;
803 if (tevent_req_is_nterror(req, &status)) {
804 return status;
807 if (create_time) {
808 *create_time = interpret_long_date((char *)state->data+0);
810 if (access_time) {
811 *access_time = interpret_long_date((char *)state->data+8);
813 if (write_time) {
814 *write_time = interpret_long_date((char *)state->data+16);
816 if (change_time) {
817 *change_time = interpret_long_date((char *)state->data+24);
819 if (mode) {
820 *mode = SVAL(state->data, 32);
822 if (size) {
823 *size = IVAL2_TO_SMB_BIG_UINT(state->data,48);
825 if (ino) {
826 *ino = IVAL(state->data, 64);
828 return NT_STATUS_OK;
831 NTSTATUS cli_qpathinfo2(struct cli_state *cli, const char *fname,
832 struct timespec *create_time,
833 struct timespec *access_time,
834 struct timespec *write_time,
835 struct timespec *change_time,
836 SMB_OFF_T *size, uint16 *mode,
837 SMB_INO_T *ino)
839 TALLOC_CTX *frame = talloc_stackframe();
840 struct event_context *ev;
841 struct tevent_req *req;
842 NTSTATUS status = NT_STATUS_NO_MEMORY;
844 if (cli_has_async_calls(cli)) {
846 * Can't use sync call while an async call is in flight
848 status = NT_STATUS_INVALID_PARAMETER;
849 goto fail;
851 ev = event_context_init(frame);
852 if (ev == NULL) {
853 goto fail;
855 req = cli_qpathinfo2_send(frame, ev, cli, fname);
856 if (req == NULL) {
857 goto fail;
859 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
860 goto fail;
862 status = cli_qpathinfo2_recv(req, create_time, access_time,
863 write_time, change_time, size, mode, ino);
864 fail:
865 TALLOC_FREE(frame);
866 if (!NT_STATUS_IS_OK(status)) {
867 cli_set_error(cli, status);
869 return status;
872 /****************************************************************************
873 Get the stream info
874 ****************************************************************************/
876 static bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *data,
877 size_t data_len,
878 unsigned int *pnum_streams,
879 struct stream_struct **pstreams);
881 struct cli_qpathinfo_streams_state {
882 uint32_t num_data;
883 uint8_t *data;
886 static void cli_qpathinfo_streams_done(struct tevent_req *subreq);
888 struct tevent_req *cli_qpathinfo_streams_send(TALLOC_CTX *mem_ctx,
889 struct tevent_context *ev,
890 struct cli_state *cli,
891 const char *fname)
893 struct tevent_req *req = NULL, *subreq = NULL;
894 struct cli_qpathinfo_streams_state *state = NULL;
896 req = tevent_req_create(mem_ctx, &state,
897 struct cli_qpathinfo_streams_state);
898 if (req == NULL) {
899 return NULL;
901 subreq = cli_qpathinfo_send(state, ev, cli, fname,
902 SMB_FILE_STREAM_INFORMATION,
903 0, cli->max_xmit);
904 if (tevent_req_nomem(subreq, req)) {
905 return tevent_req_post(req, ev);
907 tevent_req_set_callback(subreq, cli_qpathinfo_streams_done, req);
908 return req;
911 static void cli_qpathinfo_streams_done(struct tevent_req *subreq)
913 struct tevent_req *req = tevent_req_callback_data(
914 subreq, struct tevent_req);
915 struct cli_qpathinfo_streams_state *state = tevent_req_data(
916 req, struct cli_qpathinfo_streams_state);
917 NTSTATUS status;
919 status = cli_qpathinfo_recv(subreq, state, &state->data,
920 &state->num_data);
921 TALLOC_FREE(subreq);
922 if (!NT_STATUS_IS_OK(status)) {
923 tevent_req_nterror(req, status);
924 return;
926 tevent_req_done(req);
929 NTSTATUS cli_qpathinfo_streams_recv(struct tevent_req *req,
930 TALLOC_CTX *mem_ctx,
931 unsigned int *pnum_streams,
932 struct stream_struct **pstreams)
934 struct cli_qpathinfo_streams_state *state = tevent_req_data(
935 req, struct cli_qpathinfo_streams_state);
936 NTSTATUS status;
938 if (tevent_req_is_nterror(req, &status)) {
939 return status;
941 if (!parse_streams_blob(mem_ctx, state->data, state->num_data,
942 pnum_streams, pstreams)) {
943 return NT_STATUS_INVALID_NETWORK_RESPONSE;
945 return NT_STATUS_OK;
948 NTSTATUS cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
949 TALLOC_CTX *mem_ctx,
950 unsigned int *pnum_streams,
951 struct stream_struct **pstreams)
953 TALLOC_CTX *frame = talloc_stackframe();
954 struct event_context *ev;
955 struct tevent_req *req;
956 NTSTATUS status = NT_STATUS_NO_MEMORY;
958 if (cli_has_async_calls(cli)) {
960 * Can't use sync call while an async call is in flight
962 status = NT_STATUS_INVALID_PARAMETER;
963 goto fail;
965 ev = event_context_init(frame);
966 if (ev == NULL) {
967 goto fail;
969 req = cli_qpathinfo_streams_send(frame, ev, cli, fname);
970 if (req == NULL) {
971 goto fail;
973 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
974 goto fail;
976 status = cli_qpathinfo_streams_recv(req, mem_ctx, pnum_streams,
977 pstreams);
978 fail:
979 TALLOC_FREE(frame);
980 if (!NT_STATUS_IS_OK(status)) {
981 cli_set_error(cli, status);
983 return status;
986 static bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
987 size_t data_len,
988 unsigned int *pnum_streams,
989 struct stream_struct **pstreams)
991 unsigned int num_streams;
992 struct stream_struct *streams;
993 unsigned int ofs;
995 num_streams = 0;
996 streams = NULL;
997 ofs = 0;
999 while ((data_len > ofs) && (data_len - ofs >= 24)) {
1000 uint32_t nlen, len;
1001 size_t size;
1002 void *vstr;
1003 struct stream_struct *tmp;
1004 uint8_t *tmp_buf;
1006 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
1007 struct stream_struct,
1008 num_streams+1);
1010 if (tmp == NULL) {
1011 goto fail;
1013 streams = tmp;
1015 nlen = IVAL(rdata, ofs + 0x04);
1017 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
1018 rdata, ofs + 0x08);
1019 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
1020 rdata, ofs + 0x10);
1022 if (nlen > data_len - (ofs + 24)) {
1023 goto fail;
1027 * We need to null-terminate src, how do I do this with
1028 * convert_string_talloc??
1031 tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
1032 if (tmp_buf == NULL) {
1033 goto fail;
1036 memcpy(tmp_buf, rdata+ofs+24, nlen);
1037 tmp_buf[nlen] = 0;
1038 tmp_buf[nlen+1] = 0;
1040 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
1041 nlen+2, &vstr, &size, false))
1043 TALLOC_FREE(tmp_buf);
1044 goto fail;
1047 TALLOC_FREE(tmp_buf);
1048 streams[num_streams].name = (char *)vstr;
1049 num_streams++;
1051 len = IVAL(rdata, ofs);
1052 if (len > data_len - ofs) {
1053 goto fail;
1055 if (len == 0) break;
1056 ofs += len;
1059 *pnum_streams = num_streams;
1060 *pstreams = streams;
1061 return true;
1063 fail:
1064 TALLOC_FREE(streams);
1065 return false;
1068 /****************************************************************************
1069 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1070 ****************************************************************************/
1072 NTSTATUS cli_qfilename(struct cli_state *cli, uint16_t fnum, char *name,
1073 size_t namelen)
1075 uint8_t *rdata;
1076 uint32_t num_rdata;
1077 NTSTATUS status;
1079 status = cli_qfileinfo(talloc_tos(), cli, fnum,
1080 SMB_QUERY_FILE_NAME_INFO,
1081 4, cli->max_xmit,
1082 &rdata, &num_rdata);
1083 if (!NT_STATUS_IS_OK(status)) {
1084 return status;
1087 clistr_pull(cli->inbuf, name, rdata+4, namelen, IVAL(rdata, 0),
1088 STR_UNICODE);
1089 TALLOC_FREE(rdata);
1090 return NT_STATUS_OK;
1093 /****************************************************************************
1094 Send a qfileinfo call.
1095 ****************************************************************************/
1097 NTSTATUS cli_qfileinfo_basic(struct cli_state *cli, uint16_t fnum,
1098 uint16 *mode, SMB_OFF_T *size,
1099 struct timespec *create_time,
1100 struct timespec *access_time,
1101 struct timespec *write_time,
1102 struct timespec *change_time,
1103 SMB_INO_T *ino)
1105 uint8_t *rdata;
1106 uint32_t num_rdata;
1107 NTSTATUS status;
1109 /* if its a win95 server then fail this - win95 totally screws it
1110 up */
1111 if (cli->win95) {
1112 return NT_STATUS_NOT_SUPPORTED;
1115 status = cli_qfileinfo(talloc_tos(), cli, fnum,
1116 SMB_QUERY_FILE_ALL_INFO,
1117 68, MIN(cli->max_xmit, 0xffff),
1118 &rdata, &num_rdata);
1119 if (!NT_STATUS_IS_OK(status)) {
1120 return status;
1123 if (create_time) {
1124 *create_time = interpret_long_date((char *)rdata+0);
1126 if (access_time) {
1127 *access_time = interpret_long_date((char *)rdata+8);
1129 if (write_time) {
1130 *write_time = interpret_long_date((char *)rdata+16);
1132 if (change_time) {
1133 *change_time = interpret_long_date((char *)rdata+24);
1135 if (mode) {
1136 *mode = SVAL(rdata, 32);
1138 if (size) {
1139 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1141 if (ino) {
1142 *ino = IVAL(rdata, 64);
1145 TALLOC_FREE(rdata);
1146 return NT_STATUS_OK;
1149 /****************************************************************************
1150 Send a qpathinfo BASIC_INFO call.
1151 ****************************************************************************/
1153 struct cli_qpathinfo_basic_state {
1154 uint32_t num_data;
1155 uint8_t *data;
1158 static void cli_qpathinfo_basic_done(struct tevent_req *subreq);
1160 struct tevent_req *cli_qpathinfo_basic_send(TALLOC_CTX *mem_ctx,
1161 struct event_context *ev,
1162 struct cli_state *cli,
1163 const char *fname)
1165 struct tevent_req *req = NULL, *subreq = NULL;
1166 struct cli_qpathinfo_basic_state *state = NULL;
1168 req = tevent_req_create(mem_ctx, &state,
1169 struct cli_qpathinfo_basic_state);
1170 if (req == NULL) {
1171 return NULL;
1173 subreq = cli_qpathinfo_send(state, ev, cli, fname,
1174 SMB_QUERY_FILE_BASIC_INFO,
1175 36, cli->max_xmit);
1176 if (tevent_req_nomem(subreq, req)) {
1177 return tevent_req_post(req, ev);
1179 tevent_req_set_callback(subreq, cli_qpathinfo_basic_done, req);
1180 return req;
1183 static void cli_qpathinfo_basic_done(struct tevent_req *subreq)
1185 struct tevent_req *req = tevent_req_callback_data(
1186 subreq, struct tevent_req);
1187 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1188 req, struct cli_qpathinfo_basic_state);
1189 NTSTATUS status;
1191 status = cli_qpathinfo_recv(subreq, state, &state->data,
1192 &state->num_data);
1193 TALLOC_FREE(subreq);
1194 if (!NT_STATUS_IS_OK(status)) {
1195 tevent_req_nterror(req, status);
1196 return;
1198 tevent_req_done(req);
1201 NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
1202 SMB_STRUCT_STAT *sbuf, uint32 *attributes)
1204 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1205 req, struct cli_qpathinfo_basic_state);
1206 NTSTATUS status;
1208 if (tevent_req_is_nterror(req, &status)) {
1209 return status;
1212 sbuf->st_ex_atime = interpret_long_date((char *)state->data+8);
1213 sbuf->st_ex_mtime = interpret_long_date((char *)state->data+16);
1214 sbuf->st_ex_ctime = interpret_long_date((char *)state->data+24);
1215 *attributes = IVAL(state->data, 32);
1216 return NT_STATUS_OK;
1219 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
1220 SMB_STRUCT_STAT *sbuf, uint32 *attributes)
1222 TALLOC_CTX *frame = talloc_stackframe();
1223 struct event_context *ev;
1224 struct tevent_req *req;
1225 NTSTATUS status = NT_STATUS_NO_MEMORY;
1227 if (cli_has_async_calls(cli)) {
1229 * Can't use sync call while an async call is in flight
1231 status = NT_STATUS_INVALID_PARAMETER;
1232 goto fail;
1234 ev = event_context_init(frame);
1235 if (ev == NULL) {
1236 goto fail;
1238 req = cli_qpathinfo_basic_send(frame, ev, cli, name);
1239 if (req == NULL) {
1240 goto fail;
1242 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1243 goto fail;
1245 status = cli_qpathinfo_basic_recv(req, sbuf, attributes);
1246 fail:
1247 TALLOC_FREE(frame);
1248 if (!NT_STATUS_IS_OK(status)) {
1249 cli_set_error(cli, status);
1251 return status;
1254 /****************************************************************************
1255 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1256 ****************************************************************************/
1258 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1260 uint8_t *rdata;
1261 uint32_t num_rdata;
1262 unsigned int len;
1263 char *converted = NULL;
1264 size_t converted_size = 0;
1265 NTSTATUS status;
1267 status = cli_qpathinfo(talloc_tos(), cli, fname,
1268 SMB_QUERY_FILE_ALT_NAME_INFO,
1269 4, cli->max_xmit, &rdata, &num_rdata);
1270 if (!NT_STATUS_IS_OK(status)) {
1271 return status;
1274 len = IVAL(rdata, 0);
1276 if (len > num_rdata - 4) {
1277 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1280 /* The returned data is a pushed string, not raw data. */
1281 if (!convert_string_talloc(talloc_tos(),
1282 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
1283 CH_UNIX,
1284 rdata + 4,
1285 len,
1286 &converted,
1287 &converted_size,
1288 true)) {
1289 return NT_STATUS_NO_MEMORY;
1291 fstrcpy(alt_name, converted);
1293 TALLOC_FREE(converted);
1294 TALLOC_FREE(rdata);
1296 return NT_STATUS_OK;