s3: debug: smb2: Create a new DBGC_SMB2 debug class and mark all smbd/smb2_*.c files...
[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 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_SMB2
31
32 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
33                                                  struct tevent_context *ev,
34                                                  struct smbd_smb2_request *smb2req,
35                                                  struct files_struct *in_fsp,
36                                                  uint8_t in_info_type,
37                                                  uint8_t in_file_info_class,
38                                                  uint32_t in_output_buffer_length,
39                                                  DATA_BLOB in_input_buffer,
40                                                  uint32_t in_additional_information,
41                                                  uint32_t in_flags);
42 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
43                                        TALLOC_CTX *mem_ctx,
44                                        DATA_BLOB *out_output_buffer,
45                                        NTSTATUS *p_call_status);
46
47 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq);
48 NTSTATUS smbd_smb2_request_process_getinfo(struct smbd_smb2_request *req)
49 {
50         struct smbXsrv_connection *xconn = req->xconn;
51         NTSTATUS status;
52         const uint8_t *inbody;
53         uint8_t in_info_type;
54         uint8_t in_file_info_class;
55         uint32_t in_output_buffer_length;
56         uint16_t in_input_buffer_offset;
57         uint32_t in_input_buffer_length;
58         DATA_BLOB in_input_buffer;
59         uint32_t in_additional_information;
60         uint32_t in_flags;
61         uint64_t in_file_id_persistent;
62         uint64_t in_file_id_volatile;
63         struct files_struct *in_fsp;
64         struct tevent_req *subreq;
65
66         status = smbd_smb2_request_verify_sizes(req, 0x29);
67         if (!NT_STATUS_IS_OK(status)) {
68                 return smbd_smb2_request_error(req, status);
69         }
70         inbody = SMBD_SMB2_IN_BODY_PTR(req);
71
72         in_info_type                    = CVAL(inbody, 0x02);
73         in_file_info_class              = CVAL(inbody, 0x03);
74         in_output_buffer_length         = IVAL(inbody, 0x04);
75         in_input_buffer_offset          = SVAL(inbody, 0x08);
76         /* 0x0A 2 bytes reserved */
77         in_input_buffer_length          = IVAL(inbody, 0x0C);
78         in_additional_information       = IVAL(inbody, 0x10);
79         in_flags                        = IVAL(inbody, 0x14);
80         in_file_id_persistent           = BVAL(inbody, 0x18);
81         in_file_id_volatile             = BVAL(inbody, 0x20);
82
83         if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
84                 /* This is ok */
85         } else if (in_input_buffer_offset !=
86                    (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
87                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
88         }
89
90         if (in_input_buffer_length > SMBD_SMB2_IN_DYN_LEN(req)) {
91                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
92         }
93
94         in_input_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
95         in_input_buffer.length = in_input_buffer_length;
96
97         if (in_input_buffer.length > xconn->smb2.server.max_trans) {
98                 DEBUG(2,("smbd_smb2_request_process_getinfo: "
99                          "client ignored max trans: %s: 0x%08X: 0x%08X\n",
100                          __location__, (unsigned)in_input_buffer.length,
101                          (unsigned)xconn->smb2.server.max_trans));
102                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
103         }
104         if (in_output_buffer_length > xconn->smb2.server.max_trans) {
105                 DEBUG(2,("smbd_smb2_request_process_getinfo: "
106                          "client ignored max trans: %s: 0x%08X: 0x%08X\n",
107                          __location__, in_output_buffer_length,
108                          xconn->smb2.server.max_trans));
109                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
110         }
111
112         status = smbd_smb2_request_verify_creditcharge(req,
113                         MAX(in_input_buffer.length,in_output_buffer_length));
114         if (!NT_STATUS_IS_OK(status)) {
115                 return smbd_smb2_request_error(req, status);
116         }
117
118         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
119         if (in_fsp == NULL) {
120                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
121         }
122
123         subreq = smbd_smb2_getinfo_send(req, req->sconn->ev_ctx,
124                                         req, in_fsp,
125                                         in_info_type,
126                                         in_file_info_class,
127                                         in_output_buffer_length,
128                                         in_input_buffer,
129                                         in_additional_information,
130                                         in_flags);
131         if (subreq == NULL) {
132                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
133         }
134         tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
135
136         return smbd_smb2_request_pending_queue(req, subreq, 500);
137 }
138
139 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
140 {
141         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
142                                         struct smbd_smb2_request);
143         DATA_BLOB outbody;
144         DATA_BLOB outdyn;
145         uint16_t out_output_buffer_offset;
146         DATA_BLOB out_output_buffer = data_blob_null;
147         NTSTATUS status;
148         NTSTATUS call_status = NT_STATUS_OK;
149         NTSTATUS error; /* transport error */
150
151         status = smbd_smb2_getinfo_recv(subreq,
152                                         req,
153                                         &out_output_buffer,
154                                         &call_status);
155         TALLOC_FREE(subreq);
156         if (!NT_STATUS_IS_OK(status)) {
157                 error = smbd_smb2_request_error(req, status);
158                 if (!NT_STATUS_IS_OK(error)) {
159                         smbd_server_connection_terminate(req->xconn,
160                                                          nt_errstr(error));
161                         return;
162                 }
163                 return;
164         }
165
166         /* some GetInfo responses set STATUS_BUFFER_OVERFLOW and return partial,
167            but valid data */
168         if (!(NT_STATUS_IS_OK(call_status) ||
169               NT_STATUS_EQUAL(call_status, STATUS_BUFFER_OVERFLOW))) {
170                 /* Return a specific error with data. */
171                 error = smbd_smb2_request_error_ex(req,
172                                                 call_status,
173                                                 &out_output_buffer,
174                                                 __location__);
175                 if (!NT_STATUS_IS_OK(error)) {
176                         smbd_server_connection_terminate(req->xconn,
177                                                          nt_errstr(error));
178                         return;
179                 }
180                 return;
181         }
182
183         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
184
185         outbody = smbd_smb2_generate_outbody(req, 0x08);
186         if (outbody.data == NULL) {
187                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
188                 if (!NT_STATUS_IS_OK(error)) {
189                         smbd_server_connection_terminate(req->xconn,
190                                                          nt_errstr(error));
191                         return;
192                 }
193                 return;
194         }
195
196         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
197         SSVAL(outbody.data, 0x02,
198               out_output_buffer_offset);        /* output buffer offset */
199         SIVAL(outbody.data, 0x04,
200               out_output_buffer.length);        /* output buffer length */
201
202         outdyn = out_output_buffer;
203
204         error = smbd_smb2_request_done_ex(req, call_status, outbody, &outdyn, __location__);
205         if (!NT_STATUS_IS_OK(error)) {
206                 smbd_server_connection_terminate(req->xconn,
207                                                  nt_errstr(error));
208                 return;
209         }
210 }
211
212 struct smbd_smb2_getinfo_state {
213         struct smbd_smb2_request *smb2req;
214         NTSTATUS status;
215         DATA_BLOB out_output_buffer;
216 };
217
218 static void smb2_ipc_getinfo(struct tevent_req *req,
219                                 struct smbd_smb2_getinfo_state *state,
220                                 struct tevent_context *ev,
221                                 uint8_t in_info_type,
222                                 uint8_t in_file_info_class)
223 {
224         /* We want to reply to SMB2_GETINFO_FILE
225            with a class of SMB2_FILE_STANDARD_INFO as
226            otherwise a Win7 client issues this request
227            twice (2xroundtrips) if we return NOT_SUPPORTED.
228            NB. We do the same for SMB1 in call_trans2qpipeinfo() */
229
230         if (in_info_type == 0x01 && /* SMB2_GETINFO_FILE */
231                         in_file_info_class == 0x05) { /* SMB2_FILE_STANDARD_INFO */
232                 state->out_output_buffer = data_blob_talloc(state,
233                                                 NULL, 24);
234                 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
235                         return;
236                 }
237
238                 memset(state->out_output_buffer.data,0,24);
239                 SOFF_T(state->out_output_buffer.data,0,4096LL);
240                 SIVAL(state->out_output_buffer.data,16,1);
241                 SIVAL(state->out_output_buffer.data,20,1);
242                 tevent_req_done(req);
243         } else {
244                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
245         }
246 }
247
248 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
249                                                  struct tevent_context *ev,
250                                                  struct smbd_smb2_request *smb2req,
251                                                  struct files_struct *fsp,
252                                                  uint8_t in_info_type,
253                                                  uint8_t in_file_info_class,
254                                                  uint32_t in_output_buffer_length,
255                                                  DATA_BLOB in_input_buffer,
256                                                  uint32_t in_additional_information,
257                                                  uint32_t in_flags)
258 {
259         struct tevent_req *req;
260         struct smbd_smb2_getinfo_state *state;
261         struct smb_request *smbreq;
262         connection_struct *conn = smb2req->tcon->compat;
263         NTSTATUS status;
264
265         req = tevent_req_create(mem_ctx, &state,
266                                 struct smbd_smb2_getinfo_state);
267         if (req == NULL) {
268                 return NULL;
269         }
270         state->smb2req = smb2req;
271         state->status = NT_STATUS_OK;
272         state->out_output_buffer = data_blob_null;
273
274         DEBUG(10,("smbd_smb2_getinfo_send: %s - %s\n",
275                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
276
277         smbreq = smbd_smb2_fake_smb_request(smb2req);
278         if (tevent_req_nomem(smbreq, req)) {
279                 return tevent_req_post(req, ev);
280         }
281
282         if (IS_IPC(conn)) {
283                 smb2_ipc_getinfo(req, state, ev,
284                         in_info_type, in_file_info_class);
285                 return tevent_req_post(req, ev);
286         }
287
288         switch (in_info_type) {
289         case SMB2_GETINFO_FILE:
290         {
291                 uint16_t file_info_level;
292                 char *data = NULL;
293                 unsigned int data_size = 0;
294                 bool delete_pending = false;
295                 struct timespec write_time_ts;
296                 struct file_id fileid;
297                 struct ea_list *ea_list = NULL;
298                 int lock_data_count = 0;
299                 char *lock_data = NULL;
300                 size_t fixed_portion;
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->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 %s failed (%s)\n",
366                                           fsp_fnum_dbg(fsp), 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                                                &fixed_portion,
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 (in_output_buffer_length < fixed_portion) {
400                         SAFE_FREE(data);
401                         tevent_req_nterror(
402                                 req, NT_STATUS_INFO_LENGTH_MISMATCH);
403                         return tevent_req_post(req, ev);
404                 }
405                 if (data_size > 0) {
406                         state->out_output_buffer = data_blob_talloc(state,
407                                                                     data,
408                                                                     data_size);
409                         SAFE_FREE(data);
410                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
411                                 return tevent_req_post(req, ev);
412                         }
413                         if (data_size > in_output_buffer_length) {
414                                 state->out_output_buffer.length =
415                                         in_output_buffer_length;
416                                 status = STATUS_BUFFER_OVERFLOW;
417                         }
418                 }
419                 SAFE_FREE(data);
420                 break;
421         }
422
423         case SMB2_GETINFO_FS:
424         {
425                 uint16_t file_info_level;
426                 char *data = NULL;
427                 int data_size = 0;
428                 size_t fixed_portion;
429
430                 /* the levels directly map to the passthru levels */
431                 file_info_level = in_file_info_class + 1000;
432
433                 status = smbd_do_qfsinfo(smb2req->xconn, conn, state,
434                                          file_info_level,
435                                          STR_UNICODE,
436                                          in_output_buffer_length,
437                                          &fixed_portion,
438                                          fsp->fsp_name,
439                                          &data,
440                                          &data_size);
441                 /* some responses set STATUS_BUFFER_OVERFLOW and return
442                    partial, but valid data */
443                 if (!(NT_STATUS_IS_OK(status) ||
444                       NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW))) {
445                         SAFE_FREE(data);
446                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
447                                 status = NT_STATUS_INVALID_INFO_CLASS;
448                         }
449                         tevent_req_nterror(req, status);
450                         return tevent_req_post(req, ev);
451                 }
452                 if (in_output_buffer_length < fixed_portion) {
453                         SAFE_FREE(data);
454                         tevent_req_nterror(
455                                 req, NT_STATUS_INFO_LENGTH_MISMATCH);
456                         return tevent_req_post(req, ev);
457                 }
458                 if (data_size > 0) {
459                         state->out_output_buffer = data_blob_talloc(state,
460                                                                     data,
461                                                                     data_size);
462                         SAFE_FREE(data);
463                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
464                                 return tevent_req_post(req, ev);
465                         }
466                         if (data_size > in_output_buffer_length) {
467                                 state->out_output_buffer.length =
468                                         in_output_buffer_length;
469                                 status = STATUS_BUFFER_OVERFLOW;
470                         }
471                 }
472                 SAFE_FREE(data);
473                 break;
474         }
475
476         case SMB2_GETINFO_SECURITY:
477         {
478                 uint8_t *p_marshalled_sd = NULL;
479                 size_t sd_size = 0;
480
481                 status = smbd_do_query_security_desc(conn,
482                                 state,
483                                 fsp,
484                                 /* Security info wanted. */
485                                 in_additional_information &
486                                 SMB_SUPPORTED_SECINFO_FLAGS,
487                                 in_output_buffer_length,
488                                 &p_marshalled_sd,
489                                 &sd_size);
490
491                 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
492                         /* Return needed size. */
493                         state->out_output_buffer = data_blob_talloc(state,
494                                                                     NULL,
495                                                                     4);
496                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
497                                 return tevent_req_post(req, ev);
498                         }
499                         SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
500                         state->status = NT_STATUS_BUFFER_TOO_SMALL;
501                         break;
502                 }
503                 if (!NT_STATUS_IS_OK(status)) {
504                         DEBUG(10,("smbd_smb2_getinfo_send: "
505                                  "smbd_do_query_security_desc of %s failed "
506                                  "(%s)\n", fsp_str_dbg(fsp),
507                                  nt_errstr(status)));
508                         tevent_req_nterror(req, status);
509                         return tevent_req_post(req, ev);
510                 }
511
512                 if (sd_size > 0) {
513                         state->out_output_buffer = data_blob_talloc(state,
514                                                                     p_marshalled_sd,
515                                                                     sd_size);
516                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
517                                 return tevent_req_post(req, ev);
518                         }
519                 }
520                 break;
521         }
522
523         case SMB2_GETINFO_QUOTA:
524                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
525                 return tevent_req_post(req, ev);
526
527         default:
528                 DEBUG(10,("smbd_smb2_getinfo_send: "
529                         "unknown in_info_type of %u "
530                         " for file %s\n",
531                         (unsigned int)in_info_type,
532                         fsp_str_dbg(fsp) ));
533
534                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
535                 return tevent_req_post(req, ev);
536         }
537
538         state->status = status;
539         tevent_req_done(req);
540         return tevent_req_post(req, ev);
541 }
542
543 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
544                                        TALLOC_CTX *mem_ctx,
545                                        DATA_BLOB *out_output_buffer,
546                                        NTSTATUS *pstatus)
547 {
548         NTSTATUS status;
549         struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
550                                                 struct smbd_smb2_getinfo_state);
551
552         if (tevent_req_is_nterror(req, &status)) {
553                 tevent_req_received(req);
554                 return status;
555         }
556
557         *out_output_buffer = state->out_output_buffer;
558         talloc_steal(mem_ctx, out_output_buffer->data);
559         *pstatus = state->status;
560
561         tevent_req_received(req);
562         return NT_STATUS_OK;
563 }