librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/id10ts.git] / libcli / smb / smb2cli_query_directory.c
blob32f5bee0c58caad56050ba6fa3d5ee28f49541ed
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 "system/network.h"
22 #include "lib/util/tevent_ntstatus.h"
23 #include "smb_common.h"
24 #include "smbXcli_base.h"
26 struct smb2cli_query_directory_state {
27 uint8_t fixed[32];
28 uint8_t dyn_pad[1];
29 struct iovec *recv_iov;
30 uint8_t *data;
31 uint32_t data_length;
34 static void smb2cli_query_directory_done(struct tevent_req *subreq);
36 struct tevent_req *smb2cli_query_directory_send(TALLOC_CTX *mem_ctx,
37 struct tevent_context *ev,
38 struct smbXcli_conn *conn,
39 uint32_t timeout_msec,
40 struct smbXcli_session *session,
41 struct smbXcli_tcon *tcon,
42 uint8_t level,
43 uint8_t flags,
44 uint32_t file_index,
45 uint64_t fid_persistent,
46 uint64_t fid_volatile,
47 const char *mask,
48 uint32_t outbuf_len)
50 struct tevent_req *req, *subreq;
51 struct smb2cli_query_directory_state *state;
52 uint8_t *fixed;
53 uint8_t *dyn;
54 size_t dyn_len;
56 req = tevent_req_create(mem_ctx, &state,
57 struct smb2cli_query_directory_state);
58 if (req == NULL) {
59 return NULL;
62 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
63 mask, strlen(mask),
64 &dyn, &dyn_len)) {
65 tevent_req_oom(req);
66 return tevent_req_post(req, ev);
69 if (strlen(mask) == 0) {
70 TALLOC_FREE(dyn);
71 dyn_len = 0;
74 fixed = state->fixed;
75 SSVAL(fixed, 0, 33);
76 SCVAL(fixed, 2, level);
77 SCVAL(fixed, 3, flags);
78 SIVAL(fixed, 4, file_index);
79 SBVAL(fixed, 8, fid_persistent);
80 SBVAL(fixed, 16, fid_volatile);
81 SSVAL(fixed, 24, SMB2_HDR_BODY + 32);
82 SSVAL(fixed, 26, dyn_len);
83 SSVAL(fixed, 28, outbuf_len);
85 if (dyn_len == 0) {
86 dyn = state->dyn_pad;
87 dyn_len = sizeof(state->dyn_pad);
90 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_FIND,
91 0, 0, /* flags */
92 timeout_msec,
93 tcon,
94 session,
95 state->fixed, sizeof(state->fixed),
96 dyn, dyn_len);
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);
101 return req;
104 static void smb2cli_query_directory_done(struct tevent_req *subreq)
106 struct tevent_req *req =
107 tevent_req_callback_data(subreq,
108 struct tevent_req);
109 struct smb2cli_query_directory_state *state =
110 tevent_req_data(req,
111 struct smb2cli_query_directory_state);
112 NTSTATUS status;
113 struct iovec *iov;
114 uint16_t data_offset;
115 static const struct smb2cli_req_expected_response expected[] = {
117 .status = NT_STATUS_OK,
118 .body_size = 0x09
122 status = smb2cli_req_recv(subreq, state, &iov,
123 expected, ARRAY_SIZE(expected));
124 TALLOC_FREE(subreq);
125 if (tevent_req_nterror(req, status)) {
126 return;
129 data_offset = SVAL(iov[1].iov_base, 2);
130 state->data_length = IVAL(iov[1].iov_base, 4);
132 if ((data_offset != SMB2_HDR_BODY + 8) ||
133 (state->data_length > iov[2].iov_len)) {
134 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
135 return;
138 state->recv_iov = iov;
139 state->data = (uint8_t *)iov[2].iov_base;
140 tevent_req_done(req);
143 NTSTATUS smb2cli_query_directory_recv(struct tevent_req *req,
144 TALLOC_CTX *mem_ctx,
145 uint8_t **data,
146 uint32_t *data_length)
148 struct smb2cli_query_directory_state *state =
149 tevent_req_data(req,
150 struct smb2cli_query_directory_state);
151 NTSTATUS status;
153 if (tevent_req_is_nterror(req, &status)) {
154 return status;
156 talloc_steal(mem_ctx, state->recv_iov);
157 *data_length = state->data_length;
158 *data = state->data;
159 return NT_STATUS_OK;
162 NTSTATUS smb2cli_query_directory(struct smbXcli_conn *conn,
163 uint32_t timeout_msec,
164 struct smbXcli_session *session,
165 struct smbXcli_tcon *tcon,
166 uint8_t level,
167 uint8_t flags,
168 uint32_t file_index,
169 uint64_t fid_persistent,
170 uint64_t fid_volatile,
171 const char *mask,
172 uint32_t outbuf_len,
173 TALLOC_CTX *mem_ctx,
174 uint8_t **data,
175 uint32_t *data_length)
177 TALLOC_CTX *frame = talloc_stackframe();
178 struct tevent_context *ev;
179 struct tevent_req *req;
180 NTSTATUS status = NT_STATUS_NO_MEMORY;
182 if (smbXcli_conn_has_async_calls(conn)) {
184 * Can't use sync call while an async call is in flight
186 status = NT_STATUS_INVALID_PARAMETER;
187 goto fail;
189 ev = samba_tevent_context_init(frame);
190 if (ev == NULL) {
191 goto fail;
193 req = smb2cli_query_directory_send(frame, ev, conn, timeout_msec,
194 session, tcon,
195 level, flags,
196 file_index, fid_persistent,
197 fid_volatile, mask, outbuf_len);
198 if (req == NULL) {
199 goto fail;
201 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
202 goto fail;
204 status = smb2cli_query_directory_recv(req, mem_ctx,
205 data, data_length);
206 fail:
207 TALLOC_FREE(frame);
208 return status;