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 /****************************************************************************
30 ****************************************************************************/
32 bool cli_api(struct cli_state
*cli
,
33 char *param
, int prcnt
, int mprcnt
,
34 char *data
, int drcnt
, int mdrcnt
,
35 char **rparam
, unsigned int *rprcnt
,
36 char **rdata
, unsigned int *rdrcnt
)
38 cli_send_trans(cli
,SMBtrans
,
39 PIPE_LANMAN
, /* Name */
41 NULL
,0,0, /* Setup, length, max */
42 param
, prcnt
, mprcnt
, /* Params, length, max */
43 data
, drcnt
, mdrcnt
/* Data, length, max */
46 return (cli_receive_trans(cli
,SMBtrans
,
51 /****************************************************************************
52 Perform a NetWkstaUserLogon.
53 ****************************************************************************/
55 bool cli_NetWkstaUserLogon(struct cli_state
*cli
,char *user
, char *workstation
)
60 unsigned int rdrcnt
,rprcnt
;
63 memset(param
, 0, sizeof(param
));
65 /* send a SMBtrans command with api NetWkstaUserLogon */
67 SSVAL(p
,0,132); /* api number */
69 strlcpy(p
,"OOWb54WrLh",sizeof(param
)-PTR_DIFF(p
,param
));
70 p
= skip_string(param
,sizeof(param
),p
);
71 strlcpy(p
,"WB21BWDWWDDDDDDDzzzD",sizeof(param
)-PTR_DIFF(p
,param
));
72 p
= skip_string(param
,sizeof(param
),p
);
75 strlcpy(p
,user
,sizeof(param
)-PTR_DIFF(p
,param
));
81 strlcpy(p
, workstation
,sizeof(param
)-PTR_DIFF(p
,param
));
84 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
86 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
90 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
91 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
92 &rparam
, &rprcnt
, /* return params, return size */
93 &rdata
, &rdrcnt
/* return data, return size */
95 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
98 if (cli
->rap_error
== 0) {
99 DEBUG(4,("NetWkstaUserLogon success\n"));
100 cli
->privileges
= SVAL(p
, 24);
101 /* The cli->eff_name field used to be set here
102 but it wasn't used anywhere else. */
104 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
110 return (cli
->rap_error
== 0);
113 /****************************************************************************
114 Call a NetShareEnum - try and browse available connections on a host.
115 ****************************************************************************/
117 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32
, const char *, void *), void *state
)
122 unsigned int rdrcnt
,rprcnt
;
126 /* now send a SMBtrans command with api RNetShareEnum */
128 SSVAL(p
,0,0); /* api number */
130 strlcpy(p
,"WrLeh",sizeof(param
)-PTR_DIFF(p
,param
));
131 p
= skip_string(param
,sizeof(param
),p
);
132 strlcpy(p
,"B13BWz",sizeof(param
)-PTR_DIFF(p
,param
));
133 p
= skip_string(param
,sizeof(param
),p
);
136 * Win2k needs a *smaller* buffer than 0xFFFF here -
137 * it returns "out of server memory" with 0xFFFF !!! JRA.
143 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
144 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
145 &rparam
, &rprcnt
, /* return params, length */
146 &rdata
, &rdrcnt
)) /* return data, length */
148 int res
= rparam
? SVAL(rparam
,0) : -1;
150 if (res
== 0 || res
== ERRmoredata
) {
151 int converter
=SVAL(rparam
,2);
153 char *rdata_end
= rdata
+ rdrcnt
;
155 count
=SVAL(rparam
,4);
158 for (i
=0;i
<count
;i
++,p
+=20) {
166 TALLOC_CTX
*frame
= talloc_stackframe();
168 if (p
+ 20 > rdata_end
) {
175 comment_offset
= (IVAL(p
,16) & 0xFFFF) - converter
;
176 if (comment_offset
< 0 ||
177 comment_offset
> (int)rdrcnt
) {
181 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
183 /* Work out the comment length. */
184 for (p1
= cmnt
, len
= 0; *p1
&&
185 p1
< rdata_end
; len
++)
190 pull_string_talloc(frame
,rdata
,0,
191 &s1
,sname
,14,STR_ASCII
);
192 pull_string_talloc(frame
,rdata
,0,
193 &s2
,cmnt
,len
,STR_ASCII
);
199 fn(s1
, type
, s2
, state
);
204 DEBUG(4,("NetShareEnum res=%d\n", res
));
207 DEBUG(4,("NetShareEnum failed\n"));
216 /****************************************************************************
217 Call a NetServerEnum for the specified workgroup and servertype mask. This
218 function then calls the specified callback function for each name returned.
220 The callback function takes 4 arguments: the machine name, the server type,
221 the comment and a state pointer.
222 ****************************************************************************/
224 bool cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32 stype
,
225 void (*fn
)(const char *, uint32
, const char *, void *),
230 char *rdata_end
= NULL
;
231 unsigned int rdrcnt
,rprcnt
;
236 uint32 func
= RAP_NetServerEnum2
;
237 char *last_entry
= NULL
;
242 errno
= 0; /* reset */
245 * This may take more than one transaction, so we should loop until
246 * we no longer get a more data to process or we have all of the
250 /* send a SMBtrans command with api NetServerEnum */
252 SIVAL(p
,0,func
); /* api number */
255 if (func
== RAP_NetServerEnum3
) {
256 strlcpy(p
,"WrLehDzz", sizeof(param
)-PTR_DIFF(p
,param
));
258 strlcpy(p
,"WrLehDz", sizeof(param
)-PTR_DIFF(p
,param
));
261 p
= skip_string(param
, sizeof(param
), p
);
262 strlcpy(p
,"B16BBDz", sizeof(param
)-PTR_DIFF(p
,param
));
264 p
= skip_string(param
, sizeof(param
), p
);
266 SSVAL(p
,2,CLI_BUFFER_SIZE
);
271 /* If we have more data, tell the server where
276 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
277 STR_TERMINATE
|STR_UPPER
);
279 if (len
== (size_t)-1) {
280 SAFE_FREE(last_entry
);
285 if (func
== RAP_NetServerEnum3
) {
287 last_entry
? last_entry
: "",
288 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
291 if (len
== (size_t)-1) {
292 SAFE_FREE(last_entry
);
298 /* Next time through we need to use the continue api */
299 func
= RAP_NetServerEnum3
;
302 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
303 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
304 &rparam
, &rprcnt
, /* return params, return size */
305 &rdata
, &rdrcnt
)) { /* return data, return size */
307 /* break out of the loop on error */
312 rdata_end
= rdata
+ rdrcnt
;
313 res
= rparam
? SVAL(rparam
,0) : -1;
315 if (res
== 0 || res
== ERRmoredata
||
316 (res
!= -1 && cli_errno(cli
) == 0)) {
319 int converter
=SVAL(rparam
,2);
321 /* Get the number of items returned in this buffer */
322 count
= SVAL(rparam
, 4);
324 /* The next field contains the number of items left,
325 * including those returned in this buffer. So the
326 * first time through this should contain all of the
329 if (total_cnt
== 0) {
330 total_cnt
= SVAL(rparam
, 6);
333 /* Keep track of how many we have read */
337 /* The last name in the previous NetServerEnum reply is
338 * sent back to server in the NetServerEnum3 request
339 * (last_entry). The next reply should repeat this entry
340 * as the first element. We have no proof that this is
341 * always true, but from traces that seems to be the
342 * behavior from Window Servers. So first lets do a lot
343 * of checking, just being paranoid. If the string
344 * matches then we already saw this entry so skip it.
346 * NOTE: sv1_name field must be null terminated and has
347 * a max size of 16 (NetBIOS Name).
349 if (last_entry
&& count
&& p
&&
350 (strncmp(last_entry
, p
, 16) == 0)) {
351 count
-= 1; /* Skip this entry */
352 return_cnt
= -1; /* Not part of total, so don't count. */
353 p
= rdata
+ 26; /* Skip the whole record */
356 for (i
= 0; i
< count
; i
++, p
+= 26) {
361 TALLOC_CTX
*frame
= talloc_stackframe();
362 uint32_t entry_stype
;
364 if (p
+ 26 > rdata_end
) {
370 comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
371 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
373 if (comment_offset
< 0 || comment_offset
>= (int)rdrcnt
) {
378 /* Work out the comment length. */
379 for (p1
= cmnt
, len
= 0; *p1
&&
380 p1
< rdata_end
; len
++)
386 entry_stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
388 pull_string_talloc(frame
,rdata
,0,
389 &s1
,sname
,16,STR_ASCII
);
390 pull_string_talloc(frame
,rdata
,0,
391 &s2
,cmnt
,len
,STR_ASCII
);
398 fn(s1
, entry_stype
, s2
, state
);
402 /* We are done with the old last entry, so now we can free it */
404 SAFE_FREE(last_entry
); /* This will set it to null */
407 /* We always make a copy of the last entry if we have one */
409 last_entry
= smb_xstrdup(sname
);
412 /* If we have more data, but no last entry then error out */
413 if (!last_entry
&& (res
== ERRmoredata
)) {
422 } while ((res
== ERRmoredata
) && (total_cnt
> return_cnt
));
426 SAFE_FREE(last_entry
);
429 errno
= cli_errno(cli
);
432 /* this is a very special case, when the domain master for the
433 work group isn't part of the work group itself, there is something
439 return(return_cnt
> 0);
442 /****************************************************************************
443 Send a SamOEMChangePassword command.
444 ****************************************************************************/
446 bool cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
447 const char *old_password
)
450 unsigned char data
[532];
452 unsigned char old_pw_hash
[16];
453 unsigned char new_pw_hash
[16];
454 unsigned int data_len
;
455 unsigned int param_len
= 0;
458 unsigned int rprcnt
, rdrcnt
;
460 if (strlen(user
) >= sizeof(fstring
)-1) {
461 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
465 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
467 strlcpy(p
, "zsT", sizeof(param
)-PTR_DIFF(p
,param
));
468 p
= skip_string(param
,sizeof(param
),p
);
469 strlcpy(p
, "B516B16", sizeof(param
)-PTR_DIFF(p
,param
));
470 p
= skip_string(param
,sizeof(param
),p
);
471 strlcpy(p
,user
, sizeof(param
)-PTR_DIFF(p
,param
));
472 p
= skip_string(param
,sizeof(param
),p
);
476 param_len
= PTR_DIFF(p
,param
);
479 * Get the Lanman hash of the old password, we
480 * use this as the key to make_oem_passwd_hash().
482 E_deshash(old_password
, old_pw_hash
);
484 encode_pw_buffer(data
, new_password
, STR_ASCII
);
486 #ifdef DEBUG_PASSWORD
487 DEBUG(100,("make_oem_passwd_hash\n"));
488 dump_data(100, data
, 516);
490 arcfour_crypt( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
493 * Now place the old password hash in the data.
495 E_deshash(new_password
, new_pw_hash
);
497 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
501 if (cli_send_trans(cli
,SMBtrans
,
502 PIPE_LANMAN
, /* name */
503 0,0, /* fid, flags */
504 NULL
,0,0, /* setup, length, max */
505 param
,param_len
,4, /* param, length, max */
506 (char *)data
,data_len
,0 /* data, length, max */
508 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
513 if (!cli_receive_trans(cli
,SMBtrans
,
516 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
522 cli
->rap_error
= SVAL(rparam
,0);
528 return (cli
->rap_error
== 0);
531 /****************************************************************************
532 Send a qpathinfo call.
533 ****************************************************************************/
535 struct cli_qpathinfo1_state
{
536 struct cli_state
*cli
;
541 static void cli_qpathinfo1_done(struct tevent_req
*subreq
);
543 struct tevent_req
*cli_qpathinfo1_send(TALLOC_CTX
*mem_ctx
,
544 struct event_context
*ev
,
545 struct cli_state
*cli
,
548 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
549 struct cli_qpathinfo1_state
*state
= NULL
;
551 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo1_state
);
556 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_INFO_STANDARD
,
558 if (tevent_req_nomem(subreq
, req
)) {
559 return tevent_req_post(req
, ev
);
561 tevent_req_set_callback(subreq
, cli_qpathinfo1_done
, req
);
565 static void cli_qpathinfo1_done(struct tevent_req
*subreq
)
567 struct tevent_req
*req
= tevent_req_callback_data(
568 subreq
, struct tevent_req
);
569 struct cli_qpathinfo1_state
*state
= tevent_req_data(
570 req
, struct cli_qpathinfo1_state
);
573 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
576 if (!NT_STATUS_IS_OK(status
)) {
577 tevent_req_nterror(req
, status
);
580 tevent_req_done(req
);
583 NTSTATUS
cli_qpathinfo1_recv(struct tevent_req
*req
,
590 struct cli_qpathinfo1_state
*state
= tevent_req_data(
591 req
, struct cli_qpathinfo1_state
);
594 time_t (*date_fn
)(const void *buf
, int serverzone
);
596 if (tevent_req_is_nterror(req
, &status
)) {
600 if (state
->cli
->win95
) {
601 date_fn
= make_unix_date
;
603 date_fn
= make_unix_date2
;
607 *change_time
= date_fn(state
->data
+0, state
->cli
->serverzone
);
610 *access_time
= date_fn(state
->data
+4, state
->cli
->serverzone
);
613 *write_time
= date_fn(state
->data
+8, state
->cli
->serverzone
);
616 *size
= IVAL(state
->data
, 12);
619 *mode
= SVAL(state
->data
, l1_attrFile
);
624 NTSTATUS
cli_qpathinfo1(struct cli_state
*cli
,
632 TALLOC_CTX
*frame
= talloc_stackframe();
633 struct event_context
*ev
;
634 struct tevent_req
*req
;
635 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
637 if (cli_has_async_calls(cli
)) {
639 * Can't use sync call while an async call is in flight
641 status
= NT_STATUS_INVALID_PARAMETER
;
644 ev
= event_context_init(frame
);
648 req
= cli_qpathinfo1_send(frame
, ev
, cli
, fname
);
652 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
655 status
= cli_qpathinfo1_recv(req
, change_time
, access_time
,
656 write_time
, size
, mode
);
659 if (!NT_STATUS_IS_OK(status
)) {
660 cli_set_error(cli
, status
);
665 /****************************************************************************
666 Send a setpathinfo call.
667 ****************************************************************************/
669 bool cli_setpathinfo(struct cli_state
*cli
, const char *fname
,
676 unsigned int data_len
= 0;
677 unsigned int param_len
= 0;
678 unsigned int rparam_len
, rdata_len
;
679 uint16 setup
= TRANSACT2_SETPATHINFO
;
682 char *rparam
=NULL
, *rdata
=NULL
;
686 size_t nlen
= 2*(strlen(fname
)+1);
688 param
= SMB_MALLOC_ARRAY(char, 6+nlen
+2);
692 memset(param
, '\0', 6);
693 memset(data
, 0, sizeof(data
));
697 /* Add the information level */
698 SSVAL(p
, 0, SMB_FILE_BASIC_INFORMATION
);
703 /* Add the file name */
704 p
+= clistr_push(cli
, p
, fname
, nlen
, STR_TERMINATE
);
706 param_len
= PTR_DIFF(p
, param
);
711 * Add the create, last access, modification, and status change times
713 put_long_date(p
, create_time
);
716 put_long_date(p
, access_time
);
719 put_long_date(p
, write_time
);
722 put_long_date(p
, change_time
);
733 data_len
= PTR_DIFF(p
, data
);
736 ret
= (cli_send_trans(cli
, SMBtrans2
,
738 -1, 0, /* fid, flags */
739 &setup
, 1, 0, /* setup, length, max */
740 param
, param_len
, 10, /* param, length, max */
741 data
, data_len
, cli
->max_xmit
/* data, length, max */
743 cli_receive_trans(cli
, SMBtrans2
,
744 &rparam
, &rparam_len
,
745 &rdata
, &rdata_len
));
746 if (!cli_is_dos_error(cli
)) break;
748 /* we need to work around a Win95 bug - sometimes
749 it gives ERRSRV/ERRerror temprarily */
752 cli_dos_error(cli
, &eclass
, &ecode
);
753 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
756 } while (count
-- && ret
==False
);
768 /****************************************************************************
769 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
770 ****************************************************************************/
772 struct cli_qpathinfo2_state
{
777 static void cli_qpathinfo2_done(struct tevent_req
*subreq
);
779 struct tevent_req
*cli_qpathinfo2_send(TALLOC_CTX
*mem_ctx
,
780 struct event_context
*ev
,
781 struct cli_state
*cli
,
784 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
785 struct cli_qpathinfo2_state
*state
= NULL
;
787 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo2_state
);
791 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
792 SMB_QUERY_FILE_ALL_INFO
,
794 if (tevent_req_nomem(subreq
, req
)) {
795 return tevent_req_post(req
, ev
);
797 tevent_req_set_callback(subreq
, cli_qpathinfo2_done
, req
);
801 static void cli_qpathinfo2_done(struct tevent_req
*subreq
)
803 struct tevent_req
*req
= tevent_req_callback_data(
804 subreq
, struct tevent_req
);
805 struct cli_qpathinfo2_state
*state
= tevent_req_data(
806 req
, struct cli_qpathinfo2_state
);
809 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
812 if (!NT_STATUS_IS_OK(status
)) {
813 tevent_req_nterror(req
, status
);
816 tevent_req_done(req
);
819 NTSTATUS
cli_qpathinfo2_recv(struct tevent_req
*req
,
820 struct timespec
*create_time
,
821 struct timespec
*access_time
,
822 struct timespec
*write_time
,
823 struct timespec
*change_time
,
824 SMB_OFF_T
*size
, uint16
*mode
,
827 struct cli_qpathinfo2_state
*state
= tevent_req_data(
828 req
, struct cli_qpathinfo2_state
);
831 if (tevent_req_is_nterror(req
, &status
)) {
836 *create_time
= interpret_long_date((char *)state
->data
+0);
839 *access_time
= interpret_long_date((char *)state
->data
+8);
842 *write_time
= interpret_long_date((char *)state
->data
+16);
845 *change_time
= interpret_long_date((char *)state
->data
+24);
848 *mode
= SVAL(state
->data
, 32);
851 *size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,48);
854 *ino
= IVAL(state
->data
, 64);
859 NTSTATUS
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
860 struct timespec
*create_time
,
861 struct timespec
*access_time
,
862 struct timespec
*write_time
,
863 struct timespec
*change_time
,
864 SMB_OFF_T
*size
, uint16
*mode
,
867 TALLOC_CTX
*frame
= talloc_stackframe();
868 struct event_context
*ev
;
869 struct tevent_req
*req
;
870 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
872 if (cli_has_async_calls(cli
)) {
874 * Can't use sync call while an async call is in flight
876 status
= NT_STATUS_INVALID_PARAMETER
;
879 ev
= event_context_init(frame
);
883 req
= cli_qpathinfo2_send(frame
, ev
, cli
, fname
);
887 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
890 status
= cli_qpathinfo2_recv(req
, create_time
, access_time
,
891 write_time
, change_time
, size
, mode
, ino
);
894 if (!NT_STATUS_IS_OK(status
)) {
895 cli_set_error(cli
, status
);
900 /****************************************************************************
902 ****************************************************************************/
904 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *data
,
906 unsigned int *pnum_streams
,
907 struct stream_struct
**pstreams
);
909 struct cli_qpathinfo_streams_state
{
914 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
);
916 struct tevent_req
*cli_qpathinfo_streams_send(TALLOC_CTX
*mem_ctx
,
917 struct tevent_context
*ev
,
918 struct cli_state
*cli
,
921 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
922 struct cli_qpathinfo_streams_state
*state
= NULL
;
924 req
= tevent_req_create(mem_ctx
, &state
,
925 struct cli_qpathinfo_streams_state
);
929 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
930 SMB_FILE_STREAM_INFORMATION
,
932 if (tevent_req_nomem(subreq
, req
)) {
933 return tevent_req_post(req
, ev
);
935 tevent_req_set_callback(subreq
, cli_qpathinfo_streams_done
, req
);
939 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
)
941 struct tevent_req
*req
= tevent_req_callback_data(
942 subreq
, struct tevent_req
);
943 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
944 req
, struct cli_qpathinfo_streams_state
);
947 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
950 if (!NT_STATUS_IS_OK(status
)) {
951 tevent_req_nterror(req
, status
);
954 tevent_req_done(req
);
957 NTSTATUS
cli_qpathinfo_streams_recv(struct tevent_req
*req
,
959 unsigned int *pnum_streams
,
960 struct stream_struct
**pstreams
)
962 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
963 req
, struct cli_qpathinfo_streams_state
);
966 if (tevent_req_is_nterror(req
, &status
)) {
969 if (!parse_streams_blob(mem_ctx
, state
->data
, state
->num_data
,
970 pnum_streams
, pstreams
)) {
971 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
976 NTSTATUS
cli_qpathinfo_streams(struct cli_state
*cli
, const char *fname
,
978 unsigned int *pnum_streams
,
979 struct stream_struct
**pstreams
)
981 TALLOC_CTX
*frame
= talloc_stackframe();
982 struct event_context
*ev
;
983 struct tevent_req
*req
;
984 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
986 if (cli_has_async_calls(cli
)) {
988 * Can't use sync call while an async call is in flight
990 status
= NT_STATUS_INVALID_PARAMETER
;
993 ev
= event_context_init(frame
);
997 req
= cli_qpathinfo_streams_send(frame
, ev
, cli
, fname
);
1001 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1004 status
= cli_qpathinfo_streams_recv(req
, mem_ctx
, pnum_streams
,
1008 if (!NT_STATUS_IS_OK(status
)) {
1009 cli_set_error(cli
, status
);
1014 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *rdata
,
1016 unsigned int *pnum_streams
,
1017 struct stream_struct
**pstreams
)
1019 unsigned int num_streams
;
1020 struct stream_struct
*streams
;
1027 while ((data_len
> ofs
) && (data_len
- ofs
>= 24)) {
1031 struct stream_struct
*tmp
;
1034 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, streams
,
1035 struct stream_struct
,
1043 nlen
= IVAL(rdata
, ofs
+ 0x04);
1045 streams
[num_streams
].size
= IVAL_TO_SMB_OFF_T(
1047 streams
[num_streams
].alloc_size
= IVAL_TO_SMB_OFF_T(
1050 if (nlen
> data_len
- (ofs
+ 24)) {
1055 * We need to null-terminate src, how do I do this with
1056 * convert_string_talloc??
1059 tmp_buf
= TALLOC_ARRAY(streams
, uint8_t, nlen
+2);
1060 if (tmp_buf
== NULL
) {
1064 memcpy(tmp_buf
, rdata
+ofs
+24, nlen
);
1066 tmp_buf
[nlen
+1] = 0;
1068 if (!convert_string_talloc(streams
, CH_UTF16
, CH_UNIX
, tmp_buf
,
1069 nlen
+2, &vstr
, &size
, false))
1071 TALLOC_FREE(tmp_buf
);
1075 TALLOC_FREE(tmp_buf
);
1076 streams
[num_streams
].name
= (char *)vstr
;
1079 len
= IVAL(rdata
, ofs
);
1080 if (len
> data_len
- ofs
) {
1083 if (len
== 0) break;
1087 *pnum_streams
= num_streams
;
1088 *pstreams
= streams
;
1092 TALLOC_FREE(streams
);
1096 /****************************************************************************
1097 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1098 ****************************************************************************/
1100 bool cli_qfilename(struct cli_state
*cli
, uint16_t fnum
, char *name
, size_t namelen
)
1102 unsigned int data_len
= 0;
1103 unsigned int param_len
= 0;
1104 uint16 setup
= TRANSACT2_QFILEINFO
;
1106 char *rparam
=NULL
, *rdata
=NULL
;
1109 SSVAL(param
, 0, fnum
);
1110 SSVAL(param
, 2, SMB_QUERY_FILE_NAME_INFO
);
1112 if (!cli_send_trans(cli
, SMBtrans2
,
1114 -1, 0, /* fid, flags */
1115 &setup
, 1, 0, /* setup, length, max */
1116 param
, param_len
, 2, /* param, length, max */
1117 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
1122 if (!cli_receive_trans(cli
, SMBtrans2
,
1123 &rparam
, ¶m_len
,
1124 &rdata
, &data_len
)) {
1128 if (!rdata
|| data_len
< 4) {
1134 clistr_pull(cli
->inbuf
, name
, rdata
+4, namelen
, IVAL(rdata
, 0),
1143 /****************************************************************************
1144 Send a qfileinfo call.
1145 ****************************************************************************/
1147 bool cli_qfileinfo(struct cli_state
*cli
, uint16_t fnum
,
1148 uint16
*mode
, SMB_OFF_T
*size
,
1149 struct timespec
*create_time
,
1150 struct timespec
*access_time
,
1151 struct timespec
*write_time
,
1152 struct timespec
*change_time
,
1155 uint32_t data_len
= 0;
1158 uint8_t *rdata
=NULL
;
1161 /* if its a win95 server then fail this - win95 totally screws it
1163 if (cli
->win95
) return False
;
1165 SSVAL(param
, 0, fnum
);
1166 SSVAL(param
, 2, SMB_QUERY_FILE_ALL_INFO
);
1168 SSVAL(&setup
, 0, TRANSACT2_QFILEINFO
);
1170 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
,
1171 NULL
, -1, 0, 0, /* name, fid, function, flags */
1172 &setup
, 1, 0, /* setup, length, max */
1173 param
, 4, 2, /* param, length, max */
1174 NULL
, 0, MIN(cli
->max_xmit
, 0xffff), /* data, length, max */
1175 NULL
, /* recv_flags2 */
1176 NULL
, 0, NULL
, /* rsetup, length */
1177 NULL
, 0, NULL
, /* rparam, length */
1178 &rdata
, 68, &data_len
);
1180 if (!NT_STATUS_IS_OK(status
)) {
1185 *create_time
= interpret_long_date((char *)rdata
+0);
1188 *access_time
= interpret_long_date((char *)rdata
+8);
1191 *write_time
= interpret_long_date((char *)rdata
+16);
1194 *change_time
= interpret_long_date((char *)rdata
+24);
1197 *mode
= SVAL(rdata
, 32);
1200 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
1203 *ino
= IVAL(rdata
, 64);
1210 /****************************************************************************
1211 Send a qpathinfo BASIC_INFO call.
1212 ****************************************************************************/
1214 struct cli_qpathinfo_basic_state
{
1219 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
);
1221 struct tevent_req
*cli_qpathinfo_basic_send(TALLOC_CTX
*mem_ctx
,
1222 struct event_context
*ev
,
1223 struct cli_state
*cli
,
1226 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1227 struct cli_qpathinfo_basic_state
*state
= NULL
;
1229 req
= tevent_req_create(mem_ctx
, &state
,
1230 struct cli_qpathinfo_basic_state
);
1234 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
1235 SMB_QUERY_FILE_BASIC_INFO
,
1237 if (tevent_req_nomem(subreq
, req
)) {
1238 return tevent_req_post(req
, ev
);
1240 tevent_req_set_callback(subreq
, cli_qpathinfo_basic_done
, req
);
1244 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
)
1246 struct tevent_req
*req
= tevent_req_callback_data(
1247 subreq
, struct tevent_req
);
1248 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1249 req
, struct cli_qpathinfo_basic_state
);
1252 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
1254 TALLOC_FREE(subreq
);
1255 if (!NT_STATUS_IS_OK(status
)) {
1256 tevent_req_nterror(req
, status
);
1259 tevent_req_done(req
);
1262 NTSTATUS
cli_qpathinfo_basic_recv(struct tevent_req
*req
,
1263 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1265 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1266 req
, struct cli_qpathinfo_basic_state
);
1269 if (tevent_req_is_nterror(req
, &status
)) {
1273 sbuf
->st_ex_atime
= interpret_long_date((char *)state
->data
+8);
1274 sbuf
->st_ex_mtime
= interpret_long_date((char *)state
->data
+16);
1275 sbuf
->st_ex_ctime
= interpret_long_date((char *)state
->data
+24);
1276 *attributes
= IVAL(state
->data
, 32);
1277 return NT_STATUS_OK
;
1280 NTSTATUS
cli_qpathinfo_basic(struct cli_state
*cli
, const char *name
,
1281 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1283 TALLOC_CTX
*frame
= talloc_stackframe();
1284 struct event_context
*ev
;
1285 struct tevent_req
*req
;
1286 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1288 if (cli_has_async_calls(cli
)) {
1290 * Can't use sync call while an async call is in flight
1292 status
= NT_STATUS_INVALID_PARAMETER
;
1295 ev
= event_context_init(frame
);
1299 req
= cli_qpathinfo_basic_send(frame
, ev
, cli
, name
);
1303 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1306 status
= cli_qpathinfo_basic_recv(req
, sbuf
, attributes
);
1309 if (!NT_STATUS_IS_OK(status
)) {
1310 cli_set_error(cli
, status
);
1315 /****************************************************************************
1316 Send a qfileinfo call.
1317 ****************************************************************************/
1319 bool cli_qfileinfo_test(struct cli_state
*cli
, uint16_t fnum
, int level
, char **poutdata
, uint32
*poutlen
)
1321 unsigned int data_len
= 0;
1322 unsigned int param_len
= 0;
1323 uint16 setup
= TRANSACT2_QFILEINFO
;
1325 char *rparam
=NULL
, *rdata
=NULL
;
1330 /* if its a win95 server then fail this - win95 totally screws it
1337 SSVAL(param
, 0, fnum
);
1338 SSVAL(param
, 2, level
);
1340 if (!cli_send_trans(cli
, SMBtrans2
,
1342 -1, 0, /* fid, flags */
1343 &setup
, 1, 0, /* setup, length, max */
1344 param
, param_len
, 2, /* param, length, max */
1345 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
1350 if (!cli_receive_trans(cli
, SMBtrans2
,
1351 &rparam
, ¶m_len
,
1352 &rdata
, &data_len
)) {
1356 *poutdata
= (char *)memdup(rdata
, data_len
);
1363 *poutlen
= data_len
;
1370 /****************************************************************************
1371 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1372 ****************************************************************************/
1374 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
1376 unsigned int data_len
= 0;
1377 unsigned int param_len
= 0;
1378 uint16 setup
= TRANSACT2_QPATHINFO
;
1380 char *rparam
=NULL
, *rdata
=NULL
;
1385 size_t nlen
= 2*(strlen(fname
)+1);
1387 param
= SMB_MALLOC_ARRAY(char, 6+nlen
+2);
1389 return NT_STATUS_NO_MEMORY
;
1392 memset(param
, '\0', 6);
1393 SSVAL(p
, 0, SMB_QUERY_FILE_ALT_NAME_INFO
);
1395 p
+= clistr_push(cli
, p
, fname
, nlen
, STR_TERMINATE
);
1396 param_len
= PTR_DIFF(p
, param
);
1399 ret
= (cli_send_trans(cli
, SMBtrans2
,
1401 -1, 0, /* fid, flags */
1402 &setup
, 1, 0, /* setup, length, max */
1403 param
, param_len
, 10, /* param, length, max */
1404 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
1406 cli_receive_trans(cli
, SMBtrans2
,
1407 &rparam
, ¶m_len
,
1408 &rdata
, &data_len
));
1409 if (!ret
&& cli_is_dos_error(cli
)) {
1410 /* we need to work around a Win95 bug - sometimes
1411 it gives ERRSRV/ERRerror temprarily */
1414 cli_dos_error(cli
, &eclass
, &ecode
);
1415 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
1418 } while (count
-- && ret
==False
);
1422 if (!ret
|| !rdata
|| data_len
< 4) {
1423 return NT_STATUS_UNSUCCESSFUL
;
1426 len
= IVAL(rdata
, 0);
1428 if (len
> data_len
- 4) {
1429 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1432 clistr_pull(cli
->inbuf
, alt_name
, rdata
+4, sizeof(fstring
), len
,
1438 return NT_STATUS_OK
;