smbd: remove redundant conn arg from fd_open()
[samba.git] / source3 / smbd / smb2_query_directory.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
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.
11
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.
16
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/>.
19 */
20
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"
27 #include "system/filesys.h"
28 #include "lib/pthreadpool/pthreadpool_tevent.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_SMB2
32
33 static struct tevent_req *smbd_smb2_query_directory_send(TALLOC_CTX *mem_ctx,
34                                               struct tevent_context *ev,
35                                               struct smbd_smb2_request *smb2req,
36                                               struct files_struct *in_fsp,
37                                               uint8_t in_file_info_class,
38                                               uint8_t in_flags,
39                                               uint32_t in_file_index,
40                                               uint32_t in_output_buffer_length,
41                                               const char *in_file_name);
42 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
43                                     TALLOC_CTX *mem_ctx,
44                                     DATA_BLOB *out_output_buffer);
45
46 static void smbd_smb2_request_find_done(struct tevent_req *subreq);
47 NTSTATUS smbd_smb2_request_process_query_directory(struct smbd_smb2_request *req)
48 {
49         NTSTATUS status;
50         const uint8_t *inbody;
51         uint8_t in_file_info_class;
52         uint8_t in_flags;
53         uint32_t in_file_index;
54         uint64_t in_file_id_persistent;
55         uint64_t in_file_id_volatile;
56         struct files_struct *in_fsp;
57         uint16_t in_file_name_offset;
58         uint16_t in_file_name_length;
59         DATA_BLOB in_file_name_buffer;
60         char *in_file_name_string;
61         size_t in_file_name_string_size;
62         uint32_t in_output_buffer_length;
63         struct tevent_req *subreq;
64         bool ok;
65
66         status = smbd_smb2_request_verify_sizes(req, 0x21);
67         if (!NT_STATUS_IS_OK(status)) {
68                 return smbd_smb2_request_error(req, status);
69         }
70         inbody = SMBD_SMB2_IN_BODY_PTR(req);
71
72         in_file_info_class              = CVAL(inbody, 0x02);
73         in_flags                        = CVAL(inbody, 0x03);
74         in_file_index                   = IVAL(inbody, 0x04);
75         in_file_id_persistent           = BVAL(inbody, 0x08);
76         in_file_id_volatile             = BVAL(inbody, 0x10);
77         in_file_name_offset             = SVAL(inbody, 0x18);
78         in_file_name_length             = SVAL(inbody, 0x1A);
79         in_output_buffer_length         = IVAL(inbody, 0x1C);
80
81         if (in_file_name_offset == 0 && in_file_name_length == 0) {
82                 /* This is ok */
83         } else if (in_file_name_offset !=
84                    (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
85                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
86         }
87
88         if (in_file_name_length > SMBD_SMB2_IN_DYN_LEN(req)) {
89                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
90         }
91
92         /* The output header is 8 bytes. */
93         if (in_output_buffer_length <= 8) {
94                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
95         }
96
97         DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
98                 (unsigned int)in_output_buffer_length ));
99
100         /* Take into account the output header. */
101         in_output_buffer_length -= 8;
102
103         in_file_name_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
104         in_file_name_buffer.length = in_file_name_length;
105
106         ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
107                                    in_file_name_buffer.data,
108                                    in_file_name_buffer.length,
109                                    &in_file_name_string,
110                                    &in_file_name_string_size);
111         if (!ok) {
112                 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
113         }
114
115         if (in_file_name_buffer.length == 0) {
116                 in_file_name_string_size = 0;
117         }
118
119         if (strlen(in_file_name_string) != in_file_name_string_size) {
120                 return smbd_smb2_request_error(req, NT_STATUS_OBJECT_NAME_INVALID);
121         }
122
123         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
124         if (in_fsp == NULL) {
125                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
126         }
127
128         subreq = smbd_smb2_query_directory_send(req, req->sconn->ev_ctx,
129                                      req, in_fsp,
130                                      in_file_info_class,
131                                      in_flags,
132                                      in_file_index,
133                                      in_output_buffer_length,
134                                      in_file_name_string);
135         if (subreq == NULL) {
136                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
137         }
138         tevent_req_set_callback(subreq, smbd_smb2_request_find_done, req);
139
140         return smbd_smb2_request_pending_queue(req, subreq, 500);
141 }
142
143 static void smbd_smb2_request_find_done(struct tevent_req *subreq)
144 {
145         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
146                                         struct smbd_smb2_request);
147         DATA_BLOB outbody;
148         DATA_BLOB outdyn;
149         uint16_t out_output_buffer_offset;
150         DATA_BLOB out_output_buffer = data_blob_null;
151         NTSTATUS status;
152         NTSTATUS error; /* transport error */
153
154         status = smbd_smb2_query_directory_recv(subreq,
155                                      req,
156                                      &out_output_buffer);
157         TALLOC_FREE(subreq);
158         if (!NT_STATUS_IS_OK(status)) {
159                 error = smbd_smb2_request_error(req, status);
160                 if (!NT_STATUS_IS_OK(error)) {
161                         smbd_server_connection_terminate(req->xconn,
162                                                          nt_errstr(error));
163                         return;
164                 }
165                 return;
166         }
167
168         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
169
170         outbody = smbd_smb2_generate_outbody(req, 0x08);
171         if (outbody.data == NULL) {
172                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
173                 if (!NT_STATUS_IS_OK(error)) {
174                         smbd_server_connection_terminate(req->xconn,
175                                                          nt_errstr(error));
176                         return;
177                 }
178                 return;
179         }
180
181         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
182         SSVAL(outbody.data, 0x02,
183               out_output_buffer_offset);        /* output buffer offset */
184         SIVAL(outbody.data, 0x04,
185               out_output_buffer.length);        /* output buffer length */
186
187         DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
188                 (unsigned int)out_output_buffer.length ));
189
190         outdyn = out_output_buffer;
191
192         error = smbd_smb2_request_done(req, outbody, &outdyn);
193         if (!NT_STATUS_IS_OK(error)) {
194                 smbd_server_connection_terminate(req->xconn,
195                                                  nt_errstr(error));
196                 return;
197         }
198 }
199
200 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
201                                                 struct tevent_context *ev,
202                                                 connection_struct *conn,
203                                                 struct file_id id,
204                                                 int info_level,
205                                                 char *entry_marshall_buf,
206                                                 bool *stop);
207 static NTSTATUS fetch_write_time_recv(struct tevent_req *req);
208
209 static struct tevent_req *fetch_dos_mode_send(
210         TALLOC_CTX *mem_ctx,
211         struct tevent_context *ev,
212         struct files_struct *dir_fsp,
213         struct smb_filename **smb_fname,
214         uint32_t info_level,
215         uint8_t *entry_marshall_buf);
216
217 static NTSTATUS fetch_dos_mode_recv(struct tevent_req *req);
218
219 struct smbd_smb2_query_directory_state {
220         struct tevent_context *ev;
221         struct smbd_smb2_request *smb2req;
222         uint64_t async_sharemode_count;
223         uint32_t find_async_delay_usec;
224         DATA_BLOB out_output_buffer;
225         struct smb_request *smbreq;
226         int in_output_buffer_length;
227         struct files_struct *fsp;
228         const char *in_file_name;
229         NTSTATUS empty_status;
230         uint32_t info_level;
231         uint32_t max_count;
232         char *pdata;
233         char *base_data;
234         char *end_data;
235         uint32_t num;
236         uint32_t dirtype;
237         bool dont_descend;
238         bool ask_sharemode;
239         bool async_dosmode;
240         bool async_ask_sharemode;
241         int last_entry_off;
242         size_t max_async_dosmode_active;
243         uint32_t async_dosmode_active;
244         bool done;
245 };
246
247 static bool smb2_query_directory_next_entry(struct tevent_req *req);
248 static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq);
249 static void smb2_query_directory_dos_mode_done(struct tevent_req *subreq);
250 static void smb2_query_directory_waited(struct tevent_req *subreq);
251
252 static struct tevent_req *smbd_smb2_query_directory_send(TALLOC_CTX *mem_ctx,
253                                               struct tevent_context *ev,
254                                               struct smbd_smb2_request *smb2req,
255                                               struct files_struct *fsp,
256                                               uint8_t in_file_info_class,
257                                               uint8_t in_flags,
258                                               uint32_t in_file_index,
259                                               uint32_t in_output_buffer_length,
260                                               const char *in_file_name)
261 {
262         struct smbXsrv_connection *xconn = smb2req->xconn;
263         struct tevent_req *req;
264         struct smbd_smb2_query_directory_state *state;
265         connection_struct *conn = smb2req->tcon->compat;
266         const struct loadparm_substitution *lp_sub =
267                 loadparm_s3_global_substitution();
268         NTSTATUS status;
269         bool wcard_has_wild = false;
270         struct tm tm;
271         char *p;
272         bool stop = false;
273         bool ok;
274
275         req = tevent_req_create(mem_ctx, &state,
276                                 struct smbd_smb2_query_directory_state);
277         if (req == NULL) {
278                 return NULL;
279         }
280         state->ev = ev;
281         state->fsp = fsp;
282         state->smb2req = smb2req;
283         state->in_output_buffer_length = in_output_buffer_length;
284         state->in_file_name = in_file_name;
285         state->out_output_buffer = data_blob_null;
286         state->dirtype = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY;
287
288         DEBUG(10,("smbd_smb2_query_directory_send: %s - %s\n",
289                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
290
291         state->smbreq = smbd_smb2_fake_smb_request(smb2req);
292         if (tevent_req_nomem(state->smbreq, req)) {
293                 return tevent_req_post(req, ev);
294         }
295
296         if (!fsp->fsp_flags.is_directory) {
297                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
298                 return tevent_req_post(req, ev);
299         }
300
301         if (strcmp(state->in_file_name, "") == 0) {
302                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
303                 return tevent_req_post(req, ev);
304         }
305         if (strchr_m(state->in_file_name, '\\') != NULL) {
306                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
307                 return tevent_req_post(req, ev);
308         }
309         if (strchr_m(state->in_file_name, '/') != NULL) {
310                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
311                 return tevent_req_post(req, ev);
312         }
313
314         p = strptime(state->in_file_name, GMT_FORMAT, &tm);
315         if ((p != NULL) && (*p =='\0')) {
316                 /*
317                  * Bogus find that asks for a shadow copy timestamp as a
318                  * directory. The correct response is that it does not exist as
319                  * a directory.
320                  */
321                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_FILE);
322                 return tevent_req_post(req, ev);
323         }
324
325         if (in_output_buffer_length > xconn->smb2.server.max_trans) {
326                 DEBUG(2,("smbd_smb2_query_directory_send: "
327                          "client ignored max trans:%s: 0x%08X: 0x%08X\n",
328                          __location__, in_output_buffer_length,
329                          xconn->smb2.server.max_trans));
330                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
331                 return tevent_req_post(req, ev);
332         }
333
334         status = smbd_smb2_request_verify_creditcharge(smb2req,
335                                         in_output_buffer_length);
336
337         if (!NT_STATUS_IS_OK(status)) {
338                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
339                 return tevent_req_post(req, ev);
340         }
341
342         switch (in_file_info_class) {
343         case SMB2_FIND_DIRECTORY_INFO:
344                 state->info_level = SMB_FIND_FILE_DIRECTORY_INFO;
345                 break;
346
347         case SMB2_FIND_FULL_DIRECTORY_INFO:
348                 state->info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
349                 break;
350
351         case SMB2_FIND_BOTH_DIRECTORY_INFO:
352                 state->info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
353                 break;
354
355         case SMB2_FIND_NAME_INFO:
356                 state->info_level = SMB_FIND_FILE_NAMES_INFO;
357                 break;
358
359         case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
360                 state->info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
361                 break;
362
363         case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
364                 state->info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
365                 break;
366
367         default:
368                 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
369                 return tevent_req_post(req, ev);
370         }
371
372         if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
373                 int flags;
374
375                 status = fd_close(fsp);
376                 if (tevent_req_nterror(req, status)) {
377                         return tevent_req_post(req, ev);
378                 }
379
380                 /*
381                  * fd_close() will close and invalidate the fsp's file
382                  * descriptor. So we have to reopen it.
383                  */
384
385                 flags = O_RDONLY;
386 #ifdef O_DIRECTORY
387                 flags |= O_DIRECTORY;
388 #endif
389                 status = fd_open(fsp, flags, 0);
390                 if (tevent_req_nterror(req, status)) {
391                         return tevent_req_post(req, ev);
392                 }
393         }
394
395         if (!state->smbreq->posix_pathnames) {
396                 wcard_has_wild = ms_has_wild(state->in_file_name);
397         }
398
399         /* Ensure we've canonicalized any search path if not a wildcard. */
400         if (!wcard_has_wild) {
401                 struct smb_filename *smb_fname = NULL;
402                 const char *fullpath;
403                 char tmpbuf[PATH_MAX];
404                 char *to_free = NULL;
405                 uint32_t ucf_flags = UCF_ALWAYS_ALLOW_WCARD_LCOMP |
406                                      (state->smbreq->posix_pathnames ?
407                                         UCF_POSIX_PATHNAMES : 0);
408
409                 if (ISDOT(fsp->fsp_name->base_name)) {
410                         fullpath = state->in_file_name;
411                 } else {
412                         size_t len;
413                         char *tmp;
414
415                         len = full_path_tos(
416                                 fsp->fsp_name->base_name, state->in_file_name,
417                                 tmpbuf, sizeof(tmpbuf), &tmp, &to_free);
418                         if (len == -1) {
419                                 tevent_req_oom(req);
420                                 return tevent_req_post(req, ev);
421                         }
422                         fullpath = tmp;
423                 }
424                 status = filename_convert(state,
425                                 conn,
426                                 fullpath,
427                                 ucf_flags,
428                                 0,
429                                 &wcard_has_wild,
430                                 &smb_fname);
431
432                 TALLOC_FREE(to_free);
433
434                 if (tevent_req_nterror(req, status)) {
435                         return tevent_req_post(req, ev);
436                 }
437
438                 /*
439                  * We still need to do the case processing
440                  * to save off the client-supplied last component.
441                  * At least we know there's no @GMT normalization
442                  * or MS-DFS paths to do in a directory mask.
443                  */
444                 state->in_file_name = get_original_lcomp(state,
445                                                 conn,
446                                                 state->in_file_name,
447                                                 0);
448                 if (state->in_file_name == NULL) {
449                         tevent_req_oom(req);
450                         return tevent_req_post(req, ev);
451                 }
452         }
453
454         if (fsp->dptr == NULL) {
455                 status = dptr_create(conn,
456                                      NULL, /* req */
457                                      fsp,
458                                      false, /* old_handle */
459                                      false, /* expect_close */
460                                      0, /* spid */
461                                      state->in_file_name, /* wcard */
462                                      wcard_has_wild,
463                                      state->dirtype,
464                                      &fsp->dptr);
465                 if (!NT_STATUS_IS_OK(status)) {
466                         tevent_req_nterror(req, status);
467                         return tevent_req_post(req, ev);
468                 }
469
470                 state->empty_status = NT_STATUS_NO_SUCH_FILE;
471         } else {
472                 state->empty_status = STATUS_NO_MORE_FILES;
473         }
474
475         if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
476                 dptr_SeekDir(fsp->dptr, 0);
477         }
478
479         if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
480                 state->max_count = 1;
481         } else {
482                 state->max_count = UINT16_MAX;
483         }
484
485 #define DIR_ENTRY_SAFETY_MARGIN 4096
486
487         state->out_output_buffer = data_blob_talloc(state, NULL,
488                         in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
489         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
490                 return tevent_req_post(req, ev);
491         }
492
493         state->out_output_buffer.length = 0;
494         state->pdata = (char *)state->out_output_buffer.data;
495         state->base_data = state->pdata;
496         /*
497          * end_data must include the safety margin as it's what is
498          * used to determine if pushed strings have been truncated.
499          */
500         state->end_data = state->pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
501
502         DEBUG(8,("smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
503                 "in_output_buffer_length = %u\n",
504                  fsp->fsp_name->base_name, lp_dont_descend(talloc_tos(), lp_sub, SNUM(conn)),
505                 (unsigned int)in_output_buffer_length ));
506         if (in_list(fsp->fsp_name->base_name,lp_dont_descend(talloc_tos(), lp_sub, SNUM(conn)),
507                         conn->case_sensitive)) {
508                 state->dont_descend = true;
509         }
510
511         /*
512          * SMB_FIND_FILE_NAMES_INFO doesn't need stat information
513          *
514          * This may change when we try to improve the delete on close
515          * handling in future.
516          */
517         if (state->info_level != SMB_FIND_FILE_NAMES_INFO) {
518                 state->ask_sharemode = lp_smbd_search_ask_sharemode(SNUM(conn));
519
520                 state->async_dosmode = lp_smbd_async_dosmode(SNUM(conn));
521         }
522
523         if (state->ask_sharemode && lp_clustering()) {
524                 state->ask_sharemode = false;
525                 state->async_ask_sharemode = true;
526         }
527
528         if (state->async_dosmode) {
529                 size_t max_threads;
530
531                 max_threads = pthreadpool_tevent_max_threads(conn->sconn->pool);
532                 if (max_threads == 0 || !per_thread_cwd_supported()) {
533                         state->async_dosmode = false;
534                 }
535
536                 state->max_async_dosmode_active = lp_smbd_max_async_dosmode(
537                                                         SNUM(conn));
538                 if (state->max_async_dosmode_active == 0) {
539                         state->max_async_dosmode_active = max_threads * 2;
540                 }
541         }
542
543         if (state->async_dosmode || state->async_ask_sharemode) {
544                 /*
545                  * Should we only set async_internal
546                  * if we're not the last request in
547                  * a compound chain?
548                  */
549                 smb2_request_set_async_internal(smb2req, true);
550         }
551
552         /*
553          * This gets set in autobuild for some tests
554          */
555         state->find_async_delay_usec = lp_parm_ulong(SNUM(conn), "smbd",
556                                                      "find async delay usec",
557                                                      0);
558
559         while (!stop) {
560                 stop = smb2_query_directory_next_entry(req);
561         }
562
563         if (!tevent_req_is_in_progress(req)) {
564                 return tevent_req_post(req, ev);
565         }
566
567         ok = aio_add_req_to_fsp(fsp, req);
568         if (!ok) {
569                 DBG_ERR("Could not add req to fsp\n");
570                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
571                 return tevent_req_post(req, ev);
572         }
573
574         return req;
575 }
576
577 static bool smb2_query_directory_next_entry(struct tevent_req *req)
578 {
579         struct smbd_smb2_query_directory_state *state = tevent_req_data(
580                 req, struct smbd_smb2_query_directory_state);
581         struct smb_filename *smb_fname = NULL; /* relative to fsp !! */
582         bool got_exact_match = false;
583         int off = state->out_output_buffer.length;
584         int space_remaining = state->in_output_buffer_length - off;
585         struct file_id file_id;
586         NTSTATUS status;
587         bool get_dosmode = !state->async_dosmode;
588         bool stop = false;
589
590         SMB_ASSERT(space_remaining >= 0);
591
592         status = smbd_dirptr_lanman2_entry(state,
593                                            state->fsp->conn,
594                                            state->fsp->dptr,
595                                            state->smbreq->flags2,
596                                            state->in_file_name,
597                                            state->dirtype,
598                                            state->info_level,
599                                            false, /* requires_resume_key */
600                                            state->dont_descend,
601                                            state->ask_sharemode,
602                                            get_dosmode,
603                                            8, /* align to 8 bytes */
604                                            false, /* no padding */
605                                            &state->pdata,
606                                            state->base_data,
607                                            state->end_data,
608                                            space_remaining,
609                                            &smb_fname,
610                                            &got_exact_match,
611                                            &state->last_entry_off,
612                                            NULL,
613                                            &file_id);
614
615         off = (int)PTR_DIFF(state->pdata, state->base_data);
616
617         if (!NT_STATUS_IS_OK(status)) {
618                 if (NT_STATUS_EQUAL(status, NT_STATUS_ILLEGAL_CHARACTER)) {
619                         /*
620                          * Bad character conversion on name. Ignore this
621                          * entry.
622                          */
623                         return false;
624                 } else if (state->num > 0) {
625                         goto last_entry_done;
626                 } else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
627                         tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
628                         return true;
629                 } else {
630                         tevent_req_nterror(req, state->empty_status);
631                         return true;
632                 }
633         }
634
635         if (state->async_ask_sharemode &&
636             !S_ISDIR(smb_fname->st.st_ex_mode))
637         {
638                 struct tevent_req *subreq = NULL;
639                 char *buf = state->base_data + state->last_entry_off;
640
641                 subreq = fetch_write_time_send(state,
642                                                state->ev,
643                                                state->fsp->conn,
644                                                file_id,
645                                                state->info_level,
646                                                buf,
647                                                &stop);
648                 if (tevent_req_nomem(subreq, req)) {
649                         return true;
650                 }
651                 tevent_req_set_callback(
652                         subreq,
653                         smb2_query_directory_fetch_write_time_done,
654                         req);
655                 state->async_sharemode_count++;
656         }
657
658         if (state->async_dosmode) {
659                 struct tevent_req *subreq = NULL;
660                 uint8_t *buf = NULL;
661                 size_t outstanding_aio;
662
663                 buf = (uint8_t *)state->base_data + state->last_entry_off;
664
665                 subreq = fetch_dos_mode_send(state,
666                                              state->ev,
667                                              state->fsp,
668                                              &smb_fname,
669                                              state->info_level,
670                                              buf);
671                 if (tevent_req_nomem(subreq, req)) {
672                         return true;
673                 }
674                 tevent_req_set_callback(subreq,
675                                         smb2_query_directory_dos_mode_done,
676                                         req);
677
678                 state->async_dosmode_active++;
679
680                 outstanding_aio = pthreadpool_tevent_queued_jobs(
681                                         state->fsp->conn->sconn->pool);
682
683                 if (outstanding_aio > state->max_async_dosmode_active) {
684                         stop = true;
685                 }
686         }
687
688         TALLOC_FREE(smb_fname);
689
690         state->num++;
691         state->out_output_buffer.length = off;
692
693         if (!state->done && state->num < state->max_count) {
694                 return stop;
695         }
696
697 last_entry_done:
698         SIVAL(state->out_output_buffer.data, state->last_entry_off, 0);
699
700         state->done = true;
701
702         if (state->async_sharemode_count > 0) {
703                 DBG_DEBUG("Stopping after %"PRIu64" async mtime "
704                           "updates\n", state->async_sharemode_count);
705                 return true;
706         }
707
708         if (state->async_dosmode_active > 0) {
709                 return true;
710         }
711
712         if (state->find_async_delay_usec > 0) {
713                 struct timeval tv;
714                 struct tevent_req *subreq = NULL;
715
716                 /*
717                  * Should we only set async_internal
718                  * if we're not the last request in
719                  * a compound chain?
720                  */
721                 smb2_request_set_async_internal(state->smb2req, true);
722
723                 tv = timeval_current_ofs(0, state->find_async_delay_usec);
724
725                 subreq = tevent_wakeup_send(state, state->ev, tv);
726                 if (tevent_req_nomem(subreq, req)) {
727                         return true;
728                 }
729                 tevent_req_set_callback(subreq,
730                                         smb2_query_directory_waited,
731                                         req);
732                 return true;
733         }
734
735         tevent_req_done(req);
736         return true;
737 }
738
739 static void smb2_query_directory_check_next_entry(struct tevent_req *req);
740
741 static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq)
742 {
743         struct tevent_req *req = tevent_req_callback_data(
744                 subreq, struct tevent_req);
745         struct smbd_smb2_query_directory_state *state = tevent_req_data(
746                 req, struct smbd_smb2_query_directory_state);
747         NTSTATUS status;
748         bool ok;
749
750         /*
751          * Make sure we run as the user again
752          */
753         ok = change_to_user_and_service_by_fsp(state->fsp);
754         SMB_ASSERT(ok);
755
756         state->async_sharemode_count--;
757
758         status = fetch_write_time_recv(subreq);
759         TALLOC_FREE(subreq);
760         if (tevent_req_nterror(req, status)) {
761                 return;
762         }
763
764         smb2_query_directory_check_next_entry(req);
765         return;
766 }
767
768 static void smb2_query_directory_dos_mode_done(struct tevent_req *subreq)
769 {
770         struct tevent_req *req =
771                 tevent_req_callback_data(subreq,
772                 struct tevent_req);
773         struct smbd_smb2_query_directory_state *state =
774                 tevent_req_data(req,
775                 struct smbd_smb2_query_directory_state);
776         NTSTATUS status;
777         bool ok;
778
779         /*
780          * Make sure we run as the user again
781          */
782         ok = change_to_user_and_service_by_fsp(state->fsp);
783         SMB_ASSERT(ok);
784
785         status = fetch_dos_mode_recv(subreq);
786         TALLOC_FREE(subreq);
787         if (tevent_req_nterror(req, status)) {
788                 return;
789         }
790
791         state->async_dosmode_active--;
792
793         smb2_query_directory_check_next_entry(req);
794         return;
795 }
796
797 static void smb2_query_directory_check_next_entry(struct tevent_req *req)
798 {
799         struct smbd_smb2_query_directory_state *state = tevent_req_data(
800                 req, struct smbd_smb2_query_directory_state);
801         bool stop = false;
802
803         if (!state->done) {
804                 while (!stop) {
805                         stop = smb2_query_directory_next_entry(req);
806                 }
807                 return;
808         }
809
810         if (state->async_sharemode_count > 0 ||
811             state->async_dosmode_active > 0)
812         {
813                 return;
814         }
815
816         if (state->find_async_delay_usec > 0) {
817                 struct timeval tv;
818                 struct tevent_req *subreq = NULL;
819
820                 tv = timeval_current_ofs(0, state->find_async_delay_usec);
821
822                 subreq = tevent_wakeup_send(state, state->ev, tv);
823                 if (tevent_req_nomem(subreq, req)) {
824                         tevent_req_post(req, state->ev);
825                         return;
826                 }
827                 tevent_req_set_callback(subreq,
828                                         smb2_query_directory_waited,
829                                         req);
830                 return;
831         }
832
833         tevent_req_done(req);
834         return;
835 }
836
837 static void smb2_query_directory_waited(struct tevent_req *subreq)
838 {
839         struct tevent_req *req = tevent_req_callback_data(
840                 subreq, struct tevent_req);
841         bool ok;
842
843         ok = tevent_wakeup_recv(subreq);
844         TALLOC_FREE(subreq);
845         if (!ok) {
846                 tevent_req_oom(req);
847                 return;
848         }
849         tevent_req_done(req);
850 }
851
852 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
853                                     TALLOC_CTX *mem_ctx,
854                                     DATA_BLOB *out_output_buffer)
855 {
856         NTSTATUS status;
857         struct smbd_smb2_query_directory_state *state = tevent_req_data(req,
858                                              struct smbd_smb2_query_directory_state);
859
860         if (tevent_req_is_nterror(req, &status)) {
861                 tevent_req_received(req);
862                 return status;
863         }
864
865         *out_output_buffer = state->out_output_buffer;
866         talloc_steal(mem_ctx, out_output_buffer->data);
867
868         tevent_req_received(req);
869         return NT_STATUS_OK;
870 }
871
872 struct fetch_write_time_state {
873         connection_struct *conn;
874         struct file_id id;
875         int info_level;
876         char *entry_marshall_buf;
877 };
878
879 static void fetch_write_time_done(struct tevent_req *subreq);
880
881 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
882                                                 struct tevent_context *ev,
883                                                 connection_struct *conn,
884                                                 struct file_id id,
885                                                 int info_level,
886                                                 char *entry_marshall_buf,
887                                                 bool *stop)
888 {
889         struct tevent_req *req = NULL;
890         struct fetch_write_time_state *state = NULL;
891         struct tevent_req *subreq = NULL;
892         bool req_queued;
893
894         *stop = false;
895
896         req = tevent_req_create(mem_ctx, &state, struct fetch_write_time_state);
897         if (req == NULL) {
898                 return NULL;
899         }
900
901         *state = (struct fetch_write_time_state) {
902                 .conn = conn,
903                 .id = id,
904                 .info_level = info_level,
905                 .entry_marshall_buf = entry_marshall_buf,
906         };
907
908         subreq = fetch_share_mode_send(state, ev, id, &req_queued);
909         if (tevent_req_nomem(subreq, req)) {
910                 return tevent_req_post(req, ev);
911         }
912         tevent_req_set_callback(subreq, fetch_write_time_done, req);
913
914         if (req_queued) {
915                 *stop = true;
916         }
917         return req;
918 }
919
920 static void fetch_write_time_done(struct tevent_req *subreq)
921 {
922         struct tevent_req *req = tevent_req_callback_data(
923                 subreq, struct tevent_req);
924         struct fetch_write_time_state *state = tevent_req_data(
925                 req, struct fetch_write_time_state);
926         struct timespec write_time;
927         struct share_mode_lock *lck = NULL;
928         NTSTATUS status;
929         size_t off;
930
931         status = fetch_share_mode_recv(subreq, state, &lck);
932         TALLOC_FREE(subreq);
933         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
934                 tevent_req_done(req);
935                 return;
936         }
937         if (!NT_STATUS_IS_OK(status)) {
938                 tevent_req_nterror(req, status);
939                 return;
940         }
941
942         write_time = get_share_mode_write_time(lck);
943         TALLOC_FREE(lck);
944
945         if (is_omit_timespec(&write_time)) {
946                 tevent_req_done(req);
947                 return;
948         }
949
950         switch (state->info_level) {
951         case SMB_FIND_FILE_DIRECTORY_INFO:
952         case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
953         case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
954         case SMB_FIND_ID_FULL_DIRECTORY_INFO:
955         case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
956                 off = 24;
957                 break;
958
959         default:
960                 DBG_ERR("Unsupported info_level [%d]\n", state->info_level);
961                 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
962                 return;
963         }
964
965         put_long_date_full_timespec(state->conn->ts_res,
966                                state->entry_marshall_buf + off,
967                                &write_time);
968
969         tevent_req_done(req);
970         return;
971 }
972
973 static NTSTATUS fetch_write_time_recv(struct tevent_req *req)
974 {
975         NTSTATUS status;
976
977         if (tevent_req_is_nterror(req, &status)) {
978                 tevent_req_received(req);
979                 return status;
980         }
981
982         tevent_req_received(req);
983         return NT_STATUS_OK;
984 }
985
986 struct fetch_dos_mode_state {
987         struct files_struct *dir_fsp;
988         struct smb_filename *smb_fname;
989         uint32_t info_level;
990         uint8_t *entry_marshall_buf;
991 };
992
993 static void fetch_dos_mode_done(struct tevent_req *subreq);
994
995 static struct tevent_req *fetch_dos_mode_send(
996                         TALLOC_CTX *mem_ctx,
997                         struct tevent_context *ev,
998                         struct files_struct *dir_fsp,
999                         struct smb_filename **smb_fname,
1000                         uint32_t info_level,
1001                         uint8_t *entry_marshall_buf)
1002 {
1003         struct tevent_req *req = NULL;
1004         struct fetch_dos_mode_state *state = NULL;
1005         struct tevent_req *subreq = NULL;
1006
1007         req = tevent_req_create(mem_ctx, &state, struct fetch_dos_mode_state);
1008         if (req == NULL) {
1009                 return NULL;
1010         }
1011         *state = (struct fetch_dos_mode_state) {
1012                 .dir_fsp = dir_fsp,
1013                 .info_level = info_level,
1014                 .entry_marshall_buf = entry_marshall_buf,
1015         };
1016
1017         state->smb_fname = talloc_move(state, smb_fname);
1018
1019         subreq = dos_mode_at_send(state, ev, dir_fsp, state->smb_fname);
1020         if (tevent_req_nomem(subreq, req)) {
1021                 return tevent_req_post(req, ev);
1022         }
1023         tevent_req_set_callback(subreq, fetch_dos_mode_done, req);
1024
1025         return req;
1026 }
1027
1028 static void fetch_dos_mode_done(struct tevent_req *subreq)
1029 {
1030         struct tevent_req *req =
1031                 tevent_req_callback_data(subreq,
1032                 struct tevent_req);
1033         struct fetch_dos_mode_state *state =
1034                 tevent_req_data(req,
1035                 struct fetch_dos_mode_state);
1036         uint32_t dfs_dosmode;
1037         uint32_t dosmode;
1038         struct timespec btime_ts = {0};
1039         bool need_file_id = false;
1040         uint64_t file_id;
1041         off_t dosmode_off;
1042         off_t btime_off;
1043         off_t file_id_off;
1044         NTSTATUS status;
1045
1046         status = dos_mode_at_recv(subreq, &dosmode);
1047         TALLOC_FREE(subreq);
1048         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1049                 tevent_req_done(req);
1050                 return;
1051         }
1052         if (!NT_STATUS_IS_OK(status)) {
1053                 tevent_req_nterror(req, status);
1054                 return;
1055         }
1056
1057         switch (state->info_level) {
1058         case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1059         case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1060         case SMB_FIND_FILE_DIRECTORY_INFO:
1061         case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1062         case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1063                 btime_off = 8;
1064                 dosmode_off = 56;
1065                 break;
1066
1067         default:
1068                 DBG_ERR("Unsupported info_level [%u]\n", state->info_level);
1069                 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
1070                 return;
1071         }
1072
1073
1074         dfs_dosmode = IVAL(state->entry_marshall_buf, dosmode_off);
1075         if (dfs_dosmode == 0) {
1076                 /*
1077                  * DOS mode for a DFS link, only overwrite if still set to 0 and
1078                  * not already populated by the lower layer for a DFS link in
1079                  * smbd_dirptr_lanman2_mode_fn().
1080                  */
1081                 SIVAL(state->entry_marshall_buf, dosmode_off, dosmode);
1082         }
1083
1084         btime_ts = get_create_timespec(state->dir_fsp->conn,
1085                                        NULL,
1086                                        state->smb_fname);
1087         if (lp_dos_filetime_resolution(SNUM(state->dir_fsp->conn))) {
1088                 dos_filetime_timespec(&btime_ts);
1089         }
1090
1091         put_long_date_full_timespec(state->dir_fsp->conn->ts_res,
1092                                (char *)state->entry_marshall_buf + btime_off,
1093                                &btime_ts);
1094
1095         switch (state->info_level) {
1096         case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1097                 file_id_off = 96;
1098                 need_file_id = true;
1099                 break;
1100         case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1101                 file_id_off = 72;
1102                 need_file_id = true;
1103                 break;
1104         default:
1105                 break;
1106         }
1107
1108         if (need_file_id) {
1109                 /*
1110                  * File-ID might have been updated from calculated (based on
1111                  * inode) to storage based, fetch via DOS attributes in
1112                  * vfs_default.
1113                  */
1114                 file_id = SMB_VFS_FS_FILE_ID(state->dir_fsp->conn,
1115                                              &state->smb_fname->st);
1116                 SBVAL(state->entry_marshall_buf, file_id_off, file_id);
1117         }
1118
1119         tevent_req_done(req);
1120         return;
1121 }
1122
1123 static NTSTATUS fetch_dos_mode_recv(struct tevent_req *req)
1124 {
1125         NTSTATUS status;
1126
1127         if (tevent_req_is_nterror(req, &status)) {
1128                 tevent_req_received(req);
1129                 return status;
1130         }
1131
1132         tevent_req_received(req);
1133         return NT_STATUS_OK;
1134 }