714a6bdf08bde282572668e2f992d5ffa0a26e64
[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         int i = req->current_idx;
50         uint8_t in_info_type;
51         uint8_t in_file_info_class;
52         uint32_t in_output_buffer_length;
53         uint16_t in_input_buffer_offset;
54         uint32_t in_input_buffer_length;
55         DATA_BLOB in_input_buffer;
56         uint32_t in_additional_information;
57         uint32_t in_flags;
58         uint64_t in_file_id_persistent;
59         uint64_t in_file_id_volatile;
60         struct files_struct *in_fsp;
61         struct tevent_req *subreq;
62
63         status = smbd_smb2_request_verify_sizes(req, 0x29);
64         if (!NT_STATUS_IS_OK(status)) {
65                 return smbd_smb2_request_error(req, status);
66         }
67         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
68
69         in_info_type                    = CVAL(inbody, 0x02);
70         in_file_info_class              = CVAL(inbody, 0x03);
71         in_output_buffer_length         = IVAL(inbody, 0x04);
72         in_input_buffer_offset          = SVAL(inbody, 0x08);
73         /* 0x0A 2 bytes reserved */
74         in_input_buffer_length          = IVAL(inbody, 0x0C);
75         in_additional_information       = IVAL(inbody, 0x10);
76         in_flags                        = IVAL(inbody, 0x14);
77         in_file_id_persistent           = BVAL(inbody, 0x18);
78         in_file_id_volatile             = BVAL(inbody, 0x20);
79
80         if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
81                 /* This is ok */
82         } else if (in_input_buffer_offset !=
83                    (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
84                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
85         }
86
87         if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
88                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
89         }
90
91         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
92         in_input_buffer.length = in_input_buffer_length;
93
94         if (in_input_buffer.length > req->sconn->smb2.max_trans) {
95                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
96         }
97         if (in_output_buffer_length > req->sconn->smb2.max_trans) {
98                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
99         }
100
101         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
102         if (in_fsp == NULL) {
103                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
104         }
105
106         subreq = smbd_smb2_getinfo_send(req, req->sconn->smb2.event_ctx,
107                                         req, in_fsp,
108                                         in_info_type,
109                                         in_file_info_class,
110                                         in_output_buffer_length,
111                                         in_input_buffer,
112                                         in_additional_information,
113                                         in_flags);
114         if (subreq == NULL) {
115                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
116         }
117         tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
118
119         return smbd_smb2_request_pending_queue(req, subreq);
120 }
121
122 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
123 {
124         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
125                                         struct smbd_smb2_request);
126         int i = req->current_idx;
127         uint8_t *outhdr;
128         DATA_BLOB outbody;
129         DATA_BLOB outdyn;
130         uint16_t out_output_buffer_offset;
131         DATA_BLOB out_output_buffer = data_blob_null;
132         NTSTATUS status;
133         NTSTATUS call_status = NT_STATUS_OK;
134         NTSTATUS error; /* transport error */
135
136         status = smbd_smb2_getinfo_recv(subreq,
137                                         req,
138                                         &out_output_buffer,
139                                         &call_status);
140         TALLOC_FREE(subreq);
141         if (!NT_STATUS_IS_OK(status)) {
142                 error = smbd_smb2_request_error(req, status);
143                 if (!NT_STATUS_IS_OK(error)) {
144                         smbd_server_connection_terminate(req->sconn,
145                                                          nt_errstr(error));
146                         return;
147                 }
148                 return;
149         }
150
151         /* some GetInfo responses set STATUS_BUFFER_OVERFLOW and return partial,
152            but valid data */
153         if (!(NT_STATUS_IS_OK(call_status) ||
154               NT_STATUS_EQUAL(call_status, STATUS_BUFFER_OVERFLOW))) {
155                 /* Return a specific error with data. */
156                 error = smbd_smb2_request_error_ex(req,
157                                                 call_status,
158                                                 &out_output_buffer,
159                                                 __location__);
160                 if (!NT_STATUS_IS_OK(error)) {
161                         smbd_server_connection_terminate(req->sconn,
162                                                          nt_errstr(error));
163                         return;
164                 }
165                 return;
166         }
167
168         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
169
170         outhdr = (uint8_t *)req->out.vector[i].iov_base;
171
172         outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
173         if (outbody.data == NULL) {
174                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
175                 if (!NT_STATUS_IS_OK(error)) {
176                         smbd_server_connection_terminate(req->sconn,
177                                                          nt_errstr(error));
178                         return;
179                 }
180                 return;
181         }
182
183         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
184         SSVAL(outbody.data, 0x02,
185               out_output_buffer_offset);        /* output buffer offset */
186         SIVAL(outbody.data, 0x04,
187               out_output_buffer.length);        /* output buffer length */
188
189         outdyn = out_output_buffer;
190
191         error = smbd_smb2_request_done_ex(req, call_status, outbody, &outdyn, __location__);
192         if (!NT_STATUS_IS_OK(error)) {
193                 smbd_server_connection_terminate(req->sconn,
194                                                  nt_errstr(error));
195                 return;
196         }
197 }
198
199 struct smbd_smb2_getinfo_state {
200         struct smbd_smb2_request *smb2req;
201         NTSTATUS status;
202         DATA_BLOB out_output_buffer;
203 };
204
205 static void smb2_ipc_getinfo(struct tevent_req *req,
206                                 struct smbd_smb2_getinfo_state *state,
207                                 struct tevent_context *ev,
208                                 uint8_t in_info_type,
209                                 uint8_t in_file_info_class)
210 {
211         /* We want to reply to SMB2_GETINFO_FILE
212            with a class of SMB2_FILE_STANDARD_INFO as
213            otherwise a Win7 client issues this request
214            twice (2xroundtrips) if we return NOT_SUPPORTED.
215            NB. We do the same for SMB1 in call_trans2qpipeinfo() */
216
217         if (in_info_type == 0x01 && /* SMB2_GETINFO_FILE */
218                         in_file_info_class == 0x05) { /* SMB2_FILE_STANDARD_INFO */
219                 state->out_output_buffer = data_blob_talloc(state,
220                                                 NULL, 24);
221                 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
222                         return;
223                 }
224
225                 memset(state->out_output_buffer.data,0,24);
226                 SOFF_T(state->out_output_buffer.data,0,4096LL);
227                 SIVAL(state->out_output_buffer.data,16,1);
228                 SIVAL(state->out_output_buffer.data,20,1);
229                 tevent_req_done(req);
230         } else {
231                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
232         }
233 }
234
235 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
236                                                  struct tevent_context *ev,
237                                                  struct smbd_smb2_request *smb2req,
238                                                  struct files_struct *fsp,
239                                                  uint8_t in_info_type,
240                                                  uint8_t in_file_info_class,
241                                                  uint32_t in_output_buffer_length,
242                                                  DATA_BLOB in_input_buffer,
243                                                  uint32_t in_additional_information,
244                                                  uint32_t in_flags)
245 {
246         struct tevent_req *req;
247         struct smbd_smb2_getinfo_state *state;
248         struct smb_request *smbreq;
249         connection_struct *conn = smb2req->tcon->compat_conn;
250         NTSTATUS status;
251
252         req = tevent_req_create(mem_ctx, &state,
253                                 struct smbd_smb2_getinfo_state);
254         if (req == NULL) {
255                 return NULL;
256         }
257         state->smb2req = smb2req;
258         state->status = NT_STATUS_OK;
259         state->out_output_buffer = data_blob_null;
260
261         DEBUG(10,("smbd_smb2_getinfo_send: %s - fnum[%d]\n",
262                   fsp_str_dbg(fsp), fsp->fnum));
263
264         smbreq = smbd_smb2_fake_smb_request(smb2req);
265         if (tevent_req_nomem(smbreq, req)) {
266                 return tevent_req_post(req, ev);
267         }
268
269         if (IS_IPC(conn)) {
270                 smb2_ipc_getinfo(req, state, ev,
271                         in_info_type, in_file_info_class);
272                 return tevent_req_post(req, ev);
273         }
274
275         switch (in_info_type) {
276         case SMB2_GETINFO_FILE:
277         {
278                 uint16_t file_info_level;
279                 char *data = NULL;
280                 unsigned int data_size = 0;
281                 bool delete_pending = false;
282                 struct timespec write_time_ts;
283                 struct file_id fileid;
284                 struct ea_list *ea_list = NULL;
285                 int lock_data_count = 0;
286                 char *lock_data = NULL;
287                 size_t fixed_portion;
288
289                 ZERO_STRUCT(write_time_ts);
290
291                 switch (in_file_info_class) {
292                 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
293                         file_info_level = 0xFF00 | in_file_info_class;
294                         break;
295
296                 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
297                         file_info_level = 0xFF00 | in_file_info_class;
298                         break;
299
300                 default:
301                         /* the levels directly map to the passthru levels */
302                         file_info_level = in_file_info_class + 1000;
303                         break;
304                 }
305
306                 if (fsp->fake_file_handle) {
307                         /*
308                          * This is actually for the QUOTA_FAKE_FILE --metze
309                          */
310
311                         /* We know this name is ok, it's already passed the checks. */
312
313                 } else if (fsp && fsp->fh->fd == -1) {
314                         /*
315                          * This is actually a QFILEINFO on a directory
316                          * handle (returned from an NT SMB). NT5.0 seems
317                          * to do this call. JRA.
318                          */
319
320                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
321                                 /* Always do lstat for UNIX calls. */
322                                 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
323                                         DEBUG(3,("smbd_smb2_getinfo_send: "
324                                                  "SMB_VFS_LSTAT of %s failed "
325                                                  "(%s)\n", fsp_str_dbg(fsp),
326                                                  strerror(errno)));
327                                         status = map_nt_error_from_unix(errno);
328                                         tevent_req_nterror(req, status);
329                                         return tevent_req_post(req, ev);
330                                 }
331                         } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
332                                 DEBUG(3,("smbd_smb2_getinfo_send: "
333                                          "SMB_VFS_STAT of %s failed (%s)\n",
334                                          fsp_str_dbg(fsp),
335                                          strerror(errno)));
336                                 status = map_nt_error_from_unix(errno);
337                                 tevent_req_nterror(req, status);
338                                 return tevent_req_post(req, ev);
339                         }
340
341                         fileid = vfs_file_id_from_sbuf(conn,
342                                                        &fsp->fsp_name->st);
343                         get_file_infos(fileid, fsp->name_hash,
344                                 &delete_pending, &write_time_ts);
345                 } else {
346                         /*
347                          * Original code - this is an open file.
348                          */
349
350                         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
351                                 DEBUG(3, ("smbd_smb2_getinfo_send: "
352                                           "fstat of fnum %d failed (%s)\n",
353                                           fsp->fnum, strerror(errno)));
354                                 status = map_nt_error_from_unix(errno);
355                                 tevent_req_nterror(req, status);
356                                 return tevent_req_post(req, ev);
357                         }
358                         fileid = vfs_file_id_from_sbuf(conn,
359                                                        &fsp->fsp_name->st);
360                         get_file_infos(fileid, fsp->name_hash,
361                                 &delete_pending, &write_time_ts);
362                 }
363
364                 status = smbd_do_qfilepathinfo(conn, state,
365                                                file_info_level,
366                                                fsp,
367                                                fsp->fsp_name,
368                                                delete_pending,
369                                                write_time_ts,
370                                                ea_list,
371                                                lock_data_count,
372                                                lock_data,
373                                                STR_UNICODE,
374                                                in_output_buffer_length,
375                                                &fixed_portion,
376                                                &data,
377                                                &data_size);
378                 if (!NT_STATUS_IS_OK(status)) {
379                         SAFE_FREE(data);
380                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
381                                 status = NT_STATUS_INVALID_INFO_CLASS;
382                         }
383                         tevent_req_nterror(req, status);
384                         return tevent_req_post(req, ev);
385                 }
386                 if (in_output_buffer_length < fixed_portion) {
387                         SAFE_FREE(data);
388                         tevent_req_nterror(
389                                 req, NT_STATUS_INFO_LENGTH_MISMATCH);
390                         return tevent_req_post(req, ev);
391                 }
392                 if (data_size > 0) {
393                         state->out_output_buffer = data_blob_talloc(state,
394                                                                     data,
395                                                                     data_size);
396                         SAFE_FREE(data);
397                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
398                                 return tevent_req_post(req, ev);
399                         }
400                 }
401                 SAFE_FREE(data);
402                 break;
403         }
404
405         case SMB2_GETINFO_FS:
406         {
407                 uint16_t file_info_level;
408                 char *data = NULL;
409                 int data_size = 0;
410                 size_t fixed_portion;
411
412                 /* the levels directly map to the passthru levels */
413                 file_info_level = in_file_info_class + 1000;
414
415                 status = smbd_do_qfsinfo(conn, state,
416                                          file_info_level,
417                                          STR_UNICODE,
418                                          in_output_buffer_length,
419                                          &fixed_portion,
420                                          fsp->fsp_name,
421                                          &data,
422                                          &data_size);
423                 /* some responses set STATUS_BUFFER_OVERFLOW and return
424                    partial, but valid data */
425                 if (!(NT_STATUS_IS_OK(status) ||
426                       NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW))) {
427                         SAFE_FREE(data);
428                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
429                                 status = NT_STATUS_INVALID_INFO_CLASS;
430                         }
431                         tevent_req_nterror(req, status);
432                         return tevent_req_post(req, ev);
433                 }
434                 if (in_output_buffer_length < fixed_portion) {
435                         SAFE_FREE(data);
436                         tevent_req_nterror(
437                                 req, NT_STATUS_INFO_LENGTH_MISMATCH);
438                         return tevent_req_post(req, ev);
439                 }
440                 if (data_size > 0) {
441                         state->out_output_buffer = data_blob_talloc(state,
442                                                                     data,
443                                                                     data_size);
444                         SAFE_FREE(data);
445                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
446                                 return tevent_req_post(req, ev);
447                         }
448                 }
449                 SAFE_FREE(data);
450                 break;
451         }
452
453         case SMB2_GETINFO_SECURITY:
454         {
455                 uint8_t *p_marshalled_sd = NULL;
456                 size_t sd_size = 0;
457
458                 status = smbd_do_query_security_desc(conn,
459                                 state,
460                                 fsp,
461                                 /* Security info wanted. */
462                                 in_additional_information,
463                                 in_output_buffer_length,
464                                 &p_marshalled_sd,
465                                 &sd_size);
466
467                 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
468                         /* Return needed size. */
469                         state->out_output_buffer = data_blob_talloc(state,
470                                                                     NULL,
471                                                                     4);
472                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
473                                 return tevent_req_post(req, ev);
474                         }
475                         SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
476                         state->status = NT_STATUS_BUFFER_TOO_SMALL;
477                         break;
478                 }
479                 if (!NT_STATUS_IS_OK(status)) {
480                         DEBUG(10,("smbd_smb2_getinfo_send: "
481                                  "smbd_do_query_security_desc of %s failed "
482                                  "(%s)\n", fsp_str_dbg(fsp),
483                                  nt_errstr(status)));
484                         tevent_req_nterror(req, status);
485                         return tevent_req_post(req, ev);
486                 }
487
488                 if (sd_size > 0) {
489                         state->out_output_buffer = data_blob_talloc(state,
490                                                                     p_marshalled_sd,
491                                                                     sd_size);
492                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
493                                 return tevent_req_post(req, ev);
494                         }
495                 }
496                 break;
497         }
498
499         default:
500                 DEBUG(10,("smbd_smb2_getinfo_send: "
501                         "unknown in_info_type of %u "
502                         " for file %s\n",
503                         (unsigned int)in_info_type,
504                         fsp_str_dbg(fsp) ));
505
506                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
507                 return tevent_req_post(req, ev);
508         }
509
510         if (state->out_output_buffer.length > in_output_buffer_length) {
511                 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
512                 return tevent_req_post(req, ev);
513         }
514
515         state->status = status;
516         tevent_req_done(req);
517         return tevent_req_post(req, ev);
518 }
519
520 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
521                                        TALLOC_CTX *mem_ctx,
522                                        DATA_BLOB *out_output_buffer,
523                                        NTSTATUS *pstatus)
524 {
525         NTSTATUS status;
526         struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
527                                                 struct smbd_smb2_getinfo_state);
528
529         if (tevent_req_is_nterror(req, &status)) {
530                 tevent_req_received(req);
531                 return status;
532         }
533
534         *out_output_buffer = state->out_output_buffer;
535         talloc_steal(mem_ctx, out_output_buffer->data);
536         *pstatus = state->status;
537
538         tevent_req_received(req);
539         return NT_STATUS_OK;
540 }