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