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