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 "smb2cli_base.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_query_directory_state {
30 struct iovec *recv_iov;
35 static void smb2cli_query_directory_done(struct tevent_req *subreq);
37 struct tevent_req *smb2cli_query_directory_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct cli_state *cli,
43 uint64_t fid_persistent,
44 uint64_t fid_volatile,
48 struct tevent_req *req, *subreq;
49 struct smb2cli_query_directory_state *state;
54 req = tevent_req_create(mem_ctx, &state,
55 struct smb2cli_query_directory_state);
60 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
64 return tevent_req_post(req, ev);
69 SCVAL(fixed, 2, level);
70 SCVAL(fixed, 3, flags);
71 SIVAL(fixed, 4, file_index);
72 SBVAL(fixed, 8, fid_persistent);
73 SBVAL(fixed, 16, fid_volatile);
74 SSVAL(fixed, 24, SMB2_HDR_BODY + 32);
75 SSVAL(fixed, 26, dyn_len);
76 SSVAL(fixed, 28, outbuf_len);
78 subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_FIND,
83 state->fixed, sizeof(state->fixed),
85 if (tevent_req_nomem(subreq, req)) {
86 return tevent_req_post(req, ev);
88 tevent_req_set_callback(subreq, smb2cli_query_directory_done, req);
92 static void smb2cli_query_directory_done(struct tevent_req *subreq)
94 struct tevent_req *req =
95 tevent_req_callback_data(subreq,
97 struct smb2cli_query_directory_state *state =
99 struct smb2cli_query_directory_state);
102 uint16_t data_offset;
104 status = smb2cli_req_recv(subreq, state, &iov, 9);
105 if (tevent_req_nterror(req, status)) {
109 data_offset = SVAL(iov[1].iov_base, 2);
110 state->data_length = IVAL(iov[1].iov_base, 4);
112 if ((data_offset != SMB2_HDR_BODY + 8) ||
113 (state->data_length > iov[2].iov_len)) {
114 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
118 state->recv_iov = iov;
119 state->data = (uint8_t *)iov[2].iov_base;
120 tevent_req_done(req);
123 NTSTATUS smb2cli_query_directory_recv(struct tevent_req *req,
126 uint32_t *data_length)
128 struct smb2cli_query_directory_state *state =
130 struct smb2cli_query_directory_state);
133 if (tevent_req_is_nterror(req, &status)) {
136 talloc_steal(mem_ctx, state->recv_iov);
137 *data_length = state->data_length;
142 NTSTATUS smb2cli_query_directory(struct cli_state *cli,
146 uint64_t fid_persistent,
147 uint64_t fid_volatile,
152 uint32_t *data_length)
154 TALLOC_CTX *frame = talloc_stackframe();
155 struct event_context *ev;
156 struct tevent_req *req;
157 NTSTATUS status = NT_STATUS_NO_MEMORY;
159 if (cli_has_async_calls(cli)) {
161 * Can't use sync call while an async call is in flight
163 status = NT_STATUS_INVALID_PARAMETER;
166 ev = event_context_init(frame);
170 req = smb2cli_query_directory_send(frame, ev, cli, level, flags,
171 file_index, fid_persistent,
172 fid_volatile, mask, outbuf_len);
176 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
179 status = smb2cli_query_directory_recv(req, mem_ctx,