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"
32 #define PIPE_LANMAN "\\PIPE\\LANMAN"
34 /****************************************************************************
36 ****************************************************************************/
38 bool cli_api(struct cli_state
*cli
,
39 char *param
, int prcnt
, int mprcnt
,
40 char *data
, int drcnt
, int mdrcnt
,
41 char **rparam
, unsigned int *rprcnt
,
42 char **rdata
, unsigned int *rdrcnt
)
46 uint8_t *my_rparam
, *my_rdata
;
47 uint32_t num_my_rparam
, num_my_rdata
;
49 status
= cli_trans(talloc_tos(), cli
, SMBtrans
,
50 PIPE_LANMAN
, 0, /* name, fid */
51 0, 0, /* function, flags */
52 NULL
, 0, 0, /* setup */
53 (uint8_t *)param
, prcnt
, mprcnt
, /* Params, length, max */
54 (uint8_t *)data
, drcnt
, mdrcnt
, /* Data, length, max */
55 NULL
, /* recv_flags2 */
56 NULL
, 0, NULL
, /* rsetup */
57 &my_rparam
, 0, &num_my_rparam
,
58 &my_rdata
, 0, &num_my_rdata
);
59 if (!NT_STATUS_IS_OK(status
)) {
64 * I know this memcpy massively hurts, but there are just tons
65 * of callers of cli_api that eventually need changing to
69 *rparam
= (char *)memdup(my_rparam
, num_my_rparam
);
70 if (*rparam
== NULL
) {
73 *rprcnt
= num_my_rparam
;
74 TALLOC_FREE(my_rparam
);
76 *rdata
= (char *)memdup(my_rdata
, num_my_rdata
);
80 *rdrcnt
= num_my_rdata
;
81 TALLOC_FREE(my_rdata
);
85 TALLOC_FREE(my_rdata
);
86 TALLOC_FREE(my_rparam
);
94 /****************************************************************************
95 Perform a NetWkstaUserLogon.
96 ****************************************************************************/
98 bool cli_NetWkstaUserLogon(struct cli_state
*cli
,char *user
, char *workstation
)
103 unsigned int rdrcnt
,rprcnt
;
106 memset(param
, 0, sizeof(param
));
108 /* send a SMBtrans command with api NetWkstaUserLogon */
110 SSVAL(p
,0,132); /* api number */
112 strlcpy(p
,"OOWb54WrLh",sizeof(param
)-PTR_DIFF(p
,param
));
113 p
= skip_string(param
,sizeof(param
),p
);
114 strlcpy(p
,"WB21BWDWWDDDDDDDzzzD",sizeof(param
)-PTR_DIFF(p
,param
));
115 p
= skip_string(param
,sizeof(param
),p
);
118 strlcpy(p
,user
,sizeof(param
)-PTR_DIFF(p
,param
));
124 strlcpy(p
, workstation
,sizeof(param
)-PTR_DIFF(p
,param
));
127 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
129 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
133 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
134 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
135 &rparam
, &rprcnt
, /* return params, return size */
136 &rdata
, &rdrcnt
/* return data, return size */
138 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
141 if (cli
->rap_error
== 0) {
142 DEBUG(4,("NetWkstaUserLogon success\n"));
144 * The cli->privileges = SVAL(p, 24); field was set here
145 * but it was not use anywhere else.
147 /* The cli->eff_name field used to be set here
148 but it wasn't used anywhere else. */
150 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
156 return (cli
->rap_error
== 0);
159 /****************************************************************************
160 Call a NetShareEnum - try and browse available connections on a host.
161 ****************************************************************************/
163 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32
, const char *, void *), void *state
)
168 unsigned int rdrcnt
,rprcnt
;
172 /* now send a SMBtrans command with api RNetShareEnum */
174 SSVAL(p
,0,0); /* api number */
176 strlcpy(p
,"WrLeh",sizeof(param
)-PTR_DIFF(p
,param
));
177 p
= skip_string(param
,sizeof(param
),p
);
178 strlcpy(p
,"B13BWz",sizeof(param
)-PTR_DIFF(p
,param
));
179 p
= skip_string(param
,sizeof(param
),p
);
182 * Win2k needs a *smaller* buffer than 0xFFFF here -
183 * it returns "out of server memory" with 0xFFFF !!! JRA.
189 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
190 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
191 &rparam
, &rprcnt
, /* return params, length */
192 &rdata
, &rdrcnt
)) /* return data, length */
194 int res
= rparam
? SVAL(rparam
,0) : -1;
196 if (res
== 0 || res
== ERRmoredata
) {
197 int converter
=SVAL(rparam
,2);
199 char *rdata_end
= rdata
+ rdrcnt
;
201 count
=SVAL(rparam
,4);
204 for (i
=0;i
<count
;i
++,p
+=20) {
212 TALLOC_CTX
*frame
= talloc_stackframe();
214 if (p
+ 20 > rdata_end
) {
221 comment_offset
= (IVAL(p
,16) & 0xFFFF) - converter
;
222 if (comment_offset
< 0 ||
223 comment_offset
> (int)rdrcnt
) {
227 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
229 /* Work out the comment length. */
230 for (p1
= cmnt
, len
= 0; *p1
&&
231 p1
< rdata_end
; len
++)
236 pull_string_talloc(frame
,rdata
,0,
237 &s1
,sname
,14,STR_ASCII
);
238 pull_string_talloc(frame
,rdata
,0,
239 &s2
,cmnt
,len
,STR_ASCII
);
245 fn(s1
, type
, s2
, state
);
250 DEBUG(4,("NetShareEnum res=%d\n", res
));
253 DEBUG(4,("NetShareEnum failed\n"));
262 /****************************************************************************
263 Call a NetServerEnum for the specified workgroup and servertype mask. This
264 function then calls the specified callback function for each name returned.
266 The callback function takes 4 arguments: the machine name, the server type,
267 the comment and a state pointer.
268 ****************************************************************************/
270 bool cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32 stype
,
271 void (*fn
)(const char *, uint32
, const char *, void *),
276 char *rdata_end
= NULL
;
277 unsigned int rdrcnt
,rprcnt
;
282 uint32 func
= RAP_NetServerEnum2
;
283 char *last_entry
= NULL
;
288 errno
= 0; /* reset */
291 * This may take more than one transaction, so we should loop until
292 * we no longer get a more data to process or we have all of the
296 /* send a SMBtrans command with api NetServerEnum */
298 SIVAL(p
,0,func
); /* api number */
301 if (func
== RAP_NetServerEnum3
) {
302 strlcpy(p
,"WrLehDzz", sizeof(param
)-PTR_DIFF(p
,param
));
304 strlcpy(p
,"WrLehDz", sizeof(param
)-PTR_DIFF(p
,param
));
307 p
= skip_string(param
, sizeof(param
), p
);
308 strlcpy(p
,"B16BBDz", sizeof(param
)-PTR_DIFF(p
,param
));
310 p
= skip_string(param
, sizeof(param
), p
);
312 SSVAL(p
,2,CLI_BUFFER_SIZE
);
317 /* If we have more data, tell the server where
322 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
323 STR_TERMINATE
|STR_UPPER
);
325 if (len
== (size_t)-1) {
326 SAFE_FREE(last_entry
);
331 if (func
== RAP_NetServerEnum3
) {
333 last_entry
? last_entry
: "",
334 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
337 if (len
== (size_t)-1) {
338 SAFE_FREE(last_entry
);
344 /* Next time through we need to use the continue api */
345 func
= RAP_NetServerEnum3
;
348 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
349 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
350 &rparam
, &rprcnt
, /* return params, return size */
351 &rdata
, &rdrcnt
)) { /* return data, return size */
353 /* break out of the loop on error */
358 rdata_end
= rdata
+ rdrcnt
;
359 res
= rparam
? SVAL(rparam
,0) : -1;
361 if (res
== 0 || res
== ERRmoredata
||
362 (res
!= -1 && cli_errno(cli
) == 0)) {
365 int converter
=SVAL(rparam
,2);
367 /* Get the number of items returned in this buffer */
368 count
= SVAL(rparam
, 4);
370 /* The next field contains the number of items left,
371 * including those returned in this buffer. So the
372 * first time through this should contain all of the
375 if (total_cnt
== 0) {
376 total_cnt
= SVAL(rparam
, 6);
379 /* Keep track of how many we have read */
383 /* The last name in the previous NetServerEnum reply is
384 * sent back to server in the NetServerEnum3 request
385 * (last_entry). The next reply should repeat this entry
386 * as the first element. We have no proof that this is
387 * always true, but from traces that seems to be the
388 * behavior from Window Servers. So first lets do a lot
389 * of checking, just being paranoid. If the string
390 * matches then we already saw this entry so skip it.
392 * NOTE: sv1_name field must be null terminated and has
393 * a max size of 16 (NetBIOS Name).
395 if (last_entry
&& count
&& p
&&
396 (strncmp(last_entry
, p
, 16) == 0)) {
397 count
-= 1; /* Skip this entry */
398 return_cnt
= -1; /* Not part of total, so don't count. */
399 p
= rdata
+ 26; /* Skip the whole record */
402 for (i
= 0; i
< count
; i
++, p
+= 26) {
407 TALLOC_CTX
*frame
= talloc_stackframe();
408 uint32_t entry_stype
;
410 if (p
+ 26 > rdata_end
) {
416 comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
417 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
419 if (comment_offset
< 0 || comment_offset
>= (int)rdrcnt
) {
424 /* Work out the comment length. */
425 for (p1
= cmnt
, len
= 0; *p1
&&
426 p1
< rdata_end
; len
++)
432 entry_stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
434 pull_string_talloc(frame
,rdata
,0,
435 &s1
,sname
,16,STR_ASCII
);
436 pull_string_talloc(frame
,rdata
,0,
437 &s2
,cmnt
,len
,STR_ASCII
);
444 fn(s1
, entry_stype
, s2
, state
);
448 /* We are done with the old last entry, so now we can free it */
450 SAFE_FREE(last_entry
); /* This will set it to null */
453 /* We always make a copy of the last entry if we have one */
455 last_entry
= smb_xstrdup(sname
);
458 /* If we have more data, but no last entry then error out */
459 if (!last_entry
&& (res
== ERRmoredata
)) {
468 } while ((res
== ERRmoredata
) && (total_cnt
> return_cnt
));
472 SAFE_FREE(last_entry
);
475 errno
= cli_errno(cli
);
478 /* this is a very special case, when the domain master for the
479 work group isn't part of the work group itself, there is something
485 return(return_cnt
> 0);
488 /****************************************************************************
489 Send a SamOEMChangePassword command.
490 ****************************************************************************/
492 bool cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
493 const char *old_password
)
496 unsigned char data
[532];
498 unsigned char old_pw_hash
[16];
499 unsigned char new_pw_hash
[16];
500 unsigned int data_len
;
501 unsigned int param_len
= 0;
504 unsigned int rprcnt
, rdrcnt
;
506 if (strlen(user
) >= sizeof(fstring
)-1) {
507 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
511 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
513 strlcpy(p
, "zsT", sizeof(param
)-PTR_DIFF(p
,param
));
514 p
= skip_string(param
,sizeof(param
),p
);
515 strlcpy(p
, "B516B16", sizeof(param
)-PTR_DIFF(p
,param
));
516 p
= skip_string(param
,sizeof(param
),p
);
517 strlcpy(p
,user
, sizeof(param
)-PTR_DIFF(p
,param
));
518 p
= skip_string(param
,sizeof(param
),p
);
522 param_len
= PTR_DIFF(p
,param
);
525 * Get the Lanman hash of the old password, we
526 * use this as the key to make_oem_passwd_hash().
528 E_deshash(old_password
, old_pw_hash
);
530 encode_pw_buffer(data
, new_password
, STR_ASCII
);
532 #ifdef DEBUG_PASSWORD
533 DEBUG(100,("make_oem_passwd_hash\n"));
534 dump_data(100, data
, 516);
536 arcfour_crypt( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
539 * Now place the old password hash in the data.
541 E_deshash(new_password
, new_pw_hash
);
543 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
548 param
, param_len
, 4, /* param, length, max */
549 (char *)data
, data_len
, 0, /* data, length, max */
552 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
558 cli
->rap_error
= SVAL(rparam
,0);
564 return (cli
->rap_error
== 0);
567 /****************************************************************************
568 Send a qpathinfo call.
569 ****************************************************************************/
571 struct cli_qpathinfo1_state
{
572 struct cli_state
*cli
;
577 static void cli_qpathinfo1_done(struct tevent_req
*subreq
);
579 struct tevent_req
*cli_qpathinfo1_send(TALLOC_CTX
*mem_ctx
,
580 struct event_context
*ev
,
581 struct cli_state
*cli
,
584 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
585 struct cli_qpathinfo1_state
*state
= NULL
;
587 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo1_state
);
592 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_INFO_STANDARD
,
593 22, CLI_BUFFER_SIZE
);
594 if (tevent_req_nomem(subreq
, req
)) {
595 return tevent_req_post(req
, ev
);
597 tevent_req_set_callback(subreq
, cli_qpathinfo1_done
, req
);
601 static void cli_qpathinfo1_done(struct tevent_req
*subreq
)
603 struct tevent_req
*req
= tevent_req_callback_data(
604 subreq
, struct tevent_req
);
605 struct cli_qpathinfo1_state
*state
= tevent_req_data(
606 req
, struct cli_qpathinfo1_state
);
609 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
612 if (!NT_STATUS_IS_OK(status
)) {
613 tevent_req_nterror(req
, status
);
616 tevent_req_done(req
);
619 NTSTATUS
cli_qpathinfo1_recv(struct tevent_req
*req
,
626 struct cli_qpathinfo1_state
*state
= tevent_req_data(
627 req
, struct cli_qpathinfo1_state
);
630 time_t (*date_fn
)(const void *buf
, int serverzone
);
632 if (tevent_req_is_nterror(req
, &status
)) {
636 if (state
->cli
->win95
) {
637 date_fn
= make_unix_date
;
639 date_fn
= make_unix_date2
;
643 *change_time
= date_fn(state
->data
+0, cli_state_server_time_zone(state
->cli
));
646 *access_time
= date_fn(state
->data
+4, cli_state_server_time_zone(state
->cli
));
649 *write_time
= date_fn(state
->data
+8, cli_state_server_time_zone(state
->cli
));
652 *size
= IVAL(state
->data
, 12);
655 *mode
= SVAL(state
->data
, l1_attrFile
);
660 NTSTATUS
cli_qpathinfo1(struct cli_state
*cli
,
668 TALLOC_CTX
*frame
= talloc_stackframe();
669 struct event_context
*ev
;
670 struct tevent_req
*req
;
671 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
673 if (cli_has_async_calls(cli
)) {
675 * Can't use sync call while an async call is in flight
677 status
= NT_STATUS_INVALID_PARAMETER
;
680 ev
= event_context_init(frame
);
684 req
= cli_qpathinfo1_send(frame
, ev
, cli
, fname
);
688 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
691 status
= cli_qpathinfo1_recv(req
, change_time
, access_time
,
692 write_time
, size
, mode
);
698 /****************************************************************************
699 Send a setpathinfo call.
700 ****************************************************************************/
702 NTSTATUS
cli_setpathinfo_basic(struct cli_state
*cli
, const char *fname
,
709 unsigned int data_len
= 0;
716 * Add the create, last access, modification, and status change times
718 put_long_date(p
, create_time
);
721 put_long_date(p
, access_time
);
724 put_long_date(p
, write_time
);
727 put_long_date(p
, change_time
);
738 data_len
= PTR_DIFF(p
, data
);
740 return cli_setpathinfo(cli
, SMB_FILE_BASIC_INFORMATION
, fname
,
741 (uint8_t *)data
, data_len
);
744 /****************************************************************************
745 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
746 ****************************************************************************/
748 struct cli_qpathinfo2_state
{
753 static void cli_qpathinfo2_done(struct tevent_req
*subreq
);
755 struct tevent_req
*cli_qpathinfo2_send(TALLOC_CTX
*mem_ctx
,
756 struct event_context
*ev
,
757 struct cli_state
*cli
,
760 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
761 struct cli_qpathinfo2_state
*state
= NULL
;
763 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo2_state
);
767 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
768 SMB_QUERY_FILE_ALL_INFO
,
769 68, CLI_BUFFER_SIZE
);
770 if (tevent_req_nomem(subreq
, req
)) {
771 return tevent_req_post(req
, ev
);
773 tevent_req_set_callback(subreq
, cli_qpathinfo2_done
, req
);
777 static void cli_qpathinfo2_done(struct tevent_req
*subreq
)
779 struct tevent_req
*req
= tevent_req_callback_data(
780 subreq
, struct tevent_req
);
781 struct cli_qpathinfo2_state
*state
= tevent_req_data(
782 req
, struct cli_qpathinfo2_state
);
785 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
788 if (!NT_STATUS_IS_OK(status
)) {
789 tevent_req_nterror(req
, status
);
792 tevent_req_done(req
);
795 NTSTATUS
cli_qpathinfo2_recv(struct tevent_req
*req
,
796 struct timespec
*create_time
,
797 struct timespec
*access_time
,
798 struct timespec
*write_time
,
799 struct timespec
*change_time
,
800 SMB_OFF_T
*size
, uint16
*mode
,
803 struct cli_qpathinfo2_state
*state
= tevent_req_data(
804 req
, struct cli_qpathinfo2_state
);
807 if (tevent_req_is_nterror(req
, &status
)) {
812 *create_time
= interpret_long_date((char *)state
->data
+0);
815 *access_time
= interpret_long_date((char *)state
->data
+8);
818 *write_time
= interpret_long_date((char *)state
->data
+16);
821 *change_time
= interpret_long_date((char *)state
->data
+24);
824 *mode
= SVAL(state
->data
, 32);
827 *size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,48);
830 *ino
= IVAL(state
->data
, 64);
835 NTSTATUS
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
836 struct timespec
*create_time
,
837 struct timespec
*access_time
,
838 struct timespec
*write_time
,
839 struct timespec
*change_time
,
840 SMB_OFF_T
*size
, uint16
*mode
,
843 TALLOC_CTX
*frame
= talloc_stackframe();
844 struct event_context
*ev
;
845 struct tevent_req
*req
;
846 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
848 if (cli_has_async_calls(cli
)) {
850 * Can't use sync call while an async call is in flight
852 status
= NT_STATUS_INVALID_PARAMETER
;
855 ev
= event_context_init(frame
);
859 req
= cli_qpathinfo2_send(frame
, ev
, cli
, fname
);
863 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
866 status
= cli_qpathinfo2_recv(req
, create_time
, access_time
,
867 write_time
, change_time
, size
, mode
, ino
);
873 /****************************************************************************
875 ****************************************************************************/
877 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *data
,
879 unsigned int *pnum_streams
,
880 struct stream_struct
**pstreams
);
882 struct cli_qpathinfo_streams_state
{
887 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
);
889 struct tevent_req
*cli_qpathinfo_streams_send(TALLOC_CTX
*mem_ctx
,
890 struct tevent_context
*ev
,
891 struct cli_state
*cli
,
894 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
895 struct cli_qpathinfo_streams_state
*state
= NULL
;
897 req
= tevent_req_create(mem_ctx
, &state
,
898 struct cli_qpathinfo_streams_state
);
902 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
903 SMB_FILE_STREAM_INFORMATION
,
905 if (tevent_req_nomem(subreq
, req
)) {
906 return tevent_req_post(req
, ev
);
908 tevent_req_set_callback(subreq
, cli_qpathinfo_streams_done
, req
);
912 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
)
914 struct tevent_req
*req
= tevent_req_callback_data(
915 subreq
, struct tevent_req
);
916 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
917 req
, struct cli_qpathinfo_streams_state
);
920 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
923 if (!NT_STATUS_IS_OK(status
)) {
924 tevent_req_nterror(req
, status
);
927 tevent_req_done(req
);
930 NTSTATUS
cli_qpathinfo_streams_recv(struct tevent_req
*req
,
932 unsigned int *pnum_streams
,
933 struct stream_struct
**pstreams
)
935 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
936 req
, struct cli_qpathinfo_streams_state
);
939 if (tevent_req_is_nterror(req
, &status
)) {
942 if (!parse_streams_blob(mem_ctx
, state
->data
, state
->num_data
,
943 pnum_streams
, pstreams
)) {
944 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
949 NTSTATUS
cli_qpathinfo_streams(struct cli_state
*cli
, const char *fname
,
951 unsigned int *pnum_streams
,
952 struct stream_struct
**pstreams
)
954 TALLOC_CTX
*frame
= talloc_stackframe();
955 struct event_context
*ev
;
956 struct tevent_req
*req
;
957 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
959 if (cli_has_async_calls(cli
)) {
961 * Can't use sync call while an async call is in flight
963 status
= NT_STATUS_INVALID_PARAMETER
;
966 ev
= event_context_init(frame
);
970 req
= cli_qpathinfo_streams_send(frame
, ev
, cli
, fname
);
974 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
977 status
= cli_qpathinfo_streams_recv(req
, mem_ctx
, pnum_streams
,
984 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *rdata
,
986 unsigned int *pnum_streams
,
987 struct stream_struct
**pstreams
)
989 unsigned int num_streams
;
990 struct stream_struct
*streams
;
997 while ((data_len
> ofs
) && (data_len
- ofs
>= 24)) {
1001 struct stream_struct
*tmp
;
1004 tmp
= talloc_realloc(mem_ctx
, streams
,
1005 struct stream_struct
,
1013 nlen
= IVAL(rdata
, ofs
+ 0x04);
1015 streams
[num_streams
].size
= IVAL_TO_SMB_OFF_T(
1017 streams
[num_streams
].alloc_size
= IVAL_TO_SMB_OFF_T(
1020 if (nlen
> data_len
- (ofs
+ 24)) {
1025 * We need to null-terminate src, how do I do this with
1026 * convert_string_talloc??
1029 tmp_buf
= talloc_array(streams
, uint8_t, nlen
+2);
1030 if (tmp_buf
== NULL
) {
1034 memcpy(tmp_buf
, rdata
+ofs
+24, nlen
);
1036 tmp_buf
[nlen
+1] = 0;
1038 if (!convert_string_talloc(streams
, CH_UTF16
, CH_UNIX
, tmp_buf
,
1039 nlen
+2, &vstr
, &size
))
1041 TALLOC_FREE(tmp_buf
);
1045 TALLOC_FREE(tmp_buf
);
1046 streams
[num_streams
].name
= (char *)vstr
;
1049 len
= IVAL(rdata
, ofs
);
1050 if (len
> data_len
- ofs
) {
1053 if (len
== 0) break;
1057 *pnum_streams
= num_streams
;
1058 *pstreams
= streams
;
1062 TALLOC_FREE(streams
);
1066 /****************************************************************************
1067 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1068 ****************************************************************************/
1070 NTSTATUS
cli_qfilename(struct cli_state
*cli
, uint16_t fnum
,
1071 TALLOC_CTX
*mem_ctx
, char **_name
)
1073 uint16_t recv_flags2
;
1080 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1081 SMB_QUERY_FILE_NAME_INFO
,
1082 4, CLI_BUFFER_SIZE
, &recv_flags2
,
1083 &rdata
, &num_rdata
);
1084 if (!NT_STATUS_IS_OK(status
)) {
1088 namelen
= IVAL(rdata
, 0);
1089 if (namelen
> (num_rdata
- 4)) {
1091 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1094 clistr_pull_talloc(mem_ctx
,
1095 (const char *)rdata
,
1102 status
= map_nt_error_from_unix(errno
);
1109 return NT_STATUS_OK
;
1112 /****************************************************************************
1113 Send a qfileinfo call.
1114 ****************************************************************************/
1116 NTSTATUS
cli_qfileinfo_basic(struct cli_state
*cli
, uint16_t fnum
,
1117 uint16
*mode
, SMB_OFF_T
*size
,
1118 struct timespec
*create_time
,
1119 struct timespec
*access_time
,
1120 struct timespec
*write_time
,
1121 struct timespec
*change_time
,
1128 /* if its a win95 server then fail this - win95 totally screws it
1131 return NT_STATUS_NOT_SUPPORTED
;
1134 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1135 SMB_QUERY_FILE_ALL_INFO
,
1136 68, CLI_BUFFER_SIZE
,
1138 &rdata
, &num_rdata
);
1139 if (!NT_STATUS_IS_OK(status
)) {
1144 *create_time
= interpret_long_date((char *)rdata
+0);
1147 *access_time
= interpret_long_date((char *)rdata
+8);
1150 *write_time
= interpret_long_date((char *)rdata
+16);
1153 *change_time
= interpret_long_date((char *)rdata
+24);
1156 *mode
= SVAL(rdata
, 32);
1159 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
1162 *ino
= IVAL(rdata
, 64);
1166 return NT_STATUS_OK
;
1169 /****************************************************************************
1170 Send a qpathinfo BASIC_INFO call.
1171 ****************************************************************************/
1173 struct cli_qpathinfo_basic_state
{
1178 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
);
1180 struct tevent_req
*cli_qpathinfo_basic_send(TALLOC_CTX
*mem_ctx
,
1181 struct event_context
*ev
,
1182 struct cli_state
*cli
,
1185 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1186 struct cli_qpathinfo_basic_state
*state
= NULL
;
1188 req
= tevent_req_create(mem_ctx
, &state
,
1189 struct cli_qpathinfo_basic_state
);
1193 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
1194 SMB_QUERY_FILE_BASIC_INFO
,
1195 36, CLI_BUFFER_SIZE
);
1196 if (tevent_req_nomem(subreq
, req
)) {
1197 return tevent_req_post(req
, ev
);
1199 tevent_req_set_callback(subreq
, cli_qpathinfo_basic_done
, req
);
1203 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
)
1205 struct tevent_req
*req
= tevent_req_callback_data(
1206 subreq
, struct tevent_req
);
1207 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1208 req
, struct cli_qpathinfo_basic_state
);
1211 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
1213 TALLOC_FREE(subreq
);
1214 if (!NT_STATUS_IS_OK(status
)) {
1215 tevent_req_nterror(req
, status
);
1218 tevent_req_done(req
);
1221 NTSTATUS
cli_qpathinfo_basic_recv(struct tevent_req
*req
,
1222 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1224 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1225 req
, struct cli_qpathinfo_basic_state
);
1228 if (tevent_req_is_nterror(req
, &status
)) {
1232 sbuf
->st_ex_atime
= interpret_long_date((char *)state
->data
+8);
1233 sbuf
->st_ex_mtime
= interpret_long_date((char *)state
->data
+16);
1234 sbuf
->st_ex_ctime
= interpret_long_date((char *)state
->data
+24);
1235 *attributes
= IVAL(state
->data
, 32);
1236 return NT_STATUS_OK
;
1239 NTSTATUS
cli_qpathinfo_basic(struct cli_state
*cli
, const char *name
,
1240 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1242 TALLOC_CTX
*frame
= talloc_stackframe();
1243 struct event_context
*ev
;
1244 struct tevent_req
*req
;
1245 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1247 if (cli_has_async_calls(cli
)) {
1249 * Can't use sync call while an async call is in flight
1251 status
= NT_STATUS_INVALID_PARAMETER
;
1254 ev
= event_context_init(frame
);
1258 req
= cli_qpathinfo_basic_send(frame
, ev
, cli
, name
);
1262 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1265 status
= cli_qpathinfo_basic_recv(req
, sbuf
, attributes
);
1271 /****************************************************************************
1272 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1273 ****************************************************************************/
1275 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
1280 char *converted
= NULL
;
1281 size_t converted_size
= 0;
1284 status
= cli_qpathinfo(talloc_tos(), cli
, fname
,
1285 SMB_QUERY_FILE_ALT_NAME_INFO
,
1286 4, CLI_BUFFER_SIZE
, &rdata
, &num_rdata
);
1287 if (!NT_STATUS_IS_OK(status
)) {
1291 len
= IVAL(rdata
, 0);
1293 if (len
> num_rdata
- 4) {
1294 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1297 /* The returned data is a pushed string, not raw data. */
1298 if (!convert_string_talloc(talloc_tos(),
1299 cli_ucs2(cli
) ? CH_UTF16LE
: CH_DOS
,
1305 return NT_STATUS_NO_MEMORY
;
1307 fstrcpy(alt_name
, converted
);
1309 TALLOC_FREE(converted
);
1312 return NT_STATUS_OK
;