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 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_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 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
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
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
+ req
->in
.vector
[i
+1].iov_len
)) {
81 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
84 if (in_file_name_length
> req
->in
.vector
[i
+2].iov_len
) {
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
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
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
, false);
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_find_send(req
, req
->sconn
->smb2
.event_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
);
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
);
143 int i
= req
->current_idx
;
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 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
170 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
171 if (outbody
.data
== NULL
) {
172 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
173 if (!NT_STATUS_IS_OK(error
)) {
174 smbd_server_connection_terminate(req
->sconn
,
181 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
182 SSVAL(outbody
.data
, 0x02,
183 out_output_buffer_offset
); /* output buffer offset */
184 SIVAL(outbody
.data
, 0x04,
185 out_output_buffer
.length
); /* output buffer length */
187 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
188 (unsigned int)out_output_buffer
.length
));
190 outdyn
= out_output_buffer
;
192 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
193 if (!NT_STATUS_IS_OK(error
)) {
194 smbd_server_connection_terminate(req
->sconn
,
200 struct smbd_smb2_find_state
{
201 struct smbd_smb2_request
*smb2req
;
202 DATA_BLOB out_output_buffer
;
205 static struct tevent_req
*smbd_smb2_find_send(TALLOC_CTX
*mem_ctx
,
206 struct tevent_context
*ev
,
207 struct smbd_smb2_request
*smb2req
,
208 struct files_struct
*fsp
,
209 uint8_t in_file_info_class
,
211 uint32_t in_file_index
,
212 uint32_t in_output_buffer_length
,
213 const char *in_file_name
)
215 struct tevent_req
*req
;
216 struct smbd_smb2_find_state
*state
;
217 struct smb_request
*smbreq
;
218 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
220 NTSTATUS empty_status
;
226 int last_entry_off
= 0;
229 uint32_t dirtype
= FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_DIRECTORY
;
230 bool dont_descend
= false;
231 bool ask_sharemode
= true;
233 req
= tevent_req_create(mem_ctx
, &state
,
234 struct smbd_smb2_find_state
);
238 state
->smb2req
= smb2req
;
239 state
->out_output_buffer
= data_blob_null
;
241 DEBUG(10,("smbd_smb2_find_send: %s - fnum[%d]\n",
242 fsp_str_dbg(fsp
), fsp
->fnum
));
244 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
245 if (tevent_req_nomem(smbreq
, req
)) {
246 return tevent_req_post(req
, ev
);
249 if (!fsp
->is_directory
) {
250 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
251 return tevent_req_post(req
, ev
);
254 if (strcmp(in_file_name
, "") == 0) {
255 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
256 return tevent_req_post(req
, ev
);
258 if (strcmp(in_file_name
, "\\") == 0) {
259 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
260 return tevent_req_post(req
, ev
);
262 if (strcmp(in_file_name
, "/") == 0) {
263 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
264 return tevent_req_post(req
, ev
);
267 if (in_output_buffer_length
> smb2req
->sconn
->smb2
.max_trans
) {
268 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
269 return tevent_req_post(req
, ev
);
272 switch (in_file_info_class
) {
273 case SMB2_FIND_DIRECTORY_INFO
:
274 info_level
= SMB_FIND_FILE_DIRECTORY_INFO
;
277 case SMB2_FIND_FULL_DIRECTORY_INFO
:
278 info_level
= SMB_FIND_FILE_FULL_DIRECTORY_INFO
;
281 case SMB2_FIND_BOTH_DIRECTORY_INFO
:
282 info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
285 case SMB2_FIND_NAME_INFO
:
286 info_level
= SMB_FIND_FILE_NAMES_INFO
;
289 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO
:
290 info_level
= SMB_FIND_ID_BOTH_DIRECTORY_INFO
;
293 case SMB2_FIND_ID_FULL_DIRECTORY_INFO
:
294 info_level
= SMB_FIND_ID_FULL_DIRECTORY_INFO
;
298 tevent_req_nterror(req
, NT_STATUS_INVALID_INFO_CLASS
);
299 return tevent_req_post(req
, ev
);
302 if (in_flags
& SMB2_CONTINUE_FLAG_REOPEN
) {
306 if (fsp
->dptr
== NULL
) {
309 if (!(fsp
->access_mask
& SEC_DIR_LIST
)) {
310 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
311 return tevent_req_post(req
, ev
);
314 wcard_has_wild
= ms_has_wild(in_file_name
);
316 status
= dptr_create(conn
,
318 fsp
->fsp_name
->base_name
,
319 false, /* old_handle */
320 false, /* expect_close */
322 in_file_name
, /* wcard */
326 if (!NT_STATUS_IS_OK(status
)) {
327 tevent_req_nterror(req
, status
);
328 return tevent_req_post(req
, ev
);
331 empty_status
= NT_STATUS_NO_SUCH_FILE
;
333 empty_status
= STATUS_NO_MORE_FILES
;
336 if (in_flags
& SMB2_CONTINUE_FLAG_RESTART
) {
337 dptr_SeekDir(fsp
->dptr
, 0);
340 if (in_flags
& SMB2_CONTINUE_FLAG_SINGLE
) {
343 max_count
= UINT16_MAX
;
346 #define DIR_ENTRY_SAFETY_MARGIN 4096
348 state
->out_output_buffer
= data_blob_talloc(state
, NULL
,
349 in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
);
350 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
351 return tevent_req_post(req
, ev
);
354 state
->out_output_buffer
.length
= 0;
355 pdata
= (char *)state
->out_output_buffer
.data
;
358 * end_data must include the safety margin as it's what is
359 * used to determine if pushed strings have been truncated.
361 end_data
= pdata
+ in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
- 1;
366 DEBUG(8,("smbd_smb2_find_send: dirpath=<%s> dontdescend=<%s>, "
367 "in_output_buffer_length = %u\n",
368 fsp
->fsp_name
->base_name
, lp_dontdescend(SNUM(conn
)),
369 (unsigned int)in_output_buffer_length
));
370 if (in_list(fsp
->fsp_name
->base_name
,lp_dontdescend(SNUM(conn
)),
371 conn
->case_sensitive
)) {
375 ask_sharemode
= lp_parm_bool(SNUM(conn
),
376 "smbd", "search ask sharemode",
381 bool got_exact_match
= false;
382 bool out_of_space
= false;
383 int space_remaining
= in_output_buffer_length
- off
;
385 SMB_ASSERT(space_remaining
>= 0);
387 ok
= smbd_dirptr_lanman2_entry(state
,
394 false, /* requires_resume_key */
397 8, /* align to 8 bytes */
398 false, /* no padding */
408 off
= (int)PTR_DIFF(pdata
, base_data
);
412 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
413 tevent_req_done(req
);
414 return tevent_req_post(req
, ev
);
415 } else if (out_of_space
) {
416 tevent_req_nterror(req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
417 return tevent_req_post(req
, ev
);
419 tevent_req_nterror(req
, empty_status
);
420 return tevent_req_post(req
, ev
);
425 state
->out_output_buffer
.length
= off
;
427 if (num
< max_count
) {
431 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
432 tevent_req_done(req
);
433 return tevent_req_post(req
, ev
);
436 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
437 return tevent_req_post(req
, ev
);
440 static NTSTATUS
smbd_smb2_find_recv(struct tevent_req
*req
,
442 DATA_BLOB
*out_output_buffer
)
445 struct smbd_smb2_find_state
*state
= tevent_req_data(req
,
446 struct smbd_smb2_find_state
);
448 if (tevent_req_is_nterror(req
, &status
)) {
449 tevent_req_received(req
);
453 *out_output_buffer
= state
->out_output_buffer
;
454 talloc_steal(mem_ctx
, out_output_buffer
->data
);
456 tevent_req_received(req
);