s3-utils: net - Fix one error/usage message
[Samba/gebeck_regimport.git] / source3 / libsmb / smb2cli_query_directory.c
blob2d2c4658a1feba0371e31b0b7e000c965d4c78dc
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 "smb2cli_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 cli_state *cli,
41 uint8_t level,
42 uint8_t flags,
43 uint32_t file_index,
44 uint64_t fid_persistent,
45 uint64_t fid_volatile,
46 const char *mask,
47 uint32_t outbuf_len)
49 struct tevent_req *req, *subreq;
50 struct smb2cli_query_directory_state *state;
51 uint8_t *fixed;
52 uint8_t *dyn;
53 size_t dyn_len;
55 req = tevent_req_create(mem_ctx, &state,
56 struct smb2cli_query_directory_state);
57 if (req == NULL) {
58 return NULL;
61 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
62 mask, strlen(mask),
63 &dyn, &dyn_len)) {
64 tevent_req_oom(req);
65 return tevent_req_post(req, ev);
68 if (strlen(mask) == 0) {
69 TALLOC_FREE(dyn);
70 dyn_len = 0;
73 fixed = state->fixed;
74 SSVAL(fixed, 0, 33);
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);
84 if (dyn_len == 0) {
85 dyn = state->dyn_pad;
86 dyn_len = sizeof(state->dyn_pad);
89 subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_FIND,
90 0, 0, /* flags */
91 cli->timeout,
92 cli->smb2.pid,
93 cli->smb2.tid,
94 cli->smb2.uid,
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 if (tevent_req_nterror(req, status)) {
125 return;
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);
134 return;
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,
143 TALLOC_CTX *mem_ctx,
144 uint8_t **data,
145 uint32_t *data_length)
147 struct smb2cli_query_directory_state *state =
148 tevent_req_data(req,
149 struct smb2cli_query_directory_state);
150 NTSTATUS status;
152 if (tevent_req_is_nterror(req, &status)) {
153 return status;
155 talloc_steal(mem_ctx, state->recv_iov);
156 *data_length = state->data_length;
157 *data = state->data;
158 return NT_STATUS_OK;
161 NTSTATUS smb2cli_query_directory(struct cli_state *cli,
162 uint8_t level,
163 uint8_t flags,
164 uint32_t file_index,
165 uint64_t fid_persistent,
166 uint64_t fid_volatile,
167 const char *mask,
168 uint32_t outbuf_len,
169 TALLOC_CTX *mem_ctx,
170 uint8_t **data,
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;
183 goto fail;
185 ev = event_context_init(frame);
186 if (ev == NULL) {
187 goto fail;
189 req = smb2cli_query_directory_send(frame, ev, cli, level, flags,
190 file_index, fid_persistent,
191 fid_volatile, mask, outbuf_len);
192 if (req == NULL) {
193 goto fail;
195 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
196 goto fail;
198 status = smb2cli_query_directory_recv(req, mem_ctx,
199 data, data_length);
200 fail:
201 TALLOC_FREE(frame);
202 return status;