smbdotconf: add client ldap sasl wrapping = {starttls,ldaps}
[samba.git] / source3 / smbd / smb2_query_directory.c
blobebbbbb39911037402f05f511740763a0e45982ee
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 "locking/share_mode_lock.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "trans2.h"
27 #include "../lib/util/tevent_ntstatus.h"
28 #include "system/filesys.h"
29 #include "lib/pthreadpool/pthreadpool_tevent.h"
30 #include "source3/smbd/dir.h"
32 #undef DBGC_CLASS
33 #define DBGC_CLASS DBGC_SMB2
35 static struct tevent_req *smbd_smb2_query_directory_send(TALLOC_CTX *mem_ctx,
36 struct tevent_context *ev,
37 struct smbd_smb2_request *smb2req,
38 struct files_struct *in_fsp,
39 uint8_t in_file_info_class,
40 uint8_t in_flags,
41 uint32_t in_file_index,
42 uint32_t in_output_buffer_length,
43 const char *in_file_name);
44 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
45 TALLOC_CTX *mem_ctx,
46 DATA_BLOB *out_output_buffer);
48 static void smbd_smb2_request_find_done(struct tevent_req *subreq);
49 NTSTATUS smbd_smb2_request_process_query_directory(struct smbd_smb2_request *req)
51 NTSTATUS status;
52 const uint8_t *inbody;
53 uint8_t in_file_info_class;
54 uint8_t in_flags;
55 uint32_t in_file_index;
56 uint64_t in_file_id_persistent;
57 uint64_t in_file_id_volatile;
58 struct files_struct *in_fsp;
59 uint16_t in_file_name_offset;
60 uint16_t in_file_name_length;
61 DATA_BLOB in_file_name_buffer;
62 char *in_file_name_string;
63 size_t in_file_name_string_size;
64 uint32_t in_output_buffer_length;
65 struct tevent_req *subreq;
66 bool ok;
68 status = smbd_smb2_request_verify_sizes(req, 0x21);
69 if (!NT_STATUS_IS_OK(status)) {
70 return smbd_smb2_request_error(req, status);
72 inbody = SMBD_SMB2_IN_BODY_PTR(req);
74 in_file_info_class = CVAL(inbody, 0x02);
75 in_flags = CVAL(inbody, 0x03);
76 in_file_index = IVAL(inbody, 0x04);
77 in_file_id_persistent = BVAL(inbody, 0x08);
78 in_file_id_volatile = BVAL(inbody, 0x10);
79 in_file_name_offset = SVAL(inbody, 0x18);
80 in_file_name_length = SVAL(inbody, 0x1A);
81 in_output_buffer_length = IVAL(inbody, 0x1C);
83 if (in_file_name_offset == 0 && in_file_name_length == 0) {
84 /* This is ok */
85 } else if (in_file_name_offset !=
86 (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
87 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
90 if (in_file_name_length > SMBD_SMB2_IN_DYN_LEN(req)) {
91 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
94 /* The output header is 8 bytes. */
95 if (in_output_buffer_length <= 8) {
96 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
99 DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
100 (unsigned int)in_output_buffer_length ));
102 /* Take into account the output header. */
103 in_output_buffer_length -= 8;
105 in_file_name_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
106 in_file_name_buffer.length = in_file_name_length;
108 ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
109 in_file_name_buffer.data,
110 in_file_name_buffer.length,
111 &in_file_name_string,
112 &in_file_name_string_size);
113 if (!ok) {
114 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
117 if (in_file_name_buffer.length == 0) {
118 in_file_name_string_size = 0;
121 if (strlen(in_file_name_string) != in_file_name_string_size) {
122 return smbd_smb2_request_error(req, NT_STATUS_OBJECT_NAME_INVALID);
125 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
126 if (in_fsp == NULL) {
127 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
130 subreq = smbd_smb2_query_directory_send(req, req->sconn->ev_ctx,
131 req, in_fsp,
132 in_file_info_class,
133 in_flags,
134 in_file_index,
135 in_output_buffer_length,
136 in_file_name_string);
137 if (subreq == NULL) {
138 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
140 tevent_req_set_callback(subreq, smbd_smb2_request_find_done, req);
142 return smbd_smb2_request_pending_queue(req, subreq, 500);
145 static void smbd_smb2_request_find_done(struct tevent_req *subreq)
147 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
148 struct smbd_smb2_request);
149 DATA_BLOB outbody;
150 DATA_BLOB outdyn;
151 uint16_t out_output_buffer_offset;
152 DATA_BLOB out_output_buffer = data_blob_null;
153 NTSTATUS status;
154 NTSTATUS error; /* transport error */
156 status = smbd_smb2_query_directory_recv(subreq,
157 req,
158 &out_output_buffer);
159 TALLOC_FREE(subreq);
160 if (!NT_STATUS_IS_OK(status)) {
161 error = smbd_smb2_request_error(req, status);
162 if (!NT_STATUS_IS_OK(error)) {
163 smbd_server_connection_terminate(req->xconn,
164 nt_errstr(error));
165 return;
167 return;
170 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
172 outbody = smbd_smb2_generate_outbody(req, 0x08);
173 if (outbody.data == NULL) {
174 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
175 if (!NT_STATUS_IS_OK(error)) {
176 smbd_server_connection_terminate(req->xconn,
177 nt_errstr(error));
178 return;
180 return;
183 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
184 SSVAL(outbody.data, 0x02,
185 out_output_buffer_offset); /* output buffer offset */
186 SIVAL(outbody.data, 0x04,
187 out_output_buffer.length); /* output buffer length */
189 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
190 (unsigned int)out_output_buffer.length ));
192 outdyn = out_output_buffer;
194 error = smbd_smb2_request_done(req, outbody, &outdyn);
195 if (!NT_STATUS_IS_OK(error)) {
196 smbd_server_connection_terminate(req->xconn,
197 nt_errstr(error));
198 return;
202 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
203 struct tevent_context *ev,
204 connection_struct *conn,
205 struct file_id id,
206 int info_level,
207 char *entry_marshall_buf,
208 bool *stop);
209 static NTSTATUS fetch_write_time_recv(struct tevent_req *req);
211 static struct tevent_req *fetch_dos_mode_send(
212 TALLOC_CTX *mem_ctx,
213 struct tevent_context *ev,
214 struct files_struct *dir_fsp,
215 struct smb_filename **smb_fname,
216 uint32_t info_level,
217 uint8_t *entry_marshall_buf);
219 static NTSTATUS fetch_dos_mode_recv(struct tevent_req *req);
221 struct smbd_smb2_query_directory_state {
222 struct tevent_context *ev;
223 struct smbd_smb2_request *smb2req;
224 uint64_t async_sharemode_count;
225 uint32_t find_async_delay_usec;
226 DATA_BLOB out_output_buffer;
227 struct smb_request *smbreq;
228 int in_output_buffer_length;
229 struct files_struct *dirfsp;
230 const char *in_file_name;
231 NTSTATUS empty_status;
232 uint32_t info_level;
233 uint32_t max_count;
234 char *pdata;
235 char *base_data;
236 char *end_data;
237 uint32_t num;
238 uint32_t dirtype;
239 bool dont_descend;
240 bool ask_sharemode;
241 bool async_dosmode;
242 bool async_ask_sharemode;
243 int last_entry_off;
244 size_t max_async_dosmode_active;
245 uint32_t async_dosmode_active;
246 bool done;
249 static bool smb2_query_directory_next_entry(struct tevent_req *req);
250 static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq);
251 static void smb2_query_directory_dos_mode_done(struct tevent_req *subreq);
252 static void smb2_query_directory_waited(struct tevent_req *subreq);
254 static struct tevent_req *smbd_smb2_query_directory_send(TALLOC_CTX *mem_ctx,
255 struct tevent_context *ev,
256 struct smbd_smb2_request *smb2req,
257 struct files_struct *fsp,
258 uint8_t in_file_info_class,
259 uint8_t in_flags,
260 uint32_t in_file_index,
261 uint32_t in_output_buffer_length,
262 const char *in_file_name)
264 struct smbXsrv_connection *xconn = smb2req->xconn;
265 struct tevent_req *req;
266 struct smbd_smb2_query_directory_state *state;
267 connection_struct *conn = smb2req->tcon->compat;
268 const struct loadparm_substitution *lp_sub =
269 loadparm_s3_global_substitution();
270 NTSTATUS status;
271 bool wcard_has_wild = false;
272 struct tm tm = {};
273 char *p;
274 bool stop = false;
275 bool ok;
276 bool posix_dir_handle = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN);
278 req = tevent_req_create(mem_ctx, &state,
279 struct smbd_smb2_query_directory_state);
280 if (req == NULL) {
281 return NULL;
283 state->ev = ev;
284 state->dirfsp = fsp;
285 state->smb2req = smb2req;
286 state->in_output_buffer_length = in_output_buffer_length;
287 state->in_file_name = in_file_name;
288 state->out_output_buffer = data_blob_null;
289 state->dirtype = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY;
291 DEBUG(10,("smbd_smb2_query_directory_send: %s - %s\n",
292 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
294 state->smbreq = smbd_smb2_fake_smb_request(smb2req, fsp);
295 if (tevent_req_nomem(state->smbreq, req)) {
296 return tevent_req_post(req, ev);
299 if (!fsp->fsp_flags.is_directory) {
300 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
301 return tevent_req_post(req, ev);
304 if (strcmp(state->in_file_name, "") == 0) {
305 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
306 return tevent_req_post(req, ev);
308 if (strchr_m(state->in_file_name, '\\') != NULL) {
309 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
310 return tevent_req_post(req, ev);
312 if (strchr_m(state->in_file_name, '/') != NULL) {
313 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
314 return tevent_req_post(req, ev);
317 p = strptime(state->in_file_name, GMT_FORMAT, &tm);
318 if ((p != NULL) && (*p =='\0')) {
320 * Bogus find that asks for a shadow copy timestamp as a
321 * directory. The correct response is that it does not exist as
322 * a directory.
324 tevent_req_nterror(req, NT_STATUS_NO_SUCH_FILE);
325 return tevent_req_post(req, ev);
328 if (in_output_buffer_length > xconn->smb2.server.max_trans) {
329 DEBUG(2,("smbd_smb2_query_directory_send: "
330 "client ignored max trans:%s: 0x%08X: 0x%08X\n",
331 __location__, in_output_buffer_length,
332 xconn->smb2.server.max_trans));
333 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
334 return tevent_req_post(req, ev);
337 status = smbd_smb2_request_verify_creditcharge(smb2req,
338 in_output_buffer_length);
340 if (!NT_STATUS_IS_OK(status)) {
341 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
342 return tevent_req_post(req, ev);
345 switch (in_file_info_class) {
346 case SMB2_FIND_DIRECTORY_INFO:
347 state->info_level = SMB_FIND_FILE_DIRECTORY_INFO;
348 break;
350 case SMB2_FIND_FULL_DIRECTORY_INFO:
351 state->info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
352 break;
354 case SMB2_FIND_BOTH_DIRECTORY_INFO:
355 state->info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
356 break;
358 case SMB2_FIND_NAME_INFO:
359 state->info_level = SMB_FIND_FILE_NAMES_INFO;
360 break;
362 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
363 state->info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
364 break;
366 case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
367 state->info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
368 break;
370 case SMB2_FIND_POSIX_INFORMATION:
371 if (!(fsp->posix_flags & FSP_POSIX_FLAGS_OPEN)) {
372 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
373 return tevent_req_post(req, ev);
375 state->info_level = SMB2_FILE_POSIX_INFORMATION;
376 break;
377 default:
378 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
379 return tevent_req_post(req, ev);
382 if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
383 struct vfs_open_how how = { .flags = O_RDONLY, };
385 status = fd_close(fsp);
386 if (tevent_req_nterror(req, status)) {
387 return tevent_req_post(req, ev);
391 * fd_close() will close and invalidate the fsp's file
392 * descriptor. So we have to reopen it.
395 #ifdef O_DIRECTORY
396 how.flags |= O_DIRECTORY;
397 #endif
398 status = fd_openat(conn->cwd_fsp, fsp->fsp_name, fsp, &how);
399 if (tevent_req_nterror(req, status)) {
400 return tevent_req_post(req, ev);
404 if (!state->smbreq->posix_pathnames) {
405 wcard_has_wild = ms_has_wild(state->in_file_name);
408 /* Ensure we've canonicalized any search path if not a wildcard. */
409 if (!wcard_has_wild) {
411 * We still need to do the case processing
412 * to save off the client-supplied last component.
413 * At least we know there's no @GMT normalization
414 * or MS-DFS paths to do in a directory mask.
416 state->in_file_name = get_original_lcomp(state,
417 conn,
418 state->in_file_name,
420 if (tevent_req_nomem(state->in_file_name, req)) {
421 return tevent_req_post(req, ev);
425 if (fsp->dptr == NULL) {
426 status = dptr_create(conn,
427 NULL, /* req */
428 fsp,
429 false, /* old_handle */
430 state->in_file_name, /* wcard */
431 state->dirtype,
432 &fsp->dptr);
433 if (!NT_STATUS_IS_OK(status)) {
434 tevent_req_nterror(req, status);
435 return tevent_req_post(req, ev);
438 state->empty_status = NT_STATUS_NO_SUCH_FILE;
439 } else {
440 state->empty_status = STATUS_NO_MORE_FILES;
443 if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
444 dptr_RewindDir(fsp->dptr);
447 if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
448 state->max_count = 1;
449 } else {
450 state->max_count = UINT16_MAX;
453 #define DIR_ENTRY_SAFETY_MARGIN 4096
455 state->out_output_buffer = data_blob_talloc(state, NULL,
456 in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
457 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
458 return tevent_req_post(req, ev);
461 state->out_output_buffer.length = 0;
462 state->pdata = (char *)state->out_output_buffer.data;
463 state->base_data = state->pdata;
465 * end_data must include the safety margin as it's what is
466 * used to determine if pushed strings have been truncated.
468 state->end_data = state->pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
470 DEBUG(8,("smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
471 "in_output_buffer_length = %u\n",
472 fsp->fsp_name->base_name, lp_dont_descend(talloc_tos(), lp_sub, SNUM(conn)),
473 (unsigned int)in_output_buffer_length ));
475 state->dont_descend = in_list(
476 fsp->fsp_name->base_name,
477 lp_dont_descend(talloc_tos(), lp_sub, SNUM(conn)),
478 posix_dir_handle ? true : conn->case_sensitive);
481 * SMB_FIND_FILE_NAMES_INFO doesn't need stat information
483 * This may change when we try to improve the delete on close
484 * handling in future.
486 if (state->info_level != SMB_FIND_FILE_NAMES_INFO) {
487 state->ask_sharemode = fsp_search_ask_sharemode(fsp);
489 state->async_dosmode = lp_smbd_async_dosmode(SNUM(conn));
492 if (state->ask_sharemode && lp_clustering()) {
493 state->ask_sharemode = false;
494 state->async_ask_sharemode = true;
497 if (state->async_dosmode) {
498 size_t max_threads;
500 max_threads = pthreadpool_tevent_max_threads(conn->sconn->pool);
501 if (max_threads == 0 || !per_thread_cwd_supported()) {
502 state->async_dosmode = false;
505 state->max_async_dosmode_active = lp_smbd_max_async_dosmode(
506 SNUM(conn));
507 if (state->max_async_dosmode_active == 0) {
508 state->max_async_dosmode_active = max_threads * 2;
512 if (state->async_dosmode || state->async_ask_sharemode) {
514 * Should we only set async_internal
515 * if we're not the last request in
516 * a compound chain?
518 smb2_request_set_async_internal(smb2req, true);
522 * This gets set in autobuild for some tests
524 state->find_async_delay_usec = lp_parm_ulong(SNUM(conn), "smbd",
525 "find async delay usec",
528 while (!stop) {
529 stop = smb2_query_directory_next_entry(req);
532 if (!tevent_req_is_in_progress(req)) {
533 return tevent_req_post(req, ev);
536 ok = aio_add_req_to_fsp(fsp, req);
537 if (!ok) {
538 DBG_ERR("Could not add req to fsp\n");
539 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
540 return tevent_req_post(req, ev);
543 return req;
546 static bool smb2_query_directory_next_entry(struct tevent_req *req)
548 struct smbd_smb2_query_directory_state *state = tevent_req_data(
549 req, struct smbd_smb2_query_directory_state);
550 struct smb_filename *smb_fname = NULL; /* relative to fsp !! */
551 int off = state->out_output_buffer.length;
552 int space_remaining = state->in_output_buffer_length - off;
553 struct file_id file_id;
554 NTSTATUS status;
555 bool get_dosmode = !state->async_dosmode;
556 bool stop = false;
558 SMB_ASSERT(space_remaining >= 0);
560 status = smbd_dirptr_lanman2_entry(state,
561 state->dirfsp->conn,
562 state->dirfsp->dptr,
563 state->smbreq->flags2,
564 state->in_file_name,
565 state->dirtype,
566 state->info_level,
567 false, /* requires_resume_key */
568 state->dont_descend,
569 state->ask_sharemode,
570 get_dosmode,
571 8, /* align to 8 bytes */
572 false, /* no padding */
573 &state->pdata,
574 state->base_data,
575 state->end_data,
576 space_remaining,
577 &smb_fname,
578 &state->last_entry_off,
579 NULL,
580 &file_id);
582 off = (int)PTR_DIFF(state->pdata, state->base_data);
584 if (!NT_STATUS_IS_OK(status)) {
585 if (NT_STATUS_EQUAL(status, NT_STATUS_ILLEGAL_CHARACTER)) {
587 * Bad character conversion on name. Ignore this
588 * entry.
590 return false;
593 if (state->num > 0) {
594 goto last_entry_done;
597 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
598 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
599 return true;
602 tevent_req_nterror(req, state->empty_status);
603 return true;
606 if (state->async_ask_sharemode &&
607 !S_ISDIR(smb_fname->st.st_ex_mode))
609 struct tevent_req *subreq = NULL;
610 char *buf = state->base_data + state->last_entry_off;
612 subreq = fetch_write_time_send(state,
613 state->ev,
614 state->dirfsp->conn,
615 file_id,
616 state->info_level,
617 buf,
618 &stop);
619 if (tevent_req_nomem(subreq, req)) {
620 return true;
622 tevent_req_set_callback(
623 subreq,
624 smb2_query_directory_fetch_write_time_done,
625 req);
626 state->async_sharemode_count++;
629 if (state->async_dosmode) {
630 struct tevent_req *subreq = NULL;
631 uint8_t *buf = NULL;
632 size_t outstanding_aio;
634 buf = (uint8_t *)state->base_data + state->last_entry_off;
636 subreq = fetch_dos_mode_send(state,
637 state->ev,
638 state->dirfsp,
639 &smb_fname,
640 state->info_level,
641 buf);
642 if (tevent_req_nomem(subreq, req)) {
643 return true;
645 tevent_req_set_callback(subreq,
646 smb2_query_directory_dos_mode_done,
647 req);
649 state->async_dosmode_active++;
651 outstanding_aio = pthreadpool_tevent_queued_jobs(
652 state->dirfsp->conn->sconn->pool);
654 if (outstanding_aio > state->max_async_dosmode_active) {
655 stop = true;
659 TALLOC_FREE(smb_fname);
661 state->num++;
662 state->out_output_buffer.length = off;
664 if (!state->done && state->num < state->max_count) {
665 return stop;
668 last_entry_done:
669 SIVAL(state->out_output_buffer.data, state->last_entry_off, 0);
671 state->done = true;
673 if (state->async_sharemode_count > 0) {
674 DBG_DEBUG("Stopping after %"PRIu64" async mtime "
675 "updates\n", state->async_sharemode_count);
676 return true;
679 if (state->async_dosmode_active > 0) {
680 return true;
683 if (state->find_async_delay_usec > 0) {
684 struct timeval tv;
685 struct tevent_req *subreq = NULL;
688 * Should we only set async_internal
689 * if we're not the last request in
690 * a compound chain?
692 smb2_request_set_async_internal(state->smb2req, true);
694 tv = timeval_current_ofs(0, state->find_async_delay_usec);
696 subreq = tevent_wakeup_send(state, state->ev, tv);
697 if (tevent_req_nomem(subreq, req)) {
698 return true;
700 tevent_req_set_callback(subreq,
701 smb2_query_directory_waited,
702 req);
703 return true;
706 tevent_req_done(req);
707 return true;
710 static void smb2_query_directory_check_next_entry(struct tevent_req *req);
712 static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq)
714 struct tevent_req *req = tevent_req_callback_data(
715 subreq, struct tevent_req);
716 struct smbd_smb2_query_directory_state *state = tevent_req_data(
717 req, struct smbd_smb2_query_directory_state);
718 NTSTATUS status;
719 bool ok;
722 * Make sure we run as the user again
724 ok = change_to_user_and_service_by_fsp(state->dirfsp);
725 SMB_ASSERT(ok);
727 state->async_sharemode_count--;
729 status = fetch_write_time_recv(subreq);
730 TALLOC_FREE(subreq);
731 if (tevent_req_nterror(req, status)) {
732 return;
735 smb2_query_directory_check_next_entry(req);
736 return;
739 static void smb2_query_directory_dos_mode_done(struct tevent_req *subreq)
741 struct tevent_req *req =
742 tevent_req_callback_data(subreq,
743 struct tevent_req);
744 struct smbd_smb2_query_directory_state *state =
745 tevent_req_data(req,
746 struct smbd_smb2_query_directory_state);
747 NTSTATUS status;
748 bool ok;
751 * Make sure we run as the user again
753 ok = change_to_user_and_service_by_fsp(state->dirfsp);
754 SMB_ASSERT(ok);
756 status = fetch_dos_mode_recv(subreq);
757 TALLOC_FREE(subreq);
758 if (tevent_req_nterror(req, status)) {
759 return;
762 state->async_dosmode_active--;
764 smb2_query_directory_check_next_entry(req);
765 return;
768 static void smb2_query_directory_check_next_entry(struct tevent_req *req)
770 struct smbd_smb2_query_directory_state *state = tevent_req_data(
771 req, struct smbd_smb2_query_directory_state);
772 bool stop = false;
774 if (!state->done) {
775 while (!stop) {
776 stop = smb2_query_directory_next_entry(req);
778 return;
781 if (state->async_sharemode_count > 0 ||
782 state->async_dosmode_active > 0)
784 return;
787 if (state->find_async_delay_usec > 0) {
788 struct timeval tv;
789 struct tevent_req *subreq = NULL;
791 tv = timeval_current_ofs(0, state->find_async_delay_usec);
793 subreq = tevent_wakeup_send(state, state->ev, tv);
794 if (tevent_req_nomem(subreq, req)) {
795 tevent_req_post(req, state->ev);
796 return;
798 tevent_req_set_callback(subreq,
799 smb2_query_directory_waited,
800 req);
801 return;
804 tevent_req_done(req);
805 return;
808 static void smb2_query_directory_waited(struct tevent_req *subreq)
810 struct tevent_req *req = tevent_req_callback_data(
811 subreq, struct tevent_req);
812 bool ok;
814 ok = tevent_wakeup_recv(subreq);
815 TALLOC_FREE(subreq);
816 if (!ok) {
817 tevent_req_oom(req);
818 return;
820 tevent_req_done(req);
823 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
824 TALLOC_CTX *mem_ctx,
825 DATA_BLOB *out_output_buffer)
827 NTSTATUS status;
828 struct smbd_smb2_query_directory_state *state = tevent_req_data(req,
829 struct smbd_smb2_query_directory_state);
831 if (tevent_req_is_nterror(req, &status)) {
832 tevent_req_received(req);
833 return status;
836 *out_output_buffer = state->out_output_buffer;
837 talloc_steal(mem_ctx, out_output_buffer->data);
839 tevent_req_received(req);
840 return NT_STATUS_OK;
843 struct fetch_write_time_state {
844 connection_struct *conn;
845 struct file_id id;
846 int info_level;
847 char *entry_marshall_buf;
850 static void fetch_write_time_done(struct tevent_req *subreq);
852 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
853 struct tevent_context *ev,
854 connection_struct *conn,
855 struct file_id id,
856 int info_level,
857 char *entry_marshall_buf,
858 bool *stop)
860 struct tevent_req *req = NULL;
861 struct fetch_write_time_state *state = NULL;
862 struct tevent_req *subreq = NULL;
863 bool req_queued;
865 *stop = false;
867 req = tevent_req_create(mem_ctx, &state, struct fetch_write_time_state);
868 if (req == NULL) {
869 return NULL;
872 *state = (struct fetch_write_time_state) {
873 .conn = conn,
874 .id = id,
875 .info_level = info_level,
876 .entry_marshall_buf = entry_marshall_buf,
879 subreq = fetch_share_mode_send(state, ev, id, &req_queued);
880 if (tevent_req_nomem(subreq, req)) {
881 return tevent_req_post(req, ev);
883 tevent_req_set_callback(subreq, fetch_write_time_done, req);
885 if (req_queued) {
886 *stop = true;
888 return req;
891 static void fetch_write_time_done(struct tevent_req *subreq)
893 struct tevent_req *req = tevent_req_callback_data(
894 subreq, struct tevent_req);
895 struct fetch_write_time_state *state = tevent_req_data(
896 req, struct fetch_write_time_state);
897 struct timespec write_time;
898 struct share_mode_lock *lck = NULL;
899 NTSTATUS status;
900 size_t off;
902 status = fetch_share_mode_recv(subreq, state, &lck);
903 TALLOC_FREE(subreq);
904 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
905 tevent_req_done(req);
906 return;
908 if (tevent_req_nterror(req, status)) {
909 return;
912 write_time = get_share_mode_write_time(lck);
913 TALLOC_FREE(lck);
915 if (is_omit_timespec(&write_time)) {
916 tevent_req_done(req);
917 return;
920 switch (state->info_level) {
921 case SMB_FIND_FILE_DIRECTORY_INFO:
922 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
923 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
924 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
925 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
926 off = 24;
927 break;
929 default:
930 DBG_ERR("Unsupported info_level [%d]\n", state->info_level);
931 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
932 return;
935 put_long_date_full_timespec(state->conn->ts_res,
936 state->entry_marshall_buf + off,
937 &write_time);
939 tevent_req_done(req);
940 return;
943 static NTSTATUS fetch_write_time_recv(struct tevent_req *req)
945 NTSTATUS status;
947 if (tevent_req_is_nterror(req, &status)) {
948 tevent_req_received(req);
949 return status;
952 tevent_req_received(req);
953 return NT_STATUS_OK;
956 struct fetch_dos_mode_state {
957 struct files_struct *dir_fsp;
958 struct smb_filename *smb_fname;
959 uint32_t info_level;
960 uint8_t *entry_marshall_buf;
963 static void fetch_dos_mode_done(struct tevent_req *subreq);
965 static struct tevent_req *fetch_dos_mode_send(
966 TALLOC_CTX *mem_ctx,
967 struct tevent_context *ev,
968 struct files_struct *dir_fsp,
969 struct smb_filename **smb_fname,
970 uint32_t info_level,
971 uint8_t *entry_marshall_buf)
973 struct tevent_req *req = NULL;
974 struct fetch_dos_mode_state *state = NULL;
975 struct tevent_req *subreq = NULL;
977 req = tevent_req_create(mem_ctx, &state, struct fetch_dos_mode_state);
978 if (req == NULL) {
979 return NULL;
981 *state = (struct fetch_dos_mode_state) {
982 .dir_fsp = dir_fsp,
983 .info_level = info_level,
984 .entry_marshall_buf = entry_marshall_buf,
987 state->smb_fname = talloc_move(state, smb_fname);
989 subreq = dos_mode_at_send(state, ev, dir_fsp, state->smb_fname);
990 if (tevent_req_nomem(subreq, req)) {
991 return tevent_req_post(req, ev);
993 tevent_req_set_callback(subreq, fetch_dos_mode_done, req);
995 return req;
998 static void fetch_dos_mode_done(struct tevent_req *subreq)
1000 struct tevent_req *req =
1001 tevent_req_callback_data(subreq,
1002 struct tevent_req);
1003 struct fetch_dos_mode_state *state =
1004 tevent_req_data(req,
1005 struct fetch_dos_mode_state);
1006 uint32_t dfs_dosmode;
1007 uint32_t dosmode;
1008 struct timespec btime_ts = {0};
1009 off_t dosmode_off;
1010 off_t btime_off;
1011 NTSTATUS status;
1013 status = dos_mode_at_recv(subreq, &dosmode);
1014 TALLOC_FREE(subreq);
1015 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1016 tevent_req_done(req);
1017 return;
1019 if (tevent_req_nterror(req, status)) {
1020 return;
1023 switch (state->info_level) {
1024 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1025 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1026 case SMB_FIND_FILE_DIRECTORY_INFO:
1027 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1028 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1029 btime_off = 8;
1030 dosmode_off = 56;
1031 break;
1033 default:
1034 DBG_ERR("Unsupported info_level [%u]\n", state->info_level);
1035 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
1036 return;
1040 dfs_dosmode = IVAL(state->entry_marshall_buf, dosmode_off);
1041 if (dfs_dosmode == 0) {
1043 * DOS mode for a DFS link, only overwrite if still set to 0 and
1044 * not already populated by the lower layer for a DFS link in
1045 * smbd_dirptr_lanman2_mode_fn().
1047 SIVAL(state->entry_marshall_buf, dosmode_off, dosmode);
1050 btime_ts = get_create_timespec(state->dir_fsp->conn,
1051 NULL,
1052 state->smb_fname);
1053 if (lp_dos_filetime_resolution(SNUM(state->dir_fsp->conn))) {
1054 dos_filetime_timespec(&btime_ts);
1057 put_long_date_full_timespec(state->dir_fsp->conn->ts_res,
1058 (char *)state->entry_marshall_buf + btime_off,
1059 &btime_ts);
1061 tevent_req_done(req);
1062 return;
1065 static NTSTATUS fetch_dos_mode_recv(struct tevent_req *req)
1067 NTSTATUS status;
1069 if (tevent_req_is_nterror(req, &status)) {
1070 tevent_req_received(req);
1071 return status;
1074 tevent_req_received(req);
1075 return NT_STATUS_OK;