s3: debug: smb2: Create a new DBGC_SMB2 debug class and mark all smbd/smb2_*.c files...
[metze/samba/wip.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
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_SMB2
31
32 static struct tevent_req *smbd_smb2_query_directory_send(TALLOC_CTX *mem_ctx,
33                                               struct tevent_context *ev,
34                                               struct smbd_smb2_request *smb2req,
35                                               struct files_struct *in_fsp,
36                                               uint8_t in_file_info_class,
37                                               uint8_t in_flags,
38                                               uint32_t in_file_index,
39                                               uint32_t in_output_buffer_length,
40                                               const char *in_file_name);
41 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
42                                     TALLOC_CTX *mem_ctx,
43                                     DATA_BLOB *out_output_buffer);
44
45 static void smbd_smb2_request_find_done(struct tevent_req *subreq);
46 NTSTATUS smbd_smb2_request_process_query_directory(struct smbd_smb2_request *req)
47 {
48         NTSTATUS status;
49         const uint8_t *inbody;
50         uint8_t in_file_info_class;
51         uint8_t in_flags;
52         uint32_t in_file_index;
53         uint64_t in_file_id_persistent;
54         uint64_t in_file_id_volatile;
55         struct files_struct *in_fsp;
56         uint16_t in_file_name_offset;
57         uint16_t in_file_name_length;
58         DATA_BLOB in_file_name_buffer;
59         char *in_file_name_string;
60         size_t in_file_name_string_size;
61         uint32_t in_output_buffer_length;
62         struct tevent_req *subreq;
63         bool ok;
64
65         status = smbd_smb2_request_verify_sizes(req, 0x21);
66         if (!NT_STATUS_IS_OK(status)) {
67                 return smbd_smb2_request_error(req, status);
68         }
69         inbody = SMBD_SMB2_IN_BODY_PTR(req);
70
71         in_file_info_class              = CVAL(inbody, 0x02);
72         in_flags                        = CVAL(inbody, 0x03);
73         in_file_index                   = IVAL(inbody, 0x04);
74         in_file_id_persistent           = BVAL(inbody, 0x08);
75         in_file_id_volatile             = BVAL(inbody, 0x10);
76         in_file_name_offset             = SVAL(inbody, 0x18);
77         in_file_name_length             = SVAL(inbody, 0x1A);
78         in_output_buffer_length         = IVAL(inbody, 0x1C);
79
80         if (in_file_name_offset == 0 && in_file_name_length == 0) {
81                 /* This is ok */
82         } else if (in_file_name_offset !=
83                    (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
84                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
85         }
86
87         if (in_file_name_length > SMBD_SMB2_IN_DYN_LEN(req)) {
88                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
89         }
90
91         /* The output header is 8 bytes. */
92         if (in_output_buffer_length <= 8) {
93                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
94         }
95
96         DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
97                 (unsigned int)in_output_buffer_length ));
98
99         /* Take into account the output header. */
100         in_output_buffer_length -= 8;
101
102         in_file_name_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
103         in_file_name_buffer.length = in_file_name_length;
104
105         ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
106                                    in_file_name_buffer.data,
107                                    in_file_name_buffer.length,
108                                    &in_file_name_string,
109                                    &in_file_name_string_size);
110         if (!ok) {
111                 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
112         }
113
114         if (in_file_name_buffer.length == 0) {
115                 in_file_name_string_size = 0;
116         }
117
118         if (strlen(in_file_name_string) != in_file_name_string_size) {
119                 return smbd_smb2_request_error(req, NT_STATUS_OBJECT_NAME_INVALID);
120         }
121
122         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
123         if (in_fsp == NULL) {
124                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
125         }
126
127         subreq = smbd_smb2_query_directory_send(req, req->sconn->ev_ctx,
128                                      req, in_fsp,
129                                      in_file_info_class,
130                                      in_flags,
131                                      in_file_index,
132                                      in_output_buffer_length,
133                                      in_file_name_string);
134         if (subreq == NULL) {
135                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
136         }
137         tevent_req_set_callback(subreq, smbd_smb2_request_find_done, req);
138
139         return smbd_smb2_request_pending_queue(req, subreq, 500);
140 }
141
142 static void smbd_smb2_request_find_done(struct tevent_req *subreq)
143 {
144         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
145                                         struct smbd_smb2_request);
146         DATA_BLOB outbody;
147         DATA_BLOB outdyn;
148         uint16_t out_output_buffer_offset;
149         DATA_BLOB out_output_buffer = data_blob_null;
150         NTSTATUS status;
151         NTSTATUS error; /* transport error */
152
153         status = smbd_smb2_query_directory_recv(subreq,
154                                      req,
155                                      &out_output_buffer);
156         TALLOC_FREE(subreq);
157         if (!NT_STATUS_IS_OK(status)) {
158                 error = smbd_smb2_request_error(req, status);
159                 if (!NT_STATUS_IS_OK(error)) {
160                         smbd_server_connection_terminate(req->xconn,
161                                                          nt_errstr(error));
162                         return;
163                 }
164                 return;
165         }
166
167         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
168
169         outbody = smbd_smb2_generate_outbody(req, 0x08);
170         if (outbody.data == NULL) {
171                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
172                 if (!NT_STATUS_IS_OK(error)) {
173                         smbd_server_connection_terminate(req->xconn,
174                                                          nt_errstr(error));
175                         return;
176                 }
177                 return;
178         }
179
180         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
181         SSVAL(outbody.data, 0x02,
182               out_output_buffer_offset);        /* output buffer offset */
183         SIVAL(outbody.data, 0x04,
184               out_output_buffer.length);        /* output buffer length */
185
186         DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
187                 (unsigned int)out_output_buffer.length ));
188
189         outdyn = out_output_buffer;
190
191         error = smbd_smb2_request_done(req, outbody, &outdyn);
192         if (!NT_STATUS_IS_OK(error)) {
193                 smbd_server_connection_terminate(req->xconn,
194                                                  nt_errstr(error));
195                 return;
196         }
197 }
198
199 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
200                                                 struct tevent_context *ev,
201                                                 connection_struct *conn,
202                                                 struct file_id id,
203                                                 int info_level,
204                                                 char *entry_marshall_buf,
205                                                 bool *stop);
206 static NTSTATUS fetch_write_time_recv(struct tevent_req *req);
207
208
209 struct smbd_smb2_query_directory_state {
210         struct tevent_context *ev;
211         struct smbd_smb2_request *smb2req;
212         uint64_t async_count;
213         uint32_t find_async_delay_usec;
214         DATA_BLOB out_output_buffer;
215 };
216
217 static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq);
218 static void smb2_query_directory_waited(struct tevent_req *subreq);
219
220 static struct tevent_req *smbd_smb2_query_directory_send(TALLOC_CTX *mem_ctx,
221                                               struct tevent_context *ev,
222                                               struct smbd_smb2_request *smb2req,
223                                               struct files_struct *fsp,
224                                               uint8_t in_file_info_class,
225                                               uint8_t in_flags,
226                                               uint32_t in_file_index,
227                                               uint32_t in_output_buffer_length,
228                                               const char *in_file_name)
229 {
230         struct smbXsrv_connection *xconn = smb2req->xconn;
231         struct tevent_req *req;
232         struct smbd_smb2_query_directory_state *state;
233         struct smb_request *smbreq;
234         connection_struct *conn = smb2req->tcon->compat;
235         NTSTATUS status;
236         NTSTATUS empty_status;
237         uint32_t info_level;
238         uint32_t max_count;
239         char *pdata;
240         char *base_data;
241         char *end_data;
242         int last_entry_off = 0;
243         int off = 0;
244         uint32_t num = 0;
245         uint32_t dirtype = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY;
246         bool dont_descend = false;
247         bool ask_sharemode = false;
248         bool async_ask_sharemode = false;
249         bool wcard_has_wild = false;
250         struct tm tm;
251         char *p;
252
253         req = tevent_req_create(mem_ctx, &state,
254                                 struct smbd_smb2_query_directory_state);
255         if (req == NULL) {
256                 return NULL;
257         }
258         state->ev = ev;
259         state->smb2req = smb2req;
260         state->out_output_buffer = data_blob_null;
261
262         DEBUG(10,("smbd_smb2_query_directory_send: %s - %s\n",
263                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
264
265         smbreq = smbd_smb2_fake_smb_request(smb2req);
266         if (tevent_req_nomem(smbreq, req)) {
267                 return tevent_req_post(req, ev);
268         }
269
270         if (!fsp->is_directory) {
271                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
272                 return tevent_req_post(req, ev);
273         }
274
275         if (strcmp(in_file_name, "") == 0) {
276                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
277                 return tevent_req_post(req, ev);
278         }
279         if (strchr_m(in_file_name, '\\') != NULL) {
280                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
281                 return tevent_req_post(req, ev);
282         }
283         if (strchr_m(in_file_name, '/') != NULL) {
284                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
285                 return tevent_req_post(req, ev);
286         }
287
288         p = strptime(in_file_name, GMT_FORMAT, &tm);
289         if ((p != NULL) && (*p =='\0')) {
290                 /*
291                  * Bogus find that asks for a shadow copy timestamp as a
292                  * directory. The correct response is that it does not exist as
293                  * a directory.
294                  */
295                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_FILE);
296                 return tevent_req_post(req, ev);
297         }
298
299         if (in_output_buffer_length > xconn->smb2.server.max_trans) {
300                 DEBUG(2,("smbd_smb2_query_directory_send: "
301                          "client ignored max trans:%s: 0x%08X: 0x%08X\n",
302                          __location__, in_output_buffer_length,
303                          xconn->smb2.server.max_trans));
304                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
305                 return tevent_req_post(req, ev);
306         }
307
308         status = smbd_smb2_request_verify_creditcharge(smb2req,
309                                         in_output_buffer_length);
310
311         if (!NT_STATUS_IS_OK(status)) {
312                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
313                 return tevent_req_post(req, ev);
314         }
315
316         switch (in_file_info_class) {
317         case SMB2_FIND_DIRECTORY_INFO:
318                 info_level = SMB_FIND_FILE_DIRECTORY_INFO;
319                 break;
320
321         case SMB2_FIND_FULL_DIRECTORY_INFO:
322                 info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
323                 break;
324
325         case SMB2_FIND_BOTH_DIRECTORY_INFO:
326                 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
327                 break;
328
329         case SMB2_FIND_NAME_INFO:
330                 info_level = SMB_FIND_FILE_NAMES_INFO;
331                 break;
332
333         case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
334                 info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
335                 break;
336
337         case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
338                 info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
339                 break;
340
341         default:
342                 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
343                 return tevent_req_post(req, ev);
344         }
345
346         if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
347                 int flags;
348
349                 status = fd_close(fsp);
350                 if (tevent_req_nterror(req, status)) {
351                         return tevent_req_post(req, ev);
352                 }
353
354                 /*
355                  * fd_close() will close and invalidate the fsp's file
356                  * descriptor. So we have to reopen it.
357                  */
358
359                 flags = O_RDONLY;
360 #ifdef O_DIRECTORY
361                 flags |= O_DIRECTORY;
362 #endif
363                 status = fd_open(conn, fsp, flags, 0);
364                 if (tevent_req_nterror(req, status)) {
365                         return tevent_req_post(req, ev);
366                 }
367         }
368
369         if (!smbreq->posix_pathnames) {
370                 wcard_has_wild = ms_has_wild(in_file_name);
371         }
372
373         /* Ensure we've canonicalized any search path if not a wildcard. */
374         if (!wcard_has_wild) {
375                 struct smb_filename *smb_fname = NULL;
376                 const char *fullpath;
377                 char tmpbuf[PATH_MAX];
378                 char *to_free = NULL;
379                 uint32_t ucf_flags = UCF_SAVE_LCOMP |
380                                      UCF_ALWAYS_ALLOW_WCARD_LCOMP |
381                                      (smbreq->posix_pathnames ?
382                                         UCF_POSIX_PATHNAMES : 0);
383
384                 if (ISDOT(fsp->fsp_name->base_name)) {
385                         fullpath = in_file_name;
386                 } else {
387                         size_t len;
388                         char *tmp;
389
390                         len = full_path_tos(
391                                 fsp->fsp_name->base_name, in_file_name,
392                                 tmpbuf, sizeof(tmpbuf), &tmp, &to_free);
393                         if (len == -1) {
394                                 tevent_req_oom(req);
395                                 return tevent_req_post(req, ev);
396                         }
397                         fullpath = tmp;
398                 }
399                 status = filename_convert(state,
400                                 conn,
401                                 fullpath,
402                                 ucf_flags,
403                                 &wcard_has_wild,
404                                 &smb_fname);
405
406                 TALLOC_FREE(to_free);
407
408                 if (tevent_req_nterror(req, status)) {
409                         return tevent_req_post(req, ev);
410                 }
411
412                 in_file_name = smb_fname->original_lcomp;
413         }
414
415         if (fsp->dptr == NULL) {
416                 status = dptr_create(conn,
417                                      NULL, /* req */
418                                      fsp,
419                                      fsp->fsp_name,
420                                      false, /* old_handle */
421                                      false, /* expect_close */
422                                      0, /* spid */
423                                      in_file_name, /* wcard */
424                                      wcard_has_wild,
425                                      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                 empty_status = NT_STATUS_NO_SUCH_FILE;
433         } else {
434                 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                 max_count = 1;
443         } else {
444                 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         pdata = (char *)state->out_output_buffer.data;
457         base_data = 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         end_data = pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
463         last_entry_off = 0;
464         off = 0;
465         num = 0;
466
467         DEBUG(8,("smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
468                 "in_output_buffer_length = %u\n",
469                 fsp->fsp_name->base_name, lp_dont_descend(talloc_tos(), SNUM(conn)),
470                 (unsigned int)in_output_buffer_length ));
471         if (in_list(fsp->fsp_name->base_name,lp_dont_descend(talloc_tos(), SNUM(conn)),
472                         conn->case_sensitive)) {
473                 dont_descend = true;
474         }
475
476         /*
477          * SMB_FIND_FILE_NAMES_INFO doesn't need stat information
478          *
479          * This may change when we try to improve the delete on close
480          * handling in future.
481          */
482         if (info_level != SMB_FIND_FILE_NAMES_INFO) {
483                 ask_sharemode = lp_parm_bool(SNUM(conn),
484                                              "smbd", "search ask sharemode",
485                                              true);
486         }
487
488         if (ask_sharemode && lp_clustering()) {
489                 ask_sharemode = false;
490                 async_ask_sharemode = true;
491
492                 /*
493                  * Should we only set async_internal
494                  * if we're not the last request in
495                  * a compound chain?
496                  */
497                 smb2_request_set_async_internal(smb2req, true);
498         }
499
500         /*
501          * This gets set in autobuild for some tests
502          */
503         state->find_async_delay_usec = lp_parm_ulong(SNUM(conn), "smbd",
504                                                      "find async delay usec",
505                                                      0);
506
507         while (true) {
508                 bool got_exact_match = false;
509                 int space_remaining = in_output_buffer_length - off;
510                 struct file_id file_id;
511                 bool stop = false;
512
513                 SMB_ASSERT(space_remaining >= 0);
514
515                 status = smbd_dirptr_lanman2_entry(state,
516                                                conn,
517                                                fsp->dptr,
518                                                smbreq->flags2,
519                                                in_file_name,
520                                                dirtype,
521                                                info_level,
522                                                false, /* requires_resume_key */
523                                                dont_descend,
524                                                ask_sharemode,
525                                                8, /* align to 8 bytes */
526                                                false, /* no padding */
527                                                &pdata,
528                                                base_data,
529                                                end_data,
530                                                space_remaining,
531                                                &got_exact_match,
532                                                &last_entry_off,
533                                                NULL,
534                                                &file_id);
535
536                 off = (int)PTR_DIFF(pdata, base_data);
537
538                 if (!NT_STATUS_IS_OK(status)) {
539                         if (NT_STATUS_EQUAL(status, NT_STATUS_ILLEGAL_CHARACTER)) {
540                                 /*
541                                  * Bad character conversion on name. Ignore this
542                                  * entry.
543                                  */
544                                 continue;
545                         } else if (num > 0) {
546                                 goto last_entry_done;
547                         } else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
548                                 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
549                                 return tevent_req_post(req, ev);
550                         } else {
551                                 tevent_req_nterror(req, empty_status);
552                                 return tevent_req_post(req, ev);
553                         }
554                 }
555
556                 if (async_ask_sharemode) {
557                         struct tevent_req *subreq = NULL;
558
559                         subreq = fetch_write_time_send(req,
560                                                        ev,
561                                                        conn,
562                                                        file_id,
563                                                        info_level,
564                                                        base_data + last_entry_off,
565                                                        &stop);
566                         if (tevent_req_nomem(subreq, req)) {
567                                 return tevent_req_post(req, ev);
568                         }
569                         tevent_req_set_callback(
570                                 subreq,
571                                 smb2_query_directory_fetch_write_time_done,
572                                 req);
573
574                         state->async_count++;
575                 }
576
577                 num++;
578                 state->out_output_buffer.length = off;
579
580                 if (num >= max_count) {
581                         stop = true;
582                 }
583
584                 if (!stop) {
585                         continue;
586                 }
587
588 last_entry_done:
589                 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
590                 if (state->async_count > 0) {
591                         DBG_DEBUG("Stopping after %"PRIu64" async mtime "
592                                   "updates\n", state->async_count);
593                         return req;
594                 }
595
596                 if (state->find_async_delay_usec > 0) {
597                         struct timeval tv;
598                         struct tevent_req *subreq = NULL;
599
600                         /*
601                          * Should we only set async_internal
602                          * if we're not the last request in
603                          * a compound chain?
604                          */
605                         smb2_request_set_async_internal(smb2req, true);
606
607                         tv = timeval_current_ofs(0, state->find_async_delay_usec);
608
609                         subreq = tevent_wakeup_send(state, ev, tv);
610                         if (tevent_req_nomem(subreq, req)) {
611                                 return tevent_req_post(req, ev);
612                         }
613                         tevent_req_set_callback(subreq,
614                                                 smb2_query_directory_waited,
615                                                 req);
616                         return req;
617                 }
618
619                 tevent_req_done(req);
620                 return tevent_req_post(req, ev);
621         }
622
623         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
624         return tevent_req_post(req, ev);
625 }
626
627 static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq)
628 {
629         struct tevent_req *req = tevent_req_callback_data(
630                 subreq, struct tevent_req);
631         struct smbd_smb2_query_directory_state *state = tevent_req_data(
632                 req, struct smbd_smb2_query_directory_state);
633         NTSTATUS status;
634
635         state->async_count--;
636
637         status = fetch_write_time_recv(subreq);
638         TALLOC_FREE(subreq);
639         if (tevent_req_nterror(req, status)) {
640                 return;
641         }
642
643         if (state->async_count > 0) {
644                 return;
645         }
646
647         if (state->find_async_delay_usec > 0) {
648                 struct timeval tv;
649
650                 tv = timeval_current_ofs(0, state->find_async_delay_usec);
651
652                 subreq = tevent_wakeup_send(state, state->ev, tv);
653                 if (tevent_req_nomem(subreq, req)) {
654                         tevent_req_post(req, state->ev);
655                         return;
656                 }
657                 tevent_req_set_callback(subreq,
658                                         smb2_query_directory_waited,
659                                         req);
660                 return;
661         }
662
663         tevent_req_done(req);
664         return;
665 }
666
667 static void smb2_query_directory_waited(struct tevent_req *subreq)
668 {
669         struct tevent_req *req = tevent_req_callback_data(
670                 subreq, struct tevent_req);
671         bool ok;
672
673         ok = tevent_wakeup_recv(subreq);
674         TALLOC_FREE(subreq);
675         if (!ok) {
676                 tevent_req_oom(req);
677                 return;
678         }
679         tevent_req_done(req);
680 }
681
682 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
683                                     TALLOC_CTX *mem_ctx,
684                                     DATA_BLOB *out_output_buffer)
685 {
686         NTSTATUS status;
687         struct smbd_smb2_query_directory_state *state = tevent_req_data(req,
688                                              struct smbd_smb2_query_directory_state);
689
690         if (tevent_req_is_nterror(req, &status)) {
691                 tevent_req_received(req);
692                 return status;
693         }
694
695         *out_output_buffer = state->out_output_buffer;
696         talloc_steal(mem_ctx, out_output_buffer->data);
697
698         tevent_req_received(req);
699         return NT_STATUS_OK;
700 }
701
702 struct fetch_write_time_state {
703         connection_struct *conn;
704         struct file_id id;
705         int info_level;
706         char *entry_marshall_buf;
707 };
708
709 static void fetch_write_time_done(struct tevent_req *subreq);
710
711 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
712                                                 struct tevent_context *ev,
713                                                 connection_struct *conn,
714                                                 struct file_id id,
715                                                 int info_level,
716                                                 char *entry_marshall_buf,
717                                                 bool *stop)
718 {
719         struct tevent_req *req = NULL;
720         struct fetch_write_time_state *state = NULL;
721         struct tevent_req *subreq = NULL;
722         bool req_queued;
723
724         *stop = false;
725
726         req = tevent_req_create(mem_ctx, &state, struct fetch_write_time_state);
727         if (req == NULL) {
728                 return NULL;
729         }
730
731         *state = (struct fetch_write_time_state) {
732                 .conn = conn,
733                 .id = id,
734                 .info_level = info_level,
735                 .entry_marshall_buf = entry_marshall_buf,
736         };
737
738         subreq = fetch_share_mode_send(state, ev, id, &req_queued);
739         if (tevent_req_nomem(subreq, req)) {
740                 return tevent_req_post(req, ev);
741         }
742         tevent_req_set_callback(subreq, fetch_write_time_done, req);
743
744         if (req_queued) {
745                 *stop = true;
746         }
747         return req;
748 }
749
750 static void fetch_write_time_done(struct tevent_req *subreq)
751 {
752         struct tevent_req *req = tevent_req_callback_data(
753                 subreq, struct tevent_req);
754         struct fetch_write_time_state *state = tevent_req_data(
755                 req, struct fetch_write_time_state);
756         struct timespec write_time;
757         struct share_mode_lock *lck = NULL;
758         NTSTATUS status;
759         size_t off;
760
761         status = fetch_share_mode_recv(subreq, state, &lck);
762         TALLOC_FREE(subreq);
763         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
764                 tevent_req_done(req);
765                 return;
766         }
767         if (!NT_STATUS_IS_OK(status)) {
768                 tevent_req_nterror(req, status);
769                 return;
770         }
771
772         write_time = get_share_mode_write_time(lck);
773         TALLOC_FREE(lck);
774
775         if (null_timespec(write_time)) {
776                 tevent_req_done(req);
777                 return;
778         }
779
780         switch (state->info_level) {
781         case SMB_FIND_FILE_DIRECTORY_INFO:
782         case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
783         case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
784         case SMB_FIND_ID_FULL_DIRECTORY_INFO:
785         case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
786                 off = 24;
787                 break;
788
789         default:
790                 DBG_ERR("Unsupported info_level [%d]\n", state->info_level);
791                 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
792                 return;
793         }
794
795         put_long_date_timespec(state->conn->ts_res,
796                                state->entry_marshall_buf + off,
797                                write_time);
798
799         tevent_req_done(req);
800         return;
801 }
802
803 static NTSTATUS fetch_write_time_recv(struct tevent_req *req)
804 {
805         NTSTATUS status;
806
807         if (tevent_req_is_nterror(req, &status)) {
808                 tevent_req_received(req);
809                 return status;
810         }
811
812         tevent_req_received(req);
813         return NT_STATUS_OK;
814 }