2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
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.
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.
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/>.
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
26 #include "../lib/util/tevent_ntstatus.h"
27 #include "system/filesys.h"
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,
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,
40 DATA_BLOB *out_output_buffer);
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)
46 const uint8_t *inbody;
47 uint8_t in_file_info_class;
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;
62 status = smbd_smb2_request_verify_sizes(req, 0x21);
63 if (!NT_STATUS_IS_OK(status)) {
64 return smbd_smb2_request_error(req, status);
66 inbody = SMBD_SMB2_IN_BODY_PTR(req);
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);
77 if (in_file_name_offset == 0 && in_file_name_length == 0) {
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);
84 if (in_file_name_length > SMBD_SMB2_IN_DYN_LEN(req)) {
85 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
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);
93 DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
94 (unsigned int)in_output_buffer_length ));
96 /* Take into account the output header. */
97 in_output_buffer_length -= 8;
99 in_file_name_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
100 in_file_name_buffer.length = in_file_name_length;
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);
108 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
111 if (in_file_name_buffer.length == 0) {
112 in_file_name_string_size = 0;
115 if (strlen(in_file_name_string) != in_file_name_string_size) {
116 return smbd_smb2_request_error(req, NT_STATUS_OBJECT_NAME_INVALID);
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);
124 subreq = smbd_smb2_query_directory_send(req, req->sconn->ev_ctx,
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);
134 tevent_req_set_callback(subreq, smbd_smb2_request_find_done, req);
136 return smbd_smb2_request_pending_queue(req, subreq, 500);
139 static void smbd_smb2_request_find_done(struct tevent_req *subreq)
141 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
142 struct smbd_smb2_request);
145 uint16_t out_output_buffer_offset;
146 DATA_BLOB out_output_buffer = data_blob_null;
148 NTSTATUS error; /* transport error */
150 status = smbd_smb2_query_directory_recv(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,
164 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
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,
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 */
183 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
184 (unsigned int)out_output_buffer.length ));
186 outdyn = out_output_buffer;
188 error = smbd_smb2_request_done(req, outbody, &outdyn);
189 if (!NT_STATUS_IS_OK(error)) {
190 smbd_server_connection_terminate(req->xconn,
196 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
197 struct tevent_context *ev,
198 connection_struct *conn,
201 char *entry_marshall_buf,
203 static NTSTATUS fetch_write_time_recv(struct tevent_req *req);
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;
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);
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,
223 uint32_t in_file_index,
224 uint32_t in_output_buffer_length,
225 const char *in_file_name)
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;
233 NTSTATUS empty_status;
239 int last_entry_off = 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;
250 req = tevent_req_create(mem_ctx, &state,
251 struct smbd_smb2_query_directory_state);
256 state->smb2req = smb2req;
257 state->out_output_buffer = data_blob_null;
259 DEBUG(10,("smbd_smb2_query_directory_send: %s - %s\n",
260 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
262 smbreq = smbd_smb2_fake_smb_request(smb2req);
263 if (tevent_req_nomem(smbreq, req)) {
264 return tevent_req_post(req, ev);
267 if (!fsp->is_directory) {
268 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
269 return tevent_req_post(req, ev);
272 if (strcmp(in_file_name, "") == 0) {
273 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
274 return tevent_req_post(req, ev);
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);
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);
285 p = strptime(in_file_name, GMT_FORMAT, &tm);
286 if ((p != NULL) && (*p =='\0')) {
288 * Bogus find that asks for a shadow copy timestamp as a
289 * directory. The correct response is that it does not exist as
292 tevent_req_nterror(req, NT_STATUS_NO_SUCH_FILE);
293 return tevent_req_post(req, ev);
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);
305 status = smbd_smb2_request_verify_creditcharge(smb2req,
306 in_output_buffer_length);
308 if (!NT_STATUS_IS_OK(status)) {
309 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
310 return tevent_req_post(req, ev);
313 switch (in_file_info_class) {
314 case SMB2_FIND_DIRECTORY_INFO:
315 info_level = SMB_FIND_FILE_DIRECTORY_INFO;
318 case SMB2_FIND_FULL_DIRECTORY_INFO:
319 info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
322 case SMB2_FIND_BOTH_DIRECTORY_INFO:
323 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
326 case SMB2_FIND_NAME_INFO:
327 info_level = SMB_FIND_FILE_NAMES_INFO;
330 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
331 info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
334 case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
335 info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
339 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
340 return tevent_req_post(req, ev);
343 if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
349 * dptr_CloseDir() will close and invalidate the fsp's file
350 * descriptor, we have to reopen it.
355 flags |= O_DIRECTORY;
357 status = fd_open(conn, fsp, flags, 0);
358 if (tevent_req_nterror(req, status)) {
359 return tevent_req_post(req, ev);
363 if (!smbreq->posix_pathnames) {
364 wcard_has_wild = ms_has_wild(in_file_name);
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);
378 if (ISDOT(fsp->fsp_name->base_name)) {
379 fullpath = in_file_name;
385 fsp->fsp_name->base_name, in_file_name,
386 tmpbuf, sizeof(tmpbuf), &tmp, &to_free);
389 return tevent_req_post(req, ev);
393 status = filename_convert(state,
395 false, /* Not a DFS path. */
401 TALLOC_FREE(to_free);
403 if (tevent_req_nterror(req, status)) {
404 return tevent_req_post(req, ev);
407 in_file_name = smb_fname->original_lcomp;
410 if (fsp->dptr == NULL) {
411 status = dptr_create(conn,
415 false, /* old_handle */
416 false, /* expect_close */
418 in_file_name, /* wcard */
422 if (!NT_STATUS_IS_OK(status)) {
423 tevent_req_nterror(req, status);
424 return tevent_req_post(req, ev);
427 empty_status = NT_STATUS_NO_SUCH_FILE;
429 empty_status = STATUS_NO_MORE_FILES;
432 if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
433 dptr_SeekDir(fsp->dptr, 0);
436 if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
439 max_count = UINT16_MAX;
442 #define DIR_ENTRY_SAFETY_MARGIN 4096
444 state->out_output_buffer = data_blob_talloc(state, NULL,
445 in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
446 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
447 return tevent_req_post(req, ev);
450 state->out_output_buffer.length = 0;
451 pdata = (char *)state->out_output_buffer.data;
454 * end_data must include the safety margin as it's what is
455 * used to determine if pushed strings have been truncated.
457 end_data = pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
462 DEBUG(8,("smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
463 "in_output_buffer_length = %u\n",
464 fsp->fsp_name->base_name, lp_dont_descend(talloc_tos(), SNUM(conn)),
465 (unsigned int)in_output_buffer_length ));
466 if (in_list(fsp->fsp_name->base_name,lp_dont_descend(talloc_tos(), SNUM(conn)),
467 conn->case_sensitive)) {
472 * SMB_FIND_FILE_NAMES_INFO doesn't need stat information
474 * This may change when we try to improve the delete on close
475 * handling in future.
477 if (info_level != SMB_FIND_FILE_NAMES_INFO) {
478 ask_sharemode = lp_parm_bool(SNUM(conn),
479 "smbd", "search ask sharemode",
483 if (ask_sharemode && lp_clustering()) {
484 ask_sharemode = false;
485 async_ask_sharemode = true;
488 * Should we only set async_internal
489 * if we're not the last request in
492 smb2_request_set_async_internal(smb2req, true);
496 * This gets set in autobuild for some tests
498 state->find_async_delay_usec = lp_parm_ulong(SNUM(conn), "smbd",
499 "find async delay usec",
503 bool got_exact_match = false;
504 int space_remaining = in_output_buffer_length - off;
506 struct file_id file_id;
509 SMB_ASSERT(space_remaining >= 0);
511 status = smbd_dirptr_lanman2_entry(state,
518 false, /* requires_resume_key */
521 8, /* align to 8 bytes */
522 false, /* no padding */
532 off = (int)PTR_DIFF(pdata, base_data);
534 if (!NT_STATUS_IS_OK(status)) {
535 if (NT_STATUS_EQUAL(status, NT_STATUS_ILLEGAL_CHARACTER)) {
537 * Bad character conversion on name. Ignore this
541 } else if (num > 0) {
542 goto last_entry_done;
543 } else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
544 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
545 return tevent_req_post(req, ev);
547 tevent_req_nterror(req, empty_status);
548 return tevent_req_post(req, ev);
552 if (async_ask_sharemode) {
553 struct tevent_req *subreq = NULL;
555 subreq = fetch_write_time_send(req,
562 if (tevent_req_nomem(subreq, req)) {
563 return tevent_req_post(req, ev);
565 tevent_req_set_callback(
567 smb2_query_directory_fetch_write_time_done,
570 state->async_count++;
574 state->out_output_buffer.length = off;
576 if (num >= max_count) {
585 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
586 if (state->async_count > 0) {
587 DBG_DEBUG("Stopping after %zu async mtime updates\n",
592 if (state->find_async_delay_usec > 0) {
594 struct tevent_req *subreq = NULL;
597 * Should we only set async_internal
598 * if we're not the last request in
601 smb2_request_set_async_internal(smb2req, true);
603 tv = timeval_current_ofs(0, state->find_async_delay_usec);
605 subreq = tevent_wakeup_send(state, ev, tv);
606 if (tevent_req_nomem(subreq, req)) {
607 return tevent_req_post(req, ev);
609 tevent_req_set_callback(subreq,
610 smb2_query_directory_waited,
615 tevent_req_done(req);
616 return tevent_req_post(req, ev);
619 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
620 return tevent_req_post(req, ev);
623 static void smb2_query_directory_fetch_write_time_done(struct tevent_req *subreq)
625 struct tevent_req *req = tevent_req_callback_data(
626 subreq, struct tevent_req);
627 struct smbd_smb2_query_directory_state *state = tevent_req_data(
628 req, struct smbd_smb2_query_directory_state);
631 state->async_count--;
633 status = fetch_write_time_recv(subreq);
635 if (tevent_req_nterror(req, status)) {
639 if (state->async_count > 0) {
643 if (state->find_async_delay_usec > 0) {
646 tv = timeval_current_ofs(0, state->find_async_delay_usec);
648 subreq = tevent_wakeup_send(state, state->ev, tv);
649 if (tevent_req_nomem(subreq, req)) {
650 tevent_req_post(req, state->ev);
653 tevent_req_set_callback(subreq,
654 smb2_query_directory_waited,
659 tevent_req_done(req);
663 static void smb2_query_directory_waited(struct tevent_req *subreq)
665 struct tevent_req *req = tevent_req_callback_data(
666 subreq, struct tevent_req);
669 ok = tevent_wakeup_recv(subreq);
675 tevent_req_done(req);
678 static NTSTATUS smbd_smb2_query_directory_recv(struct tevent_req *req,
680 DATA_BLOB *out_output_buffer)
683 struct smbd_smb2_query_directory_state *state = tevent_req_data(req,
684 struct smbd_smb2_query_directory_state);
686 if (tevent_req_is_nterror(req, &status)) {
687 tevent_req_received(req);
691 *out_output_buffer = state->out_output_buffer;
692 talloc_steal(mem_ctx, out_output_buffer->data);
694 tevent_req_received(req);
698 struct fetch_write_time_state {
699 connection_struct *conn;
702 char *entry_marshall_buf;
705 static void fetch_write_time_done(struct tevent_req *subreq);
707 static struct tevent_req *fetch_write_time_send(TALLOC_CTX *mem_ctx,
708 struct tevent_context *ev,
709 connection_struct *conn,
712 char *entry_marshall_buf,
715 struct tevent_req *req = NULL;
716 struct fetch_write_time_state *state = NULL;
717 struct tevent_req *subreq = NULL;
722 req = tevent_req_create(mem_ctx, &state, struct fetch_write_time_state);
727 *state = (struct fetch_write_time_state) {
730 .info_level = info_level,
731 .entry_marshall_buf = entry_marshall_buf,
734 subreq = fetch_share_mode_send(state, ev, id, &req_queued);
735 if (tevent_req_nomem(subreq, req)) {
736 return tevent_req_post(req, ev);
738 tevent_req_set_callback(subreq, fetch_write_time_done, req);
746 static void fetch_write_time_done(struct tevent_req *subreq)
748 struct tevent_req *req = tevent_req_callback_data(
749 subreq, struct tevent_req);
750 struct fetch_write_time_state *state = tevent_req_data(
751 req, struct fetch_write_time_state);
752 struct timespec write_time;
753 struct share_mode_lock *lck = NULL;
757 status = fetch_share_mode_recv(subreq, state, &lck);
759 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
760 tevent_req_done(req);
763 if (!NT_STATUS_IS_OK(status)) {
764 tevent_req_nterror(req, status);
768 write_time = get_share_mode_write_time(lck);
771 if (null_timespec(write_time)) {
772 tevent_req_done(req);
776 switch (state->info_level) {
777 case SMB_FIND_FILE_DIRECTORY_INFO:
778 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
779 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
780 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
781 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
786 DBG_ERR("Unsupported info_level [%d]\n", state->info_level);
787 tevent_req_nterror(req, NT_STATUS_INVALID_LEVEL);
791 put_long_date_timespec(state->conn->ts_res,
792 state->entry_marshall_buf + off,
795 tevent_req_done(req);
799 static NTSTATUS fetch_write_time_recv(struct tevent_req *req)
803 if (tevent_req_is_nterror(req, &status)) {
804 tevent_req_received(req);
808 tevent_req_received(req);