Fix typos in LDAP schema files
[Samba.git] / source3 / smbd / smb2_getinfo.c
blob3c8c690342e260ac4f901453ed4f1efe94283846
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 const uint8_t *inhdr;
48 const uint8_t *inbody;
49 int i = req->current_idx;
50 size_t expected_body_size = 0x29;
51 size_t body_size;
52 uint8_t in_info_type;
53 uint8_t in_file_info_class;
54 uint32_t in_output_buffer_length;
55 uint16_t in_input_buffer_offset;
56 uint32_t in_input_buffer_length;
57 DATA_BLOB in_input_buffer;
58 uint32_t in_additional_information;
59 uint32_t in_flags;
60 uint64_t in_file_id_persistent;
61 uint64_t in_file_id_volatile;
62 struct tevent_req *subreq;
64 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
65 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
66 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
69 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
71 body_size = SVAL(inbody, 0x00);
72 if (body_size != expected_body_size) {
73 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
76 in_info_type = CVAL(inbody, 0x02);
77 in_file_info_class = CVAL(inbody, 0x03);
78 in_output_buffer_length = IVAL(inbody, 0x04);
79 in_input_buffer_offset = SVAL(inbody, 0x08);
80 /* 0x0A 2 bytes reserved */
81 in_input_buffer_length = IVAL(inbody, 0x0C);
82 in_additional_information = IVAL(inbody, 0x10);
83 in_flags = IVAL(inbody, 0x14);
84 in_file_id_persistent = BVAL(inbody, 0x18);
85 in_file_id_volatile = BVAL(inbody, 0x20);
87 if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
88 /* This is ok */
89 } else if (in_input_buffer_offset !=
90 (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
91 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
94 if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
95 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
98 in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
99 in_input_buffer.length = in_input_buffer_length;
101 if (req->compat_chain_fsp) {
102 /* skip check */
103 } else if (in_file_id_persistent != in_file_id_volatile) {
104 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
107 subreq = smbd_smb2_getinfo_send(req,
108 req->sconn->smb2.event_ctx,
109 req,
110 in_info_type,
111 in_file_info_class,
112 in_output_buffer_length,
113 in_input_buffer,
114 in_additional_information,
115 in_flags,
116 in_file_id_volatile);
117 if (subreq == NULL) {
118 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
120 tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
122 return smbd_smb2_request_pending_queue(req, subreq);
125 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
127 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
128 struct smbd_smb2_request);
129 int i = req->current_idx;
130 uint8_t *outhdr;
131 DATA_BLOB outbody;
132 DATA_BLOB outdyn;
133 uint16_t out_output_buffer_offset;
134 DATA_BLOB out_output_buffer = data_blob_null;
135 NTSTATUS status;
136 NTSTATUS call_status = NT_STATUS_OK;
137 NTSTATUS error; /* transport error */
139 status = smbd_smb2_getinfo_recv(subreq,
140 req,
141 &out_output_buffer,
142 &call_status);
143 TALLOC_FREE(subreq);
144 if (!NT_STATUS_IS_OK(status)) {
145 error = smbd_smb2_request_error(req, status);
146 if (!NT_STATUS_IS_OK(error)) {
147 smbd_server_connection_terminate(req->sconn,
148 nt_errstr(error));
149 return;
151 return;
154 if (!NT_STATUS_IS_OK(call_status)) {
155 /* Return a specific error with data. */
156 error = smbd_smb2_request_error_ex(req,
157 call_status,
158 &out_output_buffer,
159 __location__);
160 if (!NT_STATUS_IS_OK(error)) {
161 smbd_server_connection_terminate(req->sconn,
162 nt_errstr(error));
163 return;
165 return;
168 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
170 outhdr = (uint8_t *)req->out.vector[i].iov_base;
172 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
173 if (outbody.data == NULL) {
174 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
175 if (!NT_STATUS_IS_OK(error)) {
176 smbd_server_connection_terminate(req->sconn,
177 nt_errstr(error));
178 return;
180 return;
183 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
184 SSVAL(outbody.data, 0x02,
185 out_output_buffer_offset); /* output buffer offset */
186 SIVAL(outbody.data, 0x04,
187 out_output_buffer.length); /* output buffer length */
189 outdyn = out_output_buffer;
191 error = smbd_smb2_request_done(req, outbody, &outdyn);
192 if (!NT_STATUS_IS_OK(error)) {
193 smbd_server_connection_terminate(req->sconn,
194 nt_errstr(error));
195 return;
199 struct smbd_smb2_getinfo_state {
200 struct smbd_smb2_request *smb2req;
201 NTSTATUS status;
202 DATA_BLOB out_output_buffer;
205 static void smb2_ipc_getinfo(struct tevent_req *req,
206 struct smbd_smb2_getinfo_state *state,
207 struct tevent_context *ev,
208 uint8_t in_info_type,
209 uint8_t in_file_info_class)
211 /* We want to reply to SMB2_GETINFO_FILE
212 with a class of SMB2_FILE_STANDARD_INFO as
213 otherwise a Win7 client issues this request
214 twice (2xroundtrips) if we return NOT_SUPPORTED.
215 NB. We do the same for SMB1 in call_trans2qpipeinfo() */
217 if (in_info_type == 0x01 && /* SMB2_GETINFO_FILE */
218 in_file_info_class == 0x05) { /* SMB2_FILE_STANDARD_INFO */
219 state->out_output_buffer = data_blob_talloc(state,
220 NULL, 24);
221 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
222 return;
225 memset(state->out_output_buffer.data,0,24);
226 SOFF_T(state->out_output_buffer.data,0,4096LL);
227 SIVAL(state->out_output_buffer.data,16,1);
228 SIVAL(state->out_output_buffer.data,20,1);
229 tevent_req_done(req);
230 } else {
231 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
235 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
236 struct tevent_context *ev,
237 struct smbd_smb2_request *smb2req,
238 uint8_t in_info_type,
239 uint8_t in_file_info_class,
240 uint32_t in_output_buffer_length,
241 DATA_BLOB in_input_buffer,
242 uint32_t in_additional_information,
243 uint32_t in_flags,
244 uint64_t in_file_id_volatile)
246 struct tevent_req *req;
247 struct smbd_smb2_getinfo_state *state;
248 struct smb_request *smbreq;
249 connection_struct *conn = smb2req->tcon->compat_conn;
250 files_struct *fsp;
251 NTSTATUS status;
253 req = tevent_req_create(mem_ctx, &state,
254 struct smbd_smb2_getinfo_state);
255 if (req == NULL) {
256 return NULL;
258 state->smb2req = smb2req;
259 state->status = NT_STATUS_OK;
260 state->out_output_buffer = data_blob_null;
262 DEBUG(10,("smbd_smb2_getinfo_send: file_id[0x%016llX]\n",
263 (unsigned long long)in_file_id_volatile));
265 smbreq = smbd_smb2_fake_smb_request(smb2req);
266 if (tevent_req_nomem(smbreq, req)) {
267 return tevent_req_post(req, ev);
270 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
271 if (fsp == NULL) {
272 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
273 return tevent_req_post(req, ev);
275 if (conn != fsp->conn) {
276 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
277 return tevent_req_post(req, ev);
279 if (smb2req->session->vuid != fsp->vuid) {
280 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
281 return tevent_req_post(req, ev);
284 if (IS_IPC(conn)) {
285 smb2_ipc_getinfo(req, state, ev,
286 in_info_type, in_file_info_class);
287 return tevent_req_post(req, ev);
290 switch (in_info_type) {
291 case 0x01:/* SMB2_GETINFO_FILE */
293 uint16_t file_info_level;
294 char *data = NULL;
295 unsigned int data_size = 0;
296 bool delete_pending = false;
297 struct timespec write_time_ts;
298 struct file_id fileid;
299 struct ea_list *ea_list = NULL;
300 int lock_data_count = 0;
301 char *lock_data = NULL;
303 ZERO_STRUCT(write_time_ts);
305 switch (in_file_info_class) {
306 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
307 file_info_level = 0xFF00 | in_file_info_class;
308 break;
310 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
311 file_info_level = 0xFF00 | in_file_info_class;
312 break;
314 default:
315 /* the levels directly map to the passthru levels */
316 file_info_level = in_file_info_class + 1000;
317 break;
320 if (fsp->fake_file_handle) {
322 * This is actually for the QUOTA_FAKE_FILE --metze
325 /* We know this name is ok, it's already passed the checks. */
327 } else if (fsp && fsp->fh->fd == -1) {
329 * This is actually a QFILEINFO on a directory
330 * handle (returned from an NT SMB). NT5.0 seems
331 * to do this call. JRA.
334 if (INFO_LEVEL_IS_UNIX(file_info_level)) {
335 /* Always do lstat for UNIX calls. */
336 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
337 DEBUG(3,("smbd_smb2_getinfo_send: "
338 "SMB_VFS_LSTAT of %s failed "
339 "(%s)\n", fsp_str_dbg(fsp),
340 strerror(errno)));
341 status = map_nt_error_from_unix(errno);
342 tevent_req_nterror(req, status);
343 return tevent_req_post(req, ev);
345 } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
346 DEBUG(3,("smbd_smb2_getinfo_send: "
347 "SMB_VFS_STAT of %s failed (%s)\n",
348 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);
355 fileid = vfs_file_id_from_sbuf(conn,
356 &fsp->fsp_name->st);
357 get_file_infos(fileid, fsp->name_hash,
358 &delete_pending, &write_time_ts);
359 } else {
361 * Original code - this is an open file.
364 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
365 DEBUG(3, ("smbd_smb2_getinfo_send: "
366 "fstat of fnum %d failed (%s)\n",
367 fsp->fnum, strerror(errno)));
368 status = map_nt_error_from_unix(errno);
369 tevent_req_nterror(req, status);
370 return tevent_req_post(req, ev);
372 fileid = vfs_file_id_from_sbuf(conn,
373 &fsp->fsp_name->st);
374 get_file_infos(fileid, fsp->name_hash,
375 &delete_pending, &write_time_ts);
378 status = smbd_do_qfilepathinfo(conn, state,
379 file_info_level,
380 fsp,
381 fsp->fsp_name,
382 delete_pending,
383 write_time_ts,
384 ea_list,
385 lock_data_count,
386 lock_data,
387 STR_UNICODE,
388 in_output_buffer_length,
389 &data,
390 &data_size);
391 if (!NT_STATUS_IS_OK(status)) {
392 SAFE_FREE(data);
393 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
394 status = NT_STATUS_INVALID_INFO_CLASS;
396 tevent_req_nterror(req, status);
397 return tevent_req_post(req, ev);
399 if (data_size > 0) {
400 state->out_output_buffer = data_blob_talloc(state,
401 data,
402 data_size);
403 SAFE_FREE(data);
404 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
405 return tevent_req_post(req, ev);
408 SAFE_FREE(data);
409 break;
412 case 0x02:/* SMB2_GETINFO_FS */
414 uint16_t file_info_level;
415 char *data = NULL;
416 int data_size = 0;
418 /* the levels directly map to the passthru levels */
419 file_info_level = in_file_info_class + 1000;
421 status = smbd_do_qfsinfo(conn, state,
422 file_info_level,
423 STR_UNICODE,
424 in_output_buffer_length,
425 &data,
426 &data_size);
427 if (!NT_STATUS_IS_OK(status)) {
428 SAFE_FREE(data);
429 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
430 status = NT_STATUS_INVALID_INFO_CLASS;
432 tevent_req_nterror(req, status);
433 return tevent_req_post(req, ev);
435 if (data_size > 0) {
436 state->out_output_buffer = data_blob_talloc(state,
437 data,
438 data_size);
439 SAFE_FREE(data);
440 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
441 return tevent_req_post(req, ev);
444 SAFE_FREE(data);
445 break;
448 case 0x03:/* SMB2_GETINFO_SEC */
450 uint8_t *p_marshalled_sd = NULL;
451 size_t sd_size = 0;
453 status = smbd_do_query_security_desc(conn,
454 state,
455 fsp,
456 /* Security info wanted. */
457 in_additional_information,
458 in_output_buffer_length,
459 &p_marshalled_sd,
460 &sd_size);
462 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
463 /* Return needed size. */
464 state->out_output_buffer = data_blob_talloc(state,
465 NULL,
467 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
468 return tevent_req_post(req, ev);
470 SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
471 state->status = NT_STATUS_BUFFER_TOO_SMALL;
472 break;
474 if (!NT_STATUS_IS_OK(status)) {
475 DEBUG(10,("smbd_smb2_getinfo_send: "
476 "smbd_do_query_security_desc of %s failed "
477 "(%s)\n", fsp_str_dbg(fsp),
478 nt_errstr(status)));
479 tevent_req_nterror(req, status);
480 return tevent_req_post(req, ev);
483 if (sd_size > 0) {
484 state->out_output_buffer = data_blob_talloc(state,
485 p_marshalled_sd,
486 sd_size);
487 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
488 return tevent_req_post(req, ev);
491 break;
494 default:
495 DEBUG(10,("smbd_smb2_getinfo_send: "
496 "unknown in_info_type of %u "
497 " for file %s\n",
498 (unsigned int)in_info_type,
499 fsp_str_dbg(fsp) ));
501 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
502 return tevent_req_post(req, ev);
505 tevent_req_done(req);
506 return tevent_req_post(req, ev);
509 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
510 TALLOC_CTX *mem_ctx,
511 DATA_BLOB *out_output_buffer,
512 NTSTATUS *pstatus)
514 NTSTATUS status;
515 struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
516 struct smbd_smb2_getinfo_state);
518 if (tevent_req_is_nterror(req, &status)) {
519 tevent_req_received(req);
520 return status;
523 *out_output_buffer = state->out_output_buffer;
524 talloc_steal(mem_ctx, out_output_buffer->data);
525 *pstatus = state->status;
527 tevent_req_received(req);
528 return NT_STATUS_OK;