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/clirap.h"
31 #define PIPE_LANMAN "\\PIPE\\LANMAN"
33 /****************************************************************************
35 ****************************************************************************/
37 bool cli_api(struct cli_state
*cli
,
38 char *param
, int prcnt
, int mprcnt
,
39 char *data
, int drcnt
, int mdrcnt
,
40 char **rparam
, unsigned int *rprcnt
,
41 char **rdata
, unsigned int *rdrcnt
)
45 uint8_t *my_rparam
, *my_rdata
;
46 uint32_t num_my_rparam
, num_my_rdata
;
48 status
= cli_trans(talloc_tos(), cli
, SMBtrans
,
49 PIPE_LANMAN
, 0, /* name, fid */
50 0, 0, /* function, flags */
51 NULL
, 0, 0, /* setup */
52 (uint8_t *)param
, prcnt
, mprcnt
, /* Params, length, max */
53 (uint8_t *)data
, drcnt
, mdrcnt
, /* Data, length, max */
54 NULL
, /* recv_flags2 */
55 NULL
, 0, NULL
, /* rsetup */
56 &my_rparam
, 0, &num_my_rparam
,
57 &my_rdata
, 0, &num_my_rdata
);
58 if (!NT_STATUS_IS_OK(status
)) {
63 * I know this memcpy massively hurts, but there are just tons
64 * of callers of cli_api that eventually need changing to
68 *rparam
= (char *)memdup(my_rparam
, num_my_rparam
);
69 if (*rparam
== NULL
) {
72 *rprcnt
= num_my_rparam
;
73 TALLOC_FREE(my_rparam
);
75 *rdata
= (char *)memdup(my_rdata
, num_my_rdata
);
79 *rdrcnt
= num_my_rdata
;
80 TALLOC_FREE(my_rdata
);
84 TALLOC_FREE(my_rdata
);
85 TALLOC_FREE(my_rparam
);
93 /****************************************************************************
94 Perform a NetWkstaUserLogon.
95 ****************************************************************************/
97 bool cli_NetWkstaUserLogon(struct cli_state
*cli
,char *user
, char *workstation
)
102 unsigned int rdrcnt
,rprcnt
;
105 memset(param
, 0, sizeof(param
));
107 /* send a SMBtrans command with api NetWkstaUserLogon */
109 SSVAL(p
,0,132); /* api number */
111 strlcpy(p
,"OOWb54WrLh",sizeof(param
)-PTR_DIFF(p
,param
));
112 p
= skip_string(param
,sizeof(param
),p
);
113 strlcpy(p
,"WB21BWDWWDDDDDDDzzzD",sizeof(param
)-PTR_DIFF(p
,param
));
114 p
= skip_string(param
,sizeof(param
),p
);
117 strlcpy(p
,user
,sizeof(param
)-PTR_DIFF(p
,param
));
123 strlcpy(p
, workstation
,sizeof(param
)-PTR_DIFF(p
,param
));
126 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
128 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
132 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
133 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
134 &rparam
, &rprcnt
, /* return params, return size */
135 &rdata
, &rdrcnt
/* return data, return size */
137 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
140 if (cli
->rap_error
== 0) {
141 DEBUG(4,("NetWkstaUserLogon success\n"));
142 cli
->privileges
= SVAL(p
, 24);
143 /* The cli->eff_name field used to be set here
144 but it wasn't used anywhere else. */
146 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
152 return (cli
->rap_error
== 0);
155 /****************************************************************************
156 Call a NetShareEnum - try and browse available connections on a host.
157 ****************************************************************************/
159 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32
, const char *, void *), void *state
)
164 unsigned int rdrcnt
,rprcnt
;
168 /* now send a SMBtrans command with api RNetShareEnum */
170 SSVAL(p
,0,0); /* api number */
172 strlcpy(p
,"WrLeh",sizeof(param
)-PTR_DIFF(p
,param
));
173 p
= skip_string(param
,sizeof(param
),p
);
174 strlcpy(p
,"B13BWz",sizeof(param
)-PTR_DIFF(p
,param
));
175 p
= skip_string(param
,sizeof(param
),p
);
178 * Win2k needs a *smaller* buffer than 0xFFFF here -
179 * it returns "out of server memory" with 0xFFFF !!! JRA.
185 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
186 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
187 &rparam
, &rprcnt
, /* return params, length */
188 &rdata
, &rdrcnt
)) /* return data, length */
190 int res
= rparam
? SVAL(rparam
,0) : -1;
192 if (res
== 0 || res
== ERRmoredata
) {
193 int converter
=SVAL(rparam
,2);
195 char *rdata_end
= rdata
+ rdrcnt
;
197 count
=SVAL(rparam
,4);
200 for (i
=0;i
<count
;i
++,p
+=20) {
208 TALLOC_CTX
*frame
= talloc_stackframe();
210 if (p
+ 20 > rdata_end
) {
217 comment_offset
= (IVAL(p
,16) & 0xFFFF) - converter
;
218 if (comment_offset
< 0 ||
219 comment_offset
> (int)rdrcnt
) {
223 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
225 /* Work out the comment length. */
226 for (p1
= cmnt
, len
= 0; *p1
&&
227 p1
< rdata_end
; len
++)
232 pull_string_talloc(frame
,rdata
,0,
233 &s1
,sname
,14,STR_ASCII
);
234 pull_string_talloc(frame
,rdata
,0,
235 &s2
,cmnt
,len
,STR_ASCII
);
241 fn(s1
, type
, s2
, state
);
246 DEBUG(4,("NetShareEnum res=%d\n", res
));
249 DEBUG(4,("NetShareEnum failed\n"));
258 /****************************************************************************
259 Call a NetServerEnum for the specified workgroup and servertype mask. This
260 function then calls the specified callback function for each name returned.
262 The callback function takes 4 arguments: the machine name, the server type,
263 the comment and a state pointer.
264 ****************************************************************************/
266 bool cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32 stype
,
267 void (*fn
)(const char *, uint32
, const char *, void *),
272 char *rdata_end
= NULL
;
273 unsigned int rdrcnt
,rprcnt
;
278 uint32 func
= RAP_NetServerEnum2
;
279 char *last_entry
= NULL
;
284 errno
= 0; /* reset */
287 * This may take more than one transaction, so we should loop until
288 * we no longer get a more data to process or we have all of the
292 /* send a SMBtrans command with api NetServerEnum */
294 SIVAL(p
,0,func
); /* api number */
297 if (func
== RAP_NetServerEnum3
) {
298 strlcpy(p
,"WrLehDzz", sizeof(param
)-PTR_DIFF(p
,param
));
300 strlcpy(p
,"WrLehDz", sizeof(param
)-PTR_DIFF(p
,param
));
303 p
= skip_string(param
, sizeof(param
), p
);
304 strlcpy(p
,"B16BBDz", sizeof(param
)-PTR_DIFF(p
,param
));
306 p
= skip_string(param
, sizeof(param
), p
);
308 SSVAL(p
,2,CLI_BUFFER_SIZE
);
313 /* If we have more data, tell the server where
318 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
319 STR_TERMINATE
|STR_UPPER
);
321 if (len
== (size_t)-1) {
322 SAFE_FREE(last_entry
);
327 if (func
== RAP_NetServerEnum3
) {
329 last_entry
? last_entry
: "",
330 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
333 if (len
== (size_t)-1) {
334 SAFE_FREE(last_entry
);
340 /* Next time through we need to use the continue api */
341 func
= RAP_NetServerEnum3
;
344 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
345 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
346 &rparam
, &rprcnt
, /* return params, return size */
347 &rdata
, &rdrcnt
)) { /* return data, return size */
349 /* break out of the loop on error */
354 rdata_end
= rdata
+ rdrcnt
;
355 res
= rparam
? SVAL(rparam
,0) : -1;
357 if (res
== 0 || res
== ERRmoredata
||
358 (res
!= -1 && cli_errno(cli
) == 0)) {
361 int converter
=SVAL(rparam
,2);
363 /* Get the number of items returned in this buffer */
364 count
= SVAL(rparam
, 4);
366 /* The next field contains the number of items left,
367 * including those returned in this buffer. So the
368 * first time through this should contain all of the
371 if (total_cnt
== 0) {
372 total_cnt
= SVAL(rparam
, 6);
375 /* Keep track of how many we have read */
379 /* The last name in the previous NetServerEnum reply is
380 * sent back to server in the NetServerEnum3 request
381 * (last_entry). The next reply should repeat this entry
382 * as the first element. We have no proof that this is
383 * always true, but from traces that seems to be the
384 * behavior from Window Servers. So first lets do a lot
385 * of checking, just being paranoid. If the string
386 * matches then we already saw this entry so skip it.
388 * NOTE: sv1_name field must be null terminated and has
389 * a max size of 16 (NetBIOS Name).
391 if (last_entry
&& count
&& p
&&
392 (strncmp(last_entry
, p
, 16) == 0)) {
393 count
-= 1; /* Skip this entry */
394 return_cnt
= -1; /* Not part of total, so don't count. */
395 p
= rdata
+ 26; /* Skip the whole record */
398 for (i
= 0; i
< count
; i
++, p
+= 26) {
403 TALLOC_CTX
*frame
= talloc_stackframe();
404 uint32_t entry_stype
;
406 if (p
+ 26 > rdata_end
) {
412 comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
413 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
415 if (comment_offset
< 0 || comment_offset
>= (int)rdrcnt
) {
420 /* Work out the comment length. */
421 for (p1
= cmnt
, len
= 0; *p1
&&
422 p1
< rdata_end
; len
++)
428 entry_stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
430 pull_string_talloc(frame
,rdata
,0,
431 &s1
,sname
,16,STR_ASCII
);
432 pull_string_talloc(frame
,rdata
,0,
433 &s2
,cmnt
,len
,STR_ASCII
);
440 fn(s1
, entry_stype
, s2
, state
);
444 /* We are done with the old last entry, so now we can free it */
446 SAFE_FREE(last_entry
); /* This will set it to null */
449 /* We always make a copy of the last entry if we have one */
451 last_entry
= smb_xstrdup(sname
);
454 /* If we have more data, but no last entry then error out */
455 if (!last_entry
&& (res
== ERRmoredata
)) {
464 } while ((res
== ERRmoredata
) && (total_cnt
> return_cnt
));
468 SAFE_FREE(last_entry
);
471 errno
= cli_errno(cli
);
474 /* this is a very special case, when the domain master for the
475 work group isn't part of the work group itself, there is something
481 return(return_cnt
> 0);
484 /****************************************************************************
485 Send a SamOEMChangePassword command.
486 ****************************************************************************/
488 bool cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
489 const char *old_password
)
492 unsigned char data
[532];
494 unsigned char old_pw_hash
[16];
495 unsigned char new_pw_hash
[16];
496 unsigned int data_len
;
497 unsigned int param_len
= 0;
500 unsigned int rprcnt
, rdrcnt
;
502 if (strlen(user
) >= sizeof(fstring
)-1) {
503 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
507 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
509 strlcpy(p
, "zsT", sizeof(param
)-PTR_DIFF(p
,param
));
510 p
= skip_string(param
,sizeof(param
),p
);
511 strlcpy(p
, "B516B16", sizeof(param
)-PTR_DIFF(p
,param
));
512 p
= skip_string(param
,sizeof(param
),p
);
513 strlcpy(p
,user
, sizeof(param
)-PTR_DIFF(p
,param
));
514 p
= skip_string(param
,sizeof(param
),p
);
518 param_len
= PTR_DIFF(p
,param
);
521 * Get the Lanman hash of the old password, we
522 * use this as the key to make_oem_passwd_hash().
524 E_deshash(old_password
, old_pw_hash
);
526 encode_pw_buffer(data
, new_password
, STR_ASCII
);
528 #ifdef DEBUG_PASSWORD
529 DEBUG(100,("make_oem_passwd_hash\n"));
530 dump_data(100, data
, 516);
532 arcfour_crypt( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
535 * Now place the old password hash in the data.
537 E_deshash(new_password
, new_pw_hash
);
539 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
544 param
, param_len
, 4, /* param, length, max */
545 (char *)data
, data_len
, 0, /* data, length, max */
548 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
554 cli
->rap_error
= SVAL(rparam
,0);
560 return (cli
->rap_error
== 0);
563 /****************************************************************************
564 Send a qpathinfo call.
565 ****************************************************************************/
567 struct cli_qpathinfo1_state
{
568 struct cli_state
*cli
;
573 static void cli_qpathinfo1_done(struct tevent_req
*subreq
);
575 struct tevent_req
*cli_qpathinfo1_send(TALLOC_CTX
*mem_ctx
,
576 struct event_context
*ev
,
577 struct cli_state
*cli
,
580 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
581 struct cli_qpathinfo1_state
*state
= NULL
;
583 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo1_state
);
588 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_INFO_STANDARD
,
590 if (tevent_req_nomem(subreq
, req
)) {
591 return tevent_req_post(req
, ev
);
593 tevent_req_set_callback(subreq
, cli_qpathinfo1_done
, req
);
597 static void cli_qpathinfo1_done(struct tevent_req
*subreq
)
599 struct tevent_req
*req
= tevent_req_callback_data(
600 subreq
, struct tevent_req
);
601 struct cli_qpathinfo1_state
*state
= tevent_req_data(
602 req
, struct cli_qpathinfo1_state
);
605 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
608 if (!NT_STATUS_IS_OK(status
)) {
609 tevent_req_nterror(req
, status
);
612 tevent_req_done(req
);
615 NTSTATUS
cli_qpathinfo1_recv(struct tevent_req
*req
,
622 struct cli_qpathinfo1_state
*state
= tevent_req_data(
623 req
, struct cli_qpathinfo1_state
);
626 time_t (*date_fn
)(const void *buf
, int serverzone
);
628 if (tevent_req_is_nterror(req
, &status
)) {
632 if (state
->cli
->win95
) {
633 date_fn
= make_unix_date
;
635 date_fn
= make_unix_date2
;
639 *change_time
= date_fn(state
->data
+0, state
->cli
->serverzone
);
642 *access_time
= date_fn(state
->data
+4, state
->cli
->serverzone
);
645 *write_time
= date_fn(state
->data
+8, state
->cli
->serverzone
);
648 *size
= IVAL(state
->data
, 12);
651 *mode
= SVAL(state
->data
, l1_attrFile
);
656 NTSTATUS
cli_qpathinfo1(struct cli_state
*cli
,
664 TALLOC_CTX
*frame
= talloc_stackframe();
665 struct event_context
*ev
;
666 struct tevent_req
*req
;
667 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
669 if (cli_has_async_calls(cli
)) {
671 * Can't use sync call while an async call is in flight
673 status
= NT_STATUS_INVALID_PARAMETER
;
676 ev
= event_context_init(frame
);
680 req
= cli_qpathinfo1_send(frame
, ev
, cli
, fname
);
684 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
687 status
= cli_qpathinfo1_recv(req
, change_time
, access_time
,
688 write_time
, size
, mode
);
691 if (!NT_STATUS_IS_OK(status
)) {
692 cli_set_error(cli
, status
);
697 /****************************************************************************
698 Send a setpathinfo call.
699 ****************************************************************************/
701 NTSTATUS
cli_setpathinfo_basic(struct cli_state
*cli
, const char *fname
,
708 unsigned int data_len
= 0;
715 * Add the create, last access, modification, and status change times
717 put_long_date(p
, create_time
);
720 put_long_date(p
, access_time
);
723 put_long_date(p
, write_time
);
726 put_long_date(p
, change_time
);
737 data_len
= PTR_DIFF(p
, data
);
739 return cli_setpathinfo(cli
, SMB_FILE_BASIC_INFORMATION
, fname
,
740 (uint8_t *)data
, data_len
);
743 /****************************************************************************
744 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
745 ****************************************************************************/
747 struct cli_qpathinfo2_state
{
752 static void cli_qpathinfo2_done(struct tevent_req
*subreq
);
754 struct tevent_req
*cli_qpathinfo2_send(TALLOC_CTX
*mem_ctx
,
755 struct event_context
*ev
,
756 struct cli_state
*cli
,
759 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
760 struct cli_qpathinfo2_state
*state
= NULL
;
762 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo2_state
);
766 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
767 SMB_QUERY_FILE_ALL_INFO
,
769 if (tevent_req_nomem(subreq
, req
)) {
770 return tevent_req_post(req
, ev
);
772 tevent_req_set_callback(subreq
, cli_qpathinfo2_done
, req
);
776 static void cli_qpathinfo2_done(struct tevent_req
*subreq
)
778 struct tevent_req
*req
= tevent_req_callback_data(
779 subreq
, struct tevent_req
);
780 struct cli_qpathinfo2_state
*state
= tevent_req_data(
781 req
, struct cli_qpathinfo2_state
);
784 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
787 if (!NT_STATUS_IS_OK(status
)) {
788 tevent_req_nterror(req
, status
);
791 tevent_req_done(req
);
794 NTSTATUS
cli_qpathinfo2_recv(struct tevent_req
*req
,
795 struct timespec
*create_time
,
796 struct timespec
*access_time
,
797 struct timespec
*write_time
,
798 struct timespec
*change_time
,
799 SMB_OFF_T
*size
, uint16
*mode
,
802 struct cli_qpathinfo2_state
*state
= tevent_req_data(
803 req
, struct cli_qpathinfo2_state
);
806 if (tevent_req_is_nterror(req
, &status
)) {
811 *create_time
= interpret_long_date((char *)state
->data
+0);
814 *access_time
= interpret_long_date((char *)state
->data
+8);
817 *write_time
= interpret_long_date((char *)state
->data
+16);
820 *change_time
= interpret_long_date((char *)state
->data
+24);
823 *mode
= SVAL(state
->data
, 32);
826 *size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,48);
829 *ino
= IVAL(state
->data
, 64);
834 NTSTATUS
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
835 struct timespec
*create_time
,
836 struct timespec
*access_time
,
837 struct timespec
*write_time
,
838 struct timespec
*change_time
,
839 SMB_OFF_T
*size
, uint16
*mode
,
842 TALLOC_CTX
*frame
= talloc_stackframe();
843 struct event_context
*ev
;
844 struct tevent_req
*req
;
845 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
847 if (cli_has_async_calls(cli
)) {
849 * Can't use sync call while an async call is in flight
851 status
= NT_STATUS_INVALID_PARAMETER
;
854 ev
= event_context_init(frame
);
858 req
= cli_qpathinfo2_send(frame
, ev
, cli
, fname
);
862 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
865 status
= cli_qpathinfo2_recv(req
, create_time
, access_time
,
866 write_time
, change_time
, size
, mode
, ino
);
869 if (!NT_STATUS_IS_OK(status
)) {
870 cli_set_error(cli
, status
);
875 /****************************************************************************
877 ****************************************************************************/
879 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *data
,
881 unsigned int *pnum_streams
,
882 struct stream_struct
**pstreams
);
884 struct cli_qpathinfo_streams_state
{
889 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
);
891 struct tevent_req
*cli_qpathinfo_streams_send(TALLOC_CTX
*mem_ctx
,
892 struct tevent_context
*ev
,
893 struct cli_state
*cli
,
896 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
897 struct cli_qpathinfo_streams_state
*state
= NULL
;
899 req
= tevent_req_create(mem_ctx
, &state
,
900 struct cli_qpathinfo_streams_state
);
904 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
905 SMB_FILE_STREAM_INFORMATION
,
907 if (tevent_req_nomem(subreq
, req
)) {
908 return tevent_req_post(req
, ev
);
910 tevent_req_set_callback(subreq
, cli_qpathinfo_streams_done
, req
);
914 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
)
916 struct tevent_req
*req
= tevent_req_callback_data(
917 subreq
, struct tevent_req
);
918 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
919 req
, struct cli_qpathinfo_streams_state
);
922 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
925 if (!NT_STATUS_IS_OK(status
)) {
926 tevent_req_nterror(req
, status
);
929 tevent_req_done(req
);
932 NTSTATUS
cli_qpathinfo_streams_recv(struct tevent_req
*req
,
934 unsigned int *pnum_streams
,
935 struct stream_struct
**pstreams
)
937 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
938 req
, struct cli_qpathinfo_streams_state
);
941 if (tevent_req_is_nterror(req
, &status
)) {
944 if (!parse_streams_blob(mem_ctx
, state
->data
, state
->num_data
,
945 pnum_streams
, pstreams
)) {
946 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
951 NTSTATUS
cli_qpathinfo_streams(struct cli_state
*cli
, const char *fname
,
953 unsigned int *pnum_streams
,
954 struct stream_struct
**pstreams
)
956 TALLOC_CTX
*frame
= talloc_stackframe();
957 struct event_context
*ev
;
958 struct tevent_req
*req
;
959 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
961 if (cli_has_async_calls(cli
)) {
963 * Can't use sync call while an async call is in flight
965 status
= NT_STATUS_INVALID_PARAMETER
;
968 ev
= event_context_init(frame
);
972 req
= cli_qpathinfo_streams_send(frame
, ev
, cli
, fname
);
976 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
979 status
= cli_qpathinfo_streams_recv(req
, mem_ctx
, pnum_streams
,
983 if (!NT_STATUS_IS_OK(status
)) {
984 cli_set_error(cli
, status
);
989 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *rdata
,
991 unsigned int *pnum_streams
,
992 struct stream_struct
**pstreams
)
994 unsigned int num_streams
;
995 struct stream_struct
*streams
;
1002 while ((data_len
> ofs
) && (data_len
- ofs
>= 24)) {
1006 struct stream_struct
*tmp
;
1009 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, streams
,
1010 struct stream_struct
,
1018 nlen
= IVAL(rdata
, ofs
+ 0x04);
1020 streams
[num_streams
].size
= IVAL_TO_SMB_OFF_T(
1022 streams
[num_streams
].alloc_size
= IVAL_TO_SMB_OFF_T(
1025 if (nlen
> data_len
- (ofs
+ 24)) {
1030 * We need to null-terminate src, how do I do this with
1031 * convert_string_talloc??
1034 tmp_buf
= TALLOC_ARRAY(streams
, uint8_t, nlen
+2);
1035 if (tmp_buf
== NULL
) {
1039 memcpy(tmp_buf
, rdata
+ofs
+24, nlen
);
1041 tmp_buf
[nlen
+1] = 0;
1043 if (!convert_string_talloc(streams
, CH_UTF16
, CH_UNIX
, tmp_buf
,
1044 nlen
+2, &vstr
, &size
, false))
1046 TALLOC_FREE(tmp_buf
);
1050 TALLOC_FREE(tmp_buf
);
1051 streams
[num_streams
].name
= (char *)vstr
;
1054 len
= IVAL(rdata
, ofs
);
1055 if (len
> data_len
- ofs
) {
1058 if (len
== 0) break;
1062 *pnum_streams
= num_streams
;
1063 *pstreams
= streams
;
1067 TALLOC_FREE(streams
);
1071 /****************************************************************************
1072 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1073 ****************************************************************************/
1075 NTSTATUS
cli_qfilename(struct cli_state
*cli
, uint16_t fnum
, char *name
,
1082 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1083 SMB_QUERY_FILE_NAME_INFO
,
1085 &rdata
, &num_rdata
);
1086 if (!NT_STATUS_IS_OK(status
)) {
1090 clistr_pull(cli
->inbuf
, name
, rdata
+4, namelen
, IVAL(rdata
, 0),
1093 return NT_STATUS_OK
;
1096 /****************************************************************************
1097 Send a qfileinfo call.
1098 ****************************************************************************/
1100 NTSTATUS
cli_qfileinfo_basic(struct cli_state
*cli
, uint16_t fnum
,
1101 uint16
*mode
, SMB_OFF_T
*size
,
1102 struct timespec
*create_time
,
1103 struct timespec
*access_time
,
1104 struct timespec
*write_time
,
1105 struct timespec
*change_time
,
1112 /* if its a win95 server then fail this - win95 totally screws it
1115 return NT_STATUS_NOT_SUPPORTED
;
1118 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1119 SMB_QUERY_FILE_ALL_INFO
,
1120 68, MIN(cli
->max_xmit
, 0xffff),
1121 &rdata
, &num_rdata
);
1122 if (!NT_STATUS_IS_OK(status
)) {
1127 *create_time
= interpret_long_date((char *)rdata
+0);
1130 *access_time
= interpret_long_date((char *)rdata
+8);
1133 *write_time
= interpret_long_date((char *)rdata
+16);
1136 *change_time
= interpret_long_date((char *)rdata
+24);
1139 *mode
= SVAL(rdata
, 32);
1142 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
1145 *ino
= IVAL(rdata
, 64);
1149 return NT_STATUS_OK
;
1152 /****************************************************************************
1153 Send a qpathinfo BASIC_INFO call.
1154 ****************************************************************************/
1156 struct cli_qpathinfo_basic_state
{
1161 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
);
1163 struct tevent_req
*cli_qpathinfo_basic_send(TALLOC_CTX
*mem_ctx
,
1164 struct event_context
*ev
,
1165 struct cli_state
*cli
,
1168 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1169 struct cli_qpathinfo_basic_state
*state
= NULL
;
1171 req
= tevent_req_create(mem_ctx
, &state
,
1172 struct cli_qpathinfo_basic_state
);
1176 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
1177 SMB_QUERY_FILE_BASIC_INFO
,
1179 if (tevent_req_nomem(subreq
, req
)) {
1180 return tevent_req_post(req
, ev
);
1182 tevent_req_set_callback(subreq
, cli_qpathinfo_basic_done
, req
);
1186 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
)
1188 struct tevent_req
*req
= tevent_req_callback_data(
1189 subreq
, struct tevent_req
);
1190 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1191 req
, struct cli_qpathinfo_basic_state
);
1194 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
1196 TALLOC_FREE(subreq
);
1197 if (!NT_STATUS_IS_OK(status
)) {
1198 tevent_req_nterror(req
, status
);
1201 tevent_req_done(req
);
1204 NTSTATUS
cli_qpathinfo_basic_recv(struct tevent_req
*req
,
1205 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1207 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1208 req
, struct cli_qpathinfo_basic_state
);
1211 if (tevent_req_is_nterror(req
, &status
)) {
1215 sbuf
->st_ex_atime
= interpret_long_date((char *)state
->data
+8);
1216 sbuf
->st_ex_mtime
= interpret_long_date((char *)state
->data
+16);
1217 sbuf
->st_ex_ctime
= interpret_long_date((char *)state
->data
+24);
1218 *attributes
= IVAL(state
->data
, 32);
1219 return NT_STATUS_OK
;
1222 NTSTATUS
cli_qpathinfo_basic(struct cli_state
*cli
, const char *name
,
1223 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1225 TALLOC_CTX
*frame
= talloc_stackframe();
1226 struct event_context
*ev
;
1227 struct tevent_req
*req
;
1228 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1230 if (cli_has_async_calls(cli
)) {
1232 * Can't use sync call while an async call is in flight
1234 status
= NT_STATUS_INVALID_PARAMETER
;
1237 ev
= event_context_init(frame
);
1241 req
= cli_qpathinfo_basic_send(frame
, ev
, cli
, name
);
1245 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1248 status
= cli_qpathinfo_basic_recv(req
, sbuf
, attributes
);
1251 if (!NT_STATUS_IS_OK(status
)) {
1252 cli_set_error(cli
, status
);
1257 /****************************************************************************
1258 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1259 ****************************************************************************/
1261 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
1266 char *converted
= NULL
;
1267 size_t converted_size
= 0;
1270 status
= cli_qpathinfo(talloc_tos(), cli
, fname
,
1271 SMB_QUERY_FILE_ALT_NAME_INFO
,
1272 4, cli
->max_xmit
, &rdata
, &num_rdata
);
1273 if (!NT_STATUS_IS_OK(status
)) {
1277 len
= IVAL(rdata
, 0);
1279 if (len
> num_rdata
- 4) {
1280 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1283 /* The returned data is a pushed string, not raw data. */
1284 if (!convert_string_talloc(talloc_tos(),
1285 cli_ucs2(cli
) ? CH_UTF16LE
: CH_DOS
,
1292 return NT_STATUS_NO_MEMORY
;
1294 fstrcpy(alt_name
, converted
);
1296 TALLOC_FREE(converted
);
1299 return NT_STATUS_OK
;