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 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_find_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_find_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
->sconn
,
163 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
165 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 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
->sconn
,
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
->sconn
,
195 struct smbd_smb2_find_state
{
196 struct smbd_smb2_request
*smb2req
;
197 DATA_BLOB out_output_buffer
;
200 static struct tevent_req
*smbd_smb2_find_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 tevent_req
*req
;
211 struct smbd_smb2_find_state
*state
;
212 struct smb_request
*smbreq
;
213 connection_struct
*conn
= smb2req
->tcon
->compat
;
215 NTSTATUS empty_status
;
221 int last_entry_off
= 0;
224 uint32_t dirtype
= FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_DIRECTORY
;
225 bool dont_descend
= false;
226 bool ask_sharemode
= true;
230 req
= tevent_req_create(mem_ctx
, &state
,
231 struct smbd_smb2_find_state
);
235 state
->smb2req
= smb2req
;
236 state
->out_output_buffer
= data_blob_null
;
238 DEBUG(10,("smbd_smb2_find_send: %s - %s\n",
239 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
241 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
242 if (tevent_req_nomem(smbreq
, req
)) {
243 return tevent_req_post(req
, ev
);
246 if (!fsp
->is_directory
) {
247 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
248 return tevent_req_post(req
, ev
);
251 if (strcmp(in_file_name
, "") == 0) {
252 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
253 return tevent_req_post(req
, ev
);
255 if (strcmp(in_file_name
, "\\") == 0) {
256 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
257 return tevent_req_post(req
, ev
);
259 if (strcmp(in_file_name
, "/") == 0) {
260 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
261 return tevent_req_post(req
, ev
);
264 p
= strptime(in_file_name
, GMT_FORMAT
, &tm
);
265 if ((p
!= NULL
) && (*p
=='\0')) {
267 * Bogus find that asks for a shadow copy timestamp as a
268 * directory. The correct response is that it does not exist as
271 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_FILE
);
272 return tevent_req_post(req
, ev
);
275 if (in_output_buffer_length
> smb2req
->sconn
->smb2
.max_trans
) {
276 DEBUG(2,("smbd_smb2_find_send: "
277 "client ignored max trans:%s: 0x%08X: 0x%08X\n",
278 __location__
, in_output_buffer_length
,
279 smb2req
->sconn
->smb2
.max_trans
));
280 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
281 return tevent_req_post(req
, ev
);
284 status
= smbd_smb2_request_verify_creditcharge(smb2req
,
285 in_output_buffer_length
);
287 if (!NT_STATUS_IS_OK(status
)) {
288 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
289 return tevent_req_post(req
, ev
);
292 switch (in_file_info_class
) {
293 case SMB2_FIND_DIRECTORY_INFO
:
294 info_level
= SMB_FIND_FILE_DIRECTORY_INFO
;
297 case SMB2_FIND_FULL_DIRECTORY_INFO
:
298 info_level
= SMB_FIND_FILE_FULL_DIRECTORY_INFO
;
301 case SMB2_FIND_BOTH_DIRECTORY_INFO
:
302 info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
305 case SMB2_FIND_NAME_INFO
:
306 info_level
= SMB_FIND_FILE_NAMES_INFO
;
309 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO
:
310 info_level
= SMB_FIND_ID_BOTH_DIRECTORY_INFO
;
313 case SMB2_FIND_ID_FULL_DIRECTORY_INFO
:
314 info_level
= SMB_FIND_ID_FULL_DIRECTORY_INFO
;
318 tevent_req_nterror(req
, NT_STATUS_INVALID_INFO_CLASS
);
319 return tevent_req_post(req
, ev
);
322 if (in_flags
& SMB2_CONTINUE_FLAG_REOPEN
) {
326 if (fsp
->dptr
== NULL
) {
329 wcard_has_wild
= ms_has_wild(in_file_name
);
331 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(talloc_tos(), SNUM(conn
)),
385 (unsigned int)in_output_buffer_length
));
386 if (in_list(fsp
->fsp_name
->base_name
,lp_dontdescend(talloc_tos(), 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
);