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/crypto/arcfour.h"
26 #include "../lib/util/tevent_ntstatus.h"
27 #include "async_smb.h"
28 #include "libsmb/libsmb.h"
29 #include "libsmb/clirap.h"
31 #include "../libcli/smb/smbXcli_base.h"
32 #include "cli_smb2_fnum.h"
34 #define PIPE_LANMAN "\\PIPE\\LANMAN"
36 /****************************************************************************
38 ****************************************************************************/
40 bool cli_api(struct cli_state
*cli
,
41 char *param
, int prcnt
, int mprcnt
,
42 char *data
, int drcnt
, int mdrcnt
,
43 char **rparam
, unsigned int *rprcnt
,
44 char **rdata
, unsigned int *rdrcnt
)
48 uint8_t *my_rparam
, *my_rdata
;
49 uint32_t num_my_rparam
, num_my_rdata
;
51 status
= cli_trans(talloc_tos(), cli
, SMBtrans
,
52 PIPE_LANMAN
, 0, /* name, fid */
53 0, 0, /* function, flags */
54 NULL
, 0, 0, /* setup */
55 (uint8_t *)param
, prcnt
, mprcnt
, /* Params, length, max */
56 (uint8_t *)data
, drcnt
, mdrcnt
, /* Data, length, max */
57 NULL
, /* recv_flags2 */
58 NULL
, 0, NULL
, /* rsetup */
59 &my_rparam
, 0, &num_my_rparam
,
60 &my_rdata
, 0, &num_my_rdata
);
61 if (!NT_STATUS_IS_OK(status
)) {
66 * I know this memcpy massively hurts, but there are just tons
67 * of callers of cli_api that eventually need changing to
71 *rparam
= (char *)smb_memdup(my_rparam
, num_my_rparam
);
72 if (*rparam
== NULL
) {
75 *rprcnt
= num_my_rparam
;
76 TALLOC_FREE(my_rparam
);
78 *rdata
= (char *)smb_memdup(my_rdata
, num_my_rdata
);
82 *rdrcnt
= num_my_rdata
;
83 TALLOC_FREE(my_rdata
);
87 TALLOC_FREE(my_rdata
);
88 TALLOC_FREE(my_rparam
);
96 /****************************************************************************
97 Perform a NetWkstaUserLogon.
98 ****************************************************************************/
100 bool cli_NetWkstaUserLogon(struct cli_state
*cli
,char *user
, char *workstation
)
105 unsigned int rdrcnt
,rprcnt
;
108 memset(param
, 0, sizeof(param
));
110 /* send a SMBtrans command with api NetWkstaUserLogon */
112 SSVAL(p
,0,132); /* api number */
114 strlcpy(p
,"OOWb54WrLh",sizeof(param
)-PTR_DIFF(p
,param
));
115 p
= skip_string(param
,sizeof(param
),p
);
116 strlcpy(p
,"WB21BWDWWDDDDDDDzzzD",sizeof(param
)-PTR_DIFF(p
,param
));
117 p
= skip_string(param
,sizeof(param
),p
);
120 strlcpy(p
,user
,sizeof(param
)-PTR_DIFF(p
,param
));
121 if (!strupper_m(p
)) {
128 strlcpy(p
, workstation
,sizeof(param
)-PTR_DIFF(p
,param
));
129 if (!strupper_m(p
)) {
133 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
135 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
139 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
140 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
141 &rparam
, &rprcnt
, /* return params, return size */
142 &rdata
, &rdrcnt
/* return data, return size */
144 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
147 if (cli
->rap_error
== 0) {
148 DEBUG(4,("NetWkstaUserLogon success\n"));
150 * The cli->privileges = SVAL(p, 24); field was set here
151 * but it was not use anywhere else.
153 /* The cli->eff_name field used to be set here
154 but it wasn't used anywhere else. */
156 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
162 return (cli
->rap_error
== 0);
165 /****************************************************************************
166 Call a NetShareEnum - try and browse available connections on a host.
167 ****************************************************************************/
169 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32_t, const char *, void *), void *state
)
174 unsigned int rdrcnt
,rprcnt
;
178 /* now send a SMBtrans command with api RNetShareEnum */
180 SSVAL(p
,0,0); /* api number */
182 strlcpy(p
,"WrLeh",sizeof(param
)-PTR_DIFF(p
,param
));
183 p
= skip_string(param
,sizeof(param
),p
);
184 strlcpy(p
,"B13BWz",sizeof(param
)-PTR_DIFF(p
,param
));
185 p
= skip_string(param
,sizeof(param
),p
);
188 * Win2k needs a *smaller* buffer than 0xFFFF here -
189 * it returns "out of server memory" with 0xFFFF !!! JRA.
195 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
196 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
197 &rparam
, &rprcnt
, /* return params, length */
198 &rdata
, &rdrcnt
)) /* return data, length */
200 int res
= rparam
? SVAL(rparam
,0) : -1;
202 if (res
== 0 || res
== ERRmoredata
) {
203 int converter
=SVAL(rparam
,2);
205 char *rdata_end
= rdata
+ rdrcnt
;
207 count
=SVAL(rparam
,4);
210 for (i
=0;i
<count
;i
++,p
+=20) {
218 TALLOC_CTX
*frame
= talloc_stackframe();
220 if (p
+ 20 > rdata_end
) {
227 comment_offset
= (IVAL(p
,16) & 0xFFFF) - converter
;
228 if (comment_offset
< 0 ||
229 comment_offset
> (int)rdrcnt
) {
233 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
235 /* Work out the comment length. */
236 for (p1
= cmnt
, len
= 0; *p1
&&
237 p1
< rdata_end
; len
++)
242 pull_string_talloc(frame
,rdata
,0,
243 &s1
,sname
,14,STR_ASCII
);
244 pull_string_talloc(frame
,rdata
,0,
245 &s2
,cmnt
,len
,STR_ASCII
);
251 fn(s1
, type
, s2
, state
);
256 DEBUG(4,("NetShareEnum res=%d\n", res
));
259 DEBUG(4,("NetShareEnum failed\n"));
268 /****************************************************************************
269 Call a NetServerEnum for the specified workgroup and servertype mask. This
270 function then calls the specified callback function for each name returned.
272 The callback function takes 4 arguments: the machine name, the server type,
273 the comment and a state pointer.
274 ****************************************************************************/
276 bool cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32_t stype
,
277 void (*fn
)(const char *, uint32_t, const char *, void *),
282 char *rdata_end
= NULL
;
283 unsigned int rdrcnt
,rprcnt
;
288 uint32_t func
= RAP_NetServerEnum2
;
289 char *last_entry
= NULL
;
294 errno
= 0; /* reset */
297 * This may take more than one transaction, so we should loop until
298 * we no longer get a more data to process or we have all of the
302 /* send a SMBtrans command with api NetServerEnum */
304 SIVAL(p
,0,func
); /* api number */
307 if (func
== RAP_NetServerEnum3
) {
308 strlcpy(p
,"WrLehDzz", sizeof(param
)-PTR_DIFF(p
,param
));
310 strlcpy(p
,"WrLehDz", sizeof(param
)-PTR_DIFF(p
,param
));
313 p
= skip_string(param
, sizeof(param
), p
);
314 strlcpy(p
,"B16BBDz", sizeof(param
)-PTR_DIFF(p
,param
));
316 p
= skip_string(param
, sizeof(param
), p
);
318 SSVAL(p
,2,CLI_BUFFER_SIZE
);
323 /* If we have more data, tell the server where
328 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
329 STR_TERMINATE
|STR_UPPER
);
332 SAFE_FREE(last_entry
);
337 if (func
== RAP_NetServerEnum3
) {
339 last_entry
? last_entry
: "",
340 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
344 SAFE_FREE(last_entry
);
350 /* Next time through we need to use the continue api */
351 func
= RAP_NetServerEnum3
;
354 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
355 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
356 &rparam
, &rprcnt
, /* return params, return size */
357 &rdata
, &rdrcnt
)) { /* return data, return size */
359 /* break out of the loop on error */
364 rdata_end
= rdata
+ rdrcnt
;
365 res
= rparam
? SVAL(rparam
,0) : -1;
367 if (res
== 0 || res
== ERRmoredata
||
368 (res
!= -1 && cli_errno(cli
) == 0)) {
371 int converter
=SVAL(rparam
,2);
373 /* Get the number of items returned in this buffer */
374 count
= SVAL(rparam
, 4);
376 /* The next field contains the number of items left,
377 * including those returned in this buffer. So the
378 * first time through this should contain all of the
381 if (total_cnt
== 0) {
382 total_cnt
= SVAL(rparam
, 6);
385 /* Keep track of how many we have read */
389 /* The last name in the previous NetServerEnum reply is
390 * sent back to server in the NetServerEnum3 request
391 * (last_entry). The next reply should repeat this entry
392 * as the first element. We have no proof that this is
393 * always true, but from traces that seems to be the
394 * behavior from Window Servers. So first lets do a lot
395 * of checking, just being paranoid. If the string
396 * matches then we already saw this entry so skip it.
398 * NOTE: sv1_name field must be null terminated and has
399 * a max size of 16 (NetBIOS Name).
401 if (last_entry
&& count
&& p
&&
402 (strncmp(last_entry
, p
, 16) == 0)) {
403 count
-= 1; /* Skip this entry */
404 return_cnt
= -1; /* Not part of total, so don't count. */
405 p
= rdata
+ 26; /* Skip the whole record */
408 for (i
= 0; i
< count
; i
++, p
+= 26) {
413 TALLOC_CTX
*frame
= talloc_stackframe();
414 uint32_t entry_stype
;
416 if (p
+ 26 > rdata_end
) {
422 comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
423 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
425 if (comment_offset
< 0 || comment_offset
>= (int)rdrcnt
) {
430 /* Work out the comment length. */
431 for (p1
= cmnt
, len
= 0; *p1
&&
432 p1
< rdata_end
; len
++)
438 entry_stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
440 pull_string_talloc(frame
,rdata
,0,
441 &s1
,sname
,16,STR_ASCII
);
442 pull_string_talloc(frame
,rdata
,0,
443 &s2
,cmnt
,len
,STR_ASCII
);
450 fn(s1
, entry_stype
, s2
, state
);
454 /* We are done with the old last entry, so now we can free it */
456 SAFE_FREE(last_entry
); /* This will set it to null */
459 /* We always make a copy of the last entry if we have one */
461 last_entry
= smb_xstrdup(sname
);
464 /* If we have more data, but no last entry then error out */
465 if (!last_entry
&& (res
== ERRmoredata
)) {
474 } while ((res
== ERRmoredata
) && (total_cnt
> return_cnt
));
478 SAFE_FREE(last_entry
);
481 errno
= cli_errno(cli
);
484 /* this is a very special case, when the domain master for the
485 work group isn't part of the work group itself, there is something
491 return(return_cnt
> 0);
494 /****************************************************************************
495 Send a SamOEMChangePassword command.
496 ****************************************************************************/
498 bool cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
499 const char *old_password
)
502 unsigned char data
[532];
504 unsigned char old_pw_hash
[16];
505 unsigned char new_pw_hash
[16];
506 unsigned int data_len
;
507 unsigned int param_len
= 0;
510 unsigned int rprcnt
, rdrcnt
;
512 if (strlen(user
) >= sizeof(fstring
)-1) {
513 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
517 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
519 strlcpy(p
, "zsT", sizeof(param
)-PTR_DIFF(p
,param
));
520 p
= skip_string(param
,sizeof(param
),p
);
521 strlcpy(p
, "B516B16", sizeof(param
)-PTR_DIFF(p
,param
));
522 p
= skip_string(param
,sizeof(param
),p
);
523 strlcpy(p
,user
, sizeof(param
)-PTR_DIFF(p
,param
));
524 p
= skip_string(param
,sizeof(param
),p
);
528 param_len
= PTR_DIFF(p
,param
);
531 * Get the Lanman hash of the old password, we
532 * use this as the key to make_oem_passwd_hash().
534 E_deshash(old_password
, old_pw_hash
);
536 encode_pw_buffer(data
, new_password
, STR_ASCII
);
538 #ifdef DEBUG_PASSWORD
539 DEBUG(100,("make_oem_passwd_hash\n"));
540 dump_data(100, data
, 516);
542 arcfour_crypt( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
545 * Now place the old password hash in the data.
547 E_deshash(new_password
, new_pw_hash
);
549 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
554 param
, param_len
, 4, /* param, length, max */
555 (char *)data
, data_len
, 0, /* data, length, max */
558 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
564 cli
->rap_error
= SVAL(rparam
,0);
570 return (cli
->rap_error
== 0);
573 /****************************************************************************
574 Send a qpathinfo call.
575 ****************************************************************************/
577 struct cli_qpathinfo1_state
{
578 struct cli_state
*cli
;
583 static void cli_qpathinfo1_done(struct tevent_req
*subreq
);
585 struct tevent_req
*cli_qpathinfo1_send(TALLOC_CTX
*mem_ctx
,
586 struct tevent_context
*ev
,
587 struct cli_state
*cli
,
590 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
591 struct cli_qpathinfo1_state
*state
= NULL
;
593 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo1_state
);
598 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_INFO_STANDARD
,
599 22, CLI_BUFFER_SIZE
);
600 if (tevent_req_nomem(subreq
, req
)) {
601 return tevent_req_post(req
, ev
);
603 tevent_req_set_callback(subreq
, cli_qpathinfo1_done
, req
);
607 static void cli_qpathinfo1_done(struct tevent_req
*subreq
)
609 struct tevent_req
*req
= tevent_req_callback_data(
610 subreq
, struct tevent_req
);
611 struct cli_qpathinfo1_state
*state
= tevent_req_data(
612 req
, struct cli_qpathinfo1_state
);
615 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
618 if (!NT_STATUS_IS_OK(status
)) {
619 tevent_req_nterror(req
, status
);
622 tevent_req_done(req
);
625 NTSTATUS
cli_qpathinfo1_recv(struct tevent_req
*req
,
632 struct cli_qpathinfo1_state
*state
= tevent_req_data(
633 req
, struct cli_qpathinfo1_state
);
636 time_t (*date_fn
)(const void *buf
, int serverzone
);
638 if (tevent_req_is_nterror(req
, &status
)) {
642 if (state
->cli
->win95
) {
643 date_fn
= make_unix_date
;
645 date_fn
= make_unix_date2
;
649 *change_time
= date_fn(state
->data
+0, smb1cli_conn_server_time_zone(state
->cli
->conn
));
652 *access_time
= date_fn(state
->data
+4, smb1cli_conn_server_time_zone(state
->cli
->conn
));
655 *write_time
= date_fn(state
->data
+8, smb1cli_conn_server_time_zone(state
->cli
->conn
));
658 *size
= IVAL(state
->data
, 12);
661 *mode
= SVAL(state
->data
, l1_attrFile
);
666 NTSTATUS
cli_qpathinfo1(struct cli_state
*cli
,
674 TALLOC_CTX
*frame
= talloc_stackframe();
675 struct tevent_context
*ev
;
676 struct tevent_req
*req
;
677 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
679 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
681 * Can't use sync call while an async call is in flight
683 status
= NT_STATUS_INVALID_PARAMETER
;
686 ev
= samba_tevent_context_init(frame
);
690 req
= cli_qpathinfo1_send(frame
, ev
, cli
, fname
);
694 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
697 status
= cli_qpathinfo1_recv(req
, change_time
, access_time
,
698 write_time
, size
, mode
);
704 /****************************************************************************
705 Send a setpathinfo call.
706 ****************************************************************************/
708 NTSTATUS
cli_setpathinfo_basic(struct cli_state
*cli
, const char *fname
,
715 unsigned int data_len
= 0;
722 * Add the create, last access, modification, and status change times
724 put_long_date(p
, create_time
);
727 put_long_date(p
, access_time
);
730 put_long_date(p
, write_time
);
733 put_long_date(p
, change_time
);
736 if (mode
== (uint16_t)-1 || mode
== FILE_ATTRIBUTE_NORMAL
) {
739 } else if (mode
== 0) {
740 /* Clear all existing attributes. */
741 mode
= FILE_ATTRIBUTE_NORMAL
;
753 data_len
= PTR_DIFF(p
, data
);
755 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
756 DATA_BLOB in_data
= data_blob_const(data
, data_len
);
758 * Split out SMB2 here as we need to select
759 * the correct info type and level.
761 return cli_smb2_setpathinfo(cli
,
763 1, /* SMB2_SETINFO_FILE */
764 SMB_FILE_BASIC_INFORMATION
- 1000,
768 return cli_setpathinfo(cli
, SMB_FILE_BASIC_INFORMATION
, fname
,
769 (uint8_t *)data
, data_len
);
772 /****************************************************************************
773 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
774 ****************************************************************************/
776 struct cli_qpathinfo2_state
{
781 static void cli_qpathinfo2_done(struct tevent_req
*subreq
);
783 struct tevent_req
*cli_qpathinfo2_send(TALLOC_CTX
*mem_ctx
,
784 struct tevent_context
*ev
,
785 struct cli_state
*cli
,
788 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
789 struct cli_qpathinfo2_state
*state
= NULL
;
791 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo2_state
);
795 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
796 SMB_QUERY_FILE_ALL_INFO
,
797 68, CLI_BUFFER_SIZE
);
798 if (tevent_req_nomem(subreq
, req
)) {
799 return tevent_req_post(req
, ev
);
801 tevent_req_set_callback(subreq
, cli_qpathinfo2_done
, req
);
805 static void cli_qpathinfo2_done(struct tevent_req
*subreq
)
807 struct tevent_req
*req
= tevent_req_callback_data(
808 subreq
, struct tevent_req
);
809 struct cli_qpathinfo2_state
*state
= tevent_req_data(
810 req
, struct cli_qpathinfo2_state
);
813 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
816 if (!NT_STATUS_IS_OK(status
)) {
817 tevent_req_nterror(req
, status
);
820 tevent_req_done(req
);
823 NTSTATUS
cli_qpathinfo2_recv(struct tevent_req
*req
,
824 struct timespec
*create_time
,
825 struct timespec
*access_time
,
826 struct timespec
*write_time
,
827 struct timespec
*change_time
,
828 off_t
*size
, uint16_t *mode
,
831 struct cli_qpathinfo2_state
*state
= tevent_req_data(
832 req
, struct cli_qpathinfo2_state
);
835 if (tevent_req_is_nterror(req
, &status
)) {
840 *create_time
= interpret_long_date((char *)state
->data
+0);
843 *access_time
= interpret_long_date((char *)state
->data
+8);
846 *write_time
= interpret_long_date((char *)state
->data
+16);
849 *change_time
= interpret_long_date((char *)state
->data
+24);
852 *mode
= SVAL(state
->data
, 32);
855 *size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,48);
858 *ino
= IVAL(state
->data
, 64);
863 NTSTATUS
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
864 struct timespec
*create_time
,
865 struct timespec
*access_time
,
866 struct timespec
*write_time
,
867 struct timespec
*change_time
,
868 off_t
*size
, uint16_t *mode
,
871 TALLOC_CTX
*frame
= NULL
;
872 struct tevent_context
*ev
;
873 struct tevent_req
*req
;
874 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
876 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
877 return cli_smb2_qpathinfo2(cli
,
888 frame
= talloc_stackframe();
890 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
892 * Can't use sync call while an async call is in flight
894 status
= NT_STATUS_INVALID_PARAMETER
;
897 ev
= samba_tevent_context_init(frame
);
901 req
= cli_qpathinfo2_send(frame
, ev
, cli
, fname
);
905 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
908 status
= cli_qpathinfo2_recv(req
, create_time
, access_time
,
909 write_time
, change_time
, size
, mode
, ino
);
915 /****************************************************************************
917 ****************************************************************************/
919 struct cli_qpathinfo_streams_state
{
924 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
);
926 struct tevent_req
*cli_qpathinfo_streams_send(TALLOC_CTX
*mem_ctx
,
927 struct tevent_context
*ev
,
928 struct cli_state
*cli
,
931 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
932 struct cli_qpathinfo_streams_state
*state
= NULL
;
934 req
= tevent_req_create(mem_ctx
, &state
,
935 struct cli_qpathinfo_streams_state
);
939 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
940 SMB_FILE_STREAM_INFORMATION
,
942 if (tevent_req_nomem(subreq
, req
)) {
943 return tevent_req_post(req
, ev
);
945 tevent_req_set_callback(subreq
, cli_qpathinfo_streams_done
, req
);
949 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
)
951 struct tevent_req
*req
= tevent_req_callback_data(
952 subreq
, struct tevent_req
);
953 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
954 req
, struct cli_qpathinfo_streams_state
);
957 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
960 if (!NT_STATUS_IS_OK(status
)) {
961 tevent_req_nterror(req
, status
);
964 tevent_req_done(req
);
967 NTSTATUS
cli_qpathinfo_streams_recv(struct tevent_req
*req
,
969 unsigned int *pnum_streams
,
970 struct stream_struct
**pstreams
)
972 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
973 req
, struct cli_qpathinfo_streams_state
);
976 if (tevent_req_is_nterror(req
, &status
)) {
979 if (!parse_streams_blob(mem_ctx
, state
->data
, state
->num_data
,
980 pnum_streams
, pstreams
)) {
981 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
986 NTSTATUS
cli_qpathinfo_streams(struct cli_state
*cli
, const char *fname
,
988 unsigned int *pnum_streams
,
989 struct stream_struct
**pstreams
)
991 TALLOC_CTX
*frame
= NULL
;
992 struct tevent_context
*ev
;
993 struct tevent_req
*req
;
994 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
996 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
997 return cli_smb2_qpathinfo_streams(cli
,
1004 frame
= talloc_stackframe();
1006 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1008 * Can't use sync call while an async call is in flight
1010 status
= NT_STATUS_INVALID_PARAMETER
;
1013 ev
= samba_tevent_context_init(frame
);
1017 req
= cli_qpathinfo_streams_send(frame
, ev
, cli
, fname
);
1021 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1024 status
= cli_qpathinfo_streams_recv(req
, mem_ctx
, pnum_streams
,
1031 bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *rdata
,
1033 unsigned int *pnum_streams
,
1034 struct stream_struct
**pstreams
)
1036 unsigned int num_streams
;
1037 struct stream_struct
*streams
;
1044 while ((data_len
> ofs
) && (data_len
- ofs
>= 24)) {
1048 struct stream_struct
*tmp
;
1051 tmp
= talloc_realloc(mem_ctx
, streams
,
1052 struct stream_struct
,
1060 nlen
= IVAL(rdata
, ofs
+ 0x04);
1062 streams
[num_streams
].size
= IVAL_TO_SMB_OFF_T(
1064 streams
[num_streams
].alloc_size
= IVAL_TO_SMB_OFF_T(
1067 if (nlen
> data_len
- (ofs
+ 24)) {
1072 * We need to null-terminate src, how do I do this with
1073 * convert_string_talloc??
1076 tmp_buf
= talloc_array(streams
, uint8_t, nlen
+2);
1077 if (tmp_buf
== NULL
) {
1081 memcpy(tmp_buf
, rdata
+ofs
+24, nlen
);
1083 tmp_buf
[nlen
+1] = 0;
1085 if (!convert_string_talloc(streams
, CH_UTF16
, CH_UNIX
, tmp_buf
,
1086 nlen
+2, &vstr
, &size
))
1088 TALLOC_FREE(tmp_buf
);
1092 TALLOC_FREE(tmp_buf
);
1093 streams
[num_streams
].name
= (char *)vstr
;
1096 len
= IVAL(rdata
, ofs
);
1097 if (len
> data_len
- ofs
) {
1100 if (len
== 0) break;
1104 *pnum_streams
= num_streams
;
1105 *pstreams
= streams
;
1109 TALLOC_FREE(streams
);
1113 /****************************************************************************
1114 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1115 ****************************************************************************/
1117 NTSTATUS
cli_qfilename(struct cli_state
*cli
, uint16_t fnum
,
1118 TALLOC_CTX
*mem_ctx
, char **_name
)
1120 uint16_t recv_flags2
;
1127 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1128 SMB_QUERY_FILE_NAME_INFO
,
1129 4, CLI_BUFFER_SIZE
, &recv_flags2
,
1130 &rdata
, &num_rdata
);
1131 if (!NT_STATUS_IS_OK(status
)) {
1135 namelen
= IVAL(rdata
, 0);
1136 if (namelen
> (num_rdata
- 4)) {
1138 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1141 clistr_pull_talloc(mem_ctx
,
1142 (const char *)rdata
,
1149 status
= map_nt_error_from_unix(errno
);
1156 return NT_STATUS_OK
;
1159 /****************************************************************************
1160 Send a qfileinfo call.
1161 ****************************************************************************/
1163 NTSTATUS
cli_qfileinfo_basic(struct cli_state
*cli
, uint16_t fnum
,
1164 uint16_t *mode
, off_t
*size
,
1165 struct timespec
*create_time
,
1166 struct timespec
*access_time
,
1167 struct timespec
*write_time
,
1168 struct timespec
*change_time
,
1175 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1176 return cli_smb2_qfileinfo_basic(cli
,
1187 /* if its a win95 server then fail this - win95 totally screws it
1190 return NT_STATUS_NOT_SUPPORTED
;
1193 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1194 SMB_QUERY_FILE_ALL_INFO
,
1195 68, CLI_BUFFER_SIZE
,
1197 &rdata
, &num_rdata
);
1198 if (!NT_STATUS_IS_OK(status
)) {
1203 *create_time
= interpret_long_date((char *)rdata
+0);
1206 *access_time
= interpret_long_date((char *)rdata
+8);
1209 *write_time
= interpret_long_date((char *)rdata
+16);
1212 *change_time
= interpret_long_date((char *)rdata
+24);
1215 *mode
= SVAL(rdata
, 32);
1218 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
1221 *ino
= IVAL(rdata
, 64);
1225 return NT_STATUS_OK
;
1228 /****************************************************************************
1229 Send a qpathinfo BASIC_INFO call.
1230 ****************************************************************************/
1232 struct cli_qpathinfo_basic_state
{
1237 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
);
1239 struct tevent_req
*cli_qpathinfo_basic_send(TALLOC_CTX
*mem_ctx
,
1240 struct tevent_context
*ev
,
1241 struct cli_state
*cli
,
1244 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1245 struct cli_qpathinfo_basic_state
*state
= NULL
;
1247 req
= tevent_req_create(mem_ctx
, &state
,
1248 struct cli_qpathinfo_basic_state
);
1252 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
1253 SMB_QUERY_FILE_BASIC_INFO
,
1254 36, CLI_BUFFER_SIZE
);
1255 if (tevent_req_nomem(subreq
, req
)) {
1256 return tevent_req_post(req
, ev
);
1258 tevent_req_set_callback(subreq
, cli_qpathinfo_basic_done
, req
);
1262 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
)
1264 struct tevent_req
*req
= tevent_req_callback_data(
1265 subreq
, struct tevent_req
);
1266 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1267 req
, struct cli_qpathinfo_basic_state
);
1270 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
1272 TALLOC_FREE(subreq
);
1273 if (!NT_STATUS_IS_OK(status
)) {
1274 tevent_req_nterror(req
, status
);
1277 tevent_req_done(req
);
1280 NTSTATUS
cli_qpathinfo_basic_recv(struct tevent_req
*req
,
1281 SMB_STRUCT_STAT
*sbuf
, uint32_t *attributes
)
1283 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1284 req
, struct cli_qpathinfo_basic_state
);
1287 if (tevent_req_is_nterror(req
, &status
)) {
1291 sbuf
->st_ex_btime
= interpret_long_date((char *)state
->data
);
1292 sbuf
->st_ex_atime
= interpret_long_date((char *)state
->data
+8);
1293 sbuf
->st_ex_mtime
= interpret_long_date((char *)state
->data
+16);
1294 sbuf
->st_ex_ctime
= interpret_long_date((char *)state
->data
+24);
1295 *attributes
= IVAL(state
->data
, 32);
1296 return NT_STATUS_OK
;
1299 NTSTATUS
cli_qpathinfo_basic(struct cli_state
*cli
, const char *name
,
1300 SMB_STRUCT_STAT
*sbuf
, uint32_t *attributes
)
1302 TALLOC_CTX
*frame
= NULL
;
1303 struct tevent_context
*ev
;
1304 struct tevent_req
*req
;
1305 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1307 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1308 return cli_smb2_qpathinfo_basic(cli
,
1314 frame
= talloc_stackframe();
1316 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1318 * Can't use sync call while an async call is in flight
1320 status
= NT_STATUS_INVALID_PARAMETER
;
1323 ev
= samba_tevent_context_init(frame
);
1327 req
= cli_qpathinfo_basic_send(frame
, ev
, cli
, name
);
1331 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1334 status
= cli_qpathinfo_basic_recv(req
, sbuf
, attributes
);
1340 /****************************************************************************
1341 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1342 ****************************************************************************/
1344 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
1349 char *converted
= NULL
;
1350 size_t converted_size
= 0;
1353 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1354 return cli_smb2_qpathinfo_alt_name(cli
,
1359 status
= cli_qpathinfo(talloc_tos(), cli
, fname
,
1360 SMB_QUERY_FILE_ALT_NAME_INFO
,
1361 4, CLI_BUFFER_SIZE
, &rdata
, &num_rdata
);
1362 if (!NT_STATUS_IS_OK(status
)) {
1366 len
= IVAL(rdata
, 0);
1368 if (len
> num_rdata
- 4) {
1369 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1372 /* The returned data is a pushed string, not raw data. */
1373 if (!convert_string_talloc(talloc_tos(),
1374 smbXcli_conn_use_unicode(cli
->conn
) ? CH_UTF16LE
: CH_DOS
,
1380 return NT_STATUS_NO_MEMORY
;
1382 fstrcpy(alt_name
, converted
);
1384 TALLOC_FREE(converted
);
1387 return NT_STATUS_OK
;
1390 /****************************************************************************
1391 Send a qpathinfo SMB_QUERY_FILE_STADNDARD_INFO call.
1392 ****************************************************************************/
1394 NTSTATUS
cli_qpathinfo_standard(struct cli_state
*cli
, const char *fname
,
1395 uint64_t *allocated
, uint64_t *size
,
1397 bool *is_del_pending
, bool *is_dir
)
1403 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1404 return NT_STATUS_NOT_IMPLEMENTED
;
1407 status
= cli_qpathinfo(talloc_tos(), cli
, fname
,
1408 SMB_QUERY_FILE_STANDARD_INFO
,
1409 24, CLI_BUFFER_SIZE
, &rdata
, &num_rdata
);
1410 if (!NT_STATUS_IS_OK(status
)) {
1415 *allocated
= BVAL(rdata
, 0);
1419 *size
= BVAL(rdata
, 8);
1423 *nlinks
= IVAL(rdata
, 16);
1426 if (is_del_pending
) {
1427 *is_del_pending
= CVAL(rdata
, 20);
1431 *is_dir
= CVAL(rdata
, 20);
1436 return NT_STATUS_OK
;
1440 /* like cli_qpathinfo2 but do not use SMB_QUERY_FILE_ALL_INFO with smb1 */
1441 NTSTATUS
cli_qpathinfo3(struct cli_state
*cli
, const char *fname
,
1442 struct timespec
*create_time
,
1443 struct timespec
*access_time
,
1444 struct timespec
*write_time
,
1445 struct timespec
*change_time
,
1446 off_t
*size
, uint16_t *mode
,
1449 NTSTATUS status
= NT_STATUS_OK
;
1450 SMB_STRUCT_STAT st
= { 0 };
1454 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1455 return cli_qpathinfo2(cli
, fname
,
1456 create_time
, access_time
, write_time
, change_time
,
1460 if (create_time
|| access_time
|| write_time
|| change_time
|| mode
) {
1461 status
= cli_qpathinfo_basic(cli
, fname
, &st
, &attr
);
1462 if (!NT_STATUS_IS_OK(status
)) {
1468 status
= cli_qpathinfo_standard(cli
, fname
,
1469 NULL
, &pos
, NULL
, NULL
, NULL
);
1470 if (!NT_STATUS_IS_OK(status
)) {
1478 *create_time
= st
.st_ex_btime
;
1481 *access_time
= st
.st_ex_atime
;
1484 *write_time
= st
.st_ex_mtime
;
1487 *change_time
= st
.st_ex_ctime
;
1496 return NT_STATUS_OK
;