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 smbXcli_conn *conn,
41 uint32_t timeout_msec,
42 struct smbXcli_session *session,
47 uint64_t fid_persistent,
48 uint64_t fid_volatile,
52 struct tevent_req *req, *subreq;
53 struct smb2cli_query_directory_state *state;
58 req = tevent_req_create(mem_ctx, &state,
59 struct smb2cli_query_directory_state);
64 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
68 return tevent_req_post(req, ev);
71 if (strlen(mask) == 0) {
78 SCVAL(fixed, 2, level);
79 SCVAL(fixed, 3, flags);
80 SIVAL(fixed, 4, file_index);
81 SBVAL(fixed, 8, fid_persistent);
82 SBVAL(fixed, 16, fid_volatile);
83 SSVAL(fixed, 24, SMB2_HDR_BODY + 32);
84 SSVAL(fixed, 26, dyn_len);
85 SSVAL(fixed, 28, outbuf_len);
89 dyn_len = sizeof(state->dyn_pad);
92 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_FIND,
98 state->fixed, sizeof(state->fixed),
100 if (tevent_req_nomem(subreq, req)) {
101 return tevent_req_post(req, ev);
103 tevent_req_set_callback(subreq, smb2cli_query_directory_done, req);
107 static void smb2cli_query_directory_done(struct tevent_req *subreq)
109 struct tevent_req *req =
110 tevent_req_callback_data(subreq,
112 struct smb2cli_query_directory_state *state =
114 struct smb2cli_query_directory_state);
117 uint16_t data_offset;
118 static const struct smb2cli_req_expected_response expected[] = {
120 .status = NT_STATUS_OK,
125 status = smb2cli_req_recv(subreq, state, &iov,
126 expected, ARRAY_SIZE(expected));
127 if (tevent_req_nterror(req, status)) {
131 data_offset = SVAL(iov[1].iov_base, 2);
132 state->data_length = IVAL(iov[1].iov_base, 4);
134 if ((data_offset != SMB2_HDR_BODY + 8) ||
135 (state->data_length > iov[2].iov_len)) {
136 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
140 state->recv_iov = iov;
141 state->data = (uint8_t *)iov[2].iov_base;
142 tevent_req_done(req);
145 NTSTATUS smb2cli_query_directory_recv(struct tevent_req *req,
148 uint32_t *data_length)
150 struct smb2cli_query_directory_state *state =
152 struct smb2cli_query_directory_state);
155 if (tevent_req_is_nterror(req, &status)) {
158 talloc_steal(mem_ctx, state->recv_iov);
159 *data_length = state->data_length;
164 NTSTATUS smb2cli_query_directory(struct smbXcli_conn *conn,
165 uint32_t timeout_msec,
166 struct smbXcli_session *session,
171 uint64_t fid_persistent,
172 uint64_t fid_volatile,
177 uint32_t *data_length)
179 TALLOC_CTX *frame = talloc_stackframe();
180 struct event_context *ev;
181 struct tevent_req *req;
182 NTSTATUS status = NT_STATUS_NO_MEMORY;
184 if (smbXcli_conn_has_async_calls(conn)) {
186 * Can't use sync call while an async call is in flight
188 status = NT_STATUS_INVALID_PARAMETER;
191 ev = event_context_init(frame);
195 req = smb2cli_query_directory_send(frame, ev, conn, timeout_msec,
196 session, tcon_id, level, flags,
197 file_index, fid_persistent,
198 fid_volatile, mask, outbuf_len);
202 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
205 status = smb2cli_query_directory_recv(req, mem_ctx,