s4 dns: Only forward for zones we don't own
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_getinfo.c
blobe8d918df388a3e44ee0db3b2d87a43622cbc91c3
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 (in_input_buffer.length > req->sconn->smb2.max_trans) {
94 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
96 if (in_output_buffer_length > req->sconn->smb2.max_trans) {
97 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
100 status = smbd_smb2_request_verify_creditcharge(req,
101 MAX(in_input_buffer.length,in_output_buffer_length));
102 if (!NT_STATUS_IS_OK(status)) {
103 return smbd_smb2_request_error(req, status);
106 if (req->compat_chain_fsp) {
107 /* skip check */
108 } else if (in_file_id_persistent != in_file_id_volatile) {
109 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
112 subreq = smbd_smb2_getinfo_send(req,
113 req->sconn->ev_ctx,
114 req,
115 in_info_type,
116 in_file_info_class,
117 in_output_buffer_length,
118 in_input_buffer,
119 in_additional_information,
120 in_flags,
121 in_file_id_volatile);
122 if (subreq == NULL) {
123 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
125 tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
127 return smbd_smb2_request_pending_queue(req, subreq, 500);
130 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
132 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
133 struct smbd_smb2_request);
134 DATA_BLOB outbody;
135 DATA_BLOB outdyn;
136 uint16_t out_output_buffer_offset;
137 DATA_BLOB out_output_buffer = data_blob_null;
138 NTSTATUS status;
139 NTSTATUS call_status = NT_STATUS_OK;
140 NTSTATUS error; /* transport error */
142 status = smbd_smb2_getinfo_recv(subreq,
143 req,
144 &out_output_buffer,
145 &call_status);
146 TALLOC_FREE(subreq);
147 if (!NT_STATUS_IS_OK(status)) {
148 error = smbd_smb2_request_error(req, status);
149 if (!NT_STATUS_IS_OK(error)) {
150 smbd_server_connection_terminate(req->sconn,
151 nt_errstr(error));
152 return;
154 return;
157 if (!NT_STATUS_IS_OK(call_status)) {
158 /* Return a specific error with data. */
159 error = smbd_smb2_request_error_ex(req,
160 call_status,
161 &out_output_buffer,
162 __location__);
163 if (!NT_STATUS_IS_OK(error)) {
164 smbd_server_connection_terminate(req->sconn,
165 nt_errstr(error));
166 return;
168 return;
171 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
173 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
174 if (outbody.data == NULL) {
175 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
176 if (!NT_STATUS_IS_OK(error)) {
177 smbd_server_connection_terminate(req->sconn,
178 nt_errstr(error));
179 return;
181 return;
184 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
185 SSVAL(outbody.data, 0x02,
186 out_output_buffer_offset); /* output buffer offset */
187 SIVAL(outbody.data, 0x04,
188 out_output_buffer.length); /* output buffer length */
190 outdyn = out_output_buffer;
192 error = smbd_smb2_request_done(req, outbody, &outdyn);
193 if (!NT_STATUS_IS_OK(error)) {
194 smbd_server_connection_terminate(req->sconn,
195 nt_errstr(error));
196 return;
200 struct smbd_smb2_getinfo_state {
201 struct smbd_smb2_request *smb2req;
202 NTSTATUS status;
203 DATA_BLOB out_output_buffer;
206 static void smb2_ipc_getinfo(struct tevent_req *req,
207 struct smbd_smb2_getinfo_state *state,
208 struct tevent_context *ev,
209 uint8_t in_info_type,
210 uint8_t in_file_info_class)
212 /* We want to reply to SMB2_GETINFO_FILE
213 with a class of SMB2_FILE_STANDARD_INFO as
214 otherwise a Win7 client issues this request
215 twice (2xroundtrips) if we return NOT_SUPPORTED.
216 NB. We do the same for SMB1 in call_trans2qpipeinfo() */
218 if (in_info_type == 0x01 && /* SMB2_GETINFO_FILE */
219 in_file_info_class == 0x05) { /* SMB2_FILE_STANDARD_INFO */
220 state->out_output_buffer = data_blob_talloc(state,
221 NULL, 24);
222 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
223 return;
226 memset(state->out_output_buffer.data,0,24);
227 SOFF_T(state->out_output_buffer.data,0,4096LL);
228 SIVAL(state->out_output_buffer.data,16,1);
229 SIVAL(state->out_output_buffer.data,20,1);
230 tevent_req_done(req);
231 } else {
232 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
236 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
237 struct tevent_context *ev,
238 struct smbd_smb2_request *smb2req,
239 uint8_t in_info_type,
240 uint8_t in_file_info_class,
241 uint32_t in_output_buffer_length,
242 DATA_BLOB in_input_buffer,
243 uint32_t in_additional_information,
244 uint32_t in_flags,
245 uint64_t in_file_id_volatile)
247 struct tevent_req *req;
248 struct smbd_smb2_getinfo_state *state;
249 struct smb_request *smbreq;
250 connection_struct *conn = smb2req->tcon->compat_conn;
251 files_struct *fsp;
252 NTSTATUS status;
254 req = tevent_req_create(mem_ctx, &state,
255 struct smbd_smb2_getinfo_state);
256 if (req == NULL) {
257 return NULL;
259 state->smb2req = smb2req;
260 state->status = NT_STATUS_OK;
261 state->out_output_buffer = data_blob_null;
263 DEBUG(10,("smbd_smb2_getinfo_send: file_id[0x%016llX]\n",
264 (unsigned long long)in_file_id_volatile));
266 smbreq = smbd_smb2_fake_smb_request(smb2req);
267 if (tevent_req_nomem(smbreq, req)) {
268 return tevent_req_post(req, ev);
271 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
272 if (fsp == NULL) {
273 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
274 return tevent_req_post(req, ev);
276 if (conn != fsp->conn) {
277 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
278 return tevent_req_post(req, ev);
280 if (smb2req->session->vuid != fsp->vuid) {
281 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
282 return tevent_req_post(req, ev);
285 if (IS_IPC(conn)) {
286 smb2_ipc_getinfo(req, state, ev,
287 in_info_type, in_file_info_class);
288 return tevent_req_post(req, ev);
291 switch (in_info_type) {
292 case 0x01:/* SMB2_GETINFO_FILE */
294 uint16_t file_info_level;
295 char *data = NULL;
296 unsigned int data_size = 0;
297 bool delete_pending = false;
298 struct timespec write_time_ts;
299 struct file_id fileid;
300 struct ea_list *ea_list = NULL;
301 int lock_data_count = 0;
302 char *lock_data = NULL;
304 ZERO_STRUCT(write_time_ts);
306 switch (in_file_info_class) {
307 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
308 file_info_level = 0xFF00 | in_file_info_class;
309 break;
311 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
312 file_info_level = 0xFF00 | in_file_info_class;
313 break;
315 default:
316 /* the levels directly map to the passthru levels */
317 file_info_level = in_file_info_class + 1000;
318 break;
321 if (fsp->fake_file_handle) {
323 * This is actually for the QUOTA_FAKE_FILE --metze
326 /* We know this name is ok, it's already passed the checks. */
328 } else if (fsp && fsp->fh->fd == -1) {
330 * This is actually a QFILEINFO on a directory
331 * handle (returned from an NT SMB). NT5.0 seems
332 * to do this call. JRA.
335 if (INFO_LEVEL_IS_UNIX(file_info_level)) {
336 /* Always do lstat for UNIX calls. */
337 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
338 DEBUG(3,("smbd_smb2_getinfo_send: "
339 "SMB_VFS_LSTAT of %s failed "
340 "(%s)\n", 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);
346 } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
347 DEBUG(3,("smbd_smb2_getinfo_send: "
348 "SMB_VFS_STAT of %s failed (%s)\n",
349 fsp_str_dbg(fsp),
350 strerror(errno)));
351 status = map_nt_error_from_unix(errno);
352 tevent_req_nterror(req, status);
353 return tevent_req_post(req, ev);
356 fileid = vfs_file_id_from_sbuf(conn,
357 &fsp->fsp_name->st);
358 get_file_infos(fileid, fsp->name_hash,
359 &delete_pending, &write_time_ts);
360 } else {
362 * Original code - this is an open file.
365 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
366 DEBUG(3, ("smbd_smb2_getinfo_send: "
367 "fstat of fnum %d failed (%s)\n",
368 fsp->fnum, strerror(errno)));
369 status = map_nt_error_from_unix(errno);
370 tevent_req_nterror(req, status);
371 return tevent_req_post(req, ev);
373 fileid = vfs_file_id_from_sbuf(conn,
374 &fsp->fsp_name->st);
375 get_file_infos(fileid, fsp->name_hash,
376 &delete_pending, &write_time_ts);
379 status = smbd_do_qfilepathinfo(conn, state,
380 file_info_level,
381 fsp,
382 fsp->fsp_name,
383 delete_pending,
384 write_time_ts,
385 ea_list,
386 lock_data_count,
387 lock_data,
388 STR_UNICODE,
389 in_output_buffer_length,
390 &data,
391 &data_size);
392 if (!NT_STATUS_IS_OK(status)) {
393 SAFE_FREE(data);
394 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
395 status = NT_STATUS_INVALID_INFO_CLASS;
397 tevent_req_nterror(req, status);
398 return tevent_req_post(req, ev);
400 if (data_size > 0) {
401 state->out_output_buffer = data_blob_talloc(state,
402 data,
403 data_size);
404 SAFE_FREE(data);
405 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
406 return tevent_req_post(req, ev);
409 SAFE_FREE(data);
410 break;
413 case 0x02:/* SMB2_GETINFO_FS */
415 uint16_t file_info_level;
416 char *data = NULL;
417 int data_size = 0;
419 /* the levels directly map to the passthru levels */
420 file_info_level = in_file_info_class + 1000;
422 status = smbd_do_qfsinfo(conn, state,
423 file_info_level,
424 STR_UNICODE,
425 in_output_buffer_length,
426 &data,
427 &data_size);
428 if (!NT_STATUS_IS_OK(status)) {
429 SAFE_FREE(data);
430 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
431 status = NT_STATUS_INVALID_INFO_CLASS;
433 tevent_req_nterror(req, status);
434 return tevent_req_post(req, ev);
436 if (data_size > 0) {
437 state->out_output_buffer = data_blob_talloc(state,
438 data,
439 data_size);
440 SAFE_FREE(data);
441 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
442 return tevent_req_post(req, ev);
445 SAFE_FREE(data);
446 break;
449 case 0x03:/* SMB2_GETINFO_SEC */
451 uint8_t *p_marshalled_sd = NULL;
452 size_t sd_size = 0;
454 status = smbd_do_query_security_desc(conn,
455 state,
456 fsp,
457 /* Security info wanted. */
458 in_additional_information,
459 in_output_buffer_length,
460 &p_marshalled_sd,
461 &sd_size);
463 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
464 /* Return needed size. */
465 state->out_output_buffer = data_blob_talloc(state,
466 NULL,
468 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
469 return tevent_req_post(req, ev);
471 SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
472 state->status = NT_STATUS_BUFFER_TOO_SMALL;
473 break;
475 if (!NT_STATUS_IS_OK(status)) {
476 DEBUG(10,("smbd_smb2_getinfo_send: "
477 "smbd_do_query_security_desc of %s failed "
478 "(%s)\n", fsp_str_dbg(fsp),
479 nt_errstr(status)));
480 tevent_req_nterror(req, status);
481 return tevent_req_post(req, ev);
484 if (sd_size > 0) {
485 state->out_output_buffer = data_blob_talloc(state,
486 p_marshalled_sd,
487 sd_size);
488 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
489 return tevent_req_post(req, ev);
492 break;
495 default:
496 DEBUG(10,("smbd_smb2_getinfo_send: "
497 "unknown in_info_type of %u "
498 " for file %s\n",
499 (unsigned int)in_info_type,
500 fsp_str_dbg(fsp) ));
502 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
503 return tevent_req_post(req, ev);
506 tevent_req_done(req);
507 return tevent_req_post(req, ev);
510 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
511 TALLOC_CTX *mem_ctx,
512 DATA_BLOB *out_output_buffer,
513 NTSTATUS *pstatus)
515 NTSTATUS status;
516 struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
517 struct smbd_smb2_getinfo_state);
519 if (tevent_req_is_nterror(req, &status)) {
520 tevent_req_received(req);
521 return status;
524 *out_output_buffer = state->out_output_buffer;
525 talloc_steal(mem_ctx, out_output_buffer->data);
526 *pstatus = state->status;
528 tevent_req_received(req);
529 return NT_STATUS_OK;