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