s3: libsmb: Do some hardening in the receive processing of cli_shadow_copy_data_recv().
[Samba.git] / source3 / smbd / smb2_getinfo.c
blob7f44868bade163e302910134d3f326a0df8ae949
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
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.
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.
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/>.
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"
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);
44 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq);
45 NTSTATUS smbd_smb2_request_process_getinfo(struct smbd_smb2_request *req)
47 struct smbXsrv_connection *xconn = req->xconn;
48 NTSTATUS status;
49 const uint8_t *inbody;
50 uint8_t in_info_type;
51 uint8_t in_file_info_class;
52 uint32_t in_output_buffer_length;
53 uint16_t in_input_buffer_offset;
54 uint32_t in_input_buffer_length;
55 DATA_BLOB in_input_buffer;
56 uint32_t in_additional_information;
57 uint32_t in_flags;
58 uint64_t in_file_id_persistent;
59 uint64_t in_file_id_volatile;
60 struct files_struct *in_fsp;
61 struct tevent_req *subreq;
63 status = smbd_smb2_request_verify_sizes(req, 0x29);
64 if (!NT_STATUS_IS_OK(status)) {
65 return smbd_smb2_request_error(req, status);
67 inbody = SMBD_SMB2_IN_BODY_PTR(req);
69 in_info_type = CVAL(inbody, 0x02);
70 in_file_info_class = CVAL(inbody, 0x03);
71 in_output_buffer_length = IVAL(inbody, 0x04);
72 in_input_buffer_offset = SVAL(inbody, 0x08);
73 /* 0x0A 2 bytes reserved */
74 in_input_buffer_length = IVAL(inbody, 0x0C);
75 in_additional_information = IVAL(inbody, 0x10);
76 in_flags = IVAL(inbody, 0x14);
77 in_file_id_persistent = BVAL(inbody, 0x18);
78 in_file_id_volatile = BVAL(inbody, 0x20);
80 if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
81 /* This is ok */
82 } else if (in_input_buffer_offset !=
83 (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
84 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
87 if (in_input_buffer_length > SMBD_SMB2_IN_DYN_LEN(req)) {
88 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
91 in_input_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
92 in_input_buffer.length = in_input_buffer_length;
94 if (in_input_buffer.length > xconn->smb2.server.max_trans) {
95 DEBUG(2,("smbd_smb2_request_process_getinfo: "
96 "client ignored max trans: %s: 0x%08X: 0x%08X\n",
97 __location__, (unsigned)in_input_buffer.length,
98 (unsigned)xconn->smb2.server.max_trans));
99 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
101 if (in_output_buffer_length > xconn->smb2.server.max_trans) {
102 DEBUG(2,("smbd_smb2_request_process_getinfo: "
103 "client ignored max trans: %s: 0x%08X: 0x%08X\n",
104 __location__, in_output_buffer_length,
105 xconn->smb2.server.max_trans));
106 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
109 status = smbd_smb2_request_verify_creditcharge(req,
110 MAX(in_input_buffer.length,in_output_buffer_length));
111 if (!NT_STATUS_IS_OK(status)) {
112 return smbd_smb2_request_error(req, status);
115 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
116 if (in_fsp == NULL) {
117 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
120 subreq = smbd_smb2_getinfo_send(req, req->sconn->ev_ctx,
121 req, in_fsp,
122 in_info_type,
123 in_file_info_class,
124 in_output_buffer_length,
125 in_input_buffer,
126 in_additional_information,
127 in_flags);
128 if (subreq == NULL) {
129 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
131 tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
133 return smbd_smb2_request_pending_queue(req, subreq, 500);
136 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
138 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
139 struct smbd_smb2_request);
140 DATA_BLOB outbody;
141 DATA_BLOB outdyn;
142 uint16_t out_output_buffer_offset;
143 DATA_BLOB out_output_buffer = data_blob_null;
144 NTSTATUS status;
145 NTSTATUS call_status = NT_STATUS_OK;
146 NTSTATUS error; /* transport error */
148 status = smbd_smb2_getinfo_recv(subreq,
149 req,
150 &out_output_buffer,
151 &call_status);
152 TALLOC_FREE(subreq);
153 if (!NT_STATUS_IS_OK(status)) {
154 error = smbd_smb2_request_error(req, status);
155 if (!NT_STATUS_IS_OK(error)) {
156 smbd_server_connection_terminate(req->xconn,
157 nt_errstr(error));
158 return;
160 return;
163 /* some GetInfo responses set STATUS_BUFFER_OVERFLOW and return partial,
164 but valid data */
165 if (!(NT_STATUS_IS_OK(call_status) ||
166 NT_STATUS_EQUAL(call_status, STATUS_BUFFER_OVERFLOW))) {
167 /* Return a specific error with data. */
168 error = smbd_smb2_request_error_ex(req,
169 call_status,
170 &out_output_buffer,
171 __location__);
172 if (!NT_STATUS_IS_OK(error)) {
173 smbd_server_connection_terminate(req->xconn,
174 nt_errstr(error));
175 return;
177 return;
180 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
182 outbody = smbd_smb2_generate_outbody(req, 0x08);
183 if (outbody.data == NULL) {
184 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
185 if (!NT_STATUS_IS_OK(error)) {
186 smbd_server_connection_terminate(req->xconn,
187 nt_errstr(error));
188 return;
190 return;
193 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
194 SSVAL(outbody.data, 0x02,
195 out_output_buffer_offset); /* output buffer offset */
196 SIVAL(outbody.data, 0x04,
197 out_output_buffer.length); /* output buffer length */
199 outdyn = out_output_buffer;
201 error = smbd_smb2_request_done_ex(req, call_status, outbody, &outdyn, __location__);
202 if (!NT_STATUS_IS_OK(error)) {
203 smbd_server_connection_terminate(req->xconn,
204 nt_errstr(error));
205 return;
209 struct smbd_smb2_getinfo_state {
210 struct smbd_smb2_request *smb2req;
211 NTSTATUS status;
212 DATA_BLOB out_output_buffer;
215 static void smb2_ipc_getinfo(struct tevent_req *req,
216 struct smbd_smb2_getinfo_state *state,
217 struct tevent_context *ev,
218 uint8_t in_info_type,
219 uint8_t in_file_info_class)
221 /* We want to reply to SMB2_GETINFO_FILE
222 with a class of SMB2_FILE_STANDARD_INFO as
223 otherwise a Win7 client issues this request
224 twice (2xroundtrips) if we return NOT_SUPPORTED.
225 NB. We do the same for SMB1 in call_trans2qpipeinfo() */
227 if (in_info_type == 0x01 && /* SMB2_GETINFO_FILE */
228 in_file_info_class == 0x05) { /* SMB2_FILE_STANDARD_INFO */
229 state->out_output_buffer = data_blob_talloc(state,
230 NULL, 24);
231 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
232 return;
235 memset(state->out_output_buffer.data,0,24);
236 SOFF_T(state->out_output_buffer.data,0,4096LL);
237 SIVAL(state->out_output_buffer.data,16,1);
238 SIVAL(state->out_output_buffer.data,20,1);
239 tevent_req_done(req);
240 } else {
241 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
245 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
246 struct tevent_context *ev,
247 struct smbd_smb2_request *smb2req,
248 struct files_struct *fsp,
249 uint8_t in_info_type,
250 uint8_t in_file_info_class,
251 uint32_t in_output_buffer_length,
252 DATA_BLOB in_input_buffer,
253 uint32_t in_additional_information,
254 uint32_t in_flags)
256 struct tevent_req *req;
257 struct smbd_smb2_getinfo_state *state;
258 struct smb_request *smbreq;
259 connection_struct *conn = smb2req->tcon->compat;
260 NTSTATUS status;
262 req = tevent_req_create(mem_ctx, &state,
263 struct smbd_smb2_getinfo_state);
264 if (req == NULL) {
265 return NULL;
267 state->smb2req = smb2req;
268 state->status = NT_STATUS_OK;
269 state->out_output_buffer = data_blob_null;
271 DEBUG(10,("smbd_smb2_getinfo_send: %s - %s\n",
272 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
274 smbreq = smbd_smb2_fake_smb_request(smb2req);
275 if (tevent_req_nomem(smbreq, req)) {
276 return tevent_req_post(req, ev);
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);
285 switch (in_info_type) {
286 case SMB2_GETINFO_FILE:
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 size_t fixed_portion;
299 ZERO_STRUCT(write_time_ts);
301 switch (in_file_info_class) {
302 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
303 file_info_level = 0xFF00 | in_file_info_class;
304 break;
306 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
307 file_info_level = 0xFF00 | in_file_info_class;
308 break;
310 default:
311 /* the levels directly map to the passthru levels */
312 file_info_level = in_file_info_class + 1000;
313 break;
316 if (fsp->fake_file_handle) {
318 * This is actually for the QUOTA_FAKE_FILE --metze
321 /* We know this name is ok, it's already passed the checks. */
323 } else if (fsp->fh->fd == -1) {
325 * This is actually a QFILEINFO on a directory
326 * handle (returned from an NT SMB). NT5.0 seems
327 * to do this call. JRA.
330 if (INFO_LEVEL_IS_UNIX(file_info_level)) {
331 /* Always do lstat for UNIX calls. */
332 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
333 DEBUG(3,("smbd_smb2_getinfo_send: "
334 "SMB_VFS_LSTAT of %s failed "
335 "(%s)\n", fsp_str_dbg(fsp),
336 strerror(errno)));
337 status = map_nt_error_from_unix(errno);
338 tevent_req_nterror(req, status);
339 return tevent_req_post(req, ev);
341 } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
342 DEBUG(3,("smbd_smb2_getinfo_send: "
343 "SMB_VFS_STAT of %s failed (%s)\n",
344 fsp_str_dbg(fsp),
345 strerror(errno)));
346 status = map_nt_error_from_unix(errno);
347 tevent_req_nterror(req, status);
348 return tevent_req_post(req, ev);
351 fileid = vfs_file_id_from_sbuf(conn,
352 &fsp->fsp_name->st);
353 get_file_infos(fileid, fsp->name_hash,
354 &delete_pending, &write_time_ts);
355 } else {
357 * Original code - this is an open file.
360 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
361 DEBUG(3, ("smbd_smb2_getinfo_send: "
362 "fstat of %s failed (%s)\n",
363 fsp_fnum_dbg(fsp), strerror(errno)));
364 status = map_nt_error_from_unix(errno);
365 tevent_req_nterror(req, status);
366 return tevent_req_post(req, ev);
368 fileid = vfs_file_id_from_sbuf(conn,
369 &fsp->fsp_name->st);
370 get_file_infos(fileid, fsp->name_hash,
371 &delete_pending, &write_time_ts);
374 status = smbd_do_qfilepathinfo(conn, state,
375 file_info_level,
376 fsp,
377 fsp->fsp_name,
378 delete_pending,
379 write_time_ts,
380 ea_list,
381 lock_data_count,
382 lock_data,
383 STR_UNICODE,
384 in_output_buffer_length,
385 &fixed_portion,
386 &data,
387 &data_size);
388 if (!NT_STATUS_IS_OK(status)) {
389 SAFE_FREE(data);
390 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
391 status = NT_STATUS_INVALID_INFO_CLASS;
393 tevent_req_nterror(req, status);
394 return tevent_req_post(req, ev);
396 if (in_output_buffer_length < fixed_portion) {
397 SAFE_FREE(data);
398 tevent_req_nterror(
399 req, NT_STATUS_INFO_LENGTH_MISMATCH);
400 return tevent_req_post(req, ev);
402 if (data_size > 0) {
403 state->out_output_buffer = data_blob_talloc(state,
404 data,
405 data_size);
406 SAFE_FREE(data);
407 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
408 return tevent_req_post(req, ev);
410 if (data_size > in_output_buffer_length) {
411 state->out_output_buffer.length =
412 in_output_buffer_length;
413 status = STATUS_BUFFER_OVERFLOW;
416 SAFE_FREE(data);
417 break;
420 case SMB2_GETINFO_FS:
422 uint16_t file_info_level;
423 char *data = NULL;
424 int data_size = 0;
425 size_t fixed_portion;
427 /* the levels directly map to the passthru levels */
428 file_info_level = in_file_info_class + 1000;
430 status = smbd_do_qfsinfo(smb2req->xconn, conn, state,
431 file_info_level,
432 STR_UNICODE,
433 in_output_buffer_length,
434 &fixed_portion,
435 fsp->fsp_name,
436 &data,
437 &data_size);
438 /* some responses set STATUS_BUFFER_OVERFLOW and return
439 partial, but valid data */
440 if (!(NT_STATUS_IS_OK(status) ||
441 NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW))) {
442 SAFE_FREE(data);
443 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
444 status = NT_STATUS_INVALID_INFO_CLASS;
446 tevent_req_nterror(req, status);
447 return tevent_req_post(req, ev);
449 if (in_output_buffer_length < fixed_portion) {
450 SAFE_FREE(data);
451 tevent_req_nterror(
452 req, NT_STATUS_INFO_LENGTH_MISMATCH);
453 return tevent_req_post(req, ev);
455 if (data_size > 0) {
456 state->out_output_buffer = data_blob_talloc(state,
457 data,
458 data_size);
459 SAFE_FREE(data);
460 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
461 return tevent_req_post(req, ev);
463 if (data_size > in_output_buffer_length) {
464 state->out_output_buffer.length =
465 in_output_buffer_length;
466 status = STATUS_BUFFER_OVERFLOW;
469 SAFE_FREE(data);
470 break;
473 case SMB2_GETINFO_SECURITY:
475 uint8_t *p_marshalled_sd = NULL;
476 size_t sd_size = 0;
478 status = smbd_do_query_security_desc(conn,
479 state,
480 fsp,
481 /* Security info wanted. */
482 in_additional_information &
483 SMB_SUPPORTED_SECINFO_FLAGS,
484 in_output_buffer_length,
485 &p_marshalled_sd,
486 &sd_size);
488 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
489 /* Return needed size. */
490 state->out_output_buffer = data_blob_talloc(state,
491 NULL,
493 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
494 return tevent_req_post(req, ev);
496 SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
497 state->status = NT_STATUS_BUFFER_TOO_SMALL;
498 break;
500 if (!NT_STATUS_IS_OK(status)) {
501 DEBUG(10,("smbd_smb2_getinfo_send: "
502 "smbd_do_query_security_desc of %s failed "
503 "(%s)\n", fsp_str_dbg(fsp),
504 nt_errstr(status)));
505 tevent_req_nterror(req, status);
506 return tevent_req_post(req, ev);
509 if (sd_size > 0) {
510 state->out_output_buffer = data_blob_talloc(state,
511 p_marshalled_sd,
512 sd_size);
513 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
514 return tevent_req_post(req, ev);
517 break;
520 case SMB2_GETINFO_QUOTA:
521 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
522 return tevent_req_post(req, ev);
524 default:
525 DEBUG(10,("smbd_smb2_getinfo_send: "
526 "unknown in_info_type of %u "
527 " for file %s\n",
528 (unsigned int)in_info_type,
529 fsp_str_dbg(fsp) ));
531 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
532 return tevent_req_post(req, ev);
535 state->status = status;
536 tevent_req_done(req);
537 return tevent_req_post(req, ev);
540 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
541 TALLOC_CTX *mem_ctx,
542 DATA_BLOB *out_output_buffer,
543 NTSTATUS *pstatus)
545 NTSTATUS status;
546 struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
547 struct smbd_smb2_getinfo_state);
549 if (tevent_req_is_nterror(req, &status)) {
550 tevent_req_received(req);
551 return status;
554 *out_output_buffer = state->out_output_buffer;
555 talloc_steal(mem_ctx, out_output_buffer->data);
556 *pstatus = state->status;
558 tevent_req_received(req);
559 return NT_STATUS_OK;