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
, fsp
);
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 size_t fixed_portion
;
302 ZERO_STRUCT(write_time_ts
);
305 * MS-SMB2 3.3.5.20.1 "Handling SMB2_0_INFO_FILE"
307 * FileBasicInformation, FileAllInformation,
308 * FileNetworkOpenInformation, FileAttributeTagInformation
309 * require FILE_READ_ATTRIBUTES.
311 * FileFullEaInformation requires FILE_READ_EA.
313 switch (in_file_info_class
) {
314 case FSCC_FILE_BASIC_INFORMATION
:
315 case FSCC_FILE_ALL_INFORMATION
:
316 case FSCC_FILE_NETWORK_OPEN_INFORMATION
:
317 case FSCC_FILE_ATTRIBUTE_TAG_INFORMATION
:
318 if (!(fsp
->access_mask
& SEC_FILE_READ_ATTRIBUTE
)) {
319 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
320 return tevent_req_post(req
, ev
);
324 case FSCC_FILE_FULL_EA_INFORMATION
:
325 if (!(fsp
->access_mask
& SEC_FILE_READ_EA
)) {
326 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
327 return tevent_req_post(req
, ev
);
332 switch (in_file_info_class
) {
333 case FSCC_FILE_FULL_EA_INFORMATION
:
334 file_info_level
= SMB2_FILE_FULL_EA_INFORMATION
;
337 case FSCC_FILE_ALL_INFORMATION
:
338 file_info_level
= SMB2_FILE_ALL_INFORMATION
;
341 case SMB2_FILE_POSIX_INFORMATION
:
342 if (!(fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
)) {
343 tevent_req_nterror(req
, NT_STATUS_INVALID_LEVEL
);
344 return tevent_req_post(req
, ev
);
346 file_info_level
= SMB2_FILE_POSIX_INFORMATION_INTERNAL
;
350 /* the levels directly map to the passthru levels */
351 file_info_level
= in_file_info_class
+ 1000;
355 switch (file_info_level
) {
356 case SMB_FILE_NORMALIZED_NAME_INFORMATION
:
357 if (smb2req
->xconn
->protocol
< PROTOCOL_SMB3_11
) {
358 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
359 return tevent_req_post(req
, ev
);
364 if (fsp
->fake_file_handle
) {
366 * This is actually for the QUOTA_FAKE_FILE --metze
369 /* We know this name is ok, it's already passed the checks. */
371 } else if (fsp_get_pathref_fd(fsp
) == -1) {
373 * This is actually a QFILEINFO on a directory
374 * handle (returned from an NT SMB). NT5.0 seems
375 * to do this call. JRA.
377 int ret
= vfs_stat(conn
, fsp
->fsp_name
);
379 DBG_NOTICE("vfs_stat of %s failed (%s)\n",
382 status
= map_nt_error_from_unix(errno
);
383 tevent_req_nterror(req
, status
);
384 return tevent_req_post(req
, ev
);
387 if (fsp_getinfo_ask_sharemode(fsp
)) {
388 fileid
= vfs_file_id_from_sbuf(
389 conn
, &fsp
->fsp_name
->st
);
390 get_file_infos(fileid
, fsp
->name_hash
,
396 * Original code - this is an open file.
399 status
= vfs_stat_fsp(fsp
);
400 if (!NT_STATUS_IS_OK(status
)) {
401 DEBUG(3, ("smbd_smb2_getinfo_send: "
402 "fstat of %s failed (%s)\n",
403 fsp_fnum_dbg(fsp
), nt_errstr(status
)));
404 tevent_req_nterror(req
, status
);
405 return tevent_req_post(req
, ev
);
407 if (fsp_getinfo_ask_sharemode(fsp
)) {
408 fileid
= vfs_file_id_from_sbuf(
409 conn
, &fsp
->fsp_name
->st
);
410 get_file_infos(fileid
, fsp
->name_hash
,
416 status
= smbd_do_qfilepathinfo(conn
, state
,
425 in_output_buffer_length
,
429 if (!NT_STATUS_IS_OK(status
)) {
431 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_LEVEL
)) {
432 status
= NT_STATUS_INVALID_INFO_CLASS
;
434 tevent_req_nterror(req
, status
);
435 return tevent_req_post(req
, ev
);
437 if (in_output_buffer_length
< fixed_portion
) {
440 req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
441 return tevent_req_post(req
, ev
);
444 state
->out_output_buffer
= data_blob_talloc(state
,
448 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
449 return tevent_req_post(req
, ev
);
451 if (data_size
> in_output_buffer_length
) {
452 state
->out_output_buffer
.length
=
453 in_output_buffer_length
;
454 status
= STATUS_BUFFER_OVERFLOW
;
461 case SMB2_0_INFO_FILESYSTEM
:
463 uint16_t file_info_level
;
466 size_t fixed_portion
;
468 /* the levels directly map to the passthru levels */
469 file_info_level
= in_file_info_class
+ 1000;
471 status
= smbd_do_qfsinfo(smb2req
->xconn
, conn
, state
,
474 in_output_buffer_length
,
479 /* some responses set STATUS_BUFFER_OVERFLOW and return
480 partial, but valid data */
481 if (!(NT_STATUS_IS_OK(status
) ||
482 NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
))) {
484 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_LEVEL
)) {
485 status
= NT_STATUS_INVALID_INFO_CLASS
;
487 tevent_req_nterror(req
, status
);
488 return tevent_req_post(req
, ev
);
490 if (in_output_buffer_length
< fixed_portion
) {
493 req
, NT_STATUS_INFO_LENGTH_MISMATCH
);
494 return tevent_req_post(req
, ev
);
497 state
->out_output_buffer
= data_blob_talloc(state
,
501 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
502 return tevent_req_post(req
, ev
);
504 if (data_size
> in_output_buffer_length
) {
505 state
->out_output_buffer
.length
=
506 in_output_buffer_length
;
507 status
= STATUS_BUFFER_OVERFLOW
;
514 case SMB2_0_INFO_SECURITY
:
516 uint8_t *p_marshalled_sd
= NULL
;
519 status
= smbd_do_query_security_desc(conn
,
522 /* Security info wanted. */
523 in_additional_information
&
524 SMB_SUPPORTED_SECINFO_FLAGS
,
525 in_output_buffer_length
,
529 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
530 /* Return needed size. */
531 state
->out_output_buffer
= data_blob_talloc(state
,
534 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
535 return tevent_req_post(req
, ev
);
537 SIVAL(state
->out_output_buffer
.data
,0,(uint32_t)sd_size
);
538 state
->status
= NT_STATUS_BUFFER_TOO_SMALL
;
541 if (!NT_STATUS_IS_OK(status
)) {
542 DEBUG(10,("smbd_smb2_getinfo_send: "
543 "smbd_do_query_security_desc of %s failed "
544 "(%s)\n", fsp_str_dbg(fsp
),
546 tevent_req_nterror(req
, status
);
547 return tevent_req_post(req
, ev
);
551 state
->out_output_buffer
= data_blob_talloc(state
,
554 if (tevent_req_nomem(state
->out_output_buffer
.data
, req
)) {
555 return tevent_req_post(req
, ev
);
561 case SMB2_0_INFO_QUOTA
: {
562 #ifdef HAVE_SYS_QUOTAS
563 struct smb2_query_quota_info info
;
564 enum ndr_err_code err
;
565 uint8_t *data
= NULL
;
566 uint32_t data_size
= 0;
567 struct ndr_pull
*ndr_pull
= NULL
;
568 DATA_BLOB sid_buf
= data_blob_null
;
569 TALLOC_CTX
*tmp_ctx
= talloc_init("geninfo_quota");
573 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
574 return tevent_req_post(req
, ev
);
577 ok
= check_fsp_ntquota_handle(conn
, smbreq
, fsp
);
579 DBG_INFO("no valid QUOTA HANDLE\n");
580 TALLOC_FREE(tmp_ctx
);
581 tevent_req_nterror(req
, NT_STATUS_INVALID_HANDLE
);
582 return tevent_req_post(req
, ev
);
585 ndr_pull
= ndr_pull_init_blob(&in_input_buffer
, tmp_ctx
);
587 TALLOC_FREE(tmp_ctx
);
588 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
589 return tevent_req_post(req
, ev
);
592 err
= ndr_pull_smb2_query_quota_info(ndr_pull
,
593 NDR_SCALARS
| NDR_BUFFERS
,
596 if (!NDR_ERR_CODE_IS_SUCCESS(err
)) {
597 DBG_DEBUG("failed to pull smb2_query_quota_info\n");
598 TALLOC_FREE(tmp_ctx
);
599 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
600 return tevent_req_post(req
, ev
);
603 DBG_DEBUG("quota list returnsingle %u, restartscan %u, "
604 "sid_list_length %u, start_sid_length %u, "
605 "startsidoffset %u\n",
606 (unsigned int)info
.return_single
,
607 (unsigned int)info
.restart_scan
,
608 (unsigned int)info
.sid_list_length
,
609 (unsigned int)info
.start_sid_length
,
610 (unsigned int)info
.start_sid_offset
);
612 /* Currently we do not support the single start sid format */
613 if (info
.start_sid_length
!= 0 || info
.start_sid_offset
!= 0 ) {
614 DBG_INFO("illegal single sid query\n");
615 TALLOC_FREE(tmp_ctx
);
616 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
617 return tevent_req_post(req
, ev
);
620 if (in_input_buffer
.length
< ndr_pull
->offset
) {
621 DBG_INFO("Invalid buffer length\n");
622 TALLOC_FREE(tmp_ctx
);
623 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
624 return tevent_req_post(req
, ev
);
627 sid_buf
.data
= in_input_buffer
.data
+ ndr_pull
->offset
;
628 sid_buf
.length
= in_input_buffer
.length
- ndr_pull
->offset
;
630 status
= smbd_do_query_getinfo_quota(tmp_ctx
,
634 info
.sid_list_length
,
636 in_output_buffer_length
,
640 if (!NT_STATUS_IS_OK(status
)) {
641 TALLOC_FREE(tmp_ctx
);
642 tevent_req_nterror(req
, status
);
643 return tevent_req_post(req
, ev
);
646 state
->out_output_buffer
=
647 data_blob_talloc(state
, data
, data_size
);
648 status
= NT_STATUS_OK
;
649 TALLOC_FREE(tmp_ctx
);
652 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
653 return tevent_req_post(req
, ev
);
658 DEBUG(10,("smbd_smb2_getinfo_send: "
659 "unknown in_info_type of %u "
661 (unsigned int)in_info_type
,
664 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
665 return tevent_req_post(req
, ev
);
668 state
->status
= status
;
669 tevent_req_done(req
);
670 return tevent_req_post(req
, ev
);
673 static NTSTATUS
smbd_smb2_getinfo_recv(struct tevent_req
*req
,
675 DATA_BLOB
*out_output_buffer
,
679 struct smbd_smb2_getinfo_state
*state
= tevent_req_data(req
,
680 struct smbd_smb2_getinfo_state
);
682 if (tevent_req_is_nterror(req
, &status
)) {
683 tevent_req_received(req
);
687 *out_output_buffer
= state
->out_output_buffer
;
688 talloc_steal(mem_ctx
, out_output_buffer
->data
);
689 *pstatus
= state
->status
;
691 tevent_req_received(req
);