s3-tevent: only include ../lib/util/tevent wrappers where needed.
[bbaumbach/samba-autobuild/.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                                                  uint8_t in_info_type,
33                                                  uint8_t in_file_info_class,
34                                                  uint32_t in_output_buffer_length,
35                                                  DATA_BLOB in_input_buffer,
36                                                  uint32_t in_additional_information,
37                                                  uint32_t in_flags,
38                                                  uint64_t in_file_id_volatile);
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         const uint8_t *inhdr;
48         const uint8_t *inbody;
49         int i = req->current_idx;
50         size_t expected_body_size = 0x29;
51         size_t body_size;
52         uint8_t in_info_type;
53         uint8_t in_file_info_class;
54         uint32_t in_output_buffer_length;
55         uint16_t in_input_buffer_offset;
56         uint32_t in_input_buffer_length;
57         DATA_BLOB in_input_buffer;
58         uint32_t in_additional_information;
59         uint32_t in_flags;
60         uint64_t in_file_id_persistent;
61         uint64_t in_file_id_volatile;
62         struct tevent_req *subreq;
63
64         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
65         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
66                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
67         }
68
69         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
70
71         body_size = SVAL(inbody, 0x00);
72         if (body_size != expected_body_size) {
73                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
74         }
75
76         in_info_type                    = CVAL(inbody, 0x02);
77         in_file_info_class              = CVAL(inbody, 0x03);
78         in_output_buffer_length         = IVAL(inbody, 0x04);
79         in_input_buffer_offset          = SVAL(inbody, 0x08);
80         /* 0x0A 2 bytes reserved */
81         in_input_buffer_length          = IVAL(inbody, 0x0C);
82         in_additional_information       = IVAL(inbody, 0x10);
83         in_flags                        = IVAL(inbody, 0x14);
84         in_file_id_persistent           = BVAL(inbody, 0x18);
85         in_file_id_volatile             = BVAL(inbody, 0x20);
86
87         if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
88                 /* This is ok */
89         } else if (in_input_buffer_offset !=
90                    (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
91                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
92         }
93
94         if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
95                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
96         }
97
98         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
99         in_input_buffer.length = in_input_buffer_length;
100
101         if (req->compat_chain_fsp) {
102                 /* skip check */
103         } else if (in_file_id_persistent != in_file_id_volatile) {
104                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
105         }
106
107         subreq = smbd_smb2_getinfo_send(req,
108                                         req->sconn->smb2.event_ctx,
109                                         req,
110                                         in_info_type,
111                                         in_file_info_class,
112                                         in_output_buffer_length,
113                                         in_input_buffer,
114                                         in_additional_information,
115                                         in_flags,
116                                         in_file_id_volatile);
117         if (subreq == NULL) {
118                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
119         }
120         tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
121
122         return smbd_smb2_request_pending_queue(req, subreq);
123 }
124
125 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
126 {
127         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
128                                         struct smbd_smb2_request);
129         int i = req->current_idx;
130         uint8_t *outhdr;
131         DATA_BLOB outbody;
132         DATA_BLOB outdyn;
133         uint16_t out_output_buffer_offset;
134         DATA_BLOB out_output_buffer = data_blob_null;
135         NTSTATUS status;
136         NTSTATUS call_status = NT_STATUS_OK;
137         NTSTATUS error; /* transport error */
138
139         status = smbd_smb2_getinfo_recv(subreq,
140                                         req,
141                                         &out_output_buffer,
142                                         &call_status);
143         TALLOC_FREE(subreq);
144         if (!NT_STATUS_IS_OK(status)) {
145                 error = smbd_smb2_request_error(req, status);
146                 if (!NT_STATUS_IS_OK(error)) {
147                         smbd_server_connection_terminate(req->sconn,
148                                                          nt_errstr(error));
149                         return;
150                 }
151                 return;
152         }
153
154         if (!NT_STATUS_IS_OK(call_status)) {
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(req, outbody, &outdyn);
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                                                  uint8_t in_info_type,
239                                                  uint8_t in_file_info_class,
240                                                  uint32_t in_output_buffer_length,
241                                                  DATA_BLOB in_input_buffer,
242                                                  uint32_t in_additional_information,
243                                                  uint32_t in_flags,
244                                                  uint64_t in_file_id_volatile)
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         files_struct *fsp;
251         NTSTATUS status;
252
253         req = tevent_req_create(mem_ctx, &state,
254                                 struct smbd_smb2_getinfo_state);
255         if (req == NULL) {
256                 return NULL;
257         }
258         state->smb2req = smb2req;
259         state->status = NT_STATUS_OK;
260         state->out_output_buffer = data_blob_null;
261
262         DEBUG(10,("smbd_smb2_getinfo_send: file_id[0x%016llX]\n",
263                   (unsigned long long)in_file_id_volatile));
264
265         smbreq = smbd_smb2_fake_smb_request(smb2req);
266         if (tevent_req_nomem(smbreq, req)) {
267                 return tevent_req_post(req, ev);
268         }
269
270         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
271         if (fsp == NULL) {
272                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
273                 return tevent_req_post(req, ev);
274         }
275         if (conn != fsp->conn) {
276                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
277                 return tevent_req_post(req, ev);
278         }
279         if (smb2req->session->vuid != fsp->vuid) {
280                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
281                 return tevent_req_post(req, ev);
282         }
283
284         if (IS_IPC(conn)) {
285                 smb2_ipc_getinfo(req, state, ev,
286                         in_info_type, in_file_info_class);
287                 return tevent_req_post(req, ev);
288         }
289
290         switch (in_info_type) {
291         case 0x01:/* SMB2_GETINFO_FILE */
292         {
293                 uint16_t file_info_level;
294                 char *data = NULL;
295                 unsigned int data_size = 0;
296                 bool delete_pending = false;
297                 struct timespec write_time_ts;
298                 struct file_id fileid;
299                 struct ea_list *ea_list = NULL;
300                 int lock_data_count = 0;
301                 char *lock_data = NULL;
302
303                 ZERO_STRUCT(write_time_ts);
304
305                 switch (in_file_info_class) {
306                 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
307                         file_info_level = 0xFF00 | in_file_info_class;
308                         break;
309
310                 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
311                         file_info_level = 0xFF00 | in_file_info_class;
312                         break;
313
314                 default:
315                         /* the levels directly map to the passthru levels */
316                         file_info_level = in_file_info_class + 1000;
317                         break;
318                 }
319
320                 if (fsp->fake_file_handle) {
321                         /*
322                          * This is actually for the QUOTA_FAKE_FILE --metze
323                          */
324
325                         /* We know this name is ok, it's already passed the checks. */
326
327                 } else if (fsp && fsp->fh->fd == -1) {
328                         /*
329                          * This is actually a QFILEINFO on a directory
330                          * handle (returned from an NT SMB). NT5.0 seems
331                          * to do this call. JRA.
332                          */
333
334                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
335                                 /* Always do lstat for UNIX calls. */
336                                 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
337                                         DEBUG(3,("smbd_smb2_getinfo_send: "
338                                                  "SMB_VFS_LSTAT of %s failed "
339                                                  "(%s)\n", 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                         } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
346                                 DEBUG(3,("smbd_smb2_getinfo_send: "
347                                          "SMB_VFS_STAT of %s failed (%s)\n",
348                                          fsp_str_dbg(fsp),
349                                          strerror(errno)));
350                                 status = map_nt_error_from_unix(errno);
351                                 tevent_req_nterror(req, status);
352                                 return tevent_req_post(req, ev);
353                         }
354
355                         fileid = vfs_file_id_from_sbuf(conn,
356                                                        &fsp->fsp_name->st);
357                         get_file_infos(fileid, fsp->name_hash,
358                                 &delete_pending, &write_time_ts);
359                 } else {
360                         /*
361                          * Original code - this is an open file.
362                          */
363
364                         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
365                                 DEBUG(3, ("smbd_smb2_getinfo_send: "
366                                           "fstat of fnum %d failed (%s)\n",
367                                           fsp->fnum, strerror(errno)));
368                                 status = map_nt_error_from_unix(errno);
369                                 tevent_req_nterror(req, status);
370                                 return tevent_req_post(req, ev);
371                         }
372                         fileid = vfs_file_id_from_sbuf(conn,
373                                                        &fsp->fsp_name->st);
374                         get_file_infos(fileid, fsp->name_hash,
375                                 &delete_pending, &write_time_ts);
376                 }
377
378                 status = smbd_do_qfilepathinfo(conn, state,
379                                                file_info_level,
380                                                fsp,
381                                                fsp->fsp_name,
382                                                delete_pending,
383                                                write_time_ts,
384                                                ea_list,
385                                                lock_data_count,
386                                                lock_data,
387                                                STR_UNICODE,
388                                                in_output_buffer_length,
389                                                &data,
390                                                &data_size);
391                 if (!NT_STATUS_IS_OK(status)) {
392                         SAFE_FREE(data);
393                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
394                                 status = NT_STATUS_INVALID_INFO_CLASS;
395                         }
396                         tevent_req_nterror(req, status);
397                         return tevent_req_post(req, ev);
398                 }
399                 if (data_size > 0) {
400                         state->out_output_buffer = data_blob_talloc(state,
401                                                                     data,
402                                                                     data_size);
403                         SAFE_FREE(data);
404                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
405                                 return tevent_req_post(req, ev);
406                         }
407                 }
408                 SAFE_FREE(data);
409                 break;
410         }
411
412         case 0x02:/* SMB2_GETINFO_FS */
413         {
414                 uint16_t file_info_level;
415                 char *data = NULL;
416                 int data_size = 0;
417
418                 /* the levels directly map to the passthru levels */
419                 file_info_level = in_file_info_class + 1000;
420
421                 status = smbd_do_qfsinfo(conn, state,
422                                          file_info_level,
423                                          STR_UNICODE,
424                                          in_output_buffer_length,
425                                          &data,
426                                          &data_size);
427                 if (!NT_STATUS_IS_OK(status)) {
428                         SAFE_FREE(data);
429                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
430                                 status = NT_STATUS_INVALID_INFO_CLASS;
431                         }
432                         tevent_req_nterror(req, status);
433                         return tevent_req_post(req, ev);
434                 }
435                 if (data_size > 0) {
436                         state->out_output_buffer = data_blob_talloc(state,
437                                                                     data,
438                                                                     data_size);
439                         SAFE_FREE(data);
440                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
441                                 return tevent_req_post(req, ev);
442                         }
443                 }
444                 SAFE_FREE(data);
445                 break;
446         }
447
448         case 0x03:/* SMB2_GETINFO_SEC */
449         {
450                 uint8_t *p_marshalled_sd = NULL;
451                 size_t sd_size = 0;
452
453                 status = smbd_do_query_security_desc(conn,
454                                 state,
455                                 fsp,
456                                 /* Security info wanted. */
457                                 in_additional_information,
458                                 in_output_buffer_length,
459                                 &p_marshalled_sd,
460                                 &sd_size);
461
462                 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
463                         /* Return needed size. */
464                         state->out_output_buffer = data_blob_talloc(state,
465                                                                     NULL,
466                                                                     4);
467                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
468                                 return tevent_req_post(req, ev);
469                         }
470                         SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
471                         state->status = NT_STATUS_BUFFER_TOO_SMALL;
472                         break;
473                 }
474                 if (!NT_STATUS_IS_OK(status)) {
475                         DEBUG(10,("smbd_smb2_getinfo_send: "
476                                  "smbd_do_query_security_desc of %s failed "
477                                  "(%s)\n", fsp_str_dbg(fsp),
478                                  nt_errstr(status)));
479                         tevent_req_nterror(req, status);
480                         return tevent_req_post(req, ev);
481                 }
482
483                 if (sd_size > 0) {
484                         state->out_output_buffer = data_blob_talloc(state,
485                                                                     p_marshalled_sd,
486                                                                     sd_size);
487                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
488                                 return tevent_req_post(req, ev);
489                         }
490                 }
491                 break;
492         }
493
494         default:
495                 DEBUG(10,("smbd_smb2_getinfo_send: "
496                         "unknown in_info_type of %u "
497                         " for file %s\n",
498                         (unsigned int)in_info_type,
499                         fsp_str_dbg(fsp) ));
500
501                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
502                 return tevent_req_post(req, ev);
503         }
504
505         tevent_req_done(req);
506         return tevent_req_post(req, ev);
507 }
508
509 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
510                                        TALLOC_CTX *mem_ctx,
511                                        DATA_BLOB *out_output_buffer,
512                                        NTSTATUS *pstatus)
513 {
514         NTSTATUS status;
515         struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
516                                                 struct smbd_smb2_getinfo_state);
517
518         if (tevent_req_is_nterror(req, &status)) {
519                 tevent_req_received(req);
520                 return status;
521         }
522
523         *out_output_buffer = state->out_output_buffer;
524         talloc_steal(mem_ctx, out_output_buffer->data);
525         *pstatus = state->status;
526
527         tevent_req_received(req);
528         return NT_STATUS_OK;
529 }