Line data Source code
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 "locking/share_mode_lock.h"
23 : #include "smbd/smbd.h"
24 : #include "smbd/globals.h"
25 : #include "../libcli/smb/smb_common.h"
26 : #include "trans2.h"
27 : #include "../lib/util/tevent_ntstatus.h"
28 : #include "system/filesys.h"
29 : #include "lib/pthreadpool/pthreadpool_tevent.h"
30 :
31 : #undef DBGC_CLASS
32 : #define DBGC_CLASS DBGC_SMB2
33 :
34 : static struct tevent_req *smbd_smb2_query_directory_send(TALLOC_CTX *mem_ctx,
35 : struct tevent_context *ev,
36 : struct smbd_smb2_request *smb2req,
37 : struct files_struct *in_fsp,
38 : uint8_t in_file_info_class,
39 : uint8_t in_flags,
40 : uint32_t in_file_index,
41 : uint32_t in_output_buffer_length,
42 : const char *in_file_name);
43 : static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
44 : TALLOC_CTX *mem_ctx,
45 : DATA_BLOB *out_output_buffer);
46 :
47 : static void smbd_smb2_request_find_done(struct tevent_req *subreq);
48 4287 : NTSTATUS smbd_smb2_request_process_query_directory(struct smbd_smb2_request *req)
49 : {
50 : NTSTATUS status;
51 : const uint8_t *inbody;
52 : uint8_t in_file_info_class;
53 : uint8_t in_flags;
54 : uint32_t in_file_index;
55 : uint64_t in_file_id_persistent;
56 : uint64_t in_file_id_volatile;
57 : struct files_struct *in_fsp;
58 : uint16_t in_file_name_offset;
59 : uint16_t in_file_name_length;
60 : DATA_BLOB in_file_name_buffer;
61 : char *in_file_name_string;
62 : size_t in_file_name_string_size;
63 : uint32_t in_output_buffer_length;
64 : struct tevent_req *subreq;
65 : bool ok;
66 :
67 4287 : status = smbd_smb2_request_verify_sizes(req, 0x21);
68 4287 : if (!NT_STATUS_IS_OK(status)) {
69 0 : return smbd_smb2_request_error(req, status);
70 : }
71 4287 : inbody = SMBD_SMB2_IN_BODY_PTR(req);
72 :
73 4287 : in_file_info_class = CVAL(inbody, 0x02);
74 4287 : in_flags = CVAL(inbody, 0x03);
75 4287 : in_file_index = IVAL(inbody, 0x04);
76 4287 : in_file_id_persistent = BVAL(inbody, 0x08);
77 4287 : in_file_id_volatile = BVAL(inbody, 0x10);
78 4287 : in_file_name_offset = SVAL(inbody, 0x18);
79 4287 : in_file_name_length = SVAL(inbody, 0x1A);
80 4287 : in_output_buffer_length = IVAL(inbody, 0x1C);
81 :
82 4287 : if (in_file_name_offset == 0 && in_file_name_length == 0) {
83 : /* This is ok */
84 7681 : } else if (in_file_name_offset !=
85 4287 : (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
86 0 : return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
87 : }
88 :
89 4287 : if (in_file_name_length > SMBD_SMB2_IN_DYN_LEN(req)) {
90 0 : return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
91 : }
92 :
93 : /* The output header is 8 bytes. */
94 4287 : if (in_output_buffer_length <= 8) {
95 0 : return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
96 : }
97 :
98 4287 : DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
99 : (unsigned int)in_output_buffer_length ));
100 :
101 : /* Take into account the output header. */
102 4287 : in_output_buffer_length -= 8;
103 :
104 4287 : in_file_name_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
105 4287 : in_file_name_buffer.length = in_file_name_length;
106 :
107 7681 : ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
108 4287 : in_file_name_buffer.data,
109 : in_file_name_buffer.length,
110 : &in_file_name_string,
111 : &in_file_name_string_size);
112 4287 : if (!ok) {
113 0 : return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
114 : }
115 :
116 4287 : if (in_file_name_buffer.length == 0) {
117 0 : in_file_name_string_size = 0;
118 : }
119 :
120 4287 : if (strlen(in_file_name_string) != in_file_name_string_size) {
121 0 : return smbd_smb2_request_error(req, NT_STATUS_OBJECT_NAME_INVALID);
122 : }
123 :
124 4287 : in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
125 4287 : if (in_fsp == NULL) {
126 0 : return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
127 : }
128 :
129 4287 : subreq = smbd_smb2_query_directory_send(req, req->sconn->ev_ctx,
130 : req, in_fsp,
131 : in_file_info_class,
132 : in_flags,
133 : in_file_index,
134 : in_output_buffer_length,
135 : in_file_name_string);
136 4287 : if (subreq == NULL) {
137 0 : return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
138 : }
139 4287 : tevent_req_set_callback(subreq, smbd_smb2_request_find_done, req);
140 :
141 4287 : return smbd_smb2_request_pending_queue(req, subreq, 500);
142 : }
143 :
144 4287 : static void smbd_smb2_request_find_done(struct tevent_req *subreq)
145 : {
146 4287 : struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
147 : struct smbd_smb2_request);
148 : DATA_BLOB outbody;
149 : DATA_BLOB outdyn;
150 : uint16_t out_output_buffer_offset;
151 4287 : DATA_BLOB out_output_buffer = data_blob_null;
152 : NTSTATUS status;
153 : NTSTATUS error; /* transport error */
154 :
155 4287 : status = smbd_smb2_query_directory_recv(subreq,
156 : req,
157 : &out_output_buffer);
158 4287 : TALLOC_FREE(subreq);
159 4287 : if (!NT_STATUS_IS_OK(status)) {
160 2158 : error = smbd_smb2_request_error(req, status);
161 2158 : if (!NT_STATUS_IS_OK(error)) {
162 0 : smbd_server_connection_terminate(req->xconn,
163 : nt_errstr(error));
164 455 : return;
165 : }
166 2158 : return;
167 : }
168 :
169 2129 : out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
170 :
171 2129 : outbody = smbd_smb2_generate_outbody(req, 0x08);
172 2129 : if (outbody.data == NULL) {
173 0 : error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
174 0 : if (!NT_STATUS_IS_OK(error)) {
175 0 : smbd_server_connection_terminate(req->xconn,
176 : nt_errstr(error));
177 0 : return;
178 : }
179 0 : return;
180 : }
181 :
182 2129 : SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
183 2129 : SSVAL(outbody.data, 0x02,
184 : out_output_buffer_offset); /* output buffer offset */
185 2129 : SIVAL(outbody.data, 0x04,
186 : out_output_buffer.length); /* output buffer length */
187 :
188 2129 : DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
189 : (unsigned int)out_output_buffer.length ));
190 :
191 2129 : outdyn = out_output_buffer;
192 :
193 2129 : error = smbd_smb2_request_done(req, outbody, &outdyn);
194 2129 : if (!NT_STATUS_IS_OK(error)) {
195 0 : smbd_server_connection_terminate(req->xconn,
196 : nt_errstr(error));
197 0 : return;
198 : }
199 : }
200 :
201 : static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
202 : struct tevent_context *ev,
203 : connection_struct *conn,
204 : struct file_id id,
205 : int info_level,
206 : char *entry_marshall_buf,
207 : bool *stop);
208 : static NTSTATUS fetch_write_time_recv(struct tevent_req *req);
209 :
210 : static struct tevent_req *fetch_dos_mode_send(
211 : TALLOC_CTX *mem_ctx,
212 : struct tevent_context *ev,
213 : struct files_struct *dir_fsp,
214 : struct smb_filename **smb_fname,
215 : uint32_t info_level,
216 : uint8_t *entry_marshall_buf);
217 :
218 : static NTSTATUS fetch_dos_mode_recv(struct tevent_req *req);
219 :
220 : struct smbd_smb2_query_directory_state {
221 : struct tevent_context *ev;
222 : struct smbd_smb2_request *smb2req;
223 : uint64_t async_sharemode_count;
224 : uint32_t find_async_delay_usec;
225 : DATA_BLOB out_output_buffer;
226 : struct smb_request *smbreq;
227 : int in_output_buffer_length;
228 : struct files_struct *fsp;
229 : const char *in_file_name;
230 : NTSTATUS empty_status;
231 : uint32_t info_level;
232 : uint32_t max_count;
233 : char *pdata;
234 : char *base_data;
235 : char *end_data;
236 : uint32_t num;
237 : uint32_t dirtype;
238 : bool dont_descend;
239 : bool ask_sharemode;
240 : bool async_dosmode;
241 : bool async_ask_sharemode;
242 : int last_entry_off;
243 : size_t max_async_dosmode_active;
244 : uint32_t async_dosmode_active;
245 : bool done;
246 : };
247 :
248 : static bool smb2_query_directory_next_entry(struct tevent_req *req);
249 : static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq);
250 : static void smb2_query_directory_dos_mode_done(struct tevent_req *subreq);
251 : static void smb2_query_directory_waited(struct tevent_req *subreq);
252 :
253 4287 : static struct tevent_req *smbd_smb2_query_directory_send(TALLOC_CTX *mem_ctx,
254 : struct tevent_context *ev,
255 : struct smbd_smb2_request *smb2req,
256 : struct files_struct *fsp,
257 : uint8_t in_file_info_class,
258 : uint8_t in_flags,
259 : uint32_t in_file_index,
260 : uint32_t in_output_buffer_length,
261 : const char *in_file_name)
262 : {
263 4287 : struct smbXsrv_connection *xconn = smb2req->xconn;
264 : struct tevent_req *req;
265 : struct smbd_smb2_query_directory_state *state;
266 4287 : connection_struct *conn = smb2req->tcon->compat;
267 3394 : const struct loadparm_substitution *lp_sub =
268 893 : loadparm_s3_global_substitution();
269 : NTSTATUS status;
270 4287 : bool wcard_has_wild = false;
271 : struct tm tm;
272 : char *p;
273 4287 : bool stop = false;
274 : bool ok;
275 4287 : bool posix_dir_handle = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN);
276 :
277 4287 : req = tevent_req_create(mem_ctx, &state,
278 : struct smbd_smb2_query_directory_state);
279 4287 : if (req == NULL) {
280 0 : return NULL;
281 : }
282 4287 : state->ev = ev;
283 4287 : state->fsp = fsp;
284 4287 : state->smb2req = smb2req;
285 4287 : state->in_output_buffer_length = in_output_buffer_length;
286 4287 : state->in_file_name = in_file_name;
287 4287 : state->out_output_buffer = data_blob_null;
288 4287 : state->dirtype = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY;
289 :
290 4287 : DEBUG(10,("smbd_smb2_query_directory_send: %s - %s\n",
291 : fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
292 :
293 4287 : state->smbreq = smbd_smb2_fake_smb_request(smb2req);
294 4287 : if (tevent_req_nomem(state->smbreq, req)) {
295 0 : return tevent_req_post(req, ev);
296 : }
297 :
298 4287 : if (!fsp->fsp_flags.is_directory) {
299 0 : tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
300 0 : return tevent_req_post(req, ev);
301 : }
302 :
303 4287 : if (strcmp(state->in_file_name, "") == 0) {
304 0 : tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
305 0 : return tevent_req_post(req, ev);
306 : }
307 4287 : if (strchr_m(state->in_file_name, '\\') != NULL) {
308 0 : tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
309 0 : return tevent_req_post(req, ev);
310 : }
311 4287 : if (strchr_m(state->in_file_name, '/') != NULL) {
312 0 : tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
313 0 : return tevent_req_post(req, ev);
314 : }
315 :
316 4287 : p = strptime(state->in_file_name, GMT_FORMAT, &tm);
317 4287 : if ((p != NULL) && (*p =='\0')) {
318 : /*
319 : * Bogus find that asks for a shadow copy timestamp as a
320 : * directory. The correct response is that it does not exist as
321 : * a directory.
322 : */
323 0 : tevent_req_nterror(req, NT_STATUS_NO_SUCH_FILE);
324 0 : return tevent_req_post(req, ev);
325 : }
326 :
327 4287 : if (in_output_buffer_length > xconn->smb2.server.max_trans) {
328 0 : DEBUG(2,("smbd_smb2_query_directory_send: "
329 : "client ignored max trans:%s: 0x%08X: 0x%08X\n",
330 : __location__, in_output_buffer_length,
331 : xconn->smb2.server.max_trans));
332 0 : tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
333 0 : return tevent_req_post(req, ev);
334 : }
335 :
336 4287 : status = smbd_smb2_request_verify_creditcharge(smb2req,
337 : in_output_buffer_length);
338 :
339 4287 : if (!NT_STATUS_IS_OK(status)) {
340 0 : tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
341 0 : return tevent_req_post(req, ev);
342 : }
343 :
344 4287 : switch (in_file_info_class) {
345 0 : case SMB2_FIND_DIRECTORY_INFO:
346 0 : state->info_level = SMB_FIND_FILE_DIRECTORY_INFO;
347 0 : break;
348 :
349 0 : case SMB2_FIND_FULL_DIRECTORY_INFO:
350 0 : state->info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
351 0 : break;
352 :
353 0 : case SMB2_FIND_BOTH_DIRECTORY_INFO:
354 0 : state->info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
355 0 : break;
356 :
357 7 : case SMB2_FIND_NAME_INFO:
358 7 : state->info_level = SMB_FIND_FILE_NAMES_INFO;
359 7 : break;
360 :
361 4280 : case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
362 4280 : state->info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
363 4280 : break;
364 :
365 0 : case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
366 0 : state->info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
367 0 : break;
368 :
369 0 : default:
370 0 : tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
371 0 : return tevent_req_post(req, ev);
372 : }
373 :
374 4287 : if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
375 0 : struct vfs_open_how how = { .flags = O_RDONLY, };
376 :
377 0 : status = fd_close(fsp);
378 0 : if (tevent_req_nterror(req, status)) {
379 0 : return tevent_req_post(req, ev);
380 : }
381 :
382 : /*
383 : * fd_close() will close and invalidate the fsp's file
384 : * descriptor. So we have to reopen it.
385 : */
386 :
387 : #ifdef O_DIRECTORY
388 0 : how.flags |= O_DIRECTORY;
389 : #endif
390 0 : status = fd_openat(conn->cwd_fsp, fsp->fsp_name, fsp, &how);
391 0 : if (tevent_req_nterror(req, status)) {
392 0 : return tevent_req_post(req, ev);
393 : }
394 : }
395 :
396 4287 : if (!state->smbreq->posix_pathnames) {
397 4287 : wcard_has_wild = ms_has_wild(state->in_file_name);
398 : }
399 :
400 : /* Ensure we've canonicalized any search path if not a wildcard. */
401 4287 : if (!wcard_has_wild) {
402 : /*
403 : * We still need to do the case processing
404 : * to save off the client-supplied last component.
405 : * At least we know there's no @GMT normalization
406 : * or MS-DFS paths to do in a directory mask.
407 : */
408 382 : state->in_file_name = get_original_lcomp(state,
409 : conn,
410 256 : state->in_file_name,
411 : 0);
412 256 : if (state->in_file_name == NULL) {
413 0 : tevent_req_oom(req);
414 0 : return tevent_req_post(req, ev);
415 : }
416 : }
417 :
418 4287 : if (fsp->dptr == NULL) {
419 5583 : status = dptr_create(conn,
420 : NULL, /* req */
421 : fsp,
422 : false, /* old_handle */
423 : false, /* expect_close */
424 : 0, /* spid */
425 2165 : state->in_file_name, /* wcard */
426 2165 : state->dirtype,
427 : &fsp->dptr);
428 2165 : if (!NT_STATUS_IS_OK(status)) {
429 0 : tevent_req_nterror(req, status);
430 0 : return tevent_req_post(req, ev);
431 : }
432 :
433 2165 : state->empty_status = NT_STATUS_NO_SUCH_FILE;
434 : } else {
435 2122 : state->empty_status = STATUS_NO_MORE_FILES;
436 : }
437 :
438 4287 : if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
439 0 : dptr_SeekDir(fsp->dptr, 0);
440 : }
441 :
442 4287 : if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
443 0 : state->max_count = 1;
444 : } else {
445 4287 : state->max_count = UINT16_MAX;
446 : }
447 :
448 : #define DIR_ENTRY_SAFETY_MARGIN 4096
449 :
450 4287 : state->out_output_buffer = data_blob_talloc(state, NULL,
451 : in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
452 4287 : if (tevent_req_nomem(state->out_output_buffer.data, req)) {
453 0 : return tevent_req_post(req, ev);
454 : }
455 :
456 4287 : state->out_output_buffer.length = 0;
457 4287 : state->pdata = (char *)state->out_output_buffer.data;
458 4287 : state->base_data = state->pdata;
459 : /*
460 : * end_data must include the safety margin as it's what is
461 : * used to determine if pushed strings have been truncated.
462 : */
463 4287 : state->end_data = state->pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
464 :
465 4287 : DEBUG(8,("smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
466 : "in_output_buffer_length = %u\n",
467 : fsp->fsp_name->base_name, lp_dont_descend(talloc_tos(), lp_sub, SNUM(conn)),
468 : (unsigned int)in_output_buffer_length ));
469 8574 : if (in_list(fsp->fsp_name->base_name,lp_dont_descend(talloc_tos(), lp_sub, SNUM(conn)),
470 4287 : posix_dir_handle ? true : conn->case_sensitive)) {
471 0 : state->dont_descend = true;
472 : }
473 :
474 : /*
475 : * SMB_FIND_FILE_NAMES_INFO doesn't need stat information
476 : *
477 : * This may change when we try to improve the delete on close
478 : * handling in future.
479 : */
480 4287 : if (state->info_level != SMB_FIND_FILE_NAMES_INFO) {
481 4280 : state->ask_sharemode = lp_smbd_search_ask_sharemode(SNUM(conn));
482 :
483 4280 : state->async_dosmode = lp_smbd_async_dosmode(SNUM(conn));
484 : }
485 :
486 4287 : if (state->ask_sharemode && lp_clustering()) {
487 0 : state->ask_sharemode = false;
488 0 : state->async_ask_sharemode = true;
489 : }
490 :
491 4287 : if (state->async_dosmode) {
492 : size_t max_threads;
493 :
494 0 : max_threads = pthreadpool_tevent_max_threads(conn->sconn->pool);
495 0 : if (max_threads == 0 || !per_thread_cwd_supported()) {
496 0 : state->async_dosmode = false;
497 : }
498 :
499 0 : state->max_async_dosmode_active = lp_smbd_max_async_dosmode(
500 0 : SNUM(conn));
501 0 : if (state->max_async_dosmode_active == 0) {
502 0 : state->max_async_dosmode_active = max_threads * 2;
503 : }
504 : }
505 :
506 4287 : if (state->async_dosmode || state->async_ask_sharemode) {
507 : /*
508 : * Should we only set async_internal
509 : * if we're not the last request in
510 : * a compound chain?
511 : */
512 0 : smb2_request_set_async_internal(smb2req, true);
513 : }
514 :
515 : /*
516 : * This gets set in autobuild for some tests
517 : */
518 4287 : state->find_async_delay_usec = lp_parm_ulong(SNUM(conn), "smbd",
519 : "find async delay usec",
520 : 0);
521 :
522 22311 : while (!stop) {
523 14630 : stop = smb2_query_directory_next_entry(req);
524 : }
525 :
526 4287 : if (!tevent_req_is_in_progress(req)) {
527 4287 : return tevent_req_post(req, ev);
528 : }
529 :
530 0 : ok = aio_add_req_to_fsp(fsp, req);
531 0 : if (!ok) {
532 0 : DBG_ERR("Could not add req to fsp\n");
533 0 : tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
534 0 : return tevent_req_post(req, ev);
535 : }
536 :
537 0 : return req;
538 : }
539 :
540 14630 : static bool smb2_query_directory_next_entry(struct tevent_req *req)
541 : {
542 14630 : struct smbd_smb2_query_directory_state *state = tevent_req_data(
543 : req, struct smbd_smb2_query_directory_state);
544 14630 : struct smb_filename *smb_fname = NULL; /* relative to fsp !! */
545 14630 : bool got_exact_match = false;
546 14630 : int off = state->out_output_buffer.length;
547 14630 : int space_remaining = state->in_output_buffer_length - off;
548 : struct file_id file_id;
549 : NTSTATUS status;
550 14630 : bool get_dosmode = !state->async_dosmode;
551 14630 : bool stop = false;
552 :
553 14630 : SMB_ASSERT(space_remaining >= 0);
554 :
555 78698 : status = smbd_dirptr_lanman2_entry(state,
556 14630 : state->fsp->conn,
557 14630 : state->fsp->dptr,
558 14630 : state->smbreq->flags2,
559 : state->in_file_name,
560 : state->dirtype,
561 14630 : state->info_level,
562 : false, /* requires_resume_key */
563 14630 : state->dont_descend,
564 14630 : state->ask_sharemode,
565 : get_dosmode,
566 : 8, /* align to 8 bytes */
567 : false, /* no padding */
568 : &state->pdata,
569 : state->base_data,
570 : state->end_data,
571 : space_remaining,
572 : &smb_fname,
573 : &got_exact_match,
574 : &state->last_entry_off,
575 : NULL,
576 : &file_id);
577 :
578 14630 : off = (int)PTR_DIFF(state->pdata, state->base_data);
579 :
580 14630 : if (!NT_STATUS_IS_OK(status)) {
581 4295 : if (NT_STATUS_EQUAL(status, NT_STATUS_ILLEGAL_CHARACTER)) {
582 : /*
583 : * Bad character conversion on name. Ignore this
584 : * entry.
585 : */
586 463 : return false;
587 4287 : } else if (state->num > 0) {
588 2129 : goto last_entry_done;
589 2158 : } else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
590 0 : tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
591 0 : return true;
592 : } else {
593 2158 : tevent_req_nterror(req, state->empty_status);
594 2158 : return true;
595 : }
596 : }
597 :
598 10335 : if (state->async_ask_sharemode &&
599 0 : !S_ISDIR(smb_fname->st.st_ex_mode))
600 : {
601 0 : struct tevent_req *subreq = NULL;
602 0 : char *buf = state->base_data + state->last_entry_off;
603 :
604 0 : subreq = fetch_write_time_send(state,
605 : state->ev,
606 0 : state->fsp->conn,
607 : file_id,
608 0 : state->info_level,
609 : buf,
610 : &stop);
611 0 : if (tevent_req_nomem(subreq, req)) {
612 0 : return true;
613 : }
614 0 : tevent_req_set_callback(
615 : subreq,
616 : smb2_query_directory_fetch_write_time_done,
617 : req);
618 0 : state->async_sharemode_count++;
619 : }
620 :
621 10335 : if (state->async_dosmode) {
622 0 : struct tevent_req *subreq = NULL;
623 0 : uint8_t *buf = NULL;
624 : size_t outstanding_aio;
625 :
626 0 : buf = (uint8_t *)state->base_data + state->last_entry_off;
627 :
628 0 : subreq = fetch_dos_mode_send(state,
629 : state->ev,
630 : state->fsp,
631 : &smb_fname,
632 : state->info_level,
633 : buf);
634 0 : if (tevent_req_nomem(subreq, req)) {
635 0 : return true;
636 : }
637 0 : tevent_req_set_callback(subreq,
638 : smb2_query_directory_dos_mode_done,
639 : req);
640 :
641 0 : state->async_dosmode_active++;
642 :
643 0 : outstanding_aio = pthreadpool_tevent_queued_jobs(
644 0 : state->fsp->conn->sconn->pool);
645 :
646 0 : if (outstanding_aio > state->max_async_dosmode_active) {
647 0 : stop = true;
648 : }
649 : }
650 :
651 10335 : TALLOC_FREE(smb_fname);
652 :
653 10335 : state->num++;
654 10335 : state->out_output_buffer.length = off;
655 :
656 10335 : if (!state->done && state->num < state->max_count) {
657 10335 : return stop;
658 : }
659 :
660 0 : last_entry_done:
661 2129 : SIVAL(state->out_output_buffer.data, state->last_entry_off, 0);
662 :
663 2129 : state->done = true;
664 :
665 2129 : if (state->async_sharemode_count > 0) {
666 0 : DBG_DEBUG("Stopping after %"PRIu64" async mtime "
667 : "updates\n", state->async_sharemode_count);
668 0 : return true;
669 : }
670 :
671 2129 : if (state->async_dosmode_active > 0) {
672 0 : return true;
673 : }
674 :
675 2129 : if (state->find_async_delay_usec > 0) {
676 : struct timeval tv;
677 0 : struct tevent_req *subreq = NULL;
678 :
679 : /*
680 : * Should we only set async_internal
681 : * if we're not the last request in
682 : * a compound chain?
683 : */
684 0 : smb2_request_set_async_internal(state->smb2req, true);
685 :
686 0 : tv = timeval_current_ofs(0, state->find_async_delay_usec);
687 :
688 0 : subreq = tevent_wakeup_send(state, state->ev, tv);
689 0 : if (tevent_req_nomem(subreq, req)) {
690 0 : return true;
691 : }
692 0 : tevent_req_set_callback(subreq,
693 : smb2_query_directory_waited,
694 : req);
695 0 : return true;
696 : }
697 :
698 2129 : tevent_req_done(req);
699 2129 : return true;
700 : }
701 :
702 : static void smb2_query_directory_check_next_entry(struct tevent_req *req);
703 :
704 0 : static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq)
705 : {
706 0 : struct tevent_req *req = tevent_req_callback_data(
707 : subreq, struct tevent_req);
708 0 : struct smbd_smb2_query_directory_state *state = tevent_req_data(
709 : req, struct smbd_smb2_query_directory_state);
710 : NTSTATUS status;
711 : bool ok;
712 :
713 : /*
714 : * Make sure we run as the user again
715 : */
716 0 : ok = change_to_user_and_service_by_fsp(state->fsp);
717 0 : SMB_ASSERT(ok);
718 :
719 0 : state->async_sharemode_count--;
720 :
721 0 : status = fetch_write_time_recv(subreq);
722 0 : TALLOC_FREE(subreq);
723 0 : if (tevent_req_nterror(req, status)) {
724 0 : return;
725 : }
726 :
727 0 : smb2_query_directory_check_next_entry(req);
728 0 : return;
729 : }
730 :
731 0 : static void smb2_query_directory_dos_mode_done(struct tevent_req *subreq)
732 : {
733 0 : struct tevent_req *req =
734 0 : tevent_req_callback_data(subreq,
735 : struct tevent_req);
736 0 : struct smbd_smb2_query_directory_state *state =
737 0 : tevent_req_data(req,
738 : struct smbd_smb2_query_directory_state);
739 : NTSTATUS status;
740 : bool ok;
741 :
742 : /*
743 : * Make sure we run as the user again
744 : */
745 0 : ok = change_to_user_and_service_by_fsp(state->fsp);
746 0 : SMB_ASSERT(ok);
747 :
748 0 : status = fetch_dos_mode_recv(subreq);
749 0 : TALLOC_FREE(subreq);
750 0 : if (tevent_req_nterror(req, status)) {
751 0 : return;
752 : }
753 :
754 0 : state->async_dosmode_active--;
755 :
756 0 : smb2_query_directory_check_next_entry(req);
757 0 : return;
758 : }
759 :
760 0 : static void smb2_query_directory_check_next_entry(struct tevent_req *req)
761 : {
762 0 : struct smbd_smb2_query_directory_state *state = tevent_req_data(
763 : req, struct smbd_smb2_query_directory_state);
764 0 : bool stop = false;
765 :
766 0 : if (!state->done) {
767 0 : while (!stop) {
768 0 : stop = smb2_query_directory_next_entry(req);
769 : }
770 0 : return;
771 : }
772 :
773 0 : if (state->async_sharemode_count > 0 ||
774 0 : state->async_dosmode_active > 0)
775 : {
776 0 : return;
777 : }
778 :
779 0 : if (state->find_async_delay_usec > 0) {
780 : struct timeval tv;
781 0 : struct tevent_req *subreq = NULL;
782 :
783 0 : tv = timeval_current_ofs(0, state->find_async_delay_usec);
784 :
785 0 : subreq = tevent_wakeup_send(state, state->ev, tv);
786 0 : if (tevent_req_nomem(subreq, req)) {
787 0 : tevent_req_post(req, state->ev);
788 0 : return;
789 : }
790 0 : tevent_req_set_callback(subreq,
791 : smb2_query_directory_waited,
792 : req);
793 0 : return;
794 : }
795 :
796 0 : tevent_req_done(req);
797 0 : return;
798 : }
799 :
800 0 : static void smb2_query_directory_waited(struct tevent_req *subreq)
801 : {
802 0 : struct tevent_req *req = tevent_req_callback_data(
803 : subreq, struct tevent_req);
804 : bool ok;
805 :
806 0 : ok = tevent_wakeup_recv(subreq);
807 0 : TALLOC_FREE(subreq);
808 0 : if (!ok) {
809 0 : tevent_req_oom(req);
810 0 : return;
811 : }
812 0 : tevent_req_done(req);
813 : }
814 :
815 4287 : static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
816 : TALLOC_CTX *mem_ctx,
817 : DATA_BLOB *out_output_buffer)
818 : {
819 : NTSTATUS status;
820 4287 : struct smbd_smb2_query_directory_state *state = tevent_req_data(req,
821 : struct smbd_smb2_query_directory_state);
822 :
823 4287 : if (tevent_req_is_nterror(req, &status)) {
824 2158 : tevent_req_received(req);
825 2158 : return status;
826 : }
827 :
828 2129 : *out_output_buffer = state->out_output_buffer;
829 2129 : talloc_steal(mem_ctx, out_output_buffer->data);
830 :
831 2129 : tevent_req_received(req);
832 2129 : return NT_STATUS_OK;
833 : }
834 :
835 : struct fetch_write_time_state {
836 : connection_struct *conn;
837 : struct file_id id;
838 : int info_level;
839 : char *entry_marshall_buf;
840 : };
841 :
842 : static void fetch_write_time_done(struct tevent_req *subreq);
843 :
844 0 : static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
845 : struct tevent_context *ev,
846 : connection_struct *conn,
847 : struct file_id id,
848 : int info_level,
849 : char *entry_marshall_buf,
850 : bool *stop)
851 : {
852 0 : struct tevent_req *req = NULL;
853 0 : struct fetch_write_time_state *state = NULL;
854 0 : struct tevent_req *subreq = NULL;
855 : bool req_queued;
856 :
857 0 : *stop = false;
858 :
859 0 : req = tevent_req_create(mem_ctx, &state, struct fetch_write_time_state);
860 0 : if (req == NULL) {
861 0 : return NULL;
862 : }
863 :
864 0 : *state = (struct fetch_write_time_state) {
865 : .conn = conn,
866 : .id = id,
867 : .info_level = info_level,
868 : .entry_marshall_buf = entry_marshall_buf,
869 : };
870 :
871 0 : subreq = fetch_share_mode_send(state, ev, id, &req_queued);
872 0 : if (tevent_req_nomem(subreq, req)) {
873 0 : return tevent_req_post(req, ev);
874 : }
875 0 : tevent_req_set_callback(subreq, fetch_write_time_done, req);
876 :
877 0 : if (req_queued) {
878 0 : *stop = true;
879 : }
880 0 : return req;
881 : }
882 :
883 0 : static void fetch_write_time_done(struct tevent_req *subreq)
884 : {
885 0 : struct tevent_req *req = tevent_req_callback_data(
886 : subreq, struct tevent_req);
887 0 : struct fetch_write_time_state *state = tevent_req_data(
888 : req, struct fetch_write_time_state);
889 : struct timespec write_time;
890 0 : struct share_mode_lock *lck = NULL;
891 : NTSTATUS status;
892 : size_t off;
893 :
894 0 : status = fetch_share_mode_recv(subreq, state, &lck);
895 0 : TALLOC_FREE(subreq);
896 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
897 0 : tevent_req_done(req);
898 0 : return;
899 : }
900 0 : if (!NT_STATUS_IS_OK(status)) {
901 0 : tevent_req_nterror(req, status);
902 0 : return;
903 : }
904 :
905 0 : write_time = get_share_mode_write_time(lck);
906 0 : TALLOC_FREE(lck);
907 :
908 0 : if (is_omit_timespec(&write_time)) {
909 0 : tevent_req_done(req);
910 0 : return;
911 : }
912 :
913 0 : switch (state->info_level) {
914 0 : case SMB_FIND_FILE_DIRECTORY_INFO:
915 : case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
916 : case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
917 : case SMB_FIND_ID_FULL_DIRECTORY_INFO:
918 : case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
919 0 : off = 24;
920 0 : break;
921 :
922 0 : default:
923 0 : DBG_ERR("Unsupported info_level [%d]\n", state->info_level);
924 0 : tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
925 0 : return;
926 : }
927 :
928 0 : put_long_date_full_timespec(state->conn->ts_res,
929 0 : state->entry_marshall_buf + off,
930 : &write_time);
931 :
932 0 : tevent_req_done(req);
933 0 : return;
934 : }
935 :
936 0 : static NTSTATUS fetch_write_time_recv(struct tevent_req *req)
937 : {
938 : NTSTATUS status;
939 :
940 0 : if (tevent_req_is_nterror(req, &status)) {
941 0 : tevent_req_received(req);
942 0 : return status;
943 : }
944 :
945 0 : tevent_req_received(req);
946 0 : return NT_STATUS_OK;
947 : }
948 :
949 : struct fetch_dos_mode_state {
950 : struct files_struct *dir_fsp;
951 : struct smb_filename *smb_fname;
952 : uint32_t info_level;
953 : uint8_t *entry_marshall_buf;
954 : };
955 :
956 : static void fetch_dos_mode_done(struct tevent_req *subreq);
957 :
958 0 : static struct tevent_req *fetch_dos_mode_send(
959 : TALLOC_CTX *mem_ctx,
960 : struct tevent_context *ev,
961 : struct files_struct *dir_fsp,
962 : struct smb_filename **smb_fname,
963 : uint32_t info_level,
964 : uint8_t *entry_marshall_buf)
965 : {
966 0 : struct tevent_req *req = NULL;
967 0 : struct fetch_dos_mode_state *state = NULL;
968 0 : struct tevent_req *subreq = NULL;
969 :
970 0 : req = tevent_req_create(mem_ctx, &state, struct fetch_dos_mode_state);
971 0 : if (req == NULL) {
972 0 : return NULL;
973 : }
974 0 : *state = (struct fetch_dos_mode_state) {
975 : .dir_fsp = dir_fsp,
976 : .info_level = info_level,
977 : .entry_marshall_buf = entry_marshall_buf,
978 : };
979 :
980 0 : state->smb_fname = talloc_move(state, smb_fname);
981 :
982 0 : subreq = dos_mode_at_send(state, ev, dir_fsp, state->smb_fname);
983 0 : if (tevent_req_nomem(subreq, req)) {
984 0 : return tevent_req_post(req, ev);
985 : }
986 0 : tevent_req_set_callback(subreq, fetch_dos_mode_done, req);
987 :
988 0 : return req;
989 : }
990 :
991 0 : static void fetch_dos_mode_done(struct tevent_req *subreq)
992 : {
993 0 : struct tevent_req *req =
994 0 : tevent_req_callback_data(subreq,
995 : struct tevent_req);
996 0 : struct fetch_dos_mode_state *state =
997 0 : tevent_req_data(req,
998 : struct fetch_dos_mode_state);
999 : uint32_t dfs_dosmode;
1000 : uint32_t dosmode;
1001 0 : struct timespec btime_ts = {0};
1002 : off_t dosmode_off;
1003 : off_t btime_off;
1004 : NTSTATUS status;
1005 :
1006 0 : status = dos_mode_at_recv(subreq, &dosmode);
1007 0 : TALLOC_FREE(subreq);
1008 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1009 0 : tevent_req_done(req);
1010 0 : return;
1011 : }
1012 0 : if (!NT_STATUS_IS_OK(status)) {
1013 0 : tevent_req_nterror(req, status);
1014 0 : return;
1015 : }
1016 :
1017 0 : switch (state->info_level) {
1018 0 : case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1019 : case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1020 : case SMB_FIND_FILE_DIRECTORY_INFO:
1021 : case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1022 : case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1023 0 : btime_off = 8;
1024 0 : dosmode_off = 56;
1025 0 : break;
1026 :
1027 0 : default:
1028 0 : DBG_ERR("Unsupported info_level [%u]\n", state->info_level);
1029 0 : tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
1030 0 : return;
1031 : }
1032 :
1033 :
1034 0 : dfs_dosmode = IVAL(state->entry_marshall_buf, dosmode_off);
1035 0 : if (dfs_dosmode == 0) {
1036 : /*
1037 : * DOS mode for a DFS link, only overwrite if still set to 0 and
1038 : * not already populated by the lower layer for a DFS link in
1039 : * smbd_dirptr_lanman2_mode_fn().
1040 : */
1041 0 : SIVAL(state->entry_marshall_buf, dosmode_off, dosmode);
1042 : }
1043 :
1044 0 : btime_ts = get_create_timespec(state->dir_fsp->conn,
1045 : NULL,
1046 0 : state->smb_fname);
1047 0 : if (lp_dos_filetime_resolution(SNUM(state->dir_fsp->conn))) {
1048 0 : dos_filetime_timespec(&btime_ts);
1049 : }
1050 :
1051 0 : put_long_date_full_timespec(state->dir_fsp->conn->ts_res,
1052 0 : (char *)state->entry_marshall_buf + btime_off,
1053 : &btime_ts);
1054 :
1055 0 : tevent_req_done(req);
1056 0 : return;
1057 : }
1058 :
1059 0 : static NTSTATUS fetch_dos_mode_recv(struct tevent_req *req)
1060 : {
1061 : NTSTATUS status;
1062 :
1063 0 : if (tevent_req_is_nterror(req, &status)) {
1064 0 : tevent_req_received(req);
1065 0 : return status;
1066 : }
1067 :
1068 0 : tevent_req_received(req);
1069 0 : return NT_STATUS_OK;
1070 : }
|