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
,
630 FSCC_FILE_BASIC_INFORMATION
,
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 FSCC_FILE_BASIC_INFORMATION
,
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
);
1311 struct tevent_req
*cli_qfileinfo_basic_send(
1312 TALLOC_CTX
*mem_ctx
,
1313 struct tevent_context
*ev
,
1314 struct cli_state
*cli
,
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
);
1326 if ((smbXcli_conn_protocol(cli
->conn
) < PROTOCOL_LANMAN2
) ||
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,
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
);
1351 subreq
= cli_qfileinfo_send(state
,
1355 FSCC_FILE_ALL_INFORMATION
, /* level */
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
);
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
);
1375 status
= cli_qfileinfo_recv(
1376 subreq
, state
, NULL
, &rdata
, &num_rdata
);
1377 TALLOC_FREE(subreq
);
1378 if (tevent_req_nterror(req
, status
)) {
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);
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
);
1402 status
= cli_getattrE_recv(
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
)) {
1413 tevent_req_done(req
);
1416 NTSTATUS
cli_qfileinfo_basic_recv(
1417 struct tevent_req
*req
,
1420 struct timespec
*create_time
,
1421 struct timespec
*access_time
,
1422 struct timespec
*write_time
,
1423 struct timespec
*change_time
,
1426 struct cli_qfileinfo_basic_state
*state
= tevent_req_data(
1427 req
, struct cli_qfileinfo_basic_state
);
1430 if (tevent_req_is_nterror(req
, &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
;
1447 *attr
= state
->attr
;
1450 *size
= state
->size
;
1456 return NT_STATUS_OK
;
1458 /****************************************************************************
1459 Send a qfileinfo call.
1460 ****************************************************************************/
1462 NTSTATUS
cli_qfileinfo_basic(
1463 struct cli_state
*cli
,
1467 struct timespec
*create_time
,
1468 struct timespec
*access_time
,
1469 struct timespec
*write_time
,
1470 struct timespec
*change_time
,
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
;
1487 ev
= samba_tevent_context_init(frame
);
1491 req
= cli_qfileinfo_basic_send(frame
, ev
, cli
, fnum
);
1495 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1499 status
= cli_qfileinfo_basic_recv(
1509 /* cli_smb2_query_info_fnum_recv doesn't set this */
1510 cli
->raw_status
= status
;
1516 /****************************************************************************
1517 Send a qpathinfo BASIC_INFO call.
1518 ****************************************************************************/
1520 struct cli_qpathinfo_basic_state
{
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
,
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
);
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
);
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
);
1558 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
1560 TALLOC_FREE(subreq
);
1561 if (tevent_req_nterror(req
, status
)) {
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
);
1574 if (tevent_req_is_nterror(req
, &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
,
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
;
1610 ev
= samba_tevent_context_init(frame
);
1614 req
= cli_qpathinfo_basic_send(frame
, ev
, cli
, name
);
1618 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1621 status
= cli_qpathinfo_basic_recv(req
, sbuf
, attributes
);
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
)
1636 char *converted
= NULL
;
1637 size_t converted_size
= 0;
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
)) {
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
,
1661 return NT_STATUS_NO_MEMORY
;
1663 fstrcpy(alt_name
, converted
);
1665 TALLOC_FREE(converted
);
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
,
1678 bool *is_del_pending
, bool *is_dir
)
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
)) {
1696 *allocated
= BVAL(rdata
, 0);
1700 *size
= BVAL(rdata
, 8);
1704 *nlinks
= IVAL(rdata
, 16);
1707 if (is_del_pending
) {
1708 *is_del_pending
= CVAL(rdata
, 20);
1712 *is_dir
= CVAL(rdata
, 20);
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
,
1730 NTSTATUS status
= NT_STATUS_OK
;
1731 SMB_STRUCT_STAT st
= { 0 };
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
,
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
1761 status
= cli_qpathinfo_basic(cli
, fname
, &st
, &attr
);
1762 if (!NT_STATUS_IS_OK(status
)) {
1768 status
= cli_qpathinfo_standard(cli
, fname
,
1769 NULL
, &pos
, NULL
, NULL
, NULL
);
1770 if (!NT_STATUS_IS_OK(status
)) {
1778 *create_time
= st
.st_ex_btime
;
1781 *access_time
= st
.st_ex_atime
;
1784 *write_time
= st
.st_ex_mtime
;
1787 *change_time
= st
.st_ex_ctime
;
1796 return NT_STATUS_OK
;