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