2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Gerald (Jerry) Carter 2004
6 Copyright (C) James Peach 2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "../libcli/auth/libcli_auth.h"
24 #include "../librpc/gen_ndr/rap.h"
25 #include "../lib/crypto/arcfour.h"
26 #include "../lib/util/tevent_ntstatus.h"
27 #include "async_smb.h"
28 #include "libsmb/libsmb.h"
29 #include "libsmb/clirap.h"
31 #include "../libcli/smb/smbXcli_base.h"
33 #define PIPE_LANMAN "\\PIPE\\LANMAN"
35 /****************************************************************************
37 ****************************************************************************/
39 bool cli_api(struct cli_state
*cli
,
40 char *param
, int prcnt
, int mprcnt
,
41 char *data
, int drcnt
, int mdrcnt
,
42 char **rparam
, unsigned int *rprcnt
,
43 char **rdata
, unsigned int *rdrcnt
)
47 uint8_t *my_rparam
, *my_rdata
;
48 uint32_t num_my_rparam
, num_my_rdata
;
50 status
= cli_trans(talloc_tos(), cli
, SMBtrans
,
51 PIPE_LANMAN
, 0, /* name, fid */
52 0, 0, /* function, flags */
53 NULL
, 0, 0, /* setup */
54 (uint8_t *)param
, prcnt
, mprcnt
, /* Params, length, max */
55 (uint8_t *)data
, drcnt
, mdrcnt
, /* Data, length, max */
56 NULL
, /* recv_flags2 */
57 NULL
, 0, NULL
, /* rsetup */
58 &my_rparam
, 0, &num_my_rparam
,
59 &my_rdata
, 0, &num_my_rdata
);
60 if (!NT_STATUS_IS_OK(status
)) {
65 * I know this memcpy massively hurts, but there are just tons
66 * of callers of cli_api that eventually need changing to
70 *rparam
= (char *)memdup(my_rparam
, num_my_rparam
);
71 if (*rparam
== NULL
) {
74 *rprcnt
= num_my_rparam
;
75 TALLOC_FREE(my_rparam
);
77 *rdata
= (char *)memdup(my_rdata
, num_my_rdata
);
81 *rdrcnt
= num_my_rdata
;
82 TALLOC_FREE(my_rdata
);
86 TALLOC_FREE(my_rdata
);
87 TALLOC_FREE(my_rparam
);
95 /****************************************************************************
96 Perform a NetWkstaUserLogon.
97 ****************************************************************************/
99 bool cli_NetWkstaUserLogon(struct cli_state
*cli
,char *user
, char *workstation
)
104 unsigned int rdrcnt
,rprcnt
;
107 memset(param
, 0, sizeof(param
));
109 /* send a SMBtrans command with api NetWkstaUserLogon */
111 SSVAL(p
,0,132); /* api number */
113 strlcpy(p
,"OOWb54WrLh",sizeof(param
)-PTR_DIFF(p
,param
));
114 p
= skip_string(param
,sizeof(param
),p
);
115 strlcpy(p
,"WB21BWDWWDDDDDDDzzzD",sizeof(param
)-PTR_DIFF(p
,param
));
116 p
= skip_string(param
,sizeof(param
),p
);
119 strlcpy(p
,user
,sizeof(param
)-PTR_DIFF(p
,param
));
125 strlcpy(p
, workstation
,sizeof(param
)-PTR_DIFF(p
,param
));
128 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
130 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
134 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
135 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
136 &rparam
, &rprcnt
, /* return params, return size */
137 &rdata
, &rdrcnt
/* return data, return size */
139 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
142 if (cli
->rap_error
== 0) {
143 DEBUG(4,("NetWkstaUserLogon success\n"));
145 * The cli->privileges = SVAL(p, 24); field was set here
146 * but it was not use anywhere else.
148 /* The cli->eff_name field used to be set here
149 but it wasn't used anywhere else. */
151 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
157 return (cli
->rap_error
== 0);
160 /****************************************************************************
161 Call a NetShareEnum - try and browse available connections on a host.
162 ****************************************************************************/
164 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32
, const char *, void *), void *state
)
169 unsigned int rdrcnt
,rprcnt
;
173 /* now send a SMBtrans command with api RNetShareEnum */
175 SSVAL(p
,0,0); /* api number */
177 strlcpy(p
,"WrLeh",sizeof(param
)-PTR_DIFF(p
,param
));
178 p
= skip_string(param
,sizeof(param
),p
);
179 strlcpy(p
,"B13BWz",sizeof(param
)-PTR_DIFF(p
,param
));
180 p
= skip_string(param
,sizeof(param
),p
);
183 * Win2k needs a *smaller* buffer than 0xFFFF here -
184 * it returns "out of server memory" with 0xFFFF !!! JRA.
190 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
191 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
192 &rparam
, &rprcnt
, /* return params, length */
193 &rdata
, &rdrcnt
)) /* return data, length */
195 int res
= rparam
? SVAL(rparam
,0) : -1;
197 if (res
== 0 || res
== ERRmoredata
) {
198 int converter
=SVAL(rparam
,2);
200 char *rdata_end
= rdata
+ rdrcnt
;
202 count
=SVAL(rparam
,4);
205 for (i
=0;i
<count
;i
++,p
+=20) {
213 TALLOC_CTX
*frame
= talloc_stackframe();
215 if (p
+ 20 > rdata_end
) {
222 comment_offset
= (IVAL(p
,16) & 0xFFFF) - converter
;
223 if (comment_offset
< 0 ||
224 comment_offset
> (int)rdrcnt
) {
228 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
230 /* Work out the comment length. */
231 for (p1
= cmnt
, len
= 0; *p1
&&
232 p1
< rdata_end
; len
++)
237 pull_string_talloc(frame
,rdata
,0,
238 &s1
,sname
,14,STR_ASCII
);
239 pull_string_talloc(frame
,rdata
,0,
240 &s2
,cmnt
,len
,STR_ASCII
);
246 fn(s1
, type
, s2
, state
);
251 DEBUG(4,("NetShareEnum res=%d\n", res
));
254 DEBUG(4,("NetShareEnum failed\n"));
263 /****************************************************************************
264 Call a NetServerEnum for the specified workgroup and servertype mask. This
265 function then calls the specified callback function for each name returned.
267 The callback function takes 4 arguments: the machine name, the server type,
268 the comment and a state pointer.
269 ****************************************************************************/
271 bool cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32 stype
,
272 void (*fn
)(const char *, uint32
, const char *, void *),
277 char *rdata_end
= NULL
;
278 unsigned int rdrcnt
,rprcnt
;
283 uint32 func
= RAP_NetServerEnum2
;
284 char *last_entry
= NULL
;
289 errno
= 0; /* reset */
292 * This may take more than one transaction, so we should loop until
293 * we no longer get a more data to process or we have all of the
297 /* send a SMBtrans command with api NetServerEnum */
299 SIVAL(p
,0,func
); /* api number */
302 if (func
== RAP_NetServerEnum3
) {
303 strlcpy(p
,"WrLehDzz", sizeof(param
)-PTR_DIFF(p
,param
));
305 strlcpy(p
,"WrLehDz", sizeof(param
)-PTR_DIFF(p
,param
));
308 p
= skip_string(param
, sizeof(param
), p
);
309 strlcpy(p
,"B16BBDz", sizeof(param
)-PTR_DIFF(p
,param
));
311 p
= skip_string(param
, sizeof(param
), p
);
313 SSVAL(p
,2,CLI_BUFFER_SIZE
);
318 /* If we have more data, tell the server where
323 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
324 STR_TERMINATE
|STR_UPPER
);
326 if (len
== (size_t)-1) {
327 SAFE_FREE(last_entry
);
332 if (func
== RAP_NetServerEnum3
) {
334 last_entry
? last_entry
: "",
335 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
338 if (len
== (size_t)-1) {
339 SAFE_FREE(last_entry
);
345 /* Next time through we need to use the continue api */
346 func
= RAP_NetServerEnum3
;
349 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
350 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
351 &rparam
, &rprcnt
, /* return params, return size */
352 &rdata
, &rdrcnt
)) { /* return data, return size */
354 /* break out of the loop on error */
359 rdata_end
= rdata
+ rdrcnt
;
360 res
= rparam
? SVAL(rparam
,0) : -1;
362 if (res
== 0 || res
== ERRmoredata
||
363 (res
!= -1 && cli_errno(cli
) == 0)) {
366 int converter
=SVAL(rparam
,2);
368 /* Get the number of items returned in this buffer */
369 count
= SVAL(rparam
, 4);
371 /* The next field contains the number of items left,
372 * including those returned in this buffer. So the
373 * first time through this should contain all of the
376 if (total_cnt
== 0) {
377 total_cnt
= SVAL(rparam
, 6);
380 /* Keep track of how many we have read */
384 /* The last name in the previous NetServerEnum reply is
385 * sent back to server in the NetServerEnum3 request
386 * (last_entry). The next reply should repeat this entry
387 * as the first element. We have no proof that this is
388 * always true, but from traces that seems to be the
389 * behavior from Window Servers. So first lets do a lot
390 * of checking, just being paranoid. If the string
391 * matches then we already saw this entry so skip it.
393 * NOTE: sv1_name field must be null terminated and has
394 * a max size of 16 (NetBIOS Name).
396 if (last_entry
&& count
&& p
&&
397 (strncmp(last_entry
, p
, 16) == 0)) {
398 count
-= 1; /* Skip this entry */
399 return_cnt
= -1; /* Not part of total, so don't count. */
400 p
= rdata
+ 26; /* Skip the whole record */
403 for (i
= 0; i
< count
; i
++, p
+= 26) {
408 TALLOC_CTX
*frame
= talloc_stackframe();
409 uint32_t entry_stype
;
411 if (p
+ 26 > rdata_end
) {
417 comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
418 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
420 if (comment_offset
< 0 || comment_offset
>= (int)rdrcnt
) {
425 /* Work out the comment length. */
426 for (p1
= cmnt
, len
= 0; *p1
&&
427 p1
< rdata_end
; len
++)
433 entry_stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
435 pull_string_talloc(frame
,rdata
,0,
436 &s1
,sname
,16,STR_ASCII
);
437 pull_string_talloc(frame
,rdata
,0,
438 &s2
,cmnt
,len
,STR_ASCII
);
445 fn(s1
, entry_stype
, s2
, state
);
449 /* We are done with the old last entry, so now we can free it */
451 SAFE_FREE(last_entry
); /* This will set it to null */
454 /* We always make a copy of the last entry if we have one */
456 last_entry
= smb_xstrdup(sname
);
459 /* If we have more data, but no last entry then error out */
460 if (!last_entry
&& (res
== ERRmoredata
)) {
469 } while ((res
== ERRmoredata
) && (total_cnt
> return_cnt
));
473 SAFE_FREE(last_entry
);
476 errno
= cli_errno(cli
);
479 /* this is a very special case, when the domain master for the
480 work group isn't part of the work group itself, there is something
486 return(return_cnt
> 0);
489 /****************************************************************************
490 Send a SamOEMChangePassword command.
491 ****************************************************************************/
493 bool cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
494 const char *old_password
)
497 unsigned char data
[532];
499 unsigned char old_pw_hash
[16];
500 unsigned char new_pw_hash
[16];
501 unsigned int data_len
;
502 unsigned int param_len
= 0;
505 unsigned int rprcnt
, rdrcnt
;
507 if (strlen(user
) >= sizeof(fstring
)-1) {
508 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
512 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
514 strlcpy(p
, "zsT", sizeof(param
)-PTR_DIFF(p
,param
));
515 p
= skip_string(param
,sizeof(param
),p
);
516 strlcpy(p
, "B516B16", sizeof(param
)-PTR_DIFF(p
,param
));
517 p
= skip_string(param
,sizeof(param
),p
);
518 strlcpy(p
,user
, sizeof(param
)-PTR_DIFF(p
,param
));
519 p
= skip_string(param
,sizeof(param
),p
);
523 param_len
= PTR_DIFF(p
,param
);
526 * Get the Lanman hash of the old password, we
527 * use this as the key to make_oem_passwd_hash().
529 E_deshash(old_password
, old_pw_hash
);
531 encode_pw_buffer(data
, new_password
, STR_ASCII
);
533 #ifdef DEBUG_PASSWORD
534 DEBUG(100,("make_oem_passwd_hash\n"));
535 dump_data(100, data
, 516);
537 arcfour_crypt( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
540 * Now place the old password hash in the data.
542 E_deshash(new_password
, new_pw_hash
);
544 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
549 param
, param_len
, 4, /* param, length, max */
550 (char *)data
, data_len
, 0, /* data, length, max */
553 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
559 cli
->rap_error
= SVAL(rparam
,0);
565 return (cli
->rap_error
== 0);
568 /****************************************************************************
569 Send a qpathinfo call.
570 ****************************************************************************/
572 struct cli_qpathinfo1_state
{
573 struct cli_state
*cli
;
578 static void cli_qpathinfo1_done(struct tevent_req
*subreq
);
580 struct tevent_req
*cli_qpathinfo1_send(TALLOC_CTX
*mem_ctx
,
581 struct event_context
*ev
,
582 struct cli_state
*cli
,
585 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
586 struct cli_qpathinfo1_state
*state
= NULL
;
588 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo1_state
);
593 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_INFO_STANDARD
,
594 22, CLI_BUFFER_SIZE
);
595 if (tevent_req_nomem(subreq
, req
)) {
596 return tevent_req_post(req
, ev
);
598 tevent_req_set_callback(subreq
, cli_qpathinfo1_done
, req
);
602 static void cli_qpathinfo1_done(struct tevent_req
*subreq
)
604 struct tevent_req
*req
= tevent_req_callback_data(
605 subreq
, struct tevent_req
);
606 struct cli_qpathinfo1_state
*state
= tevent_req_data(
607 req
, struct cli_qpathinfo1_state
);
610 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
613 if (!NT_STATUS_IS_OK(status
)) {
614 tevent_req_nterror(req
, status
);
617 tevent_req_done(req
);
620 NTSTATUS
cli_qpathinfo1_recv(struct tevent_req
*req
,
627 struct cli_qpathinfo1_state
*state
= tevent_req_data(
628 req
, struct cli_qpathinfo1_state
);
631 time_t (*date_fn
)(const void *buf
, int serverzone
);
633 if (tevent_req_is_nterror(req
, &status
)) {
637 if (state
->cli
->win95
) {
638 date_fn
= make_unix_date
;
640 date_fn
= make_unix_date2
;
644 *change_time
= date_fn(state
->data
+0, smb1cli_conn_server_time_zone(state
->cli
->conn
));
647 *access_time
= date_fn(state
->data
+4, smb1cli_conn_server_time_zone(state
->cli
->conn
));
650 *write_time
= date_fn(state
->data
+8, smb1cli_conn_server_time_zone(state
->cli
->conn
));
653 *size
= IVAL(state
->data
, 12);
656 *mode
= SVAL(state
->data
, l1_attrFile
);
661 NTSTATUS
cli_qpathinfo1(struct cli_state
*cli
,
669 TALLOC_CTX
*frame
= talloc_stackframe();
670 struct event_context
*ev
;
671 struct tevent_req
*req
;
672 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
674 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
676 * Can't use sync call while an async call is in flight
678 status
= NT_STATUS_INVALID_PARAMETER
;
681 ev
= event_context_init(frame
);
685 req
= cli_qpathinfo1_send(frame
, ev
, cli
, fname
);
689 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
692 status
= cli_qpathinfo1_recv(req
, change_time
, access_time
,
693 write_time
, size
, mode
);
699 /****************************************************************************
700 Send a setpathinfo call.
701 ****************************************************************************/
703 NTSTATUS
cli_setpathinfo_basic(struct cli_state
*cli
, const char *fname
,
710 unsigned int data_len
= 0;
717 * Add the create, last access, modification, and status change times
719 put_long_date(p
, create_time
);
722 put_long_date(p
, access_time
);
725 put_long_date(p
, write_time
);
728 put_long_date(p
, change_time
);
739 data_len
= PTR_DIFF(p
, data
);
741 return cli_setpathinfo(cli
, SMB_FILE_BASIC_INFORMATION
, fname
,
742 (uint8_t *)data
, data_len
);
745 /****************************************************************************
746 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
747 ****************************************************************************/
749 struct cli_qpathinfo2_state
{
754 static void cli_qpathinfo2_done(struct tevent_req
*subreq
);
756 struct tevent_req
*cli_qpathinfo2_send(TALLOC_CTX
*mem_ctx
,
757 struct event_context
*ev
,
758 struct cli_state
*cli
,
761 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
762 struct cli_qpathinfo2_state
*state
= NULL
;
764 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo2_state
);
768 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
769 SMB_QUERY_FILE_ALL_INFO
,
770 68, CLI_BUFFER_SIZE
);
771 if (tevent_req_nomem(subreq
, req
)) {
772 return tevent_req_post(req
, ev
);
774 tevent_req_set_callback(subreq
, cli_qpathinfo2_done
, req
);
778 static void cli_qpathinfo2_done(struct tevent_req
*subreq
)
780 struct tevent_req
*req
= tevent_req_callback_data(
781 subreq
, struct tevent_req
);
782 struct cli_qpathinfo2_state
*state
= tevent_req_data(
783 req
, struct cli_qpathinfo2_state
);
786 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
789 if (!NT_STATUS_IS_OK(status
)) {
790 tevent_req_nterror(req
, status
);
793 tevent_req_done(req
);
796 NTSTATUS
cli_qpathinfo2_recv(struct tevent_req
*req
,
797 struct timespec
*create_time
,
798 struct timespec
*access_time
,
799 struct timespec
*write_time
,
800 struct timespec
*change_time
,
801 off_t
*size
, uint16
*mode
,
804 struct cli_qpathinfo2_state
*state
= tevent_req_data(
805 req
, struct cli_qpathinfo2_state
);
808 if (tevent_req_is_nterror(req
, &status
)) {
813 *create_time
= interpret_long_date((char *)state
->data
+0);
816 *access_time
= interpret_long_date((char *)state
->data
+8);
819 *write_time
= interpret_long_date((char *)state
->data
+16);
822 *change_time
= interpret_long_date((char *)state
->data
+24);
825 *mode
= SVAL(state
->data
, 32);
828 *size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,48);
831 *ino
= IVAL(state
->data
, 64);
836 NTSTATUS
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
837 struct timespec
*create_time
,
838 struct timespec
*access_time
,
839 struct timespec
*write_time
,
840 struct timespec
*change_time
,
841 off_t
*size
, uint16
*mode
,
844 TALLOC_CTX
*frame
= talloc_stackframe();
845 struct event_context
*ev
;
846 struct tevent_req
*req
;
847 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
849 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
851 * Can't use sync call while an async call is in flight
853 status
= NT_STATUS_INVALID_PARAMETER
;
856 ev
= event_context_init(frame
);
860 req
= cli_qpathinfo2_send(frame
, ev
, cli
, fname
);
864 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
867 status
= cli_qpathinfo2_recv(req
, create_time
, access_time
,
868 write_time
, change_time
, size
, mode
, ino
);
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 (smbXcli_conn_has_async_calls(cli
->conn
)) {
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
,
985 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *rdata
,
987 unsigned int *pnum_streams
,
988 struct stream_struct
**pstreams
)
990 unsigned int num_streams
;
991 struct stream_struct
*streams
;
998 while ((data_len
> ofs
) && (data_len
- ofs
>= 24)) {
1002 struct stream_struct
*tmp
;
1005 tmp
= talloc_realloc(mem_ctx
, streams
,
1006 struct stream_struct
,
1014 nlen
= IVAL(rdata
, ofs
+ 0x04);
1016 streams
[num_streams
].size
= IVAL_TO_SMB_OFF_T(
1018 streams
[num_streams
].alloc_size
= IVAL_TO_SMB_OFF_T(
1021 if (nlen
> data_len
- (ofs
+ 24)) {
1026 * We need to null-terminate src, how do I do this with
1027 * convert_string_talloc??
1030 tmp_buf
= talloc_array(streams
, uint8_t, nlen
+2);
1031 if (tmp_buf
== NULL
) {
1035 memcpy(tmp_buf
, rdata
+ofs
+24, nlen
);
1037 tmp_buf
[nlen
+1] = 0;
1039 if (!convert_string_talloc(streams
, CH_UTF16
, CH_UNIX
, tmp_buf
,
1040 nlen
+2, &vstr
, &size
))
1042 TALLOC_FREE(tmp_buf
);
1046 TALLOC_FREE(tmp_buf
);
1047 streams
[num_streams
].name
= (char *)vstr
;
1050 len
= IVAL(rdata
, ofs
);
1051 if (len
> data_len
- ofs
) {
1054 if (len
== 0) break;
1058 *pnum_streams
= num_streams
;
1059 *pstreams
= streams
;
1063 TALLOC_FREE(streams
);
1067 /****************************************************************************
1068 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1069 ****************************************************************************/
1071 NTSTATUS
cli_qfilename(struct cli_state
*cli
, uint16_t fnum
,
1072 TALLOC_CTX
*mem_ctx
, char **_name
)
1074 uint16_t recv_flags2
;
1081 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1082 SMB_QUERY_FILE_NAME_INFO
,
1083 4, CLI_BUFFER_SIZE
, &recv_flags2
,
1084 &rdata
, &num_rdata
);
1085 if (!NT_STATUS_IS_OK(status
)) {
1089 namelen
= IVAL(rdata
, 0);
1090 if (namelen
> (num_rdata
- 4)) {
1092 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1095 clistr_pull_talloc(mem_ctx
,
1096 (const char *)rdata
,
1103 status
= map_nt_error_from_unix(errno
);
1110 return NT_STATUS_OK
;
1113 /****************************************************************************
1114 Send a qfileinfo call.
1115 ****************************************************************************/
1117 NTSTATUS
cli_qfileinfo_basic(struct cli_state
*cli
, uint16_t fnum
,
1118 uint16
*mode
, off_t
*size
,
1119 struct timespec
*create_time
,
1120 struct timespec
*access_time
,
1121 struct timespec
*write_time
,
1122 struct timespec
*change_time
,
1129 /* if its a win95 server then fail this - win95 totally screws it
1132 return NT_STATUS_NOT_SUPPORTED
;
1135 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1136 SMB_QUERY_FILE_ALL_INFO
,
1137 68, CLI_BUFFER_SIZE
,
1139 &rdata
, &num_rdata
);
1140 if (!NT_STATUS_IS_OK(status
)) {
1145 *create_time
= interpret_long_date((char *)rdata
+0);
1148 *access_time
= interpret_long_date((char *)rdata
+8);
1151 *write_time
= interpret_long_date((char *)rdata
+16);
1154 *change_time
= interpret_long_date((char *)rdata
+24);
1157 *mode
= SVAL(rdata
, 32);
1160 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
1163 *ino
= IVAL(rdata
, 64);
1167 return NT_STATUS_OK
;
1170 /****************************************************************************
1171 Send a qpathinfo BASIC_INFO call.
1172 ****************************************************************************/
1174 struct cli_qpathinfo_basic_state
{
1179 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
);
1181 struct tevent_req
*cli_qpathinfo_basic_send(TALLOC_CTX
*mem_ctx
,
1182 struct event_context
*ev
,
1183 struct cli_state
*cli
,
1186 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1187 struct cli_qpathinfo_basic_state
*state
= NULL
;
1189 req
= tevent_req_create(mem_ctx
, &state
,
1190 struct cli_qpathinfo_basic_state
);
1194 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
1195 SMB_QUERY_FILE_BASIC_INFO
,
1196 36, CLI_BUFFER_SIZE
);
1197 if (tevent_req_nomem(subreq
, req
)) {
1198 return tevent_req_post(req
, ev
);
1200 tevent_req_set_callback(subreq
, cli_qpathinfo_basic_done
, req
);
1204 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
)
1206 struct tevent_req
*req
= tevent_req_callback_data(
1207 subreq
, struct tevent_req
);
1208 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1209 req
, struct cli_qpathinfo_basic_state
);
1212 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
1214 TALLOC_FREE(subreq
);
1215 if (!NT_STATUS_IS_OK(status
)) {
1216 tevent_req_nterror(req
, status
);
1219 tevent_req_done(req
);
1222 NTSTATUS
cli_qpathinfo_basic_recv(struct tevent_req
*req
,
1223 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1225 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1226 req
, struct cli_qpathinfo_basic_state
);
1229 if (tevent_req_is_nterror(req
, &status
)) {
1233 sbuf
->st_ex_atime
= interpret_long_date((char *)state
->data
+8);
1234 sbuf
->st_ex_mtime
= interpret_long_date((char *)state
->data
+16);
1235 sbuf
->st_ex_ctime
= interpret_long_date((char *)state
->data
+24);
1236 *attributes
= IVAL(state
->data
, 32);
1237 return NT_STATUS_OK
;
1240 NTSTATUS
cli_qpathinfo_basic(struct cli_state
*cli
, const char *name
,
1241 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1243 TALLOC_CTX
*frame
= talloc_stackframe();
1244 struct event_context
*ev
;
1245 struct tevent_req
*req
;
1246 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1248 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1250 * Can't use sync call while an async call is in flight
1252 status
= NT_STATUS_INVALID_PARAMETER
;
1255 ev
= event_context_init(frame
);
1259 req
= cli_qpathinfo_basic_send(frame
, ev
, cli
, name
);
1263 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1266 status
= cli_qpathinfo_basic_recv(req
, sbuf
, attributes
);
1272 /****************************************************************************
1273 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1274 ****************************************************************************/
1276 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
1281 char *converted
= NULL
;
1282 size_t converted_size
= 0;
1285 status
= cli_qpathinfo(talloc_tos(), cli
, fname
,
1286 SMB_QUERY_FILE_ALT_NAME_INFO
,
1287 4, CLI_BUFFER_SIZE
, &rdata
, &num_rdata
);
1288 if (!NT_STATUS_IS_OK(status
)) {
1292 len
= IVAL(rdata
, 0);
1294 if (len
> num_rdata
- 4) {
1295 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1298 /* The returned data is a pushed string, not raw data. */
1299 if (!convert_string_talloc(talloc_tos(),
1300 smbXcli_conn_use_unicode(cli
->conn
) ? CH_UTF16LE
: CH_DOS
,
1306 return NT_STATUS_NO_MEMORY
;
1308 fstrcpy(alt_name
, converted
);
1310 TALLOC_FREE(converted
);
1313 return NT_STATUS_OK
;