2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan (metze) Metzmacher 2003
5 Copyright (C) Jeremy Allison 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "../libcli/auth/spnego.h"
24 /****************************************************************************
25 Get UNIX extensions version info.
26 ****************************************************************************/
28 struct cli_unix_extensions_version_state
{
31 uint16_t major
, minor
;
32 uint32_t caplow
, caphigh
;
35 static void cli_unix_extensions_version_done(struct tevent_req
*subreq
);
37 struct tevent_req
*cli_unix_extensions_version_send(TALLOC_CTX
*mem_ctx
,
38 struct tevent_context
*ev
,
39 struct cli_state
*cli
)
41 struct tevent_req
*req
, *subreq
;
42 struct cli_unix_extensions_version_state
*state
;
44 req
= tevent_req_create(mem_ctx
, &state
,
45 struct cli_unix_extensions_version_state
);
49 SSVAL(state
->setup
, 0, TRANSACT2_QFSINFO
);
50 SSVAL(state
->param
, 0, SMB_QUERY_CIFS_UNIX_INFO
);
52 subreq
= cli_trans_send(state
, ev
, cli
, SMBtrans2
,
57 if (tevent_req_nomem(subreq
, req
)) {
58 return tevent_req_post(req
, ev
);
60 tevent_req_set_callback(subreq
, cli_unix_extensions_version_done
, req
);
64 static void cli_unix_extensions_version_done(struct tevent_req
*subreq
)
66 struct tevent_req
*req
= tevent_req_callback_data(
67 subreq
, struct tevent_req
);
68 struct cli_unix_extensions_version_state
*state
= tevent_req_data(
69 req
, struct cli_unix_extensions_version_state
);
74 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
,
77 if (!NT_STATUS_IS_OK(status
)) {
78 tevent_req_nterror(req
, status
);
82 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
86 state
->major
= SVAL(data
, 0);
87 state
->minor
= SVAL(data
, 2);
88 state
->caplow
= IVAL(data
, 4);
89 state
->caphigh
= IVAL(data
, 8);
94 NTSTATUS
cli_unix_extensions_version_recv(struct tevent_req
*req
,
95 uint16_t *pmajor
, uint16_t *pminor
,
99 struct cli_unix_extensions_version_state
*state
= tevent_req_data(
100 req
, struct cli_unix_extensions_version_state
);
103 if (tevent_req_is_nterror(req
, &status
)) {
106 *pmajor
= state
->major
;
107 *pminor
= state
->minor
;
108 *pcaplow
= state
->caplow
;
109 *pcaphigh
= state
->caphigh
;
113 NTSTATUS
cli_unix_extensions_version(struct cli_state
*cli
, uint16
*pmajor
,
114 uint16
*pminor
, uint32
*pcaplow
,
117 TALLOC_CTX
*frame
= talloc_stackframe();
118 struct event_context
*ev
;
119 struct tevent_req
*req
;
120 NTSTATUS status
= NT_STATUS_OK
;
122 if (cli_has_async_calls(cli
)) {
124 * Can't use sync call while an async call is in flight
126 status
= NT_STATUS_INVALID_PARAMETER
;
130 ev
= event_context_init(frame
);
132 status
= NT_STATUS_NO_MEMORY
;
136 req
= cli_unix_extensions_version_send(frame
, ev
, cli
);
138 status
= NT_STATUS_NO_MEMORY
;
142 if (!tevent_req_poll(req
, ev
)) {
143 status
= map_nt_error_from_unix(errno
);
147 status
= cli_unix_extensions_version_recv(req
, pmajor
, pminor
, pcaplow
,
149 if (NT_STATUS_IS_OK(status
)) {
150 cli
->posix_capabilities
= *pcaplow
;
154 if (!NT_STATUS_IS_OK(status
)) {
155 cli_set_error(cli
, status
);
160 /****************************************************************************
161 Set UNIX extensions capabilities.
162 ****************************************************************************/
164 bool cli_set_unix_extensions_capabilities(struct cli_state
*cli
, uint16 major
, uint16 minor
,
165 uint32 caplow
, uint32 caphigh
)
171 char *rparam
=NULL
, *rdata
=NULL
;
172 unsigned int rparam_count
=0, rdata_count
=0;
174 setup
= TRANSACT2_SETFSINFO
;
177 SSVAL(param
,2,SMB_SET_CIFS_UNIX_INFO
);
181 SIVAL(data
,4,caplow
);
182 SIVAL(data
,8,caphigh
);
184 if (!cli_send_trans(cli
, SMBtrans2
,
193 if (!cli_receive_trans(cli
, SMBtrans2
,
194 &rparam
, &rparam_count
,
195 &rdata
, &rdata_count
)) {
199 if (cli_is_error(cli
)) {
213 bool cli_get_fs_attr_info(struct cli_state
*cli
, uint32
*fs_attr
)
218 char *rparam
=NULL
, *rdata
=NULL
;
219 unsigned int rparam_count
=0, rdata_count
=0;
222 smb_panic("cli_get_fs_attr_info() called with NULL Pionter!");
224 setup
= TRANSACT2_QFSINFO
;
226 SSVAL(param
,0,SMB_QUERY_FS_ATTRIBUTE_INFO
);
228 if (!cli_send_trans(cli
, SMBtrans2
,
237 if (!cli_receive_trans(cli
, SMBtrans2
,
238 &rparam
, &rparam_count
,
239 &rdata
, &rdata_count
)) {
243 if (cli_is_error(cli
)) {
250 if (rdata_count
< 12) {
254 *fs_attr
= IVAL(rdata
,0);
256 /* todo: but not yet needed
257 * return the other stuff
267 bool cli_get_fs_volume_info_old(struct cli_state
*cli
, fstring volume_name
, uint32
*pserial_number
)
272 char *rparam
=NULL
, *rdata
=NULL
;
273 unsigned int rparam_count
=0, rdata_count
=0;
276 setup
= TRANSACT2_QFSINFO
;
278 SSVAL(param
,0,SMB_INFO_VOLUME
);
280 if (!cli_send_trans(cli
, SMBtrans2
,
289 if (!cli_receive_trans(cli
, SMBtrans2
,
290 &rparam
, &rparam_count
,
291 &rdata
, &rdata_count
)) {
295 if (cli_is_error(cli
)) {
302 if (rdata_count
< 5) {
306 if (pserial_number
) {
307 *pserial_number
= IVAL(rdata
,0);
309 nlen
= CVAL(rdata
,l2_vol_cch
);
310 clistr_pull(cli
->inbuf
, volume_name
, rdata
+ l2_vol_szVolLabel
,
311 sizeof(fstring
), nlen
, STR_NOALIGN
);
313 /* todo: but not yet needed
314 * return the other stuff
324 bool cli_get_fs_volume_info(struct cli_state
*cli
, fstring volume_name
, uint32
*pserial_number
, time_t *pdate
)
329 char *rparam
=NULL
, *rdata
=NULL
;
330 unsigned int rparam_count
=0, rdata_count
=0;
333 setup
= TRANSACT2_QFSINFO
;
335 SSVAL(param
,0,SMB_QUERY_FS_VOLUME_INFO
);
337 if (!cli_send_trans(cli
, SMBtrans2
,
346 if (!cli_receive_trans(cli
, SMBtrans2
,
347 &rparam
, &rparam_count
,
348 &rdata
, &rdata_count
)) {
352 if (cli_is_error(cli
)) {
359 if (rdata_count
< 19) {
365 ts
= interpret_long_date(rdata
);
368 if (pserial_number
) {
369 *pserial_number
= IVAL(rdata
,8);
371 nlen
= IVAL(rdata
,12);
372 clistr_pull(cli
->inbuf
, volume_name
, rdata
+ 18, sizeof(fstring
),
375 /* todo: but not yet needed
376 * return the other stuff
386 bool cli_get_fs_full_size_info(struct cli_state
*cli
,
387 uint64_t *total_allocation_units
,
388 uint64_t *caller_allocation_units
,
389 uint64_t *actual_allocation_units
,
390 uint64_t *sectors_per_allocation_unit
,
391 uint64_t *bytes_per_sector
)
396 char *rparam
=NULL
, *rdata
=NULL
;
397 unsigned int rparam_count
=0, rdata_count
=0;
399 setup
= TRANSACT2_QFSINFO
;
401 SSVAL(param
,0,SMB_FS_FULL_SIZE_INFORMATION
);
403 if (!cli_send_trans(cli
, SMBtrans2
,
412 if (!cli_receive_trans(cli
, SMBtrans2
,
413 &rparam
, &rparam_count
,
414 &rdata
, &rdata_count
)) {
418 if (cli_is_error(cli
)) {
425 if (rdata_count
!= 32) {
429 if (total_allocation_units
) {
430 *total_allocation_units
= BIG_UINT(rdata
, 0);
432 if (caller_allocation_units
) {
433 *caller_allocation_units
= BIG_UINT(rdata
,8);
435 if (actual_allocation_units
) {
436 *actual_allocation_units
= BIG_UINT(rdata
,16);
438 if (sectors_per_allocation_unit
) {
439 *sectors_per_allocation_unit
= IVAL(rdata
,24);
441 if (bytes_per_sector
) {
442 *bytes_per_sector
= IVAL(rdata
,28);
452 bool cli_get_posix_fs_info(struct cli_state
*cli
,
453 uint32
*optimal_transfer_size
,
455 uint64_t *total_blocks
,
456 uint64_t *blocks_available
,
457 uint64_t *user_blocks_available
,
458 uint64_t *total_file_nodes
,
459 uint64_t *free_file_nodes
,
460 uint64_t *fs_identifier
)
465 char *rparam
=NULL
, *rdata
=NULL
;
466 unsigned int rparam_count
=0, rdata_count
=0;
468 setup
= TRANSACT2_QFSINFO
;
470 SSVAL(param
,0,SMB_QUERY_POSIX_FS_INFO
);
472 if (!cli_send_trans(cli
, SMBtrans2
,
481 if (!cli_receive_trans(cli
, SMBtrans2
,
482 &rparam
, &rparam_count
,
483 &rdata
, &rdata_count
)) {
487 if (cli_is_error(cli
)) {
494 if (rdata_count
!= 56) {
498 if (optimal_transfer_size
) {
499 *optimal_transfer_size
= IVAL(rdata
, 0);
502 *block_size
= IVAL(rdata
,4);
505 *total_blocks
= BIG_UINT(rdata
,8);
507 if (blocks_available
) {
508 *blocks_available
= BIG_UINT(rdata
,16);
510 if (user_blocks_available
) {
511 *user_blocks_available
= BIG_UINT(rdata
,24);
513 if (total_file_nodes
) {
514 *total_file_nodes
= BIG_UINT(rdata
,32);
516 if (free_file_nodes
) {
517 *free_file_nodes
= BIG_UINT(rdata
,40);
520 *fs_identifier
= BIG_UINT(rdata
,48);
531 /******************************************************************************
532 Send/receive the request encryption blob.
533 ******************************************************************************/
535 static NTSTATUS
enc_blob_send_receive(struct cli_state
*cli
, DATA_BLOB
*in
, DATA_BLOB
*out
, DATA_BLOB
*param_out
)
539 char *rparam
=NULL
, *rdata
=NULL
;
540 unsigned int rparam_count
=0, rdata_count
=0;
541 NTSTATUS status
= NT_STATUS_OK
;
543 setup
= TRANSACT2_SETFSINFO
;
546 SSVAL(param
,2,SMB_REQUEST_TRANSPORT_ENCRYPTION
);
548 if (!cli_send_trans(cli
, SMBtrans2
,
553 (char *)in
->data
, in
->length
, CLI_BUFFER_SIZE
)) {
554 status
= cli_nt_error(cli
);
558 if (!cli_receive_trans(cli
, SMBtrans2
,
559 &rparam
, &rparam_count
,
560 &rdata
, &rdata_count
)) {
561 status
= cli_nt_error(cli
);
565 if (cli_is_error(cli
)) {
566 status
= cli_nt_error(cli
);
567 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
572 *out
= data_blob(rdata
, rdata_count
);
573 *param_out
= data_blob(rparam
, rparam_count
);
582 /******************************************************************************
583 Make a client state struct.
584 ******************************************************************************/
586 static struct smb_trans_enc_state
*make_cli_enc_state(enum smb_trans_enc_type smb_enc_type
)
588 struct smb_trans_enc_state
*es
= NULL
;
589 es
= SMB_MALLOC_P(struct smb_trans_enc_state
);
594 es
->smb_enc_type
= smb_enc_type
;
596 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
597 if (smb_enc_type
== SMB_TRANS_ENC_GSS
) {
598 es
->s
.gss_state
= SMB_MALLOC_P(struct smb_tran_enc_state_gss
);
599 if (!es
->s
.gss_state
) {
603 ZERO_STRUCTP(es
->s
.gss_state
);
609 /******************************************************************************
610 Start a raw ntlmssp encryption.
611 ******************************************************************************/
613 NTSTATUS
cli_raw_ntlm_smb_encryption_start(struct cli_state
*cli
,
618 DATA_BLOB blob_in
= data_blob_null
;
619 DATA_BLOB blob_out
= data_blob_null
;
620 DATA_BLOB param_out
= data_blob_null
;
621 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
622 struct smb_trans_enc_state
*es
= make_cli_enc_state(SMB_TRANS_ENC_NTLM
);
625 return NT_STATUS_NO_MEMORY
;
627 status
= ntlmssp_client_start(&es
->s
.ntlmssp_state
);
628 if (!NT_STATUS_IS_OK(status
)) {
632 ntlmssp_want_feature(es
->s
.ntlmssp_state
, NTLMSSP_FEATURE_SESSION_KEY
);
633 es
->s
.ntlmssp_state
->neg_flags
|= (NTLMSSP_NEGOTIATE_SIGN
|NTLMSSP_NEGOTIATE_SEAL
);
635 if (!NT_STATUS_IS_OK(status
= ntlmssp_set_username(es
->s
.ntlmssp_state
, user
))) {
638 if (!NT_STATUS_IS_OK(status
= ntlmssp_set_domain(es
->s
.ntlmssp_state
, domain
))) {
641 if (!NT_STATUS_IS_OK(status
= ntlmssp_set_password(es
->s
.ntlmssp_state
, pass
))) {
646 status
= ntlmssp_update(es
->s
.ntlmssp_state
, blob_in
, &blob_out
);
647 data_blob_free(&blob_in
);
648 data_blob_free(¶m_out
);
649 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) || NT_STATUS_IS_OK(status
)) {
650 NTSTATUS trans_status
= enc_blob_send_receive(cli
,
654 if (!NT_STATUS_EQUAL(trans_status
,
655 NT_STATUS_MORE_PROCESSING_REQUIRED
) &&
656 !NT_STATUS_IS_OK(trans_status
)) {
657 status
= trans_status
;
659 if (param_out
.length
== 2) {
660 es
->enc_ctx_num
= SVAL(param_out
.data
, 0);
664 data_blob_free(&blob_out
);
665 } while (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
));
667 data_blob_free(&blob_in
);
669 if (NT_STATUS_IS_OK(status
)) {
670 /* Replace the old state, if any. */
671 if (cli
->trans_enc_state
) {
672 common_free_encryption_state(&cli
->trans_enc_state
);
674 cli
->trans_enc_state
= es
;
675 cli
->trans_enc_state
->enc_on
= True
;
681 common_free_encryption_state(&es
);
685 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
687 #ifndef SMB_GSS_REQUIRED_FLAGS
688 #define SMB_GSS_REQUIRED_FLAGS (GSS_C_CONF_FLAG|GSS_C_INTEG_FLAG|GSS_C_MUTUAL_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)
691 /******************************************************************************
692 Get client gss blob to send to a server.
693 ******************************************************************************/
695 static NTSTATUS
make_cli_gss_blob(struct smb_trans_enc_state
*es
,
699 DATA_BLOB spnego_blob_in
,
700 DATA_BLOB
*p_blob_out
)
702 const char *krb_mechs
[] = {OID_KERBEROS5
, NULL
};
706 gss_buffer_desc input_name
;
707 gss_buffer_desc
*p_tok_in
;
708 gss_buffer_desc tok_out
, tok_in
;
709 DATA_BLOB blob_out
= data_blob_null
;
710 DATA_BLOB blob_in
= data_blob_null
;
711 char *host_princ_s
= NULL
;
712 OM_uint32 ret_flags
= 0;
713 NTSTATUS status
= NT_STATUS_OK
;
715 gss_OID_desc nt_hostbased_service
=
716 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
718 memset(&tok_out
, '\0', sizeof(tok_out
));
720 /* Get a ticket for the service@host */
721 if (asprintf(&host_princ_s
, "%s@%s", service
, host
) == -1) {
722 return NT_STATUS_NO_MEMORY
;
725 input_name
.value
= host_princ_s
;
726 input_name
.length
= strlen(host_princ_s
) + 1;
728 ret
= gss_import_name(&min
,
730 &nt_hostbased_service
,
733 if (ret
!= GSS_S_COMPLETE
) {
734 SAFE_FREE(host_princ_s
);
735 return map_nt_error_from_gss(ret
, min
);
738 if (spnego_blob_in
.length
== 0) {
739 p_tok_in
= GSS_C_NO_BUFFER
;
741 /* Remove the SPNEGO wrapper */
742 if (!spnego_parse_auth_response(spnego_blob_in
, status_in
, OID_KERBEROS5
, &blob_in
)) {
743 status
= NT_STATUS_UNSUCCESSFUL
;
746 tok_in
.value
= blob_in
.data
;
747 tok_in
.length
= blob_in
.length
;
751 ret
= gss_init_sec_context(&min
,
752 GSS_C_NO_CREDENTIAL
, /* Use our default cred. */
753 &es
->s
.gss_state
->gss_ctx
,
755 GSS_C_NO_OID
, /* default OID. */
756 GSS_C_MUTUAL_FLAG
| GSS_C_REPLAY_FLAG
| GSS_C_SEQUENCE_FLAG
| GSS_C_DELEG_FLAG
,
757 GSS_C_INDEFINITE
, /* requested ticket lifetime. */
758 NULL
, /* no channel bindings */
760 NULL
, /* ignore mech type */
763 NULL
); /* ignore time_rec */
765 status
= map_nt_error_from_gss(ret
, min
);
766 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
767 ADS_STATUS adss
= ADS_ERROR_GSS(ret
, min
);
768 DEBUG(10,("make_cli_gss_blob: gss_init_sec_context failed with %s\n",
773 if ((ret_flags
& SMB_GSS_REQUIRED_FLAGS
) != SMB_GSS_REQUIRED_FLAGS
) {
774 status
= NT_STATUS_ACCESS_DENIED
;
777 blob_out
= data_blob(tok_out
.value
, tok_out
.length
);
779 /* Wrap in an SPNEGO wrapper */
780 *p_blob_out
= gen_negTokenTarg(krb_mechs
, blob_out
);
784 data_blob_free(&blob_out
);
785 data_blob_free(&blob_in
);
786 SAFE_FREE(host_princ_s
);
787 gss_release_name(&min
, &srv_name
);
789 gss_release_buffer(&min
, &tok_out
);
794 /******************************************************************************
795 Start a SPNEGO gssapi encryption context.
796 ******************************************************************************/
798 NTSTATUS
cli_gss_smb_encryption_start(struct cli_state
*cli
)
800 DATA_BLOB blob_recv
= data_blob_null
;
801 DATA_BLOB blob_send
= data_blob_null
;
802 DATA_BLOB param_out
= data_blob_null
;
803 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
805 const char *servicename
;
806 struct smb_trans_enc_state
*es
= make_cli_enc_state(SMB_TRANS_ENC_GSS
);
809 return NT_STATUS_NO_MEMORY
;
812 name_to_fqdn(fqdn
, cli
->desthost
);
815 servicename
= "cifs";
816 status
= make_cli_gss_blob(es
, servicename
, fqdn
, NT_STATUS_OK
, blob_recv
, &blob_send
);
817 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
818 servicename
= "host";
819 status
= make_cli_gss_blob(es
, servicename
, fqdn
, NT_STATUS_OK
, blob_recv
, &blob_send
);
820 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
826 data_blob_free(&blob_recv
);
827 status
= enc_blob_send_receive(cli
, &blob_send
, &blob_recv
, ¶m_out
);
828 if (param_out
.length
== 2) {
829 es
->enc_ctx_num
= SVAL(param_out
.data
, 0);
831 data_blob_free(&blob_send
);
832 status
= make_cli_gss_blob(es
, servicename
, fqdn
, status
, blob_recv
, &blob_send
);
833 } while (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
));
834 data_blob_free(&blob_recv
);
836 if (NT_STATUS_IS_OK(status
)) {
837 /* Replace the old state, if any. */
838 if (cli
->trans_enc_state
) {
839 common_free_encryption_state(&cli
->trans_enc_state
);
841 cli
->trans_enc_state
= es
;
842 cli
->trans_enc_state
->enc_on
= True
;
848 common_free_encryption_state(&es
);
852 NTSTATUS
cli_gss_smb_encryption_start(struct cli_state
*cli
)
854 return NT_STATUS_NOT_SUPPORTED
;
858 /********************************************************************
859 Ensure a connection is encrypted.
860 ********************************************************************/
862 NTSTATUS
cli_force_encryption(struct cli_state
*c
,
863 const char *username
,
864 const char *password
,
868 uint32 caplow
, caphigh
;
871 if (!SERVER_HAS_UNIX_CIFS(c
)) {
872 return NT_STATUS_NOT_SUPPORTED
;
875 status
= cli_unix_extensions_version(c
, &major
, &minor
, &caplow
,
877 if (!NT_STATUS_IS_OK(status
)) {
878 DEBUG(10, ("cli_force_encryption: cli_unix_extensions_version "
879 "returned %s\n", nt_errstr(status
)));
880 return NT_STATUS_UNKNOWN_REVISION
;
883 if (!(caplow
& CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP
)) {
884 return NT_STATUS_UNSUPPORTED_COMPRESSION
;
887 if (c
->use_kerberos
) {
888 return cli_gss_smb_encryption_start(c
);
890 return cli_raw_ntlm_smb_encryption_start(c
,