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/globals.h"
23 #include "../libcli/smb/smb_common.h"
25 static struct tevent_req
*smbd_smb2_find_send(TALLOC_CTX
*mem_ctx
,
26 struct tevent_context
*ev
,
27 struct smbd_smb2_request
*smb2req
,
28 uint8_t in_file_info_class
,
30 uint32_t in_file_index
,
31 uint64_t in_file_id_volatile
,
32 uint32_t in_output_buffer_length
,
33 const char *in_file_name
);
34 static NTSTATUS
smbd_smb2_find_recv(struct tevent_req
*req
,
36 DATA_BLOB
*out_output_buffer
);
38 static void smbd_smb2_request_find_done(struct tevent_req
*subreq
);
39 NTSTATUS
smbd_smb2_request_process_find(struct smbd_smb2_request
*req
)
42 const uint8_t *inbody
;
43 int i
= req
->current_idx
;
44 size_t expected_body_size
= 0x21;
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 uint16_t in_file_name_offset
;
52 uint16_t in_file_name_length
;
53 DATA_BLOB in_file_name_buffer
;
54 char *in_file_name_string
;
55 size_t in_file_name_string_size
;
56 uint32_t in_output_buffer_length
;
57 struct tevent_req
*subreq
;
60 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
61 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
62 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
65 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
67 body_size
= SVAL(inbody
, 0x00);
68 if (body_size
!= expected_body_size
) {
69 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
72 in_file_info_class
= CVAL(inbody
, 0x02);
73 in_flags
= CVAL(inbody
, 0x03);
74 in_file_index
= IVAL(inbody
, 0x04);
75 in_file_id_persistent
= BVAL(inbody
, 0x08);
76 in_file_id_volatile
= BVAL(inbody
, 0x10);
77 in_file_name_offset
= SVAL(inbody
, 0x18);
78 in_file_name_length
= SVAL(inbody
, 0x1A);
79 in_output_buffer_length
= IVAL(inbody
, 0x1C);
81 if (in_file_name_offset
== 0 && in_file_name_length
== 0) {
83 } else if (in_file_name_offset
!=
84 (SMB2_HDR_BODY
+ (body_size
& 0xFFFFFFFE))) {
85 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
88 if (in_file_name_length
> req
->in
.vector
[i
+2].iov_len
) {
89 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
92 in_file_name_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
93 in_file_name_buffer
.length
= in_file_name_length
;
95 ok
= convert_string_talloc(req
, CH_UTF16
, CH_UNIX
,
96 in_file_name_buffer
.data
,
97 in_file_name_buffer
.length
,
99 &in_file_name_string_size
, false);
101 return smbd_smb2_request_error(req
, NT_STATUS_ILLEGAL_CHARACTER
);
104 if (req
->compat_chain_fsp
) {
106 } else if (in_file_id_persistent
!= 0) {
107 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
110 subreq
= smbd_smb2_find_send(req
,
111 req
->sconn
->smb2
.event_ctx
,
117 in_output_buffer_length
,
118 in_file_name_string
);
119 if (subreq
== NULL
) {
120 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
122 tevent_req_set_callback(subreq
, smbd_smb2_request_find_done
, req
);
124 return smbd_smb2_request_pending_queue(req
, subreq
);
127 static void smbd_smb2_request_find_done(struct tevent_req
*subreq
)
129 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
130 struct smbd_smb2_request
);
131 int i
= req
->current_idx
;
135 uint16_t out_output_buffer_offset
;
136 DATA_BLOB out_output_buffer
= data_blob_null
;
138 NTSTATUS error
; /* transport error */
140 status
= smbd_smb2_find_recv(subreq
,
144 if (!NT_STATUS_IS_OK(status
)) {
145 error
= smbd_smb2_request_error(req
, status
);
146 if (!NT_STATUS_IS_OK(error
)) {
147 smbd_server_connection_terminate(req
->sconn
,
154 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
156 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
158 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
159 if (outbody
.data
== NULL
) {
160 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
161 if (!NT_STATUS_IS_OK(error
)) {
162 smbd_server_connection_terminate(req
->sconn
,
169 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
170 SSVAL(outbody
.data
, 0x02,
171 out_output_buffer_offset
); /* output buffer offset */
172 SIVAL(outbody
.data
, 0x04,
173 out_output_buffer
.length
); /* output buffer length */
175 outdyn
= out_output_buffer
;
177 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
178 if (!NT_STATUS_IS_OK(error
)) {
179 smbd_server_connection_terminate(req
->sconn
,
185 struct smbd_smb2_find_state
{
186 struct smbd_smb2_request
*smb2req
;
187 DATA_BLOB out_output_buffer
;
190 static struct tevent_req
*smbd_smb2_find_send(TALLOC_CTX
*mem_ctx
,
191 struct tevent_context
*ev
,
192 struct smbd_smb2_request
*smb2req
,
193 uint8_t in_file_info_class
,
195 uint32_t in_file_index
,
196 uint64_t in_file_id_volatile
,
197 uint32_t in_output_buffer_length
,
198 const char *in_file_name
)
200 struct tevent_req
*req
;
201 struct smbd_smb2_find_state
*state
;
202 struct smb_request
*smbreq
;
203 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
206 NTSTATUS empty_status
;
212 int last_entry_off
= 0;
215 uint32_t dirtype
= aHIDDEN
| aSYSTEM
| aDIR
;
216 const char *directory
;
217 bool dont_descend
= false;
218 bool ask_sharemode
= true;
220 req
= tevent_req_create(mem_ctx
, &state
,
221 struct smbd_smb2_find_state
);
225 state
->smb2req
= smb2req
;
226 state
->out_output_buffer
= data_blob_null
;
228 DEBUG(10,("smbd_smb2_find_send: file_id[0x%016llX]\n",
229 (unsigned long long)in_file_id_volatile
));
231 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
232 if (tevent_req_nomem(smbreq
, req
)) {
233 return tevent_req_post(req
, ev
);
236 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
238 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
239 return tevent_req_post(req
, ev
);
241 if (conn
!= fsp
->conn
) {
242 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
243 return tevent_req_post(req
, ev
);
245 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
246 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
247 return tevent_req_post(req
, ev
);
250 if (!fsp
->is_directory
) {
251 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
252 return tevent_req_post(req
, ev
);
255 directory
= fsp
->fsp_name
->base_name
;
257 if (strcmp(in_file_name
, "") == 0) {
258 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
259 return tevent_req_post(req
, ev
);
261 if (strcmp(in_file_name
, "\\") == 0) {
262 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
263 return tevent_req_post(req
, ev
);
265 if (strcmp(in_file_name
, "/") == 0) {
266 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
267 return tevent_req_post(req
, ev
);
270 if (in_output_buffer_length
> 0x10000) {
271 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
272 return tevent_req_post(req
, ev
);
275 switch (in_file_info_class
) {
276 case SMB2_FIND_DIRECTORY_INFO
:
277 info_level
= SMB_FIND_FILE_DIRECTORY_INFO
;
280 case SMB2_FIND_FULL_DIRECTORY_INFO
:
281 info_level
= SMB_FIND_FILE_FULL_DIRECTORY_INFO
;
284 case SMB2_FIND_BOTH_DIRECTORY_INFO
:
285 info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
288 case SMB2_FIND_NAME_INFO
:
289 info_level
= SMB_FIND_FILE_NAMES_INFO
;
292 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO
:
293 info_level
= SMB_FIND_ID_BOTH_DIRECTORY_INFO
;
296 case SMB2_FIND_ID_FULL_DIRECTORY_INFO
:
297 info_level
= SMB_FIND_ID_FULL_DIRECTORY_INFO
;
301 tevent_req_nterror(req
, NT_STATUS_INVALID_INFO_CLASS
);
302 return tevent_req_post(req
, ev
);
305 if (in_flags
& SMB2_CONTINUE_FLAG_REOPEN
) {
307 dptr_CloseDir(fsp
->dptr
);
312 if (fsp
->dptr
== NULL
) {
315 if (!(fsp
->access_mask
& SEC_DIR_LIST
)) {
316 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
317 return tevent_req_post(req
, ev
);
320 wcard_has_wild
= ms_has_wild(in_file_name
);
322 status
= dptr_create(conn
,
324 false, /* old_handle */
325 false, /* expect_close */
327 in_file_name
, /* wcard */
331 if (!NT_STATUS_IS_OK(status
)) {
332 tevent_req_nterror(req
, status
);
333 return tevent_req_post(req
, ev
);
336 empty_status
= NT_STATUS_NO_SUCH_FILE
;
338 empty_status
= STATUS_NO_MORE_FILES
;
341 if (in_flags
& SMB2_CONTINUE_FLAG_RESTART
) {
342 dptr_SeekDir(fsp
->dptr
, 0);
345 if (in_flags
& SMB2_CONTINUE_FLAG_SINGLE
) {
348 max_count
= UINT16_MAX
;
351 #define DIR_ENTRY_SAFETY_MARGIN 4096
353 state
->out_output_buffer
= data_blob_talloc(state
, NULL
,
354 in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
);
355 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
356 return tevent_req_post(req
, ev
);
359 state
->out_output_buffer
.length
= 0;
360 pdata
= (char *)state
->out_output_buffer
.data
;
362 end_data
= pdata
+ in_output_buffer_length
;
367 DEBUG(8,("smbd_smb2_find_send: dirpath=<%s> dontdescend=<%s>\n",
368 directory
, lp_dontdescend(SNUM(conn
))));
369 if (in_list(directory
,lp_dontdescend(SNUM(conn
)),conn
->case_sensitive
)) {
373 ask_sharemode
= lp_parm_bool(SNUM(conn
),
374 "smbd", "search ask sharemode",
379 bool got_exact_match
= false;
380 bool out_of_space
= false;
381 int space_remaining
= in_output_buffer_length
- off
;
383 ok
= smbd_dirptr_lanman2_entry(state
,
390 false, /* requires_resume_key */
393 8, /* align to 8 bytes */
394 false, /* no padding */
404 off
= PTR_DIFF(pdata
, base_data
);
408 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
409 tevent_req_done(req
);
410 return tevent_req_post(req
, ev
);
411 } else if (out_of_space
) {
412 tevent_req_nterror(req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
413 return tevent_req_post(req
, ev
);
415 tevent_req_nterror(req
, empty_status
);
416 return tevent_req_post(req
, ev
);
421 state
->out_output_buffer
.length
= off
;
423 if (num
< max_count
) {
427 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
428 tevent_req_done(req
);
429 return tevent_req_post(req
, ev
);
432 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
433 return tevent_req_post(req
, ev
);
436 static NTSTATUS
smbd_smb2_find_recv(struct tevent_req
*req
,
438 DATA_BLOB
*out_output_buffer
)
441 struct smbd_smb2_find_state
*state
= tevent_req_data(req
,
442 struct smbd_smb2_find_state
);
444 if (tevent_req_is_nterror(req
, &status
)) {
445 tevent_req_received(req
);
449 *out_output_buffer
= state
->out_output_buffer
;
450 talloc_steal(mem_ctx
, out_output_buffer
->data
);
452 tevent_req_received(req
);