vfs: Implement a sys_acl_blob_get_{fd,file} for POSIX ACL backends
[kai/samba.git] / source3 / smbd / smb2_getinfo.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) Jeremy Allison 2010
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "trans2.h"
27 #include "../lib/util/tevent_ntstatus.h"
28
29 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
30                                                  struct tevent_context *ev,
31                                                  struct smbd_smb2_request *smb2req,
32                                                  struct files_struct *in_fsp,
33                                                  uint8_t in_info_type,
34                                                  uint8_t in_file_info_class,
35                                                  uint32_t in_output_buffer_length,
36                                                  DATA_BLOB in_input_buffer,
37                                                  uint32_t in_additional_information,
38                                                  uint32_t in_flags);
39 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
40                                        TALLOC_CTX *mem_ctx,
41                                        DATA_BLOB *out_output_buffer,
42                                        NTSTATUS *p_call_status);
43
44 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq);
45 NTSTATUS smbd_smb2_request_process_getinfo(struct smbd_smb2_request *req)
46 {
47         NTSTATUS status;
48         const uint8_t *inbody;
49         uint8_t in_info_type;
50         uint8_t in_file_info_class;
51         uint32_t in_output_buffer_length;
52         uint16_t in_input_buffer_offset;
53         uint32_t in_input_buffer_length;
54         DATA_BLOB in_input_buffer;
55         uint32_t in_additional_information;
56         uint32_t in_flags;
57         uint64_t in_file_id_persistent;
58         uint64_t in_file_id_volatile;
59         struct files_struct *in_fsp;
60         struct tevent_req *subreq;
61
62         status = smbd_smb2_request_verify_sizes(req, 0x29);
63         if (!NT_STATUS_IS_OK(status)) {
64                 return smbd_smb2_request_error(req, status);
65         }
66         inbody = SMBD_SMB2_IN_BODY_PTR(req);
67
68         in_info_type                    = CVAL(inbody, 0x02);
69         in_file_info_class              = CVAL(inbody, 0x03);
70         in_output_buffer_length         = IVAL(inbody, 0x04);
71         in_input_buffer_offset          = SVAL(inbody, 0x08);
72         /* 0x0A 2 bytes reserved */
73         in_input_buffer_length          = IVAL(inbody, 0x0C);
74         in_additional_information       = IVAL(inbody, 0x10);
75         in_flags                        = IVAL(inbody, 0x14);
76         in_file_id_persistent           = BVAL(inbody, 0x18);
77         in_file_id_volatile             = BVAL(inbody, 0x20);
78
79         if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
80                 /* This is ok */
81         } else if (in_input_buffer_offset !=
82                    (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
83                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
84         }
85
86         if (in_input_buffer_length > SMBD_SMB2_IN_DYN_LEN(req)) {
87                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
88         }
89
90         in_input_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
91         in_input_buffer.length = in_input_buffer_length;
92
93         if (in_input_buffer.length > req->sconn->smb2.max_trans) {
94                 DEBUG(2,("smbd_smb2_request_process_getinfo: "
95                          "client ignored max trans: %s: 0x%08X: 0x%08X\n",
96                          __location__, (unsigned)in_input_buffer.length,
97                          (unsigned)req->sconn->smb2.max_trans));
98                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
99         }
100         if (in_output_buffer_length > req->sconn->smb2.max_trans) {
101                 DEBUG(2,("smbd_smb2_request_process_getinfo: "
102                          "client ignored max trans: %s: 0x%08X: 0x%08X\n",
103                          __location__, in_output_buffer_length,
104                          req->sconn->smb2.max_trans));
105                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
106         }
107
108         status = smbd_smb2_request_verify_creditcharge(req,
109                         MAX(in_input_buffer.length,in_output_buffer_length));
110         if (!NT_STATUS_IS_OK(status)) {
111                 return smbd_smb2_request_error(req, status);
112         }
113
114         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
115         if (in_fsp == NULL) {
116                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
117         }
118
119         subreq = smbd_smb2_getinfo_send(req, req->sconn->ev_ctx,
120                                         req, in_fsp,
121                                         in_info_type,
122                                         in_file_info_class,
123                                         in_output_buffer_length,
124                                         in_input_buffer,
125                                         in_additional_information,
126                                         in_flags);
127         if (subreq == NULL) {
128                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
129         }
130         tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
131
132         return smbd_smb2_request_pending_queue(req, subreq, 500);
133 }
134
135 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
136 {
137         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
138                                         struct smbd_smb2_request);
139         DATA_BLOB outbody;
140         DATA_BLOB outdyn;
141         uint16_t out_output_buffer_offset;
142         DATA_BLOB out_output_buffer = data_blob_null;
143         NTSTATUS status;
144         NTSTATUS call_status = NT_STATUS_OK;
145         NTSTATUS error; /* transport error */
146
147         status = smbd_smb2_getinfo_recv(subreq,
148                                         req,
149                                         &out_output_buffer,
150                                         &call_status);
151         TALLOC_FREE(subreq);
152         if (!NT_STATUS_IS_OK(status)) {
153                 error = smbd_smb2_request_error(req, status);
154                 if (!NT_STATUS_IS_OK(error)) {
155                         smbd_server_connection_terminate(req->sconn,
156                                                          nt_errstr(error));
157                         return;
158                 }
159                 return;
160         }
161
162         if (!NT_STATUS_IS_OK(call_status)) {
163                 /* Return a specific error with data. */
164                 error = smbd_smb2_request_error_ex(req,
165                                                 call_status,
166                                                 &out_output_buffer,
167                                                 __location__);
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         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
177
178         outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
179         if (outbody.data == NULL) {
180                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
181                 if (!NT_STATUS_IS_OK(error)) {
182                         smbd_server_connection_terminate(req->sconn,
183                                                          nt_errstr(error));
184                         return;
185                 }
186                 return;
187         }
188
189         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
190         SSVAL(outbody.data, 0x02,
191               out_output_buffer_offset);        /* output buffer offset */
192         SIVAL(outbody.data, 0x04,
193               out_output_buffer.length);        /* output buffer length */
194
195         outdyn = out_output_buffer;
196
197         error = smbd_smb2_request_done(req, outbody, &outdyn);
198         if (!NT_STATUS_IS_OK(error)) {
199                 smbd_server_connection_terminate(req->sconn,
200                                                  nt_errstr(error));
201                 return;
202         }
203 }
204
205 struct smbd_smb2_getinfo_state {
206         struct smbd_smb2_request *smb2req;
207         NTSTATUS status;
208         DATA_BLOB out_output_buffer;
209 };
210
211 static void smb2_ipc_getinfo(struct tevent_req *req,
212                                 struct smbd_smb2_getinfo_state *state,
213                                 struct tevent_context *ev,
214                                 uint8_t in_info_type,
215                                 uint8_t in_file_info_class)
216 {
217         /* We want to reply to SMB2_GETINFO_FILE
218            with a class of SMB2_FILE_STANDARD_INFO as
219            otherwise a Win7 client issues this request
220            twice (2xroundtrips) if we return NOT_SUPPORTED.
221            NB. We do the same for SMB1 in call_trans2qpipeinfo() */
222
223         if (in_info_type == 0x01 && /* SMB2_GETINFO_FILE */
224                         in_file_info_class == 0x05) { /* SMB2_FILE_STANDARD_INFO */
225                 state->out_output_buffer = data_blob_talloc(state,
226                                                 NULL, 24);
227                 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
228                         return;
229                 }
230
231                 memset(state->out_output_buffer.data,0,24);
232                 SOFF_T(state->out_output_buffer.data,0,4096LL);
233                 SIVAL(state->out_output_buffer.data,16,1);
234                 SIVAL(state->out_output_buffer.data,20,1);
235                 tevent_req_done(req);
236         } else {
237                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
238         }
239 }
240
241 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
242                                                  struct tevent_context *ev,
243                                                  struct smbd_smb2_request *smb2req,
244                                                  struct files_struct *fsp,
245                                                  uint8_t in_info_type,
246                                                  uint8_t in_file_info_class,
247                                                  uint32_t in_output_buffer_length,
248                                                  DATA_BLOB in_input_buffer,
249                                                  uint32_t in_additional_information,
250                                                  uint32_t in_flags)
251 {
252         struct tevent_req *req;
253         struct smbd_smb2_getinfo_state *state;
254         struct smb_request *smbreq;
255         connection_struct *conn = smb2req->tcon->compat;
256         NTSTATUS status;
257
258         req = tevent_req_create(mem_ctx, &state,
259                                 struct smbd_smb2_getinfo_state);
260         if (req == NULL) {
261                 return NULL;
262         }
263         state->smb2req = smb2req;
264         state->status = NT_STATUS_OK;
265         state->out_output_buffer = data_blob_null;
266
267         DEBUG(10,("smbd_smb2_getinfo_send: %s - %s\n",
268                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
269
270         smbreq = smbd_smb2_fake_smb_request(smb2req);
271         if (tevent_req_nomem(smbreq, req)) {
272                 return tevent_req_post(req, ev);
273         }
274
275         if (IS_IPC(conn)) {
276                 smb2_ipc_getinfo(req, state, ev,
277                         in_info_type, in_file_info_class);
278                 return tevent_req_post(req, ev);
279         }
280
281         switch (in_info_type) {
282         case 0x01:/* SMB2_GETINFO_FILE */
283         {
284                 uint16_t file_info_level;
285                 char *data = NULL;
286                 unsigned int data_size = 0;
287                 bool delete_pending = false;
288                 struct timespec write_time_ts;
289                 struct file_id fileid;
290                 struct ea_list *ea_list = NULL;
291                 int lock_data_count = 0;
292                 char *lock_data = NULL;
293
294                 ZERO_STRUCT(write_time_ts);
295
296                 switch (in_file_info_class) {
297                 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
298                         file_info_level = 0xFF00 | in_file_info_class;
299                         break;
300
301                 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
302                         file_info_level = 0xFF00 | in_file_info_class;
303                         break;
304
305                 default:
306                         /* the levels directly map to the passthru levels */
307                         file_info_level = in_file_info_class + 1000;
308                         break;
309                 }
310
311                 if (fsp->fake_file_handle) {
312                         /*
313                          * This is actually for the QUOTA_FAKE_FILE --metze
314                          */
315
316                         /* We know this name is ok, it's already passed the checks. */
317
318                 } else if (fsp->fh->fd == -1) {
319                         /*
320                          * This is actually a QFILEINFO on a directory
321                          * handle (returned from an NT SMB). NT5.0 seems
322                          * to do this call. JRA.
323                          */
324
325                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
326                                 /* Always do lstat for UNIX calls. */
327                                 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
328                                         DEBUG(3,("smbd_smb2_getinfo_send: "
329                                                  "SMB_VFS_LSTAT of %s failed "
330                                                  "(%s)\n", fsp_str_dbg(fsp),
331                                                  strerror(errno)));
332                                         status = map_nt_error_from_unix(errno);
333                                         tevent_req_nterror(req, status);
334                                         return tevent_req_post(req, ev);
335                                 }
336                         } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
337                                 DEBUG(3,("smbd_smb2_getinfo_send: "
338                                          "SMB_VFS_STAT of %s failed (%s)\n",
339                                          fsp_str_dbg(fsp),
340                                          strerror(errno)));
341                                 status = map_nt_error_from_unix(errno);
342                                 tevent_req_nterror(req, status);
343                                 return tevent_req_post(req, ev);
344                         }
345
346                         fileid = vfs_file_id_from_sbuf(conn,
347                                                        &fsp->fsp_name->st);
348                         get_file_infos(fileid, fsp->name_hash,
349                                 &delete_pending, &write_time_ts);
350                 } else {
351                         /*
352                          * Original code - this is an open file.
353                          */
354
355                         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
356                                 DEBUG(3, ("smbd_smb2_getinfo_send: "
357                                           "fstat of %s failed (%s)\n",
358                                           fsp_fnum_dbg(fsp), strerror(errno)));
359                                 status = map_nt_error_from_unix(errno);
360                                 tevent_req_nterror(req, status);
361                                 return tevent_req_post(req, ev);
362                         }
363                         fileid = vfs_file_id_from_sbuf(conn,
364                                                        &fsp->fsp_name->st);
365                         get_file_infos(fileid, fsp->name_hash,
366                                 &delete_pending, &write_time_ts);
367                 }
368
369                 status = smbd_do_qfilepathinfo(conn, state,
370                                                file_info_level,
371                                                fsp,
372                                                fsp->fsp_name,
373                                                delete_pending,
374                                                write_time_ts,
375                                                ea_list,
376                                                lock_data_count,
377                                                lock_data,
378                                                STR_UNICODE,
379                                                in_output_buffer_length,
380                                                &data,
381                                                &data_size);
382                 if (!NT_STATUS_IS_OK(status)) {
383                         SAFE_FREE(data);
384                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
385                                 status = NT_STATUS_INVALID_INFO_CLASS;
386                         }
387                         tevent_req_nterror(req, status);
388                         return tevent_req_post(req, ev);
389                 }
390                 if (data_size > 0) {
391                         state->out_output_buffer = data_blob_talloc(state,
392                                                                     data,
393                                                                     data_size);
394                         SAFE_FREE(data);
395                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
396                                 return tevent_req_post(req, ev);
397                         }
398                 }
399                 SAFE_FREE(data);
400                 break;
401         }
402
403         case 0x02:/* SMB2_GETINFO_FS */
404         {
405                 uint16_t file_info_level;
406                 char *data = NULL;
407                 int data_size = 0;
408
409                 /* the levels directly map to the passthru levels */
410                 file_info_level = in_file_info_class + 1000;
411
412                 status = smbd_do_qfsinfo(conn, state,
413                                          file_info_level,
414                                          STR_UNICODE,
415                                          in_output_buffer_length,
416                                          &data,
417                                          &data_size);
418                 if (!NT_STATUS_IS_OK(status)) {
419                         SAFE_FREE(data);
420                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
421                                 status = NT_STATUS_INVALID_INFO_CLASS;
422                         }
423                         tevent_req_nterror(req, status);
424                         return tevent_req_post(req, ev);
425                 }
426                 if (data_size > 0) {
427                         state->out_output_buffer = data_blob_talloc(state,
428                                                                     data,
429                                                                     data_size);
430                         SAFE_FREE(data);
431                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
432                                 return tevent_req_post(req, ev);
433                         }
434                 }
435                 SAFE_FREE(data);
436                 break;
437         }
438
439         case 0x03:/* SMB2_GETINFO_SEC */
440         {
441                 uint8_t *p_marshalled_sd = NULL;
442                 size_t sd_size = 0;
443
444                 status = smbd_do_query_security_desc(conn,
445                                 state,
446                                 fsp,
447                                 /* Security info wanted. */
448                                 in_additional_information,
449                                 in_output_buffer_length,
450                                 &p_marshalled_sd,
451                                 &sd_size);
452
453                 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
454                         /* Return needed size. */
455                         state->out_output_buffer = data_blob_talloc(state,
456                                                                     NULL,
457                                                                     4);
458                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
459                                 return tevent_req_post(req, ev);
460                         }
461                         SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
462                         state->status = NT_STATUS_BUFFER_TOO_SMALL;
463                         break;
464                 }
465                 if (!NT_STATUS_IS_OK(status)) {
466                         DEBUG(10,("smbd_smb2_getinfo_send: "
467                                  "smbd_do_query_security_desc of %s failed "
468                                  "(%s)\n", fsp_str_dbg(fsp),
469                                  nt_errstr(status)));
470                         tevent_req_nterror(req, status);
471                         return tevent_req_post(req, ev);
472                 }
473
474                 if (sd_size > 0) {
475                         state->out_output_buffer = data_blob_talloc(state,
476                                                                     p_marshalled_sd,
477                                                                     sd_size);
478                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
479                                 return tevent_req_post(req, ev);
480                         }
481                 }
482                 break;
483         }
484
485         default:
486                 DEBUG(10,("smbd_smb2_getinfo_send: "
487                         "unknown in_info_type of %u "
488                         " for file %s\n",
489                         (unsigned int)in_info_type,
490                         fsp_str_dbg(fsp) ));
491
492                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
493                 return tevent_req_post(req, ev);
494         }
495
496         tevent_req_done(req);
497         return tevent_req_post(req, ev);
498 }
499
500 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
501                                        TALLOC_CTX *mem_ctx,
502                                        DATA_BLOB *out_output_buffer,
503                                        NTSTATUS *pstatus)
504 {
505         NTSTATUS status;
506         struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
507                                                 struct smbd_smb2_getinfo_state);
508
509         if (tevent_req_is_nterror(req, &status)) {
510                 tevent_req_received(req);
511                 return status;
512         }
513
514         *out_output_buffer = state->out_output_buffer;
515         talloc_steal(mem_ctx, out_output_buffer->data);
516         *pstatus = state->status;
517
518         tevent_req_received(req);
519         return NT_STATUS_OK;
520 }