s3: Retry the join with the short name
[Samba.git] / source3 / smbd / smb2_find.c
blob2fd7720d58d1aa6be3fd9d0fc6b95a98b2aaaa2d
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"
27 static struct tevent_req *smbd_smb2_find_send(TALLOC_CTX *mem_ctx,
28 struct tevent_context *ev,
29 struct smbd_smb2_request *smb2req,
30 uint8_t in_file_info_class,
31 uint8_t in_flags,
32 uint32_t in_file_index,
33 uint64_t in_file_id_volatile,
34 uint32_t in_output_buffer_length,
35 const char *in_file_name);
36 static NTSTATUS smbd_smb2_find_recv(struct tevent_req *req,
37 TALLOC_CTX *mem_ctx,
38 DATA_BLOB *out_output_buffer);
40 static void smbd_smb2_request_find_done(struct tevent_req *subreq);
41 NTSTATUS smbd_smb2_request_process_find(struct smbd_smb2_request *req)
43 const uint8_t *inhdr;
44 const uint8_t *inbody;
45 int i = req->current_idx;
46 size_t expected_body_size = 0x21;
47 size_t body_size;
48 uint8_t in_file_info_class;
49 uint8_t in_flags;
50 uint32_t in_file_index;
51 uint64_t in_file_id_persistent;
52 uint64_t in_file_id_volatile;
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 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
63 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
64 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
67 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
69 body_size = SVAL(inbody, 0x00);
70 if (body_size != expected_body_size) {
71 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
74 in_file_info_class = CVAL(inbody, 0x02);
75 in_flags = CVAL(inbody, 0x03);
76 in_file_index = IVAL(inbody, 0x04);
77 in_file_id_persistent = BVAL(inbody, 0x08);
78 in_file_id_volatile = BVAL(inbody, 0x10);
79 in_file_name_offset = SVAL(inbody, 0x18);
80 in_file_name_length = SVAL(inbody, 0x1A);
81 in_output_buffer_length = IVAL(inbody, 0x1C);
83 if (in_file_name_offset == 0 && in_file_name_length == 0) {
84 /* This is ok */
85 } else if (in_file_name_offset !=
86 (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
87 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
90 if (in_file_name_length > req->in.vector[i+2].iov_len) {
91 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
94 /* The output header is 8 bytes. */
95 if (in_output_buffer_length <= 8) {
96 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
99 DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
100 (unsigned int)in_output_buffer_length ));
102 /* Take into account the output header. */
103 in_output_buffer_length -= 8;
105 in_file_name_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
106 in_file_name_buffer.length = in_file_name_length;
108 ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
109 in_file_name_buffer.data,
110 in_file_name_buffer.length,
111 &in_file_name_string,
112 &in_file_name_string_size, false);
113 if (!ok) {
114 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
117 if (req->compat_chain_fsp) {
118 /* skip check */
119 } else if (in_file_id_persistent != in_file_id_volatile) {
120 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
123 subreq = smbd_smb2_find_send(req,
124 req->sconn->smb2.event_ctx,
125 req,
126 in_file_info_class,
127 in_flags,
128 in_file_index,
129 in_file_id_volatile,
130 in_output_buffer_length,
131 in_file_name_string);
132 if (subreq == NULL) {
133 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
135 tevent_req_set_callback(subreq, smbd_smb2_request_find_done, req);
137 return smbd_smb2_request_pending_queue(req, subreq);
140 static void smbd_smb2_request_find_done(struct tevent_req *subreq)
142 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
143 struct smbd_smb2_request);
144 int i = req->current_idx;
145 uint8_t *outhdr;
146 DATA_BLOB outbody;
147 DATA_BLOB outdyn;
148 uint16_t out_output_buffer_offset;
149 DATA_BLOB out_output_buffer = data_blob_null;
150 NTSTATUS status;
151 NTSTATUS error; /* transport error */
153 status = smbd_smb2_find_recv(subreq,
154 req,
155 &out_output_buffer);
156 TALLOC_FREE(subreq);
157 if (!NT_STATUS_IS_OK(status)) {
158 error = smbd_smb2_request_error(req, status);
159 if (!NT_STATUS_IS_OK(error)) {
160 smbd_server_connection_terminate(req->sconn,
161 nt_errstr(error));
162 return;
164 return;
167 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
169 outhdr = (uint8_t *)req->out.vector[i].iov_base;
171 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
172 if (outbody.data == NULL) {
173 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
174 if (!NT_STATUS_IS_OK(error)) {
175 smbd_server_connection_terminate(req->sconn,
176 nt_errstr(error));
177 return;
179 return;
182 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
183 SSVAL(outbody.data, 0x02,
184 out_output_buffer_offset); /* output buffer offset */
185 SIVAL(outbody.data, 0x04,
186 out_output_buffer.length); /* output buffer length */
188 DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
189 (unsigned int)out_output_buffer.length ));
191 outdyn = out_output_buffer;
193 error = smbd_smb2_request_done(req, outbody, &outdyn);
194 if (!NT_STATUS_IS_OK(error)) {
195 smbd_server_connection_terminate(req->sconn,
196 nt_errstr(error));
197 return;
201 struct smbd_smb2_find_state {
202 struct smbd_smb2_request *smb2req;
203 DATA_BLOB out_output_buffer;
206 static struct tevent_req *smbd_smb2_find_send(TALLOC_CTX *mem_ctx,
207 struct tevent_context *ev,
208 struct smbd_smb2_request *smb2req,
209 uint8_t in_file_info_class,
210 uint8_t in_flags,
211 uint32_t in_file_index,
212 uint64_t in_file_id_volatile,
213 uint32_t in_output_buffer_length,
214 const char *in_file_name)
216 struct tevent_req *req;
217 struct smbd_smb2_find_state *state;
218 struct smb_request *smbreq;
219 connection_struct *conn = smb2req->tcon->compat_conn;
220 files_struct *fsp;
221 NTSTATUS status;
222 NTSTATUS empty_status;
223 uint32_t info_level;
224 uint32_t max_count;
225 char *pdata;
226 char *base_data;
227 char *end_data;
228 int last_entry_off = 0;
229 int off = 0;
230 uint32_t num = 0;
231 uint32_t dirtype = aHIDDEN | aSYSTEM | aDIR;
232 bool dont_descend = false;
233 bool ask_sharemode = true;
235 req = tevent_req_create(mem_ctx, &state,
236 struct smbd_smb2_find_state);
237 if (req == NULL) {
238 return NULL;
240 state->smb2req = smb2req;
241 state->out_output_buffer = data_blob_null;
243 DEBUG(10,("smbd_smb2_find_send: file_id[0x%016llX]\n",
244 (unsigned long long)in_file_id_volatile));
246 smbreq = smbd_smb2_fake_smb_request(smb2req);
247 if (tevent_req_nomem(smbreq, req)) {
248 return tevent_req_post(req, ev);
251 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
252 if (fsp == NULL) {
253 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
254 return tevent_req_post(req, ev);
256 if (conn != fsp->conn) {
257 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
258 return tevent_req_post(req, ev);
260 if (smb2req->session->vuid != fsp->vuid) {
261 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
262 return tevent_req_post(req, ev);
265 if (!fsp->is_directory) {
266 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
267 return tevent_req_post(req, ev);
270 if (strcmp(in_file_name, "") == 0) {
271 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
272 return tevent_req_post(req, ev);
274 if (strcmp(in_file_name, "\\") == 0) {
275 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
276 return tevent_req_post(req, ev);
278 if (strcmp(in_file_name, "/") == 0) {
279 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
280 return tevent_req_post(req, ev);
283 if (in_output_buffer_length > 0x10000) {
284 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
285 return tevent_req_post(req, ev);
288 switch (in_file_info_class) {
289 case SMB2_FIND_DIRECTORY_INFO:
290 info_level = SMB_FIND_FILE_DIRECTORY_INFO;
291 break;
293 case SMB2_FIND_FULL_DIRECTORY_INFO:
294 info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
295 break;
297 case SMB2_FIND_BOTH_DIRECTORY_INFO:
298 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
299 break;
301 case SMB2_FIND_NAME_INFO:
302 info_level = SMB_FIND_FILE_NAMES_INFO;
303 break;
305 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
306 info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
307 break;
309 case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
310 info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
311 break;
313 default:
314 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
315 return tevent_req_post(req, ev);
318 if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
319 dptr_CloseDir(fsp);
322 if (fsp->dptr == NULL) {
323 bool wcard_has_wild;
325 if (!(fsp->access_mask & SEC_DIR_LIST)) {
326 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
327 return tevent_req_post(req, ev);
330 wcard_has_wild = ms_has_wild(in_file_name);
332 status = dptr_create(conn,
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(SNUM(conn)),
385 (unsigned int)in_output_buffer_length ));
386 if (in_list(fsp->fsp_name->base_name,lp_dontdescend(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;