2 Unix SMB/CIFS implementation.
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/>.
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
27 #include "../lib/util/tevent_ntstatus.h"
28 #include "librpc/gen_ndr/ndr_quota.h"
29 #include "librpc/gen_ndr/ndr_security.h"
32 #define DBGC_CLASS DBGC_SMB2
34 static struct tevent_req
*smbd_smb2_getinfo_send(TALLOC_CTX
*mem_ctx
,
35 struct tevent_context
*ev
,
36 struct smbd_smb2_request
*smb2req
,
37 struct files_struct
*in_fsp
,
39 uint8_t in_file_info_class
,
40 uint32_t in_output_buffer_length
,
41 DATA_BLOB in_input_buffer
,
42 uint32_t in_additional_information
,
44 static NTSTATUS
smbd_smb2_getinfo_recv(struct tevent_req
*req
,
46 DATA_BLOB
*out_output_buffer
,
47 NTSTATUS
*p_call_status
);
49 static void smbd_smb2_request_getinfo_done(struct tevent_req
*subreq
);
50 NTSTATUS
smbd_smb2_request_process_getinfo(struct smbd_smb2_request
*req
)
52 struct smbXsrv_connection
*xconn
= req
->xconn
;
54 const uint8_t *inbody
;
56 uint8_t in_file_info_class
;
57 uint32_t in_output_buffer_length
;
58 uint16_t in_input_buffer_offset
;
59 uint32_t in_input_buffer_length
;
60 DATA_BLOB in_input_buffer
;
61 uint32_t in_additional_information
;
63 uint64_t in_file_id_persistent
;
64 uint64_t in_file_id_volatile
;
65 struct files_struct
*in_fsp
;
66 struct tevent_req
*subreq
;
68 status
= smbd_smb2_request_verify_sizes(req
, 0x29);
69 if (!NT_STATUS_IS_OK(status
)) {
70 return smbd_smb2_request_error(req
, status
);
72 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
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) {
87 } else if (in_input_buffer_offset
!=
88 (SMB2_HDR_BODY
+ SMBD_SMB2_IN_BODY_LEN(req
))) {
89 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
92 if (in_input_buffer_length
> SMBD_SMB2_IN_DYN_LEN(req
)) {
93 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
96 in_input_buffer
.data
= SMBD_SMB2_IN_DYN_PTR(req
);
97 in_input_buffer
.length
= in_input_buffer_length
;
99 if (in_input_buffer
.length
> xconn
->smb2
.server
.max_trans
) {
100 DEBUG(2,("smbd_smb2_request_process_getinfo: "
101 "client ignored max trans: %s: 0x%08X: 0x%08X\n",
102 __location__
, (unsigned)in_input_buffer
.length
,
103 (unsigned)xconn
->smb2
.server
.max_trans
));
104 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
106 if (in_output_buffer_length
> xconn
->smb2
.server
.max_trans
) {
107 DEBUG(2,("smbd_smb2_request_process_getinfo: "
108 "client ignored max trans: %s: 0x%08X: 0x%08X\n",
109 __location__
, in_output_buffer_length
,
110 xconn
->smb2
.server
.max_trans
));
111 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
114 status
= smbd_smb2_request_verify_creditcharge(req
,
115 MAX(in_input_buffer
.length
,in_output_buffer_length
));
116 if (!NT_STATUS_IS_OK(status
)) {
117 return smbd_smb2_request_error(req
, status
);
120 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
, in_file_id_volatile
);
121 if (in_fsp
== NULL
) {
122 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
125 subreq
= smbd_smb2_getinfo_send(req
, req
->ev_ctx
,
129 in_output_buffer_length
,
131 in_additional_information
,
133 if (subreq
== NULL
) {
134 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
136 tevent_req_set_callback(subreq
, smbd_smb2_request_getinfo_done
, req
);
138 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
141 static void smbd_smb2_request_getinfo_done(struct tevent_req
*subreq
)
143 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
144 struct smbd_smb2_request
);
147 uint16_t out_output_buffer_offset
;
148 DATA_BLOB out_output_buffer
= data_blob_null
;
150 NTSTATUS call_status
= NT_STATUS_OK
;
151 NTSTATUS error
; /* transport error */
153 status
= smbd_smb2_getinfo_recv(subreq
,
158 if (!NT_STATUS_IS_OK(status
)) {
159 error
= smbd_smb2_request_error(req
, status
);
160 if (!NT_STATUS_IS_OK(error
)) {
161 smbd_server_connection_terminate(req
->xconn
,
168 /* some GetInfo responses set STATUS_BUFFER_OVERFLOW and return partial,
170 if (!(NT_STATUS_IS_OK(call_status
) ||
171 NT_STATUS_EQUAL(call_status
, STATUS_BUFFER_OVERFLOW
))) {
172 /* Return a specific error with data. */
173 error
= smbd_smb2_request_error_ex(req
,
177 if (!NT_STATUS_IS_OK(error
)) {
178 smbd_server_connection_terminate(req
->xconn
,
185 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
187 outbody
= smbd_smb2_generate_outbody(req
, 0x08);
188 if (outbody
.data
== NULL
) {
189 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
190 if (!NT_STATUS_IS_OK(error
)) {
191 smbd_server_connection_terminate(req
->xconn
,
198 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
199 SSVAL(outbody
.data
, 0x02,
200 out_output_buffer_offset
); /* output buffer offset */
201 SIVAL(outbody
.data
, 0x04,
202 out_output_buffer
.length
); /* output buffer length */
204 outdyn
= out_output_buffer
;
206 error
= smbd_smb2_request_done_ex(req
, call_status
, outbody
, &outdyn
, __location__
);
207 if (!NT_STATUS_IS_OK(error
)) {
208 smbd_server_connection_terminate(req
->xconn
,
214 struct smbd_smb2_getinfo_state
{
215 struct smbd_smb2_request
*smb2req
;
217 DATA_BLOB out_output_buffer
;
220 static void smb2_ipc_getinfo(struct tevent_req
*req
,
221 struct smbd_smb2_getinfo_state
*state
,
222 struct tevent_context
*ev
,
223 uint8_t in_info_type
,
224 uint8_t in_file_info_class
)
226 /* We want to reply to SMB2_GETINFO_FILE
227 with a class of SMB2_FILE_STANDARD_INFO as
228 otherwise a Win7 client issues this request
229 twice (2xroundtrips) if we return NOT_SUPPORTED.
230 NB. We do the same for SMB1 in call_trans2qpipeinfo() */
232 if (in_info_type
== 0x01 && /* SMB2_GETINFO_FILE */
233 in_file_info_class
== 0x05) { /* SMB2_FILE_STANDARD_INFO */
234 state
->out_output_buffer
= data_blob_talloc(state
,
236 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
240 memset(state
->out_output_buffer
.data
,0,24);
241 SOFF_T(state
->out_output_buffer
.data
,0,4096LL);
242 SIVAL(state
->out_output_buffer
.data
,16,1);
243 SIVAL(state
->out_output_buffer
.data
,20,1);
244 tevent_req_done(req
);
246 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
250 static struct tevent_req
*smbd_smb2_getinfo_send(TALLOC_CTX
*mem_ctx
,
251 struct tevent_context
*ev
,
252 struct smbd_smb2_request
*smb2req
,
253 struct files_struct
*fsp
,
254 uint8_t in_info_type
,
255 uint8_t in_file_info_class
,
256 uint32_t in_output_buffer_length
,
257 DATA_BLOB in_input_buffer
,
258 uint32_t in_additional_information
,
261 struct tevent_req
*req
;
262 struct smbd_smb2_getinfo_state
*state
;
263 struct smb_request
*smbreq
;
264 connection_struct
*conn
= smb2req
->tcon
->compat
;
267 req
= tevent_req_create(mem_ctx
, &state
,
268 struct smbd_smb2_getinfo_state
);
272 state
->smb2req
= smb2req
;
273 state
->status
= NT_STATUS_OK
;
274 state
->out_output_buffer
= data_blob_null
;
276 DEBUG(10,("smbd_smb2_getinfo_send: %s - %s\n",
277 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
279 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
280 if (tevent_req_nomem(smbreq
, req
)) {
281 return tevent_req_post(req
, ev
);
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 SMB2_GETINFO_FILE
:
293 uint16_t file_info_level
;
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
;
302 size_t fixed_portion
;
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
;
311 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
312 file_info_level
= 0xFF00 | in_file_info_class
;
316 /* the levels directly map to the passthru levels */
317 file_info_level
= in_file_info_class
+ 1000;
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
->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
),
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",
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
,
358 get_file_infos(fileid
, fsp
->name_hash
,
359 &delete_pending
, &write_time_ts
);
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 %s failed (%s)\n",
368 fsp_fnum_dbg(fsp
), 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
,
375 get_file_infos(fileid
, fsp
->name_hash
,
376 &delete_pending
, &write_time_ts
);
379 status
= smbd_do_qfilepathinfo(conn
, state
,
389 in_output_buffer_length
,
393 if (!NT_STATUS_IS_OK(status
)) {
395 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_LEVEL
)) {
396 status
= NT_STATUS_INVALID_INFO_CLASS
;
398 tevent_req_nterror(req
, status
);
399 return tevent_req_post(req
, ev
);
401 if (in_output_buffer_length
< fixed_portion
) {
404 req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
405 return tevent_req_post(req
, ev
);
408 state
->out_output_buffer
= data_blob_talloc(state
,
412 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
413 return tevent_req_post(req
, ev
);
415 if (data_size
> in_output_buffer_length
) {
416 state
->out_output_buffer
.length
=
417 in_output_buffer_length
;
418 status
= STATUS_BUFFER_OVERFLOW
;
425 case SMB2_GETINFO_FS
:
427 uint16_t file_info_level
;
430 size_t fixed_portion
;
432 /* the levels directly map to the passthru levels */
433 file_info_level
= in_file_info_class
+ 1000;
435 status
= smbd_do_qfsinfo(smb2req
->xconn
, conn
, state
,
438 in_output_buffer_length
,
443 /* some responses set STATUS_BUFFER_OVERFLOW and return
444 partial, but valid data */
445 if (!(NT_STATUS_IS_OK(status
) ||
446 NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
))) {
448 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_LEVEL
)) {
449 status
= NT_STATUS_INVALID_INFO_CLASS
;
451 tevent_req_nterror(req
, status
);
452 return tevent_req_post(req
, ev
);
454 if (in_output_buffer_length
< fixed_portion
) {
457 req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
458 return tevent_req_post(req
, ev
);
461 state
->out_output_buffer
= data_blob_talloc(state
,
465 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
466 return tevent_req_post(req
, ev
);
468 if (data_size
> in_output_buffer_length
) {
469 state
->out_output_buffer
.length
=
470 in_output_buffer_length
;
471 status
= STATUS_BUFFER_OVERFLOW
;
478 case SMB2_GETINFO_SECURITY
:
480 uint8_t *p_marshalled_sd
= NULL
;
483 status
= smbd_do_query_security_desc(conn
,
486 /* Security info wanted. */
487 in_additional_information
&
488 SMB_SUPPORTED_SECINFO_FLAGS
,
489 in_output_buffer_length
,
493 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
494 /* Return needed size. */
495 state
->out_output_buffer
= data_blob_talloc(state
,
498 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
499 return tevent_req_post(req
, ev
);
501 SIVAL(state
->out_output_buffer
.data
,0,(uint32_t)sd_size
);
502 state
->status
= NT_STATUS_BUFFER_TOO_SMALL
;
505 if (!NT_STATUS_IS_OK(status
)) {
506 DEBUG(10,("smbd_smb2_getinfo_send: "
507 "smbd_do_query_security_desc of %s failed "
508 "(%s)\n", fsp_str_dbg(fsp
),
510 tevent_req_nterror(req
, status
);
511 return tevent_req_post(req
, ev
);
515 state
->out_output_buffer
= data_blob_talloc(state
,
518 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
519 return tevent_req_post(req
, ev
);
525 case SMB2_GETINFO_QUOTA
: {
526 #ifdef HAVE_SYS_QUOTAS
527 struct smb2_query_quota_info info
;
528 enum ndr_err_code err
;
529 uint8_t *data
= NULL
;
530 uint32_t data_size
= 0;
531 struct ndr_pull
*ndr_pull
= NULL
;
532 DATA_BLOB sid_buf
= data_blob_null
;
533 TALLOC_CTX
*tmp_ctx
= talloc_init("geninfo_quota");
536 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
537 return tevent_req_post(req
, ev
);
540 ndr_pull
= ndr_pull_init_blob(&in_input_buffer
, tmp_ctx
);
542 TALLOC_FREE(tmp_ctx
);
543 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
544 return tevent_req_post(req
, ev
);
547 err
= ndr_pull_smb2_query_quota_info(ndr_pull
,
548 NDR_SCALARS
| NDR_BUFFERS
,
551 if (!NDR_ERR_CODE_IS_SUCCESS(err
)) {
552 DBG_DEBUG("failed to pull smb2_query_quota_info\n");
553 TALLOC_FREE(tmp_ctx
);
554 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
555 return tevent_req_post(req
, ev
);
558 DBG_DEBUG("quota list returnsingle %u, restartscan %u, "
559 "sid_list_length %u, start_sid_length %u, "
560 "startsidoffset %u\n",
561 (unsigned int)info
.return_single
,
562 (unsigned int)info
.restart_scan
,
563 (unsigned int)info
.sid_list_length
,
564 (unsigned int)info
.start_sid_length
,
565 (unsigned int)info
.start_sid_offset
);
567 /* Currently we do not support the single start sid format */
568 if (info
.start_sid_length
!= 0 || info
.start_sid_offset
!= 0 ) {
569 DBG_INFO("illegal single sid query\n");
570 TALLOC_FREE(tmp_ctx
);
571 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
572 return tevent_req_post(req
, ev
);
575 if (in_input_buffer
.length
< ndr_pull
->offset
) {
576 DBG_INFO("Invalid buffer length\n");
577 TALLOC_FREE(tmp_ctx
);
578 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
579 return tevent_req_post(req
, ev
);
582 sid_buf
.data
= in_input_buffer
.data
+ ndr_pull
->offset
;
583 sid_buf
.length
= in_input_buffer
.length
- ndr_pull
->offset
;
585 status
= smbd_do_query_getinfo_quota(tmp_ctx
,
589 info
.sid_list_length
,
591 in_output_buffer_length
,
595 if (!NT_STATUS_IS_OK(status
)) {
596 TALLOC_FREE(tmp_ctx
);
597 tevent_req_nterror(req
, status
);
598 return tevent_req_post(req
, ev
);
601 state
->out_output_buffer
=
602 data_blob_talloc(state
, data
, data_size
);
603 status
= NT_STATUS_OK
;
604 TALLOC_FREE(tmp_ctx
);
607 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
608 return tevent_req_post(req
, ev
);
613 DEBUG(10,("smbd_smb2_getinfo_send: "
614 "unknown in_info_type of %u "
616 (unsigned int)in_info_type
,
619 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
620 return tevent_req_post(req
, ev
);
623 state
->status
= status
;
624 tevent_req_done(req
);
625 return tevent_req_post(req
, ev
);
628 static NTSTATUS
smbd_smb2_getinfo_recv(struct tevent_req
*req
,
630 DATA_BLOB
*out_output_buffer
,
634 struct smbd_smb2_getinfo_state
*state
= tevent_req_data(req
,
635 struct smbd_smb2_getinfo_state
);
637 if (tevent_req_is_nterror(req
, &status
)) {
638 tevent_req_received(req
);
642 *out_output_buffer
= state
->out_output_buffer
;
643 talloc_steal(mem_ctx
, out_output_buffer
->data
);
644 *pstatus
= state
->status
;
646 tevent_req_received(req
);