VERSION: Disable git snapshots for the 4.1.4 release.
[Samba.git] / source3 / smbd / smb2_find.c
blobc39a35d526cd1aaf1929733bde9628a0087d10db
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
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/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "trans2.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,
33 uint8_t in_flags,
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,
38 TALLOC_CTX *mem_ctx,
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)
44 NTSTATUS status;
45 const uint8_t *inbody;
46 uint8_t in_file_info_class;
47 uint8_t in_flags;
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;
59 bool ok;
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) {
77 /* This is ok */
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);
106 if (!ok) {
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,
124 req, in_fsp,
125 in_file_info_class,
126 in_flags,
127 in_file_index,
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);
142 DATA_BLOB outbody;
143 DATA_BLOB outdyn;
144 uint16_t out_output_buffer_offset;
145 DATA_BLOB out_output_buffer = data_blob_null;
146 NTSTATUS status;
147 NTSTATUS error; /* transport error */
149 status = smbd_smb2_find_recv(subreq,
150 req,
151 &out_output_buffer);
152 TALLOC_FREE(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,
157 nt_errstr(error));
158 return;
160 return;
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,
170 nt_errstr(error));
171 return;
173 return;
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,
190 nt_errstr(error));
191 return;
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,
205 uint8_t in_flags,
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;
214 NTSTATUS status;
215 NTSTATUS empty_status;
216 uint32_t info_level;
217 uint32_t max_count;
218 char *pdata;
219 char *base_data;
220 char *end_data;
221 int last_entry_off = 0;
222 int off = 0;
223 uint32_t num = 0;
224 uint32_t dirtype = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY;
225 bool dont_descend = false;
226 bool ask_sharemode = true;
227 struct tm tm;
228 char *p;
230 req = tevent_req_create(mem_ctx, &state,
231 struct smbd_smb2_find_state);
232 if (req == NULL) {
233 return NULL;
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
269 * a directory.
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;
295 break;
297 case SMB2_FIND_FULL_DIRECTORY_INFO:
298 info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
299 break;
301 case SMB2_FIND_BOTH_DIRECTORY_INFO:
302 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
303 break;
305 case SMB2_FIND_NAME_INFO:
306 info_level = SMB_FIND_FILE_NAMES_INFO;
307 break;
309 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
310 info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
311 break;
313 case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
314 info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
315 break;
317 default:
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) {
323 dptr_CloseDir(fsp);
326 if (fsp->dptr == NULL) {
327 bool wcard_has_wild;
329 wcard_has_wild = ms_has_wild(in_file_name);
331 status = dptr_create(conn,
332 NULL, /* req */
333 fsp,
334 fsp->fsp_name->base_name,
335 false, /* old_handle */
336 false, /* expect_close */
337 0, /* spid */
338 in_file_name, /* wcard */
339 wcard_has_wild,
340 dirtype,
341 &fsp->dptr);
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;
348 } else {
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) {
357 max_count = 1;
358 } else {
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;
372 base_data = pdata;
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;
378 last_entry_off = 0;
379 off = 0;
380 num = 0;
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)) {
388 dont_descend = true;
391 ask_sharemode = lp_parm_bool(SNUM(conn),
392 "smbd", "search ask sharemode",
393 true);
395 while (true) {
396 bool ok;
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,
404 conn,
405 fsp->dptr,
406 smbreq->flags2,
407 in_file_name,
408 dirtype,
409 info_level,
410 false, /* requires_resume_key */
411 dont_descend,
412 ask_sharemode,
413 8, /* align to 8 bytes */
414 false, /* no padding */
415 &pdata,
416 base_data,
417 end_data,
418 space_remaining,
419 &out_of_space,
420 &got_exact_match,
421 &last_entry_off,
422 NULL);
424 off = (int)PTR_DIFF(pdata, base_data);
426 if (!ok) {
427 if (num > 0) {
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);
434 } else {
435 tevent_req_nterror(req, empty_status);
436 return tevent_req_post(req, ev);
440 num++;
441 state->out_output_buffer.length = off;
443 if (num < max_count) {
444 continue;
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,
457 TALLOC_CTX *mem_ctx,
458 DATA_BLOB *out_output_buffer)
460 NTSTATUS status;
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);
466 return status;
469 *out_output_buffer = state->out_output_buffer;
470 talloc_steal(mem_ctx, out_output_buffer->data);
472 tevent_req_received(req);
473 return NT_STATUS_OK;