CVE-2023-3961:s3:torture: Add test SMB2-INVALID-PIPENAME to show we allow bad pipenam...
[Samba.git] / source3 / libsmb / clirap.c
blob7944eeb847c2e2daf2783f299066812d97ebace4
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 "cli_smb2_fnum.h"
32 #include "lib/util/string_wrappers.h"
34 #include <gnutls/gnutls.h>
35 #include <gnutls/crypto.h>
37 #define PIPE_LANMAN "\\PIPE\\LANMAN"
39 /****************************************************************************
40 Call a remote api
41 ****************************************************************************/
43 bool cli_api(struct cli_state *cli,
44 char *param, int prcnt, int mprcnt,
45 char *data, int drcnt, int mdrcnt,
46 char **rparam, unsigned int *rprcnt,
47 char **rdata, unsigned int *rdrcnt)
49 NTSTATUS status;
51 uint8_t *my_rparam, *my_rdata;
52 uint32_t num_my_rparam, num_my_rdata;
54 status = cli_trans(talloc_tos(), cli, SMBtrans,
55 PIPE_LANMAN, 0, /* name, fid */
56 0, 0, /* function, flags */
57 NULL, 0, 0, /* setup */
58 (uint8_t *)param, prcnt, mprcnt, /* Params, length, max */
59 (uint8_t *)data, drcnt, mdrcnt, /* Data, length, max */
60 NULL, /* recv_flags2 */
61 NULL, 0, NULL, /* rsetup */
62 &my_rparam, 0, &num_my_rparam,
63 &my_rdata, 0, &num_my_rdata);
64 if (!NT_STATUS_IS_OK(status)) {
65 return false;
69 * I know this memcpy massively hurts, but there are just tons
70 * of callers of cli_api that eventually need changing to
71 * talloc
74 *rparam = (char *)smb_memdup(my_rparam, num_my_rparam);
75 if (*rparam == NULL) {
76 goto fail;
78 *rprcnt = num_my_rparam;
79 TALLOC_FREE(my_rparam);
81 *rdata = (char *)smb_memdup(my_rdata, num_my_rdata);
82 if (*rdata == NULL) {
83 goto fail;
85 *rdrcnt = num_my_rdata;
86 TALLOC_FREE(my_rdata);
88 return true;
89 fail:
90 TALLOC_FREE(my_rdata);
91 TALLOC_FREE(my_rparam);
92 *rparam = NULL;
93 *rprcnt = 0;
94 *rdata = NULL;
95 *rdrcnt = 0;
96 return false;
99 /****************************************************************************
100 Call a NetShareEnum - try and browse available connections on a host.
101 ****************************************************************************/
103 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32_t, const char *, void *), void *state)
105 char *rparam = NULL;
106 char *rdata = NULL;
107 char *p;
108 unsigned int rdrcnt,rprcnt;
109 char param[1024];
110 int count = -1;
111 bool ok;
112 int res;
114 /* now send a SMBtrans command with api RNetShareEnum */
115 p = param;
116 SSVAL(p,0,0); /* api number */
117 p += 2;
118 strlcpy(p,"WrLeh",sizeof(param)-PTR_DIFF(p,param));
119 p = skip_string(param,sizeof(param),p);
120 strlcpy(p,"B13BWz",sizeof(param)-PTR_DIFF(p,param));
121 p = skip_string(param,sizeof(param),p);
122 SSVAL(p,0,1);
124 * Win2k needs a *smaller* buffer than 0xFFFF here -
125 * it returns "out of server memory" with 0xFFFF !!! JRA.
127 SSVAL(p,2,0xFFE0);
128 p += 4;
130 ok = cli_api(
131 cli,
132 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
133 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
134 &rparam, &rprcnt, /* return params, length */
135 &rdata, &rdrcnt); /* return data, length */
136 if (!ok) {
137 DEBUG(4,("NetShareEnum failed\n"));
138 goto done;
141 if (rprcnt < 6) {
142 DBG_ERR("Got invalid result: rprcnt=%u\n", rprcnt);
143 goto done;
146 res = rparam? SVAL(rparam,0) : -1;
148 if (res == 0 || res == ERRmoredata) {
149 int converter=SVAL(rparam,2);
150 int i;
151 char *rdata_end = rdata + rdrcnt;
153 count=SVAL(rparam,4);
154 p = rdata;
156 for (i=0;i<count;i++,p+=20) {
157 char *sname;
158 int type;
159 int comment_offset;
160 const char *cmnt;
161 const char *p1;
162 char *s1, *s2;
163 size_t len;
164 TALLOC_CTX *frame = talloc_stackframe();
166 if (p + 20 > rdata_end) {
167 TALLOC_FREE(frame);
168 break;
171 sname = p;
172 type = SVAL(p,14);
173 comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
174 if (comment_offset < 0 ||
175 comment_offset > (int)rdrcnt) {
176 TALLOC_FREE(frame);
177 break;
179 cmnt = comment_offset?(rdata+comment_offset):"";
181 /* Work out the comment length. */
182 for (p1 = cmnt, len = 0; *p1 &&
183 p1 < rdata_end; len++)
184 p1++;
185 if (!*p1) {
186 len++;
188 pull_string_talloc(frame,rdata,0,
189 &s1,sname,14,STR_ASCII);
190 pull_string_talloc(frame,rdata,0,
191 &s2,cmnt,len,STR_ASCII);
192 if (!s1 || !s2) {
193 TALLOC_FREE(frame);
194 continue;
197 fn(s1, type, s2, state);
199 TALLOC_FREE(frame);
201 } else {
202 DEBUG(4,("NetShareEnum res=%d\n", res));
205 done:
206 SAFE_FREE(rparam);
207 SAFE_FREE(rdata);
209 return count;
212 /****************************************************************************
213 Call a NetServerEnum for the specified workgroup and servertype mask. This
214 function then calls the specified callback function for each name returned.
216 The callback function takes 4 arguments: the machine name, the server type,
217 the comment and a state pointer.
218 ****************************************************************************/
220 bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32_t stype,
221 void (*fn)(const char *, uint32_t, const char *, void *),
222 void *state)
224 char *rparam = NULL;
225 char *rdata = NULL;
226 char *rdata_end = NULL;
227 unsigned int rdrcnt,rprcnt;
228 char *p;
229 char param[1024];
230 int uLevel = 1;
231 size_t len;
232 uint32_t func = RAP_NetServerEnum2;
233 char *last_entry = NULL;
234 int total_cnt = 0;
235 int return_cnt = 0;
236 int res;
238 errno = 0; /* reset */
241 * This may take more than one transaction, so we should loop until
242 * we no longer get a more data to process or we have all of the
243 * items.
245 do {
246 /* send a SMBtrans command with api NetServerEnum */
247 p = param;
248 SIVAL(p,0,func); /* api number */
249 p += 2;
251 if (func == RAP_NetServerEnum3) {
252 strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
253 } else {
254 strlcpy(p,"WrLehDz", sizeof(param)-PTR_DIFF(p,param));
257 p = skip_string(param, sizeof(param), p);
258 strlcpy(p,"B16BBDz", sizeof(param)-PTR_DIFF(p,param));
260 p = skip_string(param, sizeof(param), p);
261 SSVAL(p,0,uLevel);
262 SSVAL(p,2,CLI_BUFFER_SIZE);
263 p += 4;
264 SIVAL(p,0,stype);
265 p += 4;
267 /* If we have more data, tell the server where
268 * to continue from.
270 len = push_ascii(p,
271 workgroup,
272 sizeof(param) - PTR_DIFF(p,param) - 1,
273 STR_TERMINATE|STR_UPPER);
275 if (len == 0) {
276 SAFE_FREE(last_entry);
277 return false;
279 p += len;
281 if (func == RAP_NetServerEnum3) {
282 len = push_ascii(p,
283 last_entry ? last_entry : "",
284 sizeof(param) - PTR_DIFF(p,param) - 1,
285 STR_TERMINATE);
287 if (len == 0) {
288 SAFE_FREE(last_entry);
289 return false;
291 p += len;
294 /* Next time through we need to use the continue api */
295 func = RAP_NetServerEnum3;
297 if (!cli_api(cli,
298 param, PTR_DIFF(p,param), 8, /* params, length, max */
299 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
300 &rparam, &rprcnt, /* return params, return size */
301 &rdata, &rdrcnt)) { /* return data, return size */
303 /* break out of the loop on error */
304 res = -1;
305 break;
308 rdata_end = rdata + rdrcnt;
310 if (rprcnt < 6) {
311 DBG_ERR("Got invalid result: rprcnt=%u\n", rprcnt);
312 res = -1;
313 break;
316 res = rparam ? SVAL(rparam,0) : -1;
318 if (res == 0 || res == ERRmoredata ||
319 (res != -1 && cli_errno(cli) == 0)) {
320 char *sname = NULL;
321 int i, count;
322 int converter=SVAL(rparam,2);
324 /* Get the number of items returned in this buffer */
325 count = SVAL(rparam, 4);
327 /* The next field contains the number of items left,
328 * including those returned in this buffer. So the
329 * first time through this should contain all of the
330 * entries.
332 if (total_cnt == 0) {
333 total_cnt = SVAL(rparam, 6);
336 /* Keep track of how many we have read */
337 return_cnt += count;
338 p = rdata;
340 /* The last name in the previous NetServerEnum reply is
341 * sent back to server in the NetServerEnum3 request
342 * (last_entry). The next reply should repeat this entry
343 * as the first element. We have no proof that this is
344 * always true, but from traces that seems to be the
345 * behavior from Window Servers. So first lets do a lot
346 * of checking, just being paranoid. If the string
347 * matches then we already saw this entry so skip it.
349 * NOTE: sv1_name field must be null terminated and has
350 * a max size of 16 (NetBIOS Name).
352 if (last_entry && count && p &&
353 (strncmp(last_entry, p, 16) == 0)) {
354 count -= 1; /* Skip this entry */
355 return_cnt = -1; /* Not part of total, so don't count. */
356 p = rdata + 26; /* Skip the whole record */
359 for (i = 0; i < count; i++, p += 26) {
360 int comment_offset;
361 const char *cmnt;
362 const char *p1;
363 char *s1, *s2;
364 TALLOC_CTX *frame = talloc_stackframe();
365 uint32_t entry_stype;
367 if (p + 26 > rdata_end) {
368 TALLOC_FREE(frame);
369 break;
372 sname = p;
373 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
374 cmnt = comment_offset?(rdata+comment_offset):"";
376 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
377 TALLOC_FREE(frame);
378 continue;
381 /* Work out the comment length. */
382 for (p1 = cmnt, len = 0; *p1 &&
383 p1 < rdata_end; len++)
384 p1++;
385 if (!*p1) {
386 len++;
389 entry_stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
391 pull_string_talloc(frame,rdata,0,
392 &s1,sname,16,STR_ASCII);
393 pull_string_talloc(frame,rdata,0,
394 &s2,cmnt,len,STR_ASCII);
396 if (!s1 || !s2) {
397 TALLOC_FREE(frame);
398 continue;
401 fn(s1, entry_stype, s2, state);
402 TALLOC_FREE(frame);
405 /* We are done with the old last entry, so now we can free it */
406 if (last_entry) {
407 SAFE_FREE(last_entry); /* This will set it to null */
410 /* We always make a copy of the last entry if we have one */
411 if (sname) {
412 last_entry = smb_xstrdup(sname);
415 /* If we have more data, but no last entry then error out */
416 if (!last_entry && (res == ERRmoredata)) {
417 errno = EINVAL;
418 res = 0;
423 SAFE_FREE(rparam);
424 SAFE_FREE(rdata);
425 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
427 SAFE_FREE(rparam);
428 SAFE_FREE(rdata);
429 SAFE_FREE(last_entry);
431 if (res == -1) {
432 errno = cli_errno(cli);
433 } else {
434 if (!return_cnt) {
435 /* this is a very special case, when the domain master for the
436 work group isn't part of the work group itself, there is something
437 wild going on */
438 errno = ENOENT;
442 return(return_cnt > 0);
445 /****************************************************************************
446 Send a SamOEMChangePassword command.
447 ****************************************************************************/
449 bool cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
450 const char *old_password)
452 char param[1024];
453 unsigned char data[532];
454 char *p = param;
455 unsigned char old_pw_hash[16];
456 unsigned char new_pw_hash[16];
457 unsigned int data_len;
458 unsigned int param_len = 0;
459 char *rparam = NULL;
460 char *rdata = NULL;
461 unsigned int rprcnt, rdrcnt;
462 gnutls_cipher_hd_t cipher_hnd = NULL;
463 gnutls_datum_t old_pw_key = {
464 .data = old_pw_hash,
465 .size = sizeof(old_pw_hash),
467 int rc;
469 if (strlen(user) >= sizeof(fstring)-1) {
470 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
471 return False;
474 SSVAL(p,0,214); /* SamOEMChangePassword command. */
475 p += 2;
476 strlcpy(p, "zsT", sizeof(param)-PTR_DIFF(p,param));
477 p = skip_string(param,sizeof(param),p);
478 strlcpy(p, "B516B16", sizeof(param)-PTR_DIFF(p,param));
479 p = skip_string(param,sizeof(param),p);
480 strlcpy(p,user, sizeof(param)-PTR_DIFF(p,param));
481 p = skip_string(param,sizeof(param),p);
482 SSVAL(p,0,532);
483 p += 2;
485 param_len = PTR_DIFF(p,param);
488 * Get the Lanman hash of the old password, we
489 * use this as the key to make_oem_passwd_hash().
491 E_deshash(old_password, old_pw_hash);
493 encode_pw_buffer(data, new_password, STR_ASCII);
495 #ifdef DEBUG_PASSWORD
496 DEBUG(100,("make_oem_passwd_hash\n"));
497 dump_data(100, data, 516);
498 #endif
499 rc = gnutls_cipher_init(&cipher_hnd,
500 GNUTLS_CIPHER_ARCFOUR_128,
501 &old_pw_key,
502 NULL);
503 if (rc < 0) {
504 DBG_ERR("gnutls_cipher_init failed: %s\n",
505 gnutls_strerror(rc));
506 return false;
508 rc = gnutls_cipher_encrypt(cipher_hnd,
509 data,
510 516);
511 gnutls_cipher_deinit(cipher_hnd);
512 if (rc < 0) {
513 return false;
517 * Now place the old password hash in the data.
519 E_deshash(new_password, new_pw_hash);
521 rc = E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
522 if (rc != 0) {
523 DBG_ERR("E_old_pw_hash failed: %s\n", gnutls_strerror(rc));
524 return false;
527 data_len = 532;
529 if (!cli_api(cli,
530 param, param_len, 4, /* param, length, max */
531 (char *)data, data_len, 0, /* data, length, max */
532 &rparam, &rprcnt,
533 &rdata, &rdrcnt)) {
534 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
535 user ));
536 return False;
539 if (rdrcnt < 2) {
540 cli->rap_error = ERRbadformat;
541 goto done;
544 if (rparam) {
545 cli->rap_error = SVAL(rparam,0);
548 done:
549 SAFE_FREE(rparam);
550 SAFE_FREE(rdata);
552 return (cli->rap_error == 0);
555 static void prep_basic_information_buf(
556 uint8_t buf[40],
557 struct timespec create_time,
558 struct timespec access_time,
559 struct timespec write_time,
560 struct timespec change_time,
561 uint32_t attr)
563 char *p = (char *)buf;
565 * Add the create, last access, modification, and status change times
567 put_long_date_full_timespec(
568 TIMESTAMP_SET_NT_OR_BETTER, p, &create_time);
569 p += 8;
571 put_long_date_full_timespec(
572 TIMESTAMP_SET_NT_OR_BETTER, p, &access_time);
573 p += 8;
575 put_long_date_full_timespec(
576 TIMESTAMP_SET_NT_OR_BETTER, p, &write_time);
577 p += 8;
579 put_long_date_full_timespec(
580 TIMESTAMP_SET_NT_OR_BETTER, p, &change_time);
581 p += 8;
583 if (attr == (uint32_t)-1 || attr == FILE_ATTRIBUTE_NORMAL) {
584 /* No change. */
585 attr = 0;
586 } else if (attr == 0) {
587 /* Clear all existing attributes. */
588 attr = FILE_ATTRIBUTE_NORMAL;
591 /* Add attributes */
592 SIVAL(p, 0, attr);
594 p += 4;
596 /* Add padding */
597 SIVAL(p, 0, 0);
598 p += 4;
600 SMB_ASSERT(PTR_DIFF(p, buf) == 40);
603 NTSTATUS cli_setpathinfo_ext(struct cli_state *cli, const char *fname,
604 struct timespec create_time,
605 struct timespec access_time,
606 struct timespec write_time,
607 struct timespec change_time,
608 uint32_t attr)
610 uint8_t buf[40];
612 prep_basic_information_buf(
613 buf,
614 create_time,
615 access_time,
616 write_time,
617 change_time,
618 attr);
620 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
621 DATA_BLOB in_data = data_blob_const(buf, sizeof(buf));
623 * Split out SMB2 here as we need to select
624 * the correct info type and level.
626 return cli_smb2_setpathinfo(cli,
627 fname,
628 1, /* SMB2_SETINFO_FILE */
629 SMB_FILE_BASIC_INFORMATION - 1000,
630 &in_data);
633 return cli_setpathinfo(
634 cli, SMB_FILE_BASIC_INFORMATION, fname, buf, sizeof(buf));
637 struct cli_setfileinfo_ext_state {
638 uint8_t data[40];
639 DATA_BLOB in_data;
642 static void cli_setfileinfo_ext_done(struct tevent_req *subreq);
643 static void cli_setfileinfo_ext_done2(struct tevent_req *subreq);
645 struct tevent_req *cli_setfileinfo_ext_send(
646 TALLOC_CTX *mem_ctx,
647 struct tevent_context *ev,
648 struct cli_state *cli,
649 uint16_t fnum,
650 struct timespec create_time,
651 struct timespec access_time,
652 struct timespec write_time,
653 struct timespec change_time,
654 uint32_t attr)
656 struct tevent_req *req = NULL, *subreq = NULL;
657 struct cli_setfileinfo_ext_state *state = NULL;
659 req = tevent_req_create(
660 mem_ctx, &state, struct cli_setfileinfo_ext_state);
661 if (req == NULL) {
662 return NULL;
664 prep_basic_information_buf(
665 state->data,
666 create_time,
667 access_time,
668 write_time,
669 change_time,
670 attr);
672 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
673 state->in_data = (DATA_BLOB) {
674 .data = state->data, .length = sizeof(state->data),
677 subreq = cli_smb2_set_info_fnum_send(
678 state,
680 cli,
681 fnum,
682 SMB2_0_INFO_FILE,
683 SMB_FILE_BASIC_INFORMATION - 1000,
684 &state->in_data,
685 0); /* in_additional_info */
686 if (tevent_req_nomem(subreq, req)) {
687 return tevent_req_post(req, ev);
689 tevent_req_set_callback(
690 subreq, cli_setfileinfo_ext_done2, req);
691 return req;
694 subreq = cli_setfileinfo_send(
695 state,
697 cli,
698 fnum,
699 SMB_FILE_BASIC_INFORMATION,
700 state->data,
701 sizeof(state->data));
702 if (tevent_req_nomem(subreq, req)) {
703 return tevent_req_post(req, ev);
705 tevent_req_set_callback(subreq, cli_setfileinfo_ext_done, req);
706 return req;
709 static void cli_setfileinfo_ext_done(struct tevent_req *subreq)
711 NTSTATUS status = cli_setfileinfo_recv(subreq);
712 tevent_req_simple_finish_ntstatus(subreq, status);
715 static void cli_setfileinfo_ext_done2(struct tevent_req *subreq)
717 NTSTATUS status = cli_smb2_set_info_fnum_recv(subreq);
718 tevent_req_simple_finish_ntstatus(subreq, status);
721 NTSTATUS cli_setfileinfo_ext_recv(struct tevent_req *req)
723 return tevent_req_simple_recv_ntstatus(req);
726 NTSTATUS cli_setfileinfo_ext(
727 struct cli_state *cli,
728 uint16_t fnum,
729 struct timespec create_time,
730 struct timespec access_time,
731 struct timespec write_time,
732 struct timespec change_time,
733 uint32_t attr)
735 TALLOC_CTX *frame = NULL;
736 struct tevent_context *ev = NULL;
737 struct tevent_req *req = NULL;
738 NTSTATUS status = NT_STATUS_NO_MEMORY;
740 if (smbXcli_conn_has_async_calls(cli->conn)) {
742 * Can't use sync call while an async call is in flight
744 return NT_STATUS_INVALID_PARAMETER;
747 frame = talloc_stackframe();
749 ev = samba_tevent_context_init(frame);
750 if (ev == NULL) {
751 goto fail;
753 req = cli_setfileinfo_ext_send(
756 cli,
757 fnum,
758 create_time,
759 access_time,
760 write_time,
761 change_time,
762 attr);
763 if (req == NULL) {
764 goto fail;
766 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
767 goto fail;
769 status = cli_setfileinfo_ext_recv(req);
770 fail:
771 TALLOC_FREE(frame);
772 return status;
775 /****************************************************************************
776 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
777 ****************************************************************************/
779 struct cli_qpathinfo2_state {
780 struct timespec create_time;
781 struct timespec access_time;
782 struct timespec write_time;
783 struct timespec change_time;
784 off_t size;
785 uint32_t attr;
786 SMB_INO_T ino;
789 static void cli_qpathinfo2_done2(struct tevent_req *subreq);
790 static void cli_qpathinfo2_done(struct tevent_req *subreq);
792 struct tevent_req *cli_qpathinfo2_send(TALLOC_CTX *mem_ctx,
793 struct tevent_context *ev,
794 struct cli_state *cli,
795 const char *fname)
797 struct tevent_req *req = NULL, *subreq = NULL;
798 struct cli_qpathinfo2_state *state = NULL;
800 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo2_state);
801 if (req == NULL) {
802 return NULL;
804 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
805 subreq = cli_smb2_qpathinfo_send(state,
807 cli,
808 fname,
809 FSCC_FILE_ALL_INFORMATION,
810 0x60,
811 UINT16_MAX);
812 if (tevent_req_nomem(subreq, req)) {
813 return tevent_req_post(req, ev);
815 tevent_req_set_callback(subreq, cli_qpathinfo2_done2, req);
816 return req;
818 subreq = cli_qpathinfo_send(state, ev, cli, fname,
819 SMB_QUERY_FILE_ALL_INFO,
820 68, CLI_BUFFER_SIZE);
821 if (tevent_req_nomem(subreq, req)) {
822 return tevent_req_post(req, ev);
824 tevent_req_set_callback(subreq, cli_qpathinfo2_done, req);
825 return req;
828 static void cli_qpathinfo2_done2(struct tevent_req *subreq)
830 struct tevent_req *req =
831 tevent_req_callback_data(subreq, struct tevent_req);
832 struct cli_qpathinfo2_state *state =
833 tevent_req_data(req, struct cli_qpathinfo2_state);
834 uint8_t *rdata = NULL;
835 uint32_t num_rdata;
836 NTSTATUS status;
838 status = cli_smb2_qpathinfo_recv(subreq, state, &rdata, &num_rdata);
839 TALLOC_FREE(subreq);
840 if (tevent_req_nterror(req, status)) {
841 return;
843 state->create_time = interpret_long_date(BVAL(rdata, 0x0));
844 state->access_time = interpret_long_date(BVAL(rdata, 0x8));
845 state->write_time = interpret_long_date(BVAL(rdata, 0x10));
846 state->change_time = interpret_long_date(BVAL(rdata, 0x18));
847 state->attr = PULL_LE_U32(rdata, 0x20);
848 state->size = PULL_LE_U64(rdata, 0x30);
849 state->ino = PULL_LE_U64(rdata, 0x40);
851 tevent_req_done(req);
854 static void cli_qpathinfo2_done(struct tevent_req *subreq)
856 struct tevent_req *req = tevent_req_callback_data(
857 subreq, struct tevent_req);
858 struct cli_qpathinfo2_state *state = tevent_req_data(
859 req, struct cli_qpathinfo2_state);
860 uint8_t *data = NULL;
861 uint32_t num_data;
862 NTSTATUS status;
864 status = cli_qpathinfo_recv(subreq, state, &data, &num_data);
865 TALLOC_FREE(subreq);
866 if (tevent_req_nterror(req, status)) {
867 return;
870 state->create_time = interpret_long_date(BVAL(data, 0));
871 state->access_time = interpret_long_date(BVAL(data, 8));
872 state->write_time = interpret_long_date(BVAL(data, 16));
873 state->change_time = interpret_long_date(BVAL(data, 24));
874 state->attr = PULL_LE_U32(data, 32);
875 state->size = PULL_LE_U64(data, 48);
878 * SMB1 qpathinfo2 uses SMB_QUERY_FILE_ALL_INFO which doesn't
879 * return an inode number (fileid). We can't change this to
880 * one of the FILE_ID info levels as only Win2003 and above
881 * support these [MS-SMB: 2.2.2.3.1] and the SMB1 code needs
882 * to support older servers.
884 state->ino = 0;
886 TALLOC_FREE(data);
888 tevent_req_done(req);
891 NTSTATUS cli_qpathinfo2_recv(struct tevent_req *req,
892 struct timespec *create_time,
893 struct timespec *access_time,
894 struct timespec *write_time,
895 struct timespec *change_time,
896 off_t *size, uint32_t *pattr,
897 SMB_INO_T *ino)
899 struct cli_qpathinfo2_state *state = tevent_req_data(
900 req, struct cli_qpathinfo2_state);
901 NTSTATUS status;
903 if (tevent_req_is_nterror(req, &status)) {
904 return status;
907 if (create_time) {
908 *create_time = state->create_time;
910 if (access_time) {
911 *access_time = state->access_time;
913 if (write_time) {
914 *write_time = state->write_time;
916 if (change_time) {
917 *change_time = state->change_time;
919 if (pattr) {
920 *pattr = state->attr;
922 if (size) {
923 *size = state->size;
925 if (ino) {
926 *ino = state->ino;
928 return NT_STATUS_OK;
931 NTSTATUS cli_qpathinfo2(struct cli_state *cli, const char *fname,
932 struct timespec *create_time,
933 struct timespec *access_time,
934 struct timespec *write_time,
935 struct timespec *change_time,
936 off_t *size, uint32_t *pattr,
937 SMB_INO_T *ino)
939 TALLOC_CTX *frame = talloc_stackframe();
940 struct tevent_context *ev = NULL;
941 struct tevent_req *req = NULL;
942 NTSTATUS status = NT_STATUS_NO_MEMORY;
944 if (smbXcli_conn_has_async_calls(cli->conn)) {
946 * Can't use sync call while an async call is in flight
948 status = NT_STATUS_INVALID_PARAMETER;
949 goto fail;
951 ev = samba_tevent_context_init(frame);
952 if (ev == NULL) {
953 goto fail;
955 req = cli_qpathinfo2_send(frame, ev, cli, fname);
956 if (req == NULL) {
957 goto fail;
959 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
960 goto fail;
962 status = cli_qpathinfo2_recv(req, create_time, access_time,
963 write_time, change_time, size, pattr, ino);
964 fail:
965 TALLOC_FREE(frame);
966 return status;
969 /****************************************************************************
970 Get the stream info
971 ****************************************************************************/
973 struct cli_qpathinfo_streams_state {
974 uint32_t num_data;
975 uint8_t *data;
978 static void cli_qpathinfo_streams_done(struct tevent_req *subreq);
979 static void cli_qpathinfo_streams_done2(struct tevent_req *subreq);
981 struct tevent_req *cli_qpathinfo_streams_send(TALLOC_CTX *mem_ctx,
982 struct tevent_context *ev,
983 struct cli_state *cli,
984 const char *fname)
986 struct tevent_req *req = NULL, *subreq = NULL;
987 struct cli_qpathinfo_streams_state *state = NULL;
989 req = tevent_req_create(mem_ctx, &state,
990 struct cli_qpathinfo_streams_state);
991 if (req == NULL) {
992 return NULL;
994 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
995 subreq = cli_smb2_qpathinfo_send(state,
997 cli,
998 fname,
999 FSCC_FILE_STREAM_INFORMATION,
1001 CLI_BUFFER_SIZE);
1002 if (tevent_req_nomem(subreq, req)) {
1003 return tevent_req_post(req, ev);
1005 tevent_req_set_callback(subreq,
1006 cli_qpathinfo_streams_done2,
1007 req);
1008 return req;
1010 subreq = cli_qpathinfo_send(state, ev, cli, fname,
1011 SMB_FILE_STREAM_INFORMATION,
1012 0, CLI_BUFFER_SIZE);
1013 if (tevent_req_nomem(subreq, req)) {
1014 return tevent_req_post(req, ev);
1016 tevent_req_set_callback(subreq, cli_qpathinfo_streams_done, req);
1017 return req;
1020 static void cli_qpathinfo_streams_done(struct tevent_req *subreq)
1022 struct tevent_req *req = tevent_req_callback_data(
1023 subreq, struct tevent_req);
1024 struct cli_qpathinfo_streams_state *state = tevent_req_data(
1025 req, struct cli_qpathinfo_streams_state);
1026 NTSTATUS status;
1028 status = cli_qpathinfo_recv(subreq, state, &state->data,
1029 &state->num_data);
1030 tevent_req_simple_finish_ntstatus(subreq, status);
1033 static void cli_qpathinfo_streams_done2(struct tevent_req *subreq)
1035 struct tevent_req *req =
1036 tevent_req_callback_data(subreq, struct tevent_req);
1037 struct cli_qpathinfo_streams_state *state =
1038 tevent_req_data(req, struct cli_qpathinfo_streams_state);
1039 NTSTATUS status;
1041 status = cli_smb2_qpathinfo_recv(subreq,
1042 state,
1043 &state->data,
1044 &state->num_data);
1045 tevent_req_simple_finish_ntstatus(subreq, status);
1048 NTSTATUS cli_qpathinfo_streams_recv(struct tevent_req *req,
1049 TALLOC_CTX *mem_ctx,
1050 unsigned int *pnum_streams,
1051 struct stream_struct **pstreams)
1053 struct cli_qpathinfo_streams_state *state = tevent_req_data(
1054 req, struct cli_qpathinfo_streams_state);
1055 NTSTATUS status;
1057 if (tevent_req_is_nterror(req, &status)) {
1058 return status;
1060 if (!parse_streams_blob(mem_ctx, state->data, state->num_data,
1061 pnum_streams, pstreams)) {
1062 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1064 return NT_STATUS_OK;
1067 NTSTATUS cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
1068 TALLOC_CTX *mem_ctx,
1069 unsigned int *pnum_streams,
1070 struct stream_struct **pstreams)
1072 TALLOC_CTX *frame = NULL;
1073 struct tevent_context *ev;
1074 struct tevent_req *req;
1075 NTSTATUS status = NT_STATUS_NO_MEMORY;
1077 frame = talloc_stackframe();
1079 if (smbXcli_conn_has_async_calls(cli->conn)) {
1081 * Can't use sync call while an async call is in flight
1083 status = NT_STATUS_INVALID_PARAMETER;
1084 goto fail;
1086 ev = samba_tevent_context_init(frame);
1087 if (ev == NULL) {
1088 goto fail;
1090 req = cli_qpathinfo_streams_send(frame, ev, cli, fname);
1091 if (req == NULL) {
1092 goto fail;
1094 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1095 goto fail;
1097 status = cli_qpathinfo_streams_recv(req, mem_ctx, pnum_streams,
1098 pstreams);
1099 fail:
1100 TALLOC_FREE(frame);
1101 return status;
1104 bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
1105 size_t data_len,
1106 unsigned int *pnum_streams,
1107 struct stream_struct **pstreams)
1109 unsigned int num_streams;
1110 struct stream_struct *streams;
1111 unsigned int ofs;
1113 num_streams = 0;
1114 streams = NULL;
1115 ofs = 0;
1117 while ((data_len > ofs) && (data_len - ofs >= 24)) {
1118 uint32_t nlen, len;
1119 size_t size;
1120 void *vstr;
1121 struct stream_struct *tmp;
1122 uint8_t *tmp_buf;
1124 tmp = talloc_realloc(mem_ctx, streams,
1125 struct stream_struct,
1126 num_streams+1);
1128 if (tmp == NULL) {
1129 goto fail;
1131 streams = tmp;
1133 nlen = IVAL(rdata, ofs + 0x04);
1135 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
1136 rdata, ofs + 0x08);
1137 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
1138 rdata, ofs + 0x10);
1140 if (nlen > data_len - (ofs + 24)) {
1141 goto fail;
1145 * We need to null-terminate src, how do I do this with
1146 * convert_string_talloc??
1149 tmp_buf = talloc_array(streams, uint8_t, nlen+2);
1150 if (tmp_buf == NULL) {
1151 goto fail;
1154 memcpy(tmp_buf, rdata+ofs+24, nlen);
1155 tmp_buf[nlen] = 0;
1156 tmp_buf[nlen+1] = 0;
1158 if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
1159 nlen+2, &vstr, &size))
1161 TALLOC_FREE(tmp_buf);
1162 goto fail;
1165 TALLOC_FREE(tmp_buf);
1166 streams[num_streams].name = (char *)vstr;
1167 num_streams++;
1169 len = IVAL(rdata, ofs);
1170 if (len > data_len - ofs) {
1171 goto fail;
1173 if (len == 0) break;
1174 ofs += len;
1177 *pnum_streams = num_streams;
1178 *pstreams = streams;
1179 return true;
1181 fail:
1182 TALLOC_FREE(streams);
1183 return false;
1186 /****************************************************************************
1187 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1188 ****************************************************************************/
1190 struct cli_qfileinfo_basic_state {
1191 uint32_t attr;
1192 off_t size;
1193 struct timespec create_time;
1194 struct timespec access_time;
1195 struct timespec write_time;
1196 struct timespec change_time;
1197 SMB_INO_T ino;
1200 static void cli_qfileinfo_basic_done(struct tevent_req *subreq);
1201 static void cli_qfileinfo_basic_doneE(struct tevent_req *subreq);
1202 static void cli_qfileinfo_basic_done2(struct tevent_req *subreq);
1204 struct tevent_req *cli_qfileinfo_basic_send(
1205 TALLOC_CTX *mem_ctx,
1206 struct tevent_context *ev,
1207 struct cli_state *cli,
1208 uint16_t fnum)
1210 struct tevent_req *req = NULL, *subreq = NULL;
1211 struct cli_qfileinfo_basic_state *state = NULL;
1213 req = tevent_req_create(
1214 mem_ctx, &state, struct cli_qfileinfo_basic_state);
1215 if (req == NULL) {
1216 return NULL;
1219 if ((smbXcli_conn_protocol(cli->conn) < PROTOCOL_LANMAN2) ||
1220 cli->win95) {
1222 * According to
1223 * https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cifs/3d9d8f3e-dc70-410d-a3fc-6f4a881e8cab
1224 * SMB_COM_TRANSACTION2 used in cli_qfileinfo_send()
1225 * further down was introduced with the LAN Manager
1226 * 1.2 dialect, which we encode as PROTOCOL_LANMAN2.
1228 * The "win95" check was introduced with commit
1229 * 27e5850fd3e1c8 in 1998. Hard to check these days,
1230 * but leave it in.
1232 * Use a lowerlevel fallback in both cases.
1235 subreq = cli_getattrE_send(state, ev, cli, fnum);
1236 if (tevent_req_nomem(subreq, req)) {
1237 return tevent_req_post(req, ev);
1239 tevent_req_set_callback(
1240 subreq, cli_qfileinfo_basic_doneE, req);
1241 return req;
1244 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1245 subreq = cli_smb2_query_info_fnum_send(
1246 state, /* mem_ctx */
1247 ev, /* ev */
1248 cli, /* cli */
1249 fnum, /* fnum */
1250 1, /* in_info_type */
1251 (SMB_FILE_ALL_INFORMATION - 1000), /* in_file_info_class */
1252 0xFFFF, /* in_max_output_length */
1253 NULL, /* in_input_buffer */
1254 0, /* in_additional_info */
1255 0); /* in_flags */
1256 if (tevent_req_nomem(subreq, req)) {
1257 return tevent_req_post(req, ev);
1259 tevent_req_set_callback(
1260 subreq, cli_qfileinfo_basic_done2, req);
1261 return req;
1264 subreq = cli_qfileinfo_send(
1265 state,
1267 cli,
1268 fnum,
1269 SMB_QUERY_FILE_ALL_INFO, /* level */
1270 68, /* min_rdata */
1271 CLI_BUFFER_SIZE); /* max_rdata */
1272 if (tevent_req_nomem(subreq, req)) {
1273 return tevent_req_post(req, ev);
1275 tevent_req_set_callback(subreq, cli_qfileinfo_basic_done, req);
1276 return req;
1279 static void cli_qfileinfo_basic_done(struct tevent_req *subreq)
1281 struct tevent_req *req = tevent_req_callback_data(
1282 subreq, struct tevent_req);
1283 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1284 req, struct cli_qfileinfo_basic_state);
1285 uint8_t *rdata;
1286 uint32_t num_rdata;
1287 NTSTATUS status;
1289 status = cli_qfileinfo_recv(
1290 subreq, state, NULL, &rdata, &num_rdata);
1291 TALLOC_FREE(subreq);
1292 if (tevent_req_nterror(req, status)) {
1293 return;
1296 state->create_time = interpret_long_date(BVAL(rdata, 0));
1297 state->access_time = interpret_long_date(BVAL(rdata, 8));
1298 state->write_time = interpret_long_date(BVAL(rdata, 16));
1299 state->change_time = interpret_long_date(BVAL(rdata, 24));
1300 state->attr = PULL_LE_U32(rdata, 32);
1301 state->size = PULL_LE_U64(rdata,48);
1302 state->ino = PULL_LE_U32(rdata, 64);
1303 TALLOC_FREE(rdata);
1305 tevent_req_done(req);
1308 static void cli_qfileinfo_basic_doneE(struct tevent_req *subreq)
1310 struct tevent_req *req = tevent_req_callback_data(
1311 subreq, struct tevent_req);
1312 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1313 req, struct cli_qfileinfo_basic_state);
1314 NTSTATUS status;
1316 status = cli_getattrE_recv(
1317 subreq,
1318 &state->attr,
1319 &state->size,
1320 &state->change_time.tv_sec,
1321 &state->access_time.tv_sec,
1322 &state->write_time.tv_sec);
1323 TALLOC_FREE(subreq);
1324 if (tevent_req_nterror(req, status)) {
1325 return;
1327 tevent_req_done(req);
1330 static void cli_qfileinfo_basic_done2(struct tevent_req *subreq)
1332 struct tevent_req *req = tevent_req_callback_data(
1333 subreq, struct tevent_req);
1334 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1335 req, struct cli_qfileinfo_basic_state);
1336 DATA_BLOB outbuf = {0};
1337 NTSTATUS status;
1339 status = cli_smb2_query_info_fnum_recv(subreq, state, &outbuf);
1340 TALLOC_FREE(subreq);
1341 if (tevent_req_nterror(req, status)) {
1342 return;
1345 /* Parse the reply. */
1346 if (outbuf.length < 0x60) {
1347 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1348 return;
1351 state->create_time = interpret_long_date(BVAL(outbuf.data, 0x0));
1352 state->access_time = interpret_long_date(BVAL(outbuf.data, 0x8));
1353 state->write_time = interpret_long_date(BVAL(outbuf.data, 0x10));
1354 state->change_time = interpret_long_date(BVAL(outbuf.data, 0x18));
1355 state->attr = IVAL(outbuf.data, 0x20);
1356 state->size = BVAL(outbuf.data, 0x30);
1357 state->ino = BVAL(outbuf.data, 0x40);
1359 data_blob_free(&outbuf);
1361 tevent_req_done(req);
1364 NTSTATUS cli_qfileinfo_basic_recv(
1365 struct tevent_req *req,
1366 uint32_t *attr,
1367 off_t *size,
1368 struct timespec *create_time,
1369 struct timespec *access_time,
1370 struct timespec *write_time,
1371 struct timespec *change_time,
1372 SMB_INO_T *ino)
1374 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1375 req, struct cli_qfileinfo_basic_state);
1376 NTSTATUS status;
1378 if (tevent_req_is_nterror(req, &status)) {
1379 return status;
1382 if (create_time != NULL) {
1383 *create_time = state->create_time;
1385 if (access_time != NULL) {
1386 *access_time = state->access_time;
1388 if (write_time != NULL) {
1389 *write_time = state->write_time;
1391 if (change_time != NULL) {
1392 *change_time = state->change_time;
1394 if (attr != NULL) {
1395 *attr = state->attr;
1397 if (size != NULL) {
1398 *size = state->size;
1400 if (ino) {
1401 *ino = state->ino;
1404 return NT_STATUS_OK;
1406 /****************************************************************************
1407 Send a qfileinfo call.
1408 ****************************************************************************/
1410 NTSTATUS cli_qfileinfo_basic(
1411 struct cli_state *cli,
1412 uint16_t fnum,
1413 uint32_t *attr,
1414 off_t *size,
1415 struct timespec *create_time,
1416 struct timespec *access_time,
1417 struct timespec *write_time,
1418 struct timespec *change_time,
1419 SMB_INO_T *ino)
1421 TALLOC_CTX *frame = NULL;
1422 struct tevent_context *ev = NULL;
1423 struct tevent_req *req = NULL;
1424 NTSTATUS status = NT_STATUS_NO_MEMORY;
1426 frame = talloc_stackframe();
1428 if (smbXcli_conn_has_async_calls(cli->conn)) {
1430 * Can't use sync call while an async call is in flight
1432 status = NT_STATUS_INVALID_PARAMETER;
1433 goto fail;
1435 ev = samba_tevent_context_init(frame);
1436 if (ev == NULL) {
1437 goto fail;
1439 req = cli_qfileinfo_basic_send(frame, ev, cli, fnum);
1440 if (req == NULL) {
1441 goto fail;
1443 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1444 goto fail;
1447 status = cli_qfileinfo_basic_recv(
1448 req,
1449 attr,
1450 size,
1451 create_time,
1452 access_time,
1453 write_time,
1454 change_time,
1455 ino);
1457 /* cli_smb2_query_info_fnum_recv doesn't set this */
1458 cli->raw_status = status;
1459 fail:
1460 TALLOC_FREE(frame);
1461 return status;
1464 /****************************************************************************
1465 Send a qpathinfo BASIC_INFO call.
1466 ****************************************************************************/
1468 struct cli_qpathinfo_basic_state {
1469 uint32_t num_data;
1470 uint8_t *data;
1473 static void cli_qpathinfo_basic_done(struct tevent_req *subreq);
1475 struct tevent_req *cli_qpathinfo_basic_send(TALLOC_CTX *mem_ctx,
1476 struct tevent_context *ev,
1477 struct cli_state *cli,
1478 const char *fname)
1480 struct tevent_req *req = NULL, *subreq = NULL;
1481 struct cli_qpathinfo_basic_state *state = NULL;
1483 req = tevent_req_create(mem_ctx, &state,
1484 struct cli_qpathinfo_basic_state);
1485 if (req == NULL) {
1486 return NULL;
1488 subreq = cli_qpathinfo_send(state, ev, cli, fname,
1489 SMB_QUERY_FILE_BASIC_INFO,
1490 36, CLI_BUFFER_SIZE);
1491 if (tevent_req_nomem(subreq, req)) {
1492 return tevent_req_post(req, ev);
1494 tevent_req_set_callback(subreq, cli_qpathinfo_basic_done, req);
1495 return req;
1498 static void cli_qpathinfo_basic_done(struct tevent_req *subreq)
1500 struct tevent_req *req = tevent_req_callback_data(
1501 subreq, struct tevent_req);
1502 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1503 req, struct cli_qpathinfo_basic_state);
1504 NTSTATUS status;
1506 status = cli_qpathinfo_recv(subreq, state, &state->data,
1507 &state->num_data);
1508 TALLOC_FREE(subreq);
1509 if (tevent_req_nterror(req, status)) {
1510 return;
1512 tevent_req_done(req);
1515 NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
1516 SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
1518 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1519 req, struct cli_qpathinfo_basic_state);
1520 NTSTATUS status;
1522 if (tevent_req_is_nterror(req, &status)) {
1523 return status;
1526 sbuf->st_ex_btime = interpret_long_date(BVAL(state->data, 0));
1527 sbuf->st_ex_atime = interpret_long_date(BVAL(state->data, 8));
1528 sbuf->st_ex_mtime = interpret_long_date(BVAL(state->data, 16));
1529 sbuf->st_ex_ctime = interpret_long_date(BVAL(state->data, 24));
1530 *attributes = IVAL(state->data, 32);
1531 return NT_STATUS_OK;
1534 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
1535 SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
1537 TALLOC_CTX *frame = NULL;
1538 struct tevent_context *ev;
1539 struct tevent_req *req;
1540 NTSTATUS status = NT_STATUS_NO_MEMORY;
1542 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1543 return cli_smb2_qpathinfo_basic(cli,
1544 name,
1545 sbuf,
1546 attributes);
1549 frame = talloc_stackframe();
1551 if (smbXcli_conn_has_async_calls(cli->conn)) {
1553 * Can't use sync call while an async call is in flight
1555 status = NT_STATUS_INVALID_PARAMETER;
1556 goto fail;
1558 ev = samba_tevent_context_init(frame);
1559 if (ev == NULL) {
1560 goto fail;
1562 req = cli_qpathinfo_basic_send(frame, ev, cli, name);
1563 if (req == NULL) {
1564 goto fail;
1566 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1567 goto fail;
1569 status = cli_qpathinfo_basic_recv(req, sbuf, attributes);
1570 fail:
1571 TALLOC_FREE(frame);
1572 return status;
1575 /****************************************************************************
1576 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1577 ****************************************************************************/
1579 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1581 uint8_t *rdata;
1582 uint32_t num_rdata;
1583 unsigned int len;
1584 char *converted = NULL;
1585 size_t converted_size = 0;
1586 NTSTATUS status;
1588 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1589 return cli_smb2_qpathinfo_alt_name(cli,
1590 fname,
1591 alt_name);
1594 status = cli_qpathinfo(talloc_tos(), cli, fname,
1595 SMB_QUERY_FILE_ALT_NAME_INFO,
1596 4, CLI_BUFFER_SIZE, &rdata, &num_rdata);
1597 if (!NT_STATUS_IS_OK(status)) {
1598 return status;
1601 len = IVAL(rdata, 0);
1603 if (len > num_rdata - 4) {
1604 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1607 /* The returned data is a pushed string, not raw data. */
1608 if (!convert_string_talloc(talloc_tos(),
1609 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
1610 CH_UNIX,
1611 rdata + 4,
1612 len,
1613 &converted,
1614 &converted_size)) {
1615 return NT_STATUS_NO_MEMORY;
1617 fstrcpy(alt_name, converted);
1619 TALLOC_FREE(converted);
1620 TALLOC_FREE(rdata);
1622 return NT_STATUS_OK;
1625 /****************************************************************************
1626 Send a qpathinfo SMB_QUERY_FILE_STANDARD_INFO call.
1627 ****************************************************************************/
1629 NTSTATUS cli_qpathinfo_standard(struct cli_state *cli, const char *fname,
1630 uint64_t *allocated, uint64_t *size,
1631 uint32_t *nlinks,
1632 bool *is_del_pending, bool *is_dir)
1634 uint8_t *rdata;
1635 uint32_t num_rdata;
1636 NTSTATUS status;
1638 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1639 return NT_STATUS_NOT_IMPLEMENTED;
1642 status = cli_qpathinfo(talloc_tos(), cli, fname,
1643 SMB_QUERY_FILE_STANDARD_INFO,
1644 24, CLI_BUFFER_SIZE, &rdata, &num_rdata);
1645 if (!NT_STATUS_IS_OK(status)) {
1646 return status;
1649 if (allocated) {
1650 *allocated = BVAL(rdata, 0);
1653 if (size) {
1654 *size = BVAL(rdata, 8);
1657 if (nlinks) {
1658 *nlinks = IVAL(rdata, 16);
1661 if (is_del_pending) {
1662 *is_del_pending = CVAL(rdata, 20);
1665 if (is_dir) {
1666 *is_dir = CVAL(rdata, 20);
1669 TALLOC_FREE(rdata);
1671 return NT_STATUS_OK;
1675 /* like cli_qpathinfo2 but do not use SMB_QUERY_FILE_ALL_INFO with smb1 */
1676 NTSTATUS cli_qpathinfo3(struct cli_state *cli, const char *fname,
1677 struct timespec *create_time,
1678 struct timespec *access_time,
1679 struct timespec *write_time,
1680 struct timespec *change_time,
1681 off_t *size, uint32_t *pattr,
1682 SMB_INO_T *ino)
1684 NTSTATUS status = NT_STATUS_OK;
1685 SMB_STRUCT_STAT st = { 0 };
1686 uint32_t attr = 0;
1687 uint64_t pos;
1689 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1691 * NB. cli_qpathinfo2() checks pattr is valid before
1692 * storing a value into it, so we don't need to use
1693 * an intermediate attr variable as below but can
1694 * pass pattr directly.
1696 return cli_qpathinfo2(cli, fname,
1697 create_time, access_time, write_time, change_time,
1698 size, pattr, ino);
1701 if (create_time || access_time || write_time || change_time || pattr) {
1703 * cli_qpathinfo_basic() always indirects the passed
1704 * in pointers so we use intermediate variables to
1705 * collect all of them before assigning any requested
1706 * below.
1708 status = cli_qpathinfo_basic(cli, fname, &st, &attr);
1709 if (!NT_STATUS_IS_OK(status)) {
1710 return status;
1714 if (size) {
1715 status = cli_qpathinfo_standard(cli, fname,
1716 NULL, &pos, NULL, NULL, NULL);
1717 if (!NT_STATUS_IS_OK(status)) {
1718 return status;
1721 *size = pos;
1724 if (create_time) {
1725 *create_time = st.st_ex_btime;
1727 if (access_time) {
1728 *access_time = st.st_ex_atime;
1730 if (write_time) {
1731 *write_time = st.st_ex_mtime;
1733 if (change_time) {
1734 *change_time = st.st_ex_ctime;
1736 if (pattr) {
1737 *pattr = attr;
1739 if (ino) {
1740 *ino = 0;
1743 return NT_STATUS_OK;