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