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 "async_smb.h"
27 #include "libsmb/clirap.h"
30 #define PIPE_LANMAN "\\PIPE\\LANMAN"
32 /****************************************************************************
34 ****************************************************************************/
36 bool cli_api(struct cli_state
*cli
,
37 char *param
, int prcnt
, int mprcnt
,
38 char *data
, int drcnt
, int mdrcnt
,
39 char **rparam
, unsigned int *rprcnt
,
40 char **rdata
, unsigned int *rdrcnt
)
44 uint8_t *my_rparam
, *my_rdata
;
45 uint32_t num_my_rparam
, num_my_rdata
;
47 status
= cli_trans(talloc_tos(), cli
, SMBtrans
,
48 PIPE_LANMAN
, 0, /* name, fid */
49 0, 0, /* function, flags */
50 NULL
, 0, 0, /* setup */
51 (uint8_t *)param
, prcnt
, mprcnt
, /* Params, length, max */
52 (uint8_t *)data
, drcnt
, mdrcnt
, /* Data, length, max */
53 NULL
, /* recv_flags2 */
54 NULL
, 0, NULL
, /* rsetup */
55 &my_rparam
, 0, &num_my_rparam
,
56 &my_rdata
, 0, &num_my_rdata
);
57 if (!NT_STATUS_IS_OK(status
)) {
62 * I know this memcpy massively hurts, but there are just tons
63 * of callers of cli_api that eventually need changing to
67 *rparam
= (char *)memdup(my_rparam
, num_my_rparam
);
68 if (*rparam
== NULL
) {
71 *rprcnt
= num_my_rparam
;
72 TALLOC_FREE(my_rparam
);
74 *rdata
= (char *)memdup(my_rdata
, num_my_rdata
);
78 *rdrcnt
= num_my_rdata
;
79 TALLOC_FREE(my_rdata
);
83 TALLOC_FREE(my_rdata
);
84 TALLOC_FREE(my_rparam
);
92 /****************************************************************************
93 Perform a NetWkstaUserLogon.
94 ****************************************************************************/
96 bool cli_NetWkstaUserLogon(struct cli_state
*cli
,char *user
, char *workstation
)
101 unsigned int rdrcnt
,rprcnt
;
104 memset(param
, 0, sizeof(param
));
106 /* send a SMBtrans command with api NetWkstaUserLogon */
108 SSVAL(p
,0,132); /* api number */
110 strlcpy(p
,"OOWb54WrLh",sizeof(param
)-PTR_DIFF(p
,param
));
111 p
= skip_string(param
,sizeof(param
),p
);
112 strlcpy(p
,"WB21BWDWWDDDDDDDzzzD",sizeof(param
)-PTR_DIFF(p
,param
));
113 p
= skip_string(param
,sizeof(param
),p
);
116 strlcpy(p
,user
,sizeof(param
)-PTR_DIFF(p
,param
));
122 strlcpy(p
, workstation
,sizeof(param
)-PTR_DIFF(p
,param
));
125 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
127 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
131 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
132 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
133 &rparam
, &rprcnt
, /* return params, return size */
134 &rdata
, &rdrcnt
/* return data, return size */
136 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
139 if (cli
->rap_error
== 0) {
140 DEBUG(4,("NetWkstaUserLogon success\n"));
141 cli
->privileges
= SVAL(p
, 24);
142 /* The cli->eff_name field used to be set here
143 but it wasn't used anywhere else. */
145 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
151 return (cli
->rap_error
== 0);
154 /****************************************************************************
155 Call a NetShareEnum - try and browse available connections on a host.
156 ****************************************************************************/
158 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32
, const char *, void *), void *state
)
163 unsigned int rdrcnt
,rprcnt
;
167 /* now send a SMBtrans command with api RNetShareEnum */
169 SSVAL(p
,0,0); /* api number */
171 strlcpy(p
,"WrLeh",sizeof(param
)-PTR_DIFF(p
,param
));
172 p
= skip_string(param
,sizeof(param
),p
);
173 strlcpy(p
,"B13BWz",sizeof(param
)-PTR_DIFF(p
,param
));
174 p
= skip_string(param
,sizeof(param
),p
);
177 * Win2k needs a *smaller* buffer than 0xFFFF here -
178 * it returns "out of server memory" with 0xFFFF !!! JRA.
184 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
185 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
186 &rparam
, &rprcnt
, /* return params, length */
187 &rdata
, &rdrcnt
)) /* return data, length */
189 int res
= rparam
? SVAL(rparam
,0) : -1;
191 if (res
== 0 || res
== ERRmoredata
) {
192 int converter
=SVAL(rparam
,2);
194 char *rdata_end
= rdata
+ rdrcnt
;
196 count
=SVAL(rparam
,4);
199 for (i
=0;i
<count
;i
++,p
+=20) {
207 TALLOC_CTX
*frame
= talloc_stackframe();
209 if (p
+ 20 > rdata_end
) {
216 comment_offset
= (IVAL(p
,16) & 0xFFFF) - converter
;
217 if (comment_offset
< 0 ||
218 comment_offset
> (int)rdrcnt
) {
222 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
224 /* Work out the comment length. */
225 for (p1
= cmnt
, len
= 0; *p1
&&
226 p1
< rdata_end
; len
++)
231 pull_string_talloc(frame
,rdata
,0,
232 &s1
,sname
,14,STR_ASCII
);
233 pull_string_talloc(frame
,rdata
,0,
234 &s2
,cmnt
,len
,STR_ASCII
);
240 fn(s1
, type
, s2
, state
);
245 DEBUG(4,("NetShareEnum res=%d\n", res
));
248 DEBUG(4,("NetShareEnum failed\n"));
257 /****************************************************************************
258 Call a NetServerEnum for the specified workgroup and servertype mask. This
259 function then calls the specified callback function for each name returned.
261 The callback function takes 4 arguments: the machine name, the server type,
262 the comment and a state pointer.
263 ****************************************************************************/
265 bool cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32 stype
,
266 void (*fn
)(const char *, uint32
, const char *, void *),
271 char *rdata_end
= NULL
;
272 unsigned int rdrcnt
,rprcnt
;
277 uint32 func
= RAP_NetServerEnum2
;
278 char *last_entry
= NULL
;
283 errno
= 0; /* reset */
286 * This may take more than one transaction, so we should loop until
287 * we no longer get a more data to process or we have all of the
291 /* send a SMBtrans command with api NetServerEnum */
293 SIVAL(p
,0,func
); /* api number */
296 if (func
== RAP_NetServerEnum3
) {
297 strlcpy(p
,"WrLehDzz", sizeof(param
)-PTR_DIFF(p
,param
));
299 strlcpy(p
,"WrLehDz", sizeof(param
)-PTR_DIFF(p
,param
));
302 p
= skip_string(param
, sizeof(param
), p
);
303 strlcpy(p
,"B16BBDz", sizeof(param
)-PTR_DIFF(p
,param
));
305 p
= skip_string(param
, sizeof(param
), p
);
307 SSVAL(p
,2,CLI_BUFFER_SIZE
);
312 /* If we have more data, tell the server where
317 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
318 STR_TERMINATE
|STR_UPPER
);
320 if (len
== (size_t)-1) {
321 SAFE_FREE(last_entry
);
326 if (func
== RAP_NetServerEnum3
) {
328 last_entry
? last_entry
: "",
329 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
332 if (len
== (size_t)-1) {
333 SAFE_FREE(last_entry
);
339 /* Next time through we need to use the continue api */
340 func
= RAP_NetServerEnum3
;
343 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
344 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
345 &rparam
, &rprcnt
, /* return params, return size */
346 &rdata
, &rdrcnt
)) { /* return data, return size */
348 /* break out of the loop on error */
353 rdata_end
= rdata
+ rdrcnt
;
354 res
= rparam
? SVAL(rparam
,0) : -1;
356 if (res
== 0 || res
== ERRmoredata
||
357 (res
!= -1 && cli_errno(cli
) == 0)) {
360 int converter
=SVAL(rparam
,2);
362 /* Get the number of items returned in this buffer */
363 count
= SVAL(rparam
, 4);
365 /* The next field contains the number of items left,
366 * including those returned in this buffer. So the
367 * first time through this should contain all of the
370 if (total_cnt
== 0) {
371 total_cnt
= SVAL(rparam
, 6);
374 /* Keep track of how many we have read */
378 /* The last name in the previous NetServerEnum reply is
379 * sent back to server in the NetServerEnum3 request
380 * (last_entry). The next reply should repeat this entry
381 * as the first element. We have no proof that this is
382 * always true, but from traces that seems to be the
383 * behavior from Window Servers. So first lets do a lot
384 * of checking, just being paranoid. If the string
385 * matches then we already saw this entry so skip it.
387 * NOTE: sv1_name field must be null terminated and has
388 * a max size of 16 (NetBIOS Name).
390 if (last_entry
&& count
&& p
&&
391 (strncmp(last_entry
, p
, 16) == 0)) {
392 count
-= 1; /* Skip this entry */
393 return_cnt
= -1; /* Not part of total, so don't count. */
394 p
= rdata
+ 26; /* Skip the whole record */
397 for (i
= 0; i
< count
; i
++, p
+= 26) {
402 TALLOC_CTX
*frame
= talloc_stackframe();
403 uint32_t entry_stype
;
405 if (p
+ 26 > rdata_end
) {
411 comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
412 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
414 if (comment_offset
< 0 || comment_offset
>= (int)rdrcnt
) {
419 /* Work out the comment length. */
420 for (p1
= cmnt
, len
= 0; *p1
&&
421 p1
< rdata_end
; len
++)
427 entry_stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
429 pull_string_talloc(frame
,rdata
,0,
430 &s1
,sname
,16,STR_ASCII
);
431 pull_string_talloc(frame
,rdata
,0,
432 &s2
,cmnt
,len
,STR_ASCII
);
439 fn(s1
, entry_stype
, s2
, state
);
443 /* We are done with the old last entry, so now we can free it */
445 SAFE_FREE(last_entry
); /* This will set it to null */
448 /* We always make a copy of the last entry if we have one */
450 last_entry
= smb_xstrdup(sname
);
453 /* If we have more data, but no last entry then error out */
454 if (!last_entry
&& (res
== ERRmoredata
)) {
463 } while ((res
== ERRmoredata
) && (total_cnt
> return_cnt
));
467 SAFE_FREE(last_entry
);
470 errno
= cli_errno(cli
);
473 /* this is a very special case, when the domain master for the
474 work group isn't part of the work group itself, there is something
480 return(return_cnt
> 0);
483 /****************************************************************************
484 Send a SamOEMChangePassword command.
485 ****************************************************************************/
487 bool cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
488 const char *old_password
)
491 unsigned char data
[532];
493 unsigned char old_pw_hash
[16];
494 unsigned char new_pw_hash
[16];
495 unsigned int data_len
;
496 unsigned int param_len
= 0;
499 unsigned int rprcnt
, rdrcnt
;
501 if (strlen(user
) >= sizeof(fstring
)-1) {
502 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
506 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
508 strlcpy(p
, "zsT", sizeof(param
)-PTR_DIFF(p
,param
));
509 p
= skip_string(param
,sizeof(param
),p
);
510 strlcpy(p
, "B516B16", sizeof(param
)-PTR_DIFF(p
,param
));
511 p
= skip_string(param
,sizeof(param
),p
);
512 strlcpy(p
,user
, sizeof(param
)-PTR_DIFF(p
,param
));
513 p
= skip_string(param
,sizeof(param
),p
);
517 param_len
= PTR_DIFF(p
,param
);
520 * Get the Lanman hash of the old password, we
521 * use this as the key to make_oem_passwd_hash().
523 E_deshash(old_password
, old_pw_hash
);
525 encode_pw_buffer(data
, new_password
, STR_ASCII
);
527 #ifdef DEBUG_PASSWORD
528 DEBUG(100,("make_oem_passwd_hash\n"));
529 dump_data(100, data
, 516);
531 arcfour_crypt( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
534 * Now place the old password hash in the data.
536 E_deshash(new_password
, new_pw_hash
);
538 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
543 param
, param_len
, 4, /* param, length, max */
544 (char *)data
, data_len
, 0, /* data, length, max */
547 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
553 cli
->rap_error
= SVAL(rparam
,0);
559 return (cli
->rap_error
== 0);
562 /****************************************************************************
563 Send a qpathinfo call.
564 ****************************************************************************/
566 struct cli_qpathinfo1_state
{
567 struct cli_state
*cli
;
572 static void cli_qpathinfo1_done(struct tevent_req
*subreq
);
574 struct tevent_req
*cli_qpathinfo1_send(TALLOC_CTX
*mem_ctx
,
575 struct event_context
*ev
,
576 struct cli_state
*cli
,
579 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
580 struct cli_qpathinfo1_state
*state
= NULL
;
582 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo1_state
);
587 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_INFO_STANDARD
,
589 if (tevent_req_nomem(subreq
, req
)) {
590 return tevent_req_post(req
, ev
);
592 tevent_req_set_callback(subreq
, cli_qpathinfo1_done
, req
);
596 static void cli_qpathinfo1_done(struct tevent_req
*subreq
)
598 struct tevent_req
*req
= tevent_req_callback_data(
599 subreq
, struct tevent_req
);
600 struct cli_qpathinfo1_state
*state
= tevent_req_data(
601 req
, struct cli_qpathinfo1_state
);
604 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
607 if (!NT_STATUS_IS_OK(status
)) {
608 tevent_req_nterror(req
, status
);
611 tevent_req_done(req
);
614 NTSTATUS
cli_qpathinfo1_recv(struct tevent_req
*req
,
621 struct cli_qpathinfo1_state
*state
= tevent_req_data(
622 req
, struct cli_qpathinfo1_state
);
625 time_t (*date_fn
)(const void *buf
, int serverzone
);
627 if (tevent_req_is_nterror(req
, &status
)) {
631 if (state
->cli
->win95
) {
632 date_fn
= make_unix_date
;
634 date_fn
= make_unix_date2
;
638 *change_time
= date_fn(state
->data
+0, state
->cli
->serverzone
);
641 *access_time
= date_fn(state
->data
+4, state
->cli
->serverzone
);
644 *write_time
= date_fn(state
->data
+8, state
->cli
->serverzone
);
647 *size
= IVAL(state
->data
, 12);
650 *mode
= SVAL(state
->data
, l1_attrFile
);
655 NTSTATUS
cli_qpathinfo1(struct cli_state
*cli
,
663 TALLOC_CTX
*frame
= talloc_stackframe();
664 struct event_context
*ev
;
665 struct tevent_req
*req
;
666 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
668 if (cli_has_async_calls(cli
)) {
670 * Can't use sync call while an async call is in flight
672 status
= NT_STATUS_INVALID_PARAMETER
;
675 ev
= event_context_init(frame
);
679 req
= cli_qpathinfo1_send(frame
, ev
, cli
, fname
);
683 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
686 status
= cli_qpathinfo1_recv(req
, change_time
, access_time
,
687 write_time
, size
, mode
);
690 if (!NT_STATUS_IS_OK(status
)) {
691 cli_set_error(cli
, status
);
696 /****************************************************************************
697 Send a setpathinfo call.
698 ****************************************************************************/
700 NTSTATUS
cli_setpathinfo_basic(struct cli_state
*cli
, const char *fname
,
707 unsigned int data_len
= 0;
714 * Add the create, last access, modification, and status change times
716 put_long_date(p
, create_time
);
719 put_long_date(p
, access_time
);
722 put_long_date(p
, write_time
);
725 put_long_date(p
, change_time
);
736 data_len
= PTR_DIFF(p
, data
);
738 return cli_setpathinfo(cli
, SMB_FILE_BASIC_INFORMATION
, fname
,
739 (uint8_t *)data
, data_len
);
742 /****************************************************************************
743 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
744 ****************************************************************************/
746 struct cli_qpathinfo2_state
{
751 static void cli_qpathinfo2_done(struct tevent_req
*subreq
);
753 struct tevent_req
*cli_qpathinfo2_send(TALLOC_CTX
*mem_ctx
,
754 struct event_context
*ev
,
755 struct cli_state
*cli
,
758 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
759 struct cli_qpathinfo2_state
*state
= NULL
;
761 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo2_state
);
765 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
766 SMB_QUERY_FILE_ALL_INFO
,
768 if (tevent_req_nomem(subreq
, req
)) {
769 return tevent_req_post(req
, ev
);
771 tevent_req_set_callback(subreq
, cli_qpathinfo2_done
, req
);
775 static void cli_qpathinfo2_done(struct tevent_req
*subreq
)
777 struct tevent_req
*req
= tevent_req_callback_data(
778 subreq
, struct tevent_req
);
779 struct cli_qpathinfo2_state
*state
= tevent_req_data(
780 req
, struct cli_qpathinfo2_state
);
783 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
786 if (!NT_STATUS_IS_OK(status
)) {
787 tevent_req_nterror(req
, status
);
790 tevent_req_done(req
);
793 NTSTATUS
cli_qpathinfo2_recv(struct tevent_req
*req
,
794 struct timespec
*create_time
,
795 struct timespec
*access_time
,
796 struct timespec
*write_time
,
797 struct timespec
*change_time
,
798 SMB_OFF_T
*size
, uint16
*mode
,
801 struct cli_qpathinfo2_state
*state
= tevent_req_data(
802 req
, struct cli_qpathinfo2_state
);
805 if (tevent_req_is_nterror(req
, &status
)) {
810 *create_time
= interpret_long_date((char *)state
->data
+0);
813 *access_time
= interpret_long_date((char *)state
->data
+8);
816 *write_time
= interpret_long_date((char *)state
->data
+16);
819 *change_time
= interpret_long_date((char *)state
->data
+24);
822 *mode
= SVAL(state
->data
, 32);
825 *size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,48);
828 *ino
= IVAL(state
->data
, 64);
833 NTSTATUS
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
834 struct timespec
*create_time
,
835 struct timespec
*access_time
,
836 struct timespec
*write_time
,
837 struct timespec
*change_time
,
838 SMB_OFF_T
*size
, uint16
*mode
,
841 TALLOC_CTX
*frame
= talloc_stackframe();
842 struct event_context
*ev
;
843 struct tevent_req
*req
;
844 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
846 if (cli_has_async_calls(cli
)) {
848 * Can't use sync call while an async call is in flight
850 status
= NT_STATUS_INVALID_PARAMETER
;
853 ev
= event_context_init(frame
);
857 req
= cli_qpathinfo2_send(frame
, ev
, cli
, fname
);
861 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
864 status
= cli_qpathinfo2_recv(req
, create_time
, access_time
,
865 write_time
, change_time
, size
, mode
, ino
);
868 if (!NT_STATUS_IS_OK(status
)) {
869 cli_set_error(cli
, status
);
874 /****************************************************************************
876 ****************************************************************************/
878 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *data
,
880 unsigned int *pnum_streams
,
881 struct stream_struct
**pstreams
);
883 struct cli_qpathinfo_streams_state
{
888 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
);
890 struct tevent_req
*cli_qpathinfo_streams_send(TALLOC_CTX
*mem_ctx
,
891 struct tevent_context
*ev
,
892 struct cli_state
*cli
,
895 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
896 struct cli_qpathinfo_streams_state
*state
= NULL
;
898 req
= tevent_req_create(mem_ctx
, &state
,
899 struct cli_qpathinfo_streams_state
);
903 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
904 SMB_FILE_STREAM_INFORMATION
,
906 if (tevent_req_nomem(subreq
, req
)) {
907 return tevent_req_post(req
, ev
);
909 tevent_req_set_callback(subreq
, cli_qpathinfo_streams_done
, req
);
913 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
)
915 struct tevent_req
*req
= tevent_req_callback_data(
916 subreq
, struct tevent_req
);
917 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
918 req
, struct cli_qpathinfo_streams_state
);
921 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
924 if (!NT_STATUS_IS_OK(status
)) {
925 tevent_req_nterror(req
, status
);
928 tevent_req_done(req
);
931 NTSTATUS
cli_qpathinfo_streams_recv(struct tevent_req
*req
,
933 unsigned int *pnum_streams
,
934 struct stream_struct
**pstreams
)
936 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
937 req
, struct cli_qpathinfo_streams_state
);
940 if (tevent_req_is_nterror(req
, &status
)) {
943 if (!parse_streams_blob(mem_ctx
, state
->data
, state
->num_data
,
944 pnum_streams
, pstreams
)) {
945 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
950 NTSTATUS
cli_qpathinfo_streams(struct cli_state
*cli
, const char *fname
,
952 unsigned int *pnum_streams
,
953 struct stream_struct
**pstreams
)
955 TALLOC_CTX
*frame
= talloc_stackframe();
956 struct event_context
*ev
;
957 struct tevent_req
*req
;
958 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
960 if (cli_has_async_calls(cli
)) {
962 * Can't use sync call while an async call is in flight
964 status
= NT_STATUS_INVALID_PARAMETER
;
967 ev
= event_context_init(frame
);
971 req
= cli_qpathinfo_streams_send(frame
, ev
, cli
, fname
);
975 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
978 status
= cli_qpathinfo_streams_recv(req
, mem_ctx
, pnum_streams
,
982 if (!NT_STATUS_IS_OK(status
)) {
983 cli_set_error(cli
, status
);
988 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *rdata
,
990 unsigned int *pnum_streams
,
991 struct stream_struct
**pstreams
)
993 unsigned int num_streams
;
994 struct stream_struct
*streams
;
1001 while ((data_len
> ofs
) && (data_len
- ofs
>= 24)) {
1005 struct stream_struct
*tmp
;
1008 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, streams
,
1009 struct stream_struct
,
1017 nlen
= IVAL(rdata
, ofs
+ 0x04);
1019 streams
[num_streams
].size
= IVAL_TO_SMB_OFF_T(
1021 streams
[num_streams
].alloc_size
= IVAL_TO_SMB_OFF_T(
1024 if (nlen
> data_len
- (ofs
+ 24)) {
1029 * We need to null-terminate src, how do I do this with
1030 * convert_string_talloc??
1033 tmp_buf
= TALLOC_ARRAY(streams
, uint8_t, nlen
+2);
1034 if (tmp_buf
== NULL
) {
1038 memcpy(tmp_buf
, rdata
+ofs
+24, nlen
);
1040 tmp_buf
[nlen
+1] = 0;
1042 if (!convert_string_talloc(streams
, CH_UTF16
, CH_UNIX
, tmp_buf
,
1043 nlen
+2, &vstr
, &size
, false))
1045 TALLOC_FREE(tmp_buf
);
1049 TALLOC_FREE(tmp_buf
);
1050 streams
[num_streams
].name
= (char *)vstr
;
1053 len
= IVAL(rdata
, ofs
);
1054 if (len
> data_len
- ofs
) {
1057 if (len
== 0) break;
1061 *pnum_streams
= num_streams
;
1062 *pstreams
= streams
;
1066 TALLOC_FREE(streams
);
1070 /****************************************************************************
1071 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1072 ****************************************************************************/
1074 NTSTATUS
cli_qfilename(struct cli_state
*cli
, uint16_t fnum
, char *name
,
1081 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1082 SMB_QUERY_FILE_NAME_INFO
,
1084 &rdata
, &num_rdata
);
1085 if (!NT_STATUS_IS_OK(status
)) {
1089 clistr_pull(cli
->inbuf
, name
, rdata
+4, namelen
, IVAL(rdata
, 0),
1092 return NT_STATUS_OK
;
1095 /****************************************************************************
1096 Send a qfileinfo call.
1097 ****************************************************************************/
1099 NTSTATUS
cli_qfileinfo_basic(struct cli_state
*cli
, uint16_t fnum
,
1100 uint16
*mode
, SMB_OFF_T
*size
,
1101 struct timespec
*create_time
,
1102 struct timespec
*access_time
,
1103 struct timespec
*write_time
,
1104 struct timespec
*change_time
,
1111 /* if its a win95 server then fail this - win95 totally screws it
1114 return NT_STATUS_NOT_SUPPORTED
;
1117 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1118 SMB_QUERY_FILE_ALL_INFO
,
1119 68, MIN(cli
->max_xmit
, 0xffff),
1120 &rdata
, &num_rdata
);
1121 if (!NT_STATUS_IS_OK(status
)) {
1126 *create_time
= interpret_long_date((char *)rdata
+0);
1129 *access_time
= interpret_long_date((char *)rdata
+8);
1132 *write_time
= interpret_long_date((char *)rdata
+16);
1135 *change_time
= interpret_long_date((char *)rdata
+24);
1138 *mode
= SVAL(rdata
, 32);
1141 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
1144 *ino
= IVAL(rdata
, 64);
1148 return NT_STATUS_OK
;
1151 /****************************************************************************
1152 Send a qpathinfo BASIC_INFO call.
1153 ****************************************************************************/
1155 struct cli_qpathinfo_basic_state
{
1160 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
);
1162 struct tevent_req
*cli_qpathinfo_basic_send(TALLOC_CTX
*mem_ctx
,
1163 struct event_context
*ev
,
1164 struct cli_state
*cli
,
1167 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1168 struct cli_qpathinfo_basic_state
*state
= NULL
;
1170 req
= tevent_req_create(mem_ctx
, &state
,
1171 struct cli_qpathinfo_basic_state
);
1175 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
1176 SMB_QUERY_FILE_BASIC_INFO
,
1178 if (tevent_req_nomem(subreq
, req
)) {
1179 return tevent_req_post(req
, ev
);
1181 tevent_req_set_callback(subreq
, cli_qpathinfo_basic_done
, req
);
1185 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
)
1187 struct tevent_req
*req
= tevent_req_callback_data(
1188 subreq
, struct tevent_req
);
1189 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1190 req
, struct cli_qpathinfo_basic_state
);
1193 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
1195 TALLOC_FREE(subreq
);
1196 if (!NT_STATUS_IS_OK(status
)) {
1197 tevent_req_nterror(req
, status
);
1200 tevent_req_done(req
);
1203 NTSTATUS
cli_qpathinfo_basic_recv(struct tevent_req
*req
,
1204 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1206 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1207 req
, struct cli_qpathinfo_basic_state
);
1210 if (tevent_req_is_nterror(req
, &status
)) {
1214 sbuf
->st_ex_atime
= interpret_long_date((char *)state
->data
+8);
1215 sbuf
->st_ex_mtime
= interpret_long_date((char *)state
->data
+16);
1216 sbuf
->st_ex_ctime
= interpret_long_date((char *)state
->data
+24);
1217 *attributes
= IVAL(state
->data
, 32);
1218 return NT_STATUS_OK
;
1221 NTSTATUS
cli_qpathinfo_basic(struct cli_state
*cli
, const char *name
,
1222 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1224 TALLOC_CTX
*frame
= talloc_stackframe();
1225 struct event_context
*ev
;
1226 struct tevent_req
*req
;
1227 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1229 if (cli_has_async_calls(cli
)) {
1231 * Can't use sync call while an async call is in flight
1233 status
= NT_STATUS_INVALID_PARAMETER
;
1236 ev
= event_context_init(frame
);
1240 req
= cli_qpathinfo_basic_send(frame
, ev
, cli
, name
);
1244 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1247 status
= cli_qpathinfo_basic_recv(req
, sbuf
, attributes
);
1250 if (!NT_STATUS_IS_OK(status
)) {
1251 cli_set_error(cli
, status
);
1256 /****************************************************************************
1257 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1258 ****************************************************************************/
1260 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
1265 char *converted
= NULL
;
1266 size_t converted_size
= 0;
1269 status
= cli_qpathinfo(talloc_tos(), cli
, fname
,
1270 SMB_QUERY_FILE_ALT_NAME_INFO
,
1271 4, cli
->max_xmit
, &rdata
, &num_rdata
);
1272 if (!NT_STATUS_IS_OK(status
)) {
1276 len
= IVAL(rdata
, 0);
1278 if (len
> num_rdata
- 4) {
1279 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1282 /* The returned data is a pushed string, not raw data. */
1283 if (!convert_string_talloc(talloc_tos(),
1284 cli_ucs2(cli
) ? CH_UTF16LE
: CH_DOS
,
1291 return NT_STATUS_NO_MEMORY
;
1293 fstrcpy(alt_name
, converted
);
1295 TALLOC_FREE(converted
);
1298 return NT_STATUS_OK
;