s3:smb2_server: use smbd_smb2_request_verify_sizes() in smb2_getinfo.c
[Samba.git] / source3 / smbd / smb2_getinfo.c
blob61e0cfa06c387bb45aca9acb681b3fe38b605355
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 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);
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 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;
62 status = smbd_smb2_request_verify_sizes(req, 0x29);
63 if (!NT_STATUS_IS_OK(status)) {
64 return smbd_smb2_request_error(req, status);
66 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
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);
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);
86 if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
87 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
90 in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
91 in_input_buffer.length = in_input_buffer_length;
93 if (req->compat_chain_fsp) {
94 /* skip check */
95 } else if (in_file_id_persistent != in_file_id_volatile) {
96 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
99 subreq = smbd_smb2_getinfo_send(req,
100 req->sconn->smb2.event_ctx,
101 req,
102 in_info_type,
103 in_file_info_class,
104 in_output_buffer_length,
105 in_input_buffer,
106 in_additional_information,
107 in_flags,
108 in_file_id_volatile);
109 if (subreq == NULL) {
110 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
112 tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
114 return smbd_smb2_request_pending_queue(req, subreq);
117 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
119 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
120 struct smbd_smb2_request);
121 int i = req->current_idx;
122 uint8_t *outhdr;
123 DATA_BLOB outbody;
124 DATA_BLOB outdyn;
125 uint16_t out_output_buffer_offset;
126 DATA_BLOB out_output_buffer = data_blob_null;
127 NTSTATUS status;
128 NTSTATUS call_status = NT_STATUS_OK;
129 NTSTATUS error; /* transport error */
131 status = smbd_smb2_getinfo_recv(subreq,
132 req,
133 &out_output_buffer,
134 &call_status);
135 TALLOC_FREE(subreq);
136 if (!NT_STATUS_IS_OK(status)) {
137 error = smbd_smb2_request_error(req, status);
138 if (!NT_STATUS_IS_OK(error)) {
139 smbd_server_connection_terminate(req->sconn,
140 nt_errstr(error));
141 return;
143 return;
146 if (!NT_STATUS_IS_OK(call_status)) {
147 /* Return a specific error with data. */
148 error = smbd_smb2_request_error_ex(req,
149 call_status,
150 &out_output_buffer,
151 __location__);
152 if (!NT_STATUS_IS_OK(error)) {
153 smbd_server_connection_terminate(req->sconn,
154 nt_errstr(error));
155 return;
157 return;
160 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
162 outhdr = (uint8_t *)req->out.vector[i].iov_base;
164 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
165 if (outbody.data == NULL) {
166 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
167 if (!NT_STATUS_IS_OK(error)) {
168 smbd_server_connection_terminate(req->sconn,
169 nt_errstr(error));
170 return;
172 return;
175 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
176 SSVAL(outbody.data, 0x02,
177 out_output_buffer_offset); /* output buffer offset */
178 SIVAL(outbody.data, 0x04,
179 out_output_buffer.length); /* output buffer length */
181 outdyn = out_output_buffer;
183 error = smbd_smb2_request_done(req, outbody, &outdyn);
184 if (!NT_STATUS_IS_OK(error)) {
185 smbd_server_connection_terminate(req->sconn,
186 nt_errstr(error));
187 return;
191 struct smbd_smb2_getinfo_state {
192 struct smbd_smb2_request *smb2req;
193 NTSTATUS status;
194 DATA_BLOB out_output_buffer;
197 static void smb2_ipc_getinfo(struct tevent_req *req,
198 struct smbd_smb2_getinfo_state *state,
199 struct tevent_context *ev,
200 uint8_t in_info_type,
201 uint8_t in_file_info_class)
203 /* We want to reply to SMB2_GETINFO_FILE
204 with a class of SMB2_FILE_STANDARD_INFO as
205 otherwise a Win7 client issues this request
206 twice (2xroundtrips) if we return NOT_SUPPORTED.
207 NB. We do the same for SMB1 in call_trans2qpipeinfo() */
209 if (in_info_type == 0x01 && /* SMB2_GETINFO_FILE */
210 in_file_info_class == 0x05) { /* SMB2_FILE_STANDARD_INFO */
211 state->out_output_buffer = data_blob_talloc(state,
212 NULL, 24);
213 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
214 return;
217 memset(state->out_output_buffer.data,0,24);
218 SOFF_T(state->out_output_buffer.data,0,4096LL);
219 SIVAL(state->out_output_buffer.data,16,1);
220 SIVAL(state->out_output_buffer.data,20,1);
221 tevent_req_done(req);
222 } else {
223 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
227 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
228 struct tevent_context *ev,
229 struct smbd_smb2_request *smb2req,
230 uint8_t in_info_type,
231 uint8_t in_file_info_class,
232 uint32_t in_output_buffer_length,
233 DATA_BLOB in_input_buffer,
234 uint32_t in_additional_information,
235 uint32_t in_flags,
236 uint64_t in_file_id_volatile)
238 struct tevent_req *req;
239 struct smbd_smb2_getinfo_state *state;
240 struct smb_request *smbreq;
241 connection_struct *conn = smb2req->tcon->compat_conn;
242 files_struct *fsp;
243 NTSTATUS status;
245 req = tevent_req_create(mem_ctx, &state,
246 struct smbd_smb2_getinfo_state);
247 if (req == NULL) {
248 return NULL;
250 state->smb2req = smb2req;
251 state->status = NT_STATUS_OK;
252 state->out_output_buffer = data_blob_null;
254 DEBUG(10,("smbd_smb2_getinfo_send: file_id[0x%016llX]\n",
255 (unsigned long long)in_file_id_volatile));
257 smbreq = smbd_smb2_fake_smb_request(smb2req);
258 if (tevent_req_nomem(smbreq, req)) {
259 return tevent_req_post(req, ev);
262 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
263 if (fsp == NULL) {
264 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
265 return tevent_req_post(req, ev);
267 if (conn != fsp->conn) {
268 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
269 return tevent_req_post(req, ev);
271 if (smb2req->session->vuid != fsp->vuid) {
272 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
273 return tevent_req_post(req, ev);
276 if (IS_IPC(conn)) {
277 smb2_ipc_getinfo(req, state, ev,
278 in_info_type, in_file_info_class);
279 return tevent_req_post(req, ev);
282 switch (in_info_type) {
283 case 0x01:/* SMB2_GETINFO_FILE */
285 uint16_t file_info_level;
286 char *data = NULL;
287 unsigned int data_size = 0;
288 bool delete_pending = false;
289 struct timespec write_time_ts;
290 struct file_id fileid;
291 struct ea_list *ea_list = NULL;
292 int lock_data_count = 0;
293 char *lock_data = NULL;
295 ZERO_STRUCT(write_time_ts);
297 switch (in_file_info_class) {
298 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
299 file_info_level = 0xFF00 | in_file_info_class;
300 break;
302 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
303 file_info_level = 0xFF00 | in_file_info_class;
304 break;
306 default:
307 /* the levels directly map to the passthru levels */
308 file_info_level = in_file_info_class + 1000;
309 break;
312 if (fsp->fake_file_handle) {
314 * This is actually for the QUOTA_FAKE_FILE --metze
317 /* We know this name is ok, it's already passed the checks. */
319 } else if (fsp && fsp->fh->fd == -1) {
321 * This is actually a QFILEINFO on a directory
322 * handle (returned from an NT SMB). NT5.0 seems
323 * to do this call. JRA.
326 if (INFO_LEVEL_IS_UNIX(file_info_level)) {
327 /* Always do lstat for UNIX calls. */
328 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
329 DEBUG(3,("smbd_smb2_getinfo_send: "
330 "SMB_VFS_LSTAT of %s failed "
331 "(%s)\n", fsp_str_dbg(fsp),
332 strerror(errno)));
333 status = map_nt_error_from_unix(errno);
334 tevent_req_nterror(req, status);
335 return tevent_req_post(req, ev);
337 } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
338 DEBUG(3,("smbd_smb2_getinfo_send: "
339 "SMB_VFS_STAT of %s failed (%s)\n",
340 fsp_str_dbg(fsp),
341 strerror(errno)));
342 status = map_nt_error_from_unix(errno);
343 tevent_req_nterror(req, status);
344 return tevent_req_post(req, ev);
347 fileid = vfs_file_id_from_sbuf(conn,
348 &fsp->fsp_name->st);
349 get_file_infos(fileid, fsp->name_hash,
350 &delete_pending, &write_time_ts);
351 } else {
353 * Original code - this is an open file.
356 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
357 DEBUG(3, ("smbd_smb2_getinfo_send: "
358 "fstat of fnum %d failed (%s)\n",
359 fsp->fnum, strerror(errno)));
360 status = map_nt_error_from_unix(errno);
361 tevent_req_nterror(req, status);
362 return tevent_req_post(req, ev);
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);
370 status = smbd_do_qfilepathinfo(conn, state,
371 file_info_level,
372 fsp,
373 fsp->fsp_name,
374 delete_pending,
375 write_time_ts,
376 ea_list,
377 lock_data_count,
378 lock_data,
379 STR_UNICODE,
380 in_output_buffer_length,
381 &data,
382 &data_size);
383 if (!NT_STATUS_IS_OK(status)) {
384 SAFE_FREE(data);
385 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
386 status = NT_STATUS_INVALID_INFO_CLASS;
388 tevent_req_nterror(req, status);
389 return tevent_req_post(req, ev);
391 if (data_size > 0) {
392 state->out_output_buffer = data_blob_talloc(state,
393 data,
394 data_size);
395 SAFE_FREE(data);
396 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
397 return tevent_req_post(req, ev);
400 SAFE_FREE(data);
401 break;
404 case 0x02:/* SMB2_GETINFO_FS */
406 uint16_t file_info_level;
407 char *data = NULL;
408 int data_size = 0;
410 /* the levels directly map to the passthru levels */
411 file_info_level = in_file_info_class + 1000;
413 status = smbd_do_qfsinfo(conn, state,
414 file_info_level,
415 STR_UNICODE,
416 in_output_buffer_length,
417 &data,
418 &data_size);
419 if (!NT_STATUS_IS_OK(status)) {
420 SAFE_FREE(data);
421 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
422 status = NT_STATUS_INVALID_INFO_CLASS;
424 tevent_req_nterror(req, status);
425 return tevent_req_post(req, ev);
427 if (data_size > 0) {
428 state->out_output_buffer = data_blob_talloc(state,
429 data,
430 data_size);
431 SAFE_FREE(data);
432 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
433 return tevent_req_post(req, ev);
436 SAFE_FREE(data);
437 break;
440 case 0x03:/* SMB2_GETINFO_SEC */
442 uint8_t *p_marshalled_sd = NULL;
443 size_t sd_size = 0;
445 status = smbd_do_query_security_desc(conn,
446 state,
447 fsp,
448 /* Security info wanted. */
449 in_additional_information,
450 in_output_buffer_length,
451 &p_marshalled_sd,
452 &sd_size);
454 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
455 /* Return needed size. */
456 state->out_output_buffer = data_blob_talloc(state,
457 NULL,
459 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
460 return tevent_req_post(req, ev);
462 SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
463 state->status = NT_STATUS_BUFFER_TOO_SMALL;
464 break;
466 if (!NT_STATUS_IS_OK(status)) {
467 DEBUG(10,("smbd_smb2_getinfo_send: "
468 "smbd_do_query_security_desc of %s failed "
469 "(%s)\n", fsp_str_dbg(fsp),
470 nt_errstr(status)));
471 tevent_req_nterror(req, status);
472 return tevent_req_post(req, ev);
475 if (sd_size > 0) {
476 state->out_output_buffer = data_blob_talloc(state,
477 p_marshalled_sd,
478 sd_size);
479 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
480 return tevent_req_post(req, ev);
483 break;
486 default:
487 DEBUG(10,("smbd_smb2_getinfo_send: "
488 "unknown in_info_type of %u "
489 " for file %s\n",
490 (unsigned int)in_info_type,
491 fsp_str_dbg(fsp) ));
493 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
494 return tevent_req_post(req, ev);
497 tevent_req_done(req);
498 return tevent_req_post(req, ev);
501 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
502 TALLOC_CTX *mem_ctx,
503 DATA_BLOB *out_output_buffer,
504 NTSTATUS *pstatus)
506 NTSTATUS status;
507 struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
508 struct smbd_smb2_getinfo_state);
510 if (tevent_req_is_nterror(req, &status)) {
511 tevent_req_received(req);
512 return status;
515 *out_output_buffer = state->out_output_buffer;
516 talloc_steal(mem_ctx, out_output_buffer->data);
517 *pstatus = state->status;
519 tevent_req_received(req);
520 return NT_STATUS_OK;