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"
27 static struct tevent_req
*smbd_smb2_find_send(TALLOC_CTX
*mem_ctx
,
28 struct tevent_context
*ev
,
29 struct smbd_smb2_request
*smb2req
,
30 uint8_t in_file_info_class
,
32 uint32_t in_file_index
,
33 uint64_t in_file_id_volatile
,
34 uint32_t in_output_buffer_length
,
35 const char *in_file_name
);
36 static NTSTATUS
smbd_smb2_find_recv(struct tevent_req
*req
,
38 DATA_BLOB
*out_output_buffer
);
40 static void smbd_smb2_request_find_done(struct tevent_req
*subreq
);
41 NTSTATUS
smbd_smb2_request_process_find(struct smbd_smb2_request
*req
)
44 const uint8_t *inbody
;
45 int i
= req
->current_idx
;
46 size_t expected_body_size
= 0x21;
48 uint8_t in_file_info_class
;
50 uint32_t in_file_index
;
51 uint64_t in_file_id_persistent
;
52 uint64_t in_file_id_volatile
;
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 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
63 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
64 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
67 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
69 body_size
= SVAL(inbody
, 0x00);
70 if (body_size
!= expected_body_size
) {
71 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
74 in_file_info_class
= CVAL(inbody
, 0x02);
75 in_flags
= CVAL(inbody
, 0x03);
76 in_file_index
= IVAL(inbody
, 0x04);
77 in_file_id_persistent
= BVAL(inbody
, 0x08);
78 in_file_id_volatile
= BVAL(inbody
, 0x10);
79 in_file_name_offset
= SVAL(inbody
, 0x18);
80 in_file_name_length
= SVAL(inbody
, 0x1A);
81 in_output_buffer_length
= IVAL(inbody
, 0x1C);
83 if (in_file_name_offset
== 0 && in_file_name_length
== 0) {
85 } else if (in_file_name_offset
!=
86 (SMB2_HDR_BODY
+ (body_size
& 0xFFFFFFFE))) {
87 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
90 if (in_file_name_length
> req
->in
.vector
[i
+2].iov_len
) {
91 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
94 /* The output header is 8 bytes. */
95 if (in_output_buffer_length
<= 8) {
96 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
99 DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
100 (unsigned int)in_output_buffer_length
));
102 /* Take into account the output header. */
103 in_output_buffer_length
-= 8;
105 in_file_name_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
106 in_file_name_buffer
.length
= in_file_name_length
;
108 ok
= convert_string_talloc(req
, CH_UTF16
, CH_UNIX
,
109 in_file_name_buffer
.data
,
110 in_file_name_buffer
.length
,
111 &in_file_name_string
,
112 &in_file_name_string_size
, false);
114 return smbd_smb2_request_error(req
, NT_STATUS_ILLEGAL_CHARACTER
);
117 if (req
->compat_chain_fsp
) {
119 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
120 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
123 subreq
= smbd_smb2_find_send(req
,
124 req
->sconn
->smb2
.event_ctx
,
130 in_output_buffer_length
,
131 in_file_name_string
);
132 if (subreq
== NULL
) {
133 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
135 tevent_req_set_callback(subreq
, smbd_smb2_request_find_done
, req
);
137 return smbd_smb2_request_pending_queue(req
, subreq
);
140 static void smbd_smb2_request_find_done(struct tevent_req
*subreq
)
142 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
143 struct smbd_smb2_request
);
144 int i
= req
->current_idx
;
148 uint16_t out_output_buffer_offset
;
149 DATA_BLOB out_output_buffer
= data_blob_null
;
151 NTSTATUS error
; /* transport error */
153 status
= smbd_smb2_find_recv(subreq
,
157 if (!NT_STATUS_IS_OK(status
)) {
158 error
= smbd_smb2_request_error(req
, status
);
159 if (!NT_STATUS_IS_OK(error
)) {
160 smbd_server_connection_terminate(req
->sconn
,
167 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
169 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
171 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
172 if (outbody
.data
== NULL
) {
173 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
174 if (!NT_STATUS_IS_OK(error
)) {
175 smbd_server_connection_terminate(req
->sconn
,
182 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
183 SSVAL(outbody
.data
, 0x02,
184 out_output_buffer_offset
); /* output buffer offset */
185 SIVAL(outbody
.data
, 0x04,
186 out_output_buffer
.length
); /* output buffer length */
188 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
189 (unsigned int)out_output_buffer
.length
));
191 outdyn
= out_output_buffer
;
193 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
194 if (!NT_STATUS_IS_OK(error
)) {
195 smbd_server_connection_terminate(req
->sconn
,
201 struct smbd_smb2_find_state
{
202 struct smbd_smb2_request
*smb2req
;
203 DATA_BLOB out_output_buffer
;
206 static struct tevent_req
*smbd_smb2_find_send(TALLOC_CTX
*mem_ctx
,
207 struct tevent_context
*ev
,
208 struct smbd_smb2_request
*smb2req
,
209 uint8_t in_file_info_class
,
211 uint32_t in_file_index
,
212 uint64_t in_file_id_volatile
,
213 uint32_t in_output_buffer_length
,
214 const char *in_file_name
)
216 struct tevent_req
*req
;
217 struct smbd_smb2_find_state
*state
;
218 struct smb_request
*smbreq
;
219 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
222 NTSTATUS empty_status
;
228 int last_entry_off
= 0;
231 uint32_t dirtype
= aHIDDEN
| aSYSTEM
| aDIR
;
232 bool dont_descend
= false;
233 bool ask_sharemode
= true;
235 req
= tevent_req_create(mem_ctx
, &state
,
236 struct smbd_smb2_find_state
);
240 state
->smb2req
= smb2req
;
241 state
->out_output_buffer
= data_blob_null
;
243 DEBUG(10,("smbd_smb2_find_send: file_id[0x%016llX]\n",
244 (unsigned long long)in_file_id_volatile
));
246 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
247 if (tevent_req_nomem(smbreq
, req
)) {
248 return tevent_req_post(req
, ev
);
251 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
253 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
254 return tevent_req_post(req
, ev
);
256 if (conn
!= fsp
->conn
) {
257 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
258 return tevent_req_post(req
, ev
);
260 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
261 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
262 return tevent_req_post(req
, ev
);
265 if (!fsp
->is_directory
) {
266 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
267 return tevent_req_post(req
, ev
);
270 if (strcmp(in_file_name
, "") == 0) {
271 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
272 return tevent_req_post(req
, ev
);
274 if (strcmp(in_file_name
, "\\") == 0) {
275 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
276 return tevent_req_post(req
, ev
);
278 if (strcmp(in_file_name
, "/") == 0) {
279 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
280 return tevent_req_post(req
, ev
);
283 if (in_output_buffer_length
> 0x10000) {
284 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
285 return tevent_req_post(req
, ev
);
288 switch (in_file_info_class
) {
289 case SMB2_FIND_DIRECTORY_INFO
:
290 info_level
= SMB_FIND_FILE_DIRECTORY_INFO
;
293 case SMB2_FIND_FULL_DIRECTORY_INFO
:
294 info_level
= SMB_FIND_FILE_FULL_DIRECTORY_INFO
;
297 case SMB2_FIND_BOTH_DIRECTORY_INFO
:
298 info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
301 case SMB2_FIND_NAME_INFO
:
302 info_level
= SMB_FIND_FILE_NAMES_INFO
;
305 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO
:
306 info_level
= SMB_FIND_ID_BOTH_DIRECTORY_INFO
;
309 case SMB2_FIND_ID_FULL_DIRECTORY_INFO
:
310 info_level
= SMB_FIND_ID_FULL_DIRECTORY_INFO
;
314 tevent_req_nterror(req
, NT_STATUS_INVALID_INFO_CLASS
);
315 return tevent_req_post(req
, ev
);
318 if (in_flags
& SMB2_CONTINUE_FLAG_REOPEN
) {
322 if (fsp
->dptr
== NULL
) {
325 if (!(fsp
->access_mask
& SEC_DIR_LIST
)) {
326 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
327 return tevent_req_post(req
, ev
);
330 wcard_has_wild
= ms_has_wild(in_file_name
);
332 status
= dptr_create(conn
,
334 fsp
->fsp_name
->base_name
,
335 false, /* old_handle */
336 false, /* expect_close */
338 in_file_name
, /* wcard */
342 if (!NT_STATUS_IS_OK(status
)) {
343 tevent_req_nterror(req
, status
);
344 return tevent_req_post(req
, ev
);
347 empty_status
= NT_STATUS_NO_SUCH_FILE
;
349 empty_status
= STATUS_NO_MORE_FILES
;
352 if (in_flags
& SMB2_CONTINUE_FLAG_RESTART
) {
353 dptr_SeekDir(fsp
->dptr
, 0);
356 if (in_flags
& SMB2_CONTINUE_FLAG_SINGLE
) {
359 max_count
= UINT16_MAX
;
362 #define DIR_ENTRY_SAFETY_MARGIN 4096
364 state
->out_output_buffer
= data_blob_talloc(state
, NULL
,
365 in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
);
366 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
367 return tevent_req_post(req
, ev
);
370 state
->out_output_buffer
.length
= 0;
371 pdata
= (char *)state
->out_output_buffer
.data
;
374 * end_data must include the safety margin as it's what is
375 * used to determine if pushed strings have been truncated.
377 end_data
= pdata
+ in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
- 1;
382 DEBUG(8,("smbd_smb2_find_send: dirpath=<%s> dontdescend=<%s>, "
383 "in_output_buffer_length = %u\n",
384 fsp
->fsp_name
->base_name
, lp_dontdescend(SNUM(conn
)),
385 (unsigned int)in_output_buffer_length
));
386 if (in_list(fsp
->fsp_name
->base_name
,lp_dontdescend(SNUM(conn
)),
387 conn
->case_sensitive
)) {
391 ask_sharemode
= lp_parm_bool(SNUM(conn
),
392 "smbd", "search ask sharemode",
397 bool got_exact_match
= false;
398 bool out_of_space
= false;
399 int space_remaining
= in_output_buffer_length
- off
;
401 SMB_ASSERT(space_remaining
>= 0);
403 ok
= smbd_dirptr_lanman2_entry(state
,
410 false, /* requires_resume_key */
413 8, /* align to 8 bytes */
414 false, /* no padding */
424 off
= (int)PTR_DIFF(pdata
, base_data
);
428 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
429 tevent_req_done(req
);
430 return tevent_req_post(req
, ev
);
431 } else if (out_of_space
) {
432 tevent_req_nterror(req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
433 return tevent_req_post(req
, ev
);
435 tevent_req_nterror(req
, empty_status
);
436 return tevent_req_post(req
, ev
);
441 state
->out_output_buffer
.length
= off
;
443 if (num
< max_count
) {
447 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
448 tevent_req_done(req
);
449 return tevent_req_post(req
, ev
);
452 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
453 return tevent_req_post(req
, ev
);
456 static NTSTATUS
smbd_smb2_find_recv(struct tevent_req
*req
,
458 DATA_BLOB
*out_output_buffer
)
461 struct smbd_smb2_find_state
*state
= tevent_req_data(req
,
462 struct smbd_smb2_find_state
);
464 if (tevent_req_is_nterror(req
, &status
)) {
465 tevent_req_received(req
);
469 *out_output_buffer
= state
->out_output_buffer
;
470 talloc_steal(mem_ctx
, out_output_buffer
->data
);
472 tevent_req_received(req
);