s4 dns: Forward questions we can't answer to another server
[Samba/bb.git] / source3 / smbd / smb2_find.c
blob9c0d18b278fa7033e6ba33cf901742f85dab729b
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "trans2.h"
26 #include "../lib/util/tevent_ntstatus.h"
28 static struct tevent_req *smbd_smb2_find_send(TALLOC_CTX *mem_ctx,
29 struct tevent_context *ev,
30 struct smbd_smb2_request *smb2req,
31 uint8_t in_file_info_class,
32 uint8_t in_flags,
33 uint32_t in_file_index,
34 uint64_t in_file_id_volatile,
35 uint32_t in_output_buffer_length,
36 const char *in_file_name);
37 static NTSTATUS smbd_smb2_find_recv(struct tevent_req *req,
38 TALLOC_CTX *mem_ctx,
39 DATA_BLOB *out_output_buffer);
41 static void smbd_smb2_request_find_done(struct tevent_req *subreq);
42 NTSTATUS smbd_smb2_request_process_find(struct smbd_smb2_request *req)
44 NTSTATUS status;
45 const uint8_t *inbody;
46 int i = req->current_idx;
47 uint8_t in_file_info_class;
48 uint8_t in_flags;
49 uint32_t in_file_index;
50 uint64_t in_file_id_persistent;
51 uint64_t in_file_id_volatile;
52 uint16_t in_file_name_offset;
53 uint16_t in_file_name_length;
54 DATA_BLOB in_file_name_buffer;
55 char *in_file_name_string;
56 size_t in_file_name_string_size;
57 uint32_t in_output_buffer_length;
58 struct tevent_req *subreq;
59 bool ok;
61 status = smbd_smb2_request_verify_sizes(req, 0x21);
62 if (!NT_STATUS_IS_OK(status)) {
63 return smbd_smb2_request_error(req, status);
65 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
67 in_file_info_class = CVAL(inbody, 0x02);
68 in_flags = CVAL(inbody, 0x03);
69 in_file_index = IVAL(inbody, 0x04);
70 in_file_id_persistent = BVAL(inbody, 0x08);
71 in_file_id_volatile = BVAL(inbody, 0x10);
72 in_file_name_offset = SVAL(inbody, 0x18);
73 in_file_name_length = SVAL(inbody, 0x1A);
74 in_output_buffer_length = IVAL(inbody, 0x1C);
76 if (in_file_name_offset == 0 && in_file_name_length == 0) {
77 /* This is ok */
78 } else if (in_file_name_offset !=
79 (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
80 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83 if (in_file_name_length > req->in.vector[i+2].iov_len) {
84 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
87 /* The output header is 8 bytes. */
88 if (in_output_buffer_length <= 8) {
89 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
92 DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
93 (unsigned int)in_output_buffer_length ));
95 /* Take into account the output header. */
96 in_output_buffer_length -= 8;
98 in_file_name_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
99 in_file_name_buffer.length = in_file_name_length;
101 ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
102 in_file_name_buffer.data,
103 in_file_name_buffer.length,
104 &in_file_name_string,
105 &in_file_name_string_size);
106 if (!ok) {
107 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
110 if (in_file_name_buffer.length == 0) {
111 in_file_name_string_size = 0;
114 if (strlen(in_file_name_string) != in_file_name_string_size) {
115 return smbd_smb2_request_error(req, NT_STATUS_OBJECT_NAME_INVALID);
118 if (req->compat_chain_fsp) {
119 /* skip check */
120 } else if (in_file_id_persistent != in_file_id_volatile) {
121 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
124 subreq = smbd_smb2_find_send(req,
125 req->sconn->ev_ctx,
126 req,
127 in_file_info_class,
128 in_flags,
129 in_file_index,
130 in_file_id_volatile,
131 in_output_buffer_length,
132 in_file_name_string);
133 if (subreq == NULL) {
134 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
136 tevent_req_set_callback(subreq, smbd_smb2_request_find_done, req);
138 return smbd_smb2_request_pending_queue(req, subreq, 500);
141 static void smbd_smb2_request_find_done(struct tevent_req *subreq)
143 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
144 struct smbd_smb2_request);
145 DATA_BLOB outbody;
146 DATA_BLOB outdyn;
147 uint16_t out_output_buffer_offset;
148 DATA_BLOB out_output_buffer = data_blob_null;
149 NTSTATUS status;
150 NTSTATUS error; /* transport error */
152 status = smbd_smb2_find_recv(subreq,
153 req,
154 &out_output_buffer);
155 TALLOC_FREE(subreq);
156 if (!NT_STATUS_IS_OK(status)) {
157 error = smbd_smb2_request_error(req, status);
158 if (!NT_STATUS_IS_OK(error)) {
159 smbd_server_connection_terminate(req->sconn,
160 nt_errstr(error));
161 return;
163 return;
166 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
168 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
169 if (outbody.data == NULL) {
170 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
171 if (!NT_STATUS_IS_OK(error)) {
172 smbd_server_connection_terminate(req->sconn,
173 nt_errstr(error));
174 return;
176 return;
179 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
180 SSVAL(outbody.data, 0x02,
181 out_output_buffer_offset); /* output buffer offset */
182 SIVAL(outbody.data, 0x04,
183 out_output_buffer.length); /* output buffer length */
185 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
186 (unsigned int)out_output_buffer.length ));
188 outdyn = out_output_buffer;
190 error = smbd_smb2_request_done(req, outbody, &outdyn);
191 if (!NT_STATUS_IS_OK(error)) {
192 smbd_server_connection_terminate(req->sconn,
193 nt_errstr(error));
194 return;
198 struct smbd_smb2_find_state {
199 struct smbd_smb2_request *smb2req;
200 DATA_BLOB out_output_buffer;
203 static struct tevent_req *smbd_smb2_find_send(TALLOC_CTX *mem_ctx,
204 struct tevent_context *ev,
205 struct smbd_smb2_request *smb2req,
206 uint8_t in_file_info_class,
207 uint8_t in_flags,
208 uint32_t in_file_index,
209 uint64_t in_file_id_volatile,
210 uint32_t in_output_buffer_length,
211 const char *in_file_name)
213 struct tevent_req *req;
214 struct smbd_smb2_find_state *state;
215 struct smb_request *smbreq;
216 connection_struct *conn = smb2req->tcon->compat_conn;
217 files_struct *fsp;
218 NTSTATUS status;
219 NTSTATUS empty_status;
220 uint32_t info_level;
221 uint32_t max_count;
222 char *pdata;
223 char *base_data;
224 char *end_data;
225 int last_entry_off = 0;
226 int off = 0;
227 uint32_t num = 0;
228 uint32_t dirtype = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY;
229 bool dont_descend = false;
230 bool ask_sharemode = true;
232 req = tevent_req_create(mem_ctx, &state,
233 struct smbd_smb2_find_state);
234 if (req == NULL) {
235 return NULL;
237 state->smb2req = smb2req;
238 state->out_output_buffer = data_blob_null;
240 DEBUG(10,("smbd_smb2_find_send: file_id[0x%016llX]\n",
241 (unsigned long long)in_file_id_volatile));
243 smbreq = smbd_smb2_fake_smb_request(smb2req);
244 if (tevent_req_nomem(smbreq, req)) {
245 return tevent_req_post(req, ev);
248 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
249 if (fsp == NULL) {
250 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
251 return tevent_req_post(req, ev);
253 if (conn != fsp->conn) {
254 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
255 return tevent_req_post(req, ev);
257 if (smb2req->session->vuid != fsp->vuid) {
258 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
259 return tevent_req_post(req, ev);
262 if (!fsp->is_directory) {
263 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
264 return tevent_req_post(req, ev);
267 if (strcmp(in_file_name, "") == 0) {
268 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
269 return tevent_req_post(req, ev);
271 if (strcmp(in_file_name, "\\") == 0) {
272 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
273 return tevent_req_post(req, ev);
275 if (strcmp(in_file_name, "/") == 0) {
276 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
277 return tevent_req_post(req, ev);
280 if (in_output_buffer_length > smb2req->sconn->smb2.max_trans) {
281 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
282 return tevent_req_post(req, ev);
285 status = smbd_smb2_request_verify_creditcharge(smb2req,
286 in_output_buffer_length);
288 if (!NT_STATUS_IS_OK(status)) {
289 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
290 return tevent_req_post(req, ev);
293 switch (in_file_info_class) {
294 case SMB2_FIND_DIRECTORY_INFO:
295 info_level = SMB_FIND_FILE_DIRECTORY_INFO;
296 break;
298 case SMB2_FIND_FULL_DIRECTORY_INFO:
299 info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
300 break;
302 case SMB2_FIND_BOTH_DIRECTORY_INFO:
303 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
304 break;
306 case SMB2_FIND_NAME_INFO:
307 info_level = SMB_FIND_FILE_NAMES_INFO;
308 break;
310 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
311 info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
312 break;
314 case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
315 info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
316 break;
318 default:
319 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
320 return tevent_req_post(req, ev);
323 if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
324 dptr_CloseDir(fsp);
327 if (fsp->dptr == NULL) {
328 bool wcard_has_wild;
330 wcard_has_wild = ms_has_wild(in_file_name);
332 status = dptr_create(conn,
333 NULL, /* req */
334 fsp,
335 fsp->fsp_name->base_name,
336 false, /* old_handle */
337 false, /* expect_close */
338 0, /* spid */
339 in_file_name, /* wcard */
340 wcard_has_wild,
341 dirtype,
342 &fsp->dptr);
343 if (!NT_STATUS_IS_OK(status)) {
344 tevent_req_nterror(req, status);
345 return tevent_req_post(req, ev);
348 empty_status = NT_STATUS_NO_SUCH_FILE;
349 } else {
350 empty_status = STATUS_NO_MORE_FILES;
353 if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
354 dptr_SeekDir(fsp->dptr, 0);
357 if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
358 max_count = 1;
359 } else {
360 max_count = UINT16_MAX;
363 #define DIR_ENTRY_SAFETY_MARGIN 4096
365 state->out_output_buffer = data_blob_talloc(state, NULL,
366 in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
367 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
368 return tevent_req_post(req, ev);
371 state->out_output_buffer.length = 0;
372 pdata = (char *)state->out_output_buffer.data;
373 base_data = pdata;
375 * end_data must include the safety margin as it's what is
376 * used to determine if pushed strings have been truncated.
378 end_data = pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
379 last_entry_off = 0;
380 off = 0;
381 num = 0;
383 DEBUG(8,("smbd_smb2_find_send: dirpath=<%s> dontdescend=<%s>, "
384 "in_output_buffer_length = %u\n",
385 fsp->fsp_name->base_name, lp_dontdescend(SNUM(conn)),
386 (unsigned int)in_output_buffer_length ));
387 if (in_list(fsp->fsp_name->base_name,lp_dontdescend(SNUM(conn)),
388 conn->case_sensitive)) {
389 dont_descend = true;
392 ask_sharemode = lp_parm_bool(SNUM(conn),
393 "smbd", "search ask sharemode",
394 true);
396 while (true) {
397 bool ok;
398 bool got_exact_match = false;
399 bool out_of_space = false;
400 int space_remaining = in_output_buffer_length - off;
402 SMB_ASSERT(space_remaining >= 0);
404 ok = smbd_dirptr_lanman2_entry(state,
405 conn,
406 fsp->dptr,
407 smbreq->flags2,
408 in_file_name,
409 dirtype,
410 info_level,
411 false, /* requires_resume_key */
412 dont_descend,
413 ask_sharemode,
414 8, /* align to 8 bytes */
415 false, /* no padding */
416 &pdata,
417 base_data,
418 end_data,
419 space_remaining,
420 &out_of_space,
421 &got_exact_match,
422 &last_entry_off,
423 NULL);
425 off = (int)PTR_DIFF(pdata, base_data);
427 if (!ok) {
428 if (num > 0) {
429 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
430 tevent_req_done(req);
431 return tevent_req_post(req, ev);
432 } else if (out_of_space) {
433 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
434 return tevent_req_post(req, ev);
435 } else {
436 tevent_req_nterror(req, empty_status);
437 return tevent_req_post(req, ev);
441 num++;
442 state->out_output_buffer.length = off;
444 if (num < max_count) {
445 continue;
448 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
449 tevent_req_done(req);
450 return tevent_req_post(req, ev);
453 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
454 return tevent_req_post(req, ev);
457 static NTSTATUS smbd_smb2_find_recv(struct tevent_req *req,
458 TALLOC_CTX *mem_ctx,
459 DATA_BLOB *out_output_buffer)
461 NTSTATUS status;
462 struct smbd_smb2_find_state *state = tevent_req_data(req,
463 struct smbd_smb2_find_state);
465 if (tevent_req_is_nterror(req, &status)) {
466 tevent_req_received(req);
467 return status;
470 *out_output_buffer = state->out_output_buffer;
471 talloc_steal(mem_ctx, out_output_buffer->data);
473 tevent_req_received(req);
474 return NT_STATUS_OK;