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"
28 static struct tevent_req
*smbd_smb2_query_directory_send(TALLOC_CTX
*mem_ctx
,
29 struct tevent_context
*ev
,
30 struct smbd_smb2_request
*smb2req
,
31 struct files_struct
*in_fsp
,
32 uint8_t in_file_info_class
,
34 uint32_t in_file_index
,
35 uint32_t in_output_buffer_length
,
36 const char *in_file_name
);
37 static NTSTATUS
smbd_smb2_query_directory_recv(struct tevent_req
*req
,
39 DATA_BLOB
*out_output_buffer
);
41 static void smbd_smb2_request_find_done(struct tevent_req
*subreq
);
42 NTSTATUS
smbd_smb2_request_process_query_directory(struct smbd_smb2_request
*req
)
45 const uint8_t *inbody
;
46 uint8_t in_file_info_class
;
48 uint32_t in_file_index
;
49 uint64_t in_file_id_persistent
;
50 uint64_t in_file_id_volatile
;
51 struct files_struct
*in_fsp
;
52 uint16_t in_file_name_offset
;
53 uint16_t in_file_name_length
;
54 DATA_BLOB in_file_name_buffer
;
55 char *in_file_name_string
;
56 size_t in_file_name_string_size
;
57 uint32_t in_output_buffer_length
;
58 struct tevent_req
*subreq
;
61 status
= smbd_smb2_request_verify_sizes(req
, 0x21);
62 if (!NT_STATUS_IS_OK(status
)) {
63 return smbd_smb2_request_error(req
, status
);
65 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
67 in_file_info_class
= CVAL(inbody
, 0x02);
68 in_flags
= CVAL(inbody
, 0x03);
69 in_file_index
= IVAL(inbody
, 0x04);
70 in_file_id_persistent
= BVAL(inbody
, 0x08);
71 in_file_id_volatile
= BVAL(inbody
, 0x10);
72 in_file_name_offset
= SVAL(inbody
, 0x18);
73 in_file_name_length
= SVAL(inbody
, 0x1A);
74 in_output_buffer_length
= IVAL(inbody
, 0x1C);
76 if (in_file_name_offset
== 0 && in_file_name_length
== 0) {
78 } else if (in_file_name_offset
!=
79 (SMB2_HDR_BODY
+ SMBD_SMB2_IN_BODY_LEN(req
))) {
80 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
83 if (in_file_name_length
> SMBD_SMB2_IN_DYN_LEN(req
)) {
84 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
87 /* The output header is 8 bytes. */
88 if (in_output_buffer_length
<= 8) {
89 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
92 DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
93 (unsigned int)in_output_buffer_length
));
95 /* Take into account the output header. */
96 in_output_buffer_length
-= 8;
98 in_file_name_buffer
.data
= SMBD_SMB2_IN_DYN_PTR(req
);
99 in_file_name_buffer
.length
= in_file_name_length
;
101 ok
= convert_string_talloc(req
, CH_UTF16
, CH_UNIX
,
102 in_file_name_buffer
.data
,
103 in_file_name_buffer
.length
,
104 &in_file_name_string
,
105 &in_file_name_string_size
);
107 return smbd_smb2_request_error(req
, NT_STATUS_ILLEGAL_CHARACTER
);
110 if (in_file_name_buffer
.length
== 0) {
111 in_file_name_string_size
= 0;
114 if (strlen(in_file_name_string
) != in_file_name_string_size
) {
115 return smbd_smb2_request_error(req
, NT_STATUS_OBJECT_NAME_INVALID
);
118 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
, in_file_id_volatile
);
119 if (in_fsp
== NULL
) {
120 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
123 subreq
= smbd_smb2_query_directory_send(req
, req
->sconn
->ev_ctx
,
128 in_output_buffer_length
,
129 in_file_name_string
);
130 if (subreq
== NULL
) {
131 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
133 tevent_req_set_callback(subreq
, smbd_smb2_request_find_done
, req
);
135 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
138 static void smbd_smb2_request_find_done(struct tevent_req
*subreq
)
140 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
141 struct smbd_smb2_request
);
144 uint16_t out_output_buffer_offset
;
145 DATA_BLOB out_output_buffer
= data_blob_null
;
147 NTSTATUS error
; /* transport error */
149 status
= smbd_smb2_query_directory_recv(subreq
,
153 if (!NT_STATUS_IS_OK(status
)) {
154 error
= smbd_smb2_request_error(req
, status
);
155 if (!NT_STATUS_IS_OK(error
)) {
156 smbd_server_connection_terminate(req
->xconn
,
163 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
165 outbody
= smbd_smb2_generate_outbody(req
, 0x08);
166 if (outbody
.data
== NULL
) {
167 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
168 if (!NT_STATUS_IS_OK(error
)) {
169 smbd_server_connection_terminate(req
->xconn
,
176 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
177 SSVAL(outbody
.data
, 0x02,
178 out_output_buffer_offset
); /* output buffer offset */
179 SIVAL(outbody
.data
, 0x04,
180 out_output_buffer
.length
); /* output buffer length */
182 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
183 (unsigned int)out_output_buffer
.length
));
185 outdyn
= out_output_buffer
;
187 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
188 if (!NT_STATUS_IS_OK(error
)) {
189 smbd_server_connection_terminate(req
->xconn
,
195 struct smbd_smb2_query_directory_state
{
196 struct smbd_smb2_request
*smb2req
;
197 DATA_BLOB out_output_buffer
;
200 static struct tevent_req
*smbd_smb2_query_directory_send(TALLOC_CTX
*mem_ctx
,
201 struct tevent_context
*ev
,
202 struct smbd_smb2_request
*smb2req
,
203 struct files_struct
*fsp
,
204 uint8_t in_file_info_class
,
206 uint32_t in_file_index
,
207 uint32_t in_output_buffer_length
,
208 const char *in_file_name
)
210 struct smbXsrv_connection
*xconn
= smb2req
->xconn
;
211 struct tevent_req
*req
;
212 struct smbd_smb2_query_directory_state
*state
;
213 struct smb_request
*smbreq
;
214 connection_struct
*conn
= smb2req
->tcon
->compat
;
216 NTSTATUS empty_status
;
222 int last_entry_off
= 0;
225 uint32_t dirtype
= FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_DIRECTORY
;
226 bool dont_descend
= false;
227 bool ask_sharemode
= true;
228 bool wcard_has_wild
= false;
232 req
= tevent_req_create(mem_ctx
, &state
,
233 struct smbd_smb2_query_directory_state
);
237 state
->smb2req
= smb2req
;
238 state
->out_output_buffer
= data_blob_null
;
240 DEBUG(10,("smbd_smb2_query_directory_send: %s - %s\n",
241 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
243 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
244 if (tevent_req_nomem(smbreq
, req
)) {
245 return tevent_req_post(req
, ev
);
248 if (!fsp
->is_directory
) {
249 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
250 return tevent_req_post(req
, ev
);
253 if (strcmp(in_file_name
, "") == 0) {
254 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
255 return tevent_req_post(req
, ev
);
257 if (strchr_m(in_file_name
, '\\') != NULL
) {
258 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
259 return tevent_req_post(req
, ev
);
261 if (strchr_m(in_file_name
, '/') != NULL
) {
262 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
263 return tevent_req_post(req
, ev
);
266 p
= strptime(in_file_name
, GMT_FORMAT
, &tm
);
267 if ((p
!= NULL
) && (*p
=='\0')) {
269 * Bogus find that asks for a shadow copy timestamp as a
270 * directory. The correct response is that it does not exist as
273 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_FILE
);
274 return tevent_req_post(req
, ev
);
277 if (in_output_buffer_length
> xconn
->smb2
.server
.max_trans
) {
278 DEBUG(2,("smbd_smb2_query_directory_send: "
279 "client ignored max trans:%s: 0x%08X: 0x%08X\n",
280 __location__
, in_output_buffer_length
,
281 xconn
->smb2
.server
.max_trans
));
282 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
283 return tevent_req_post(req
, ev
);
286 status
= smbd_smb2_request_verify_creditcharge(smb2req
,
287 in_output_buffer_length
);
289 if (!NT_STATUS_IS_OK(status
)) {
290 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
291 return tevent_req_post(req
, ev
);
294 switch (in_file_info_class
) {
295 case SMB2_FIND_DIRECTORY_INFO
:
296 info_level
= SMB_FIND_FILE_DIRECTORY_INFO
;
299 case SMB2_FIND_FULL_DIRECTORY_INFO
:
300 info_level
= SMB_FIND_FILE_FULL_DIRECTORY_INFO
;
303 case SMB2_FIND_BOTH_DIRECTORY_INFO
:
304 info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
307 case SMB2_FIND_NAME_INFO
:
308 info_level
= SMB_FIND_FILE_NAMES_INFO
;
311 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO
:
312 info_level
= SMB_FIND_ID_BOTH_DIRECTORY_INFO
;
315 case SMB2_FIND_ID_FULL_DIRECTORY_INFO
:
316 info_level
= SMB_FIND_ID_FULL_DIRECTORY_INFO
;
320 tevent_req_nterror(req
, NT_STATUS_INVALID_INFO_CLASS
);
321 return tevent_req_post(req
, ev
);
324 if (in_flags
& SMB2_CONTINUE_FLAG_REOPEN
) {
328 if (!smbreq
->posix_pathnames
) {
329 wcard_has_wild
= ms_has_wild(in_file_name
);
332 /* Ensure we've canonicalized any search path if not a wildcard. */
333 if (!wcard_has_wild
) {
334 struct smb_filename
*smb_fname
= NULL
;
335 const char *fullpath
;
336 char tmpbuf
[PATH_MAX
];
337 char *to_free
= NULL
;
338 uint32_t ucf_flags
= UCF_SAVE_LCOMP
|
339 UCF_ALWAYS_ALLOW_WCARD_LCOMP
|
340 (smbreq
->posix_pathnames
?
341 UCF_POSIX_PATHNAMES
: 0);
343 if (ISDOT(fsp
->fsp_name
->base_name
)) {
344 fullpath
= in_file_name
;
350 fsp
->fsp_name
->base_name
, in_file_name
,
351 tmpbuf
, sizeof(tmpbuf
), &tmp
, &to_free
);
354 return tevent_req_post(req
, ev
);
358 status
= filename_convert(state
,
360 false, /* Not a DFS path. */
366 TALLOC_FREE(to_free
);
368 if (tevent_req_nterror(req
, status
)) {
369 return tevent_req_post(req
, ev
);
372 in_file_name
= smb_fname
->original_lcomp
;
375 if (fsp
->dptr
== NULL
) {
376 status
= dptr_create(conn
,
380 false, /* old_handle */
381 false, /* expect_close */
383 in_file_name
, /* wcard */
387 if (!NT_STATUS_IS_OK(status
)) {
388 tevent_req_nterror(req
, status
);
389 return tevent_req_post(req
, ev
);
392 empty_status
= NT_STATUS_NO_SUCH_FILE
;
394 empty_status
= STATUS_NO_MORE_FILES
;
397 if (in_flags
& SMB2_CONTINUE_FLAG_RESTART
) {
398 dptr_SeekDir(fsp
->dptr
, 0);
401 if (in_flags
& SMB2_CONTINUE_FLAG_SINGLE
) {
404 max_count
= UINT16_MAX
;
407 #define DIR_ENTRY_SAFETY_MARGIN 4096
409 state
->out_output_buffer
= data_blob_talloc(state
, NULL
,
410 in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
);
411 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
412 return tevent_req_post(req
, ev
);
415 state
->out_output_buffer
.length
= 0;
416 pdata
= (char *)state
->out_output_buffer
.data
;
419 * end_data must include the safety margin as it's what is
420 * used to determine if pushed strings have been truncated.
422 end_data
= pdata
+ in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
- 1;
427 DEBUG(8,("smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
428 "in_output_buffer_length = %u\n",
429 fsp
->fsp_name
->base_name
, lp_dont_descend(talloc_tos(), SNUM(conn
)),
430 (unsigned int)in_output_buffer_length
));
431 if (in_list(fsp
->fsp_name
->base_name
,lp_dont_descend(talloc_tos(), SNUM(conn
)),
432 conn
->case_sensitive
)) {
436 ask_sharemode
= lp_parm_bool(SNUM(conn
),
437 "smbd", "search ask sharemode",
441 bool got_exact_match
= false;
442 int space_remaining
= in_output_buffer_length
- off
;
444 SMB_ASSERT(space_remaining
>= 0);
446 status
= smbd_dirptr_lanman2_entry(state
,
453 false, /* requires_resume_key */
456 8, /* align to 8 bytes */
457 false, /* no padding */
466 off
= (int)PTR_DIFF(pdata
, base_data
);
468 if (!NT_STATUS_IS_OK(status
)) {
469 if (NT_STATUS_EQUAL(status
, NT_STATUS_ILLEGAL_CHARACTER
)) {
471 * Bad character conversion on name. Ignore this
475 } else if (num
> 0) {
476 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
477 tevent_req_done(req
);
478 return tevent_req_post(req
, ev
);
479 } else if (NT_STATUS_EQUAL(status
, STATUS_MORE_ENTRIES
)) {
480 tevent_req_nterror(req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
481 return tevent_req_post(req
, ev
);
483 tevent_req_nterror(req
, empty_status
);
484 return tevent_req_post(req
, ev
);
489 state
->out_output_buffer
.length
= off
;
491 if (num
< max_count
) {
495 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
496 tevent_req_done(req
);
497 return tevent_req_post(req
, ev
);
500 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
501 return tevent_req_post(req
, ev
);
504 static NTSTATUS
smbd_smb2_query_directory_recv(struct tevent_req
*req
,
506 DATA_BLOB
*out_output_buffer
)
509 struct smbd_smb2_query_directory_state
*state
= tevent_req_data(req
,
510 struct smbd_smb2_query_directory_state
);
512 if (tevent_req_is_nterror(req
, &status
)) {
513 tevent_req_received(req
);
517 *out_output_buffer
= state
->out_output_buffer
;
518 talloc_steal(mem_ctx
, out_output_buffer
->data
);
520 tevent_req_received(req
);