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
));
120 if (!strupper_m(p
)) {
127 strlcpy(p
, workstation
,sizeof(param
)-PTR_DIFF(p
,param
));
128 if (!strupper_m(p
)) {
132 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
134 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
138 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
139 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
140 &rparam
, &rprcnt
, /* return params, return size */
141 &rdata
, &rdrcnt
/* return data, return size */
143 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
146 if (cli
->rap_error
== 0) {
147 DEBUG(4,("NetWkstaUserLogon success\n"));
149 * The cli->privileges = SVAL(p, 24); field was set here
150 * but it was not use anywhere else.
152 /* The cli->eff_name field used to be set here
153 but it wasn't used anywhere else. */
155 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
161 return (cli
->rap_error
== 0);
164 /****************************************************************************
165 Call a NetShareEnum - try and browse available connections on a host.
166 ****************************************************************************/
168 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32
, const char *, void *), void *state
)
173 unsigned int rdrcnt
,rprcnt
;
177 /* now send a SMBtrans command with api RNetShareEnum */
179 SSVAL(p
,0,0); /* api number */
181 strlcpy(p
,"WrLeh",sizeof(param
)-PTR_DIFF(p
,param
));
182 p
= skip_string(param
,sizeof(param
),p
);
183 strlcpy(p
,"B13BWz",sizeof(param
)-PTR_DIFF(p
,param
));
184 p
= skip_string(param
,sizeof(param
),p
);
187 * Win2k needs a *smaller* buffer than 0xFFFF here -
188 * it returns "out of server memory" with 0xFFFF !!! JRA.
194 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
195 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
196 &rparam
, &rprcnt
, /* return params, length */
197 &rdata
, &rdrcnt
)) /* return data, length */
199 int res
= rparam
? SVAL(rparam
,0) : -1;
201 if (res
== 0 || res
== ERRmoredata
) {
202 int converter
=SVAL(rparam
,2);
204 char *rdata_end
= rdata
+ rdrcnt
;
206 count
=SVAL(rparam
,4);
209 for (i
=0;i
<count
;i
++,p
+=20) {
217 TALLOC_CTX
*frame
= talloc_stackframe();
219 if (p
+ 20 > rdata_end
) {
226 comment_offset
= (IVAL(p
,16) & 0xFFFF) - converter
;
227 if (comment_offset
< 0 ||
228 comment_offset
> (int)rdrcnt
) {
232 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
234 /* Work out the comment length. */
235 for (p1
= cmnt
, len
= 0; *p1
&&
236 p1
< rdata_end
; len
++)
241 pull_string_talloc(frame
,rdata
,0,
242 &s1
,sname
,14,STR_ASCII
);
243 pull_string_talloc(frame
,rdata
,0,
244 &s2
,cmnt
,len
,STR_ASCII
);
250 fn(s1
, type
, s2
, state
);
255 DEBUG(4,("NetShareEnum res=%d\n", res
));
258 DEBUG(4,("NetShareEnum failed\n"));
267 /****************************************************************************
268 Call a NetServerEnum for the specified workgroup and servertype mask. This
269 function then calls the specified callback function for each name returned.
271 The callback function takes 4 arguments: the machine name, the server type,
272 the comment and a state pointer.
273 ****************************************************************************/
275 bool cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32 stype
,
276 void (*fn
)(const char *, uint32
, const char *, void *),
281 char *rdata_end
= NULL
;
282 unsigned int rdrcnt
,rprcnt
;
287 uint32 func
= RAP_NetServerEnum2
;
288 char *last_entry
= NULL
;
293 errno
= 0; /* reset */
296 * This may take more than one transaction, so we should loop until
297 * we no longer get a more data to process or we have all of the
301 /* send a SMBtrans command with api NetServerEnum */
303 SIVAL(p
,0,func
); /* api number */
306 if (func
== RAP_NetServerEnum3
) {
307 strlcpy(p
,"WrLehDzz", sizeof(param
)-PTR_DIFF(p
,param
));
309 strlcpy(p
,"WrLehDz", sizeof(param
)-PTR_DIFF(p
,param
));
312 p
= skip_string(param
, sizeof(param
), p
);
313 strlcpy(p
,"B16BBDz", sizeof(param
)-PTR_DIFF(p
,param
));
315 p
= skip_string(param
, sizeof(param
), p
);
317 SSVAL(p
,2,CLI_BUFFER_SIZE
);
322 /* If we have more data, tell the server where
327 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
328 STR_TERMINATE
|STR_UPPER
);
330 if (len
== (size_t)-1) {
331 SAFE_FREE(last_entry
);
336 if (func
== RAP_NetServerEnum3
) {
338 last_entry
? last_entry
: "",
339 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
342 if (len
== (size_t)-1) {
343 SAFE_FREE(last_entry
);
349 /* Next time through we need to use the continue api */
350 func
= RAP_NetServerEnum3
;
353 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
354 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
355 &rparam
, &rprcnt
, /* return params, return size */
356 &rdata
, &rdrcnt
)) { /* return data, return size */
358 /* break out of the loop on error */
363 rdata_end
= rdata
+ rdrcnt
;
364 res
= rparam
? SVAL(rparam
,0) : -1;
366 if (res
== 0 || res
== ERRmoredata
||
367 (res
!= -1 && cli_errno(cli
) == 0)) {
370 int converter
=SVAL(rparam
,2);
372 /* Get the number of items returned in this buffer */
373 count
= SVAL(rparam
, 4);
375 /* The next field contains the number of items left,
376 * including those returned in this buffer. So the
377 * first time through this should contain all of the
380 if (total_cnt
== 0) {
381 total_cnt
= SVAL(rparam
, 6);
384 /* Keep track of how many we have read */
388 /* The last name in the previous NetServerEnum reply is
389 * sent back to server in the NetServerEnum3 request
390 * (last_entry). The next reply should repeat this entry
391 * as the first element. We have no proof that this is
392 * always true, but from traces that seems to be the
393 * behavior from Window Servers. So first lets do a lot
394 * of checking, just being paranoid. If the string
395 * matches then we already saw this entry so skip it.
397 * NOTE: sv1_name field must be null terminated and has
398 * a max size of 16 (NetBIOS Name).
400 if (last_entry
&& count
&& p
&&
401 (strncmp(last_entry
, p
, 16) == 0)) {
402 count
-= 1; /* Skip this entry */
403 return_cnt
= -1; /* Not part of total, so don't count. */
404 p
= rdata
+ 26; /* Skip the whole record */
407 for (i
= 0; i
< count
; i
++, p
+= 26) {
412 TALLOC_CTX
*frame
= talloc_stackframe();
413 uint32_t entry_stype
;
415 if (p
+ 26 > rdata_end
) {
421 comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
422 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
424 if (comment_offset
< 0 || comment_offset
>= (int)rdrcnt
) {
429 /* Work out the comment length. */
430 for (p1
= cmnt
, len
= 0; *p1
&&
431 p1
< rdata_end
; len
++)
437 entry_stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
439 pull_string_talloc(frame
,rdata
,0,
440 &s1
,sname
,16,STR_ASCII
);
441 pull_string_talloc(frame
,rdata
,0,
442 &s2
,cmnt
,len
,STR_ASCII
);
449 fn(s1
, entry_stype
, s2
, state
);
453 /* We are done with the old last entry, so now we can free it */
455 SAFE_FREE(last_entry
); /* This will set it to null */
458 /* We always make a copy of the last entry if we have one */
460 last_entry
= smb_xstrdup(sname
);
463 /* If we have more data, but no last entry then error out */
464 if (!last_entry
&& (res
== ERRmoredata
)) {
473 } while ((res
== ERRmoredata
) && (total_cnt
> return_cnt
));
477 SAFE_FREE(last_entry
);
480 errno
= cli_errno(cli
);
483 /* this is a very special case, when the domain master for the
484 work group isn't part of the work group itself, there is something
490 return(return_cnt
> 0);
493 /****************************************************************************
494 Send a SamOEMChangePassword command.
495 ****************************************************************************/
497 bool cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
498 const char *old_password
)
501 unsigned char data
[532];
503 unsigned char old_pw_hash
[16];
504 unsigned char new_pw_hash
[16];
505 unsigned int data_len
;
506 unsigned int param_len
= 0;
509 unsigned int rprcnt
, rdrcnt
;
511 if (strlen(user
) >= sizeof(fstring
)-1) {
512 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
516 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
518 strlcpy(p
, "zsT", sizeof(param
)-PTR_DIFF(p
,param
));
519 p
= skip_string(param
,sizeof(param
),p
);
520 strlcpy(p
, "B516B16", sizeof(param
)-PTR_DIFF(p
,param
));
521 p
= skip_string(param
,sizeof(param
),p
);
522 strlcpy(p
,user
, sizeof(param
)-PTR_DIFF(p
,param
));
523 p
= skip_string(param
,sizeof(param
),p
);
527 param_len
= PTR_DIFF(p
,param
);
530 * Get the Lanman hash of the old password, we
531 * use this as the key to make_oem_passwd_hash().
533 E_deshash(old_password
, old_pw_hash
);
535 encode_pw_buffer(data
, new_password
, STR_ASCII
);
537 #ifdef DEBUG_PASSWORD
538 DEBUG(100,("make_oem_passwd_hash\n"));
539 dump_data(100, data
, 516);
541 arcfour_crypt( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
544 * Now place the old password hash in the data.
546 E_deshash(new_password
, new_pw_hash
);
548 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
553 param
, param_len
, 4, /* param, length, max */
554 (char *)data
, data_len
, 0, /* data, length, max */
557 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
563 cli
->rap_error
= SVAL(rparam
,0);
569 return (cli
->rap_error
== 0);
572 /****************************************************************************
573 Send a qpathinfo call.
574 ****************************************************************************/
576 struct cli_qpathinfo1_state
{
577 struct cli_state
*cli
;
582 static void cli_qpathinfo1_done(struct tevent_req
*subreq
);
584 struct tevent_req
*cli_qpathinfo1_send(TALLOC_CTX
*mem_ctx
,
585 struct event_context
*ev
,
586 struct cli_state
*cli
,
589 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
590 struct cli_qpathinfo1_state
*state
= NULL
;
592 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo1_state
);
597 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_INFO_STANDARD
,
598 22, CLI_BUFFER_SIZE
);
599 if (tevent_req_nomem(subreq
, req
)) {
600 return tevent_req_post(req
, ev
);
602 tevent_req_set_callback(subreq
, cli_qpathinfo1_done
, req
);
606 static void cli_qpathinfo1_done(struct tevent_req
*subreq
)
608 struct tevent_req
*req
= tevent_req_callback_data(
609 subreq
, struct tevent_req
);
610 struct cli_qpathinfo1_state
*state
= tevent_req_data(
611 req
, struct cli_qpathinfo1_state
);
614 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
617 if (!NT_STATUS_IS_OK(status
)) {
618 tevent_req_nterror(req
, status
);
621 tevent_req_done(req
);
624 NTSTATUS
cli_qpathinfo1_recv(struct tevent_req
*req
,
631 struct cli_qpathinfo1_state
*state
= tevent_req_data(
632 req
, struct cli_qpathinfo1_state
);
635 time_t (*date_fn
)(const void *buf
, int serverzone
);
637 if (tevent_req_is_nterror(req
, &status
)) {
641 if (state
->cli
->win95
) {
642 date_fn
= make_unix_date
;
644 date_fn
= make_unix_date2
;
648 *change_time
= date_fn(state
->data
+0, smb1cli_conn_server_time_zone(state
->cli
->conn
));
651 *access_time
= date_fn(state
->data
+4, smb1cli_conn_server_time_zone(state
->cli
->conn
));
654 *write_time
= date_fn(state
->data
+8, smb1cli_conn_server_time_zone(state
->cli
->conn
));
657 *size
= IVAL(state
->data
, 12);
660 *mode
= SVAL(state
->data
, l1_attrFile
);
665 NTSTATUS
cli_qpathinfo1(struct cli_state
*cli
,
673 TALLOC_CTX
*frame
= talloc_stackframe();
674 struct event_context
*ev
;
675 struct tevent_req
*req
;
676 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
678 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
680 * Can't use sync call while an async call is in flight
682 status
= NT_STATUS_INVALID_PARAMETER
;
685 ev
= event_context_init(frame
);
689 req
= cli_qpathinfo1_send(frame
, ev
, cli
, fname
);
693 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
696 status
= cli_qpathinfo1_recv(req
, change_time
, access_time
,
697 write_time
, size
, mode
);
703 /****************************************************************************
704 Send a setpathinfo call.
705 ****************************************************************************/
707 NTSTATUS
cli_setpathinfo_basic(struct cli_state
*cli
, const char *fname
,
714 unsigned int data_len
= 0;
721 * Add the create, last access, modification, and status change times
723 put_long_date(p
, create_time
);
726 put_long_date(p
, access_time
);
729 put_long_date(p
, write_time
);
732 put_long_date(p
, change_time
);
743 data_len
= PTR_DIFF(p
, data
);
745 return cli_setpathinfo(cli
, SMB_FILE_BASIC_INFORMATION
, fname
,
746 (uint8_t *)data
, data_len
);
749 /****************************************************************************
750 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
751 ****************************************************************************/
753 struct cli_qpathinfo2_state
{
758 static void cli_qpathinfo2_done(struct tevent_req
*subreq
);
760 struct tevent_req
*cli_qpathinfo2_send(TALLOC_CTX
*mem_ctx
,
761 struct event_context
*ev
,
762 struct cli_state
*cli
,
765 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
766 struct cli_qpathinfo2_state
*state
= NULL
;
768 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo2_state
);
772 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
773 SMB_QUERY_FILE_ALL_INFO
,
774 68, CLI_BUFFER_SIZE
);
775 if (tevent_req_nomem(subreq
, req
)) {
776 return tevent_req_post(req
, ev
);
778 tevent_req_set_callback(subreq
, cli_qpathinfo2_done
, req
);
782 static void cli_qpathinfo2_done(struct tevent_req
*subreq
)
784 struct tevent_req
*req
= tevent_req_callback_data(
785 subreq
, struct tevent_req
);
786 struct cli_qpathinfo2_state
*state
= tevent_req_data(
787 req
, struct cli_qpathinfo2_state
);
790 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
793 if (!NT_STATUS_IS_OK(status
)) {
794 tevent_req_nterror(req
, status
);
797 tevent_req_done(req
);
800 NTSTATUS
cli_qpathinfo2_recv(struct tevent_req
*req
,
801 struct timespec
*create_time
,
802 struct timespec
*access_time
,
803 struct timespec
*write_time
,
804 struct timespec
*change_time
,
805 off_t
*size
, uint16
*mode
,
808 struct cli_qpathinfo2_state
*state
= tevent_req_data(
809 req
, struct cli_qpathinfo2_state
);
812 if (tevent_req_is_nterror(req
, &status
)) {
817 *create_time
= interpret_long_date((char *)state
->data
+0);
820 *access_time
= interpret_long_date((char *)state
->data
+8);
823 *write_time
= interpret_long_date((char *)state
->data
+16);
826 *change_time
= interpret_long_date((char *)state
->data
+24);
829 *mode
= SVAL(state
->data
, 32);
832 *size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,48);
835 *ino
= IVAL(state
->data
, 64);
840 NTSTATUS
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
841 struct timespec
*create_time
,
842 struct timespec
*access_time
,
843 struct timespec
*write_time
,
844 struct timespec
*change_time
,
845 off_t
*size
, uint16
*mode
,
848 TALLOC_CTX
*frame
= talloc_stackframe();
849 struct event_context
*ev
;
850 struct tevent_req
*req
;
851 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
853 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
855 * Can't use sync call while an async call is in flight
857 status
= NT_STATUS_INVALID_PARAMETER
;
860 ev
= event_context_init(frame
);
864 req
= cli_qpathinfo2_send(frame
, ev
, cli
, fname
);
868 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
871 status
= cli_qpathinfo2_recv(req
, create_time
, access_time
,
872 write_time
, change_time
, size
, mode
, ino
);
878 /****************************************************************************
880 ****************************************************************************/
882 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *data
,
884 unsigned int *pnum_streams
,
885 struct stream_struct
**pstreams
);
887 struct cli_qpathinfo_streams_state
{
892 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
);
894 struct tevent_req
*cli_qpathinfo_streams_send(TALLOC_CTX
*mem_ctx
,
895 struct tevent_context
*ev
,
896 struct cli_state
*cli
,
899 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
900 struct cli_qpathinfo_streams_state
*state
= NULL
;
902 req
= tevent_req_create(mem_ctx
, &state
,
903 struct cli_qpathinfo_streams_state
);
907 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
908 SMB_FILE_STREAM_INFORMATION
,
910 if (tevent_req_nomem(subreq
, req
)) {
911 return tevent_req_post(req
, ev
);
913 tevent_req_set_callback(subreq
, cli_qpathinfo_streams_done
, req
);
917 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
)
919 struct tevent_req
*req
= tevent_req_callback_data(
920 subreq
, struct tevent_req
);
921 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
922 req
, struct cli_qpathinfo_streams_state
);
925 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
928 if (!NT_STATUS_IS_OK(status
)) {
929 tevent_req_nterror(req
, status
);
932 tevent_req_done(req
);
935 NTSTATUS
cli_qpathinfo_streams_recv(struct tevent_req
*req
,
937 unsigned int *pnum_streams
,
938 struct stream_struct
**pstreams
)
940 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
941 req
, struct cli_qpathinfo_streams_state
);
944 if (tevent_req_is_nterror(req
, &status
)) {
947 if (!parse_streams_blob(mem_ctx
, state
->data
, state
->num_data
,
948 pnum_streams
, pstreams
)) {
949 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
954 NTSTATUS
cli_qpathinfo_streams(struct cli_state
*cli
, const char *fname
,
956 unsigned int *pnum_streams
,
957 struct stream_struct
**pstreams
)
959 TALLOC_CTX
*frame
= talloc_stackframe();
960 struct event_context
*ev
;
961 struct tevent_req
*req
;
962 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
964 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
966 * Can't use sync call while an async call is in flight
968 status
= NT_STATUS_INVALID_PARAMETER
;
971 ev
= event_context_init(frame
);
975 req
= cli_qpathinfo_streams_send(frame
, ev
, cli
, fname
);
979 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
982 status
= cli_qpathinfo_streams_recv(req
, mem_ctx
, pnum_streams
,
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(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
))
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
,
1076 TALLOC_CTX
*mem_ctx
, char **_name
)
1078 uint16_t recv_flags2
;
1085 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1086 SMB_QUERY_FILE_NAME_INFO
,
1087 4, CLI_BUFFER_SIZE
, &recv_flags2
,
1088 &rdata
, &num_rdata
);
1089 if (!NT_STATUS_IS_OK(status
)) {
1093 namelen
= IVAL(rdata
, 0);
1094 if (namelen
> (num_rdata
- 4)) {
1096 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1099 clistr_pull_talloc(mem_ctx
,
1100 (const char *)rdata
,
1107 status
= map_nt_error_from_unix(errno
);
1114 return NT_STATUS_OK
;
1117 /****************************************************************************
1118 Send a qfileinfo call.
1119 ****************************************************************************/
1121 NTSTATUS
cli_qfileinfo_basic(struct cli_state
*cli
, uint16_t fnum
,
1122 uint16
*mode
, off_t
*size
,
1123 struct timespec
*create_time
,
1124 struct timespec
*access_time
,
1125 struct timespec
*write_time
,
1126 struct timespec
*change_time
,
1133 /* if its a win95 server then fail this - win95 totally screws it
1136 return NT_STATUS_NOT_SUPPORTED
;
1139 status
= cli_qfileinfo(talloc_tos(), cli
, fnum
,
1140 SMB_QUERY_FILE_ALL_INFO
,
1141 68, CLI_BUFFER_SIZE
,
1143 &rdata
, &num_rdata
);
1144 if (!NT_STATUS_IS_OK(status
)) {
1149 *create_time
= interpret_long_date((char *)rdata
+0);
1152 *access_time
= interpret_long_date((char *)rdata
+8);
1155 *write_time
= interpret_long_date((char *)rdata
+16);
1158 *change_time
= interpret_long_date((char *)rdata
+24);
1161 *mode
= SVAL(rdata
, 32);
1164 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
1167 *ino
= IVAL(rdata
, 64);
1171 return NT_STATUS_OK
;
1174 /****************************************************************************
1175 Send a qpathinfo BASIC_INFO call.
1176 ****************************************************************************/
1178 struct cli_qpathinfo_basic_state
{
1183 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
);
1185 struct tevent_req
*cli_qpathinfo_basic_send(TALLOC_CTX
*mem_ctx
,
1186 struct event_context
*ev
,
1187 struct cli_state
*cli
,
1190 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1191 struct cli_qpathinfo_basic_state
*state
= NULL
;
1193 req
= tevent_req_create(mem_ctx
, &state
,
1194 struct cli_qpathinfo_basic_state
);
1198 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
1199 SMB_QUERY_FILE_BASIC_INFO
,
1200 36, CLI_BUFFER_SIZE
);
1201 if (tevent_req_nomem(subreq
, req
)) {
1202 return tevent_req_post(req
, ev
);
1204 tevent_req_set_callback(subreq
, cli_qpathinfo_basic_done
, req
);
1208 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
)
1210 struct tevent_req
*req
= tevent_req_callback_data(
1211 subreq
, struct tevent_req
);
1212 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1213 req
, struct cli_qpathinfo_basic_state
);
1216 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
1218 TALLOC_FREE(subreq
);
1219 if (!NT_STATUS_IS_OK(status
)) {
1220 tevent_req_nterror(req
, status
);
1223 tevent_req_done(req
);
1226 NTSTATUS
cli_qpathinfo_basic_recv(struct tevent_req
*req
,
1227 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1229 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1230 req
, struct cli_qpathinfo_basic_state
);
1233 if (tevent_req_is_nterror(req
, &status
)) {
1237 sbuf
->st_ex_atime
= interpret_long_date((char *)state
->data
+8);
1238 sbuf
->st_ex_mtime
= interpret_long_date((char *)state
->data
+16);
1239 sbuf
->st_ex_ctime
= interpret_long_date((char *)state
->data
+24);
1240 *attributes
= IVAL(state
->data
, 32);
1241 return NT_STATUS_OK
;
1244 NTSTATUS
cli_qpathinfo_basic(struct cli_state
*cli
, const char *name
,
1245 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1247 TALLOC_CTX
*frame
= talloc_stackframe();
1248 struct event_context
*ev
;
1249 struct tevent_req
*req
;
1250 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1252 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1254 * Can't use sync call while an async call is in flight
1256 status
= NT_STATUS_INVALID_PARAMETER
;
1259 ev
= event_context_init(frame
);
1263 req
= cli_qpathinfo_basic_send(frame
, ev
, cli
, name
);
1267 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1270 status
= cli_qpathinfo_basic_recv(req
, sbuf
, attributes
);
1276 /****************************************************************************
1277 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1278 ****************************************************************************/
1280 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
1285 char *converted
= NULL
;
1286 size_t converted_size
= 0;
1289 status
= cli_qpathinfo(talloc_tos(), cli
, fname
,
1290 SMB_QUERY_FILE_ALT_NAME_INFO
,
1291 4, CLI_BUFFER_SIZE
, &rdata
, &num_rdata
);
1292 if (!NT_STATUS_IS_OK(status
)) {
1296 len
= IVAL(rdata
, 0);
1298 if (len
> num_rdata
- 4) {
1299 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1302 /* The returned data is a pushed string, not raw data. */
1303 if (!convert_string_talloc(talloc_tos(),
1304 smbXcli_conn_use_unicode(cli
->conn
) ? CH_UTF16LE
: CH_DOS
,
1310 return NT_STATUS_NO_MEMORY
;
1312 fstrcpy(alt_name
, converted
);
1314 TALLOC_FREE(converted
);
1317 return NT_STATUS_OK
;