smbd: Use #defines in smb2_getinfo_send
[Samba.git] / source3 / smbd / smb2_getinfo.c
blob36b5140396c71c88b1ac9c76b93a47b5659a18ae
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 struct files_struct *in_fsp,
33 uint8_t in_info_type,
34 uint8_t in_file_info_class,
35 uint32_t in_output_buffer_length,
36 DATA_BLOB in_input_buffer,
37 uint32_t in_additional_information,
38 uint32_t in_flags);
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 files_struct *in_fsp;
61 struct tevent_req *subreq;
63 status = smbd_smb2_request_verify_sizes(req, 0x29);
64 if (!NT_STATUS_IS_OK(status)) {
65 return smbd_smb2_request_error(req, status);
67 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
69 in_info_type = CVAL(inbody, 0x02);
70 in_file_info_class = CVAL(inbody, 0x03);
71 in_output_buffer_length = IVAL(inbody, 0x04);
72 in_input_buffer_offset = SVAL(inbody, 0x08);
73 /* 0x0A 2 bytes reserved */
74 in_input_buffer_length = IVAL(inbody, 0x0C);
75 in_additional_information = IVAL(inbody, 0x10);
76 in_flags = IVAL(inbody, 0x14);
77 in_file_id_persistent = BVAL(inbody, 0x18);
78 in_file_id_volatile = BVAL(inbody, 0x20);
80 if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
81 /* This is ok */
82 } else if (in_input_buffer_offset !=
83 (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
84 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
87 if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
88 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
91 in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
92 in_input_buffer.length = in_input_buffer_length;
94 if (in_input_buffer.length > req->sconn->smb2.max_trans) {
95 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
97 if (in_output_buffer_length > req->sconn->smb2.max_trans) {
98 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
101 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
102 if (in_fsp == NULL) {
103 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
106 subreq = smbd_smb2_getinfo_send(req, req->sconn->smb2.event_ctx,
107 req, in_fsp,
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 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 /* some GetInfo responses set STATUS_BUFFER_OVERFLOW and return partial,
152 but valid data */
153 if (!(NT_STATUS_IS_OK(call_status) ||
154 NT_STATUS_EQUAL(call_status, STATUS_BUFFER_OVERFLOW))) {
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_ex(req, call_status, outbody, &outdyn, __location__);
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 struct files_struct *fsp,
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)
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 NTSTATUS status;
252 req = tevent_req_create(mem_ctx, &state,
253 struct smbd_smb2_getinfo_state);
254 if (req == NULL) {
255 return NULL;
257 state->smb2req = smb2req;
258 state->status = NT_STATUS_OK;
259 state->out_output_buffer = data_blob_null;
261 DEBUG(10,("smbd_smb2_getinfo_send: %s - fnum[%d]\n",
262 fsp_str_dbg(fsp), fsp->fnum));
264 smbreq = smbd_smb2_fake_smb_request(smb2req);
265 if (tevent_req_nomem(smbreq, req)) {
266 return tevent_req_post(req, ev);
269 if (IS_IPC(conn)) {
270 smb2_ipc_getinfo(req, state, ev,
271 in_info_type, in_file_info_class);
272 return tevent_req_post(req, ev);
275 switch (in_info_type) {
276 case SMB2_GETINFO_FILE:
278 uint16_t file_info_level;
279 char *data = NULL;
280 unsigned int data_size = 0;
281 bool delete_pending = false;
282 struct timespec write_time_ts;
283 struct file_id fileid;
284 struct ea_list *ea_list = NULL;
285 int lock_data_count = 0;
286 char *lock_data = NULL;
288 ZERO_STRUCT(write_time_ts);
290 switch (in_file_info_class) {
291 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
292 file_info_level = 0xFF00 | in_file_info_class;
293 break;
295 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
296 file_info_level = 0xFF00 | in_file_info_class;
297 break;
299 default:
300 /* the levels directly map to the passthru levels */
301 file_info_level = in_file_info_class + 1000;
302 break;
305 if (fsp->fake_file_handle) {
307 * This is actually for the QUOTA_FAKE_FILE --metze
310 /* We know this name is ok, it's already passed the checks. */
312 } else if (fsp && fsp->fh->fd == -1) {
314 * This is actually a QFILEINFO on a directory
315 * handle (returned from an NT SMB). NT5.0 seems
316 * to do this call. JRA.
319 if (INFO_LEVEL_IS_UNIX(file_info_level)) {
320 /* Always do lstat for UNIX calls. */
321 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
322 DEBUG(3,("smbd_smb2_getinfo_send: "
323 "SMB_VFS_LSTAT of %s failed "
324 "(%s)\n", fsp_str_dbg(fsp),
325 strerror(errno)));
326 status = map_nt_error_from_unix(errno);
327 tevent_req_nterror(req, status);
328 return tevent_req_post(req, ev);
330 } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
331 DEBUG(3,("smbd_smb2_getinfo_send: "
332 "SMB_VFS_STAT of %s failed (%s)\n",
333 fsp_str_dbg(fsp),
334 strerror(errno)));
335 status = map_nt_error_from_unix(errno);
336 tevent_req_nterror(req, status);
337 return tevent_req_post(req, ev);
340 fileid = vfs_file_id_from_sbuf(conn,
341 &fsp->fsp_name->st);
342 get_file_infos(fileid, fsp->name_hash,
343 &delete_pending, &write_time_ts);
344 } else {
346 * Original code - this is an open file.
349 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
350 DEBUG(3, ("smbd_smb2_getinfo_send: "
351 "fstat of fnum %d failed (%s)\n",
352 fsp->fnum, strerror(errno)));
353 status = map_nt_error_from_unix(errno);
354 tevent_req_nterror(req, status);
355 return tevent_req_post(req, ev);
357 fileid = vfs_file_id_from_sbuf(conn,
358 &fsp->fsp_name->st);
359 get_file_infos(fileid, fsp->name_hash,
360 &delete_pending, &write_time_ts);
363 status = smbd_do_qfilepathinfo(conn, state,
364 file_info_level,
365 fsp,
366 fsp->fsp_name,
367 delete_pending,
368 write_time_ts,
369 ea_list,
370 lock_data_count,
371 lock_data,
372 STR_UNICODE,
373 in_output_buffer_length,
374 &data,
375 &data_size);
376 if (!NT_STATUS_IS_OK(status)) {
377 SAFE_FREE(data);
378 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
379 status = NT_STATUS_INVALID_INFO_CLASS;
381 tevent_req_nterror(req, status);
382 return tevent_req_post(req, ev);
384 if (data_size > 0) {
385 state->out_output_buffer = data_blob_talloc(state,
386 data,
387 data_size);
388 SAFE_FREE(data);
389 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
390 return tevent_req_post(req, ev);
393 SAFE_FREE(data);
394 break;
397 case SMB2_GETINFO_FS:
399 uint16_t file_info_level;
400 char *data = NULL;
401 int data_size = 0;
403 /* the levels directly map to the passthru levels */
404 file_info_level = in_file_info_class + 1000;
406 status = smbd_do_qfsinfo(conn, state,
407 file_info_level,
408 STR_UNICODE,
409 in_output_buffer_length,
410 fsp->fsp_name,
411 &data,
412 &data_size);
413 /* some responses set STATUS_BUFFER_OVERFLOW and return
414 partial, but valid data */
415 if (!(NT_STATUS_IS_OK(status) ||
416 NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW))) {
417 SAFE_FREE(data);
418 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
419 status = NT_STATUS_INVALID_INFO_CLASS;
421 tevent_req_nterror(req, status);
422 return tevent_req_post(req, ev);
424 if (data_size > 0) {
425 state->out_output_buffer = data_blob_talloc(state,
426 data,
427 data_size);
428 SAFE_FREE(data);
429 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
430 return tevent_req_post(req, ev);
433 SAFE_FREE(data);
434 break;
437 case SMB2_GETINFO_SECURITY:
439 uint8_t *p_marshalled_sd = NULL;
440 size_t sd_size = 0;
442 status = smbd_do_query_security_desc(conn,
443 state,
444 fsp,
445 /* Security info wanted. */
446 in_additional_information,
447 in_output_buffer_length,
448 &p_marshalled_sd,
449 &sd_size);
451 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
452 /* Return needed size. */
453 state->out_output_buffer = data_blob_talloc(state,
454 NULL,
456 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
457 return tevent_req_post(req, ev);
459 SIVAL(state->out_output_buffer.data,0,(uint32_t)sd_size);
460 state->status = NT_STATUS_BUFFER_TOO_SMALL;
461 break;
463 if (!NT_STATUS_IS_OK(status)) {
464 DEBUG(10,("smbd_smb2_getinfo_send: "
465 "smbd_do_query_security_desc of %s failed "
466 "(%s)\n", fsp_str_dbg(fsp),
467 nt_errstr(status)));
468 tevent_req_nterror(req, status);
469 return tevent_req_post(req, ev);
472 if (sd_size > 0) {
473 state->out_output_buffer = data_blob_talloc(state,
474 p_marshalled_sd,
475 sd_size);
476 if (tevent_req_nomem(state->out_output_buffer.data, req)) {
477 return tevent_req_post(req, ev);
480 break;
483 default:
484 DEBUG(10,("smbd_smb2_getinfo_send: "
485 "unknown in_info_type of %u "
486 " for file %s\n",
487 (unsigned int)in_info_type,
488 fsp_str_dbg(fsp) ));
490 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
491 return tevent_req_post(req, ev);
494 if (state->out_output_buffer.length > in_output_buffer_length) {
495 tevent_req_nterror(req, NT_STATUS_INFO_LENGTH_MISMATCH);
496 return tevent_req_post(req, ev);
499 state->status = status;
500 tevent_req_done(req);
501 return tevent_req_post(req, ev);
504 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
505 TALLOC_CTX *mem_ctx,
506 DATA_BLOB *out_output_buffer,
507 NTSTATUS *pstatus)
509 NTSTATUS status;
510 struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
511 struct smbd_smb2_getinfo_state);
513 if (tevent_req_is_nterror(req, &status)) {
514 tevent_req_received(req);
515 return status;
518 *out_output_buffer = state->out_output_buffer;
519 talloc_steal(mem_ctx, out_output_buffer->data);
520 *pstatus = state->status;
522 tevent_req_received(req);
523 return NT_STATUS_OK;