s3: add FILE_ATTRIBUTE_SPARSE to get_stat_dos_flags
[Samba/wip.git] / source3 / smbd / smb2_find.c
blob9a7738ab7629a6c0253e4d9f5920d9ef7bd0a3f3
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/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,
29 uint8_t in_flags,
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,
35 TALLOC_CTX *mem_ctx,
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)
41 const uint8_t *inhdr;
42 const uint8_t *inbody;
43 int i = req->current_idx;
44 size_t expected_body_size = 0x21;
45 size_t body_size;
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 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;
58 bool ok;
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) {
82 /* This is ok */
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 /* The output header is 8 bytes. */
93 if (in_output_buffer_length <= 8) {
94 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
97 DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
98 (unsigned int)in_output_buffer_length ));
100 /* Take into account the output header. */
101 in_output_buffer_length -= 8;
103 in_file_name_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
104 in_file_name_buffer.length = in_file_name_length;
106 ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
107 in_file_name_buffer.data,
108 in_file_name_buffer.length,
109 &in_file_name_string,
110 &in_file_name_string_size, false);
111 if (!ok) {
112 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
115 if (req->compat_chain_fsp) {
116 /* skip check */
117 } else if (in_file_id_persistent != in_file_id_volatile) {
118 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
121 subreq = smbd_smb2_find_send(req,
122 req->sconn->smb2.event_ctx,
123 req,
124 in_file_info_class,
125 in_flags,
126 in_file_index,
127 in_file_id_volatile,
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);
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 int i = req->current_idx;
143 uint8_t *outhdr;
144 DATA_BLOB outbody;
145 DATA_BLOB outdyn;
146 uint16_t out_output_buffer_offset;
147 DATA_BLOB out_output_buffer = data_blob_null;
148 NTSTATUS status;
149 NTSTATUS error; /* transport error */
151 status = smbd_smb2_find_recv(subreq,
152 req,
153 &out_output_buffer);
154 TALLOC_FREE(subreq);
155 if (!NT_STATUS_IS_OK(status)) {
156 error = smbd_smb2_request_error(req, status);
157 if (!NT_STATUS_IS_OK(error)) {
158 smbd_server_connection_terminate(req->sconn,
159 nt_errstr(error));
160 return;
162 return;
165 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
167 outhdr = (uint8_t *)req->out.vector[i].iov_base;
169 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
170 if (outbody.data == NULL) {
171 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
172 if (!NT_STATUS_IS_OK(error)) {
173 smbd_server_connection_terminate(req->sconn,
174 nt_errstr(error));
175 return;
177 return;
180 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
181 SSVAL(outbody.data, 0x02,
182 out_output_buffer_offset); /* output buffer offset */
183 SIVAL(outbody.data, 0x04,
184 out_output_buffer.length); /* output buffer length */
186 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
187 (unsigned int)out_output_buffer.length ));
189 outdyn = out_output_buffer;
191 error = smbd_smb2_request_done(req, outbody, &outdyn);
192 if (!NT_STATUS_IS_OK(error)) {
193 smbd_server_connection_terminate(req->sconn,
194 nt_errstr(error));
195 return;
199 struct smbd_smb2_find_state {
200 struct smbd_smb2_request *smb2req;
201 DATA_BLOB out_output_buffer;
204 static struct tevent_req *smbd_smb2_find_send(TALLOC_CTX *mem_ctx,
205 struct tevent_context *ev,
206 struct smbd_smb2_request *smb2req,
207 uint8_t in_file_info_class,
208 uint8_t in_flags,
209 uint32_t in_file_index,
210 uint64_t in_file_id_volatile,
211 uint32_t in_output_buffer_length,
212 const char *in_file_name)
214 struct tevent_req *req;
215 struct smbd_smb2_find_state *state;
216 struct smb_request *smbreq;
217 connection_struct *conn = smb2req->tcon->compat_conn;
218 files_struct *fsp;
219 NTSTATUS status;
220 NTSTATUS empty_status;
221 uint32_t info_level;
222 uint32_t max_count;
223 char *pdata;
224 char *base_data;
225 char *end_data;
226 int last_entry_off = 0;
227 int off = 0;
228 uint32_t num = 0;
229 uint32_t dirtype = aHIDDEN | aSYSTEM | aDIR;
230 const char *directory;
231 bool dont_descend = false;
232 bool ask_sharemode = true;
234 req = tevent_req_create(mem_ctx, &state,
235 struct smbd_smb2_find_state);
236 if (req == NULL) {
237 return NULL;
239 state->smb2req = smb2req;
240 state->out_output_buffer = data_blob_null;
242 DEBUG(10,("smbd_smb2_find_send: file_id[0x%016llX]\n",
243 (unsigned long long)in_file_id_volatile));
245 smbreq = smbd_smb2_fake_smb_request(smb2req);
246 if (tevent_req_nomem(smbreq, req)) {
247 return tevent_req_post(req, ev);
250 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
251 if (fsp == NULL) {
252 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
253 return tevent_req_post(req, ev);
255 if (conn != fsp->conn) {
256 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
257 return tevent_req_post(req, ev);
259 if (smb2req->session->vuid != fsp->vuid) {
260 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
261 return tevent_req_post(req, ev);
264 if (!fsp->is_directory) {
265 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
266 return tevent_req_post(req, ev);
269 directory = fsp->fsp_name->base_name;
271 if (strcmp(in_file_name, "") == 0) {
272 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
273 return tevent_req_post(req, ev);
275 if (strcmp(in_file_name, "\\") == 0) {
276 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
277 return tevent_req_post(req, ev);
279 if (strcmp(in_file_name, "/") == 0) {
280 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
281 return tevent_req_post(req, ev);
284 if (in_output_buffer_length > 0x10000) {
285 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
286 return tevent_req_post(req, ev);
289 switch (in_file_info_class) {
290 case SMB2_FIND_DIRECTORY_INFO:
291 info_level = SMB_FIND_FILE_DIRECTORY_INFO;
292 break;
294 case SMB2_FIND_FULL_DIRECTORY_INFO:
295 info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
296 break;
298 case SMB2_FIND_BOTH_DIRECTORY_INFO:
299 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
300 break;
302 case SMB2_FIND_NAME_INFO:
303 info_level = SMB_FIND_FILE_NAMES_INFO;
304 break;
306 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
307 info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
308 break;
310 case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
311 info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
312 break;
314 default:
315 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
316 return tevent_req_post(req, ev);
319 if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
320 if (fsp->dptr) {
321 dptr_CloseDir(fsp->dptr);
322 fsp->dptr = NULL;
326 if (fsp->dptr == NULL) {
327 bool wcard_has_wild;
329 if (!(fsp->access_mask & SEC_DIR_LIST)) {
330 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
331 return tevent_req_post(req, ev);
334 wcard_has_wild = ms_has_wild(in_file_name);
336 status = dptr_create(conn,
337 directory,
338 false, /* old_handle */
339 false, /* expect_close */
340 0, /* spid */
341 in_file_name, /* wcard */
342 wcard_has_wild,
343 dirtype,
344 &fsp->dptr);
345 if (!NT_STATUS_IS_OK(status)) {
346 tevent_req_nterror(req, status);
347 return tevent_req_post(req, ev);
350 empty_status = NT_STATUS_NO_SUCH_FILE;
351 } else {
352 empty_status = STATUS_NO_MORE_FILES;
355 if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
356 dptr_SeekDir(fsp->dptr, 0);
359 if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
360 max_count = 1;
361 } else {
362 max_count = UINT16_MAX;
365 #define DIR_ENTRY_SAFETY_MARGIN 4096
367 state->out_output_buffer = data_blob_talloc(state, NULL,
368 in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
369 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
370 return tevent_req_post(req, ev);
373 state->out_output_buffer.length = 0;
374 pdata = (char *)state->out_output_buffer.data;
375 base_data = pdata;
377 * end_data must include the safety margin as it's what is
378 * used to determine if pushed strings have been truncated.
380 end_data = pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
381 last_entry_off = 0;
382 off = 0;
383 num = 0;
385 DEBUG(8,("smbd_smb2_find_send: dirpath=<%s> dontdescend=<%s>, "
386 "in_output_buffer_length = %u\n",
387 directory, lp_dontdescend(SNUM(conn)),
388 (unsigned int)in_output_buffer_length ));
389 if (in_list(directory,lp_dontdescend(SNUM(conn)),conn->case_sensitive)) {
390 dont_descend = true;
393 ask_sharemode = lp_parm_bool(SNUM(conn),
394 "smbd", "search ask sharemode",
395 true);
397 while (true) {
398 bool ok;
399 bool got_exact_match = false;
400 bool out_of_space = false;
401 int space_remaining = in_output_buffer_length - off;
403 SMB_ASSERT(space_remaining >= 0);
405 ok = smbd_dirptr_lanman2_entry(state,
406 conn,
407 fsp->dptr,
408 smbreq->flags2,
409 in_file_name,
410 dirtype,
411 info_level,
412 false, /* requires_resume_key */
413 dont_descend,
414 ask_sharemode,
415 8, /* align to 8 bytes */
416 false, /* no padding */
417 &pdata,
418 base_data,
419 end_data,
420 space_remaining,
421 &out_of_space,
422 &got_exact_match,
423 &last_entry_off,
424 NULL);
426 off = (int)PTR_DIFF(pdata, base_data);
428 if (!ok) {
429 if (num > 0) {
430 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
431 tevent_req_done(req);
432 return tevent_req_post(req, ev);
433 } else if (out_of_space) {
434 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
435 return tevent_req_post(req, ev);
436 } else {
437 tevent_req_nterror(req, empty_status);
438 return tevent_req_post(req, ev);
442 num++;
443 state->out_output_buffer.length = off;
445 if (num < max_count) {
446 continue;
449 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
450 tevent_req_done(req);
451 return tevent_req_post(req, ev);
454 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
455 return tevent_req_post(req, ev);
458 static NTSTATUS smbd_smb2_find_recv(struct tevent_req *req,
459 TALLOC_CTX *mem_ctx,
460 DATA_BLOB *out_output_buffer)
462 NTSTATUS status;
463 struct smbd_smb2_find_state *state = tevent_req_data(req,
464 struct smbd_smb2_find_state);
466 if (tevent_req_is_nterror(req, &status)) {
467 tevent_req_received(req);
468 return status;
471 *out_output_buffer = state->out_output_buffer;
472 talloc_steal(mem_ctx, out_output_buffer->data);
474 tevent_req_received(req);
475 return NT_STATUS_OK;