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_find_send(TALLOC_CTX
*mem_ctx
,
29 struct tevent_context
*ev
,
30 struct smbd_smb2_request
*smb2req
,
31 uint8_t in_file_info_class
,
33 uint32_t in_file_index
,
34 uint64_t in_file_id_volatile
,
35 uint32_t in_output_buffer_length
,
36 const char *in_file_name
);
37 static NTSTATUS
smbd_smb2_find_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_find(struct smbd_smb2_request
*req
)
45 const uint8_t *inbody
;
46 int i
= req
->current_idx
;
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 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
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
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
+ req
->in
.vector
[i
+1].iov_len
)) {
80 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
83 if (in_file_name_length
> req
->in
.vector
[i
+2].iov_len
) {
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
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
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 if (req
->compat_chain_fsp
) {
120 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
121 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
124 subreq
= smbd_smb2_find_send(req
,
131 in_output_buffer_length
,
132 in_file_name_string
);
133 if (subreq
== NULL
) {
134 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
136 tevent_req_set_callback(subreq
, smbd_smb2_request_find_done
, req
);
138 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
141 static void smbd_smb2_request_find_done(struct tevent_req
*subreq
)
143 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
144 struct smbd_smb2_request
);
147 uint16_t out_output_buffer_offset
;
148 DATA_BLOB out_output_buffer
= data_blob_null
;
150 NTSTATUS error
; /* transport error */
152 status
= smbd_smb2_find_recv(subreq
,
156 if (!NT_STATUS_IS_OK(status
)) {
157 error
= smbd_smb2_request_error(req
, status
);
158 if (!NT_STATUS_IS_OK(error
)) {
159 smbd_server_connection_terminate(req
->sconn
,
166 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
168 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
169 if (outbody
.data
== NULL
) {
170 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
171 if (!NT_STATUS_IS_OK(error
)) {
172 smbd_server_connection_terminate(req
->sconn
,
179 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
180 SSVAL(outbody
.data
, 0x02,
181 out_output_buffer_offset
); /* output buffer offset */
182 SIVAL(outbody
.data
, 0x04,
183 out_output_buffer
.length
); /* output buffer length */
185 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
186 (unsigned int)out_output_buffer
.length
));
188 outdyn
= out_output_buffer
;
190 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
191 if (!NT_STATUS_IS_OK(error
)) {
192 smbd_server_connection_terminate(req
->sconn
,
198 struct smbd_smb2_find_state
{
199 struct smbd_smb2_request
*smb2req
;
200 DATA_BLOB out_output_buffer
;
203 static struct tevent_req
*smbd_smb2_find_send(TALLOC_CTX
*mem_ctx
,
204 struct tevent_context
*ev
,
205 struct smbd_smb2_request
*smb2req
,
206 uint8_t in_file_info_class
,
208 uint32_t in_file_index
,
209 uint64_t in_file_id_volatile
,
210 uint32_t in_output_buffer_length
,
211 const char *in_file_name
)
213 struct tevent_req
*req
;
214 struct smbd_smb2_find_state
*state
;
215 struct smb_request
*smbreq
;
216 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
219 NTSTATUS empty_status
;
225 int last_entry_off
= 0;
228 uint32_t dirtype
= FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_DIRECTORY
;
229 bool dont_descend
= false;
230 bool ask_sharemode
= true;
232 req
= tevent_req_create(mem_ctx
, &state
,
233 struct smbd_smb2_find_state
);
237 state
->smb2req
= smb2req
;
238 state
->out_output_buffer
= data_blob_null
;
240 DEBUG(10,("smbd_smb2_find_send: file_id[0x%016llX]\n",
241 (unsigned long long)in_file_id_volatile
));
243 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
244 if (tevent_req_nomem(smbreq
, req
)) {
245 return tevent_req_post(req
, ev
);
248 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
250 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
251 return tevent_req_post(req
, ev
);
253 if (conn
!= fsp
->conn
) {
254 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
255 return tevent_req_post(req
, ev
);
257 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
258 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
259 return tevent_req_post(req
, ev
);
262 if (!fsp
->is_directory
) {
263 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
264 return tevent_req_post(req
, ev
);
267 if (strcmp(in_file_name
, "") == 0) {
268 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
269 return tevent_req_post(req
, ev
);
271 if (strcmp(in_file_name
, "\\") == 0) {
272 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
273 return tevent_req_post(req
, ev
);
275 if (strcmp(in_file_name
, "/") == 0) {
276 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
277 return tevent_req_post(req
, ev
);
280 if (in_output_buffer_length
> smb2req
->sconn
->smb2
.max_trans
) {
281 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
282 return tevent_req_post(req
, ev
);
285 status
= smbd_smb2_request_verify_creditcharge(smb2req
,
286 in_output_buffer_length
);
288 if (!NT_STATUS_IS_OK(status
)) {
289 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
290 return tevent_req_post(req
, ev
);
293 switch (in_file_info_class
) {
294 case SMB2_FIND_DIRECTORY_INFO
:
295 info_level
= SMB_FIND_FILE_DIRECTORY_INFO
;
298 case SMB2_FIND_FULL_DIRECTORY_INFO
:
299 info_level
= SMB_FIND_FILE_FULL_DIRECTORY_INFO
;
302 case SMB2_FIND_BOTH_DIRECTORY_INFO
:
303 info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
306 case SMB2_FIND_NAME_INFO
:
307 info_level
= SMB_FIND_FILE_NAMES_INFO
;
310 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO
:
311 info_level
= SMB_FIND_ID_BOTH_DIRECTORY_INFO
;
314 case SMB2_FIND_ID_FULL_DIRECTORY_INFO
:
315 info_level
= SMB_FIND_ID_FULL_DIRECTORY_INFO
;
319 tevent_req_nterror(req
, NT_STATUS_INVALID_INFO_CLASS
);
320 return tevent_req_post(req
, ev
);
323 if (in_flags
& SMB2_CONTINUE_FLAG_REOPEN
) {
327 if (fsp
->dptr
== NULL
) {
330 wcard_has_wild
= ms_has_wild(in_file_name
);
332 status
= dptr_create(conn
,
335 fsp
->fsp_name
->base_name
,
336 false, /* old_handle */
337 false, /* expect_close */
339 in_file_name
, /* wcard */
343 if (!NT_STATUS_IS_OK(status
)) {
344 tevent_req_nterror(req
, status
);
345 return tevent_req_post(req
, ev
);
348 empty_status
= NT_STATUS_NO_SUCH_FILE
;
350 empty_status
= STATUS_NO_MORE_FILES
;
353 if (in_flags
& SMB2_CONTINUE_FLAG_RESTART
) {
354 dptr_SeekDir(fsp
->dptr
, 0);
357 if (in_flags
& SMB2_CONTINUE_FLAG_SINGLE
) {
360 max_count
= UINT16_MAX
;
363 #define DIR_ENTRY_SAFETY_MARGIN 4096
365 state
->out_output_buffer
= data_blob_talloc(state
, NULL
,
366 in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
);
367 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
368 return tevent_req_post(req
, ev
);
371 state
->out_output_buffer
.length
= 0;
372 pdata
= (char *)state
->out_output_buffer
.data
;
375 * end_data must include the safety margin as it's what is
376 * used to determine if pushed strings have been truncated.
378 end_data
= pdata
+ in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
- 1;
383 DEBUG(8,("smbd_smb2_find_send: dirpath=<%s> dontdescend=<%s>, "
384 "in_output_buffer_length = %u\n",
385 fsp
->fsp_name
->base_name
, lp_dontdescend(SNUM(conn
)),
386 (unsigned int)in_output_buffer_length
));
387 if (in_list(fsp
->fsp_name
->base_name
,lp_dontdescend(SNUM(conn
)),
388 conn
->case_sensitive
)) {
392 ask_sharemode
= lp_parm_bool(SNUM(conn
),
393 "smbd", "search ask sharemode",
398 bool got_exact_match
= false;
399 bool out_of_space
= false;
400 int space_remaining
= in_output_buffer_length
- off
;
402 SMB_ASSERT(space_remaining
>= 0);
404 ok
= smbd_dirptr_lanman2_entry(state
,
411 false, /* requires_resume_key */
414 8, /* align to 8 bytes */
415 false, /* no padding */
425 off
= (int)PTR_DIFF(pdata
, base_data
);
429 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
430 tevent_req_done(req
);
431 return tevent_req_post(req
, ev
);
432 } else if (out_of_space
) {
433 tevent_req_nterror(req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
434 return tevent_req_post(req
, ev
);
436 tevent_req_nterror(req
, empty_status
);
437 return tevent_req_post(req
, ev
);
442 state
->out_output_buffer
.length
= off
;
444 if (num
< max_count
) {
448 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
449 tevent_req_done(req
);
450 return tevent_req_post(req
, ev
);
453 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
454 return tevent_req_post(req
, ev
);
457 static NTSTATUS
smbd_smb2_find_recv(struct tevent_req
*req
,
459 DATA_BLOB
*out_output_buffer
)
462 struct smbd_smb2_find_state
*state
= tevent_req_data(req
,
463 struct smbd_smb2_find_state
);
465 if (tevent_req_is_nterror(req
, &status
)) {
466 tevent_req_received(req
);
470 *out_output_buffer
= state
->out_output_buffer
;
471 talloc_steal(mem_ctx
, out_output_buffer
->data
);
473 tevent_req_received(req
);