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"
27 /****************************************************************************
29 ****************************************************************************/
31 bool cli_api(struct cli_state
*cli
,
32 char *param
, int prcnt
, int mprcnt
,
33 char *data
, int drcnt
, int mdrcnt
,
34 char **rparam
, unsigned int *rprcnt
,
35 char **rdata
, unsigned int *rdrcnt
)
37 cli_send_trans(cli
,SMBtrans
,
38 PIPE_LANMAN
, /* Name */
40 NULL
,0,0, /* Setup, length, max */
41 param
, prcnt
, mprcnt
, /* Params, length, max */
42 data
, drcnt
, mdrcnt
/* Data, length, max */
45 return (cli_receive_trans(cli
,SMBtrans
,
50 /****************************************************************************
51 Perform a NetWkstaUserLogon.
52 ****************************************************************************/
54 bool cli_NetWkstaUserLogon(struct cli_state
*cli
,char *user
, char *workstation
)
59 unsigned int rdrcnt
,rprcnt
;
62 memset(param
, 0, sizeof(param
));
64 /* send a SMBtrans command with api NetWkstaUserLogon */
66 SSVAL(p
,0,132); /* api number */
68 strlcpy(p
,"OOWb54WrLh",sizeof(param
)-PTR_DIFF(p
,param
));
69 p
= skip_string(param
,sizeof(param
),p
);
70 strlcpy(p
,"WB21BWDWWDDDDDDDzzzD",sizeof(param
)-PTR_DIFF(p
,param
));
71 p
= skip_string(param
,sizeof(param
),p
);
74 strlcpy(p
,user
,sizeof(param
)-PTR_DIFF(p
,param
));
80 strlcpy(p
, workstation
,sizeof(param
)-PTR_DIFF(p
,param
));
83 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
85 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
89 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
90 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
91 &rparam
, &rprcnt
, /* return params, return size */
92 &rdata
, &rdrcnt
/* return data, return size */
94 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
97 if (cli
->rap_error
== 0) {
98 DEBUG(4,("NetWkstaUserLogon success\n"));
99 cli
->privileges
= SVAL(p
, 24);
100 /* The cli->eff_name field used to be set here
101 but it wasn't used anywhere else. */
103 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
109 return (cli
->rap_error
== 0);
112 /****************************************************************************
113 Call a NetShareEnum - try and browse available connections on a host.
114 ****************************************************************************/
116 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32
, const char *, void *), void *state
)
121 unsigned int rdrcnt
,rprcnt
;
125 /* now send a SMBtrans command with api RNetShareEnum */
127 SSVAL(p
,0,0); /* api number */
129 strlcpy(p
,"WrLeh",sizeof(param
)-PTR_DIFF(p
,param
));
130 p
= skip_string(param
,sizeof(param
),p
);
131 strlcpy(p
,"B13BWz",sizeof(param
)-PTR_DIFF(p
,param
));
132 p
= skip_string(param
,sizeof(param
),p
);
135 * Win2k needs a *smaller* buffer than 0xFFFF here -
136 * it returns "out of server memory" with 0xFFFF !!! JRA.
142 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
143 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
144 &rparam
, &rprcnt
, /* return params, length */
145 &rdata
, &rdrcnt
)) /* return data, length */
147 int res
= rparam
? SVAL(rparam
,0) : -1;
149 if (res
== 0 || res
== ERRmoredata
) {
150 int converter
=SVAL(rparam
,2);
152 char *rdata_end
= rdata
+ rdrcnt
;
154 count
=SVAL(rparam
,4);
157 for (i
=0;i
<count
;i
++,p
+=20) {
165 TALLOC_CTX
*frame
= talloc_stackframe();
167 if (p
+ 20 > rdata_end
) {
174 comment_offset
= (IVAL(p
,16) & 0xFFFF) - converter
;
175 if (comment_offset
< 0 ||
176 comment_offset
> (int)rdrcnt
) {
180 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
182 /* Work out the comment length. */
183 for (p1
= cmnt
, len
= 0; *p1
&&
184 p1
< rdata_end
; len
++)
189 pull_string_talloc(frame
,rdata
,0,
190 &s1
,sname
,14,STR_ASCII
);
191 pull_string_talloc(frame
,rdata
,0,
192 &s2
,cmnt
,len
,STR_ASCII
);
198 fn(s1
, type
, s2
, state
);
203 DEBUG(4,("NetShareEnum res=%d\n", res
));
206 DEBUG(4,("NetShareEnum failed\n"));
215 /****************************************************************************
216 Call a NetServerEnum for the specified workgroup and servertype mask. This
217 function then calls the specified callback function for each name returned.
219 The callback function takes 4 arguments: the machine name, the server type,
220 the comment and a state pointer.
221 ****************************************************************************/
223 bool cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32 stype
,
224 void (*fn
)(const char *, uint32
, const char *, void *),
229 char *rdata_end
= NULL
;
230 unsigned int rdrcnt
,rprcnt
;
235 uint32 func
= RAP_NetServerEnum2
;
236 char *last_entry
= NULL
;
241 errno
= 0; /* reset */
244 * This may take more than one transaction, so we should loop until
245 * we no longer get a more data to process or we have all of the
249 /* send a SMBtrans command with api NetServerEnum */
251 SIVAL(p
,0,func
); /* api number */
254 if (func
== RAP_NetServerEnum3
) {
255 strlcpy(p
,"WrLehDzz", sizeof(param
)-PTR_DIFF(p
,param
));
257 strlcpy(p
,"WrLehDz", sizeof(param
)-PTR_DIFF(p
,param
));
260 p
= skip_string(param
, sizeof(param
), p
);
261 strlcpy(p
,"B16BBDz", sizeof(param
)-PTR_DIFF(p
,param
));
263 p
= skip_string(param
, sizeof(param
), p
);
265 SSVAL(p
,2,CLI_BUFFER_SIZE
);
270 /* If we have more data, tell the server where
275 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
276 STR_TERMINATE
|STR_UPPER
);
278 if (len
== (size_t)-1) {
279 SAFE_FREE(last_entry
);
284 if (func
== RAP_NetServerEnum3
) {
286 last_entry
? last_entry
: "",
287 sizeof(param
) - PTR_DIFF(p
,param
) - 1,
290 if (len
== (size_t)-1) {
291 SAFE_FREE(last_entry
);
297 /* Next time through we need to use the continue api */
298 func
= RAP_NetServerEnum3
;
301 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
302 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
303 &rparam
, &rprcnt
, /* return params, return size */
304 &rdata
, &rdrcnt
)) { /* return data, return size */
306 /* break out of the loop on error */
311 rdata_end
= rdata
+ rdrcnt
;
312 res
= rparam
? SVAL(rparam
,0) : -1;
314 if (res
== 0 || res
== ERRmoredata
||
315 (res
!= -1 && cli_errno(cli
) == 0)) {
318 int converter
=SVAL(rparam
,2);
320 /* Get the number of items returned in this buffer */
321 count
= SVAL(rparam
, 4);
323 /* The next field contains the number of items left,
324 * including those returned in this buffer. So the
325 * first time through this should contain all of the
328 if (total_cnt
== 0) {
329 total_cnt
= SVAL(rparam
, 6);
332 /* Keep track of how many we have read */
336 /* The last name in the previous NetServerEnum reply is
337 * sent back to server in the NetServerEnum3 request
338 * (last_entry). The next reply should repeat this entry
339 * as the first element. We have no proof that this is
340 * always true, but from traces that seems to be the
341 * behavior from Window Servers. So first lets do a lot
342 * of checking, just being paranoid. If the string
343 * matches then we already saw this entry so skip it.
345 * NOTE: sv1_name field must be null terminated and has
346 * a max size of 16 (NetBIOS Name).
348 if (last_entry
&& count
&& p
&&
349 (strncmp(last_entry
, p
, 16) == 0)) {
350 count
-= 1; /* Skip this entry */
351 return_cnt
= -1; /* Not part of total, so don't count. */
352 p
= rdata
+ 26; /* Skip the whole record */
355 for (i
= 0; i
< count
; i
++, p
+= 26) {
360 TALLOC_CTX
*frame
= talloc_stackframe();
361 uint32_t entry_stype
;
363 if (p
+ 26 > rdata_end
) {
369 comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
370 cmnt
= comment_offset
?(rdata
+comment_offset
):"";
372 if (comment_offset
< 0 || comment_offset
>= (int)rdrcnt
) {
377 /* Work out the comment length. */
378 for (p1
= cmnt
, len
= 0; *p1
&&
379 p1
< rdata_end
; len
++)
385 entry_stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
387 pull_string_talloc(frame
,rdata
,0,
388 &s1
,sname
,16,STR_ASCII
);
389 pull_string_talloc(frame
,rdata
,0,
390 &s2
,cmnt
,len
,STR_ASCII
);
397 fn(s1
, entry_stype
, s2
, state
);
401 /* We are done with the old last entry, so now we can free it */
403 SAFE_FREE(last_entry
); /* This will set it to null */
406 /* We always make a copy of the last entry if we have one */
408 last_entry
= smb_xstrdup(sname
);
411 /* If we have more data, but no last entry then error out */
412 if (!last_entry
&& (res
== ERRmoredata
)) {
421 } while ((res
== ERRmoredata
) && (total_cnt
> return_cnt
));
425 SAFE_FREE(last_entry
);
428 errno
= cli_errno(cli
);
431 /* this is a very special case, when the domain master for the
432 work group isn't part of the work group itself, there is something
438 return(return_cnt
> 0);
441 /****************************************************************************
442 Send a SamOEMChangePassword command.
443 ****************************************************************************/
445 bool cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
446 const char *old_password
)
449 unsigned char data
[532];
451 unsigned char old_pw_hash
[16];
452 unsigned char new_pw_hash
[16];
453 unsigned int data_len
;
454 unsigned int param_len
= 0;
457 unsigned int rprcnt
, rdrcnt
;
459 if (strlen(user
) >= sizeof(fstring
)-1) {
460 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
464 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
466 strlcpy(p
, "zsT", sizeof(param
)-PTR_DIFF(p
,param
));
467 p
= skip_string(param
,sizeof(param
),p
);
468 strlcpy(p
, "B516B16", sizeof(param
)-PTR_DIFF(p
,param
));
469 p
= skip_string(param
,sizeof(param
),p
);
470 strlcpy(p
,user
, sizeof(param
)-PTR_DIFF(p
,param
));
471 p
= skip_string(param
,sizeof(param
),p
);
475 param_len
= PTR_DIFF(p
,param
);
478 * Get the Lanman hash of the old password, we
479 * use this as the key to make_oem_passwd_hash().
481 E_deshash(old_password
, old_pw_hash
);
483 encode_pw_buffer(data
, new_password
, STR_ASCII
);
485 #ifdef DEBUG_PASSWORD
486 DEBUG(100,("make_oem_passwd_hash\n"));
487 dump_data(100, data
, 516);
489 arcfour_crypt( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
492 * Now place the old password hash in the data.
494 E_deshash(new_password
, new_pw_hash
);
496 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
500 if (cli_send_trans(cli
,SMBtrans
,
501 PIPE_LANMAN
, /* name */
502 0,0, /* fid, flags */
503 NULL
,0,0, /* setup, length, max */
504 param
,param_len
,4, /* param, length, max */
505 (char *)data
,data_len
,0 /* data, length, max */
507 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
512 if (!cli_receive_trans(cli
,SMBtrans
,
515 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
521 cli
->rap_error
= SVAL(rparam
,0);
527 return (cli
->rap_error
== 0);
530 /****************************************************************************
531 Send a qpathinfo call.
532 ****************************************************************************/
534 struct cli_qpathinfo1_state
{
535 struct cli_state
*cli
;
540 static void cli_qpathinfo1_done(struct tevent_req
*subreq
);
542 struct tevent_req
*cli_qpathinfo1_send(TALLOC_CTX
*mem_ctx
,
543 struct event_context
*ev
,
544 struct cli_state
*cli
,
547 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
548 struct cli_qpathinfo1_state
*state
= NULL
;
550 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo1_state
);
555 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_INFO_STANDARD
,
557 if (tevent_req_nomem(subreq
, req
)) {
558 return tevent_req_post(req
, ev
);
560 tevent_req_set_callback(subreq
, cli_qpathinfo1_done
, req
);
564 static void cli_qpathinfo1_done(struct tevent_req
*subreq
)
566 struct tevent_req
*req
= tevent_req_callback_data(
567 subreq
, struct tevent_req
);
568 struct cli_qpathinfo1_state
*state
= tevent_req_data(
569 req
, struct cli_qpathinfo1_state
);
572 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
575 if (!NT_STATUS_IS_OK(status
)) {
576 tevent_req_nterror(req
, status
);
579 tevent_req_done(req
);
582 NTSTATUS
cli_qpathinfo1_recv(struct tevent_req
*req
,
589 struct cli_qpathinfo1_state
*state
= tevent_req_data(
590 req
, struct cli_qpathinfo1_state
);
593 time_t (*date_fn
)(const void *buf
, int serverzone
);
595 if (tevent_req_is_nterror(req
, &status
)) {
599 if (state
->cli
->win95
) {
600 date_fn
= make_unix_date
;
602 date_fn
= make_unix_date2
;
606 *change_time
= date_fn(state
->data
+0, state
->cli
->serverzone
);
609 *access_time
= date_fn(state
->data
+4, state
->cli
->serverzone
);
612 *write_time
= date_fn(state
->data
+8, state
->cli
->serverzone
);
615 *size
= IVAL(state
->data
, 12);
618 *mode
= SVAL(state
->data
, l1_attrFile
);
623 NTSTATUS
cli_qpathinfo1(struct cli_state
*cli
,
631 TALLOC_CTX
*frame
= talloc_stackframe();
632 struct event_context
*ev
;
633 struct tevent_req
*req
;
634 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
636 if (cli_has_async_calls(cli
)) {
638 * Can't use sync call while an async call is in flight
640 status
= NT_STATUS_INVALID_PARAMETER
;
643 ev
= event_context_init(frame
);
647 req
= cli_qpathinfo1_send(frame
, ev
, cli
, fname
);
651 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
654 status
= cli_qpathinfo1_recv(req
, change_time
, access_time
,
655 write_time
, size
, mode
);
658 if (!NT_STATUS_IS_OK(status
)) {
659 cli_set_error(cli
, status
);
664 /****************************************************************************
665 Send a setpathinfo call.
666 ****************************************************************************/
668 bool cli_setpathinfo(struct cli_state
*cli
, const char *fname
,
675 unsigned int data_len
= 0;
676 unsigned int param_len
= 0;
677 unsigned int rparam_len
, rdata_len
;
678 uint16 setup
= TRANSACT2_SETPATHINFO
;
681 char *rparam
=NULL
, *rdata
=NULL
;
685 size_t nlen
= 2*(strlen(fname
)+1);
687 param
= SMB_MALLOC_ARRAY(char, 6+nlen
+2);
691 memset(param
, '\0', 6);
692 memset(data
, 0, sizeof(data
));
696 /* Add the information level */
697 SSVAL(p
, 0, SMB_FILE_BASIC_INFORMATION
);
702 /* Add the file name */
703 p
+= clistr_push(cli
, p
, fname
, nlen
, STR_TERMINATE
);
705 param_len
= PTR_DIFF(p
, param
);
710 * Add the create, last access, modification, and status change times
712 put_long_date(p
, create_time
);
715 put_long_date(p
, access_time
);
718 put_long_date(p
, write_time
);
721 put_long_date(p
, change_time
);
732 data_len
= PTR_DIFF(p
, data
);
735 ret
= (cli_send_trans(cli
, SMBtrans2
,
737 -1, 0, /* fid, flags */
738 &setup
, 1, 0, /* setup, length, max */
739 param
, param_len
, 10, /* param, length, max */
740 data
, data_len
, cli
->max_xmit
/* data, length, max */
742 cli_receive_trans(cli
, SMBtrans2
,
743 &rparam
, &rparam_len
,
744 &rdata
, &rdata_len
));
745 if (!cli_is_dos_error(cli
)) break;
747 /* we need to work around a Win95 bug - sometimes
748 it gives ERRSRV/ERRerror temprarily */
751 cli_dos_error(cli
, &eclass
, &ecode
);
752 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
755 } while (count
-- && ret
==False
);
767 /****************************************************************************
768 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
769 ****************************************************************************/
771 struct cli_qpathinfo2_state
{
776 static void cli_qpathinfo2_done(struct tevent_req
*subreq
);
778 struct tevent_req
*cli_qpathinfo2_send(TALLOC_CTX
*mem_ctx
,
779 struct event_context
*ev
,
780 struct cli_state
*cli
,
783 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
784 struct cli_qpathinfo2_state
*state
= NULL
;
786 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo2_state
);
790 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
791 SMB_QUERY_FILE_ALL_INFO
,
793 if (tevent_req_nomem(subreq
, req
)) {
794 return tevent_req_post(req
, ev
);
796 tevent_req_set_callback(subreq
, cli_qpathinfo2_done
, req
);
800 static void cli_qpathinfo2_done(struct tevent_req
*subreq
)
802 struct tevent_req
*req
= tevent_req_callback_data(
803 subreq
, struct tevent_req
);
804 struct cli_qpathinfo2_state
*state
= tevent_req_data(
805 req
, struct cli_qpathinfo2_state
);
808 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
811 if (!NT_STATUS_IS_OK(status
)) {
812 tevent_req_nterror(req
, status
);
815 tevent_req_done(req
);
818 NTSTATUS
cli_qpathinfo2_recv(struct tevent_req
*req
,
819 struct timespec
*create_time
,
820 struct timespec
*access_time
,
821 struct timespec
*write_time
,
822 struct timespec
*change_time
,
823 SMB_OFF_T
*size
, uint16
*mode
,
826 struct cli_qpathinfo2_state
*state
= tevent_req_data(
827 req
, struct cli_qpathinfo2_state
);
830 if (tevent_req_is_nterror(req
, &status
)) {
835 *create_time
= interpret_long_date((char *)state
->data
+0);
838 *access_time
= interpret_long_date((char *)state
->data
+8);
841 *write_time
= interpret_long_date((char *)state
->data
+16);
844 *change_time
= interpret_long_date((char *)state
->data
+24);
847 *mode
= SVAL(state
->data
, 32);
850 *size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,48);
853 *ino
= IVAL(state
->data
, 64);
858 NTSTATUS
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
859 struct timespec
*create_time
,
860 struct timespec
*access_time
,
861 struct timespec
*write_time
,
862 struct timespec
*change_time
,
863 SMB_OFF_T
*size
, uint16
*mode
,
866 TALLOC_CTX
*frame
= talloc_stackframe();
867 struct event_context
*ev
;
868 struct tevent_req
*req
;
869 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
871 if (cli_has_async_calls(cli
)) {
873 * Can't use sync call while an async call is in flight
875 status
= NT_STATUS_INVALID_PARAMETER
;
878 ev
= event_context_init(frame
);
882 req
= cli_qpathinfo2_send(frame
, ev
, cli
, fname
);
886 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
889 status
= cli_qpathinfo2_recv(req
, create_time
, access_time
,
890 write_time
, change_time
, size
, mode
, ino
);
893 if (!NT_STATUS_IS_OK(status
)) {
894 cli_set_error(cli
, status
);
899 /****************************************************************************
901 ****************************************************************************/
903 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *data
,
905 unsigned int *pnum_streams
,
906 struct stream_struct
**pstreams
);
908 struct cli_qpathinfo_streams_state
{
913 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
);
915 struct tevent_req
*cli_qpathinfo_streams_send(TALLOC_CTX
*mem_ctx
,
916 struct tevent_context
*ev
,
917 struct cli_state
*cli
,
920 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
921 struct cli_qpathinfo_streams_state
*state
= NULL
;
923 req
= tevent_req_create(mem_ctx
, &state
,
924 struct cli_qpathinfo_streams_state
);
928 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
929 SMB_FILE_STREAM_INFORMATION
,
931 if (tevent_req_nomem(subreq
, req
)) {
932 return tevent_req_post(req
, ev
);
934 tevent_req_set_callback(subreq
, cli_qpathinfo_streams_done
, req
);
938 static void cli_qpathinfo_streams_done(struct tevent_req
*subreq
)
940 struct tevent_req
*req
= tevent_req_callback_data(
941 subreq
, struct tevent_req
);
942 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
943 req
, struct cli_qpathinfo_streams_state
);
946 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
949 if (!NT_STATUS_IS_OK(status
)) {
950 tevent_req_nterror(req
, status
);
953 tevent_req_done(req
);
956 NTSTATUS
cli_qpathinfo_streams_recv(struct tevent_req
*req
,
958 unsigned int *pnum_streams
,
959 struct stream_struct
**pstreams
)
961 struct cli_qpathinfo_streams_state
*state
= tevent_req_data(
962 req
, struct cli_qpathinfo_streams_state
);
965 if (tevent_req_is_nterror(req
, &status
)) {
968 if (!parse_streams_blob(mem_ctx
, state
->data
, state
->num_data
,
969 pnum_streams
, pstreams
)) {
970 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
975 NTSTATUS
cli_qpathinfo_streams(struct cli_state
*cli
, const char *fname
,
977 unsigned int *pnum_streams
,
978 struct stream_struct
**pstreams
)
980 TALLOC_CTX
*frame
= talloc_stackframe();
981 struct event_context
*ev
;
982 struct tevent_req
*req
;
983 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
985 if (cli_has_async_calls(cli
)) {
987 * Can't use sync call while an async call is in flight
989 status
= NT_STATUS_INVALID_PARAMETER
;
992 ev
= event_context_init(frame
);
996 req
= cli_qpathinfo_streams_send(frame
, ev
, cli
, fname
);
1000 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1003 status
= cli_qpathinfo_streams_recv(req
, mem_ctx
, pnum_streams
,
1007 if (!NT_STATUS_IS_OK(status
)) {
1008 cli_set_error(cli
, status
);
1013 static bool parse_streams_blob(TALLOC_CTX
*mem_ctx
, const uint8_t *rdata
,
1015 unsigned int *pnum_streams
,
1016 struct stream_struct
**pstreams
)
1018 unsigned int num_streams
;
1019 struct stream_struct
*streams
;
1026 while ((data_len
> ofs
) && (data_len
- ofs
>= 24)) {
1030 struct stream_struct
*tmp
;
1033 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, streams
,
1034 struct stream_struct
,
1042 nlen
= IVAL(rdata
, ofs
+ 0x04);
1044 streams
[num_streams
].size
= IVAL_TO_SMB_OFF_T(
1046 streams
[num_streams
].alloc_size
= IVAL_TO_SMB_OFF_T(
1049 if (nlen
> data_len
- (ofs
+ 24)) {
1054 * We need to null-terminate src, how do I do this with
1055 * convert_string_talloc??
1058 tmp_buf
= TALLOC_ARRAY(streams
, uint8_t, nlen
+2);
1059 if (tmp_buf
== NULL
) {
1063 memcpy(tmp_buf
, rdata
+ofs
+24, nlen
);
1065 tmp_buf
[nlen
+1] = 0;
1067 if (!convert_string_talloc(streams
, CH_UTF16
, CH_UNIX
, tmp_buf
,
1068 nlen
+2, &vstr
, &size
, false))
1070 TALLOC_FREE(tmp_buf
);
1074 TALLOC_FREE(tmp_buf
);
1075 streams
[num_streams
].name
= (char *)vstr
;
1078 len
= IVAL(rdata
, ofs
);
1079 if (len
> data_len
- ofs
) {
1082 if (len
== 0) break;
1086 *pnum_streams
= num_streams
;
1087 *pstreams
= streams
;
1091 TALLOC_FREE(streams
);
1095 /****************************************************************************
1096 Send a qfileinfo QUERY_FILE_NAME_INFO call.
1097 ****************************************************************************/
1099 bool cli_qfilename(struct cli_state
*cli
, uint16_t fnum
, char *name
, size_t namelen
)
1101 unsigned int data_len
= 0;
1102 unsigned int param_len
= 0;
1103 uint16 setup
= TRANSACT2_QFILEINFO
;
1105 char *rparam
=NULL
, *rdata
=NULL
;
1108 SSVAL(param
, 0, fnum
);
1109 SSVAL(param
, 2, SMB_QUERY_FILE_NAME_INFO
);
1111 if (!cli_send_trans(cli
, SMBtrans2
,
1113 -1, 0, /* fid, flags */
1114 &setup
, 1, 0, /* setup, length, max */
1115 param
, param_len
, 2, /* param, length, max */
1116 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
1121 if (!cli_receive_trans(cli
, SMBtrans2
,
1122 &rparam
, ¶m_len
,
1123 &rdata
, &data_len
)) {
1127 if (!rdata
|| data_len
< 4) {
1133 clistr_pull(cli
->inbuf
, name
, rdata
+4, namelen
, IVAL(rdata
, 0),
1142 /****************************************************************************
1143 Send a qfileinfo call.
1144 ****************************************************************************/
1146 bool cli_qfileinfo(struct cli_state
*cli
, uint16_t fnum
,
1147 uint16
*mode
, SMB_OFF_T
*size
,
1148 struct timespec
*create_time
,
1149 struct timespec
*access_time
,
1150 struct timespec
*write_time
,
1151 struct timespec
*change_time
,
1154 uint32_t data_len
= 0;
1157 uint8_t *rdata
=NULL
;
1160 /* if its a win95 server then fail this - win95 totally screws it
1162 if (cli
->win95
) return False
;
1164 SSVAL(param
, 0, fnum
);
1165 SSVAL(param
, 2, SMB_QUERY_FILE_ALL_INFO
);
1167 SSVAL(&setup
, 0, TRANSACT2_QFILEINFO
);
1169 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
,
1170 NULL
, -1, 0, 0, /* name, fid, function, flags */
1171 &setup
, 1, 0, /* setup, length, max */
1172 param
, 4, 2, /* param, length, max */
1173 NULL
, 0, MIN(cli
->max_xmit
, 0xffff), /* data, length, max */
1174 NULL
, /* recv_flags2 */
1175 NULL
, 0, NULL
, /* rsetup, length */
1176 NULL
, 0, NULL
, /* rparam, length */
1177 &rdata
, 68, &data_len
);
1179 if (!NT_STATUS_IS_OK(status
)) {
1184 *create_time
= interpret_long_date((char *)rdata
+0);
1187 *access_time
= interpret_long_date((char *)rdata
+8);
1190 *write_time
= interpret_long_date((char *)rdata
+16);
1193 *change_time
= interpret_long_date((char *)rdata
+24);
1196 *mode
= SVAL(rdata
, 32);
1199 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
1202 *ino
= IVAL(rdata
, 64);
1209 /****************************************************************************
1210 Send a qpathinfo BASIC_INFO call.
1211 ****************************************************************************/
1213 struct cli_qpathinfo_basic_state
{
1218 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
);
1220 struct tevent_req
*cli_qpathinfo_basic_send(TALLOC_CTX
*mem_ctx
,
1221 struct event_context
*ev
,
1222 struct cli_state
*cli
,
1225 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1226 struct cli_qpathinfo_basic_state
*state
= NULL
;
1228 req
= tevent_req_create(mem_ctx
, &state
,
1229 struct cli_qpathinfo_basic_state
);
1233 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
1234 SMB_QUERY_FILE_BASIC_INFO
,
1236 if (tevent_req_nomem(subreq
, req
)) {
1237 return tevent_req_post(req
, ev
);
1239 tevent_req_set_callback(subreq
, cli_qpathinfo_basic_done
, req
);
1243 static void cli_qpathinfo_basic_done(struct tevent_req
*subreq
)
1245 struct tevent_req
*req
= tevent_req_callback_data(
1246 subreq
, struct tevent_req
);
1247 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1248 req
, struct cli_qpathinfo_basic_state
);
1251 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
1253 TALLOC_FREE(subreq
);
1254 if (!NT_STATUS_IS_OK(status
)) {
1255 tevent_req_nterror(req
, status
);
1258 tevent_req_done(req
);
1261 NTSTATUS
cli_qpathinfo_basic_recv(struct tevent_req
*req
,
1262 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1264 struct cli_qpathinfo_basic_state
*state
= tevent_req_data(
1265 req
, struct cli_qpathinfo_basic_state
);
1268 if (tevent_req_is_nterror(req
, &status
)) {
1272 sbuf
->st_ex_atime
= interpret_long_date((char *)state
->data
+8);
1273 sbuf
->st_ex_mtime
= interpret_long_date((char *)state
->data
+16);
1274 sbuf
->st_ex_ctime
= interpret_long_date((char *)state
->data
+24);
1275 *attributes
= IVAL(state
->data
, 32);
1276 return NT_STATUS_OK
;
1279 NTSTATUS
cli_qpathinfo_basic(struct cli_state
*cli
, const char *name
,
1280 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
1282 TALLOC_CTX
*frame
= talloc_stackframe();
1283 struct event_context
*ev
;
1284 struct tevent_req
*req
;
1285 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1287 if (cli_has_async_calls(cli
)) {
1289 * Can't use sync call while an async call is in flight
1291 status
= NT_STATUS_INVALID_PARAMETER
;
1294 ev
= event_context_init(frame
);
1298 req
= cli_qpathinfo_basic_send(frame
, ev
, cli
, name
);
1302 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1305 status
= cli_qpathinfo_basic_recv(req
, sbuf
, attributes
);
1308 if (!NT_STATUS_IS_OK(status
)) {
1309 cli_set_error(cli
, status
);
1314 /****************************************************************************
1315 Send a qfileinfo call.
1316 ****************************************************************************/
1318 bool cli_qfileinfo_test(struct cli_state
*cli
, uint16_t fnum
, int level
, char **poutdata
, uint32
*poutlen
)
1320 unsigned int data_len
= 0;
1321 unsigned int param_len
= 0;
1322 uint16 setup
= TRANSACT2_QFILEINFO
;
1324 char *rparam
=NULL
, *rdata
=NULL
;
1329 /* if its a win95 server then fail this - win95 totally screws it
1336 SSVAL(param
, 0, fnum
);
1337 SSVAL(param
, 2, level
);
1339 if (!cli_send_trans(cli
, SMBtrans2
,
1341 -1, 0, /* fid, flags */
1342 &setup
, 1, 0, /* setup, length, max */
1343 param
, param_len
, 2, /* param, length, max */
1344 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
1349 if (!cli_receive_trans(cli
, SMBtrans2
,
1350 &rparam
, ¶m_len
,
1351 &rdata
, &data_len
)) {
1355 *poutdata
= (char *)memdup(rdata
, data_len
);
1362 *poutlen
= data_len
;
1369 /****************************************************************************
1370 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
1371 ****************************************************************************/
1373 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
1375 unsigned int data_len
= 0;
1376 unsigned int param_len
= 0;
1377 uint16 setup
= TRANSACT2_QPATHINFO
;
1379 char *rparam
=NULL
, *rdata
=NULL
;
1384 size_t nlen
= 2*(strlen(fname
)+1);
1386 param
= SMB_MALLOC_ARRAY(char, 6+nlen
+2);
1388 return NT_STATUS_NO_MEMORY
;
1391 memset(param
, '\0', 6);
1392 SSVAL(p
, 0, SMB_QUERY_FILE_ALT_NAME_INFO
);
1394 p
+= clistr_push(cli
, p
, fname
, nlen
, STR_TERMINATE
);
1395 param_len
= PTR_DIFF(p
, param
);
1398 ret
= (cli_send_trans(cli
, SMBtrans2
,
1400 -1, 0, /* fid, flags */
1401 &setup
, 1, 0, /* setup, length, max */
1402 param
, param_len
, 10, /* param, length, max */
1403 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
1405 cli_receive_trans(cli
, SMBtrans2
,
1406 &rparam
, ¶m_len
,
1407 &rdata
, &data_len
));
1408 if (!ret
&& cli_is_dos_error(cli
)) {
1409 /* we need to work around a Win95 bug - sometimes
1410 it gives ERRSRV/ERRerror temprarily */
1413 cli_dos_error(cli
, &eclass
, &ecode
);
1414 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
1417 } while (count
-- && ret
==False
);
1421 if (!ret
|| !rdata
|| data_len
< 4) {
1422 return NT_STATUS_UNSUCCESSFUL
;
1425 len
= IVAL(rdata
, 0);
1427 if (len
> data_len
- 4) {
1428 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1431 clistr_pull(cli
->inbuf
, alt_name
, rdata
+4, sizeof(fstring
), len
,
1437 return NT_STATUS_OK
;