s3: Lift the server_messaging_context from notify_job_status
[Samba/gbeck.git] / source3 / smbd / smb2_getinfo.c
blob09234cd129518e6109eb176d585ab105554afbf9
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"
26 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
27 struct tevent_context *ev,
28 struct smbd_smb2_request *smb2req,
29 uint8_t in_info_type,
30 uint8_t in_file_info_class,
31 uint32_t in_output_buffer_length,
32 DATA_BLOB in_input_buffer,
33 uint32_t in_additional_information,
34 uint32_t in_flags,
35 uint64_t in_file_id_volatile);
36 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
37 TALLOC_CTX *mem_ctx,
38 DATA_BLOB *out_output_buffer,
39 NTSTATUS *p_call_status);
41 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq);
42 NTSTATUS smbd_smb2_request_process_getinfo(struct smbd_smb2_request *req)
44 const uint8_t *inhdr;
45 const uint8_t *inbody;
46 int i = req->current_idx;
47 size_t expected_body_size = 0x29;
48 size_t body_size;
49 uint8_t in_info_type;
50 uint8_t in_file_info_class;
51 uint32_t in_output_buffer_length;
52 uint16_t in_input_buffer_offset;
53 uint32_t in_input_buffer_length;
54 DATA_BLOB in_input_buffer;
55 uint32_t in_additional_information;
56 uint32_t in_flags;
57 uint64_t in_file_id_persistent;
58 uint64_t in_file_id_volatile;
59 struct tevent_req *subreq;
61 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
62 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
63 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
66 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
68 body_size = SVAL(inbody, 0x00);
69 if (body_size != expected_body_size) {
70 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
73 in_info_type = CVAL(inbody, 0x02);
74 in_file_info_class = CVAL(inbody, 0x03);
75 in_output_buffer_length = IVAL(inbody, 0x04);
76 in_input_buffer_offset = SVAL(inbody, 0x08);
77 /* 0x0A 2 bytes reserved */
78 in_input_buffer_length = IVAL(inbody, 0x0C);
79 in_additional_information = IVAL(inbody, 0x10);
80 in_flags = IVAL(inbody, 0x14);
81 in_file_id_persistent = BVAL(inbody, 0x18);
82 in_file_id_volatile = BVAL(inbody, 0x20);
84 if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
85 /* This is ok */
86 } else if (in_input_buffer_offset !=
87 (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
88 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
91 if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
92 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
95 in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
96 in_input_buffer.length = in_input_buffer_length;
98 if (req->compat_chain_fsp) {
99 /* skip check */
100 } else if (in_file_id_persistent != in_file_id_volatile) {
101 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
104 subreq = smbd_smb2_getinfo_send(req,
105 req->sconn->smb2.event_ctx,
106 req,
107 in_info_type,
108 in_file_info_class,
109 in_output_buffer_length,
110 in_input_buffer,
111 in_additional_information,
112 in_flags,
113 in_file_id_volatile);
114 if (subreq == NULL) {
115 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
117 tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
119 return smbd_smb2_request_pending_queue(req, subreq);
122 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
124 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
125 struct smbd_smb2_request);
126 int i = req->current_idx;
127 uint8_t *outhdr;
128 DATA_BLOB outbody;
129 DATA_BLOB outdyn;
130 uint16_t out_output_buffer_offset;
131 DATA_BLOB out_output_buffer = data_blob_null;
132 NTSTATUS status;
133 NTSTATUS call_status = NT_STATUS_OK;
134 NTSTATUS error; /* transport error */
136 status = smbd_smb2_getinfo_recv(subreq,
137 req,
138 &out_output_buffer,
139 &call_status);
140 TALLOC_FREE(subreq);
141 if (!NT_STATUS_IS_OK(status)) {
142 error = smbd_smb2_request_error(req, status);
143 if (!NT_STATUS_IS_OK(error)) {
144 smbd_server_connection_terminate(req->sconn,
145 nt_errstr(error));
146 return;
148 return;
151 if (!NT_STATUS_IS_OK(call_status)) {
152 /* Return a specific error with data. */
153 error = smbd_smb2_request_error_ex(req,
154 call_status,
155 &out_output_buffer,
156 __location__);
157 if (!NT_STATUS_IS_OK(error)) {
158 smbd_server_connection_terminate(req->sconn,
159 nt_errstr(error));
160 return;
162 return;
165 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
167 outhdr = (uint8_t *)req->out.vector[i].iov_base;
169 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
170 if (outbody.data == NULL) {
171 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
172 if (!NT_STATUS_IS_OK(error)) {
173 smbd_server_connection_terminate(req->sconn,
174 nt_errstr(error));
175 return;
177 return;
180 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
181 SSVAL(outbody.data, 0x02,
182 out_output_buffer_offset); /* output buffer offset */
183 SIVAL(outbody.data, 0x04,
184 out_output_buffer.length); /* output buffer length */
186 outdyn = out_output_buffer;
188 error = smbd_smb2_request_done(req, outbody, &outdyn);
189 if (!NT_STATUS_IS_OK(error)) {
190 smbd_server_connection_terminate(req->sconn,
191 nt_errstr(error));
192 return;
196 struct smbd_smb2_getinfo_state {
197 struct smbd_smb2_request *smb2req;
198 NTSTATUS status;
199 DATA_BLOB out_output_buffer;
202 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
203 struct tevent_context *ev,
204 struct smbd_smb2_request *smb2req,
205 uint8_t in_info_type,
206 uint8_t in_file_info_class,
207 uint32_t in_output_buffer_length,
208 DATA_BLOB in_input_buffer,
209 uint32_t in_additional_information,
210 uint32_t in_flags,
211 uint64_t in_file_id_volatile)
213 struct tevent_req *req;
214 struct smbd_smb2_getinfo_state *state;
215 struct smb_request *smbreq;
216 connection_struct *conn = smb2req->tcon->compat_conn;
217 files_struct *fsp;
218 NTSTATUS status;
220 req = tevent_req_create(mem_ctx, &state,
221 struct smbd_smb2_getinfo_state);
222 if (req == NULL) {
223 return NULL;
225 state->smb2req = smb2req;
226 state->status = NT_STATUS_OK;
227 state->out_output_buffer = data_blob_null;
229 DEBUG(10,("smbd_smb2_getinfo_send: file_id[0x%016llX]\n",
230 (unsigned long long)in_file_id_volatile));
232 smbreq = smbd_smb2_fake_smb_request(smb2req);
233 if (tevent_req_nomem(smbreq, req)) {
234 return tevent_req_post(req, ev);
237 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
238 if (fsp == NULL) {
239 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
240 return tevent_req_post(req, ev);
242 if (conn != fsp->conn) {
243 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
244 return tevent_req_post(req, ev);
246 if (smb2req->session->vuid != fsp->vuid) {
247 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
248 return tevent_req_post(req, ev);
251 if (IS_IPC(conn)) {
252 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
253 return tevent_req_post(req, ev);
256 switch (in_info_type) {
257 case 0x01:/* SMB2_GETINFO_FILE */
259 uint16_t file_info_level;
260 char *data = NULL;
261 unsigned int data_size = 0;
262 bool delete_pending = false;
263 struct timespec write_time_ts;
264 struct file_id fileid;
265 struct ea_list *ea_list = NULL;
266 int lock_data_count = 0;
267 char *lock_data = NULL;
269 ZERO_STRUCT(write_time_ts);
271 switch (in_file_info_class) {
272 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
273 file_info_level = 0xFF00 | in_file_info_class;
274 break;
276 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
277 file_info_level = 0xFF00 | in_file_info_class;
278 break;
280 default:
281 /* the levels directly map to the passthru levels */
282 file_info_level = in_file_info_class + 1000;
283 break;
286 if (fsp->fake_file_handle) {
288 * This is actually for the QUOTA_FAKE_FILE --metze
291 /* We know this name is ok, it's already passed the checks. */
293 } else if (fsp && (fsp->is_directory || fsp->fh->fd == -1)) {
295 * This is actually a QFILEINFO on a directory
296 * handle (returned from an NT SMB). NT5.0 seems
297 * to do this call. JRA.
300 if (INFO_LEVEL_IS_UNIX(file_info_level)) {
301 /* Always do lstat for UNIX calls. */
302 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
303 DEBUG(3,("smbd_smb2_getinfo_send: "
304 "SMB_VFS_LSTAT of %s failed "
305 "(%s)\n", fsp_str_dbg(fsp),
306 strerror(errno)));
307 status = map_nt_error_from_unix(errno);
308 tevent_req_nterror(req, status);
309 return tevent_req_post(req, ev);
311 } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
312 DEBUG(3,("smbd_smb2_getinfo_send: "
313 "SMB_VFS_STAT of %s failed (%s)\n",
314 fsp_str_dbg(fsp),
315 strerror(errno)));
316 status = map_nt_error_from_unix(errno);
317 tevent_req_nterror(req, status);
318 return tevent_req_post(req, ev);
321 fileid = vfs_file_id_from_sbuf(conn,
322 &fsp->fsp_name->st);
323 get_file_infos(fileid, &delete_pending, &write_time_ts);
324 } else {
326 * Original code - this is an open file.
329 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
330 DEBUG(3, ("smbd_smb2_getinfo_send: "
331 "fstat of fnum %d failed (%s)\n",
332 fsp->fnum, strerror(errno)));
333 status = map_nt_error_from_unix(errno);
334 tevent_req_nterror(req, status);
335 return tevent_req_post(req, ev);
337 fileid = vfs_file_id_from_sbuf(conn,
338 &fsp->fsp_name->st);
339 get_file_infos(fileid, &delete_pending, &write_time_ts);
342 status = smbd_do_qfilepathinfo(conn, state,
343 file_info_level,
344 fsp,
345 fsp->fsp_name,
346 delete_pending,
347 write_time_ts,
348 ea_list,
349 lock_data_count,
350 lock_data,
351 STR_UNICODE,
352 in_output_buffer_length,
353 &data,
354 &data_size);
355 if (!NT_STATUS_IS_OK(status)) {
356 SAFE_FREE(data);
357 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
358 status = NT_STATUS_INVALID_INFO_CLASS;
360 tevent_req_nterror(req, status);
361 return tevent_req_post(req, ev);
363 if (data_size > 0) {
364 state->out_output_buffer = data_blob_talloc(state,
365 data,
366 data_size);
367 SAFE_FREE(data);
368 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
369 return tevent_req_post(req, ev);
372 SAFE_FREE(data);
373 break;
376 case 0x02:/* SMB2_GETINFO_FS */
378 uint16_t file_info_level;
379 char *data = NULL;
380 int data_size = 0;
382 /* the levels directly map to the passthru levels */
383 file_info_level = in_file_info_class + 1000;
385 status = smbd_do_qfsinfo(conn, state,
386 file_info_level,
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 0x03:/* SMB2_GETINFO_SEC */
414 uint8_t *p_marshalled_sd = NULL;
415 size_t sd_size = 0;
417 status = smbd_do_query_security_desc(conn,
418 state,
419 fsp,
420 /* Security info wanted. */
421 in_additional_information,
422 in_output_buffer_length,
423 &p_marshalled_sd,
424 &sd_size);
426 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
427 /* Return needed size. */
428 state->out_output_buffer = data_blob_talloc(state,
429 NULL,
431 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
432 return tevent_req_post(req, ev);
434 SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
435 state->status = NT_STATUS_BUFFER_TOO_SMALL;
436 break;
438 if (!NT_STATUS_IS_OK(status)) {
439 DEBUG(10,("smbd_smb2_getinfo_send: "
440 "smbd_do_query_security_desc of %s failed "
441 "(%s)\n", fsp_str_dbg(fsp),
442 nt_errstr(status)));
443 tevent_req_nterror(req, status);
444 return tevent_req_post(req, ev);
447 if (sd_size > 0) {
448 state->out_output_buffer = data_blob_talloc(state,
449 p_marshalled_sd,
450 sd_size);
451 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
452 return tevent_req_post(req, ev);
455 break;
458 default:
459 DEBUG(10,("smbd_smb2_getinfo_send: "
460 "unknown in_info_type of %u "
461 " for file %s\n",
462 (unsigned int)in_info_type,
463 fsp_str_dbg(fsp) ));
465 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
466 return tevent_req_post(req, ev);
469 tevent_req_done(req);
470 return tevent_req_post(req, ev);
473 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
474 TALLOC_CTX *mem_ctx,
475 DATA_BLOB *out_output_buffer,
476 NTSTATUS *pstatus)
478 NTSTATUS status;
479 struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
480 struct smbd_smb2_getinfo_state);
482 if (tevent_req_is_nterror(req, &status)) {
483 tevent_req_received(req);
484 return status;
487 *out_output_buffer = state->out_output_buffer;
488 talloc_steal(mem_ctx, out_output_buffer->data);
489 *pstatus = state->status;
491 tevent_req_received(req);
492 return NT_STATUS_OK;