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"
28 #define PIPE_LANMAN "\\PIPE\\LANMAN"
30 /****************************************************************************
32 ****************************************************************************/
34 bool cli_api(struct cli_state
*cli
,
35 char *param
, int prcnt
, int mprcnt
,
36 char *data
, int drcnt
, int mdrcnt
,
37 char **rparam
, unsigned int *rprcnt
,
38 char **rdata
, unsigned int *rdrcnt
)
42 uint8_t *my_rparam
, *my_rdata
;
43 uint32_t num_my_rparam
, num_my_rdata
;
45 status
= cli_trans(talloc_tos(), cli
, SMBtrans
,
46 PIPE_LANMAN
, 0, /* name, fid */
47 0, 0, /* function, flags */
48 NULL
, 0, 0, /* setup */
49 (uint8_t *)param
, prcnt
, mprcnt
, /* Params, length, max */
50 (uint8_t *)data
, drcnt
, mdrcnt
, /* Data, length, max */
51 NULL
, /* recv_flags2 */
52 NULL
, 0, NULL
, /* rsetup */
53 &my_rparam
, 0, &num_my_rparam
,
54 &my_rdata
, 0, &num_my_rdata
);
55 if (!NT_STATUS_IS_OK(status
)) {
60 * I know this memcpy massively hurts, but there are just tons
61 * of callers of cli_api that eventually need changing to
65 *rparam
= (char *)memdup(my_rparam
, num_my_rparam
);
66 if (*rparam
== NULL
) {
69 *rprcnt
= num_my_rparam
;
70 TALLOC_FREE(my_rparam
);
72 *rdata
= (char *)memdup(my_rdata
, num_my_rdata
);
76 *rdrcnt
= num_my_rdata
;
77 TALLOC_FREE(my_rdata
);
81 TALLOC_FREE(my_rdata
);
82 TALLOC_FREE(my_rparam
);
90 /****************************************************************************
91 Perform a NetWkstaUserLogon.
92 ****************************************************************************/
94 bool cli_NetWkstaUserLogon(struct cli_state
*cli
,char *user
, char *workstation
)
99 unsigned int rdrcnt
,rprcnt
;
102 memset(param
, 0, sizeof(param
));
104 /* send a SMBtrans command with api NetWkstaUserLogon */
106 SSVAL(p
,0,132); /* api number */
108 strlcpy(p
,"OOWb54WrLh",sizeof(param
)-PTR_DIFF(p
,param
));
109 p
= skip_string(param
,sizeof(param
),p
);
110 strlcpy(p
,"WB21BWDWWDDDDDDDzzzD",sizeof(param
)-PTR_DIFF(p
,param
));
111 p
= skip_string(param
,sizeof(param
),p
);
114 strlcpy(p
,user
,sizeof(param
)-PTR_DIFF(p
,param
));
120 strlcpy(p
, workstation
,sizeof(param
)-PTR_DIFF(p
,param
));
123 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
125 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
129 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
130 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
131 &rparam
, &rprcnt
, /* return params, return size */
132 &rdata
, &rdrcnt
/* return data, return size */
134 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
137 if (cli
->rap_error
== 0) {
138 DEBUG(4,("NetWkstaUserLogon success\n"));
139 cli
->privileges
= SVAL(p
, 24);
140 /* The cli->eff_name field used to be set here
141 but it wasn't used anywhere else. */
143 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
149 return (cli
->rap_error
== 0);
152 /****************************************************************************
153 Call a NetShareEnum - try and browse available connections on a host.
154 ****************************************************************************/
156 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32
, const char *, void *), void *state
)
161 unsigned int rdrcnt
,rprcnt
;
165 /* now send a SMBtrans command with api RNetShareEnum */
167 SSVAL(p
,0,0); /* api number */
169 strlcpy(p
,"WrLeh",sizeof(param
)-PTR_DIFF(p
,param
));
170 p
= skip_string(param
,sizeof(param
),p
);
171 strlcpy(p
,"B13BWz",sizeof(param
)-PTR_DIFF(p
,param
));
172 p
= skip_string(param
,sizeof(param
),p
);
175 * Win2k needs a *smaller* buffer than 0xFFFF here -
176 * it returns "out of server memory" with 0xFFFF !!! JRA.
182 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
183 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
184 &rparam
, &rprcnt
, /* return params, length */
185 &rdata
, &rdrcnt
)) /* return data, length */
187 int res
= rparam
? SVAL(rparam
,0) : -1;
189 if (res
== 0 || res
== ERRmoredata
) {
190 int converter
=SVAL(rparam
,2);
192 char *rdata_end
= rdata
+ rdrcnt
;
194 count
=SVAL(rparam
,4);
197 for (i
=0;i
<count
;i
++,p
+=20) {
205 TALLOC_CTX
*frame
= talloc_stackframe();
207 if (p
+ 20 > rdata_end
) {
214 comment_offset
= (IVAL(p
,16) & 0xFFFF) - converter
;
215 if (comment_offset
< 0 ||
216 comment_offset
> (int)rdrcnt
) {
220 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
222 /* Work out the comment length. */
223 for (p1
= cmnt
, len
= 0; *p1
&&
224 p1
< rdata_end
; len
++)
229 pull_string_talloc(frame
,rdata
,0,
230 &s1
,sname
,14,STR_ASCII
);
231 pull_string_talloc(frame
,rdata
,0,
232 &s2
,cmnt
,len
,STR_ASCII
);
238 fn(s1
, type
, s2
, state
);
243 DEBUG(4,("NetShareEnum res=%d\n", res
));
246 DEBUG(4,("NetShareEnum failed\n"));
255 /****************************************************************************
256 Call a NetServerEnum for the specified workgroup and servertype mask. This
257 function then calls the specified callback function for each name returned.
259 The callback function takes 4 arguments: the machine name, the server type,
260 the comment and a state pointer.
261 ****************************************************************************/
263 bool cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32 stype
,
264 void (*fn
)(const char *, uint32
, const char *, void *),
269 char *rdata_end
= NULL
;
270 unsigned int rdrcnt
,rprcnt
;
275 uint32 func
= RAP_NetServerEnum2
;
276 char *last_entry
= NULL
;
281 errno
= 0; /* reset */
284 * This may take more than one transaction, so we should loop until
285 * we no longer get a more data to process or we have all of the
289 /* send a SMBtrans command with api NetServerEnum */
291 SIVAL(p
,0,func
); /* api number */
294 if (func
== RAP_NetServerEnum3
) {
295 strlcpy(p
,"WrLehDzz", sizeof(param
)-PTR_DIFF(p
,param
));
297 strlcpy(p
,"WrLehDz", sizeof(param
)-PTR_DIFF(p
,param
));
300 p
= skip_string(param
, sizeof(param
), p
);
301 strlcpy(p
,"B16BBDz", sizeof(param
)-PTR_DIFF(p
,param
));
303 p
= skip_string(param
, sizeof(param
), p
);
305 SSVAL(p
,2,CLI_BUFFER_SIZE
);
310 /* If we have more data, tell the server where
315 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
316 STR_TERMINATE
|STR_UPPER
);
318 if (len
== (size_t)-1) {
319 SAFE_FREE(last_entry
);
324 if (func
== RAP_NetServerEnum3
) {
326 last_entry
? last_entry
: "",
327 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
330 if (len
== (size_t)-1) {
331 SAFE_FREE(last_entry
);
337 /* Next time through we need to use the continue api */
338 func
= RAP_NetServerEnum3
;
341 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
342 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
343 &rparam
, &rprcnt
, /* return params, return size */
344 &rdata
, &rdrcnt
)) { /* return data, return size */
346 /* break out of the loop on error */
351 rdata_end
= rdata
+ rdrcnt
;
352 res
= rparam
? SVAL(rparam
,0) : -1;
354 if (res
== 0 || res
== ERRmoredata
||
355 (res
!= -1 && cli_errno(cli
) == 0)) {
358 int converter
=SVAL(rparam
,2);
360 /* Get the number of items returned in this buffer */
361 count
= SVAL(rparam
, 4);
363 /* The next field contains the number of items left,
364 * including those returned in this buffer. So the
365 * first time through this should contain all of the
368 if (total_cnt
== 0) {
369 total_cnt
= SVAL(rparam
, 6);
372 /* Keep track of how many we have read */
376 /* The last name in the previous NetServerEnum reply is
377 * sent back to server in the NetServerEnum3 request
378 * (last_entry). The next reply should repeat this entry
379 * as the first element. We have no proof that this is
380 * always true, but from traces that seems to be the
381 * behavior from Window Servers. So first lets do a lot
382 * of checking, just being paranoid. If the string
383 * matches then we already saw this entry so skip it.
385 * NOTE: sv1_name field must be null terminated and has
386 * a max size of 16 (NetBIOS Name).
388 if (last_entry
&& count
&& p
&&
389 (strncmp(last_entry
, p
, 16) == 0)) {
390 count
-= 1; /* Skip this entry */
391 return_cnt
= -1; /* Not part of total, so don't count. */
392 p
= rdata
+ 26; /* Skip the whole record */
395 for (i
= 0; i
< count
; i
++, p
+= 26) {
400 TALLOC_CTX
*frame
= talloc_stackframe();
401 uint32_t entry_stype
;
403 if (p
+ 26 > rdata_end
) {
409 comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
410 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
412 if (comment_offset
< 0 || comment_offset
>= (int)rdrcnt
) {
417 /* Work out the comment length. */
418 for (p1
= cmnt
, len
= 0; *p1
&&
419 p1
< rdata_end
; len
++)
425 entry_stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
427 pull_string_talloc(frame
,rdata
,0,
428 &s1
,sname
,16,STR_ASCII
);
429 pull_string_talloc(frame
,rdata
,0,
430 &s2
,cmnt
,len
,STR_ASCII
);
437 fn(s1
, entry_stype
, s2
, state
);
441 /* We are done with the old last entry, so now we can free it */
443 SAFE_FREE(last_entry
); /* This will set it to null */
446 /* We always make a copy of the last entry if we have one */
448 last_entry
= smb_xstrdup(sname
);
451 /* If we have more data, but no last entry then error out */
452 if (!last_entry
&& (res
== ERRmoredata
)) {
461 } while ((res
== ERRmoredata
) && (total_cnt
> return_cnt
));
465 SAFE_FREE(last_entry
);
468 errno
= cli_errno(cli
);
471 /* this is a very special case, when the domain master for the
472 work group isn't part of the work group itself, there is something
478 return(return_cnt
> 0);
481 /****************************************************************************
482 Send a SamOEMChangePassword command.
483 ****************************************************************************/
485 bool cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
486 const char *old_password
)
489 unsigned char data
[532];
491 unsigned char old_pw_hash
[16];
492 unsigned char new_pw_hash
[16];
493 unsigned int data_len
;
494 unsigned int param_len
= 0;
497 unsigned int rprcnt
, rdrcnt
;
499 if (strlen(user
) >= sizeof(fstring
)-1) {
500 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
504 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
506 strlcpy(p
, "zsT", sizeof(param
)-PTR_DIFF(p
,param
));
507 p
= skip_string(param
,sizeof(param
),p
);
508 strlcpy(p
, "B516B16", sizeof(param
)-PTR_DIFF(p
,param
));
509 p
= skip_string(param
,sizeof(param
),p
);
510 strlcpy(p
,user
, sizeof(param
)-PTR_DIFF(p
,param
));
511 p
= skip_string(param
,sizeof(param
),p
);
515 param_len
= PTR_DIFF(p
,param
);
518 * Get the Lanman hash of the old password, we
519 * use this as the key to make_oem_passwd_hash().
521 E_deshash(old_password
, old_pw_hash
);
523 encode_pw_buffer(data
, new_password
, STR_ASCII
);
525 #ifdef DEBUG_PASSWORD
526 DEBUG(100,("make_oem_passwd_hash\n"));
527 dump_data(100, data
, 516);
529 arcfour_crypt( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
532 * Now place the old password hash in the data.
534 E_deshash(new_password
, new_pw_hash
);
536 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
541 param
, param_len
, 4, /* param, length, max */
542 (char *)data
, data_len
, 0, /* data, length, max */
545 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
551 cli
->rap_error
= SVAL(rparam
,0);
557 return (cli
->rap_error
== 0);
560 /****************************************************************************
561 Send a qpathinfo call.
562 ****************************************************************************/
564 struct cli_qpathinfo1_state
{
565 struct cli_state
*cli
;
570 static void cli_qpathinfo1_done(struct tevent_req
*subreq
);
572 struct tevent_req
*cli_qpathinfo1_send(TALLOC_CTX
*mem_ctx
,
573 struct event_context
*ev
,
574 struct cli_state
*cli
,
577 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
578 struct cli_qpathinfo1_state
*state
= NULL
;
580 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo1_state
);
585 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_INFO_STANDARD
,
587 if (tevent_req_nomem(subreq
, req
)) {
588 return tevent_req_post(req
, ev
);
590 tevent_req_set_callback(subreq
, cli_qpathinfo1_done
, req
);
594 static void cli_qpathinfo1_done(struct tevent_req
*subreq
)
596 struct tevent_req
*req
= tevent_req_callback_data(
597 subreq
, struct tevent_req
);
598 struct cli_qpathinfo1_state
*state
= tevent_req_data(
599 req
, struct cli_qpathinfo1_state
);
602 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
605 if (!NT_STATUS_IS_OK(status
)) {
606 tevent_req_nterror(req
, status
);
609 tevent_req_done(req
);
612 NTSTATUS
cli_qpathinfo1_recv(struct tevent_req
*req
,
619 struct cli_qpathinfo1_state
*state
= tevent_req_data(
620 req
, struct cli_qpathinfo1_state
);
623 time_t (*date_fn
)(const void *buf
, int serverzone
);
625 if (tevent_req_is_nterror(req
, &status
)) {
629 if (state
->cli
->win95
) {
630 date_fn
= make_unix_date
;
632 date_fn
= make_unix_date2
;
636 *change_time
= date_fn(state
->data
+0, state
->cli
->serverzone
);
639 *access_time
= date_fn(state
->data
+4, state
->cli
->serverzone
);
642 *write_time
= date_fn(state
->data
+8, state
->cli
->serverzone
);
645 *size
= IVAL(state
->data
, 12);
648 *mode
= SVAL(state
->data
, l1_attrFile
);
653 NTSTATUS
cli_qpathinfo1(struct cli_state
*cli
,
661 TALLOC_CTX
*frame
= talloc_stackframe();
662 struct event_context
*ev
;
663 struct tevent_req
*req
;
664 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
666 if (cli_has_async_calls(cli
)) {
668 * Can't use sync call while an async call is in flight
670 status
= NT_STATUS_INVALID_PARAMETER
;
673 ev
= event_context_init(frame
);
677 req
= cli_qpathinfo1_send(frame
, ev
, cli
, fname
);
681 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
684 status
= cli_qpathinfo1_recv(req
, change_time
, access_time
,
685 write_time
, size
, mode
);
688 if (!NT_STATUS_IS_OK(status
)) {
689 cli_set_error(cli
, status
);
694 /****************************************************************************
695 Send a setpathinfo call.
696 ****************************************************************************/
698 NTSTATUS
cli_setpathinfo_basic(struct cli_state
*cli
, const char *fname
,
705 unsigned int data_len
= 0;
712 * Add the create, last access, modification, and status change times
714 put_long_date(p
, create_time
);
717 put_long_date(p
, access_time
);
720 put_long_date(p
, write_time
);
723 put_long_date(p
, change_time
);
734 data_len
= PTR_DIFF(p
, data
);
736 return cli_setpathinfo(cli
, SMB_FILE_BASIC_INFORMATION
, fname
,
737 (uint8_t *)data
, data_len
);
740 /****************************************************************************
741 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
742 ****************************************************************************/
744 struct cli_qpathinfo2_state
{
749 static void cli_qpathinfo2_done(struct tevent_req
*subreq
);
751 struct tevent_req
*cli_qpathinfo2_send(TALLOC_CTX
*mem_ctx
,
752 struct event_context
*ev
,
753 struct cli_state
*cli
,
756 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
757 struct cli_qpathinfo2_state
*state
= NULL
;
759 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo2_state
);
763 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
764 SMB_QUERY_FILE_ALL_INFO
,
766 if (tevent_req_nomem(subreq
, req
)) {
767 return tevent_req_post(req
, ev
);
769 tevent_req_set_callback(subreq
, cli_qpathinfo2_done
, req
);
773 static void cli_qpathinfo2_done(struct tevent_req
*subreq
)
775 struct tevent_req
*req
= tevent_req_callback_data(
776 subreq
, struct tevent_req
);
777 struct cli_qpathinfo2_state
*state
= tevent_req_data(
778 req
, struct cli_qpathinfo2_state
);
781 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
784 if (!NT_STATUS_IS_OK(status
)) {
785 tevent_req_nterror(req
, status
);
788 tevent_req_done(req
);
791 NTSTATUS
cli_qpathinfo2_recv(struct tevent_req
*req
,
792 struct timespec
*create_time
,
793 struct timespec
*access_time
,
794 struct timespec
*write_time
,
795 struct timespec
*change_time
,
796 SMB_OFF_T
*size
, uint16
*mode
,
799 struct cli_qpathinfo2_state
*state
= tevent_req_data(
800 req
, struct cli_qpathinfo2_state
);
803 if (tevent_req_is_nterror(req
, &status
)) {
808 *create_time
= interpret_long_date((char *)state
->data
+0);
811 *access_time
= interpret_long_date((char *)state
->data
+8);
814 *write_time
= interpret_long_date((char *)state
->data
+16);
817 *change_time
= interpret_long_date((char *)state
->data
+24);
820 *mode
= SVAL(state
->data
, 32);
823 *size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,48);
826 *ino
= IVAL(state
->data
, 64);
831 NTSTATUS
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
832 struct timespec
*create_time
,
833 struct timespec
*access_time
,
834 struct timespec
*write_time
,
835 struct timespec
*change_time
,
836 SMB_OFF_T
*size
, uint16
*mode
,
839 TALLOC_CTX
*frame
= talloc_stackframe();
840 struct event_context
*ev
;
841 struct tevent_req
*req
;
842 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
844 if (cli_has_async_calls(cli
)) {
846 * Can't use sync call while an async call is in flight
848 status
= NT_STATUS_INVALID_PARAMETER
;
851 ev
= event_context_init(frame
);
855 req
= cli_qpathinfo2_send(frame
, ev
, cli
, fname
);
859 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
862 status
= cli_qpathinfo2_recv(req
, create_time
, access_time
,
863 write_time
, change_time
, size
, mode
, ino
);
866 if (!NT_STATUS_IS_OK(status
)) {
867 cli_set_error(cli
, status
);
872 /****************************************************************************
874 ****************************************************************************/
876 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *data
,
878 unsigned int *pnum_streams
,
879 struct stream_struct
**pstreams
);
881 struct cli_qpathinfo_streams_state
{
886 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
);
888 struct tevent_req
*cli_qpathinfo_streams_send(TALLOC_CTX
*mem_ctx
,
889 struct tevent_context
*ev
,
890 struct cli_state
*cli
,
893 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
894 struct cli_qpathinfo_streams_state
*state
= NULL
;
896 req
= tevent_req_create(mem_ctx
, &state
,
897 struct cli_qpathinfo_streams_state
);
901 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
902 SMB_FILE_STREAM_INFORMATION
,
904 if (tevent_req_nomem(subreq
, req
)) {
905 return tevent_req_post(req
, ev
);
907 tevent_req_set_callback(subreq
, cli_qpathinfo_streams_done
, req
);
911 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
)
913 struct tevent_req
*req
= tevent_req_callback_data(
914 subreq
, struct tevent_req
);
915 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
916 req
, struct cli_qpathinfo_streams_state
);
919 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
922 if (!NT_STATUS_IS_OK(status
)) {
923 tevent_req_nterror(req
, status
);
926 tevent_req_done(req
);
929 NTSTATUS
cli_qpathinfo_streams_recv(struct tevent_req
*req
,
931 unsigned int *pnum_streams
,
932 struct stream_struct
**pstreams
)
934 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
935 req
, struct cli_qpathinfo_streams_state
);
938 if (tevent_req_is_nterror(req
, &status
)) {
941 if (!parse_streams_blob(mem_ctx
, state
->data
, state
->num_data
,
942 pnum_streams
, pstreams
)) {
943 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
948 NTSTATUS
cli_qpathinfo_streams(struct cli_state
*cli
, const char *fname
,
950 unsigned int *pnum_streams
,
951 struct stream_struct
**pstreams
)
953 TALLOC_CTX
*frame
= talloc_stackframe();
954 struct event_context
*ev
;
955 struct tevent_req
*req
;
956 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
958 if (cli_has_async_calls(cli
)) {
960 * Can't use sync call while an async call is in flight
962 status
= NT_STATUS_INVALID_PARAMETER
;
965 ev
= event_context_init(frame
);
969 req
= cli_qpathinfo_streams_send(frame
, ev
, cli
, fname
);
973 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
976 status
= cli_qpathinfo_streams_recv(req
, mem_ctx
, pnum_streams
,
980 if (!NT_STATUS_IS_OK(status
)) {
981 cli_set_error(cli
, status
);
986 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *rdata
,
988 unsigned int *pnum_streams
,
989 struct stream_struct
**pstreams
)
991 unsigned int num_streams
;
992 struct stream_struct
*streams
;
999 while ((data_len
> ofs
) && (data_len
- ofs
>= 24)) {
1003 struct stream_struct
*tmp
;
1006 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, streams
,
1007 struct stream_struct
,
1015 nlen
= IVAL(rdata
, ofs
+ 0x04);
1017 streams
[num_streams
].size
= IVAL_TO_SMB_OFF_T(
1019 streams
[num_streams
].alloc_size
= IVAL_TO_SMB_OFF_T(
1022 if (nlen
> data_len
- (ofs
+ 24)) {
1027 * We need to null-terminate src, how do I do this with
1028 * convert_string_talloc??
1031 tmp_buf
= TALLOC_ARRAY(streams
, uint8_t, nlen
+2);
1032 if (tmp_buf
== NULL
) {
1036 memcpy(tmp_buf
, rdata
+ofs
+24, nlen
);
1038 tmp_buf
[nlen
+1] = 0;
1040 if (!convert_string_talloc(streams
, CH_UTF16
, CH_UNIX
, tmp_buf
,
1041 nlen
+2, &vstr
, &size
, false))
1043 TALLOC_FREE(tmp_buf
);
1047 TALLOC_FREE(tmp_buf
);
1048 streams
[num_streams
].name
= (char *)vstr
;
1051 len
= IVAL(rdata
, ofs
);
1052 if (len
> data_len
- ofs
) {
1055 if (len
== 0) break;
1059 *pnum_streams
= num_streams
;
1060 *pstreams
= streams
;
1064 TALLOC_FREE(streams
);
1068 /****************************************************************************
1069 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1070 ****************************************************************************/
1072 NTSTATUS
cli_qfilename(struct cli_state
*cli
, uint16_t fnum
, char *name
,
1079 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1080 SMB_QUERY_FILE_NAME_INFO
,
1082 &rdata
, &num_rdata
);
1083 if (!NT_STATUS_IS_OK(status
)) {
1087 clistr_pull(cli
->inbuf
, name
, rdata
+4, namelen
, IVAL(rdata
, 0),
1090 return NT_STATUS_OK
;
1093 /****************************************************************************
1094 Send a qfileinfo call.
1095 ****************************************************************************/
1097 NTSTATUS
cli_qfileinfo_basic(struct cli_state
*cli
, uint16_t fnum
,
1098 uint16
*mode
, SMB_OFF_T
*size
,
1099 struct timespec
*create_time
,
1100 struct timespec
*access_time
,
1101 struct timespec
*write_time
,
1102 struct timespec
*change_time
,
1109 /* if its a win95 server then fail this - win95 totally screws it
1112 return NT_STATUS_NOT_SUPPORTED
;
1115 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1116 SMB_QUERY_FILE_ALL_INFO
,
1117 68, MIN(cli
->max_xmit
, 0xffff),
1118 &rdata
, &num_rdata
);
1119 if (!NT_STATUS_IS_OK(status
)) {
1124 *create_time
= interpret_long_date((char *)rdata
+0);
1127 *access_time
= interpret_long_date((char *)rdata
+8);
1130 *write_time
= interpret_long_date((char *)rdata
+16);
1133 *change_time
= interpret_long_date((char *)rdata
+24);
1136 *mode
= SVAL(rdata
, 32);
1139 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
1142 *ino
= IVAL(rdata
, 64);
1146 return NT_STATUS_OK
;
1149 /****************************************************************************
1150 Send a qpathinfo BASIC_INFO call.
1151 ****************************************************************************/
1153 struct cli_qpathinfo_basic_state
{
1158 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
);
1160 struct tevent_req
*cli_qpathinfo_basic_send(TALLOC_CTX
*mem_ctx
,
1161 struct event_context
*ev
,
1162 struct cli_state
*cli
,
1165 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1166 struct cli_qpathinfo_basic_state
*state
= NULL
;
1168 req
= tevent_req_create(mem_ctx
, &state
,
1169 struct cli_qpathinfo_basic_state
);
1173 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
1174 SMB_QUERY_FILE_BASIC_INFO
,
1176 if (tevent_req_nomem(subreq
, req
)) {
1177 return tevent_req_post(req
, ev
);
1179 tevent_req_set_callback(subreq
, cli_qpathinfo_basic_done
, req
);
1183 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
)
1185 struct tevent_req
*req
= tevent_req_callback_data(
1186 subreq
, struct tevent_req
);
1187 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1188 req
, struct cli_qpathinfo_basic_state
);
1191 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
1193 TALLOC_FREE(subreq
);
1194 if (!NT_STATUS_IS_OK(status
)) {
1195 tevent_req_nterror(req
, status
);
1198 tevent_req_done(req
);
1201 NTSTATUS
cli_qpathinfo_basic_recv(struct tevent_req
*req
,
1202 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1204 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1205 req
, struct cli_qpathinfo_basic_state
);
1208 if (tevent_req_is_nterror(req
, &status
)) {
1212 sbuf
->st_ex_atime
= interpret_long_date((char *)state
->data
+8);
1213 sbuf
->st_ex_mtime
= interpret_long_date((char *)state
->data
+16);
1214 sbuf
->st_ex_ctime
= interpret_long_date((char *)state
->data
+24);
1215 *attributes
= IVAL(state
->data
, 32);
1216 return NT_STATUS_OK
;
1219 NTSTATUS
cli_qpathinfo_basic(struct cli_state
*cli
, const char *name
,
1220 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1222 TALLOC_CTX
*frame
= talloc_stackframe();
1223 struct event_context
*ev
;
1224 struct tevent_req
*req
;
1225 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1227 if (cli_has_async_calls(cli
)) {
1229 * Can't use sync call while an async call is in flight
1231 status
= NT_STATUS_INVALID_PARAMETER
;
1234 ev
= event_context_init(frame
);
1238 req
= cli_qpathinfo_basic_send(frame
, ev
, cli
, name
);
1242 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1245 status
= cli_qpathinfo_basic_recv(req
, sbuf
, attributes
);
1248 if (!NT_STATUS_IS_OK(status
)) {
1249 cli_set_error(cli
, status
);
1254 /****************************************************************************
1255 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1256 ****************************************************************************/
1258 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
1263 char *converted
= NULL
;
1264 size_t converted_size
= 0;
1267 status
= cli_qpathinfo(talloc_tos(), cli
, fname
,
1268 SMB_QUERY_FILE_ALT_NAME_INFO
,
1269 4, cli
->max_xmit
, &rdata
, &num_rdata
);
1270 if (!NT_STATUS_IS_OK(status
)) {
1274 len
= IVAL(rdata
, 0);
1276 if (len
> num_rdata
- 4) {
1277 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1280 /* The returned data is a pushed string, not raw data. */
1281 if (!convert_string_talloc(talloc_tos(),
1282 cli_ucs2(cli
) ? CH_UTF16LE
: CH_DOS
,
1289 return NT_STATUS_NO_MEMORY
;
1291 fstrcpy(alt_name
, converted
);
1293 TALLOC_FREE(converted
);
1296 return NT_STATUS_OK
;