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