s4:dsdb/common: prepare dsdb_user_obj_set_defaults() for tombstone reanimation
[Samba.git] / source3 / smbd / smb2_query_directory.c
blobe18a279d9b404f951a210ba262e2a6c68e716498
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_query_directory_send(TALLOC_CTX *mem_ctx,
29 struct tevent_context *ev,
30 struct smbd_smb2_request *smb2req,
31 struct files_struct *in_fsp,
32 uint8_t in_file_info_class,
33 uint8_t in_flags,
34 uint32_t in_file_index,
35 uint32_t in_output_buffer_length,
36 const char *in_file_name);
37 static NTSTATUS smbd_smb2_query_directory_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_query_directory(struct smbd_smb2_request *req)
44 NTSTATUS status;
45 const uint8_t *inbody;
46 uint8_t in_file_info_class;
47 uint8_t in_flags;
48 uint32_t in_file_index;
49 uint64_t in_file_id_persistent;
50 uint64_t in_file_id_volatile;
51 struct files_struct *in_fsp;
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 = SMBD_SMB2_IN_BODY_PTR(req);
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 + SMBD_SMB2_IN_BODY_LEN(req))) {
80 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83 if (in_file_name_length > SMBD_SMB2_IN_DYN_LEN(req)) {
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 = SMBD_SMB2_IN_DYN_PTR(req);
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 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
119 if (in_fsp == NULL) {
120 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
123 subreq = smbd_smb2_query_directory_send(req, req->sconn->ev_ctx,
124 req, in_fsp,
125 in_file_info_class,
126 in_flags,
127 in_file_index,
128 in_output_buffer_length,
129 in_file_name_string);
130 if (subreq == NULL) {
131 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
133 tevent_req_set_callback(subreq, smbd_smb2_request_find_done, req);
135 return smbd_smb2_request_pending_queue(req, subreq, 500);
138 static void smbd_smb2_request_find_done(struct tevent_req *subreq)
140 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
141 struct smbd_smb2_request);
142 DATA_BLOB outbody;
143 DATA_BLOB outdyn;
144 uint16_t out_output_buffer_offset;
145 DATA_BLOB out_output_buffer = data_blob_null;
146 NTSTATUS status;
147 NTSTATUS error; /* transport error */
149 status = smbd_smb2_query_directory_recv(subreq,
150 req,
151 &out_output_buffer);
152 TALLOC_FREE(subreq);
153 if (!NT_STATUS_IS_OK(status)) {
154 error = smbd_smb2_request_error(req, status);
155 if (!NT_STATUS_IS_OK(error)) {
156 smbd_server_connection_terminate(req->xconn,
157 nt_errstr(error));
158 return;
160 return;
163 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
165 outbody = smbd_smb2_generate_outbody(req, 0x08);
166 if (outbody.data == NULL) {
167 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
168 if (!NT_STATUS_IS_OK(error)) {
169 smbd_server_connection_terminate(req->xconn,
170 nt_errstr(error));
171 return;
173 return;
176 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
177 SSVAL(outbody.data, 0x02,
178 out_output_buffer_offset); /* output buffer offset */
179 SIVAL(outbody.data, 0x04,
180 out_output_buffer.length); /* output buffer length */
182 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
183 (unsigned int)out_output_buffer.length ));
185 outdyn = out_output_buffer;
187 error = smbd_smb2_request_done(req, outbody, &outdyn);
188 if (!NT_STATUS_IS_OK(error)) {
189 smbd_server_connection_terminate(req->xconn,
190 nt_errstr(error));
191 return;
195 struct smbd_smb2_query_directory_state {
196 struct smbd_smb2_request *smb2req;
197 DATA_BLOB out_output_buffer;
200 static struct tevent_req *smbd_smb2_query_directory_send(TALLOC_CTX *mem_ctx,
201 struct tevent_context *ev,
202 struct smbd_smb2_request *smb2req,
203 struct files_struct *fsp,
204 uint8_t in_file_info_class,
205 uint8_t in_flags,
206 uint32_t in_file_index,
207 uint32_t in_output_buffer_length,
208 const char *in_file_name)
210 struct smbXsrv_connection *xconn = smb2req->xconn;
211 struct tevent_req *req;
212 struct smbd_smb2_query_directory_state *state;
213 struct smb_request *smbreq;
214 connection_struct *conn = smb2req->tcon->compat;
215 NTSTATUS status;
216 NTSTATUS empty_status;
217 uint32_t info_level;
218 uint32_t max_count;
219 char *pdata;
220 char *base_data;
221 char *end_data;
222 int last_entry_off = 0;
223 int off = 0;
224 uint32_t num = 0;
225 uint32_t dirtype = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY;
226 bool dont_descend = false;
227 bool ask_sharemode = true;
228 bool wcard_has_wild = false;
229 struct tm tm;
230 char *p;
232 req = tevent_req_create(mem_ctx, &state,
233 struct smbd_smb2_query_directory_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_query_directory_send: %s - %s\n",
241 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
243 smbreq = smbd_smb2_fake_smb_request(smb2req);
244 if (tevent_req_nomem(smbreq, req)) {
245 return tevent_req_post(req, ev);
248 if (!fsp->is_directory) {
249 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
250 return tevent_req_post(req, ev);
253 if (strcmp(in_file_name, "") == 0) {
254 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
255 return tevent_req_post(req, ev);
257 if (strchr_m(in_file_name, '\\') != NULL) {
258 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
259 return tevent_req_post(req, ev);
261 if (strchr_m(in_file_name, '/') != NULL) {
262 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
263 return tevent_req_post(req, ev);
266 p = strptime(in_file_name, GMT_FORMAT, &tm);
267 if ((p != NULL) && (*p =='\0')) {
269 * Bogus find that asks for a shadow copy timestamp as a
270 * directory. The correct response is that it does not exist as
271 * a directory.
273 tevent_req_nterror(req, NT_STATUS_NO_SUCH_FILE);
274 return tevent_req_post(req, ev);
277 if (in_output_buffer_length > xconn->smb2.server.max_trans) {
278 DEBUG(2,("smbd_smb2_query_directory_send: "
279 "client ignored max trans:%s: 0x%08X: 0x%08X\n",
280 __location__, in_output_buffer_length,
281 xconn->smb2.server.max_trans));
282 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
283 return tevent_req_post(req, ev);
286 status = smbd_smb2_request_verify_creditcharge(smb2req,
287 in_output_buffer_length);
289 if (!NT_STATUS_IS_OK(status)) {
290 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
291 return tevent_req_post(req, ev);
294 switch (in_file_info_class) {
295 case SMB2_FIND_DIRECTORY_INFO:
296 info_level = SMB_FIND_FILE_DIRECTORY_INFO;
297 break;
299 case SMB2_FIND_FULL_DIRECTORY_INFO:
300 info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
301 break;
303 case SMB2_FIND_BOTH_DIRECTORY_INFO:
304 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
305 break;
307 case SMB2_FIND_NAME_INFO:
308 info_level = SMB_FIND_FILE_NAMES_INFO;
309 break;
311 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
312 info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
313 break;
315 case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
316 info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
317 break;
319 default:
320 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
321 return tevent_req_post(req, ev);
324 if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
325 dptr_CloseDir(fsp);
328 if (!smbreq->posix_pathnames) {
329 wcard_has_wild = ms_has_wild(in_file_name);
332 /* Ensure we've canonicalized any search path if not a wildcard. */
333 if (!wcard_has_wild) {
334 struct smb_filename *smb_fname = NULL;
335 const char *fullpath;
336 char tmpbuf[PATH_MAX];
337 char *to_free = NULL;
338 uint32_t ucf_flags = UCF_SAVE_LCOMP |
339 UCF_ALWAYS_ALLOW_WCARD_LCOMP |
340 (smbreq->posix_pathnames ?
341 UCF_POSIX_PATHNAMES : 0);
343 if (ISDOT(fsp->fsp_name->base_name)) {
344 fullpath = in_file_name;
345 } else {
346 size_t len;
347 char *tmp;
349 len = full_path_tos(
350 fsp->fsp_name->base_name, in_file_name,
351 tmpbuf, sizeof(tmpbuf), &tmp, &to_free);
352 if (len == -1) {
353 tevent_req_oom(req);
354 return tevent_req_post(req, ev);
356 fullpath = tmp;
358 status = filename_convert(state,
359 conn,
360 false, /* Not a DFS path. */
361 fullpath,
362 ucf_flags,
363 &wcard_has_wild,
364 &smb_fname);
366 TALLOC_FREE(to_free);
368 if (tevent_req_nterror(req, status)) {
369 return tevent_req_post(req, ev);
372 in_file_name = smb_fname->original_lcomp;
375 if (fsp->dptr == NULL) {
376 status = dptr_create(conn,
377 NULL, /* req */
378 fsp,
379 fsp->fsp_name,
380 false, /* old_handle */
381 false, /* expect_close */
382 0, /* spid */
383 in_file_name, /* wcard */
384 wcard_has_wild,
385 dirtype,
386 &fsp->dptr);
387 if (!NT_STATUS_IS_OK(status)) {
388 tevent_req_nterror(req, status);
389 return tevent_req_post(req, ev);
392 empty_status = NT_STATUS_NO_SUCH_FILE;
393 } else {
394 empty_status = STATUS_NO_MORE_FILES;
397 if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
398 dptr_SeekDir(fsp->dptr, 0);
401 if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
402 max_count = 1;
403 } else {
404 max_count = UINT16_MAX;
407 #define DIR_ENTRY_SAFETY_MARGIN 4096
409 state->out_output_buffer = data_blob_talloc(state, NULL,
410 in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
411 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
412 return tevent_req_post(req, ev);
415 state->out_output_buffer.length = 0;
416 pdata = (char *)state->out_output_buffer.data;
417 base_data = pdata;
419 * end_data must include the safety margin as it's what is
420 * used to determine if pushed strings have been truncated.
422 end_data = pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
423 last_entry_off = 0;
424 off = 0;
425 num = 0;
427 DEBUG(8,("smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
428 "in_output_buffer_length = %u\n",
429 fsp->fsp_name->base_name, lp_dont_descend(talloc_tos(), SNUM(conn)),
430 (unsigned int)in_output_buffer_length ));
431 if (in_list(fsp->fsp_name->base_name,lp_dont_descend(talloc_tos(), SNUM(conn)),
432 conn->case_sensitive)) {
433 dont_descend = true;
436 ask_sharemode = lp_parm_bool(SNUM(conn),
437 "smbd", "search ask sharemode",
438 true);
440 while (true) {
441 bool got_exact_match = false;
442 int space_remaining = in_output_buffer_length - off;
444 SMB_ASSERT(space_remaining >= 0);
446 status = smbd_dirptr_lanman2_entry(state,
447 conn,
448 fsp->dptr,
449 smbreq->flags2,
450 in_file_name,
451 dirtype,
452 info_level,
453 false, /* requires_resume_key */
454 dont_descend,
455 ask_sharemode,
456 8, /* align to 8 bytes */
457 false, /* no padding */
458 &pdata,
459 base_data,
460 end_data,
461 space_remaining,
462 &got_exact_match,
463 &last_entry_off,
464 NULL);
466 off = (int)PTR_DIFF(pdata, base_data);
468 if (!NT_STATUS_IS_OK(status)) {
469 if (NT_STATUS_EQUAL(status, NT_STATUS_ILLEGAL_CHARACTER)) {
471 * Bad character conversion on name. Ignore this
472 * entry.
474 continue;
475 } else if (num > 0) {
476 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
477 tevent_req_done(req);
478 return tevent_req_post(req, ev);
479 } else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
480 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
481 return tevent_req_post(req, ev);
482 } else {
483 tevent_req_nterror(req, empty_status);
484 return tevent_req_post(req, ev);
488 num++;
489 state->out_output_buffer.length = off;
491 if (num < max_count) {
492 continue;
495 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
496 tevent_req_done(req);
497 return tevent_req_post(req, ev);
500 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
501 return tevent_req_post(req, ev);
504 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
505 TALLOC_CTX *mem_ctx,
506 DATA_BLOB *out_output_buffer)
508 NTSTATUS status;
509 struct smbd_smb2_query_directory_state *state = tevent_req_data(req,
510 struct smbd_smb2_query_directory_state);
512 if (tevent_req_is_nterror(req, &status)) {
513 tevent_req_received(req);
514 return status;
517 *out_output_buffer = state->out_output_buffer;
518 talloc_steal(mem_ctx, out_output_buffer->data);
520 tevent_req_received(req);
521 return NT_STATUS_OK;