s3:smbd/smb2_getinfo add a debug message
[ambi/samba-autobuild/.git] / source3 / smbd / smb2_getinfo.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) Jeremy Allison 2010
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "trans2.h"
27 #include "../lib/util/tevent_ntstatus.h"
28
29 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
30                                                  struct tevent_context *ev,
31                                                  struct smbd_smb2_request *smb2req,
32                                                  uint8_t in_info_type,
33                                                  uint8_t in_file_info_class,
34                                                  uint32_t in_output_buffer_length,
35                                                  DATA_BLOB in_input_buffer,
36                                                  uint32_t in_additional_information,
37                                                  uint32_t in_flags,
38                                                  uint64_t in_file_id_volatile);
39 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
40                                        TALLOC_CTX *mem_ctx,
41                                        DATA_BLOB *out_output_buffer,
42                                        NTSTATUS *p_call_status);
43
44 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq);
45 NTSTATUS smbd_smb2_request_process_getinfo(struct smbd_smb2_request *req)
46 {
47         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                 DEBUG(2,("smbd_smb2_request_process_getinfo: "
95                          "client ignored max trans: %s: 0x%08X: 0x%08X\n",
96                          __location__, in_input_buffer.length,
97                          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         if (req->compat_chain_fsp) {
115                 /* skip check */
116         } else if (in_file_id_persistent != in_file_id_volatile) {
117                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
118         }
119
120         subreq = smbd_smb2_getinfo_send(req,
121                                         req->sconn->ev_ctx,
122                                         req,
123                                         in_info_type,
124                                         in_file_info_class,
125                                         in_output_buffer_length,
126                                         in_input_buffer,
127                                         in_additional_information,
128                                         in_flags,
129                                         in_file_id_volatile);
130         if (subreq == NULL) {
131                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
132         }
133         tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
134
135         return smbd_smb2_request_pending_queue(req, subreq, 500);
136 }
137
138 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
139 {
140         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
141                                         struct smbd_smb2_request);
142         DATA_BLOB outbody;
143         DATA_BLOB outdyn;
144         uint16_t out_output_buffer_offset;
145         DATA_BLOB out_output_buffer = data_blob_null;
146         NTSTATUS status;
147         NTSTATUS call_status = NT_STATUS_OK;
148         NTSTATUS error; /* transport error */
149
150         status = smbd_smb2_getinfo_recv(subreq,
151                                         req,
152                                         &out_output_buffer,
153                                         &call_status);
154         TALLOC_FREE(subreq);
155         if (!NT_STATUS_IS_OK(status)) {
156                 error = smbd_smb2_request_error(req, status);
157                 if (!NT_STATUS_IS_OK(error)) {
158                         smbd_server_connection_terminate(req->sconn,
159                                                          nt_errstr(error));
160                         return;
161                 }
162                 return;
163         }
164
165         if (!NT_STATUS_IS_OK(call_status)) {
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(req, outbody, &outdyn);
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                                                  uint8_t in_info_type,
248                                                  uint8_t in_file_info_class,
249                                                  uint32_t in_output_buffer_length,
250                                                  DATA_BLOB in_input_buffer,
251                                                  uint32_t in_additional_information,
252                                                  uint32_t in_flags,
253                                                  uint64_t in_file_id_volatile)
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_conn;
259         files_struct *fsp;
260         NTSTATUS status;
261
262         req = tevent_req_create(mem_ctx, &state,
263                                 struct smbd_smb2_getinfo_state);
264         if (req == NULL) {
265                 return NULL;
266         }
267         state->smb2req = smb2req;
268         state->status = NT_STATUS_OK;
269         state->out_output_buffer = data_blob_null;
270
271         DEBUG(10,("smbd_smb2_getinfo_send: file_id[0x%016llX]\n",
272                   (unsigned long long)in_file_id_volatile));
273
274         smbreq = smbd_smb2_fake_smb_request(smb2req);
275         if (tevent_req_nomem(smbreq, req)) {
276                 return tevent_req_post(req, ev);
277         }
278
279         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
280         if (fsp == NULL) {
281                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
282                 return tevent_req_post(req, ev);
283         }
284         if (conn != fsp->conn) {
285                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
286                 return tevent_req_post(req, ev);
287         }
288         if (smb2req->session->vuid != fsp->vuid) {
289                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
290                 return tevent_req_post(req, ev);
291         }
292
293         if (IS_IPC(conn)) {
294                 smb2_ipc_getinfo(req, state, ev,
295                         in_info_type, in_file_info_class);
296                 return tevent_req_post(req, ev);
297         }
298
299         switch (in_info_type) {
300         case 0x01:/* SMB2_GETINFO_FILE */
301         {
302                 uint16_t file_info_level;
303                 char *data = NULL;
304                 unsigned int data_size = 0;
305                 bool delete_pending = false;
306                 struct timespec write_time_ts;
307                 struct file_id fileid;
308                 struct ea_list *ea_list = NULL;
309                 int lock_data_count = 0;
310                 char *lock_data = NULL;
311
312                 ZERO_STRUCT(write_time_ts);
313
314                 switch (in_file_info_class) {
315                 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
316                         file_info_level = 0xFF00 | in_file_info_class;
317                         break;
318
319                 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
320                         file_info_level = 0xFF00 | in_file_info_class;
321                         break;
322
323                 default:
324                         /* the levels directly map to the passthru levels */
325                         file_info_level = in_file_info_class + 1000;
326                         break;
327                 }
328
329                 if (fsp->fake_file_handle) {
330                         /*
331                          * This is actually for the QUOTA_FAKE_FILE --metze
332                          */
333
334                         /* We know this name is ok, it's already passed the checks. */
335
336                 } else if (fsp && fsp->fh->fd == -1) {
337                         /*
338                          * This is actually a QFILEINFO on a directory
339                          * handle (returned from an NT SMB). NT5.0 seems
340                          * to do this call. JRA.
341                          */
342
343                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
344                                 /* Always do lstat for UNIX calls. */
345                                 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
346                                         DEBUG(3,("smbd_smb2_getinfo_send: "
347                                                  "SMB_VFS_LSTAT of %s failed "
348                                                  "(%s)\n", fsp_str_dbg(fsp),
349                                                  strerror(errno)));
350                                         status = map_nt_error_from_unix(errno);
351                                         tevent_req_nterror(req, status);
352                                         return tevent_req_post(req, ev);
353                                 }
354                         } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
355                                 DEBUG(3,("smbd_smb2_getinfo_send: "
356                                          "SMB_VFS_STAT of %s failed (%s)\n",
357                                          fsp_str_dbg(fsp),
358                                          strerror(errno)));
359                                 status = map_nt_error_from_unix(errno);
360                                 tevent_req_nterror(req, status);
361                                 return tevent_req_post(req, ev);
362                         }
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                 } else {
369                         /*
370                          * Original code - this is an open file.
371                          */
372
373                         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
374                                 DEBUG(3, ("smbd_smb2_getinfo_send: "
375                                           "fstat of fnum %d failed (%s)\n",
376                                           fsp->fnum, strerror(errno)));
377                                 status = map_nt_error_from_unix(errno);
378                                 tevent_req_nterror(req, status);
379                                 return tevent_req_post(req, ev);
380                         }
381                         fileid = vfs_file_id_from_sbuf(conn,
382                                                        &fsp->fsp_name->st);
383                         get_file_infos(fileid, fsp->name_hash,
384                                 &delete_pending, &write_time_ts);
385                 }
386
387                 status = smbd_do_qfilepathinfo(conn, state,
388                                                file_info_level,
389                                                fsp,
390                                                fsp->fsp_name,
391                                                delete_pending,
392                                                write_time_ts,
393                                                ea_list,
394                                                lock_data_count,
395                                                lock_data,
396                                                STR_UNICODE,
397                                                in_output_buffer_length,
398                                                &data,
399                                                &data_size);
400                 if (!NT_STATUS_IS_OK(status)) {
401                         SAFE_FREE(data);
402                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
403                                 status = NT_STATUS_INVALID_INFO_CLASS;
404                         }
405                         tevent_req_nterror(req, status);
406                         return tevent_req_post(req, ev);
407                 }
408                 if (data_size > 0) {
409                         state->out_output_buffer = data_blob_talloc(state,
410                                                                     data,
411                                                                     data_size);
412                         SAFE_FREE(data);
413                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
414                                 return tevent_req_post(req, ev);
415                         }
416                 }
417                 SAFE_FREE(data);
418                 break;
419         }
420
421         case 0x02:/* SMB2_GETINFO_FS */
422         {
423                 uint16_t file_info_level;
424                 char *data = NULL;
425                 int data_size = 0;
426
427                 /* the levels directly map to the passthru levels */
428                 file_info_level = in_file_info_class + 1000;
429
430                 status = smbd_do_qfsinfo(conn, state,
431                                          file_info_level,
432                                          STR_UNICODE,
433                                          in_output_buffer_length,
434                                          &data,
435                                          &data_size);
436                 if (!NT_STATUS_IS_OK(status)) {
437                         SAFE_FREE(data);
438                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
439                                 status = NT_STATUS_INVALID_INFO_CLASS;
440                         }
441                         tevent_req_nterror(req, status);
442                         return tevent_req_post(req, ev);
443                 }
444                 if (data_size > 0) {
445                         state->out_output_buffer = data_blob_talloc(state,
446                                                                     data,
447                                                                     data_size);
448                         SAFE_FREE(data);
449                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
450                                 return tevent_req_post(req, ev);
451                         }
452                 }
453                 SAFE_FREE(data);
454                 break;
455         }
456
457         case 0x03:/* SMB2_GETINFO_SEC */
458         {
459                 uint8_t *p_marshalled_sd = NULL;
460                 size_t sd_size = 0;
461
462                 status = smbd_do_query_security_desc(conn,
463                                 state,
464                                 fsp,
465                                 /* Security info wanted. */
466                                 in_additional_information,
467                                 in_output_buffer_length,
468                                 &p_marshalled_sd,
469                                 &sd_size);
470
471                 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
472                         /* Return needed size. */
473                         state->out_output_buffer = data_blob_talloc(state,
474                                                                     NULL,
475                                                                     4);
476                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
477                                 return tevent_req_post(req, ev);
478                         }
479                         SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
480                         state->status = NT_STATUS_BUFFER_TOO_SMALL;
481                         break;
482                 }
483                 if (!NT_STATUS_IS_OK(status)) {
484                         DEBUG(10,("smbd_smb2_getinfo_send: "
485                                  "smbd_do_query_security_desc of %s failed "
486                                  "(%s)\n", fsp_str_dbg(fsp),
487                                  nt_errstr(status)));
488                         tevent_req_nterror(req, status);
489                         return tevent_req_post(req, ev);
490                 }
491
492                 if (sd_size > 0) {
493                         state->out_output_buffer = data_blob_talloc(state,
494                                                                     p_marshalled_sd,
495                                                                     sd_size);
496                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
497                                 return tevent_req_post(req, ev);
498                         }
499                 }
500                 break;
501         }
502
503         default:
504                 DEBUG(10,("smbd_smb2_getinfo_send: "
505                         "unknown in_info_type of %u "
506                         " for file %s\n",
507                         (unsigned int)in_info_type,
508                         fsp_str_dbg(fsp) ));
509
510                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
511                 return tevent_req_post(req, ev);
512         }
513
514         tevent_req_done(req);
515         return tevent_req_post(req, ev);
516 }
517
518 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
519                                        TALLOC_CTX *mem_ctx,
520                                        DATA_BLOB *out_output_buffer,
521                                        NTSTATUS *pstatus)
522 {
523         NTSTATUS status;
524         struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
525                                                 struct smbd_smb2_getinfo_state);
526
527         if (tevent_req_is_nterror(req, &status)) {
528                 tevent_req_received(req);
529                 return status;
530         }
531
532         *out_output_buffer = state->out_output_buffer;
533         talloc_steal(mem_ctx, out_output_buffer->data);
534         *pstatus = state->status;
535
536         tevent_req_received(req);
537         return NT_STATUS_OK;
538 }