s3: smbd: Fix possible directory fd leak if the underlying OS doesn't support fdopendir()
[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                 status = fd_close(fsp);
347                 if (tevent_req_nterror(req, status)) {
348                         return tevent_req_post(req, ev);
349                 }
350
351                 /*
352                  * fd_close() will close and invalidate the fsp's file
353                  * descriptor. So we have to reopen it.
354                  */
355
356                 flags = O_RDONLY;
357 #ifdef O_DIRECTORY
358                 flags |= O_DIRECTORY;
359 #endif
360                 status = fd_open(conn, fsp, flags, 0);
361                 if (tevent_req_nterror(req, status)) {
362                         return tevent_req_post(req, ev);
363                 }
364         }
365
366         if (!smbreq->posix_pathnames) {
367                 wcard_has_wild = ms_has_wild(in_file_name);
368         }
369
370         /* Ensure we've canonicalized any search path if not a wildcard. */
371         if (!wcard_has_wild) {
372                 struct smb_filename *smb_fname = NULL;
373                 const char *fullpath;
374                 char tmpbuf[PATH_MAX];
375                 char *to_free = NULL;
376                 uint32_t ucf_flags = UCF_SAVE_LCOMP |
377                                      UCF_ALWAYS_ALLOW_WCARD_LCOMP |
378                                      (smbreq->posix_pathnames ?
379                                         UCF_POSIX_PATHNAMES : 0);
380
381                 if (ISDOT(fsp->fsp_name->base_name)) {
382                         fullpath = in_file_name;
383                 } else {
384                         size_t len;
385                         char *tmp;
386
387                         len = full_path_tos(
388                                 fsp->fsp_name->base_name, in_file_name,
389                                 tmpbuf, sizeof(tmpbuf), &tmp, &to_free);
390                         if (len == -1) {
391                                 tevent_req_oom(req);
392                                 return tevent_req_post(req, ev);
393                         }
394                         fullpath = tmp;
395                 }
396                 status = filename_convert(state,
397                                 conn,
398                                 fullpath,
399                                 ucf_flags,
400                                 &wcard_has_wild,
401                                 &smb_fname);
402
403                 TALLOC_FREE(to_free);
404
405                 if (tevent_req_nterror(req, status)) {
406                         return tevent_req_post(req, ev);
407                 }
408
409                 in_file_name = smb_fname->original_lcomp;
410         }
411
412         if (fsp->dptr == NULL) {
413                 status = dptr_create(conn,
414                                      NULL, /* req */
415                                      fsp,
416                                      fsp->fsp_name,
417                                      false, /* old_handle */
418                                      false, /* expect_close */
419                                      0, /* spid */
420                                      in_file_name, /* wcard */
421                                      wcard_has_wild,
422                                      dirtype,
423                                      &fsp->dptr);
424                 if (!NT_STATUS_IS_OK(status)) {
425                         tevent_req_nterror(req, status);
426                         return tevent_req_post(req, ev);
427                 }
428
429                 empty_status = NT_STATUS_NO_SUCH_FILE;
430         } else {
431                 empty_status = STATUS_NO_MORE_FILES;
432         }
433
434         if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
435                 dptr_SeekDir(fsp->dptr, 0);
436         }
437
438         if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
439                 max_count = 1;
440         } else {
441                 max_count = UINT16_MAX;
442         }
443
444 #define DIR_ENTRY_SAFETY_MARGIN 4096
445
446         state->out_output_buffer = data_blob_talloc(state, NULL,
447                         in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
448         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
449                 return tevent_req_post(req, ev);
450         }
451
452         state->out_output_buffer.length = 0;
453         pdata = (char *)state->out_output_buffer.data;
454         base_data = pdata;
455         /*
456          * end_data must include the safety margin as it's what is
457          * used to determine if pushed strings have been truncated.
458          */
459         end_data = pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
460         last_entry_off = 0;
461         off = 0;
462         num = 0;
463
464         DEBUG(8,("smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
465                 "in_output_buffer_length = %u\n",
466                 fsp->fsp_name->base_name, lp_dont_descend(talloc_tos(), SNUM(conn)),
467                 (unsigned int)in_output_buffer_length ));
468         if (in_list(fsp->fsp_name->base_name,lp_dont_descend(talloc_tos(), SNUM(conn)),
469                         conn->case_sensitive)) {
470                 dont_descend = true;
471         }
472
473         /*
474          * SMB_FIND_FILE_NAMES_INFO doesn't need stat information
475          *
476          * This may change when we try to improve the delete on close
477          * handling in future.
478          */
479         if (info_level != SMB_FIND_FILE_NAMES_INFO) {
480                 ask_sharemode = lp_parm_bool(SNUM(conn),
481                                              "smbd", "search ask sharemode",
482                                              true);
483         }
484
485         if (ask_sharemode && lp_clustering()) {
486                 ask_sharemode = false;
487                 async_ask_sharemode = true;
488
489                 /*
490                  * Should we only set async_internal
491                  * if we're not the last request in
492                  * a compound chain?
493                  */
494                 smb2_request_set_async_internal(smb2req, true);
495         }
496
497         /*
498          * This gets set in autobuild for some tests
499          */
500         state->find_async_delay_usec = lp_parm_ulong(SNUM(conn), "smbd",
501                                                      "find async delay usec",
502                                                      0);
503
504         while (true) {
505                 bool got_exact_match = false;
506                 int space_remaining = in_output_buffer_length - off;
507                 struct file_id file_id;
508                 bool stop = false;
509
510                 SMB_ASSERT(space_remaining >= 0);
511
512                 status = smbd_dirptr_lanman2_entry(state,
513                                                conn,
514                                                fsp->dptr,
515                                                smbreq->flags2,
516                                                in_file_name,
517                                                dirtype,
518                                                info_level,
519                                                false, /* requires_resume_key */
520                                                dont_descend,
521                                                ask_sharemode,
522                                                8, /* align to 8 bytes */
523                                                false, /* no padding */
524                                                &pdata,
525                                                base_data,
526                                                end_data,
527                                                space_remaining,
528                                                &got_exact_match,
529                                                &last_entry_off,
530                                                NULL,
531                                                &file_id);
532
533                 off = (int)PTR_DIFF(pdata, base_data);
534
535                 if (!NT_STATUS_IS_OK(status)) {
536                         if (NT_STATUS_EQUAL(status, NT_STATUS_ILLEGAL_CHARACTER)) {
537                                 /*
538                                  * Bad character conversion on name. Ignore this
539                                  * entry.
540                                  */
541                                 continue;
542                         } else if (num > 0) {
543                                 goto last_entry_done;
544                         } else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
545                                 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
546                                 return tevent_req_post(req, ev);
547                         } else {
548                                 tevent_req_nterror(req, empty_status);
549                                 return tevent_req_post(req, ev);
550                         }
551                 }
552
553                 if (async_ask_sharemode) {
554                         struct tevent_req *subreq = NULL;
555
556                         subreq = fetch_write_time_send(req,
557                                                        ev,
558                                                        conn,
559                                                        file_id,
560                                                        info_level,
561                                                        base_data + last_entry_off,
562                                                        &stop);
563                         if (tevent_req_nomem(subreq, req)) {
564                                 return tevent_req_post(req, ev);
565                         }
566                         tevent_req_set_callback(
567                                 subreq,
568                                 smb2_query_directory_fetch_write_time_done,
569                                 req);
570
571                         state->async_count++;
572                 }
573
574                 num++;
575                 state->out_output_buffer.length = off;
576
577                 if (num >= max_count) {
578                         stop = true;
579                 }
580
581                 if (!stop) {
582                         continue;
583                 }
584
585 last_entry_done:
586                 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
587                 if (state->async_count > 0) {
588                         DBG_DEBUG("Stopping after %"PRIu64" async mtime "
589                                   "updates\n", state->async_count);
590                         return req;
591                 }
592
593                 if (state->find_async_delay_usec > 0) {
594                         struct timeval tv;
595                         struct tevent_req *subreq = NULL;
596
597                         /*
598                          * Should we only set async_internal
599                          * if we're not the last request in
600                          * a compound chain?
601                          */
602                         smb2_request_set_async_internal(smb2req, true);
603
604                         tv = timeval_current_ofs(0, state->find_async_delay_usec);
605
606                         subreq = tevent_wakeup_send(state, ev, tv);
607                         if (tevent_req_nomem(subreq, req)) {
608                                 return tevent_req_post(req, ev);
609                         }
610                         tevent_req_set_callback(subreq,
611                                                 smb2_query_directory_waited,
612                                                 req);
613                         return req;
614                 }
615
616                 tevent_req_done(req);
617                 return tevent_req_post(req, ev);
618         }
619
620         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
621         return tevent_req_post(req, ev);
622 }
623
624 static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq)
625 {
626         struct tevent_req *req = tevent_req_callback_data(
627                 subreq, struct tevent_req);
628         struct smbd_smb2_query_directory_state *state = tevent_req_data(
629                 req, struct smbd_smb2_query_directory_state);
630         NTSTATUS status;
631
632         state->async_count--;
633
634         status = fetch_write_time_recv(subreq);
635         TALLOC_FREE(subreq);
636         if (tevent_req_nterror(req, status)) {
637                 return;
638         }
639
640         if (state->async_count > 0) {
641                 return;
642         }
643
644         if (state->find_async_delay_usec > 0) {
645                 struct timeval tv;
646
647                 tv = timeval_current_ofs(0, state->find_async_delay_usec);
648
649                 subreq = tevent_wakeup_send(state, state->ev, tv);
650                 if (tevent_req_nomem(subreq, req)) {
651                         tevent_req_post(req, state->ev);
652                         return;
653                 }
654                 tevent_req_set_callback(subreq,
655                                         smb2_query_directory_waited,
656                                         req);
657                 return;
658         }
659
660         tevent_req_done(req);
661         return;
662 }
663
664 static void smb2_query_directory_waited(struct tevent_req *subreq)
665 {
666         struct tevent_req *req = tevent_req_callback_data(
667                 subreq, struct tevent_req);
668         bool ok;
669
670         ok = tevent_wakeup_recv(subreq);
671         TALLOC_FREE(subreq);
672         if (!ok) {
673                 tevent_req_oom(req);
674                 return;
675         }
676         tevent_req_done(req);
677 }
678
679 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
680                                     TALLOC_CTX *mem_ctx,
681                                     DATA_BLOB *out_output_buffer)
682 {
683         NTSTATUS status;
684         struct smbd_smb2_query_directory_state *state = tevent_req_data(req,
685                                              struct smbd_smb2_query_directory_state);
686
687         if (tevent_req_is_nterror(req, &status)) {
688                 tevent_req_received(req);
689                 return status;
690         }
691
692         *out_output_buffer = state->out_output_buffer;
693         talloc_steal(mem_ctx, out_output_buffer->data);
694
695         tevent_req_received(req);
696         return NT_STATUS_OK;
697 }
698
699 struct fetch_write_time_state {
700         connection_struct *conn;
701         struct file_id id;
702         int info_level;
703         char *entry_marshall_buf;
704 };
705
706 static void fetch_write_time_done(struct tevent_req *subreq);
707
708 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
709                                                 struct tevent_context *ev,
710                                                 connection_struct *conn,
711                                                 struct file_id id,
712                                                 int info_level,
713                                                 char *entry_marshall_buf,
714                                                 bool *stop)
715 {
716         struct tevent_req *req = NULL;
717         struct fetch_write_time_state *state = NULL;
718         struct tevent_req *subreq = NULL;
719         bool req_queued;
720
721         *stop = false;
722
723         req = tevent_req_create(mem_ctx, &state, struct fetch_write_time_state);
724         if (req == NULL) {
725                 return NULL;
726         }
727
728         *state = (struct fetch_write_time_state) {
729                 .conn = conn,
730                 .id = id,
731                 .info_level = info_level,
732                 .entry_marshall_buf = entry_marshall_buf,
733         };
734
735         subreq = fetch_share_mode_send(state, ev, id, &req_queued);
736         if (tevent_req_nomem(subreq, req)) {
737                 return tevent_req_post(req, ev);
738         }
739         tevent_req_set_callback(subreq, fetch_write_time_done, req);
740
741         if (req_queued) {
742                 *stop = true;
743         }
744         return req;
745 }
746
747 static void fetch_write_time_done(struct tevent_req *subreq)
748 {
749         struct tevent_req *req = tevent_req_callback_data(
750                 subreq, struct tevent_req);
751         struct fetch_write_time_state *state = tevent_req_data(
752                 req, struct fetch_write_time_state);
753         struct timespec write_time;
754         struct share_mode_lock *lck = NULL;
755         NTSTATUS status;
756         size_t off;
757
758         status = fetch_share_mode_recv(subreq, state, &lck);
759         TALLOC_FREE(subreq);
760         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
761                 tevent_req_done(req);
762                 return;
763         }
764         if (!NT_STATUS_IS_OK(status)) {
765                 tevent_req_nterror(req, status);
766                 return;
767         }
768
769         write_time = get_share_mode_write_time(lck);
770         TALLOC_FREE(lck);
771
772         if (null_timespec(write_time)) {
773                 tevent_req_done(req);
774                 return;
775         }
776
777         switch (state->info_level) {
778         case SMB_FIND_FILE_DIRECTORY_INFO:
779         case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
780         case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
781         case SMB_FIND_ID_FULL_DIRECTORY_INFO:
782         case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
783                 off = 24;
784                 break;
785
786         default:
787                 DBG_ERR("Unsupported info_level [%d]\n", state->info_level);
788                 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
789                 return;
790         }
791
792         put_long_date_timespec(state->conn->ts_res,
793                                state->entry_marshall_buf + off,
794                                write_time);
795
796         tevent_req_done(req);
797         return;
798 }
799
800 static NTSTATUS fetch_write_time_recv(struct tevent_req *req)
801 {
802         NTSTATUS status;
803
804         if (tevent_req_is_nterror(req, &status)) {
805                 tevent_req_received(req);
806                 return status;
807         }
808
809         tevent_req_received(req);
810         return NT_STATUS_OK;
811 }