s3/smbd: sticky write time offset miscalculation causes broken timestamps
[garming/samba-autobuild/.git] / source3 / smbd / smb2_query_directory.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "trans2.h"
26 #include "../lib/util/tevent_ntstatus.h"
27 #include "system/filesys.h"
28
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                                 fullpath,
396                                 ucf_flags,
397                                 &wcard_has_wild,
398                                 &smb_fname);
399
400                 TALLOC_FREE(to_free);
401
402                 if (tevent_req_nterror(req, status)) {
403                         return tevent_req_post(req, ev);
404                 }
405
406                 in_file_name = smb_fname->original_lcomp;
407         }
408
409         if (fsp->dptr == NULL) {
410                 status = dptr_create(conn,
411                                      NULL, /* req */
412                                      fsp,
413                                      fsp->fsp_name,
414                                      false, /* old_handle */
415                                      false, /* expect_close */
416                                      0, /* spid */
417                                      in_file_name, /* wcard */
418                                      wcard_has_wild,
419                                      dirtype,
420                                      &fsp->dptr);
421                 if (!NT_STATUS_IS_OK(status)) {
422                         tevent_req_nterror(req, status);
423                         return tevent_req_post(req, ev);
424                 }
425
426                 empty_status = NT_STATUS_NO_SUCH_FILE;
427         } else {
428                 empty_status = STATUS_NO_MORE_FILES;
429         }
430
431         if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
432                 dptr_SeekDir(fsp->dptr, 0);
433         }
434
435         if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
436                 max_count = 1;
437         } else {
438                 max_count = UINT16_MAX;
439         }
440
441 #define DIR_ENTRY_SAFETY_MARGIN 4096
442
443         state->out_output_buffer = data_blob_talloc(state, NULL,
444                         in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
445         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
446                 return tevent_req_post(req, ev);
447         }
448
449         state->out_output_buffer.length = 0;
450         pdata = (char *)state->out_output_buffer.data;
451         base_data = pdata;
452         /*
453          * end_data must include the safety margin as it's what is
454          * used to determine if pushed strings have been truncated.
455          */
456         end_data = pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
457         last_entry_off = 0;
458         off = 0;
459         num = 0;
460
461         DEBUG(8,("smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
462                 "in_output_buffer_length = %u\n",
463                 fsp->fsp_name->base_name, lp_dont_descend(talloc_tos(), SNUM(conn)),
464                 (unsigned int)in_output_buffer_length ));
465         if (in_list(fsp->fsp_name->base_name,lp_dont_descend(talloc_tos(), SNUM(conn)),
466                         conn->case_sensitive)) {
467                 dont_descend = true;
468         }
469
470         /*
471          * SMB_FIND_FILE_NAMES_INFO doesn't need stat information
472          *
473          * This may change when we try to improve the delete on close
474          * handling in future.
475          */
476         if (info_level != SMB_FIND_FILE_NAMES_INFO) {
477                 ask_sharemode = lp_parm_bool(SNUM(conn),
478                                              "smbd", "search ask sharemode",
479                                              true);
480         }
481
482         if (ask_sharemode && lp_clustering()) {
483                 ask_sharemode = false;
484                 async_ask_sharemode = true;
485
486                 /*
487                  * Should we only set async_internal
488                  * if we're not the last request in
489                  * a compound chain?
490                  */
491                 smb2_request_set_async_internal(smb2req, true);
492         }
493
494         /*
495          * This gets set in autobuild for some tests
496          */
497         state->find_async_delay_usec = lp_parm_ulong(SNUM(conn), "smbd",
498                                                      "find async delay usec",
499                                                      0);
500
501         while (true) {
502                 bool got_exact_match = false;
503                 int space_remaining = in_output_buffer_length - off;
504                 struct file_id file_id;
505                 bool stop = false;
506
507                 SMB_ASSERT(space_remaining >= 0);
508
509                 status = smbd_dirptr_lanman2_entry(state,
510                                                conn,
511                                                fsp->dptr,
512                                                smbreq->flags2,
513                                                in_file_name,
514                                                dirtype,
515                                                info_level,
516                                                false, /* requires_resume_key */
517                                                dont_descend,
518                                                ask_sharemode,
519                                                8, /* align to 8 bytes */
520                                                false, /* no padding */
521                                                &pdata,
522                                                base_data,
523                                                end_data,
524                                                space_remaining,
525                                                &got_exact_match,
526                                                &last_entry_off,
527                                                NULL,
528                                                &file_id);
529
530                 off = (int)PTR_DIFF(pdata, base_data);
531
532                 if (!NT_STATUS_IS_OK(status)) {
533                         if (NT_STATUS_EQUAL(status, NT_STATUS_ILLEGAL_CHARACTER)) {
534                                 /*
535                                  * Bad character conversion on name. Ignore this
536                                  * entry.
537                                  */
538                                 continue;
539                         } else if (num > 0) {
540                                 goto last_entry_done;
541                         } else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
542                                 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
543                                 return tevent_req_post(req, ev);
544                         } else {
545                                 tevent_req_nterror(req, empty_status);
546                                 return tevent_req_post(req, ev);
547                         }
548                 }
549
550                 if (async_ask_sharemode) {
551                         struct tevent_req *subreq = NULL;
552
553                         subreq = fetch_write_time_send(req,
554                                                        ev,
555                                                        conn,
556                                                        file_id,
557                                                        info_level,
558                                                        base_data + last_entry_off,
559                                                        &stop);
560                         if (tevent_req_nomem(subreq, req)) {
561                                 return tevent_req_post(req, ev);
562                         }
563                         tevent_req_set_callback(
564                                 subreq,
565                                 smb2_query_directory_fetch_write_time_done,
566                                 req);
567
568                         state->async_count++;
569                 }
570
571                 num++;
572                 state->out_output_buffer.length = off;
573
574                 if (num >= max_count) {
575                         stop = true;
576                 }
577
578                 if (!stop) {
579                         continue;
580                 }
581
582 last_entry_done:
583                 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
584                 if (state->async_count > 0) {
585                         DBG_DEBUG("Stopping after %"PRIu64" async mtime "
586                                   "updates\n", state->async_count);
587                         return req;
588                 }
589
590                 if (state->find_async_delay_usec > 0) {
591                         struct timeval tv;
592                         struct tevent_req *subreq = NULL;
593
594                         /*
595                          * Should we only set async_internal
596                          * if we're not the last request in
597                          * a compound chain?
598                          */
599                         smb2_request_set_async_internal(smb2req, true);
600
601                         tv = timeval_current_ofs(0, state->find_async_delay_usec);
602
603                         subreq = tevent_wakeup_send(state, ev, tv);
604                         if (tevent_req_nomem(subreq, req)) {
605                                 return tevent_req_post(req, ev);
606                         }
607                         tevent_req_set_callback(subreq,
608                                                 smb2_query_directory_waited,
609                                                 req);
610                         return req;
611                 }
612
613                 tevent_req_done(req);
614                 return tevent_req_post(req, ev);
615         }
616
617         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
618         return tevent_req_post(req, ev);
619 }
620
621 static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq)
622 {
623         struct tevent_req *req = tevent_req_callback_data(
624                 subreq, struct tevent_req);
625         struct smbd_smb2_query_directory_state *state = tevent_req_data(
626                 req, struct smbd_smb2_query_directory_state);
627         NTSTATUS status;
628
629         state->async_count--;
630
631         status = fetch_write_time_recv(subreq);
632         TALLOC_FREE(subreq);
633         if (tevent_req_nterror(req, status)) {
634                 return;
635         }
636
637         if (state->async_count > 0) {
638                 return;
639         }
640
641         if (state->find_async_delay_usec > 0) {
642                 struct timeval tv;
643
644                 tv = timeval_current_ofs(0, state->find_async_delay_usec);
645
646                 subreq = tevent_wakeup_send(state, state->ev, tv);
647                 if (tevent_req_nomem(subreq, req)) {
648                         tevent_req_post(req, state->ev);
649                         return;
650                 }
651                 tevent_req_set_callback(subreq,
652                                         smb2_query_directory_waited,
653                                         req);
654                 return;
655         }
656
657         tevent_req_done(req);
658         return;
659 }
660
661 static void smb2_query_directory_waited(struct tevent_req *subreq)
662 {
663         struct tevent_req *req = tevent_req_callback_data(
664                 subreq, struct tevent_req);
665         bool ok;
666
667         ok = tevent_wakeup_recv(subreq);
668         TALLOC_FREE(subreq);
669         if (!ok) {
670                 tevent_req_oom(req);
671                 return;
672         }
673         tevent_req_done(req);
674 }
675
676 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
677                                     TALLOC_CTX *mem_ctx,
678                                     DATA_BLOB *out_output_buffer)
679 {
680         NTSTATUS status;
681         struct smbd_smb2_query_directory_state *state = tevent_req_data(req,
682                                              struct smbd_smb2_query_directory_state);
683
684         if (tevent_req_is_nterror(req, &status)) {
685                 tevent_req_received(req);
686                 return status;
687         }
688
689         *out_output_buffer = state->out_output_buffer;
690         talloc_steal(mem_ctx, out_output_buffer->data);
691
692         tevent_req_received(req);
693         return NT_STATUS_OK;
694 }
695
696 struct fetch_write_time_state {
697         connection_struct *conn;
698         struct file_id id;
699         int info_level;
700         char *entry_marshall_buf;
701 };
702
703 static void fetch_write_time_done(struct tevent_req *subreq);
704
705 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
706                                                 struct tevent_context *ev,
707                                                 connection_struct *conn,
708                                                 struct file_id id,
709                                                 int info_level,
710                                                 char *entry_marshall_buf,
711                                                 bool *stop)
712 {
713         struct tevent_req *req = NULL;
714         struct fetch_write_time_state *state = NULL;
715         struct tevent_req *subreq = NULL;
716         bool req_queued;
717
718         *stop = false;
719
720         req = tevent_req_create(mem_ctx, &state, struct fetch_write_time_state);
721         if (req == NULL) {
722                 return NULL;
723         }
724
725         *state = (struct fetch_write_time_state) {
726                 .conn = conn,
727                 .id = id,
728                 .info_level = info_level,
729                 .entry_marshall_buf = entry_marshall_buf,
730         };
731
732         subreq = fetch_share_mode_send(state, ev, id, &req_queued);
733         if (tevent_req_nomem(subreq, req)) {
734                 return tevent_req_post(req, ev);
735         }
736         tevent_req_set_callback(subreq, fetch_write_time_done, req);
737
738         if (req_queued) {
739                 *stop = true;
740         }
741         return req;
742 }
743
744 static void fetch_write_time_done(struct tevent_req *subreq)
745 {
746         struct tevent_req *req = tevent_req_callback_data(
747                 subreq, struct tevent_req);
748         struct fetch_write_time_state *state = tevent_req_data(
749                 req, struct fetch_write_time_state);
750         struct timespec write_time;
751         struct share_mode_lock *lck = NULL;
752         NTSTATUS status;
753         size_t off;
754
755         status = fetch_share_mode_recv(subreq, state, &lck);
756         TALLOC_FREE(subreq);
757         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
758                 tevent_req_done(req);
759                 return;
760         }
761         if (!NT_STATUS_IS_OK(status)) {
762                 tevent_req_nterror(req, status);
763                 return;
764         }
765
766         write_time = get_share_mode_write_time(lck);
767         TALLOC_FREE(lck);
768
769         if (null_timespec(write_time)) {
770                 tevent_req_done(req);
771                 return;
772         }
773
774         switch (state->info_level) {
775         case SMB_FIND_FILE_DIRECTORY_INFO:
776         case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
777         case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
778         case SMB_FIND_ID_FULL_DIRECTORY_INFO:
779         case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
780                 off = 24;
781                 break;
782
783         default:
784                 DBG_ERR("Unsupported info_level [%d]\n", state->info_level);
785                 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
786                 return;
787         }
788
789         put_long_date_timespec(state->conn->ts_res,
790                                state->entry_marshall_buf + off,
791                                write_time);
792
793         tevent_req_done(req);
794         return;
795 }
796
797 static NTSTATUS fetch_write_time_recv(struct tevent_req *req)
798 {
799         NTSTATUS status;
800
801         if (tevent_req_is_nterror(req, &status)) {
802                 tevent_req_received(req);
803                 return status;
804         }
805
806         tevent_req_received(req);
807         return NT_STATUS_OK;
808 }