ctdb-failover: omit "restrict" optimization keyword
[Samba.git] / source3 / libsmb / clirap.c
blobffc643625c9eb5dd304e841f98c6fcae359f2317
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/util/tevent_ntstatus.h"
26 #include "async_smb.h"
27 #include "libsmb/libsmb.h"
28 #include "libsmb/clirap.h"
29 #include "trans2.h"
30 #include "../libcli/smb/smbXcli_base.h"
31 #include "libcli/smb/reparse.h"
32 #include "cli_smb2_fnum.h"
33 #include "lib/util/string_wrappers.h"
35 #include <gnutls/gnutls.h>
36 #include <gnutls/crypto.h>
38 #define PIPE_LANMAN "\\PIPE\\LANMAN"
40 /****************************************************************************
41 Call a remote api
42 ****************************************************************************/
44 bool cli_api(struct cli_state *cli,
45 char *param, int prcnt, int mprcnt,
46 char *data, int drcnt, int mdrcnt,
47 char **rparam, unsigned int *rprcnt,
48 char **rdata, unsigned int *rdrcnt)
50 NTSTATUS status;
52 uint8_t *my_rparam, *my_rdata;
53 uint32_t num_my_rparam, num_my_rdata;
55 status = cli_trans(talloc_tos(), cli, SMBtrans,
56 PIPE_LANMAN, 0, /* name, fid */
57 0, 0, /* function, flags */
58 NULL, 0, 0, /* setup */
59 (uint8_t *)param, prcnt, mprcnt, /* Params, length, max */
60 (uint8_t *)data, drcnt, mdrcnt, /* Data, length, max */
61 NULL, /* recv_flags2 */
62 NULL, 0, NULL, /* rsetup */
63 &my_rparam, 0, &num_my_rparam,
64 &my_rdata, 0, &num_my_rdata);
65 if (!NT_STATUS_IS_OK(status)) {
66 return false;
70 * I know this memcpy massively hurts, but there are just tons
71 * of callers of cli_api that eventually need changing to
72 * talloc
75 *rparam = (char *)smb_memdup(my_rparam, num_my_rparam);
76 if (*rparam == NULL) {
77 goto fail;
79 *rprcnt = num_my_rparam;
80 TALLOC_FREE(my_rparam);
82 *rdata = (char *)smb_memdup(my_rdata, num_my_rdata);
83 if (*rdata == NULL) {
84 goto fail;
86 *rdrcnt = num_my_rdata;
87 TALLOC_FREE(my_rdata);
89 return true;
90 fail:
91 TALLOC_FREE(my_rdata);
92 TALLOC_FREE(my_rparam);
93 *rparam = NULL;
94 *rprcnt = 0;
95 *rdata = NULL;
96 *rdrcnt = 0;
97 return false;
100 /****************************************************************************
101 Call a NetShareEnum - try and browse available connections on a host.
102 ****************************************************************************/
104 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32_t, const char *, void *), void *state)
106 char *rparam = NULL;
107 char *rdata = NULL;
108 char *p;
109 unsigned int rdrcnt,rprcnt;
110 char param[1024];
111 int count = -1;
112 bool ok;
113 int res;
115 /* now send a SMBtrans command with api RNetShareEnum */
116 p = param;
117 SSVAL(p,0,0); /* api number */
118 p += 2;
119 strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
120 p = skip_string(param,sizeof(param),p);
121 strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
122 p = skip_string(param,sizeof(param),p);
123 SSVAL(p,0,1);
125 * Win2k needs a *smaller* buffer than 0xFFFF here -
126 * it returns "out of server memory" with 0xFFFF !!! JRA.
128 SSVAL(p,2,0xFFE0);
129 p += 4;
131 ok = cli_api(
132 cli,
133 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
134 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
135 &rparam, &rprcnt, /* return params, length */
136 &rdata, &rdrcnt); /* return data, length */
137 if (!ok) {
138 DEBUG(4,("NetShareEnum failed\n"));
139 goto done;
142 if (rprcnt < 6) {
143 DBG_ERR("Got invalid result: rprcnt=%u\n", rprcnt);
144 goto done;
147 res = rparam? SVAL(rparam,0) : -1;
149 if (res == 0 || res == ERRmoredata) {
150 int converter=SVAL(rparam,2);
151 int i;
152 char *rdata_end = rdata + rdrcnt;
154 count=SVAL(rparam,4);
155 p = rdata;
157 for (i=0;i<count;i++,p+=20) {
158 char *sname;
159 int type;
160 int comment_offset;
161 const char *cmnt;
162 const char *p1;
163 char *s1, *s2;
164 size_t len;
165 TALLOC_CTX *frame = talloc_stackframe();
167 if (p + 20 > rdata_end) {
168 TALLOC_FREE(frame);
169 break;
172 sname = p;
173 type = SVAL(p,14);
174 comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
175 if (comment_offset < 0 ||
176 comment_offset > (int)rdrcnt) {
177 TALLOC_FREE(frame);
178 break;
180 cmnt = comment_offset?(rdata+comment_offset):"";
182 /* Work out the comment length. */
183 for (p1 = cmnt, len = 0; *p1 &&
184 p1 < rdata_end; len++)
185 p1++;
186 if (!*p1) {
187 len++;
189 pull_string_talloc(frame,rdata,0,
190 &s1,sname,14,STR_ASCII);
191 pull_string_talloc(frame,rdata,0,
192 &s2,cmnt,len,STR_ASCII);
193 if (!s1 || !s2) {
194 TALLOC_FREE(frame);
195 continue;
198 fn(s1, type, s2, state);
200 TALLOC_FREE(frame);
202 } else {
203 DEBUG(4,("NetShareEnum res=%d\n", res));
206 done:
207 SAFE_FREE(rparam);
208 SAFE_FREE(rdata);
210 return count;
213 /****************************************************************************
214 Call a NetServerEnum for the specified workgroup and servertype mask. This
215 function then calls the specified callback function for each name returned.
217 The callback function takes 4 arguments: the machine name, the server type,
218 the comment and a state pointer.
219 ****************************************************************************/
221 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32_t stype,
222 void (*fn)(const char *, uint32_t, const char *, void *),
223 void *state)
225 char *rparam = NULL;
226 char *rdata = NULL;
227 char *rdata_end = NULL;
228 unsigned int rdrcnt,rprcnt;
229 char *p;
230 char param[1024];
231 int uLevel = 1;
232 size_t len;
233 uint32_t func = RAP_NetServerEnum2;
234 char *last_entry = NULL;
235 int total_cnt = 0;
236 int return_cnt = 0;
237 int res;
239 errno = 0; /* reset */
242 * This may take more than one transaction, so we should loop until
243 * we no longer get a more data to process or we have all of the
244 * items.
246 do {
247 /* send a SMBtrans command with api NetServerEnum */
248 p = param;
249 SIVAL(p,0,func); /* api number */
250 p += 2;
252 if (func == RAP_NetServerEnum3) {
253 strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
254 } else {
255 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
258 p = skip_string(param, sizeof(param), p);
259 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
261 p = skip_string(param, sizeof(param), p);
262 SSVAL(p,0,uLevel);
263 SSVAL(p,2,CLI_BUFFER_SIZE);
264 p += 4;
265 SIVAL(p,0,stype);
266 p += 4;
268 /* If we have more data, tell the server where
269 * to continue from.
271 len = push_ascii(p,
272 workgroup,
273 sizeof(param) - PTR_DIFF(p,param) - 1,
274 STR_TERMINATE|STR_UPPER);
276 if (len == 0) {
277 SAFE_FREE(last_entry);
278 return false;
280 p += len;
282 if (func == RAP_NetServerEnum3) {
283 len = push_ascii(p,
284 last_entry ? last_entry : "",
285 sizeof(param) - PTR_DIFF(p,param) - 1,
286 STR_TERMINATE);
288 if (len == 0) {
289 SAFE_FREE(last_entry);
290 return false;
292 p += len;
295 /* Next time through we need to use the continue api */
296 func = RAP_NetServerEnum3;
298 if (!cli_api(cli,
299 param, PTR_DIFF(p,param), 8, /* params, length, max */
300 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
301 &rparam, &rprcnt, /* return params, return size */
302 &rdata, &rdrcnt)) { /* return data, return size */
304 /* break out of the loop on error */
305 res = -1;
306 break;
309 rdata_end = rdata + rdrcnt;
311 if (rprcnt < 6) {
312 DBG_ERR("Got invalid result: rprcnt=%u\n", rprcnt);
313 res = -1;
314 break;
317 res = rparam ? SVAL(rparam,0) : -1;
319 if (res == 0 || res == ERRmoredata ||
320 (res != -1 && cli_errno(cli) == 0)) {
321 char *sname = NULL;
322 int i, count;
323 int converter=SVAL(rparam,2);
325 /* Get the number of items returned in this buffer */
326 count = SVAL(rparam, 4);
328 /* The next field contains the number of items left,
329 * including those returned in this buffer. So the
330 * first time through this should contain all of the
331 * entries.
333 if (total_cnt == 0) {
334 total_cnt = SVAL(rparam, 6);
337 /* Keep track of how many we have read */
338 return_cnt += count;
339 p = rdata;
341 /* The last name in the previous NetServerEnum reply is
342 * sent back to server in the NetServerEnum3 request
343 * (last_entry). The next reply should repeat this entry
344 * as the first element. We have no proof that this is
345 * always true, but from traces that seems to be the
346 * behavior from Window Servers. So first lets do a lot
347 * of checking, just being paranoid. If the string
348 * matches then we already saw this entry so skip it.
350 * NOTE: sv1_name field must be null terminated and has
351 * a max size of 16 (NetBIOS Name).
353 if (last_entry && count && p &&
354 (strncmp(last_entry, p, 16) == 0)) {
355 count -= 1; /* Skip this entry */
356 return_cnt = -1; /* Not part of total, so don't count. */
357 p = rdata + 26; /* Skip the whole record */
360 for (i = 0; i < count; i++, p += 26) {
361 int comment_offset;
362 const char *cmnt;
363 const char *p1;
364 char *s1, *s2;
365 TALLOC_CTX *frame = talloc_stackframe();
366 uint32_t entry_stype;
368 if (p + 26 > rdata_end) {
369 TALLOC_FREE(frame);
370 break;
373 sname = p;
374 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
375 cmnt = comment_offset?(rdata+comment_offset):"";
377 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
378 TALLOC_FREE(frame);
379 continue;
382 /* Work out the comment length. */
383 for (p1 = cmnt, len = 0; *p1 &&
384 p1 < rdata_end; len++)
385 p1++;
386 if (!*p1) {
387 len++;
390 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
392 pull_string_talloc(frame,rdata,0,
393 &s1,sname,16,STR_ASCII);
394 pull_string_talloc(frame,rdata,0,
395 &s2,cmnt,len,STR_ASCII);
397 if (!s1 || !s2) {
398 TALLOC_FREE(frame);
399 continue;
402 fn(s1, entry_stype, s2, state);
403 TALLOC_FREE(frame);
406 /* We are done with the old last entry, so now we can free it */
407 if (last_entry) {
408 SAFE_FREE(last_entry); /* This will set it to null */
411 /* We always make a copy of the last entry if we have one */
412 if (sname) {
413 last_entry = smb_xstrdup(sname);
416 /* If we have more data, but no last entry then error out */
417 if (!last_entry && (res == ERRmoredata)) {
418 errno = EINVAL;
419 res = 0;
424 SAFE_FREE(rparam);
425 SAFE_FREE(rdata);
426 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
428 SAFE_FREE(rparam);
429 SAFE_FREE(rdata);
430 SAFE_FREE(last_entry);
432 if (res == -1) {
433 errno = cli_errno(cli);
434 } else {
435 if (!return_cnt) {
436 /* this is a very special case, when the domain master for the
437 work group isn't part of the work group itself, there is something
438 wild going on */
439 errno = ENOENT;
443 return(return_cnt > 0);
446 /****************************************************************************
447 Send a SamOEMChangePassword command.
448 ****************************************************************************/
450 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
451 const char *old_password)
453 char param[1024];
454 unsigned char data[532];
455 char *p = param;
456 unsigned char old_pw_hash[16];
457 unsigned char new_pw_hash[16];
458 unsigned int data_len;
459 unsigned int param_len = 0;
460 char *rparam = NULL;
461 char *rdata = NULL;
462 unsigned int rprcnt, rdrcnt;
463 gnutls_cipher_hd_t cipher_hnd = NULL;
464 gnutls_datum_t old_pw_key = {
465 .data = old_pw_hash,
466 .size = sizeof(old_pw_hash),
468 int rc;
470 if (strlen(user) >= sizeof(fstring)-1) {
471 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
472 return False;
475 SSVAL(p,0,214); /* SamOEMChangePassword command. */
476 p += 2;
477 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
478 p = skip_string(param,sizeof(param),p);
479 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
480 p = skip_string(param,sizeof(param),p);
481 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
482 p = skip_string(param,sizeof(param),p);
483 SSVAL(p,0,532);
484 p += 2;
486 param_len = PTR_DIFF(p,param);
489 * Get the Lanman hash of the old password, we
490 * use this as the key to make_oem_passwd_hash().
492 E_deshash(old_password, old_pw_hash);
494 encode_pw_buffer(data, new_password, STR_ASCII);
496 #ifdef DEBUG_PASSWORD
497 DEBUG(100,("make_oem_passwd_hash\n"));
498 dump_data(100, data, 516);
499 #endif
500 rc = gnutls_cipher_init(&cipher_hnd,
501 GNUTLS_CIPHER_ARCFOUR_128,
502 &old_pw_key,
503 NULL);
504 if (rc < 0) {
505 DBG_ERR("gnutls_cipher_init failed: %s\n",
506 gnutls_strerror(rc));
507 return false;
509 rc = gnutls_cipher_encrypt(cipher_hnd,
510 data,
511 516);
512 gnutls_cipher_deinit(cipher_hnd);
513 if (rc < 0) {
514 return false;
518 * Now place the old password hash in the data.
520 E_deshash(new_password, new_pw_hash);
522 rc = E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
523 if (rc != 0) {
524 DBG_ERR("E_old_pw_hash failed: %s\n", gnutls_strerror(rc));
525 return false;
528 data_len = 532;
530 if (!cli_api(cli,
531 param, param_len, 4, /* param, length, max */
532 (char *)data, data_len, 0, /* data, length, max */
533 &rparam, &rprcnt,
534 &rdata, &rdrcnt)) {
535 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
536 user ));
537 return False;
540 if (rdrcnt < 2) {
541 cli->rap_error = ERRbadformat;
542 goto done;
545 if (rparam) {
546 cli->rap_error = SVAL(rparam,0);
549 done:
550 SAFE_FREE(rparam);
551 SAFE_FREE(rdata);
553 return (cli->rap_error == 0);
556 static void prep_basic_information_buf(
557 uint8_t buf[40],
558 struct timespec create_time,
559 struct timespec access_time,
560 struct timespec write_time,
561 struct timespec change_time,
562 uint32_t attr)
564 char *p = (char *)buf;
566 * Add the create, last access, modification, and status change times
568 put_long_date_full_timespec(
569 TIMESTAMP_SET_NT_OR_BETTER, p, &create_time);
570 p += 8;
572 put_long_date_full_timespec(
573 TIMESTAMP_SET_NT_OR_BETTER, p, &access_time);
574 p += 8;
576 put_long_date_full_timespec(
577 TIMESTAMP_SET_NT_OR_BETTER, p, &write_time);
578 p += 8;
580 put_long_date_full_timespec(
581 TIMESTAMP_SET_NT_OR_BETTER, p, &change_time);
582 p += 8;
584 if (attr == (uint32_t)-1 || attr == FILE_ATTRIBUTE_NORMAL) {
585 /* No change. */
586 attr = 0;
587 } else if (attr == 0) {
588 /* Clear all existing attributes. */
589 attr = FILE_ATTRIBUTE_NORMAL;
592 /* Add attributes */
593 SIVAL(p, 0, attr);
595 p += 4;
597 /* Add padding */
598 SIVAL(p, 0, 0);
599 p += 4;
601 SMB_ASSERT(PTR_DIFF(p, buf) == 40);
604 NTSTATUS cli_setpathinfo_ext(struct cli_state *cli, const char *fname,
605 struct timespec create_time,
606 struct timespec access_time,
607 struct timespec write_time,
608 struct timespec change_time,
609 uint32_t attr)
611 uint8_t buf[40];
613 prep_basic_information_buf(
614 buf,
615 create_time,
616 access_time,
617 write_time,
618 change_time,
619 attr);
621 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
622 DATA_BLOB in_data = data_blob_const(buf, sizeof(buf));
624 * Split out SMB2 here as we need to select
625 * the correct info type and level.
627 return cli_smb2_setpathinfo(cli,
628 fname,
629 SMB2_0_INFO_FILE,
630 FSCC_FILE_BASIC_INFORMATION,
631 &in_data);
634 return cli_setpathinfo(
635 cli, SMB_FILE_BASIC_INFORMATION, fname, buf, sizeof(buf));
638 struct cli_setfileinfo_ext_state {
639 uint8_t data[40];
640 DATA_BLOB in_data;
643 static void cli_setfileinfo_ext_done(struct tevent_req *subreq);
644 static void cli_setfileinfo_ext_done2(struct tevent_req *subreq);
646 struct tevent_req *cli_setfileinfo_ext_send(
647 TALLOC_CTX *mem_ctx,
648 struct tevent_context *ev,
649 struct cli_state *cli,
650 uint16_t fnum,
651 struct timespec create_time,
652 struct timespec access_time,
653 struct timespec write_time,
654 struct timespec change_time,
655 uint32_t attr)
657 struct tevent_req *req = NULL, *subreq = NULL;
658 struct cli_setfileinfo_ext_state *state = NULL;
660 req = tevent_req_create(
661 mem_ctx, &state, struct cli_setfileinfo_ext_state);
662 if (req == NULL) {
663 return NULL;
665 prep_basic_information_buf(
666 state->data,
667 create_time,
668 access_time,
669 write_time,
670 change_time,
671 attr);
673 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
674 state->in_data = (DATA_BLOB) {
675 .data = state->data, .length = sizeof(state->data),
678 subreq = cli_smb2_set_info_fnum_send(
679 state,
681 cli,
682 fnum,
683 SMB2_0_INFO_FILE,
684 FSCC_FILE_BASIC_INFORMATION,
685 &state->in_data,
686 0); /* in_additional_info */
687 if (tevent_req_nomem(subreq, req)) {
688 return tevent_req_post(req, ev);
690 tevent_req_set_callback(
691 subreq, cli_setfileinfo_ext_done2, req);
692 return req;
695 subreq = cli_setfileinfo_send(
696 state,
698 cli,
699 fnum,
700 SMB_FILE_BASIC_INFORMATION,
701 state->data,
702 sizeof(state->data));
703 if (tevent_req_nomem(subreq, req)) {
704 return tevent_req_post(req, ev);
706 tevent_req_set_callback(subreq, cli_setfileinfo_ext_done, req);
707 return req;
710 static void cli_setfileinfo_ext_done(struct tevent_req *subreq)
712 NTSTATUS status = cli_setfileinfo_recv(subreq);
713 tevent_req_simple_finish_ntstatus(subreq, status);
716 static void cli_setfileinfo_ext_done2(struct tevent_req *subreq)
718 NTSTATUS status = cli_smb2_set_info_fnum_recv(subreq);
719 tevent_req_simple_finish_ntstatus(subreq, status);
722 NTSTATUS cli_setfileinfo_ext_recv(struct tevent_req *req)
724 return tevent_req_simple_recv_ntstatus(req);
727 NTSTATUS cli_setfileinfo_ext(
728 struct cli_state *cli,
729 uint16_t fnum,
730 struct timespec create_time,
731 struct timespec access_time,
732 struct timespec write_time,
733 struct timespec change_time,
734 uint32_t attr)
736 TALLOC_CTX *frame = NULL;
737 struct tevent_context *ev = NULL;
738 struct tevent_req *req = NULL;
739 NTSTATUS status = NT_STATUS_NO_MEMORY;
741 if (smbXcli_conn_has_async_calls(cli->conn)) {
743 * Can't use sync call while an async call is in flight
745 return NT_STATUS_INVALID_PARAMETER;
748 frame = talloc_stackframe();
750 ev = samba_tevent_context_init(frame);
751 if (ev == NULL) {
752 goto fail;
754 req = cli_setfileinfo_ext_send(
757 cli,
758 fnum,
759 create_time,
760 access_time,
761 write_time,
762 change_time,
763 attr);
764 if (req == NULL) {
765 goto fail;
767 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
768 goto fail;
770 status = cli_setfileinfo_ext_recv(req);
771 fail:
772 TALLOC_FREE(frame);
773 return status;
776 /****************************************************************************
777 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
778 ****************************************************************************/
780 struct cli_qpathinfo2_state {
781 struct tevent_context *ev;
782 struct cli_state *cli;
783 const char *fname;
784 struct timespec create_time;
785 struct timespec access_time;
786 struct timespec write_time;
787 struct timespec change_time;
788 off_t size;
789 uint32_t attr;
790 SMB_INO_T ino;
791 mode_t mode;
794 static void cli_qpathinfo2_done2(struct tevent_req *subreq);
795 static void cli_qpathinfo2_done(struct tevent_req *subreq);
796 static void cli_qpathinfo2_got_reparse(struct tevent_req *subreq);
798 struct tevent_req *cli_qpathinfo2_send(TALLOC_CTX *mem_ctx,
799 struct tevent_context *ev,
800 struct cli_state *cli,
801 const char *fname)
803 struct tevent_req *req = NULL, *subreq = NULL;
804 struct cli_qpathinfo2_state *state = NULL;
806 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo2_state);
807 if (req == NULL) {
808 return NULL;
810 state->ev = ev;
811 state->cli = cli;
812 state->fname = fname;
814 state->mode = S_IFREG;
816 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
817 subreq = cli_smb2_qpathinfo_send(state,
819 cli,
820 fname,
821 FSCC_FILE_ALL_INFORMATION,
822 0x60,
823 UINT16_MAX);
824 if (tevent_req_nomem(subreq, req)) {
825 return tevent_req_post(req, ev);
827 tevent_req_set_callback(subreq, cli_qpathinfo2_done2, req);
828 return req;
830 subreq = cli_qpathinfo_send(state, ev, cli, fname,
831 SMB_QUERY_FILE_ALL_INFO,
832 68, CLI_BUFFER_SIZE);
833 if (tevent_req_nomem(subreq, req)) {
834 return tevent_req_post(req, ev);
836 tevent_req_set_callback(subreq, cli_qpathinfo2_done, req);
837 return req;
840 static void cli_qpathinfo2_done2(struct tevent_req *subreq)
842 struct tevent_req *req =
843 tevent_req_callback_data(subreq, struct tevent_req);
844 struct cli_qpathinfo2_state *state =
845 tevent_req_data(req, struct cli_qpathinfo2_state);
846 uint8_t *rdata = NULL;
847 uint32_t num_rdata;
848 NTSTATUS status;
850 status = cli_smb2_qpathinfo_recv(subreq, state, &rdata, &num_rdata);
851 TALLOC_FREE(subreq);
852 if (tevent_req_nterror(req, status)) {
853 return;
855 state->create_time = interpret_long_date(BVAL(rdata, 0x0));
856 state->access_time = interpret_long_date(BVAL(rdata, 0x8));
857 state->write_time = interpret_long_date(BVAL(rdata, 0x10));
858 state->change_time = interpret_long_date(BVAL(rdata, 0x18));
859 state->attr = PULL_LE_U32(rdata, 0x20);
860 state->size = PULL_LE_U64(rdata, 0x30);
861 state->ino = PULL_LE_U64(rdata, 0x40);
863 if (state->attr & FILE_ATTRIBUTE_REPARSE_POINT) {
864 subreq = cli_get_reparse_data_send(state,
865 state->ev,
866 state->cli,
867 state->fname);
868 if (tevent_req_nomem(subreq, req)) {
869 return;
871 tevent_req_set_callback(subreq,
872 cli_qpathinfo2_got_reparse,
873 req);
874 return;
877 tevent_req_done(req);
880 static void cli_qpathinfo2_done(struct tevent_req *subreq)
882 struct tevent_req *req = tevent_req_callback_data(
883 subreq, struct tevent_req);
884 struct cli_qpathinfo2_state *state = tevent_req_data(
885 req, struct cli_qpathinfo2_state);
886 uint8_t *data = NULL;
887 uint32_t num_data;
888 NTSTATUS status;
890 status = cli_qpathinfo_recv(subreq, state, &data, &num_data);
891 TALLOC_FREE(subreq);
892 if (tevent_req_nterror(req, status)) {
893 return;
896 state->create_time = interpret_long_date(BVAL(data, 0));
897 state->access_time = interpret_long_date(BVAL(data, 8));
898 state->write_time = interpret_long_date(BVAL(data, 16));
899 state->change_time = interpret_long_date(BVAL(data, 24));
900 state->attr = PULL_LE_U32(data, 32);
901 state->size = PULL_LE_U64(data, 48);
904 * SMB1 qpathinfo2 uses SMB_QUERY_FILE_ALL_INFO which doesn't
905 * return an inode number (fileid). We can't change this to
906 * one of the FILE_ID info levels as only Win2003 and above
907 * support these [MS-SMB: 2.2.2.3.1] and the SMB1 code needs
908 * to support older servers.
910 state->ino = 0;
912 TALLOC_FREE(data);
914 if (state->attr & FILE_ATTRIBUTE_REPARSE_POINT) {
915 subreq = cli_get_reparse_data_send(state,
916 state->ev,
917 state->cli,
918 state->fname);
919 if (tevent_req_nomem(subreq, req)) {
920 return;
922 tevent_req_set_callback(subreq,
923 cli_qpathinfo2_got_reparse,
924 req);
925 return;
928 tevent_req_done(req);
931 static void cli_qpathinfo2_got_reparse(struct tevent_req *subreq)
933 struct tevent_req *req =
934 tevent_req_callback_data(subreq, struct tevent_req);
935 struct cli_qpathinfo2_state *state =
936 tevent_req_data(req, struct cli_qpathinfo2_state);
937 uint8_t *data = NULL;
938 uint32_t num_data;
939 struct reparse_data_buffer reparse = {
940 .tag = 0,
942 NTSTATUS status;
944 status = cli_get_reparse_data_recv(subreq, state, &data, &num_data);
945 TALLOC_FREE(subreq);
946 if (tevent_req_nterror(req, status)) {
947 return;
950 status = reparse_data_buffer_parse(state, &reparse, data, num_data);
951 if (!NT_STATUS_IS_OK(status)) {
952 DBG_DEBUG("Ignoring unknown reparse data\n");
953 goto done;
956 switch (reparse.tag) {
957 case IO_REPARSE_TAG_SYMLINK:
958 state->mode = S_IFLNK;
959 break;
960 case IO_REPARSE_TAG_NFS:
961 switch (reparse.parsed.nfs.type) {
962 case NFS_SPECFILE_LNK:
963 state->mode = S_IFLNK;
964 break;
965 case NFS_SPECFILE_CHR:
966 state->mode = S_IFCHR;
967 break;
968 case NFS_SPECFILE_BLK:
969 state->mode = S_IFBLK;
970 break;
971 case NFS_SPECFILE_FIFO:
972 state->mode = S_IFIFO;
973 break;
974 case NFS_SPECFILE_SOCK:
975 state->mode = S_IFSOCK;
976 break;
978 break;
980 done:
981 tevent_req_done(req);
984 NTSTATUS cli_qpathinfo2_recv(struct tevent_req *req,
985 struct timespec *create_time,
986 struct timespec *access_time,
987 struct timespec *write_time,
988 struct timespec *change_time,
989 off_t *size,
990 uint32_t *pattr,
991 SMB_INO_T *ino,
992 mode_t *mode)
994 struct cli_qpathinfo2_state *state = tevent_req_data(
995 req, struct cli_qpathinfo2_state);
996 NTSTATUS status;
998 if (tevent_req_is_nterror(req, &status)) {
999 return status;
1002 if (create_time) {
1003 *create_time = state->create_time;
1005 if (access_time) {
1006 *access_time = state->access_time;
1008 if (write_time) {
1009 *write_time = state->write_time;
1011 if (change_time) {
1012 *change_time = state->change_time;
1014 if (pattr) {
1015 *pattr = state->attr;
1017 if (size) {
1018 *size = state->size;
1020 if (ino) {
1021 *ino = state->ino;
1023 if (mode != NULL) {
1024 *mode = state->mode;
1026 return NT_STATUS_OK;
1029 NTSTATUS cli_qpathinfo2(struct cli_state *cli,
1030 const char *fname,
1031 struct timespec *create_time,
1032 struct timespec *access_time,
1033 struct timespec *write_time,
1034 struct timespec *change_time,
1035 off_t *size,
1036 uint32_t *pattr,
1037 SMB_INO_T *ino,
1038 mode_t *mode)
1040 TALLOC_CTX *frame = talloc_stackframe();
1041 struct tevent_context *ev = NULL;
1042 struct tevent_req *req = NULL;
1043 NTSTATUS status = NT_STATUS_NO_MEMORY;
1045 if (smbXcli_conn_has_async_calls(cli->conn)) {
1047 * Can't use sync call while an async call is in flight
1049 status = NT_STATUS_INVALID_PARAMETER;
1050 goto fail;
1052 ev = samba_tevent_context_init(frame);
1053 if (ev == NULL) {
1054 goto fail;
1056 req = cli_qpathinfo2_send(frame, ev, cli, fname);
1057 if (req == NULL) {
1058 goto fail;
1060 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1061 goto fail;
1063 status = cli_qpathinfo2_recv(req,
1064 create_time,
1065 access_time,
1066 write_time,
1067 change_time,
1068 size,
1069 pattr,
1070 ino,
1071 mode);
1072 fail:
1073 TALLOC_FREE(frame);
1074 return status;
1077 /****************************************************************************
1078 Get the stream info
1079 ****************************************************************************/
1081 struct cli_qpathinfo_streams_state {
1082 uint32_t num_data;
1083 uint8_t *data;
1086 static void cli_qpathinfo_streams_done(struct tevent_req *subreq);
1087 static void cli_qpathinfo_streams_done2(struct tevent_req *subreq);
1089 struct tevent_req *cli_qpathinfo_streams_send(TALLOC_CTX *mem_ctx,
1090 struct tevent_context *ev,
1091 struct cli_state *cli,
1092 const char *fname)
1094 struct tevent_req *req = NULL, *subreq = NULL;
1095 struct cli_qpathinfo_streams_state *state = NULL;
1097 req = tevent_req_create(mem_ctx, &state,
1098 struct cli_qpathinfo_streams_state);
1099 if (req == NULL) {
1100 return NULL;
1102 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1103 subreq = cli_smb2_qpathinfo_send(state,
1105 cli,
1106 fname,
1107 FSCC_FILE_STREAM_INFORMATION,
1109 CLI_BUFFER_SIZE);
1110 if (tevent_req_nomem(subreq, req)) {
1111 return tevent_req_post(req, ev);
1113 tevent_req_set_callback(subreq,
1114 cli_qpathinfo_streams_done2,
1115 req);
1116 return req;
1118 subreq = cli_qpathinfo_send(state, ev, cli, fname,
1119 SMB_FILE_STREAM_INFORMATION,
1120 0, CLI_BUFFER_SIZE);
1121 if (tevent_req_nomem(subreq, req)) {
1122 return tevent_req_post(req, ev);
1124 tevent_req_set_callback(subreq, cli_qpathinfo_streams_done, req);
1125 return req;
1128 static void cli_qpathinfo_streams_done(struct tevent_req *subreq)
1130 struct tevent_req *req = tevent_req_callback_data(
1131 subreq, struct tevent_req);
1132 struct cli_qpathinfo_streams_state *state = tevent_req_data(
1133 req, struct cli_qpathinfo_streams_state);
1134 NTSTATUS status;
1136 status = cli_qpathinfo_recv(subreq, state, &state->data,
1137 &state->num_data);
1138 tevent_req_simple_finish_ntstatus(subreq, status);
1141 static void cli_qpathinfo_streams_done2(struct tevent_req *subreq)
1143 struct tevent_req *req =
1144 tevent_req_callback_data(subreq, struct tevent_req);
1145 struct cli_qpathinfo_streams_state *state =
1146 tevent_req_data(req, struct cli_qpathinfo_streams_state);
1147 NTSTATUS status;
1149 status = cli_smb2_qpathinfo_recv(subreq,
1150 state,
1151 &state->data,
1152 &state->num_data);
1153 tevent_req_simple_finish_ntstatus(subreq, status);
1156 NTSTATUS cli_qpathinfo_streams_recv(struct tevent_req *req,
1157 TALLOC_CTX *mem_ctx,
1158 unsigned int *pnum_streams,
1159 struct stream_struct **pstreams)
1161 struct cli_qpathinfo_streams_state *state = tevent_req_data(
1162 req, struct cli_qpathinfo_streams_state);
1163 NTSTATUS status;
1165 if (tevent_req_is_nterror(req, &status)) {
1166 return status;
1168 if (!parse_streams_blob(mem_ctx, state->data, state->num_data,
1169 pnum_streams, pstreams)) {
1170 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1172 return NT_STATUS_OK;
1175 NTSTATUS cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
1176 TALLOC_CTX *mem_ctx,
1177 unsigned int *pnum_streams,
1178 struct stream_struct **pstreams)
1180 TALLOC_CTX *frame = NULL;
1181 struct tevent_context *ev;
1182 struct tevent_req *req;
1183 NTSTATUS status = NT_STATUS_NO_MEMORY;
1185 frame = talloc_stackframe();
1187 if (smbXcli_conn_has_async_calls(cli->conn)) {
1189 * Can't use sync call while an async call is in flight
1191 status = NT_STATUS_INVALID_PARAMETER;
1192 goto fail;
1194 ev = samba_tevent_context_init(frame);
1195 if (ev == NULL) {
1196 goto fail;
1198 req = cli_qpathinfo_streams_send(frame, ev, cli, fname);
1199 if (req == NULL) {
1200 goto fail;
1202 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1203 goto fail;
1205 status = cli_qpathinfo_streams_recv(req, mem_ctx, pnum_streams,
1206 pstreams);
1207 fail:
1208 TALLOC_FREE(frame);
1209 return status;
1212 bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
1213 size_t data_len,
1214 unsigned int *pnum_streams,
1215 struct stream_struct **pstreams)
1217 unsigned int num_streams;
1218 struct stream_struct *streams;
1219 unsigned int ofs;
1221 num_streams = 0;
1222 streams = NULL;
1223 ofs = 0;
1225 while ((data_len > ofs) && (data_len - ofs >= 24)) {
1226 uint32_t nlen, len;
1227 size_t size;
1228 void *vstr;
1229 struct stream_struct *tmp;
1230 uint8_t *tmp_buf;
1232 tmp = talloc_realloc(mem_ctx, streams,
1233 struct stream_struct,
1234 num_streams+1);
1236 if (tmp == NULL) {
1237 goto fail;
1239 streams = tmp;
1241 nlen = IVAL(rdata, ofs + 0x04);
1243 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
1244 rdata, ofs + 0x08);
1245 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
1246 rdata, ofs + 0x10);
1248 if (nlen > data_len - (ofs + 24)) {
1249 goto fail;
1253 * We need to null-terminate src, how do I do this with
1254 * convert_string_talloc??
1257 tmp_buf = talloc_array(streams, uint8_t, nlen+2);
1258 if (tmp_buf == NULL) {
1259 goto fail;
1262 memcpy(tmp_buf, rdata+ofs+24, nlen);
1263 tmp_buf[nlen] = 0;
1264 tmp_buf[nlen+1] = 0;
1266 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
1267 nlen+2, &vstr, &size))
1269 TALLOC_FREE(tmp_buf);
1270 goto fail;
1273 TALLOC_FREE(tmp_buf);
1274 streams[num_streams].name = (char *)vstr;
1275 num_streams++;
1277 len = IVAL(rdata, ofs);
1278 if (len > data_len - ofs) {
1279 goto fail;
1281 if (len == 0) break;
1282 ofs += len;
1285 *pnum_streams = num_streams;
1286 *pstreams = streams;
1287 return true;
1289 fail:
1290 TALLOC_FREE(streams);
1291 return false;
1294 /****************************************************************************
1295 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1296 ****************************************************************************/
1298 struct cli_qfileinfo_basic_state {
1299 uint32_t attr;
1300 off_t size;
1301 struct timespec create_time;
1302 struct timespec access_time;
1303 struct timespec write_time;
1304 struct timespec change_time;
1305 SMB_INO_T ino;
1308 static void cli_qfileinfo_basic_done(struct tevent_req *subreq);
1309 static void cli_qfileinfo_basic_doneE(struct tevent_req *subreq);
1311 struct tevent_req *cli_qfileinfo_basic_send(
1312 TALLOC_CTX *mem_ctx,
1313 struct tevent_context *ev,
1314 struct cli_state *cli,
1315 uint16_t fnum)
1317 struct tevent_req *req = NULL, *subreq = NULL;
1318 struct cli_qfileinfo_basic_state *state = NULL;
1320 req = tevent_req_create(
1321 mem_ctx, &state, struct cli_qfileinfo_basic_state);
1322 if (req == NULL) {
1323 return NULL;
1326 if ((smbXcli_conn_protocol(cli->conn) < PROTOCOL_LANMAN2) ||
1327 cli->win95) {
1329 * According to
1330 * https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cifs/3d9d8f3e-dc70-410d-a3fc-6f4a881e8cab
1331 * SMB_COM_TRANSACTION2 used in cli_qfileinfo_send()
1332 * further down was introduced with the LAN Manager
1333 * 1.2 dialect, which we encode as PROTOCOL_LANMAN2.
1335 * The "win95" check was introduced with commit
1336 * 27e5850fd3e1c8 in 1998. Hard to check these days,
1337 * but leave it in.
1339 * Use a lowerlevel fallback in both cases.
1342 subreq = cli_getattrE_send(state, ev, cli, fnum);
1343 if (tevent_req_nomem(subreq, req)) {
1344 return tevent_req_post(req, ev);
1346 tevent_req_set_callback(
1347 subreq, cli_qfileinfo_basic_doneE, req);
1348 return req;
1351 subreq = cli_qfileinfo_send(state,
1353 cli,
1354 fnum,
1355 FSCC_FILE_ALL_INFORMATION, /* level */
1356 68, /* min_rdata */
1357 CLI_BUFFER_SIZE); /* max_rdata */
1358 if (tevent_req_nomem(subreq, req)) {
1359 return tevent_req_post(req, ev);
1361 tevent_req_set_callback(subreq, cli_qfileinfo_basic_done, req);
1362 return req;
1365 static void cli_qfileinfo_basic_done(struct tevent_req *subreq)
1367 struct tevent_req *req = tevent_req_callback_data(
1368 subreq, struct tevent_req);
1369 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1370 req, struct cli_qfileinfo_basic_state);
1371 uint8_t *rdata;
1372 uint32_t num_rdata;
1373 NTSTATUS status;
1375 status = cli_qfileinfo_recv(
1376 subreq, state, NULL, &rdata, &num_rdata);
1377 TALLOC_FREE(subreq);
1378 if (tevent_req_nterror(req, status)) {
1379 return;
1382 state->create_time = interpret_long_date(BVAL(rdata, 0));
1383 state->access_time = interpret_long_date(BVAL(rdata, 8));
1384 state->write_time = interpret_long_date(BVAL(rdata, 16));
1385 state->change_time = interpret_long_date(BVAL(rdata, 24));
1386 state->attr = PULL_LE_U32(rdata, 32);
1387 state->size = PULL_LE_U64(rdata,48);
1388 state->ino = PULL_LE_U32(rdata, 64);
1389 TALLOC_FREE(rdata);
1391 tevent_req_done(req);
1394 static void cli_qfileinfo_basic_doneE(struct tevent_req *subreq)
1396 struct tevent_req *req = tevent_req_callback_data(
1397 subreq, struct tevent_req);
1398 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1399 req, struct cli_qfileinfo_basic_state);
1400 NTSTATUS status;
1402 status = cli_getattrE_recv(
1403 subreq,
1404 &state->attr,
1405 &state->size,
1406 &state->change_time.tv_sec,
1407 &state->access_time.tv_sec,
1408 &state->write_time.tv_sec);
1409 TALLOC_FREE(subreq);
1410 if (tevent_req_nterror(req, status)) {
1411 return;
1413 tevent_req_done(req);
1416 NTSTATUS cli_qfileinfo_basic_recv(
1417 struct tevent_req *req,
1418 uint32_t *attr,
1419 off_t *size,
1420 struct timespec *create_time,
1421 struct timespec *access_time,
1422 struct timespec *write_time,
1423 struct timespec *change_time,
1424 SMB_INO_T *ino)
1426 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1427 req, struct cli_qfileinfo_basic_state);
1428 NTSTATUS status;
1430 if (tevent_req_is_nterror(req, &status)) {
1431 return status;
1434 if (create_time != NULL) {
1435 *create_time = state->create_time;
1437 if (access_time != NULL) {
1438 *access_time = state->access_time;
1440 if (write_time != NULL) {
1441 *write_time = state->write_time;
1443 if (change_time != NULL) {
1444 *change_time = state->change_time;
1446 if (attr != NULL) {
1447 *attr = state->attr;
1449 if (size != NULL) {
1450 *size = state->size;
1452 if (ino) {
1453 *ino = state->ino;
1456 return NT_STATUS_OK;
1458 /****************************************************************************
1459 Send a qfileinfo call.
1460 ****************************************************************************/
1462 NTSTATUS cli_qfileinfo_basic(
1463 struct cli_state *cli,
1464 uint16_t fnum,
1465 uint32_t *attr,
1466 off_t *size,
1467 struct timespec *create_time,
1468 struct timespec *access_time,
1469 struct timespec *write_time,
1470 struct timespec *change_time,
1471 SMB_INO_T *ino)
1473 TALLOC_CTX *frame = NULL;
1474 struct tevent_context *ev = NULL;
1475 struct tevent_req *req = NULL;
1476 NTSTATUS status = NT_STATUS_NO_MEMORY;
1478 frame = talloc_stackframe();
1480 if (smbXcli_conn_has_async_calls(cli->conn)) {
1482 * Can't use sync call while an async call is in flight
1484 status = NT_STATUS_INVALID_PARAMETER;
1485 goto fail;
1487 ev = samba_tevent_context_init(frame);
1488 if (ev == NULL) {
1489 goto fail;
1491 req = cli_qfileinfo_basic_send(frame, ev, cli, fnum);
1492 if (req == NULL) {
1493 goto fail;
1495 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1496 goto fail;
1499 status = cli_qfileinfo_basic_recv(
1500 req,
1501 attr,
1502 size,
1503 create_time,
1504 access_time,
1505 write_time,
1506 change_time,
1507 ino);
1509 /* cli_smb2_query_info_fnum_recv doesn't set this */
1510 cli->raw_status = status;
1511 fail:
1512 TALLOC_FREE(frame);
1513 return status;
1516 /****************************************************************************
1517 Send a qpathinfo BASIC_INFO call.
1518 ****************************************************************************/
1520 struct cli_qpathinfo_basic_state {
1521 uint32_t num_data;
1522 uint8_t *data;
1525 static void cli_qpathinfo_basic_done(struct tevent_req *subreq);
1527 struct tevent_req *cli_qpathinfo_basic_send(TALLOC_CTX *mem_ctx,
1528 struct tevent_context *ev,
1529 struct cli_state *cli,
1530 const char *fname)
1532 struct tevent_req *req = NULL, *subreq = NULL;
1533 struct cli_qpathinfo_basic_state *state = NULL;
1535 req = tevent_req_create(mem_ctx, &state,
1536 struct cli_qpathinfo_basic_state);
1537 if (req == NULL) {
1538 return NULL;
1540 subreq = cli_qpathinfo_send(state, ev, cli, fname,
1541 SMB_QUERY_FILE_BASIC_INFO,
1542 36, CLI_BUFFER_SIZE);
1543 if (tevent_req_nomem(subreq, req)) {
1544 return tevent_req_post(req, ev);
1546 tevent_req_set_callback(subreq, cli_qpathinfo_basic_done, req);
1547 return req;
1550 static void cli_qpathinfo_basic_done(struct tevent_req *subreq)
1552 struct tevent_req *req = tevent_req_callback_data(
1553 subreq, struct tevent_req);
1554 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1555 req, struct cli_qpathinfo_basic_state);
1556 NTSTATUS status;
1558 status = cli_qpathinfo_recv(subreq, state, &state->data,
1559 &state->num_data);
1560 TALLOC_FREE(subreq);
1561 if (tevent_req_nterror(req, status)) {
1562 return;
1564 tevent_req_done(req);
1567 NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
1568 SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
1570 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1571 req, struct cli_qpathinfo_basic_state);
1572 NTSTATUS status;
1574 if (tevent_req_is_nterror(req, &status)) {
1575 return status;
1578 sbuf->st_ex_btime = interpret_long_date(BVAL(state->data, 0));
1579 sbuf->st_ex_atime = interpret_long_date(BVAL(state->data, 8));
1580 sbuf->st_ex_mtime = interpret_long_date(BVAL(state->data, 16));
1581 sbuf->st_ex_ctime = interpret_long_date(BVAL(state->data, 24));
1582 *attributes = IVAL(state->data, 32);
1583 return NT_STATUS_OK;
1586 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
1587 SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
1589 TALLOC_CTX *frame = NULL;
1590 struct tevent_context *ev;
1591 struct tevent_req *req;
1592 NTSTATUS status = NT_STATUS_NO_MEMORY;
1594 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1595 return cli_smb2_qpathinfo_basic(cli,
1596 name,
1597 sbuf,
1598 attributes);
1601 frame = talloc_stackframe();
1603 if (smbXcli_conn_has_async_calls(cli->conn)) {
1605 * Can't use sync call while an async call is in flight
1607 status = NT_STATUS_INVALID_PARAMETER;
1608 goto fail;
1610 ev = samba_tevent_context_init(frame);
1611 if (ev == NULL) {
1612 goto fail;
1614 req = cli_qpathinfo_basic_send(frame, ev, cli, name);
1615 if (req == NULL) {
1616 goto fail;
1618 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1619 goto fail;
1621 status = cli_qpathinfo_basic_recv(req, sbuf, attributes);
1622 fail:
1623 TALLOC_FREE(frame);
1624 return status;
1627 /****************************************************************************
1628 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1629 ****************************************************************************/
1631 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1633 uint8_t *rdata;
1634 uint32_t num_rdata;
1635 unsigned int len;
1636 char *converted = NULL;
1637 size_t converted_size = 0;
1638 NTSTATUS status;
1640 status = cli_qpathinfo(talloc_tos(), cli, fname,
1641 SMB_QUERY_FILE_ALT_NAME_INFO,
1642 4, CLI_BUFFER_SIZE, &rdata, &num_rdata);
1643 if (!NT_STATUS_IS_OK(status)) {
1644 return status;
1647 len = IVAL(rdata, 0);
1649 if (len > num_rdata - 4) {
1650 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1653 /* The returned data is a pushed string, not raw data. */
1654 if (!convert_string_talloc(talloc_tos(),
1655 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
1656 CH_UNIX,
1657 rdata + 4,
1658 len,
1659 &converted,
1660 &converted_size)) {
1661 return NT_STATUS_NO_MEMORY;
1663 fstrcpy(alt_name, converted);
1665 TALLOC_FREE(converted);
1666 TALLOC_FREE(rdata);
1668 return NT_STATUS_OK;
1671 /****************************************************************************
1672 Send a qpathinfo SMB_QUERY_FILE_STANDARD_INFO call.
1673 ****************************************************************************/
1675 NTSTATUS cli_qpathinfo_standard(struct cli_state *cli, const char *fname,
1676 uint64_t *allocated, uint64_t *size,
1677 uint32_t *nlinks,
1678 bool *is_del_pending, bool *is_dir)
1680 uint8_t *rdata;
1681 uint32_t num_rdata;
1682 NTSTATUS status;
1684 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1685 return NT_STATUS_NOT_IMPLEMENTED;
1688 status = cli_qpathinfo(talloc_tos(), cli, fname,
1689 SMB_QUERY_FILE_STANDARD_INFO,
1690 24, CLI_BUFFER_SIZE, &rdata, &num_rdata);
1691 if (!NT_STATUS_IS_OK(status)) {
1692 return status;
1695 if (allocated) {
1696 *allocated = BVAL(rdata, 0);
1699 if (size) {
1700 *size = BVAL(rdata, 8);
1703 if (nlinks) {
1704 *nlinks = IVAL(rdata, 16);
1707 if (is_del_pending) {
1708 *is_del_pending = CVAL(rdata, 20);
1711 if (is_dir) {
1712 *is_dir = CVAL(rdata, 20);
1715 TALLOC_FREE(rdata);
1717 return NT_STATUS_OK;
1721 /* like cli_qpathinfo2 but do not use SMB_QUERY_FILE_ALL_INFO with smb1 */
1722 NTSTATUS cli_qpathinfo3(struct cli_state *cli, const char *fname,
1723 struct timespec *create_time,
1724 struct timespec *access_time,
1725 struct timespec *write_time,
1726 struct timespec *change_time,
1727 off_t *size, uint32_t *pattr,
1728 SMB_INO_T *ino)
1730 NTSTATUS status = NT_STATUS_OK;
1731 SMB_STRUCT_STAT st = { 0 };
1732 uint32_t attr = 0;
1733 uint64_t pos;
1735 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1737 * NB. cli_qpathinfo2() checks pattr is valid before
1738 * storing a value into it, so we don't need to use
1739 * an intermediate attr variable as below but can
1740 * pass pattr directly.
1742 return cli_qpathinfo2(cli,
1743 fname,
1744 create_time,
1745 access_time,
1746 write_time,
1747 change_time,
1748 size,
1749 pattr,
1750 ino,
1751 NULL);
1754 if (create_time || access_time || write_time || change_time || pattr) {
1756 * cli_qpathinfo_basic() always indirects the passed
1757 * in pointers so we use intermediate variables to
1758 * collect all of them before assigning any requested
1759 * below.
1761 status = cli_qpathinfo_basic(cli, fname, &st, &attr);
1762 if (!NT_STATUS_IS_OK(status)) {
1763 return status;
1767 if (size) {
1768 status = cli_qpathinfo_standard(cli, fname,
1769 NULL, &pos, NULL, NULL, NULL);
1770 if (!NT_STATUS_IS_OK(status)) {
1771 return status;
1774 *size = pos;
1777 if (create_time) {
1778 *create_time = st.st_ex_btime;
1780 if (access_time) {
1781 *access_time = st.st_ex_atime;
1783 if (write_time) {
1784 *write_time = st.st_ex_mtime;
1786 if (change_time) {
1787 *change_time = st.st_ex_ctime;
1789 if (pattr) {
1790 *pattr = attr;
1792 if (ino) {
1793 *ino = 0;
1796 return NT_STATUS_OK;