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
->sconn
->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
,
178 if (!NT_STATUS_IS_OK(error
)) {
179 smbd_server_connection_terminate(req
->xconn
,
186 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
188 outbody
= smbd_smb2_generate_outbody(req
, 0x08);
189 if (outbody
.data
== NULL
) {
190 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
191 if (!NT_STATUS_IS_OK(error
)) {
192 smbd_server_connection_terminate(req
->xconn
,
199 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
200 SSVAL(outbody
.data
, 0x02,
201 out_output_buffer_offset
); /* output buffer offset */
202 SIVAL(outbody
.data
, 0x04,
203 out_output_buffer
.length
); /* output buffer length */
205 outdyn
= out_output_buffer
;
207 error
= smbd_smb2_request_done_ex(req
, call_status
, outbody
, &outdyn
, __location__
);
208 if (!NT_STATUS_IS_OK(error
)) {
209 smbd_server_connection_terminate(req
->xconn
,
215 struct smbd_smb2_getinfo_state
{
216 struct smbd_smb2_request
*smb2req
;
218 DATA_BLOB out_output_buffer
;
221 static void smb2_ipc_getinfo(struct tevent_req
*req
,
222 struct smbd_smb2_getinfo_state
*state
,
223 struct tevent_context
*ev
,
224 uint8_t in_info_type
,
225 uint8_t in_file_info_class
)
227 /* We want to reply to SMB2_GETINFO_FILE
228 with a class of SMB2_FILE_STANDARD_INFO as
229 otherwise a Win7 client issues this request
230 twice (2xroundtrips) if we return NOT_SUPPORTED.
231 NB. We do the same for SMB1 in call_trans2qpipeinfo() */
233 if (in_info_type
== 0x01 && /* SMB2_GETINFO_FILE */
234 in_file_info_class
== 0x05) { /* SMB2_FILE_STANDARD_INFO */
235 state
->out_output_buffer
= data_blob_talloc(state
,
237 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
241 memset(state
->out_output_buffer
.data
,0,24);
242 SOFF_T(state
->out_output_buffer
.data
,0,4096LL);
243 SIVAL(state
->out_output_buffer
.data
,16,1);
244 SIVAL(state
->out_output_buffer
.data
,20,1);
245 tevent_req_done(req
);
247 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
251 static struct tevent_req
*smbd_smb2_getinfo_send(TALLOC_CTX
*mem_ctx
,
252 struct tevent_context
*ev
,
253 struct smbd_smb2_request
*smb2req
,
254 struct files_struct
*fsp
,
255 uint8_t in_info_type
,
256 uint8_t in_file_info_class
,
257 uint32_t in_output_buffer_length
,
258 DATA_BLOB in_input_buffer
,
259 uint32_t in_additional_information
,
262 struct tevent_req
*req
;
263 struct smbd_smb2_getinfo_state
*state
;
264 struct smb_request
*smbreq
;
265 connection_struct
*conn
= smb2req
->tcon
->compat
;
268 req
= tevent_req_create(mem_ctx
, &state
,
269 struct smbd_smb2_getinfo_state
);
273 state
->smb2req
= smb2req
;
274 state
->status
= NT_STATUS_OK
;
275 state
->out_output_buffer
= data_blob_null
;
277 DEBUG(10,("smbd_smb2_getinfo_send: %s - %s\n",
278 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
280 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
281 if (tevent_req_nomem(smbreq
, req
)) {
282 return tevent_req_post(req
, ev
);
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 SMB2_0_INFO_FILE
:
294 uint16_t file_info_level
;
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
;
303 size_t fixed_portion
;
305 ZERO_STRUCT(write_time_ts
);
308 * MS-SMB2 3.3.5.20.1 "Handling SMB2_0_INFO_FILE"
310 * FileBasicInformation, FileAllInformation,
311 * FileNetworkOpenInformation, FileAttributeTagInformation
312 * require FILE_READ_ATTRIBUTES.
314 * FileFullEaInformation requires FILE_READ_EA.
316 switch (in_file_info_class
) {
317 case FSCC_FILE_BASIC_INFORMATION
:
318 case FSCC_FILE_ALL_INFORMATION
:
319 case FSCC_FILE_NETWORK_OPEN_INFORMATION
:
320 case FSCC_FILE_ATTRIBUTE_TAG_INFORMATION
:
321 if (!(fsp
->access_mask
& SEC_FILE_READ_ATTRIBUTE
)) {
322 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
323 return tevent_req_post(req
, ev
);
327 case FSCC_FILE_FULL_EA_INFORMATION
:
328 if (!(fsp
->access_mask
& SEC_FILE_READ_EA
)) {
329 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
330 return tevent_req_post(req
, ev
);
335 switch (in_file_info_class
) {
336 case FSCC_FILE_FULL_EA_INFORMATION
:
337 file_info_level
= SMB2_FILE_FULL_EA_INFORMATION
;
340 case FSCC_FILE_ALL_INFORMATION
:
341 file_info_level
= SMB2_FILE_ALL_INFORMATION
;
344 case SMB2_FILE_POSIX_INFORMATION
:
345 if (!(fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
)) {
346 tevent_req_nterror(req
, NT_STATUS_INVALID_LEVEL
);
347 return tevent_req_post(req
, ev
);
349 file_info_level
= SMB2_FILE_POSIX_INFORMATION_INTERNAL
;
353 /* the levels directly map to the passthru levels */
354 file_info_level
= in_file_info_class
+ 1000;
358 switch (file_info_level
) {
359 case SMB_FILE_NORMALIZED_NAME_INFORMATION
:
360 if (smb2req
->xconn
->protocol
< PROTOCOL_SMB3_11
) {
361 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
362 return tevent_req_post(req
, ev
);
367 if (fsp
->fake_file_handle
) {
369 * This is actually for the QUOTA_FAKE_FILE --metze
372 /* We know this name is ok, it's already passed the checks. */
374 } else if (fsp_get_pathref_fd(fsp
) == -1) {
376 * This is actually a QFILEINFO on a directory
377 * handle (returned from an NT SMB). NT5.0 seems
378 * to do this call. JRA.
380 int ret
= vfs_stat(conn
, fsp
->fsp_name
);
382 DBG_NOTICE("vfs_stat of %s failed (%s)\n",
385 status
= map_nt_error_from_unix(errno
);
386 tevent_req_nterror(req
, status
);
387 return tevent_req_post(req
, ev
);
390 if (fsp_getinfo_ask_sharemode(fsp
)) {
391 fileid
= vfs_file_id_from_sbuf(
392 conn
, &fsp
->fsp_name
->st
);
393 get_file_infos(fileid
, fsp
->name_hash
,
399 * Original code - this is an open file.
402 status
= vfs_stat_fsp(fsp
);
403 if (!NT_STATUS_IS_OK(status
)) {
404 DEBUG(3, ("smbd_smb2_getinfo_send: "
405 "fstat of %s failed (%s)\n",
406 fsp_fnum_dbg(fsp
), nt_errstr(status
)));
407 tevent_req_nterror(req
, status
);
408 return tevent_req_post(req
, ev
);
410 if (fsp_getinfo_ask_sharemode(fsp
)) {
411 fileid
= vfs_file_id_from_sbuf(
412 conn
, &fsp
->fsp_name
->st
);
413 get_file_infos(fileid
, fsp
->name_hash
,
419 status
= smbd_do_qfilepathinfo(conn
, state
,
430 in_output_buffer_length
,
434 if (!NT_STATUS_IS_OK(status
)) {
436 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_LEVEL
)) {
437 status
= NT_STATUS_INVALID_INFO_CLASS
;
439 tevent_req_nterror(req
, status
);
440 return tevent_req_post(req
, ev
);
442 if (in_output_buffer_length
< fixed_portion
) {
445 req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
446 return tevent_req_post(req
, ev
);
449 state
->out_output_buffer
= data_blob_talloc(state
,
453 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
454 return tevent_req_post(req
, ev
);
456 if (data_size
> in_output_buffer_length
) {
457 state
->out_output_buffer
.length
=
458 in_output_buffer_length
;
459 status
= STATUS_BUFFER_OVERFLOW
;
466 case SMB2_0_INFO_FILESYSTEM
:
468 uint16_t file_info_level
;
471 size_t fixed_portion
;
473 /* the levels directly map to the passthru levels */
474 file_info_level
= in_file_info_class
+ 1000;
476 status
= smbd_do_qfsinfo(smb2req
->xconn
, conn
, state
,
479 in_output_buffer_length
,
484 /* some responses set STATUS_BUFFER_OVERFLOW and return
485 partial, but valid data */
486 if (!(NT_STATUS_IS_OK(status
) ||
487 NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
))) {
489 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_LEVEL
)) {
490 status
= NT_STATUS_INVALID_INFO_CLASS
;
492 tevent_req_nterror(req
, status
);
493 return tevent_req_post(req
, ev
);
495 if (in_output_buffer_length
< fixed_portion
) {
498 req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
499 return tevent_req_post(req
, ev
);
502 state
->out_output_buffer
= data_blob_talloc(state
,
506 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
507 return tevent_req_post(req
, ev
);
509 if (data_size
> in_output_buffer_length
) {
510 state
->out_output_buffer
.length
=
511 in_output_buffer_length
;
512 status
= STATUS_BUFFER_OVERFLOW
;
519 case SMB2_0_INFO_SECURITY
:
521 uint8_t *p_marshalled_sd
= NULL
;
524 status
= smbd_do_query_security_desc(conn
,
527 /* Security info wanted. */
528 in_additional_information
&
529 SMB_SUPPORTED_SECINFO_FLAGS
,
530 in_output_buffer_length
,
534 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
535 /* Return needed size. */
536 state
->out_output_buffer
= data_blob_talloc(state
,
539 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
540 return tevent_req_post(req
, ev
);
542 SIVAL(state
->out_output_buffer
.data
,0,(uint32_t)sd_size
);
543 state
->status
= NT_STATUS_BUFFER_TOO_SMALL
;
546 if (!NT_STATUS_IS_OK(status
)) {
547 DEBUG(10,("smbd_smb2_getinfo_send: "
548 "smbd_do_query_security_desc of %s failed "
549 "(%s)\n", fsp_str_dbg(fsp
),
551 tevent_req_nterror(req
, status
);
552 return tevent_req_post(req
, ev
);
556 state
->out_output_buffer
= data_blob_talloc(state
,
559 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
560 return tevent_req_post(req
, ev
);
566 case SMB2_0_INFO_QUOTA
: {
567 #ifdef HAVE_SYS_QUOTAS
568 struct smb2_query_quota_info info
;
569 enum ndr_err_code err
;
570 uint8_t *data
= NULL
;
571 uint32_t data_size
= 0;
572 struct ndr_pull
*ndr_pull
= NULL
;
573 DATA_BLOB sid_buf
= data_blob_null
;
574 TALLOC_CTX
*tmp_ctx
= talloc_init("geninfo_quota");
578 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
579 return tevent_req_post(req
, ev
);
582 ok
= check_fsp_ntquota_handle(conn
, smbreq
, fsp
);
584 DBG_INFO("no valid QUOTA HANDLE\n");
585 TALLOC_FREE(tmp_ctx
);
586 tevent_req_nterror(req
, NT_STATUS_INVALID_HANDLE
);
587 return tevent_req_post(req
, ev
);
590 ndr_pull
= ndr_pull_init_blob(&in_input_buffer
, tmp_ctx
);
592 TALLOC_FREE(tmp_ctx
);
593 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
594 return tevent_req_post(req
, ev
);
597 err
= ndr_pull_smb2_query_quota_info(ndr_pull
,
598 NDR_SCALARS
| NDR_BUFFERS
,
601 if (!NDR_ERR_CODE_IS_SUCCESS(err
)) {
602 DBG_DEBUG("failed to pull smb2_query_quota_info\n");
603 TALLOC_FREE(tmp_ctx
);
604 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
605 return tevent_req_post(req
, ev
);
608 DBG_DEBUG("quota list returnsingle %u, restartscan %u, "
609 "sid_list_length %u, start_sid_length %u, "
610 "startsidoffset %u\n",
611 (unsigned int)info
.return_single
,
612 (unsigned int)info
.restart_scan
,
613 (unsigned int)info
.sid_list_length
,
614 (unsigned int)info
.start_sid_length
,
615 (unsigned int)info
.start_sid_offset
);
617 /* Currently we do not support the single start sid format */
618 if (info
.start_sid_length
!= 0 || info
.start_sid_offset
!= 0 ) {
619 DBG_INFO("illegal single sid query\n");
620 TALLOC_FREE(tmp_ctx
);
621 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
622 return tevent_req_post(req
, ev
);
625 if (in_input_buffer
.length
< ndr_pull
->offset
) {
626 DBG_INFO("Invalid buffer length\n");
627 TALLOC_FREE(tmp_ctx
);
628 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
629 return tevent_req_post(req
, ev
);
632 sid_buf
.data
= in_input_buffer
.data
+ ndr_pull
->offset
;
633 sid_buf
.length
= in_input_buffer
.length
- ndr_pull
->offset
;
635 status
= smbd_do_query_getinfo_quota(tmp_ctx
,
639 info
.sid_list_length
,
641 in_output_buffer_length
,
645 if (!NT_STATUS_IS_OK(status
)) {
646 TALLOC_FREE(tmp_ctx
);
647 tevent_req_nterror(req
, status
);
648 return tevent_req_post(req
, ev
);
651 state
->out_output_buffer
=
652 data_blob_talloc(state
, data
, data_size
);
653 status
= NT_STATUS_OK
;
654 TALLOC_FREE(tmp_ctx
);
657 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
658 return tevent_req_post(req
, ev
);
663 DEBUG(10,("smbd_smb2_getinfo_send: "
664 "unknown in_info_type of %u "
666 (unsigned int)in_info_type
,
669 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
670 return tevent_req_post(req
, ev
);
673 state
->status
= status
;
674 tevent_req_done(req
);
675 return tevent_req_post(req
, ev
);
678 static NTSTATUS
smbd_smb2_getinfo_recv(struct tevent_req
*req
,
680 DATA_BLOB
*out_output_buffer
,
684 struct smbd_smb2_getinfo_state
*state
= tevent_req_data(req
,
685 struct smbd_smb2_getinfo_state
);
687 if (tevent_req_is_nterror(req
, &status
)) {
688 tevent_req_received(req
);
692 *out_output_buffer
= state
->out_output_buffer
;
693 talloc_steal(mem_ctx
, out_output_buffer
->data
);
694 *pstatus
= state
->status
;
696 tevent_req_received(req
);