libcli/smb: move smb2cli_query_info.c from source3 to the toplevel
[Samba/bjacke.git] / source3 / libsmb / smb2cli_query_directory.c
blobcdd530c3f790ddac685e50ee2f8db3ee0ecfaf71
1 /*
2 Unix SMB/CIFS implementation.
3 smb2 lib
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/>.
20 #include "includes.h"
21 #include "client.h"
22 #include "async_smb.h"
23 #include "../libcli/smb/smbXcli_base.h"
24 #include "smb2cli.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_query_directory_state {
29 uint8_t fixed[32];
30 uint8_t dyn_pad[1];
31 struct iovec *recv_iov;
32 uint8_t *data;
33 uint32_t data_length;
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,
43 uint32_t tcon_id,
44 uint8_t level,
45 uint8_t flags,
46 uint32_t file_index,
47 uint64_t fid_persistent,
48 uint64_t fid_volatile,
49 const char *mask,
50 uint32_t outbuf_len)
52 struct tevent_req *req, *subreq;
53 struct smb2cli_query_directory_state *state;
54 uint8_t *fixed;
55 uint8_t *dyn;
56 size_t dyn_len;
58 req = tevent_req_create(mem_ctx, &state,
59 struct smb2cli_query_directory_state);
60 if (req == NULL) {
61 return NULL;
64 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
65 mask, strlen(mask),
66 &dyn, &dyn_len)) {
67 tevent_req_oom(req);
68 return tevent_req_post(req, ev);
71 if (strlen(mask) == 0) {
72 TALLOC_FREE(dyn);
73 dyn_len = 0;
76 fixed = state->fixed;
77 SSVAL(fixed, 0, 33);
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);
87 if (dyn_len == 0) {
88 dyn = state->dyn_pad;
89 dyn_len = sizeof(state->dyn_pad);
92 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_FIND,
93 0, 0, /* flags */
94 timeout_msec,
95 0xFEFF, /* pid */
96 tcon_id,
97 session,
98 state->fixed, sizeof(state->fixed),
99 dyn, dyn_len);
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);
104 return req;
107 static void smb2cli_query_directory_done(struct tevent_req *subreq)
109 struct tevent_req *req =
110 tevent_req_callback_data(subreq,
111 struct tevent_req);
112 struct smb2cli_query_directory_state *state =
113 tevent_req_data(req,
114 struct smb2cli_query_directory_state);
115 NTSTATUS status;
116 struct iovec *iov;
117 uint16_t data_offset;
118 static const struct smb2cli_req_expected_response expected[] = {
120 .status = NT_STATUS_OK,
121 .body_size = 0x09
125 status = smb2cli_req_recv(subreq, state, &iov,
126 expected, ARRAY_SIZE(expected));
127 if (tevent_req_nterror(req, status)) {
128 return;
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);
137 return;
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,
146 TALLOC_CTX *mem_ctx,
147 uint8_t **data,
148 uint32_t *data_length)
150 struct smb2cli_query_directory_state *state =
151 tevent_req_data(req,
152 struct smb2cli_query_directory_state);
153 NTSTATUS status;
155 if (tevent_req_is_nterror(req, &status)) {
156 return status;
158 talloc_steal(mem_ctx, state->recv_iov);
159 *data_length = state->data_length;
160 *data = state->data;
161 return NT_STATUS_OK;
164 NTSTATUS smb2cli_query_directory(struct smbXcli_conn *conn,
165 uint32_t timeout_msec,
166 struct smbXcli_session *session,
167 uint32_t tcon_id,
168 uint8_t level,
169 uint8_t flags,
170 uint32_t file_index,
171 uint64_t fid_persistent,
172 uint64_t fid_volatile,
173 const char *mask,
174 uint32_t outbuf_len,
175 TALLOC_CTX *mem_ctx,
176 uint8_t **data,
177 uint32_t *data_length)
179 TALLOC_CTX *frame = talloc_stackframe();
180 struct tevent_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;
189 goto fail;
191 ev = tevent_context_init(frame);
192 if (ev == NULL) {
193 goto fail;
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);
199 if (req == NULL) {
200 goto fail;
202 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
203 goto fail;
205 status = smb2cli_query_directory_recv(req, mem_ctx,
206 data, data_length);
207 fail:
208 TALLOC_FREE(frame);
209 return status;