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
= smbd_smb2_generate_outbody(req
, 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 smbXsrv_connection
*xconn
= smb2req
->sconn
->conn
;
211 struct tevent_req
*req
;
212 struct smbd_smb2_find_state
*state
;
213 struct smb_request
*smbreq
;
214 connection_struct
*conn
= smb2req
->tcon
->compat
;
216 NTSTATUS empty_status
;
222 int last_entry_off
= 0;
225 uint32_t dirtype
= FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_DIRECTORY
;
226 bool dont_descend
= false;
227 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: %s - %s\n",
241 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
243 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
244 if (tevent_req_nomem(smbreq
, req
)) {
245 return tevent_req_post(req
, ev
);
248 if (!fsp
->is_directory
) {
249 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
250 return tevent_req_post(req
, ev
);
253 if (strcmp(in_file_name
, "") == 0) {
254 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
255 return tevent_req_post(req
, ev
);
257 if (strchr_m(in_file_name
, '\\') != NULL
) {
258 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
259 return tevent_req_post(req
, ev
);
261 if (strchr_m(in_file_name
, '/') != NULL
) {
262 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_INVALID
);
263 return tevent_req_post(req
, ev
);
266 p
= strptime(in_file_name
, GMT_FORMAT
, &tm
);
267 if ((p
!= NULL
) && (*p
=='\0')) {
269 * Bogus find that asks for a shadow copy timestamp as a
270 * directory. The correct response is that it does not exist as
273 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_FILE
);
274 return tevent_req_post(req
, ev
);
277 if (in_output_buffer_length
> xconn
->smb2
.server
.max_trans
) {
278 DEBUG(2,("smbd_smb2_find_send: "
279 "client ignored max trans:%s: 0x%08X: 0x%08X\n",
280 __location__
, in_output_buffer_length
,
281 xconn
->smb2
.server
.max_trans
));
282 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
283 return tevent_req_post(req
, ev
);
286 status
= smbd_smb2_request_verify_creditcharge(smb2req
,
287 in_output_buffer_length
);
289 if (!NT_STATUS_IS_OK(status
)) {
290 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
291 return tevent_req_post(req
, ev
);
294 switch (in_file_info_class
) {
295 case SMB2_FIND_DIRECTORY_INFO
:
296 info_level
= SMB_FIND_FILE_DIRECTORY_INFO
;
299 case SMB2_FIND_FULL_DIRECTORY_INFO
:
300 info_level
= SMB_FIND_FILE_FULL_DIRECTORY_INFO
;
303 case SMB2_FIND_BOTH_DIRECTORY_INFO
:
304 info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
307 case SMB2_FIND_NAME_INFO
:
308 info_level
= SMB_FIND_FILE_NAMES_INFO
;
311 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO
:
312 info_level
= SMB_FIND_ID_BOTH_DIRECTORY_INFO
;
315 case SMB2_FIND_ID_FULL_DIRECTORY_INFO
:
316 info_level
= SMB_FIND_ID_FULL_DIRECTORY_INFO
;
320 tevent_req_nterror(req
, NT_STATUS_INVALID_INFO_CLASS
);
321 return tevent_req_post(req
, ev
);
324 if (in_flags
& SMB2_CONTINUE_FLAG_REOPEN
) {
328 wcard_has_wild
= ms_has_wild(in_file_name
);
330 /* Ensure we've canonicalized any search path if not a wildcard. */
331 if (!wcard_has_wild
) {
332 struct smb_filename
*smb_fname
= NULL
;
333 const char *fullpath
;
334 char tmpbuf
[PATH_MAX
];
335 char *to_free
= NULL
;
337 if (ISDOT(fsp
->fsp_name
->base_name
)) {
338 fullpath
= in_file_name
;
344 fsp
->fsp_name
->base_name
, in_file_name
,
345 tmpbuf
, sizeof(tmpbuf
), &tmp
, &to_free
);
348 return tevent_req_post(req
, ev
);
352 status
= filename_convert(state
,
354 false, /* Not a DFS path. */
356 UCF_SAVE_LCOMP
| UCF_ALWAYS_ALLOW_WCARD_LCOMP
,
360 TALLOC_FREE(to_free
);
362 if (tevent_req_nterror(req
, status
)) {
363 return tevent_req_post(req
, ev
);
366 in_file_name
= smb_fname
->original_lcomp
;
369 if (fsp
->dptr
== NULL
) {
370 status
= dptr_create(conn
,
373 fsp
->fsp_name
->base_name
,
374 false, /* old_handle */
375 false, /* expect_close */
377 in_file_name
, /* wcard */
381 if (!NT_STATUS_IS_OK(status
)) {
382 tevent_req_nterror(req
, status
);
383 return tevent_req_post(req
, ev
);
386 empty_status
= NT_STATUS_NO_SUCH_FILE
;
388 empty_status
= STATUS_NO_MORE_FILES
;
391 if (in_flags
& SMB2_CONTINUE_FLAG_RESTART
) {
392 dptr_SeekDir(fsp
->dptr
, 0);
395 if (in_flags
& SMB2_CONTINUE_FLAG_SINGLE
) {
398 max_count
= UINT16_MAX
;
401 #define DIR_ENTRY_SAFETY_MARGIN 4096
403 state
->out_output_buffer
= data_blob_talloc(state
, NULL
,
404 in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
);
405 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
406 return tevent_req_post(req
, ev
);
409 state
->out_output_buffer
.length
= 0;
410 pdata
= (char *)state
->out_output_buffer
.data
;
413 * end_data must include the safety margin as it's what is
414 * used to determine if pushed strings have been truncated.
416 end_data
= pdata
+ in_output_buffer_length
+ DIR_ENTRY_SAFETY_MARGIN
- 1;
421 DEBUG(8,("smbd_smb2_find_send: dirpath=<%s> dontdescend=<%s>, "
422 "in_output_buffer_length = %u\n",
423 fsp
->fsp_name
->base_name
, lp_dont_descend(talloc_tos(), SNUM(conn
)),
424 (unsigned int)in_output_buffer_length
));
425 if (in_list(fsp
->fsp_name
->base_name
,lp_dont_descend(talloc_tos(), SNUM(conn
)),
426 conn
->case_sensitive
)) {
430 ask_sharemode
= lp_parm_bool(SNUM(conn
),
431 "smbd", "search ask sharemode",
436 bool got_exact_match
= false;
437 bool out_of_space
= false;
438 int space_remaining
= in_output_buffer_length
- off
;
440 SMB_ASSERT(space_remaining
>= 0);
442 ok
= smbd_dirptr_lanman2_entry(state
,
449 false, /* requires_resume_key */
452 8, /* align to 8 bytes */
453 false, /* no padding */
463 off
= (int)PTR_DIFF(pdata
, base_data
);
467 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
468 tevent_req_done(req
);
469 return tevent_req_post(req
, ev
);
470 } else if (out_of_space
) {
471 tevent_req_nterror(req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
472 return tevent_req_post(req
, ev
);
474 tevent_req_nterror(req
, empty_status
);
475 return tevent_req_post(req
, ev
);
480 state
->out_output_buffer
.length
= off
;
482 if (num
< max_count
) {
486 SIVAL(state
->out_output_buffer
.data
, last_entry_off
, 0);
487 tevent_req_done(req
);
488 return tevent_req_post(req
, ev
);
491 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
492 return tevent_req_post(req
, ev
);
495 static NTSTATUS
smbd_smb2_find_recv(struct tevent_req
*req
,
497 DATA_BLOB
*out_output_buffer
)
500 struct smbd_smb2_find_state
*state
= tevent_req_data(req
,
501 struct smbd_smb2_find_state
);
503 if (tevent_req_is_nterror(req
, &status
)) {
504 tevent_req_received(req
);
508 *out_output_buffer
= state
->out_output_buffer
;
509 talloc_steal(mem_ctx
, out_output_buffer
->data
);
511 tevent_req_received(req
);