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