Fix bug 9811 - Old DOS SMB CTEMP request uses a non-VFS function to access the filesy...
[Samba.git] / source3 / smbd / smb2_find.c
blob59e5b66e7d24694cfd8a8c671f160647a254c537
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 int i = req->current_idx;
47 uint8_t in_file_info_class;
48 uint8_t in_flags;
49 uint32_t in_file_index;
50 uint64_t in_file_id_persistent;
51 uint64_t in_file_id_volatile;
52 struct files_struct *in_fsp;
53 uint16_t in_file_name_offset;
54 uint16_t in_file_name_length;
55 DATA_BLOB in_file_name_buffer;
56 char *in_file_name_string;
57 size_t in_file_name_string_size;
58 uint32_t in_output_buffer_length;
59 struct tevent_req *subreq;
60 bool ok;
62 status = smbd_smb2_request_verify_sizes(req, 0x21);
63 if (!NT_STATUS_IS_OK(status)) {
64 return smbd_smb2_request_error(req, status);
66 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
68 in_file_info_class = CVAL(inbody, 0x02);
69 in_flags = CVAL(inbody, 0x03);
70 in_file_index = IVAL(inbody, 0x04);
71 in_file_id_persistent = BVAL(inbody, 0x08);
72 in_file_id_volatile = BVAL(inbody, 0x10);
73 in_file_name_offset = SVAL(inbody, 0x18);
74 in_file_name_length = SVAL(inbody, 0x1A);
75 in_output_buffer_length = IVAL(inbody, 0x1C);
77 if (in_file_name_offset == 0 && in_file_name_length == 0) {
78 /* This is ok */
79 } else if (in_file_name_offset !=
80 (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
81 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
84 if (in_file_name_length > req->in.vector[i+2].iov_len) {
85 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
88 /* The output header is 8 bytes. */
89 if (in_output_buffer_length <= 8) {
90 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
93 DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
94 (unsigned int)in_output_buffer_length ));
96 /* Take into account the output header. */
97 in_output_buffer_length -= 8;
99 in_file_name_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
100 in_file_name_buffer.length = in_file_name_length;
102 ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
103 in_file_name_buffer.data,
104 in_file_name_buffer.length,
105 &in_file_name_string,
106 &in_file_name_string_size, false);
107 if (!ok) {
108 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
111 if (in_file_name_buffer.length == 0) {
112 in_file_name_string_size = 0;
115 if (strlen(in_file_name_string) != in_file_name_string_size) {
116 return smbd_smb2_request_error(req, NT_STATUS_OBJECT_NAME_INVALID);
119 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
120 if (in_fsp == NULL) {
121 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
124 subreq = smbd_smb2_find_send(req, req->sconn->smb2.event_ctx,
125 req, in_fsp,
126 in_file_info_class,
127 in_flags,
128 in_file_index,
129 in_output_buffer_length,
130 in_file_name_string);
131 if (subreq == NULL) {
132 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
134 tevent_req_set_callback(subreq, smbd_smb2_request_find_done, req);
136 return smbd_smb2_request_pending_queue(req, subreq);
139 static void smbd_smb2_request_find_done(struct tevent_req *subreq)
141 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
142 struct smbd_smb2_request);
143 int i = req->current_idx;
144 uint8_t *outhdr;
145 DATA_BLOB outbody;
146 DATA_BLOB outdyn;
147 uint16_t out_output_buffer_offset;
148 DATA_BLOB out_output_buffer = data_blob_null;
149 NTSTATUS status;
150 NTSTATUS error; /* transport error */
152 status = smbd_smb2_find_recv(subreq,
153 req,
154 &out_output_buffer);
155 TALLOC_FREE(subreq);
156 if (!NT_STATUS_IS_OK(status)) {
157 error = smbd_smb2_request_error(req, status);
158 if (!NT_STATUS_IS_OK(error)) {
159 smbd_server_connection_terminate(req->sconn,
160 nt_errstr(error));
161 return;
163 return;
166 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
168 outhdr = (uint8_t *)req->out.vector[i].iov_base;
170 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
171 if (outbody.data == NULL) {
172 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
173 if (!NT_STATUS_IS_OK(error)) {
174 smbd_server_connection_terminate(req->sconn,
175 nt_errstr(error));
176 return;
178 return;
181 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
182 SSVAL(outbody.data, 0x02,
183 out_output_buffer_offset); /* output buffer offset */
184 SIVAL(outbody.data, 0x04,
185 out_output_buffer.length); /* output buffer length */
187 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
188 (unsigned int)out_output_buffer.length ));
190 outdyn = out_output_buffer;
192 error = smbd_smb2_request_done(req, outbody, &outdyn);
193 if (!NT_STATUS_IS_OK(error)) {
194 smbd_server_connection_terminate(req->sconn,
195 nt_errstr(error));
196 return;
200 struct smbd_smb2_find_state {
201 struct smbd_smb2_request *smb2req;
202 DATA_BLOB out_output_buffer;
205 static struct tevent_req *smbd_smb2_find_send(TALLOC_CTX *mem_ctx,
206 struct tevent_context *ev,
207 struct smbd_smb2_request *smb2req,
208 struct files_struct *fsp,
209 uint8_t in_file_info_class,
210 uint8_t in_flags,
211 uint32_t in_file_index,
212 uint32_t in_output_buffer_length,
213 const char *in_file_name)
215 struct tevent_req *req;
216 struct smbd_smb2_find_state *state;
217 struct smb_request *smbreq;
218 connection_struct *conn = smb2req->tcon->compat_conn;
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 = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY;
230 bool dont_descend = false;
231 bool ask_sharemode = true;
233 req = tevent_req_create(mem_ctx, &state,
234 struct smbd_smb2_find_state);
235 if (req == NULL) {
236 return NULL;
238 state->smb2req = smb2req;
239 state->out_output_buffer = data_blob_null;
241 DEBUG(10,("smbd_smb2_find_send: %s - fnum[%d]\n",
242 fsp_str_dbg(fsp), fsp->fnum));
244 smbreq = smbd_smb2_fake_smb_request(smb2req);
245 if (tevent_req_nomem(smbreq, req)) {
246 return tevent_req_post(req, ev);
249 if (!fsp->is_directory) {
250 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
251 return tevent_req_post(req, ev);
254 if (strcmp(in_file_name, "") == 0) {
255 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
256 return tevent_req_post(req, ev);
258 if (strcmp(in_file_name, "\\") == 0) {
259 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
260 return tevent_req_post(req, ev);
262 if (strcmp(in_file_name, "/") == 0) {
263 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
264 return tevent_req_post(req, ev);
267 if (in_output_buffer_length > smb2req->sconn->smb2.max_trans) {
268 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
269 return tevent_req_post(req, ev);
272 switch (in_file_info_class) {
273 case SMB2_FIND_DIRECTORY_INFO:
274 info_level = SMB_FIND_FILE_DIRECTORY_INFO;
275 break;
277 case SMB2_FIND_FULL_DIRECTORY_INFO:
278 info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
279 break;
281 case SMB2_FIND_BOTH_DIRECTORY_INFO:
282 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
283 break;
285 case SMB2_FIND_NAME_INFO:
286 info_level = SMB_FIND_FILE_NAMES_INFO;
287 break;
289 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
290 info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
291 break;
293 case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
294 info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
295 break;
297 default:
298 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
299 return tevent_req_post(req, ev);
302 if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
303 dptr_CloseDir(fsp);
306 if (fsp->dptr == NULL) {
307 bool wcard_has_wild;
309 if (!(fsp->access_mask & SEC_DIR_LIST)) {
310 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
311 return tevent_req_post(req, ev);
314 wcard_has_wild = ms_has_wild(in_file_name);
316 status = dptr_create(conn,
317 fsp,
318 fsp->fsp_name->base_name,
319 false, /* old_handle */
320 false, /* expect_close */
321 0, /* spid */
322 in_file_name, /* wcard */
323 wcard_has_wild,
324 dirtype,
325 &fsp->dptr);
326 if (!NT_STATUS_IS_OK(status)) {
327 tevent_req_nterror(req, status);
328 return tevent_req_post(req, ev);
331 empty_status = NT_STATUS_NO_SUCH_FILE;
332 } else {
333 empty_status = STATUS_NO_MORE_FILES;
336 if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
337 dptr_SeekDir(fsp->dptr, 0);
340 if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
341 max_count = 1;
342 } else {
343 max_count = UINT16_MAX;
346 #define DIR_ENTRY_SAFETY_MARGIN 4096
348 state->out_output_buffer = data_blob_talloc(state, NULL,
349 in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
350 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
351 return tevent_req_post(req, ev);
354 state->out_output_buffer.length = 0;
355 pdata = (char *)state->out_output_buffer.data;
356 base_data = pdata;
358 * end_data must include the safety margin as it's what is
359 * used to determine if pushed strings have been truncated.
361 end_data = pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
362 last_entry_off = 0;
363 off = 0;
364 num = 0;
366 DEBUG(8,("smbd_smb2_find_send: dirpath=<%s> dontdescend=<%s>, "
367 "in_output_buffer_length = %u\n",
368 fsp->fsp_name->base_name, lp_dontdescend(SNUM(conn)),
369 (unsigned int)in_output_buffer_length ));
370 if (in_list(fsp->fsp_name->base_name,lp_dontdescend(SNUM(conn)),
371 conn->case_sensitive)) {
372 dont_descend = true;
375 ask_sharemode = lp_parm_bool(SNUM(conn),
376 "smbd", "search ask sharemode",
377 true);
379 while (true) {
380 bool ok;
381 bool got_exact_match = false;
382 bool out_of_space = false;
383 int space_remaining = in_output_buffer_length - off;
385 SMB_ASSERT(space_remaining >= 0);
387 ok = smbd_dirptr_lanman2_entry(state,
388 conn,
389 fsp->dptr,
390 smbreq->flags2,
391 in_file_name,
392 dirtype,
393 info_level,
394 false, /* requires_resume_key */
395 dont_descend,
396 ask_sharemode,
397 8, /* align to 8 bytes */
398 false, /* no padding */
399 &pdata,
400 base_data,
401 end_data,
402 space_remaining,
403 &out_of_space,
404 &got_exact_match,
405 &last_entry_off,
406 NULL);
408 off = (int)PTR_DIFF(pdata, base_data);
410 if (!ok) {
411 if (num > 0) {
412 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
413 tevent_req_done(req);
414 return tevent_req_post(req, ev);
415 } else if (out_of_space) {
416 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
417 return tevent_req_post(req, ev);
418 } else {
419 tevent_req_nterror(req, empty_status);
420 return tevent_req_post(req, ev);
424 num++;
425 state->out_output_buffer.length = off;
427 if (num < max_count) {
428 continue;
431 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
432 tevent_req_done(req);
433 return tevent_req_post(req, ev);
436 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
437 return tevent_req_post(req, ev);
440 static NTSTATUS smbd_smb2_find_recv(struct tevent_req *req,
441 TALLOC_CTX *mem_ctx,
442 DATA_BLOB *out_output_buffer)
444 NTSTATUS status;
445 struct smbd_smb2_find_state *state = tevent_req_data(req,
446 struct smbd_smb2_find_state);
448 if (tevent_req_is_nterror(req, &status)) {
449 tevent_req_received(req);
450 return status;
453 *out_output_buffer = state->out_output_buffer;
454 talloc_steal(mem_ctx, out_output_buffer->data);
456 tevent_req_received(req);
457 return NT_STATUS_OK;