68c00241a5108c79377cdaca027bf66dc22deb4a
[kamenim/samba-autobuild/.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->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 smb_vfs_ev_glue *evg,
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 smb_vfs_ev_glue *evg;
221         struct tevent_context *ev;
222         struct smbd_smb2_request *smb2req;
223         uint64_t async_sharemode_count;
224         uint32_t find_async_delay_usec;
225         DATA_BLOB out_output_buffer;
226         struct smb_request *smbreq;
227         int in_output_buffer_length;
228         struct files_struct *fsp;
229         const char *in_file_name;
230         NTSTATUS empty_status;
231         uint32_t info_level;
232         uint32_t max_count;
233         char *pdata;
234         char *base_data;
235         char *end_data;
236         uint32_t num;
237         uint32_t dirtype;
238         bool dont_descend;
239         bool ask_sharemode;
240         bool async_dosmode;
241         bool async_ask_sharemode;
242         int last_entry_off;
243         struct pthreadpool_tevent *tp_chdir_safe;
244         size_t max_async_dosmode_active;
245         uint32_t async_dosmode_active;
246         bool done;
247 };
248
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);
253
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)
263 {
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         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->evg = conn->user_vfs_evg;
281         state->ev = ev;
282         state->tp_chdir_safe = smb_vfs_ev_glue_tp_chdir_safe(state->evg);
283         state->fsp = fsp;
284         state->smb2req = smb2req;
285         state->in_output_buffer_length = in_output_buffer_length;
286         state->in_file_name = in_file_name;
287         state->out_output_buffer = data_blob_null;
288         state->dirtype = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY;
289
290         DEBUG(10,("smbd_smb2_query_directory_send: %s - %s\n",
291                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
292
293         state->smbreq = smbd_smb2_fake_smb_request(smb2req);
294         if (tevent_req_nomem(state->smbreq, req)) {
295                 return tevent_req_post(req, ev);
296         }
297
298         if (!fsp->is_directory) {
299                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
300                 return tevent_req_post(req, ev);
301         }
302
303         if (strcmp(state->in_file_name, "") == 0) {
304                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
305                 return tevent_req_post(req, ev);
306         }
307         if (strchr_m(state->in_file_name, '\\') != NULL) {
308                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
309                 return tevent_req_post(req, ev);
310         }
311         if (strchr_m(state->in_file_name, '/') != NULL) {
312                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
313                 return tevent_req_post(req, ev);
314         }
315
316         p = strptime(state->in_file_name, GMT_FORMAT, &tm);
317         if ((p != NULL) && (*p =='\0')) {
318                 /*
319                  * Bogus find that asks for a shadow copy timestamp as a
320                  * directory. The correct response is that it does not exist as
321                  * a directory.
322                  */
323                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_FILE);
324                 return tevent_req_post(req, ev);
325         }
326
327         if (in_output_buffer_length > xconn->smb2.server.max_trans) {
328                 DEBUG(2,("smbd_smb2_query_directory_send: "
329                          "client ignored max trans:%s: 0x%08X: 0x%08X\n",
330                          __location__, in_output_buffer_length,
331                          xconn->smb2.server.max_trans));
332                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
333                 return tevent_req_post(req, ev);
334         }
335
336         status = smbd_smb2_request_verify_creditcharge(smb2req,
337                                         in_output_buffer_length);
338
339         if (!NT_STATUS_IS_OK(status)) {
340                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
341                 return tevent_req_post(req, ev);
342         }
343
344         switch (in_file_info_class) {
345         case SMB2_FIND_DIRECTORY_INFO:
346                 state->info_level = SMB_FIND_FILE_DIRECTORY_INFO;
347                 break;
348
349         case SMB2_FIND_FULL_DIRECTORY_INFO:
350                 state->info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
351                 break;
352
353         case SMB2_FIND_BOTH_DIRECTORY_INFO:
354                 state->info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
355                 break;
356
357         case SMB2_FIND_NAME_INFO:
358                 state->info_level = SMB_FIND_FILE_NAMES_INFO;
359                 break;
360
361         case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
362                 state->info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
363                 break;
364
365         case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
366                 state->info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
367                 break;
368
369         default:
370                 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
371                 return tevent_req_post(req, ev);
372         }
373
374         if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
375                 int flags;
376
377                 status = fd_close(fsp);
378                 if (tevent_req_nterror(req, status)) {
379                         return tevent_req_post(req, ev);
380                 }
381
382                 /*
383                  * fd_close() will close and invalidate the fsp's file
384                  * descriptor. So we have to reopen it.
385                  */
386
387                 flags = O_RDONLY;
388 #ifdef O_DIRECTORY
389                 flags |= O_DIRECTORY;
390 #endif
391                 status = fd_open(conn, fsp, flags, 0);
392                 if (tevent_req_nterror(req, status)) {
393                         return tevent_req_post(req, ev);
394                 }
395         }
396
397         if (!state->smbreq->posix_pathnames) {
398                 wcard_has_wild = ms_has_wild(state->in_file_name);
399         }
400
401         /* Ensure we've canonicalized any search path if not a wildcard. */
402         if (!wcard_has_wild) {
403                 struct smb_filename *smb_fname = NULL;
404                 const char *fullpath;
405                 char tmpbuf[PATH_MAX];
406                 char *to_free = NULL;
407                 uint32_t ucf_flags = UCF_SAVE_LCOMP |
408                                      UCF_ALWAYS_ALLOW_WCARD_LCOMP |
409                                      (state->smbreq->posix_pathnames ?
410                                         UCF_POSIX_PATHNAMES : 0);
411
412                 if (ISDOT(fsp->fsp_name->base_name)) {
413                         fullpath = state->in_file_name;
414                 } else {
415                         size_t len;
416                         char *tmp;
417
418                         len = full_path_tos(
419                                 fsp->fsp_name->base_name, state->in_file_name,
420                                 tmpbuf, sizeof(tmpbuf), &tmp, &to_free);
421                         if (len == -1) {
422                                 tevent_req_oom(req);
423                                 return tevent_req_post(req, ev);
424                         }
425                         fullpath = tmp;
426                 }
427                 status = filename_convert(state,
428                                 conn,
429                                 fullpath,
430                                 ucf_flags,
431                                 NULL,
432                                 &wcard_has_wild,
433                                 &smb_fname);
434
435                 TALLOC_FREE(to_free);
436
437                 if (tevent_req_nterror(req, status)) {
438                         return tevent_req_post(req, ev);
439                 }
440
441                 state->in_file_name = smb_fname->original_lcomp;
442         }
443
444         if (fsp->dptr == NULL) {
445                 status = dptr_create(conn,
446                                      NULL, /* req */
447                                      fsp,
448                                      fsp->fsp_name,
449                                      false, /* old_handle */
450                                      false, /* expect_close */
451                                      0, /* spid */
452                                      state->in_file_name, /* wcard */
453                                      wcard_has_wild,
454                                      state->dirtype,
455                                      &fsp->dptr);
456                 if (!NT_STATUS_IS_OK(status)) {
457                         tevent_req_nterror(req, status);
458                         return tevent_req_post(req, ev);
459                 }
460
461                 state->empty_status = NT_STATUS_NO_SUCH_FILE;
462         } else {
463                 state->empty_status = STATUS_NO_MORE_FILES;
464         }
465
466         if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
467                 dptr_SeekDir(fsp->dptr, 0);
468         }
469
470         if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
471                 state->max_count = 1;
472         } else {
473                 state->max_count = UINT16_MAX;
474         }
475
476 #define DIR_ENTRY_SAFETY_MARGIN 4096
477
478         state->out_output_buffer = data_blob_talloc(state, NULL,
479                         in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
480         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
481                 return tevent_req_post(req, ev);
482         }
483
484         state->out_output_buffer.length = 0;
485         state->pdata = (char *)state->out_output_buffer.data;
486         state->base_data = state->pdata;
487         /*
488          * end_data must include the safety margin as it's what is
489          * used to determine if pushed strings have been truncated.
490          */
491         state->end_data = state->pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
492
493         DEBUG(8,("smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
494                 "in_output_buffer_length = %u\n",
495                 fsp->fsp_name->base_name, lp_dont_descend(talloc_tos(), SNUM(conn)),
496                 (unsigned int)in_output_buffer_length ));
497         if (in_list(fsp->fsp_name->base_name,lp_dont_descend(talloc_tos(), SNUM(conn)),
498                         conn->case_sensitive)) {
499                 state->dont_descend = true;
500         }
501
502         /*
503          * SMB_FIND_FILE_NAMES_INFO doesn't need stat information
504          *
505          * This may change when we try to improve the delete on close
506          * handling in future.
507          */
508         if (state->info_level != SMB_FIND_FILE_NAMES_INFO) {
509                 state->ask_sharemode = lp_smbd_search_ask_sharemode(SNUM(conn));
510
511                 state->async_dosmode = lp_smbd_async_dosmode(SNUM(conn));
512         }
513
514         if (state->ask_sharemode && lp_clustering()) {
515                 state->ask_sharemode = false;
516                 state->async_ask_sharemode = true;
517         }
518
519         if (state->async_dosmode) {
520                 size_t max_threads;
521
522                 max_threads = pthreadpool_tevent_max_threads(state->tp_chdir_safe);
523
524                 state->max_async_dosmode_active = lp_smbd_max_async_dosmode(
525                                                         SNUM(conn));
526                 if (state->max_async_dosmode_active == 0) {
527                         state->max_async_dosmode_active = max_threads * 2;
528                 }
529         }
530
531         if (state->async_dosmode || state->async_ask_sharemode) {
532                 /*
533                  * Should we only set async_internal
534                  * if we're not the last request in
535                  * a compound chain?
536                  */
537                 smb2_request_set_async_internal(smb2req, true);
538         }
539
540         /*
541          * This gets set in autobuild for some tests
542          */
543         state->find_async_delay_usec = lp_parm_ulong(SNUM(conn), "smbd",
544                                                      "find async delay usec",
545                                                      0);
546
547         while (!stop) {
548                 stop = smb2_query_directory_next_entry(req);
549         }
550
551         if (!tevent_req_is_in_progress(req)) {
552                 return tevent_req_post(req, ev);
553         }
554
555         ok = aio_add_req_to_fsp(fsp, req);
556         if (!ok) {
557                 DBG_ERR("Could not add req to fsp\n");
558                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
559                 return tevent_req_post(req, ev);
560         }
561
562         return req;
563 }
564
565 static bool smb2_query_directory_next_entry(struct tevent_req *req)
566 {
567         struct smbd_smb2_query_directory_state *state = tevent_req_data(
568                 req, struct smbd_smb2_query_directory_state);
569         struct smb_filename *smb_fname = NULL; /* relative to fsp !! */
570         bool got_exact_match = false;
571         int off = state->out_output_buffer.length;
572         int space_remaining = state->in_output_buffer_length - off;
573         struct file_id file_id;
574         NTSTATUS status;
575         bool get_dosmode = !state->async_dosmode;
576         bool stop = false;
577
578         SMB_ASSERT(space_remaining >= 0);
579
580         status = smbd_dirptr_lanman2_entry(state,
581                                            state->fsp->conn,
582                                            state->fsp->dptr,
583                                            state->smbreq->flags2,
584                                            state->in_file_name,
585                                            state->dirtype,
586                                            state->info_level,
587                                            false, /* requires_resume_key */
588                                            state->dont_descend,
589                                            state->ask_sharemode,
590                                            get_dosmode,
591                                            8, /* align to 8 bytes */
592                                            false, /* no padding */
593                                            &state->pdata,
594                                            state->base_data,
595                                            state->end_data,
596                                            space_remaining,
597                                            &smb_fname,
598                                            &got_exact_match,
599                                            &state->last_entry_off,
600                                            NULL,
601                                            &file_id);
602
603         off = (int)PTR_DIFF(state->pdata, state->base_data);
604
605         if (!NT_STATUS_IS_OK(status)) {
606                 if (NT_STATUS_EQUAL(status, NT_STATUS_ILLEGAL_CHARACTER)) {
607                         /*
608                          * Bad character conversion on name. Ignore this
609                          * entry.
610                          */
611                         return false;
612                 } else if (state->num > 0) {
613                         goto last_entry_done;
614                 } else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
615                         tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
616                         return true;
617                 } else {
618                         tevent_req_nterror(req, state->empty_status);
619                         return true;
620                 }
621         }
622
623         if (state->async_ask_sharemode) {
624                 struct tevent_req *subreq = NULL;
625                 char *buf = state->base_data + state->last_entry_off;
626
627                 subreq = fetch_write_time_send(state,
628                                                state->ev,
629                                                state->fsp->conn,
630                                                file_id,
631                                                state->info_level,
632                                                buf,
633                                                &stop);
634                 if (tevent_req_nomem(subreq, req)) {
635                         return true;
636                 }
637                 tevent_req_set_callback(
638                         subreq,
639                         smb2_query_directory_fetch_write_time_done,
640                         req);
641                 state->async_sharemode_count++;
642         }
643
644         if (state->async_dosmode) {
645                 struct tevent_req *subreq = NULL;
646                 uint8_t *buf = NULL;
647                 size_t outstanding_aio;
648
649                 buf = (uint8_t *)state->base_data + state->last_entry_off;
650
651                 subreq = fetch_dos_mode_send(state,
652                                              state->evg,
653                                              state->fsp,
654                                              &smb_fname,
655                                              state->info_level,
656                                              buf);
657                 if (tevent_req_nomem(subreq, req)) {
658                         return true;
659                 }
660                 tevent_req_set_callback(subreq,
661                                         smb2_query_directory_dos_mode_done,
662                                         req);
663
664                 state->async_dosmode_active++;
665
666                 outstanding_aio = pthreadpool_tevent_queued_jobs(
667                                         state->tp_chdir_safe);
668
669                 if (outstanding_aio > state->max_async_dosmode_active) {
670                         stop = true;
671                 }
672         }
673
674         TALLOC_FREE(smb_fname);
675
676         state->num++;
677         state->out_output_buffer.length = off;
678
679         if (!state->done && state->num < state->max_count) {
680                 return stop;
681         }
682
683 last_entry_done:
684         SIVAL(state->out_output_buffer.data, state->last_entry_off, 0);
685
686         state->done = true;
687
688         if (state->async_sharemode_count > 0) {
689                 DBG_DEBUG("Stopping after %"PRIu64" async mtime "
690                           "updates\n", state->async_sharemode_count);
691                 return true;
692         }
693
694         if (state->async_dosmode_active > 0) {
695                 return true;
696         }
697
698         if (state->find_async_delay_usec > 0) {
699                 struct timeval tv;
700                 struct tevent_req *subreq = NULL;
701
702                 /*
703                  * Should we only set async_internal
704                  * if we're not the last request in
705                  * a compound chain?
706                  */
707                 smb2_request_set_async_internal(state->smb2req, true);
708
709                 tv = timeval_current_ofs(0, state->find_async_delay_usec);
710
711                 subreq = tevent_wakeup_send(state, state->ev, tv);
712                 if (tevent_req_nomem(subreq, req)) {
713                         return true;
714                 }
715                 tevent_req_set_callback(subreq,
716                                         smb2_query_directory_waited,
717                                         req);
718                 return true;
719         }
720
721         tevent_req_done(req);
722         return true;
723 }
724
725 static void smb2_query_directory_check_next_entry(struct tevent_req *req);
726
727 static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq)
728 {
729         struct tevent_req *req = tevent_req_callback_data(
730                 subreq, struct tevent_req);
731         struct smbd_smb2_query_directory_state *state = tevent_req_data(
732                 req, struct smbd_smb2_query_directory_state);
733         NTSTATUS status;
734
735         state->async_sharemode_count--;
736
737         status = fetch_write_time_recv(subreq);
738         TALLOC_FREE(subreq);
739         if (tevent_req_nterror(req, status)) {
740                 return;
741         }
742
743         smb2_query_directory_check_next_entry(req);
744         return;
745 }
746
747 static void smb2_query_directory_dos_mode_done(struct tevent_req *subreq)
748 {
749         struct tevent_req *req =
750                 tevent_req_callback_data(subreq,
751                 struct tevent_req);
752         struct smbd_smb2_query_directory_state *state =
753                 tevent_req_data(req,
754                 struct smbd_smb2_query_directory_state);
755         NTSTATUS status;
756
757         status = fetch_dos_mode_recv(subreq);
758         TALLOC_FREE(subreq);
759         if (tevent_req_nterror(req, status)) {
760                 return;
761         }
762
763         state->async_dosmode_active--;
764
765         smb2_query_directory_check_next_entry(req);
766         return;
767 }
768
769 static void smb2_query_directory_check_next_entry(struct tevent_req *req)
770 {
771         struct smbd_smb2_query_directory_state *state = tevent_req_data(
772                 req, struct smbd_smb2_query_directory_state);
773         bool stop = false;
774
775         if (!state->done) {
776                 while (!stop) {
777                         stop = smb2_query_directory_next_entry(req);
778                 }
779                 return;
780         }
781
782         if (state->async_sharemode_count > 0 ||
783             state->async_dosmode_active > 0)
784         {
785                 return;
786         }
787
788         if (state->find_async_delay_usec > 0) {
789                 struct timeval tv;
790                 struct tevent_req *subreq = NULL;
791
792                 tv = timeval_current_ofs(0, state->find_async_delay_usec);
793
794                 subreq = tevent_wakeup_send(state, state->ev, tv);
795                 if (tevent_req_nomem(subreq, req)) {
796                         tevent_req_post(req, state->ev);
797                         return;
798                 }
799                 tevent_req_set_callback(subreq,
800                                         smb2_query_directory_waited,
801                                         req);
802                 return;
803         }
804
805         tevent_req_done(req);
806         return;
807 }
808
809 static void smb2_query_directory_waited(struct tevent_req *subreq)
810 {
811         struct tevent_req *req = tevent_req_callback_data(
812                 subreq, struct tevent_req);
813         bool ok;
814
815         ok = tevent_wakeup_recv(subreq);
816         TALLOC_FREE(subreq);
817         if (!ok) {
818                 tevent_req_oom(req);
819                 return;
820         }
821         tevent_req_done(req);
822 }
823
824 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
825                                     TALLOC_CTX *mem_ctx,
826                                     DATA_BLOB *out_output_buffer)
827 {
828         NTSTATUS status;
829         struct smbd_smb2_query_directory_state *state = tevent_req_data(req,
830                                              struct smbd_smb2_query_directory_state);
831
832         if (tevent_req_is_nterror(req, &status)) {
833                 tevent_req_received(req);
834                 return status;
835         }
836
837         *out_output_buffer = state->out_output_buffer;
838         talloc_steal(mem_ctx, out_output_buffer->data);
839
840         tevent_req_received(req);
841         return NT_STATUS_OK;
842 }
843
844 struct fetch_write_time_state {
845         connection_struct *conn;
846         struct file_id id;
847         int info_level;
848         char *entry_marshall_buf;
849 };
850
851 static void fetch_write_time_done(struct tevent_req *subreq);
852
853 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
854                                                 struct tevent_context *ev,
855                                                 connection_struct *conn,
856                                                 struct file_id id,
857                                                 int info_level,
858                                                 char *entry_marshall_buf,
859                                                 bool *stop)
860 {
861         struct tevent_req *req = NULL;
862         struct fetch_write_time_state *state = NULL;
863         struct tevent_req *subreq = NULL;
864         bool req_queued;
865
866         *stop = false;
867
868         req = tevent_req_create(mem_ctx, &state, struct fetch_write_time_state);
869         if (req == NULL) {
870                 return NULL;
871         }
872
873         *state = (struct fetch_write_time_state) {
874                 .conn = conn,
875                 .id = id,
876                 .info_level = info_level,
877                 .entry_marshall_buf = entry_marshall_buf,
878         };
879
880         subreq = fetch_share_mode_send(state, ev, id, &req_queued);
881         if (tevent_req_nomem(subreq, req)) {
882                 return tevent_req_post(req, ev);
883         }
884         tevent_req_set_callback(subreq, fetch_write_time_done, req);
885
886         if (req_queued) {
887                 *stop = true;
888         }
889         return req;
890 }
891
892 static void fetch_write_time_done(struct tevent_req *subreq)
893 {
894         struct tevent_req *req = tevent_req_callback_data(
895                 subreq, struct tevent_req);
896         struct fetch_write_time_state *state = tevent_req_data(
897                 req, struct fetch_write_time_state);
898         struct timespec write_time;
899         struct share_mode_lock *lck = NULL;
900         NTSTATUS status;
901         size_t off;
902
903         status = fetch_share_mode_recv(subreq, state, &lck);
904         TALLOC_FREE(subreq);
905         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
906                 tevent_req_done(req);
907                 return;
908         }
909         if (!NT_STATUS_IS_OK(status)) {
910                 tevent_req_nterror(req, status);
911                 return;
912         }
913
914         write_time = get_share_mode_write_time(lck);
915         TALLOC_FREE(lck);
916
917         if (null_timespec(write_time)) {
918                 tevent_req_done(req);
919                 return;
920         }
921
922         switch (state->info_level) {
923         case SMB_FIND_FILE_DIRECTORY_INFO:
924         case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
925         case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
926         case SMB_FIND_ID_FULL_DIRECTORY_INFO:
927         case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
928                 off = 24;
929                 break;
930
931         default:
932                 DBG_ERR("Unsupported info_level [%d]\n", state->info_level);
933                 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
934                 return;
935         }
936
937         put_long_date_timespec(state->conn->ts_res,
938                                state->entry_marshall_buf + off,
939                                write_time);
940
941         tevent_req_done(req);
942         return;
943 }
944
945 static NTSTATUS fetch_write_time_recv(struct tevent_req *req)
946 {
947         NTSTATUS status;
948
949         if (tevent_req_is_nterror(req, &status)) {
950                 tevent_req_received(req);
951                 return status;
952         }
953
954         tevent_req_received(req);
955         return NT_STATUS_OK;
956 }
957
958 struct fetch_dos_mode_state {
959         struct files_struct *dir_fsp;
960         struct smb_filename *smb_fname;
961         uint32_t info_level;
962         uint8_t *entry_marshall_buf;
963 };
964
965 static void fetch_dos_mode_done(struct tevent_req *subreq);
966
967 static struct tevent_req *fetch_dos_mode_send(
968                         TALLOC_CTX *mem_ctx,
969                         struct smb_vfs_ev_glue *evg,
970                         struct files_struct *dir_fsp,
971                         struct smb_filename **smb_fname,
972                         uint32_t info_level,
973                         uint8_t *entry_marshall_buf)
974 {
975         struct tevent_context *ev = smb_vfs_ev_glue_ev_ctx(evg);
976         struct tevent_req *req = NULL;
977         struct fetch_dos_mode_state *state = NULL;
978         struct tevent_req *subreq = NULL;
979
980         req = tevent_req_create(mem_ctx, &state, struct fetch_dos_mode_state);
981         if (req == NULL) {
982                 return NULL;
983         }
984         *state = (struct fetch_dos_mode_state) {
985                 .dir_fsp = dir_fsp,
986                 .info_level = info_level,
987                 .entry_marshall_buf = entry_marshall_buf,
988         };
989
990         state->smb_fname = talloc_move(state, smb_fname);
991
992         subreq = dos_mode_at_send(state, evg, dir_fsp, state->smb_fname);
993         if (tevent_req_nomem(subreq, req)) {
994                 return tevent_req_post(req, ev);
995         }
996         tevent_req_set_callback(subreq, fetch_dos_mode_done, req);
997
998         return req;
999 }
1000
1001 static void fetch_dos_mode_done(struct tevent_req *subreq)
1002 {
1003         struct tevent_req *req =
1004                 tevent_req_callback_data(subreq,
1005                 struct tevent_req);
1006         struct fetch_dos_mode_state *state =
1007                 tevent_req_data(req,
1008                 struct fetch_dos_mode_state);
1009         uint32_t dfs_dosmode;
1010         uint32_t dosmode;
1011         struct timespec btime_ts = {0};
1012         off_t dosmode_off;
1013         off_t btime_off;
1014         NTSTATUS status;
1015
1016         status = dos_mode_at_recv(subreq, &dosmode);
1017         TALLOC_FREE(subreq);
1018         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1019                 tevent_req_done(req);
1020                 return;
1021         }
1022         if (!NT_STATUS_IS_OK(status)) {
1023                 tevent_req_nterror(req, status);
1024                 return;
1025         }
1026
1027         switch (state->info_level) {
1028         case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1029         case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1030         case SMB_FIND_FILE_DIRECTORY_INFO:
1031         case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1032         case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1033                 btime_off = 8;
1034                 dosmode_off = 56;
1035                 break;
1036
1037         default:
1038                 DBG_ERR("Unsupported info_level [%u]\n", state->info_level);
1039                 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
1040                 return;
1041         }
1042
1043
1044         dfs_dosmode = IVAL(state->entry_marshall_buf, dosmode_off);
1045         if (dfs_dosmode == 0) {
1046                 /*
1047                  * DOS mode for a DFS link, only overwrite if still set to 0 and
1048                  * not already populated by the lower layer for a DFS link in
1049                  * smbd_dirptr_lanman2_mode_fn().
1050                  */
1051                 SIVAL(state->entry_marshall_buf, dosmode_off, dosmode);
1052         }
1053
1054         btime_ts = get_create_timespec(state->dir_fsp->conn,
1055                                        NULL,
1056                                        state->smb_fname);
1057         if (lp_dos_filetime_resolution(SNUM(state->dir_fsp->conn))) {
1058                 dos_filetime_timespec(&btime_ts);
1059         }
1060
1061         put_long_date_timespec(state->dir_fsp->conn->ts_res,
1062                                (char *)state->entry_marshall_buf + btime_off,
1063                                btime_ts);
1064
1065         tevent_req_done(req);
1066         return;
1067 }
1068
1069 static NTSTATUS fetch_dos_mode_recv(struct tevent_req *req)
1070 {
1071         NTSTATUS status;
1072
1073         if (tevent_req_is_nterror(req, &status)) {
1074                 tevent_req_received(req);
1075                 return status;
1076         }
1077
1078         tevent_req_received(req);
1079         return NT_STATUS_OK;
1080 }