2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "async_smb.h"
23 #include "../libcli/smb/smbXcli_base.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_query_directory_state
{
31 struct iovec
*recv_iov
;
36 static void smb2cli_query_directory_done(struct tevent_req
*subreq
);
38 struct tevent_req
*smb2cli_query_directory_send(TALLOC_CTX
*mem_ctx
,
39 struct tevent_context
*ev
,
40 struct cli_state
*cli
,
44 uint64_t fid_persistent
,
45 uint64_t fid_volatile
,
49 struct tevent_req
*req
, *subreq
;
50 struct smb2cli_query_directory_state
*state
;
55 req
= tevent_req_create(mem_ctx
, &state
,
56 struct smb2cli_query_directory_state
);
61 if (!convert_string_talloc(state
, CH_UNIX
, CH_UTF16
,
65 return tevent_req_post(req
, ev
);
68 if (strlen(mask
) == 0) {
75 SCVAL(fixed
, 2, level
);
76 SCVAL(fixed
, 3, flags
);
77 SIVAL(fixed
, 4, file_index
);
78 SBVAL(fixed
, 8, fid_persistent
);
79 SBVAL(fixed
, 16, fid_volatile
);
80 SSVAL(fixed
, 24, SMB2_HDR_BODY
+ 32);
81 SSVAL(fixed
, 26, dyn_len
);
82 SSVAL(fixed
, 28, outbuf_len
);
86 dyn_len
= sizeof(state
->dyn_pad
);
89 subreq
= smb2cli_req_send(state
, ev
, cli
->conn
, SMB2_OP_FIND
,
95 state
->fixed
, sizeof(state
->fixed
),
97 if (tevent_req_nomem(subreq
, req
)) {
98 return tevent_req_post(req
, ev
);
100 tevent_req_set_callback(subreq
, smb2cli_query_directory_done
, req
);
104 static void smb2cli_query_directory_done(struct tevent_req
*subreq
)
106 struct tevent_req
*req
=
107 tevent_req_callback_data(subreq
,
109 struct smb2cli_query_directory_state
*state
=
111 struct smb2cli_query_directory_state
);
114 uint16_t data_offset
;
115 static const struct smb2cli_req_expected_response expected
[] = {
117 .status
= NT_STATUS_OK
,
122 status
= smb2cli_req_recv(subreq
, state
, &iov
,
123 expected
, ARRAY_SIZE(expected
));
124 if (tevent_req_nterror(req
, status
)) {
128 data_offset
= SVAL(iov
[1].iov_base
, 2);
129 state
->data_length
= IVAL(iov
[1].iov_base
, 4);
131 if ((data_offset
!= SMB2_HDR_BODY
+ 8) ||
132 (state
->data_length
> iov
[2].iov_len
)) {
133 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
137 state
->recv_iov
= iov
;
138 state
->data
= (uint8_t *)iov
[2].iov_base
;
139 tevent_req_done(req
);
142 NTSTATUS
smb2cli_query_directory_recv(struct tevent_req
*req
,
145 uint32_t *data_length
)
147 struct smb2cli_query_directory_state
*state
=
149 struct smb2cli_query_directory_state
);
152 if (tevent_req_is_nterror(req
, &status
)) {
155 talloc_steal(mem_ctx
, state
->recv_iov
);
156 *data_length
= state
->data_length
;
161 NTSTATUS
smb2cli_query_directory(struct cli_state
*cli
,
165 uint64_t fid_persistent
,
166 uint64_t fid_volatile
,
171 uint32_t *data_length
)
173 TALLOC_CTX
*frame
= talloc_stackframe();
174 struct event_context
*ev
;
175 struct tevent_req
*req
;
176 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
178 if (cli_has_async_calls(cli
)) {
180 * Can't use sync call while an async call is in flight
182 status
= NT_STATUS_INVALID_PARAMETER
;
185 ev
= event_context_init(frame
);
189 req
= smb2cli_query_directory_send(frame
, ev
, cli
, level
, flags
,
190 file_index
, fid_persistent
,
191 fid_volatile
, mask
, outbuf_len
);
195 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
198 status
= smb2cli_query_directory_recv(req
, mem_ctx
,