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