ldb_kv: Use ldb_msg_add_steal_value() in msg_add_distinguished_name()
[Samba.git] / source3 / libsmb / clirap.c
blobe80dfc92a77b0bd2dd6c614e6f05d2f24dda8e1e
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 "../lib/util/tevent_ntstatus.h"
27 #include "async_smb.h"
28 #include "libsmb/libsmb.h"
29 #include "libsmb/clirap.h"
30 #include "trans2.h"
31 #include "../libcli/smb/smbXcli_base.h"
32 #include "cli_smb2_fnum.h"
34 #define PIPE_LANMAN "\\PIPE\\LANMAN"
36 /****************************************************************************
37 Call a remote api
38 ****************************************************************************/
40 bool cli_api(struct cli_state *cli,
41 char *param, int prcnt, int mprcnt,
42 char *data, int drcnt, int mdrcnt,
43 char **rparam, unsigned int *rprcnt,
44 char **rdata, unsigned int *rdrcnt)
46 NTSTATUS status;
48 uint8_t *my_rparam, *my_rdata;
49 uint32_t num_my_rparam, num_my_rdata;
51 status = cli_trans(talloc_tos(), cli, SMBtrans,
52 PIPE_LANMAN, 0, /* name, fid */
53 0, 0, /* function, flags */
54 NULL, 0, 0, /* setup */
55 (uint8_t *)param, prcnt, mprcnt, /* Params, length, max */
56 (uint8_t *)data, drcnt, mdrcnt, /* Data, length, max */
57 NULL, /* recv_flags2 */
58 NULL, 0, NULL, /* rsetup */
59 &my_rparam, 0, &num_my_rparam,
60 &my_rdata, 0, &num_my_rdata);
61 if (!NT_STATUS_IS_OK(status)) {
62 return false;
66 * I know this memcpy massively hurts, but there are just tons
67 * of callers of cli_api that eventually need changing to
68 * talloc
71 *rparam = (char *)smb_memdup(my_rparam, num_my_rparam);
72 if (*rparam == NULL) {
73 goto fail;
75 *rprcnt = num_my_rparam;
76 TALLOC_FREE(my_rparam);
78 *rdata = (char *)smb_memdup(my_rdata, num_my_rdata);
79 if (*rdata == NULL) {
80 goto fail;
82 *rdrcnt = num_my_rdata;
83 TALLOC_FREE(my_rdata);
85 return true;
86 fail:
87 TALLOC_FREE(my_rdata);
88 TALLOC_FREE(my_rparam);
89 *rparam = NULL;
90 *rprcnt = 0;
91 *rdata = NULL;
92 *rdrcnt = 0;
93 return false;
96 /****************************************************************************
97 Perform a NetWkstaUserLogon.
98 ****************************************************************************/
100 bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
102 char *rparam = NULL;
103 char *rdata = NULL;
104 char *p;
105 unsigned int rdrcnt,rprcnt;
106 char param[1024];
108 memset(param, 0, sizeof(param));
110 /* send a SMBtrans command with api NetWkstaUserLogon */
111 p = param;
112 SSVAL(p,0,132); /* api number */
113 p += 2;
114 strlcpy(p,"OOWb54WrLh",sizeof(param)-PTR_DIFF(p,param));
115 p = skip_string(param,sizeof(param),p);
116 strlcpy(p,"WB21BWDWWDDDDDDDzzzD",sizeof(param)-PTR_DIFF(p,param));
117 p = skip_string(param,sizeof(param),p);
118 SSVAL(p,0,1);
119 p += 2;
120 strlcpy(p,user,sizeof(param)-PTR_DIFF(p,param));
121 if (!strupper_m(p)) {
122 return false;
124 p += 21;
125 p++;
126 p += 15;
127 p++;
128 strlcpy(p, workstation,sizeof(param)-PTR_DIFF(p,param));
129 if (!strupper_m(p)) {
130 return false;
132 p += 16;
133 SSVAL(p, 0, CLI_BUFFER_SIZE);
134 p += 2;
135 SSVAL(p, 0, CLI_BUFFER_SIZE);
136 p += 2;
138 if (cli_api(cli,
139 param, PTR_DIFF(p,param),1024, /* param, length, max */
140 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
141 &rparam, &rprcnt, /* return params, return size */
142 &rdata, &rdrcnt /* return data, return size */
143 )) {
144 cli->rap_error = rparam? SVAL(rparam,0) : -1;
145 p = rdata;
147 if (cli->rap_error == 0) {
148 DEBUG(4,("NetWkstaUserLogon success\n"));
150 * The cli->privileges = SVAL(p, 24); field was set here
151 * but it was not use anywhere else.
153 /* The cli->eff_name field used to be set here
154 but it wasn't used anywhere else. */
155 } else {
156 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
160 SAFE_FREE(rparam);
161 SAFE_FREE(rdata);
162 return (cli->rap_error == 0);
165 /****************************************************************************
166 Call a NetShareEnum - try and browse available connections on a host.
167 ****************************************************************************/
169 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32_t, const char *, void *), void *state)
171 char *rparam = NULL;
172 char *rdata = NULL;
173 char *p;
174 unsigned int rdrcnt,rprcnt;
175 char param[1024];
176 int count = -1;
178 /* now send a SMBtrans command with api RNetShareEnum */
179 p = param;
180 SSVAL(p,0,0); /* api number */
181 p += 2;
182 strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
183 p = skip_string(param,sizeof(param),p);
184 strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
185 p = skip_string(param,sizeof(param),p);
186 SSVAL(p,0,1);
188 * Win2k needs a *smaller* buffer than 0xFFFF here -
189 * it returns "out of server memory" with 0xFFFF !!! JRA.
191 SSVAL(p,2,0xFFE0);
192 p += 4;
194 if (cli_api(cli,
195 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
196 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
197 &rparam, &rprcnt, /* return params, length */
198 &rdata, &rdrcnt)) /* return data, length */
200 int res = rparam? SVAL(rparam,0) : -1;
202 if (res == 0 || res == ERRmoredata) {
203 int converter=SVAL(rparam,2);
204 int i;
205 char *rdata_end = rdata + rdrcnt;
207 count=SVAL(rparam,4);
208 p = rdata;
210 for (i=0;i<count;i++,p+=20) {
211 char *sname;
212 int type;
213 int comment_offset;
214 const char *cmnt;
215 const char *p1;
216 char *s1, *s2;
217 size_t len;
218 TALLOC_CTX *frame = talloc_stackframe();
220 if (p + 20 > rdata_end) {
221 TALLOC_FREE(frame);
222 break;
225 sname = p;
226 type = SVAL(p,14);
227 comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
228 if (comment_offset < 0 ||
229 comment_offset > (int)rdrcnt) {
230 TALLOC_FREE(frame);
231 break;
233 cmnt = comment_offset?(rdata+comment_offset):"";
235 /* Work out the comment length. */
236 for (p1 = cmnt, len = 0; *p1 &&
237 p1 < rdata_end; len++)
238 p1++;
239 if (!*p1) {
240 len++;
242 pull_string_talloc(frame,rdata,0,
243 &s1,sname,14,STR_ASCII);
244 pull_string_talloc(frame,rdata,0,
245 &s2,cmnt,len,STR_ASCII);
246 if (!s1 || !s2) {
247 TALLOC_FREE(frame);
248 continue;
251 fn(s1, type, s2, state);
253 TALLOC_FREE(frame);
255 } else {
256 DEBUG(4,("NetShareEnum res=%d\n", res));
258 } else {
259 DEBUG(4,("NetShareEnum failed\n"));
262 SAFE_FREE(rparam);
263 SAFE_FREE(rdata);
265 return count;
268 /****************************************************************************
269 Call a NetServerEnum for the specified workgroup and servertype mask. This
270 function then calls the specified callback function for each name returned.
272 The callback function takes 4 arguments: the machine name, the server type,
273 the comment and a state pointer.
274 ****************************************************************************/
276 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32_t stype,
277 void (*fn)(const char *, uint32_t, const char *, void *),
278 void *state)
280 char *rparam = NULL;
281 char *rdata = NULL;
282 char *rdata_end = NULL;
283 unsigned int rdrcnt,rprcnt;
284 char *p;
285 char param[1024];
286 int uLevel = 1;
287 size_t len;
288 uint32_t func = RAP_NetServerEnum2;
289 char *last_entry = NULL;
290 int total_cnt = 0;
291 int return_cnt = 0;
292 int res;
294 errno = 0; /* reset */
297 * This may take more than one transaction, so we should loop until
298 * we no longer get a more data to process or we have all of the
299 * items.
301 do {
302 /* send a SMBtrans command with api NetServerEnum */
303 p = param;
304 SIVAL(p,0,func); /* api number */
305 p += 2;
307 if (func == RAP_NetServerEnum3) {
308 strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
309 } else {
310 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
313 p = skip_string(param, sizeof(param), p);
314 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
316 p = skip_string(param, sizeof(param), p);
317 SSVAL(p,0,uLevel);
318 SSVAL(p,2,CLI_BUFFER_SIZE);
319 p += 4;
320 SIVAL(p,0,stype);
321 p += 4;
323 /* If we have more data, tell the server where
324 * to continue from.
326 len = push_ascii(p,
327 workgroup,
328 sizeof(param) - PTR_DIFF(p,param) - 1,
329 STR_TERMINATE|STR_UPPER);
331 if (len == 0) {
332 SAFE_FREE(last_entry);
333 return false;
335 p += len;
337 if (func == RAP_NetServerEnum3) {
338 len = push_ascii(p,
339 last_entry ? last_entry : "",
340 sizeof(param) - PTR_DIFF(p,param) - 1,
341 STR_TERMINATE);
343 if (len == 0) {
344 SAFE_FREE(last_entry);
345 return false;
347 p += len;
350 /* Next time through we need to use the continue api */
351 func = RAP_NetServerEnum3;
353 if (!cli_api(cli,
354 param, PTR_DIFF(p,param), 8, /* params, length, max */
355 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
356 &rparam, &rprcnt, /* return params, return size */
357 &rdata, &rdrcnt)) { /* return data, return size */
359 /* break out of the loop on error */
360 res = -1;
361 break;
364 rdata_end = rdata + rdrcnt;
365 res = rparam ? SVAL(rparam,0) : -1;
367 if (res == 0 || res == ERRmoredata ||
368 (res != -1 && cli_errno(cli) == 0)) {
369 char *sname = NULL;
370 int i, count;
371 int converter=SVAL(rparam,2);
373 /* Get the number of items returned in this buffer */
374 count = SVAL(rparam, 4);
376 /* The next field contains the number of items left,
377 * including those returned in this buffer. So the
378 * first time through this should contain all of the
379 * entries.
381 if (total_cnt == 0) {
382 total_cnt = SVAL(rparam, 6);
385 /* Keep track of how many we have read */
386 return_cnt += count;
387 p = rdata;
389 /* The last name in the previous NetServerEnum reply is
390 * sent back to server in the NetServerEnum3 request
391 * (last_entry). The next reply should repeat this entry
392 * as the first element. We have no proof that this is
393 * always true, but from traces that seems to be the
394 * behavior from Window Servers. So first lets do a lot
395 * of checking, just being paranoid. If the string
396 * matches then we already saw this entry so skip it.
398 * NOTE: sv1_name field must be null terminated and has
399 * a max size of 16 (NetBIOS Name).
401 if (last_entry && count && p &&
402 (strncmp(last_entry, p, 16) == 0)) {
403 count -= 1; /* Skip this entry */
404 return_cnt = -1; /* Not part of total, so don't count. */
405 p = rdata + 26; /* Skip the whole record */
408 for (i = 0; i < count; i++, p += 26) {
409 int comment_offset;
410 const char *cmnt;
411 const char *p1;
412 char *s1, *s2;
413 TALLOC_CTX *frame = talloc_stackframe();
414 uint32_t entry_stype;
416 if (p + 26 > rdata_end) {
417 TALLOC_FREE(frame);
418 break;
421 sname = p;
422 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
423 cmnt = comment_offset?(rdata+comment_offset):"";
425 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
426 TALLOC_FREE(frame);
427 continue;
430 /* Work out the comment length. */
431 for (p1 = cmnt, len = 0; *p1 &&
432 p1 < rdata_end; len++)
433 p1++;
434 if (!*p1) {
435 len++;
438 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
440 pull_string_talloc(frame,rdata,0,
441 &s1,sname,16,STR_ASCII);
442 pull_string_talloc(frame,rdata,0,
443 &s2,cmnt,len,STR_ASCII);
445 if (!s1 || !s2) {
446 TALLOC_FREE(frame);
447 continue;
450 fn(s1, entry_stype, s2, state);
451 TALLOC_FREE(frame);
454 /* We are done with the old last entry, so now we can free it */
455 if (last_entry) {
456 SAFE_FREE(last_entry); /* This will set it to null */
459 /* We always make a copy of the last entry if we have one */
460 if (sname) {
461 last_entry = smb_xstrdup(sname);
464 /* If we have more data, but no last entry then error out */
465 if (!last_entry && (res == ERRmoredata)) {
466 errno = EINVAL;
467 res = 0;
472 SAFE_FREE(rparam);
473 SAFE_FREE(rdata);
474 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
476 SAFE_FREE(rparam);
477 SAFE_FREE(rdata);
478 SAFE_FREE(last_entry);
480 if (res == -1) {
481 errno = cli_errno(cli);
482 } else {
483 if (!return_cnt) {
484 /* this is a very special case, when the domain master for the
485 work group isn't part of the work group itself, there is something
486 wild going on */
487 errno = ENOENT;
491 return(return_cnt > 0);
494 /****************************************************************************
495 Send a SamOEMChangePassword command.
496 ****************************************************************************/
498 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
499 const char *old_password)
501 char param[1024];
502 unsigned char data[532];
503 char *p = param;
504 unsigned char old_pw_hash[16];
505 unsigned char new_pw_hash[16];
506 unsigned int data_len;
507 unsigned int param_len = 0;
508 char *rparam = NULL;
509 char *rdata = NULL;
510 unsigned int rprcnt, rdrcnt;
512 if (strlen(user) >= sizeof(fstring)-1) {
513 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
514 return False;
517 SSVAL(p,0,214); /* SamOEMChangePassword command. */
518 p += 2;
519 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
520 p = skip_string(param,sizeof(param),p);
521 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
522 p = skip_string(param,sizeof(param),p);
523 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
524 p = skip_string(param,sizeof(param),p);
525 SSVAL(p,0,532);
526 p += 2;
528 param_len = PTR_DIFF(p,param);
531 * Get the Lanman hash of the old password, we
532 * use this as the key to make_oem_passwd_hash().
534 E_deshash(old_password, old_pw_hash);
536 encode_pw_buffer(data, new_password, STR_ASCII);
538 #ifdef DEBUG_PASSWORD
539 DEBUG(100,("make_oem_passwd_hash\n"));
540 dump_data(100, data, 516);
541 #endif
542 arcfour_crypt( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
545 * Now place the old password hash in the data.
547 E_deshash(new_password, new_pw_hash);
549 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
551 data_len = 532;
553 if (!cli_api(cli,
554 param, param_len, 4, /* param, length, max */
555 (char *)data, data_len, 0, /* data, length, max */
556 &rparam, &rprcnt,
557 &rdata, &rdrcnt)) {
558 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
559 user ));
560 return False;
563 if (rparam) {
564 cli->rap_error = SVAL(rparam,0);
567 SAFE_FREE(rparam);
568 SAFE_FREE(rdata);
570 return (cli->rap_error == 0);
573 /****************************************************************************
574 Send a qpathinfo call.
575 ****************************************************************************/
577 struct cli_qpathinfo1_state {
578 struct cli_state *cli;
579 uint32_t num_data;
580 uint8_t *data;
583 static void cli_qpathinfo1_done(struct tevent_req *subreq);
585 struct tevent_req *cli_qpathinfo1_send(TALLOC_CTX *mem_ctx,
586 struct tevent_context *ev,
587 struct cli_state *cli,
588 const char *fname)
590 struct tevent_req *req = NULL, *subreq = NULL;
591 struct cli_qpathinfo1_state *state = NULL;
593 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo1_state);
594 if (req == NULL) {
595 return NULL;
597 state->cli = cli;
598 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_INFO_STANDARD,
599 22, CLI_BUFFER_SIZE);
600 if (tevent_req_nomem(subreq, req)) {
601 return tevent_req_post(req, ev);
603 tevent_req_set_callback(subreq, cli_qpathinfo1_done, req);
604 return req;
607 static void cli_qpathinfo1_done(struct tevent_req *subreq)
609 struct tevent_req *req = tevent_req_callback_data(
610 subreq, struct tevent_req);
611 struct cli_qpathinfo1_state *state = tevent_req_data(
612 req, struct cli_qpathinfo1_state);
613 NTSTATUS status;
615 status = cli_qpathinfo_recv(subreq, state, &state->data,
616 &state->num_data);
617 TALLOC_FREE(subreq);
618 if (!NT_STATUS_IS_OK(status)) {
619 tevent_req_nterror(req, status);
620 return;
622 tevent_req_done(req);
625 NTSTATUS cli_qpathinfo1_recv(struct tevent_req *req,
626 time_t *change_time,
627 time_t *access_time,
628 time_t *write_time,
629 off_t *size,
630 uint16_t *mode)
632 struct cli_qpathinfo1_state *state = tevent_req_data(
633 req, struct cli_qpathinfo1_state);
634 NTSTATUS status;
636 time_t (*date_fn)(const void *buf, int serverzone);
638 if (tevent_req_is_nterror(req, &status)) {
639 return status;
642 if (state->cli->win95) {
643 date_fn = make_unix_date;
644 } else {
645 date_fn = make_unix_date2;
648 if (change_time) {
649 *change_time = date_fn(state->data+0, smb1cli_conn_server_time_zone(state->cli->conn));
651 if (access_time) {
652 *access_time = date_fn(state->data+4, smb1cli_conn_server_time_zone(state->cli->conn));
654 if (write_time) {
655 *write_time = date_fn(state->data+8, smb1cli_conn_server_time_zone(state->cli->conn));
657 if (size) {
658 *size = IVAL(state->data, 12);
660 if (mode) {
661 *mode = SVAL(state->data, l1_attrFile);
663 return NT_STATUS_OK;
666 NTSTATUS cli_qpathinfo1(struct cli_state *cli,
667 const char *fname,
668 time_t *change_time,
669 time_t *access_time,
670 time_t *write_time,
671 off_t *size,
672 uint16_t *mode)
674 TALLOC_CTX *frame = talloc_stackframe();
675 struct tevent_context *ev;
676 struct tevent_req *req;
677 NTSTATUS status = NT_STATUS_NO_MEMORY;
679 if (smbXcli_conn_has_async_calls(cli->conn)) {
681 * Can't use sync call while an async call is in flight
683 status = NT_STATUS_INVALID_PARAMETER;
684 goto fail;
686 ev = samba_tevent_context_init(frame);
687 if (ev == NULL) {
688 goto fail;
690 req = cli_qpathinfo1_send(frame, ev, cli, fname);
691 if (req == NULL) {
692 goto fail;
694 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
695 goto fail;
697 status = cli_qpathinfo1_recv(req, change_time, access_time,
698 write_time, size, mode);
699 fail:
700 TALLOC_FREE(frame);
701 return status;
704 /****************************************************************************
705 Send a setpathinfo call.
706 ****************************************************************************/
708 NTSTATUS cli_setpathinfo_basic(struct cli_state *cli, const char *fname,
709 time_t create_time,
710 time_t access_time,
711 time_t write_time,
712 time_t change_time,
713 uint16_t mode)
715 unsigned int data_len = 0;
716 char data[40];
717 char *p;
719 p = data;
722 * Add the create, last access, modification, and status change times
724 put_long_date(p, create_time);
725 p += 8;
727 put_long_date(p, access_time);
728 p += 8;
730 put_long_date(p, write_time);
731 p += 8;
733 put_long_date(p, change_time);
734 p += 8;
736 if (mode == (uint16_t)-1 || mode == FILE_ATTRIBUTE_NORMAL) {
737 /* No change. */
738 mode = 0;
739 } else if (mode == 0) {
740 /* Clear all existing attributes. */
741 mode = FILE_ATTRIBUTE_NORMAL;
744 /* Add attributes */
745 SIVAL(p, 0, mode);
747 p += 4;
749 /* Add padding */
750 SIVAL(p, 0, 0);
751 p += 4;
753 data_len = PTR_DIFF(p, data);
755 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
756 DATA_BLOB in_data = data_blob_const(data, data_len);
758 * Split out SMB2 here as we need to select
759 * the correct info type and level.
761 return cli_smb2_setpathinfo(cli,
762 fname,
763 1, /* SMB2_SETINFO_FILE */
764 SMB_FILE_BASIC_INFORMATION - 1000,
765 &in_data);
768 return cli_setpathinfo(cli, SMB_FILE_BASIC_INFORMATION, fname,
769 (uint8_t *)data, data_len);
772 /****************************************************************************
773 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
774 ****************************************************************************/
776 struct cli_qpathinfo2_state {
777 uint32_t num_data;
778 uint8_t *data;
781 static void cli_qpathinfo2_done(struct tevent_req *subreq);
783 struct tevent_req *cli_qpathinfo2_send(TALLOC_CTX *mem_ctx,
784 struct tevent_context *ev,
785 struct cli_state *cli,
786 const char *fname)
788 struct tevent_req *req = NULL, *subreq = NULL;
789 struct cli_qpathinfo2_state *state = NULL;
791 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo2_state);
792 if (req == NULL) {
793 return NULL;
795 subreq = cli_qpathinfo_send(state, ev, cli, fname,
796 SMB_QUERY_FILE_ALL_INFO,
797 68, CLI_BUFFER_SIZE);
798 if (tevent_req_nomem(subreq, req)) {
799 return tevent_req_post(req, ev);
801 tevent_req_set_callback(subreq, cli_qpathinfo2_done, req);
802 return req;
805 static void cli_qpathinfo2_done(struct tevent_req *subreq)
807 struct tevent_req *req = tevent_req_callback_data(
808 subreq, struct tevent_req);
809 struct cli_qpathinfo2_state *state = tevent_req_data(
810 req, struct cli_qpathinfo2_state);
811 NTSTATUS status;
813 status = cli_qpathinfo_recv(subreq, state, &state->data,
814 &state->num_data);
815 TALLOC_FREE(subreq);
816 if (!NT_STATUS_IS_OK(status)) {
817 tevent_req_nterror(req, status);
818 return;
820 tevent_req_done(req);
823 NTSTATUS cli_qpathinfo2_recv(struct tevent_req *req,
824 struct timespec *create_time,
825 struct timespec *access_time,
826 struct timespec *write_time,
827 struct timespec *change_time,
828 off_t *size, uint16_t *mode,
829 SMB_INO_T *ino)
831 struct cli_qpathinfo2_state *state = tevent_req_data(
832 req, struct cli_qpathinfo2_state);
833 NTSTATUS status;
835 if (tevent_req_is_nterror(req, &status)) {
836 return status;
839 if (create_time) {
840 *create_time = interpret_long_date((char *)state->data+0);
842 if (access_time) {
843 *access_time = interpret_long_date((char *)state->data+8);
845 if (write_time) {
846 *write_time = interpret_long_date((char *)state->data+16);
848 if (change_time) {
849 *change_time = interpret_long_date((char *)state->data+24);
851 if (mode) {
852 *mode = SVAL(state->data, 32);
854 if (size) {
855 *size = IVAL2_TO_SMB_BIG_UINT(state->data,48);
857 if (ino) {
858 *ino = IVAL(state->data, 64);
860 return NT_STATUS_OK;
863 NTSTATUS cli_qpathinfo2(struct cli_state *cli, const char *fname,
864 struct timespec *create_time,
865 struct timespec *access_time,
866 struct timespec *write_time,
867 struct timespec *change_time,
868 off_t *size, uint16_t *mode,
869 SMB_INO_T *ino)
871 TALLOC_CTX *frame = NULL;
872 struct tevent_context *ev;
873 struct tevent_req *req;
874 NTSTATUS status = NT_STATUS_NO_MEMORY;
876 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
877 return cli_smb2_qpathinfo2(cli,
878 fname,
879 create_time,
880 access_time,
881 write_time,
882 change_time,
883 size,
884 mode,
885 ino);
888 frame = talloc_stackframe();
890 if (smbXcli_conn_has_async_calls(cli->conn)) {
892 * Can't use sync call while an async call is in flight
894 status = NT_STATUS_INVALID_PARAMETER;
895 goto fail;
897 ev = samba_tevent_context_init(frame);
898 if (ev == NULL) {
899 goto fail;
901 req = cli_qpathinfo2_send(frame, ev, cli, fname);
902 if (req == NULL) {
903 goto fail;
905 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
906 goto fail;
908 status = cli_qpathinfo2_recv(req, create_time, access_time,
909 write_time, change_time, size, mode, ino);
910 fail:
911 TALLOC_FREE(frame);
912 return status;
915 /****************************************************************************
916 Get the stream info
917 ****************************************************************************/
919 struct cli_qpathinfo_streams_state {
920 uint32_t num_data;
921 uint8_t *data;
924 static void cli_qpathinfo_streams_done(struct tevent_req *subreq);
926 struct tevent_req *cli_qpathinfo_streams_send(TALLOC_CTX *mem_ctx,
927 struct tevent_context *ev,
928 struct cli_state *cli,
929 const char *fname)
931 struct tevent_req *req = NULL, *subreq = NULL;
932 struct cli_qpathinfo_streams_state *state = NULL;
934 req = tevent_req_create(mem_ctx, &state,
935 struct cli_qpathinfo_streams_state);
936 if (req == NULL) {
937 return NULL;
939 subreq = cli_qpathinfo_send(state, ev, cli, fname,
940 SMB_FILE_STREAM_INFORMATION,
941 0, CLI_BUFFER_SIZE);
942 if (tevent_req_nomem(subreq, req)) {
943 return tevent_req_post(req, ev);
945 tevent_req_set_callback(subreq, cli_qpathinfo_streams_done, req);
946 return req;
949 static void cli_qpathinfo_streams_done(struct tevent_req *subreq)
951 struct tevent_req *req = tevent_req_callback_data(
952 subreq, struct tevent_req);
953 struct cli_qpathinfo_streams_state *state = tevent_req_data(
954 req, struct cli_qpathinfo_streams_state);
955 NTSTATUS status;
957 status = cli_qpathinfo_recv(subreq, state, &state->data,
958 &state->num_data);
959 TALLOC_FREE(subreq);
960 if (!NT_STATUS_IS_OK(status)) {
961 tevent_req_nterror(req, status);
962 return;
964 tevent_req_done(req);
967 NTSTATUS cli_qpathinfo_streams_recv(struct tevent_req *req,
968 TALLOC_CTX *mem_ctx,
969 unsigned int *pnum_streams,
970 struct stream_struct **pstreams)
972 struct cli_qpathinfo_streams_state *state = tevent_req_data(
973 req, struct cli_qpathinfo_streams_state);
974 NTSTATUS status;
976 if (tevent_req_is_nterror(req, &status)) {
977 return status;
979 if (!parse_streams_blob(mem_ctx, state->data, state->num_data,
980 pnum_streams, pstreams)) {
981 return NT_STATUS_INVALID_NETWORK_RESPONSE;
983 return NT_STATUS_OK;
986 NTSTATUS cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
987 TALLOC_CTX *mem_ctx,
988 unsigned int *pnum_streams,
989 struct stream_struct **pstreams)
991 TALLOC_CTX *frame = NULL;
992 struct tevent_context *ev;
993 struct tevent_req *req;
994 NTSTATUS status = NT_STATUS_NO_MEMORY;
996 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
997 return cli_smb2_qpathinfo_streams(cli,
998 fname,
999 mem_ctx,
1000 pnum_streams,
1001 pstreams);
1004 frame = talloc_stackframe();
1006 if (smbXcli_conn_has_async_calls(cli->conn)) {
1008 * Can't use sync call while an async call is in flight
1010 status = NT_STATUS_INVALID_PARAMETER;
1011 goto fail;
1013 ev = samba_tevent_context_init(frame);
1014 if (ev == NULL) {
1015 goto fail;
1017 req = cli_qpathinfo_streams_send(frame, ev, cli, fname);
1018 if (req == NULL) {
1019 goto fail;
1021 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1022 goto fail;
1024 status = cli_qpathinfo_streams_recv(req, mem_ctx, pnum_streams,
1025 pstreams);
1026 fail:
1027 TALLOC_FREE(frame);
1028 return status;
1031 bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
1032 size_t data_len,
1033 unsigned int *pnum_streams,
1034 struct stream_struct **pstreams)
1036 unsigned int num_streams;
1037 struct stream_struct *streams;
1038 unsigned int ofs;
1040 num_streams = 0;
1041 streams = NULL;
1042 ofs = 0;
1044 while ((data_len > ofs) && (data_len - ofs >= 24)) {
1045 uint32_t nlen, len;
1046 size_t size;
1047 void *vstr;
1048 struct stream_struct *tmp;
1049 uint8_t *tmp_buf;
1051 tmp = talloc_realloc(mem_ctx, streams,
1052 struct stream_struct,
1053 num_streams+1);
1055 if (tmp == NULL) {
1056 goto fail;
1058 streams = tmp;
1060 nlen = IVAL(rdata, ofs + 0x04);
1062 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
1063 rdata, ofs + 0x08);
1064 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
1065 rdata, ofs + 0x10);
1067 if (nlen > data_len - (ofs + 24)) {
1068 goto fail;
1072 * We need to null-terminate src, how do I do this with
1073 * convert_string_talloc??
1076 tmp_buf = talloc_array(streams, uint8_t, nlen+2);
1077 if (tmp_buf == NULL) {
1078 goto fail;
1081 memcpy(tmp_buf, rdata+ofs+24, nlen);
1082 tmp_buf[nlen] = 0;
1083 tmp_buf[nlen+1] = 0;
1085 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
1086 nlen+2, &vstr, &size))
1088 TALLOC_FREE(tmp_buf);
1089 goto fail;
1092 TALLOC_FREE(tmp_buf);
1093 streams[num_streams].name = (char *)vstr;
1094 num_streams++;
1096 len = IVAL(rdata, ofs);
1097 if (len > data_len - ofs) {
1098 goto fail;
1100 if (len == 0) break;
1101 ofs += len;
1104 *pnum_streams = num_streams;
1105 *pstreams = streams;
1106 return true;
1108 fail:
1109 TALLOC_FREE(streams);
1110 return false;
1113 /****************************************************************************
1114 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1115 ****************************************************************************/
1117 NTSTATUS cli_qfilename(struct cli_state *cli, uint16_t fnum,
1118 TALLOC_CTX *mem_ctx, char **_name)
1120 uint16_t recv_flags2;
1121 uint8_t *rdata;
1122 uint32_t num_rdata;
1123 NTSTATUS status;
1124 char *name = NULL;
1125 uint32_t namelen;
1127 status = cli_qfileinfo(talloc_tos(), cli, fnum,
1128 SMB_QUERY_FILE_NAME_INFO,
1129 4, CLI_BUFFER_SIZE, &recv_flags2,
1130 &rdata, &num_rdata);
1131 if (!NT_STATUS_IS_OK(status)) {
1132 return status;
1135 namelen = IVAL(rdata, 0);
1136 if (namelen > (num_rdata - 4)) {
1137 TALLOC_FREE(rdata);
1138 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1141 clistr_pull_talloc(mem_ctx,
1142 (const char *)rdata,
1143 recv_flags2,
1144 &name,
1145 rdata + 4,
1146 namelen,
1147 STR_UNICODE);
1148 if (name == NULL) {
1149 status = map_nt_error_from_unix(errno);
1150 TALLOC_FREE(rdata);
1151 return status;
1154 *_name = name;
1155 TALLOC_FREE(rdata);
1156 return NT_STATUS_OK;
1159 /****************************************************************************
1160 Send a qfileinfo call.
1161 ****************************************************************************/
1163 NTSTATUS cli_qfileinfo_basic(struct cli_state *cli, uint16_t fnum,
1164 uint16_t *mode, off_t *size,
1165 struct timespec *create_time,
1166 struct timespec *access_time,
1167 struct timespec *write_time,
1168 struct timespec *change_time,
1169 SMB_INO_T *ino)
1171 uint8_t *rdata;
1172 uint32_t num_rdata;
1173 NTSTATUS status;
1175 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1176 return cli_smb2_qfileinfo_basic(cli,
1177 fnum,
1178 mode,
1179 size,
1180 create_time,
1181 access_time,
1182 write_time,
1183 change_time,
1184 ino);
1187 /* if its a win95 server then fail this - win95 totally screws it
1188 up */
1189 if (cli->win95) {
1190 return NT_STATUS_NOT_SUPPORTED;
1193 status = cli_qfileinfo(talloc_tos(), cli, fnum,
1194 SMB_QUERY_FILE_ALL_INFO,
1195 68, CLI_BUFFER_SIZE,
1196 NULL,
1197 &rdata, &num_rdata);
1198 if (!NT_STATUS_IS_OK(status)) {
1199 return status;
1202 if (create_time) {
1203 *create_time = interpret_long_date((char *)rdata+0);
1205 if (access_time) {
1206 *access_time = interpret_long_date((char *)rdata+8);
1208 if (write_time) {
1209 *write_time = interpret_long_date((char *)rdata+16);
1211 if (change_time) {
1212 *change_time = interpret_long_date((char *)rdata+24);
1214 if (mode) {
1215 *mode = SVAL(rdata, 32);
1217 if (size) {
1218 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
1220 if (ino) {
1221 *ino = IVAL(rdata, 64);
1224 TALLOC_FREE(rdata);
1225 return NT_STATUS_OK;
1228 /****************************************************************************
1229 Send a qpathinfo BASIC_INFO call.
1230 ****************************************************************************/
1232 struct cli_qpathinfo_basic_state {
1233 uint32_t num_data;
1234 uint8_t *data;
1237 static void cli_qpathinfo_basic_done(struct tevent_req *subreq);
1239 struct tevent_req *cli_qpathinfo_basic_send(TALLOC_CTX *mem_ctx,
1240 struct tevent_context *ev,
1241 struct cli_state *cli,
1242 const char *fname)
1244 struct tevent_req *req = NULL, *subreq = NULL;
1245 struct cli_qpathinfo_basic_state *state = NULL;
1247 req = tevent_req_create(mem_ctx, &state,
1248 struct cli_qpathinfo_basic_state);
1249 if (req == NULL) {
1250 return NULL;
1252 subreq = cli_qpathinfo_send(state, ev, cli, fname,
1253 SMB_QUERY_FILE_BASIC_INFO,
1254 36, CLI_BUFFER_SIZE);
1255 if (tevent_req_nomem(subreq, req)) {
1256 return tevent_req_post(req, ev);
1258 tevent_req_set_callback(subreq, cli_qpathinfo_basic_done, req);
1259 return req;
1262 static void cli_qpathinfo_basic_done(struct tevent_req *subreq)
1264 struct tevent_req *req = tevent_req_callback_data(
1265 subreq, struct tevent_req);
1266 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1267 req, struct cli_qpathinfo_basic_state);
1268 NTSTATUS status;
1270 status = cli_qpathinfo_recv(subreq, state, &state->data,
1271 &state->num_data);
1272 TALLOC_FREE(subreq);
1273 if (!NT_STATUS_IS_OK(status)) {
1274 tevent_req_nterror(req, status);
1275 return;
1277 tevent_req_done(req);
1280 NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
1281 SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
1283 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1284 req, struct cli_qpathinfo_basic_state);
1285 NTSTATUS status;
1287 if (tevent_req_is_nterror(req, &status)) {
1288 return status;
1291 sbuf->st_ex_btime = interpret_long_date((char *)state->data);
1292 sbuf->st_ex_atime = interpret_long_date((char *)state->data+8);
1293 sbuf->st_ex_mtime = interpret_long_date((char *)state->data+16);
1294 sbuf->st_ex_ctime = interpret_long_date((char *)state->data+24);
1295 *attributes = IVAL(state->data, 32);
1296 return NT_STATUS_OK;
1299 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
1300 SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
1302 TALLOC_CTX *frame = NULL;
1303 struct tevent_context *ev;
1304 struct tevent_req *req;
1305 NTSTATUS status = NT_STATUS_NO_MEMORY;
1307 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1308 return cli_smb2_qpathinfo_basic(cli,
1309 name,
1310 sbuf,
1311 attributes);
1314 frame = talloc_stackframe();
1316 if (smbXcli_conn_has_async_calls(cli->conn)) {
1318 * Can't use sync call while an async call is in flight
1320 status = NT_STATUS_INVALID_PARAMETER;
1321 goto fail;
1323 ev = samba_tevent_context_init(frame);
1324 if (ev == NULL) {
1325 goto fail;
1327 req = cli_qpathinfo_basic_send(frame, ev, cli, name);
1328 if (req == NULL) {
1329 goto fail;
1331 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1332 goto fail;
1334 status = cli_qpathinfo_basic_recv(req, sbuf, attributes);
1335 fail:
1336 TALLOC_FREE(frame);
1337 return status;
1340 /****************************************************************************
1341 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1342 ****************************************************************************/
1344 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1346 uint8_t *rdata;
1347 uint32_t num_rdata;
1348 unsigned int len;
1349 char *converted = NULL;
1350 size_t converted_size = 0;
1351 NTSTATUS status;
1353 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1354 return cli_smb2_qpathinfo_alt_name(cli,
1355 fname,
1356 alt_name);
1359 status = cli_qpathinfo(talloc_tos(), cli, fname,
1360 SMB_QUERY_FILE_ALT_NAME_INFO,
1361 4, CLI_BUFFER_SIZE, &rdata, &num_rdata);
1362 if (!NT_STATUS_IS_OK(status)) {
1363 return status;
1366 len = IVAL(rdata, 0);
1368 if (len > num_rdata - 4) {
1369 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1372 /* The returned data is a pushed string, not raw data. */
1373 if (!convert_string_talloc(talloc_tos(),
1374 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
1375 CH_UNIX,
1376 rdata + 4,
1377 len,
1378 &converted,
1379 &converted_size)) {
1380 return NT_STATUS_NO_MEMORY;
1382 fstrcpy(alt_name, converted);
1384 TALLOC_FREE(converted);
1385 TALLOC_FREE(rdata);
1387 return NT_STATUS_OK;
1390 /****************************************************************************
1391 Send a qpathinfo SMB_QUERY_FILE_STADNDARD_INFO call.
1392 ****************************************************************************/
1394 NTSTATUS cli_qpathinfo_standard(struct cli_state *cli, const char *fname,
1395 uint64_t *allocated, uint64_t *size,
1396 uint32_t *nlinks,
1397 bool *is_del_pending, bool *is_dir)
1399 uint8_t *rdata;
1400 uint32_t num_rdata;
1401 NTSTATUS status;
1403 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1404 return NT_STATUS_NOT_IMPLEMENTED;
1407 status = cli_qpathinfo(talloc_tos(), cli, fname,
1408 SMB_QUERY_FILE_STANDARD_INFO,
1409 24, CLI_BUFFER_SIZE, &rdata, &num_rdata);
1410 if (!NT_STATUS_IS_OK(status)) {
1411 return status;
1414 if (allocated) {
1415 *allocated = BVAL(rdata, 0);
1418 if (size) {
1419 *size = BVAL(rdata, 8);
1422 if (nlinks) {
1423 *nlinks = IVAL(rdata, 16);
1426 if (is_del_pending) {
1427 *is_del_pending = CVAL(rdata, 20);
1430 if (is_dir) {
1431 *is_dir = CVAL(rdata, 20);
1434 TALLOC_FREE(rdata);
1436 return NT_STATUS_OK;
1440 /* like cli_qpathinfo2 but do not use SMB_QUERY_FILE_ALL_INFO with smb1 */
1441 NTSTATUS cli_qpathinfo3(struct cli_state *cli, const char *fname,
1442 struct timespec *create_time,
1443 struct timespec *access_time,
1444 struct timespec *write_time,
1445 struct timespec *change_time,
1446 off_t *size, uint16_t *mode,
1447 SMB_INO_T *ino)
1449 NTSTATUS status = NT_STATUS_OK;
1450 SMB_STRUCT_STAT st = { 0 };
1451 uint32_t attr;
1452 uint64_t pos;
1454 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1455 return cli_qpathinfo2(cli, fname,
1456 create_time, access_time, write_time, change_time,
1457 size, mode, ino);
1460 if (create_time || access_time || write_time || change_time || mode) {
1461 status = cli_qpathinfo_basic(cli, fname, &st, &attr);
1462 if (!NT_STATUS_IS_OK(status)) {
1463 return status;
1467 if (size) {
1468 status = cli_qpathinfo_standard(cli, fname,
1469 NULL, &pos, NULL, NULL, NULL);
1470 if (!NT_STATUS_IS_OK(status)) {
1471 return status;
1474 *size = pos;
1477 if (create_time) {
1478 *create_time = st.st_ex_btime;
1480 if (access_time) {
1481 *access_time = st.st_ex_atime;
1483 if (write_time) {
1484 *write_time = st.st_ex_mtime;
1486 if (change_time) {
1487 *change_time = st.st_ex_ctime;
1489 if (mode) {
1490 *mode = attr;
1492 if (ino) {
1493 *ino = 0;
1496 return NT_STATUS_OK;