2 Unix SMB/CIFS implementation.
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/>.
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"
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 /****************************************************************************
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)
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)) {
70 * I know this memcpy massively hurts, but there are just tons
71 * of callers of cli_api that eventually need changing to
75 *rparam = (char *)smb_memdup(my_rparam, num_my_rparam);
76 if (*rparam == NULL) {
79 *rprcnt = num_my_rparam;
80 TALLOC_FREE(my_rparam);
82 *rdata = (char *)smb_memdup(my_rdata, num_my_rdata);
86 *rdrcnt = num_my_rdata;
87 TALLOC_FREE(my_rdata);
91 TALLOC_FREE(my_rdata);
92 TALLOC_FREE(my_rparam);
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)
109 unsigned int rdrcnt,rprcnt;
115 /* now send a SMBtrans command with api RNetShareEnum */
117 SSVAL(p,0,0); /* api number */
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);
125 * Win2k needs a *smaller* buffer than 0xFFFF here -
126 * it returns "out of server memory" with 0xFFFF !!! JRA.
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 */
138 DEBUG(4,("NetShareEnum failed\n"));
143 DBG_ERR("Got invalid result: rprcnt=%u\n", rprcnt);
147 res = rparam? SVAL(rparam,0) : -1;
149 if (res == 0 || res == ERRmoredata) {
150 int converter=SVAL(rparam,2);
152 char *rdata_end = rdata + rdrcnt;
154 count=SVAL(rparam,4);
157 for (i=0;i<count;i++,p+=20) {
165 TALLOC_CTX *frame = talloc_stackframe();
167 if (p + 20 > rdata_end) {
174 comment_offset = (IVAL(p,16) & 0xFFFF) - converter;
175 if (comment_offset < 0 ||
176 comment_offset > (int)rdrcnt) {
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++)
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);
198 fn(s1, type, s2, state);
203 DEBUG(4,("NetShareEnum res=%d\n", res));
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 *),
227 char *rdata_end = NULL;
228 unsigned int rdrcnt,rprcnt;
233 uint32_t func = RAP_NetServerEnum2;
234 char *last_entry = NULL;
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
247 /* send a SMBtrans command with api NetServerEnum */
249 SIVAL(p,0,func); /* api number */
252 if (func == RAP_NetServerEnum3) {
253 strlcpy(p,"WrLehDzz", sizeof(param)-PTR_DIFF(p,param));
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);
263 SSVAL(p,2,CLI_BUFFER_SIZE);
268 /* If we have more data, tell the server where
273 sizeof(param) - PTR_DIFF(p,param) - 1,
274 STR_TERMINATE|STR_UPPER);
277 SAFE_FREE(last_entry);
282 if (func == RAP_NetServerEnum3) {
284 last_entry ? last_entry : "",
285 sizeof(param) - PTR_DIFF(p,param) - 1,
289 SAFE_FREE(last_entry);
295 /* Next time through we need to use the continue api */
296 func = RAP_NetServerEnum3;
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 */
309 rdata_end = rdata + rdrcnt;
312 DBG_ERR("Got invalid result: rprcnt=%u\n", rprcnt);
317 res = rparam ? SVAL(rparam,0) : -1;
319 if (res == 0 || res == ERRmoredata ||
320 (res != -1 && cli_errno(cli) == 0)) {
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
333 if (total_cnt == 0) {
334 total_cnt = SVAL(rparam, 6);
337 /* Keep track of how many we have read */
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) {
365 TALLOC_CTX *frame = talloc_stackframe();
366 uint32_t entry_stype;
368 if (p + 26 > rdata_end) {
374 comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
375 cmnt = comment_offset?(rdata+comment_offset):"";
377 if (comment_offset < 0 || comment_offset >= (int)rdrcnt) {
382 /* Work out the comment length. */
383 for (p1 = cmnt, len = 0; *p1 &&
384 p1 < rdata_end; 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);
402 fn(s1, entry_stype, s2, state);
406 /* We are done with the old last entry, so now we can free it */
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 */
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)) {
426 } while ((res == ERRmoredata) && (total_cnt > return_cnt));
430 SAFE_FREE(last_entry);
433 errno = cli_errno(cli);
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
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)
454 unsigned char data[532];
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;
462 unsigned int rprcnt, rdrcnt;
463 gnutls_cipher_hd_t cipher_hnd = NULL;
464 gnutls_datum_t old_pw_key = {
466 .size = sizeof(old_pw_hash),
470 if (strlen(user) >= sizeof(fstring)-1) {
471 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
475 SSVAL(p,0,214); /* SamOEMChangePassword command. */
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);
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);
500 rc = gnutls_cipher_init(&cipher_hnd,
501 GNUTLS_CIPHER_ARCFOUR_128,
505 DBG_ERR("gnutls_cipher_init failed: %s\n",
506 gnutls_strerror(rc));
509 rc = gnutls_cipher_encrypt(cipher_hnd,
512 gnutls_cipher_deinit(cipher_hnd);
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]);
524 DBG_ERR("E_old_pw_hash failed: %s\n", gnutls_strerror(rc));
531 param, param_len, 4, /* param, length, max */
532 (char *)data, data_len, 0, /* data, length, max */
535 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
541 cli->rap_error = ERRbadformat;
546 cli->rap_error = SVAL(rparam,0);
553 return (cli->rap_error == 0);
556 static void prep_basic_information_buf(
558 struct timespec create_time,
559 struct timespec access_time,
560 struct timespec write_time,
561 struct timespec change_time,
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);
572 put_long_date_full_timespec(
573 TIMESTAMP_SET_NT_OR_BETTER, p, &access_time);
576 put_long_date_full_timespec(
577 TIMESTAMP_SET_NT_OR_BETTER, p, &write_time);
580 put_long_date_full_timespec(
581 TIMESTAMP_SET_NT_OR_BETTER, p, &change_time);
584 if (attr == (uint32_t)-1 || attr == FILE_ATTRIBUTE_NORMAL) {
587 } else if (attr == 0) {
588 /* Clear all existing attributes. */
589 attr = FILE_ATTRIBUTE_NORMAL;
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,
613 prep_basic_information_buf(
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,
629 1, /* SMB2_SETINFO_FILE */
630 SMB_FILE_BASIC_INFORMATION - 1000,
634 return cli_setpathinfo(
635 cli, SMB_FILE_BASIC_INFORMATION, fname, buf, sizeof(buf));
638 struct cli_setfileinfo_ext_state {
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(
648 struct tevent_context *ev,
649 struct cli_state *cli,
651 struct timespec create_time,
652 struct timespec access_time,
653 struct timespec write_time,
654 struct timespec change_time,
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);
665 prep_basic_information_buf(
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(
684 SMB_FILE_BASIC_INFORMATION - 1000,
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);
695 subreq = cli_setfileinfo_send(
700 SMB_FILE_BASIC_INFORMATION,
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);
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,
730 struct timespec create_time,
731 struct timespec access_time,
732 struct timespec write_time,
733 struct timespec change_time,
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);
754 req = cli_setfileinfo_ext_send(
767 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
770 status = cli_setfileinfo_ext_recv(req);
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;
784 struct timespec create_time;
785 struct timespec access_time;
786 struct timespec write_time;
787 struct timespec change_time;
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,
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);
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,
821 FSCC_FILE_ALL_INFORMATION,
824 if (tevent_req_nomem(subreq, req)) {
825 return tevent_req_post(req, ev);
827 tevent_req_set_callback(subreq, cli_qpathinfo2_done2, 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);
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;
850 status = cli_smb2_qpathinfo_recv(subreq, state, &rdata, &num_rdata);
852 if (tevent_req_nterror(req, status)) {
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,
868 if (tevent_req_nomem(subreq, req)) {
871 tevent_req_set_callback(subreq,
872 cli_qpathinfo2_got_reparse,
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;
890 status = cli_qpathinfo_recv(subreq, state, &data, &num_data);
892 if (tevent_req_nterror(req, status)) {
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.
914 if (state->attr & FILE_ATTRIBUTE_REPARSE_POINT) {
915 subreq = cli_get_reparse_data_send(state,
919 if (tevent_req_nomem(subreq, req)) {
922 tevent_req_set_callback(subreq,
923 cli_qpathinfo2_got_reparse,
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;
939 struct reparse_data_buffer reparse = {
944 status = cli_get_reparse_data_recv(subreq, state, &data, &num_data);
946 if (tevent_req_nterror(req, status)) {
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");
956 switch (reparse.tag) {
957 case IO_REPARSE_TAG_SYMLINK:
958 state->mode = S_IFLNK;
960 case IO_REPARSE_TAG_NFS:
961 switch (reparse.parsed.nfs.type) {
962 case NFS_SPECFILE_LNK:
963 state->mode = S_IFLNK;
965 case NFS_SPECFILE_CHR:
966 state->mode = S_IFCHR;
968 case NFS_SPECFILE_BLK:
969 state->mode = S_IFBLK;
971 case NFS_SPECFILE_FIFO:
972 state->mode = S_IFIFO;
974 case NFS_SPECFILE_SOCK:
975 state->mode = S_IFSOCK;
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,
994 struct cli_qpathinfo2_state *state = tevent_req_data(
995 req, struct cli_qpathinfo2_state);
998 if (tevent_req_is_nterror(req, &status)) {
1003 *create_time = state->create_time;
1006 *access_time = state->access_time;
1009 *write_time = state->write_time;
1012 *change_time = state->change_time;
1015 *pattr = state->attr;
1018 *size = state->size;
1024 *mode = state->mode;
1026 return NT_STATUS_OK;
1029 NTSTATUS cli_qpathinfo2(struct cli_state *cli,
1031 struct timespec *create_time,
1032 struct timespec *access_time,
1033 struct timespec *write_time,
1034 struct timespec *change_time,
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;
1052 ev = samba_tevent_context_init(frame);
1056 req = cli_qpathinfo2_send(frame, ev, cli, fname);
1060 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1063 status = cli_qpathinfo2_recv(req,
1077 /****************************************************************************
1079 ****************************************************************************/
1081 struct cli_qpathinfo_streams_state {
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,
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);
1102 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1103 subreq = cli_smb2_qpathinfo_send(state,
1107 FSCC_FILE_STREAM_INFORMATION,
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,
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);
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);
1136 status = cli_qpathinfo_recv(subreq, state, &state->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);
1149 status = cli_smb2_qpathinfo_recv(subreq,
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);
1165 if (tevent_req_is_nterror(req, &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;
1194 ev = samba_tevent_context_init(frame);
1198 req = cli_qpathinfo_streams_send(frame, ev, cli, fname);
1202 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1205 status = cli_qpathinfo_streams_recv(req, mem_ctx, pnum_streams,
1212 bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
1214 unsigned int *pnum_streams,
1215 struct stream_struct **pstreams)
1217 unsigned int num_streams;
1218 struct stream_struct *streams;
1225 while ((data_len > ofs) && (data_len - ofs >= 24)) {
1229 struct stream_struct *tmp;
1232 tmp = talloc_realloc(mem_ctx, streams,
1233 struct stream_struct,
1241 nlen = IVAL(rdata, ofs + 0x04);
1243 streams[num_streams].size = IVAL_TO_SMB_OFF_T(
1245 streams[num_streams].alloc_size = IVAL_TO_SMB_OFF_T(
1248 if (nlen > data_len - (ofs + 24)) {
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) {
1262 memcpy(tmp_buf, rdata+ofs+24, nlen);
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);
1273 TALLOC_FREE(tmp_buf);
1274 streams[num_streams].name = (char *)vstr;
1277 len = IVAL(rdata, ofs);
1278 if (len > data_len - ofs) {
1281 if (len == 0) break;
1285 *pnum_streams = num_streams;
1286 *pstreams = streams;
1290 TALLOC_FREE(streams);
1294 /****************************************************************************
1295 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1296 ****************************************************************************/
1298 struct cli_qfileinfo_basic_state {
1301 struct timespec create_time;
1302 struct timespec access_time;
1303 struct timespec write_time;
1304 struct timespec change_time;
1308 static void cli_qfileinfo_basic_done(struct tevent_req *subreq);
1309 static void cli_qfileinfo_basic_doneE(struct tevent_req *subreq);
1310 static void cli_qfileinfo_basic_done2(struct tevent_req *subreq);
1312 struct tevent_req *cli_qfileinfo_basic_send(
1313 TALLOC_CTX *mem_ctx,
1314 struct tevent_context *ev,
1315 struct cli_state *cli,
1318 struct tevent_req *req = NULL, *subreq = NULL;
1319 struct cli_qfileinfo_basic_state *state = NULL;
1321 req = tevent_req_create(
1322 mem_ctx, &state, struct cli_qfileinfo_basic_state);
1327 if ((smbXcli_conn_protocol(cli->conn) < PROTOCOL_LANMAN2) ||
1331 * https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cifs/3d9d8f3e-dc70-410d-a3fc-6f4a881e8cab
1332 * SMB_COM_TRANSACTION2 used in cli_qfileinfo_send()
1333 * further down was introduced with the LAN Manager
1334 * 1.2 dialect, which we encode as PROTOCOL_LANMAN2.
1336 * The "win95" check was introduced with commit
1337 * 27e5850fd3e1c8 in 1998. Hard to check these days,
1340 * Use a lowerlevel fallback in both cases.
1343 subreq = cli_getattrE_send(state, ev, cli, fnum);
1344 if (tevent_req_nomem(subreq, req)) {
1345 return tevent_req_post(req, ev);
1347 tevent_req_set_callback(
1348 subreq, cli_qfileinfo_basic_doneE, req);
1352 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1353 subreq = cli_smb2_query_info_fnum_send(
1354 state, /* mem_ctx */
1358 1, /* in_info_type */
1359 (SMB_FILE_ALL_INFORMATION - 1000), /* in_file_info_class */
1360 0xFFFF, /* in_max_output_length */
1361 NULL, /* in_input_buffer */
1362 0, /* in_additional_info */
1364 if (tevent_req_nomem(subreq, req)) {
1365 return tevent_req_post(req, ev);
1367 tevent_req_set_callback(
1368 subreq, cli_qfileinfo_basic_done2, req);
1372 subreq = cli_qfileinfo_send(
1377 SMB_QUERY_FILE_ALL_INFO, /* level */
1379 CLI_BUFFER_SIZE); /* max_rdata */
1380 if (tevent_req_nomem(subreq, req)) {
1381 return tevent_req_post(req, ev);
1383 tevent_req_set_callback(subreq, cli_qfileinfo_basic_done, req);
1387 static void cli_qfileinfo_basic_done(struct tevent_req *subreq)
1389 struct tevent_req *req = tevent_req_callback_data(
1390 subreq, struct tevent_req);
1391 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1392 req, struct cli_qfileinfo_basic_state);
1397 status = cli_qfileinfo_recv(
1398 subreq, state, NULL, &rdata, &num_rdata);
1399 TALLOC_FREE(subreq);
1400 if (tevent_req_nterror(req, status)) {
1404 state->create_time = interpret_long_date(BVAL(rdata, 0));
1405 state->access_time = interpret_long_date(BVAL(rdata, 8));
1406 state->write_time = interpret_long_date(BVAL(rdata, 16));
1407 state->change_time = interpret_long_date(BVAL(rdata, 24));
1408 state->attr = PULL_LE_U32(rdata, 32);
1409 state->size = PULL_LE_U64(rdata,48);
1410 state->ino = PULL_LE_U32(rdata, 64);
1413 tevent_req_done(req);
1416 static void cli_qfileinfo_basic_doneE(struct tevent_req *subreq)
1418 struct tevent_req *req = tevent_req_callback_data(
1419 subreq, struct tevent_req);
1420 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1421 req, struct cli_qfileinfo_basic_state);
1424 status = cli_getattrE_recv(
1428 &state->change_time.tv_sec,
1429 &state->access_time.tv_sec,
1430 &state->write_time.tv_sec);
1431 TALLOC_FREE(subreq);
1432 if (tevent_req_nterror(req, status)) {
1435 tevent_req_done(req);
1438 static void cli_qfileinfo_basic_done2(struct tevent_req *subreq)
1440 struct tevent_req *req = tevent_req_callback_data(
1441 subreq, struct tevent_req);
1442 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1443 req, struct cli_qfileinfo_basic_state);
1444 DATA_BLOB outbuf = {0};
1447 status = cli_smb2_query_info_fnum_recv(subreq, state, &outbuf);
1448 TALLOC_FREE(subreq);
1449 if (tevent_req_nterror(req, status)) {
1453 /* Parse the reply. */
1454 if (outbuf.length < 0x60) {
1455 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1459 state->create_time = interpret_long_date(BVAL(outbuf.data, 0x0));
1460 state->access_time = interpret_long_date(BVAL(outbuf.data, 0x8));
1461 state->write_time = interpret_long_date(BVAL(outbuf.data, 0x10));
1462 state->change_time = interpret_long_date(BVAL(outbuf.data, 0x18));
1463 state->attr = IVAL(outbuf.data, 0x20);
1464 state->size = BVAL(outbuf.data, 0x30);
1465 state->ino = BVAL(outbuf.data, 0x40);
1467 data_blob_free(&outbuf);
1469 tevent_req_done(req);
1472 NTSTATUS cli_qfileinfo_basic_recv(
1473 struct tevent_req *req,
1476 struct timespec *create_time,
1477 struct timespec *access_time,
1478 struct timespec *write_time,
1479 struct timespec *change_time,
1482 struct cli_qfileinfo_basic_state *state = tevent_req_data(
1483 req, struct cli_qfileinfo_basic_state);
1486 if (tevent_req_is_nterror(req, &status)) {
1490 if (create_time != NULL) {
1491 *create_time = state->create_time;
1493 if (access_time != NULL) {
1494 *access_time = state->access_time;
1496 if (write_time != NULL) {
1497 *write_time = state->write_time;
1499 if (change_time != NULL) {
1500 *change_time = state->change_time;
1503 *attr = state->attr;
1506 *size = state->size;
1512 return NT_STATUS_OK;
1514 /****************************************************************************
1515 Send a qfileinfo call.
1516 ****************************************************************************/
1518 NTSTATUS cli_qfileinfo_basic(
1519 struct cli_state *cli,
1523 struct timespec *create_time,
1524 struct timespec *access_time,
1525 struct timespec *write_time,
1526 struct timespec *change_time,
1529 TALLOC_CTX *frame = NULL;
1530 struct tevent_context *ev = NULL;
1531 struct tevent_req *req = NULL;
1532 NTSTATUS status = NT_STATUS_NO_MEMORY;
1534 frame = talloc_stackframe();
1536 if (smbXcli_conn_has_async_calls(cli->conn)) {
1538 * Can't use sync call while an async call is in flight
1540 status = NT_STATUS_INVALID_PARAMETER;
1543 ev = samba_tevent_context_init(frame);
1547 req = cli_qfileinfo_basic_send(frame, ev, cli, fnum);
1551 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1555 status = cli_qfileinfo_basic_recv(
1565 /* cli_smb2_query_info_fnum_recv doesn't set this */
1566 cli->raw_status = status;
1572 /****************************************************************************
1573 Send a qpathinfo BASIC_INFO call.
1574 ****************************************************************************/
1576 struct cli_qpathinfo_basic_state {
1581 static void cli_qpathinfo_basic_done(struct tevent_req *subreq);
1583 struct tevent_req *cli_qpathinfo_basic_send(TALLOC_CTX *mem_ctx,
1584 struct tevent_context *ev,
1585 struct cli_state *cli,
1588 struct tevent_req *req = NULL, *subreq = NULL;
1589 struct cli_qpathinfo_basic_state *state = NULL;
1591 req = tevent_req_create(mem_ctx, &state,
1592 struct cli_qpathinfo_basic_state);
1596 subreq = cli_qpathinfo_send(state, ev, cli, fname,
1597 SMB_QUERY_FILE_BASIC_INFO,
1598 36, CLI_BUFFER_SIZE);
1599 if (tevent_req_nomem(subreq, req)) {
1600 return tevent_req_post(req, ev);
1602 tevent_req_set_callback(subreq, cli_qpathinfo_basic_done, req);
1606 static void cli_qpathinfo_basic_done(struct tevent_req *subreq)
1608 struct tevent_req *req = tevent_req_callback_data(
1609 subreq, struct tevent_req);
1610 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1611 req, struct cli_qpathinfo_basic_state);
1614 status = cli_qpathinfo_recv(subreq, state, &state->data,
1616 TALLOC_FREE(subreq);
1617 if (tevent_req_nterror(req, status)) {
1620 tevent_req_done(req);
1623 NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
1624 SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
1626 struct cli_qpathinfo_basic_state *state = tevent_req_data(
1627 req, struct cli_qpathinfo_basic_state);
1630 if (tevent_req_is_nterror(req, &status)) {
1634 sbuf->st_ex_btime = interpret_long_date(BVAL(state->data, 0));
1635 sbuf->st_ex_atime = interpret_long_date(BVAL(state->data, 8));
1636 sbuf->st_ex_mtime = interpret_long_date(BVAL(state->data, 16));
1637 sbuf->st_ex_ctime = interpret_long_date(BVAL(state->data, 24));
1638 *attributes = IVAL(state->data, 32);
1639 return NT_STATUS_OK;
1642 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
1643 SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
1645 TALLOC_CTX *frame = NULL;
1646 struct tevent_context *ev;
1647 struct tevent_req *req;
1648 NTSTATUS status = NT_STATUS_NO_MEMORY;
1650 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1651 return cli_smb2_qpathinfo_basic(cli,
1657 frame = talloc_stackframe();
1659 if (smbXcli_conn_has_async_calls(cli->conn)) {
1661 * Can't use sync call while an async call is in flight
1663 status = NT_STATUS_INVALID_PARAMETER;
1666 ev = samba_tevent_context_init(frame);
1670 req = cli_qpathinfo_basic_send(frame, ev, cli, name);
1674 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1677 status = cli_qpathinfo_basic_recv(req, sbuf, attributes);
1683 /****************************************************************************
1684 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1685 ****************************************************************************/
1687 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
1692 char *converted = NULL;
1693 size_t converted_size = 0;
1696 status = cli_qpathinfo(talloc_tos(), cli, fname,
1697 SMB_QUERY_FILE_ALT_NAME_INFO,
1698 4, CLI_BUFFER_SIZE, &rdata, &num_rdata);
1699 if (!NT_STATUS_IS_OK(status)) {
1703 len = IVAL(rdata, 0);
1705 if (len > num_rdata - 4) {
1706 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1709 /* The returned data is a pushed string, not raw data. */
1710 if (!convert_string_talloc(talloc_tos(),
1711 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
1717 return NT_STATUS_NO_MEMORY;
1719 fstrcpy(alt_name, converted);
1721 TALLOC_FREE(converted);
1724 return NT_STATUS_OK;
1727 /****************************************************************************
1728 Send a qpathinfo SMB_QUERY_FILE_STANDARD_INFO call.
1729 ****************************************************************************/
1731 NTSTATUS cli_qpathinfo_standard(struct cli_state *cli, const char *fname,
1732 uint64_t *allocated, uint64_t *size,
1734 bool *is_del_pending, bool *is_dir)
1740 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1741 return NT_STATUS_NOT_IMPLEMENTED;
1744 status = cli_qpathinfo(talloc_tos(), cli, fname,
1745 SMB_QUERY_FILE_STANDARD_INFO,
1746 24, CLI_BUFFER_SIZE, &rdata, &num_rdata);
1747 if (!NT_STATUS_IS_OK(status)) {
1752 *allocated = BVAL(rdata, 0);
1756 *size = BVAL(rdata, 8);
1760 *nlinks = IVAL(rdata, 16);
1763 if (is_del_pending) {
1764 *is_del_pending = CVAL(rdata, 20);
1768 *is_dir = CVAL(rdata, 20);
1773 return NT_STATUS_OK;
1777 /* like cli_qpathinfo2 but do not use SMB_QUERY_FILE_ALL_INFO with smb1 */
1778 NTSTATUS cli_qpathinfo3(struct cli_state *cli, const char *fname,
1779 struct timespec *create_time,
1780 struct timespec *access_time,
1781 struct timespec *write_time,
1782 struct timespec *change_time,
1783 off_t *size, uint32_t *pattr,
1786 NTSTATUS status = NT_STATUS_OK;
1787 SMB_STRUCT_STAT st = { 0 };
1791 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1793 * NB. cli_qpathinfo2() checks pattr is valid before
1794 * storing a value into it, so we don't need to use
1795 * an intermediate attr variable as below but can
1796 * pass pattr directly.
1798 return cli_qpathinfo2(cli,
1810 if (create_time || access_time || write_time || change_time || pattr) {
1812 * cli_qpathinfo_basic() always indirects the passed
1813 * in pointers so we use intermediate variables to
1814 * collect all of them before assigning any requested
1817 status = cli_qpathinfo_basic(cli, fname, &st, &attr);
1818 if (!NT_STATUS_IS_OK(status)) {
1824 status = cli_qpathinfo_standard(cli, fname,
1825 NULL, &pos, NULL, NULL, NULL);
1826 if (!NT_STATUS_IS_OK(status)) {
1834 *create_time = st.st_ex_btime;
1837 *access_time = st.st_ex_atime;
1840 *write_time = st.st_ex_mtime;
1843 *change_time = st.st_ex_ctime;
1852 return NT_STATUS_OK;