s3-trans2: only include trans2.h where needed.
[Samba/vl.git] / source3 / smbd / smb2_getinfo.c
blob70225f18d2d9e319fb04fdc1489e9ab349471896
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/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "trans2.h"
27 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
28 struct tevent_context *ev,
29 struct smbd_smb2_request *smb2req,
30 uint8_t in_info_type,
31 uint8_t in_file_info_class,
32 uint32_t in_output_buffer_length,
33 DATA_BLOB in_input_buffer,
34 uint32_t in_additional_information,
35 uint32_t in_flags,
36 uint64_t in_file_id_volatile);
37 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
38 TALLOC_CTX *mem_ctx,
39 DATA_BLOB *out_output_buffer,
40 NTSTATUS *p_call_status);
42 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq);
43 NTSTATUS smbd_smb2_request_process_getinfo(struct smbd_smb2_request *req)
45 const uint8_t *inhdr;
46 const uint8_t *inbody;
47 int i = req->current_idx;
48 size_t expected_body_size = 0x29;
49 size_t body_size;
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 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
63 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
64 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
67 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
69 body_size = SVAL(inbody, 0x00);
70 if (body_size != expected_body_size) {
71 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
74 in_info_type = CVAL(inbody, 0x02);
75 in_file_info_class = CVAL(inbody, 0x03);
76 in_output_buffer_length = IVAL(inbody, 0x04);
77 in_input_buffer_offset = SVAL(inbody, 0x08);
78 /* 0x0A 2 bytes reserved */
79 in_input_buffer_length = IVAL(inbody, 0x0C);
80 in_additional_information = IVAL(inbody, 0x10);
81 in_flags = IVAL(inbody, 0x14);
82 in_file_id_persistent = BVAL(inbody, 0x18);
83 in_file_id_volatile = BVAL(inbody, 0x20);
85 if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
86 /* This is ok */
87 } else if (in_input_buffer_offset !=
88 (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
89 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
92 if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
93 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
96 in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
97 in_input_buffer.length = in_input_buffer_length;
99 if (req->compat_chain_fsp) {
100 /* skip check */
101 } else if (in_file_id_persistent != in_file_id_volatile) {
102 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
105 subreq = smbd_smb2_getinfo_send(req,
106 req->sconn->smb2.event_ctx,
107 req,
108 in_info_type,
109 in_file_info_class,
110 in_output_buffer_length,
111 in_input_buffer,
112 in_additional_information,
113 in_flags,
114 in_file_id_volatile);
115 if (subreq == NULL) {
116 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
118 tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
120 return smbd_smb2_request_pending_queue(req, subreq);
123 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
125 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
126 struct smbd_smb2_request);
127 int i = req->current_idx;
128 uint8_t *outhdr;
129 DATA_BLOB outbody;
130 DATA_BLOB outdyn;
131 uint16_t out_output_buffer_offset;
132 DATA_BLOB out_output_buffer = data_blob_null;
133 NTSTATUS status;
134 NTSTATUS call_status = NT_STATUS_OK;
135 NTSTATUS error; /* transport error */
137 status = smbd_smb2_getinfo_recv(subreq,
138 req,
139 &out_output_buffer,
140 &call_status);
141 TALLOC_FREE(subreq);
142 if (!NT_STATUS_IS_OK(status)) {
143 error = smbd_smb2_request_error(req, status);
144 if (!NT_STATUS_IS_OK(error)) {
145 smbd_server_connection_terminate(req->sconn,
146 nt_errstr(error));
147 return;
149 return;
152 if (!NT_STATUS_IS_OK(call_status)) {
153 /* Return a specific error with data. */
154 error = smbd_smb2_request_error_ex(req,
155 call_status,
156 &out_output_buffer,
157 __location__);
158 if (!NT_STATUS_IS_OK(error)) {
159 smbd_server_connection_terminate(req->sconn,
160 nt_errstr(error));
161 return;
163 return;
166 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
168 outhdr = (uint8_t *)req->out.vector[i].iov_base;
170 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
171 if (outbody.data == NULL) {
172 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
173 if (!NT_STATUS_IS_OK(error)) {
174 smbd_server_connection_terminate(req->sconn,
175 nt_errstr(error));
176 return;
178 return;
181 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
182 SSVAL(outbody.data, 0x02,
183 out_output_buffer_offset); /* output buffer offset */
184 SIVAL(outbody.data, 0x04,
185 out_output_buffer.length); /* output buffer length */
187 outdyn = out_output_buffer;
189 error = smbd_smb2_request_done(req, outbody, &outdyn);
190 if (!NT_STATUS_IS_OK(error)) {
191 smbd_server_connection_terminate(req->sconn,
192 nt_errstr(error));
193 return;
197 struct smbd_smb2_getinfo_state {
198 struct smbd_smb2_request *smb2req;
199 NTSTATUS status;
200 DATA_BLOB out_output_buffer;
203 static void smb2_ipc_getinfo(struct tevent_req *req,
204 struct smbd_smb2_getinfo_state *state,
205 struct tevent_context *ev,
206 uint8_t in_info_type,
207 uint8_t in_file_info_class)
209 /* We want to reply to SMB2_GETINFO_FILE
210 with a class of SMB2_FILE_STANDARD_INFO as
211 otherwise a Win7 client issues this request
212 twice (2xroundtrips) if we return NOT_SUPPORTED.
213 NB. We do the same for SMB1 in call_trans2qpipeinfo() */
215 if (in_info_type == 0x01 && /* SMB2_GETINFO_FILE */
216 in_file_info_class == 0x05) { /* SMB2_FILE_STANDARD_INFO */
217 state->out_output_buffer = data_blob_talloc(state,
218 NULL, 24);
219 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
220 return;
223 memset(state->out_output_buffer.data,0,24);
224 SOFF_T(state->out_output_buffer.data,0,4096LL);
225 SIVAL(state->out_output_buffer.data,16,1);
226 SIVAL(state->out_output_buffer.data,20,1);
227 tevent_req_done(req);
228 } else {
229 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
233 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
234 struct tevent_context *ev,
235 struct smbd_smb2_request *smb2req,
236 uint8_t in_info_type,
237 uint8_t in_file_info_class,
238 uint32_t in_output_buffer_length,
239 DATA_BLOB in_input_buffer,
240 uint32_t in_additional_information,
241 uint32_t in_flags,
242 uint64_t in_file_id_volatile)
244 struct tevent_req *req;
245 struct smbd_smb2_getinfo_state *state;
246 struct smb_request *smbreq;
247 connection_struct *conn = smb2req->tcon->compat_conn;
248 files_struct *fsp;
249 NTSTATUS status;
251 req = tevent_req_create(mem_ctx, &state,
252 struct smbd_smb2_getinfo_state);
253 if (req == NULL) {
254 return NULL;
256 state->smb2req = smb2req;
257 state->status = NT_STATUS_OK;
258 state->out_output_buffer = data_blob_null;
260 DEBUG(10,("smbd_smb2_getinfo_send: file_id[0x%016llX]\n",
261 (unsigned long long)in_file_id_volatile));
263 smbreq = smbd_smb2_fake_smb_request(smb2req);
264 if (tevent_req_nomem(smbreq, req)) {
265 return tevent_req_post(req, ev);
268 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
269 if (fsp == NULL) {
270 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
271 return tevent_req_post(req, ev);
273 if (conn != fsp->conn) {
274 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
275 return tevent_req_post(req, ev);
277 if (smb2req->session->vuid != fsp->vuid) {
278 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
279 return tevent_req_post(req, ev);
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);
288 switch (in_info_type) {
289 case 0x01:/* SMB2_GETINFO_FILE */
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;
301 ZERO_STRUCT(write_time_ts);
303 switch (in_file_info_class) {
304 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
305 file_info_level = 0xFF00 | in_file_info_class;
306 break;
308 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
309 file_info_level = 0xFF00 | in_file_info_class;
310 break;
312 default:
313 /* the levels directly map to the passthru levels */
314 file_info_level = in_file_info_class + 1000;
315 break;
318 if (fsp->fake_file_handle) {
320 * This is actually for the QUOTA_FAKE_FILE --metze
323 /* We know this name is ok, it's already passed the checks. */
325 } else if (fsp && fsp->fh->fd == -1) {
327 * This is actually a QFILEINFO on a directory
328 * handle (returned from an NT SMB). NT5.0 seems
329 * to do this call. JRA.
332 if (INFO_LEVEL_IS_UNIX(file_info_level)) {
333 /* Always do lstat for UNIX calls. */
334 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
335 DEBUG(3,("smbd_smb2_getinfo_send: "
336 "SMB_VFS_LSTAT of %s failed "
337 "(%s)\n", fsp_str_dbg(fsp),
338 strerror(errno)));
339 status = map_nt_error_from_unix(errno);
340 tevent_req_nterror(req, status);
341 return tevent_req_post(req, ev);
343 } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
344 DEBUG(3,("smbd_smb2_getinfo_send: "
345 "SMB_VFS_STAT of %s failed (%s)\n",
346 fsp_str_dbg(fsp),
347 strerror(errno)));
348 status = map_nt_error_from_unix(errno);
349 tevent_req_nterror(req, status);
350 return tevent_req_post(req, ev);
353 fileid = vfs_file_id_from_sbuf(conn,
354 &fsp->fsp_name->st);
355 get_file_infos(fileid, fsp->name_hash,
356 &delete_pending, &write_time_ts);
357 } else {
359 * Original code - this is an open file.
362 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
363 DEBUG(3, ("smbd_smb2_getinfo_send: "
364 "fstat of fnum %d failed (%s)\n",
365 fsp->fnum, strerror(errno)));
366 status = map_nt_error_from_unix(errno);
367 tevent_req_nterror(req, status);
368 return tevent_req_post(req, ev);
370 fileid = vfs_file_id_from_sbuf(conn,
371 &fsp->fsp_name->st);
372 get_file_infos(fileid, fsp->name_hash,
373 &delete_pending, &write_time_ts);
376 status = smbd_do_qfilepathinfo(conn, state,
377 file_info_level,
378 fsp,
379 fsp->fsp_name,
380 delete_pending,
381 write_time_ts,
382 ea_list,
383 lock_data_count,
384 lock_data,
385 STR_UNICODE,
386 in_output_buffer_length,
387 &data,
388 &data_size);
389 if (!NT_STATUS_IS_OK(status)) {
390 SAFE_FREE(data);
391 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
392 status = NT_STATUS_INVALID_INFO_CLASS;
394 tevent_req_nterror(req, status);
395 return tevent_req_post(req, ev);
397 if (data_size > 0) {
398 state->out_output_buffer = data_blob_talloc(state,
399 data,
400 data_size);
401 SAFE_FREE(data);
402 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
403 return tevent_req_post(req, ev);
406 SAFE_FREE(data);
407 break;
410 case 0x02:/* SMB2_GETINFO_FS */
412 uint16_t file_info_level;
413 char *data = NULL;
414 int data_size = 0;
416 /* the levels directly map to the passthru levels */
417 file_info_level = in_file_info_class + 1000;
419 status = smbd_do_qfsinfo(conn, state,
420 file_info_level,
421 STR_UNICODE,
422 in_output_buffer_length,
423 &data,
424 &data_size);
425 if (!NT_STATUS_IS_OK(status)) {
426 SAFE_FREE(data);
427 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
428 status = NT_STATUS_INVALID_INFO_CLASS;
430 tevent_req_nterror(req, status);
431 return tevent_req_post(req, ev);
433 if (data_size > 0) {
434 state->out_output_buffer = data_blob_talloc(state,
435 data,
436 data_size);
437 SAFE_FREE(data);
438 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
439 return tevent_req_post(req, ev);
442 SAFE_FREE(data);
443 break;
446 case 0x03:/* SMB2_GETINFO_SEC */
448 uint8_t *p_marshalled_sd = NULL;
449 size_t sd_size = 0;
451 status = smbd_do_query_security_desc(conn,
452 state,
453 fsp,
454 /* Security info wanted. */
455 in_additional_information,
456 in_output_buffer_length,
457 &p_marshalled_sd,
458 &sd_size);
460 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
461 /* Return needed size. */
462 state->out_output_buffer = data_blob_talloc(state,
463 NULL,
465 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
466 return tevent_req_post(req, ev);
468 SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
469 state->status = NT_STATUS_BUFFER_TOO_SMALL;
470 break;
472 if (!NT_STATUS_IS_OK(status)) {
473 DEBUG(10,("smbd_smb2_getinfo_send: "
474 "smbd_do_query_security_desc of %s failed "
475 "(%s)\n", fsp_str_dbg(fsp),
476 nt_errstr(status)));
477 tevent_req_nterror(req, status);
478 return tevent_req_post(req, ev);
481 if (sd_size > 0) {
482 state->out_output_buffer = data_blob_talloc(state,
483 p_marshalled_sd,
484 sd_size);
485 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
486 return tevent_req_post(req, ev);
489 break;
492 default:
493 DEBUG(10,("smbd_smb2_getinfo_send: "
494 "unknown in_info_type of %u "
495 " for file %s\n",
496 (unsigned int)in_info_type,
497 fsp_str_dbg(fsp) ));
499 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
500 return tevent_req_post(req, ev);
503 tevent_req_done(req);
504 return tevent_req_post(req, ev);
507 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
508 TALLOC_CTX *mem_ctx,
509 DATA_BLOB *out_output_buffer,
510 NTSTATUS *pstatus)
512 NTSTATUS status;
513 struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
514 struct smbd_smb2_getinfo_state);
516 if (tevent_req_is_nterror(req, &status)) {
517 tevent_req_received(req);
518 return status;
521 *out_output_buffer = state->out_output_buffer;
522 talloc_steal(mem_ctx, out_output_buffer->data);
523 *pstatus = state->status;
525 tevent_req_received(req);
526 return NT_STATUS_OK;