s3-smbd: Call sys_acl_free_acl() directly rather than via the VFS
[samba.git] / source3 / smbd / smb2_find.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
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.
11
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.
16
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/>.
19 */
20
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"
27
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);
40
41 static void smbd_smb2_request_find_done(struct tevent_req *subreq);
42 NTSTATUS smbd_smb2_request_process_find(struct smbd_smb2_request *req)
43 {
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;
60
61         status = smbd_smb2_request_verify_sizes(req, 0x21);
62         if (!NT_STATUS_IS_OK(status)) {
63                 return smbd_smb2_request_error(req, status);
64         }
65         inbody = SMBD_SMB2_IN_BODY_PTR(req);
66
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);
75
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);
81         }
82
83         if (in_file_name_length > SMBD_SMB2_IN_DYN_LEN(req)) {
84                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
85         }
86
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);
90         }
91
92         DEBUG(10,("smbd_smb2_request_find_done: in_output_buffer_length = %u\n",
93                 (unsigned int)in_output_buffer_length ));
94
95         /* Take into account the output header. */
96         in_output_buffer_length -= 8;
97
98         in_file_name_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
99         in_file_name_buffer.length = in_file_name_length;
100
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);
108         }
109
110         if (in_file_name_buffer.length == 0) {
111                 in_file_name_string_size = 0;
112         }
113
114         if (strlen(in_file_name_string) != in_file_name_string_size) {
115                 return smbd_smb2_request_error(req, NT_STATUS_OBJECT_NAME_INVALID);
116         }
117
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);
121         }
122
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);
132         }
133         tevent_req_set_callback(subreq, smbd_smb2_request_find_done, req);
134
135         return smbd_smb2_request_pending_queue(req, subreq, 500);
136 }
137
138 static void smbd_smb2_request_find_done(struct tevent_req *subreq)
139 {
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 */
148
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;
159                 }
160                 return;
161         }
162
163         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
164
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;
172                 }
173                 return;
174         }
175
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 */
181
182         DEBUG(10,("smbd_smb2_request_find_done: out_output_buffer.length = %u\n",
183                 (unsigned int)out_output_buffer.length ));
184
185         outdyn = out_output_buffer;
186
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;
192         }
193 }
194
195 struct smbd_smb2_find_state {
196         struct smbd_smb2_request *smb2req;
197         DATA_BLOB out_output_buffer;
198 };
199
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)
209 {
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
228         req = tevent_req_create(mem_ctx, &state,
229                                 struct smbd_smb2_find_state);
230         if (req == NULL) {
231                 return NULL;
232         }
233         state->smb2req = smb2req;
234         state->out_output_buffer = data_blob_null;
235
236         DEBUG(10,("smbd_smb2_find_send: %s - %s\n",
237                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
238
239         smbreq = smbd_smb2_fake_smb_request(smb2req);
240         if (tevent_req_nomem(smbreq, req)) {
241                 return tevent_req_post(req, ev);
242         }
243
244         if (!fsp->is_directory) {
245                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
246                 return tevent_req_post(req, ev);
247         }
248
249         if (strcmp(in_file_name, "") == 0) {
250                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
251                 return tevent_req_post(req, ev);
252         }
253         if (strcmp(in_file_name, "\\") == 0) {
254                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
255                 return tevent_req_post(req, ev);
256         }
257         if (strcmp(in_file_name, "/") == 0) {
258                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_INVALID);
259                 return tevent_req_post(req, ev);
260         }
261
262         if (in_output_buffer_length > smb2req->sconn->smb2.max_trans) {
263                 DEBUG(2,("smbd_smb2_find_send: "
264                          "client ignored max trans:%s: 0x%08X: 0x%08X\n",
265                          __location__, in_output_buffer_length,
266                          smb2req->sconn->smb2.max_trans));
267                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
268                 return tevent_req_post(req, ev);
269         }
270
271         status = smbd_smb2_request_verify_creditcharge(smb2req,
272                                         in_output_buffer_length);
273
274         if (!NT_STATUS_IS_OK(status)) {
275                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
276                 return tevent_req_post(req, ev);
277         }
278
279         switch (in_file_info_class) {
280         case SMB2_FIND_DIRECTORY_INFO:
281                 info_level = SMB_FIND_FILE_DIRECTORY_INFO;
282                 break;
283
284         case SMB2_FIND_FULL_DIRECTORY_INFO:
285                 info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
286                 break;
287
288         case SMB2_FIND_BOTH_DIRECTORY_INFO:
289                 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
290                 break;
291
292         case SMB2_FIND_NAME_INFO:
293                 info_level = SMB_FIND_FILE_NAMES_INFO;
294                 break;
295
296         case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
297                 info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO;
298                 break;
299
300         case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
301                 info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO;
302                 break;
303
304         default:
305                 tevent_req_nterror(req, NT_STATUS_INVALID_INFO_CLASS);
306                 return tevent_req_post(req, ev);
307         }
308
309         if (in_flags & SMB2_CONTINUE_FLAG_REOPEN) {
310                 dptr_CloseDir(fsp);
311         }
312
313         if (fsp->dptr == NULL) {
314                 bool wcard_has_wild;
315
316                 wcard_has_wild = ms_has_wild(in_file_name);
317
318                 status = dptr_create(conn,
319                                      NULL, /* req */
320                                      fsp,
321                                      fsp->fsp_name->base_name,
322                                      false, /* old_handle */
323                                      false, /* expect_close */
324                                      0, /* spid */
325                                      in_file_name, /* wcard */
326                                      wcard_has_wild,
327                                      dirtype,
328                                      &fsp->dptr);
329                 if (!NT_STATUS_IS_OK(status)) {
330                         tevent_req_nterror(req, status);
331                         return tevent_req_post(req, ev);
332                 }
333
334                 empty_status = NT_STATUS_NO_SUCH_FILE;
335         } else {
336                 empty_status = STATUS_NO_MORE_FILES;
337         }
338
339         if (in_flags & SMB2_CONTINUE_FLAG_RESTART) {
340                 dptr_SeekDir(fsp->dptr, 0);
341         }
342
343         if (in_flags & SMB2_CONTINUE_FLAG_SINGLE) {
344                 max_count = 1;
345         } else {
346                 max_count = UINT16_MAX;
347         }
348
349 #define DIR_ENTRY_SAFETY_MARGIN 4096
350
351         state->out_output_buffer = data_blob_talloc(state, NULL,
352                         in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN);
353         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
354                 return tevent_req_post(req, ev);
355         }
356
357         state->out_output_buffer.length = 0;
358         pdata = (char *)state->out_output_buffer.data;
359         base_data = pdata;
360         /*
361          * end_data must include the safety margin as it's what is
362          * used to determine if pushed strings have been truncated.
363          */
364         end_data = pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1;
365         last_entry_off = 0;
366         off = 0;
367         num = 0;
368
369         DEBUG(8,("smbd_smb2_find_send: dirpath=<%s> dontdescend=<%s>, "
370                 "in_output_buffer_length = %u\n",
371                 fsp->fsp_name->base_name, lp_dontdescend(talloc_tos(), SNUM(conn)),
372                 (unsigned int)in_output_buffer_length ));
373         if (in_list(fsp->fsp_name->base_name,lp_dontdescend(talloc_tos(), SNUM(conn)),
374                         conn->case_sensitive)) {
375                 dont_descend = true;
376         }
377
378         ask_sharemode = lp_parm_bool(SNUM(conn),
379                                      "smbd", "search ask sharemode",
380                                      true);
381
382         while (true) {
383                 bool ok;
384                 bool got_exact_match = false;
385                 bool out_of_space = false;
386                 int space_remaining = in_output_buffer_length - off;
387
388                 SMB_ASSERT(space_remaining >= 0);
389
390                 ok = smbd_dirptr_lanman2_entry(state,
391                                                conn,
392                                                fsp->dptr,
393                                                smbreq->flags2,
394                                                in_file_name,
395                                                dirtype,
396                                                info_level,
397                                                false, /* requires_resume_key */
398                                                dont_descend,
399                                                ask_sharemode,
400                                                8, /* align to 8 bytes */
401                                                false, /* no padding */
402                                                &pdata,
403                                                base_data,
404                                                end_data,
405                                                space_remaining,
406                                                &out_of_space,
407                                                &got_exact_match,
408                                                &last_entry_off,
409                                                NULL);
410
411                 off = (int)PTR_DIFF(pdata, base_data);
412
413                 if (!ok) {
414                         if (num > 0) {
415                                 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
416                                 tevent_req_done(req);
417                                 return tevent_req_post(req, ev);
418                         } else if (out_of_space) {
419                                 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
420                                 return tevent_req_post(req, ev);
421                         } else {
422                                 tevent_req_nterror(req, empty_status);
423                                 return tevent_req_post(req, ev);
424                         }
425                 }
426
427                 num++;
428                 state->out_output_buffer.length = off;
429
430                 if (num < max_count) {
431                         continue;
432                 }
433
434                 SIVAL(state->out_output_buffer.data, last_entry_off, 0);
435                 tevent_req_done(req);
436                 return tevent_req_post(req, ev);
437         }
438
439         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
440         return tevent_req_post(req, ev);
441 }
442
443 static NTSTATUS smbd_smb2_find_recv(struct tevent_req *req,
444                                     TALLOC_CTX *mem_ctx,
445                                     DATA_BLOB *out_output_buffer)
446 {
447         NTSTATUS status;
448         struct smbd_smb2_find_state *state = tevent_req_data(req,
449                                              struct smbd_smb2_find_state);
450
451         if (tevent_req_is_nterror(req, &status)) {
452                 tevent_req_received(req);
453                 return status;
454         }
455
456         *out_output_buffer = state->out_output_buffer;
457         talloc_steal(mem_ctx, out_output_buffer->data);
458
459         tevent_req_received(req);
460         return NT_STATUS_OK;
461 }