2012-01-30 15:42:39 +04:00
/*
2005-04-25 09:03:50 +04:00
Unix SMB / Netbios implementation .
Version 3.0
handle NLTMSSP , client server side parsing
Copyright ( C ) Andrew Tridgell 2001
Copyright ( C ) Andrew Bartlett < abartlet @ samba . org > 2001 - 2005
Copyright ( C ) Stefan Metzmacher 2005
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2005-04-25 09:03:50 +04:00
( at your option ) any later version .
2012-01-30 15:42:39 +04:00
2005-04-25 09:03:50 +04:00
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2012-01-30 15:42:39 +04:00
2005-04-25 09:03:50 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2005-04-25 09:03:50 +04:00
*/
2011-07-25 10:04:38 +04:00
struct auth_session_info ;
2005-04-25 09:03:50 +04:00
# include "includes.h"
2017-05-11 10:04:02 +03:00
# include <tevent.h>
# include "lib/util/tevent_ntstatus.h"
2005-04-25 09:03:50 +04:00
# include "auth/ntlmssp/ntlmssp.h"
2012-01-30 15:42:39 +04:00
# include "auth/ntlmssp/ntlmssp_private.h"
2009-04-16 04:17:17 +04:00
# include "../libcli/auth/libcli_auth.h"
2006-03-18 18:42:57 +03:00
# include "librpc/gen_ndr/ndr_dcerpc.h"
2006-11-07 03:48:36 +03:00
# include "auth/gensec/gensec.h"
2013-08-05 09:12:01 +04:00
# include "auth/gensec/gensec_internal.h"
2005-04-25 09:03:50 +04:00
2017-12-19 12:49:10 +03:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_AUTH
2005-04-25 09:03:50 +04:00
/**
* Callbacks for NTLMSSP - for both client and server operating modes
2012-01-30 15:42:39 +04:00
*
2005-04-25 09:03:50 +04:00
*/
static const struct ntlmssp_callbacks {
enum ntlmssp_role role ;
2006-07-27 23:33:15 +04:00
enum ntlmssp_message_type command ;
NTSTATUS ( * sync_fn ) ( struct gensec_security * gensec_security ,
TALLOC_CTX * out_mem_ctx ,
DATA_BLOB in , DATA_BLOB * out ) ;
2017-06-15 01:34:26 +03:00
struct tevent_req * ( * send_fn ) ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct gensec_security * gensec_security ,
const DATA_BLOB in ) ;
NTSTATUS ( * recv_fn ) ( struct tevent_req * req ,
TALLOC_CTX * out_mem_ctx ,
DATA_BLOB * out ) ;
2005-04-25 09:03:50 +04:00
} ntlmssp_callbacks [ ] = {
2006-07-27 23:33:15 +04:00
{
. role = NTLMSSP_CLIENT ,
. command = NTLMSSP_INITIAL ,
. sync_fn = ntlmssp_client_initial ,
2015-11-25 23:41:23 +03:00
} , {
. role = NTLMSSP_CLIENT ,
. command = NTLMSSP_NEGOTIATE ,
. sync_fn = gensec_ntlmssp_resume_ccache ,
2006-07-27 23:33:15 +04:00
} , {
. role = NTLMSSP_SERVER ,
. command = NTLMSSP_NEGOTIATE ,
2010-08-06 11:53:44 +04:00
. sync_fn = gensec_ntlmssp_server_negotiate ,
2006-07-27 23:33:15 +04:00
} , {
. role = NTLMSSP_CLIENT ,
. command = NTLMSSP_CHALLENGE ,
. sync_fn = ntlmssp_client_challenge ,
} , {
. role = NTLMSSP_SERVER ,
. command = NTLMSSP_AUTH ,
2017-06-16 17:16:15 +03:00
. send_fn = ntlmssp_server_auth_send ,
. recv_fn = ntlmssp_server_auth_recv ,
2006-07-27 23:33:15 +04:00
}
2005-04-25 09:03:50 +04:00
} ;
2012-03-09 07:28:46 +04:00
static NTSTATUS gensec_ntlmssp_update_find ( struct gensec_security * gensec_security ,
struct gensec_ntlmssp_context * gensec_ntlmssp ,
2006-07-27 23:20:57 +04:00
const DATA_BLOB input , uint32_t * idx )
2005-04-25 09:03:50 +04:00
{
uint32_t ntlmssp_command ;
2006-07-27 23:20:57 +04:00
uint32_t i ;
2005-04-25 09:03:50 +04:00
2012-02-07 10:47:42 +04:00
if ( gensec_ntlmssp - > ntlmssp_state - > expected_state = = NTLMSSP_DONE ) {
2005-08-23 09:29:37 +04:00
/* We are strict here because other modules, which we
* don ' t fully control ( such as GSSAPI ) are also
* strict , but are tested less often */
DEBUG ( 1 , ( " Called NTLMSSP after state machine was 'done' \n " ) ) ;
return NT_STATUS_INVALID_PARAMETER ;
2005-04-25 09:03:50 +04:00
}
if ( ! input . length ) {
2012-02-07 10:47:42 +04:00
switch ( gensec_ntlmssp - > ntlmssp_state - > role ) {
2005-04-25 09:03:50 +04:00
case NTLMSSP_CLIENT :
2015-11-25 23:41:23 +03:00
if ( gensec_ntlmssp - > ntlmssp_state - > resume_ccache ) {
/*
* make sure gensec_ntlmssp_resume_ccache ( )
* will be called
*/
ntlmssp_command = NTLMSSP_NEGOTIATE ;
break ;
}
2005-04-25 09:03:50 +04:00
ntlmssp_command = NTLMSSP_INITIAL ;
break ;
case NTLMSSP_SERVER :
2005-08-20 08:42:19 +04:00
if ( gensec_security - > want_features & GENSEC_FEATURE_DATAGRAM_MODE ) {
/* 'datagram' mode - no neg packet */
ntlmssp_command = NTLMSSP_NEGOTIATE ;
} else {
/* This is normal in SPNEGO mech negotiation fallback */
DEBUG ( 2 , ( " Failed to parse NTLMSSP packet: zero length \n " ) ) ;
return NT_STATUS_INVALID_PARAMETER ;
}
2005-04-25 09:03:50 +04:00
break ;
2016-12-21 03:24:46 +03:00
default :
DEBUG ( 1 , ( " NTLMSSP state has invalid role %d \n " ,
gensec_ntlmssp - > ntlmssp_state - > role ) ) ;
return NT_STATUS_INVALID_PARAMETER ;
2005-04-25 09:03:50 +04:00
}
} else {
2012-02-07 10:47:42 +04:00
if ( ! msrpc_parse ( gensec_ntlmssp - > ntlmssp_state ,
2005-04-25 09:03:50 +04:00
& input , " Cd " ,
" NTLMSSP " ,
& ntlmssp_command ) ) {
DEBUG ( 1 , ( " Failed to parse NTLMSSP packet, could not extract NTLMSSP command \n " ) ) ;
dump_data ( 2 , input . data , input . length ) ;
return NT_STATUS_INVALID_PARAMETER ;
}
}
2012-02-07 10:47:42 +04:00
if ( ntlmssp_command ! = gensec_ntlmssp - > ntlmssp_state - > expected_state ) {
DEBUG ( 2 , ( " got NTLMSSP command %u, expected %u \n " , ntlmssp_command ,
gensec_ntlmssp - > ntlmssp_state - > expected_state ) ) ;
2005-04-25 09:03:50 +04:00
return NT_STATUS_INVALID_PARAMETER ;
}
for ( i = 0 ; i < ARRAY_SIZE ( ntlmssp_callbacks ) ; i + + ) {
2012-02-07 10:47:42 +04:00
if ( ntlmssp_callbacks [ i ] . role = = gensec_ntlmssp - > ntlmssp_state - > role & &
2006-07-27 23:33:15 +04:00
ntlmssp_callbacks [ i ] . command = = ntlmssp_command ) {
2006-07-27 23:20:57 +04:00
* idx = i ;
return NT_STATUS_OK ;
2005-04-25 09:03:50 +04:00
}
}
2012-01-30 15:42:39 +04:00
DEBUG ( 1 , ( " failed to find NTLMSSP callback for NTLMSSP mode %u, command %u \n " ,
2012-02-07 10:47:42 +04:00
gensec_ntlmssp - > ntlmssp_state - > role , ntlmssp_command ) ) ;
2012-01-30 15:42:39 +04:00
2006-07-27 23:20:57 +04:00
return NT_STATUS_INVALID_PARAMETER ;
}
2017-05-11 10:04:02 +03:00
struct gensec_ntlmssp_update_state {
2017-06-15 01:34:26 +03:00
const struct ntlmssp_callbacks * cb ;
2017-05-11 10:04:02 +03:00
NTSTATUS status ;
DATA_BLOB out ;
} ;
2005-04-25 09:03:50 +04:00
2017-06-15 01:34:26 +03:00
static void gensec_ntlmssp_update_done ( struct tevent_req * subreq ) ;
2017-05-11 10:04:02 +03:00
static struct tevent_req * gensec_ntlmssp_update_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct gensec_security * gensec_security ,
const DATA_BLOB in )
2006-07-27 23:20:57 +04:00
{
2009-12-30 10:23:13 +03:00
struct gensec_ntlmssp_context * gensec_ntlmssp =
talloc_get_type_abort ( gensec_security - > private_data ,
struct gensec_ntlmssp_context ) ;
2017-05-11 10:04:02 +03:00
struct tevent_req * req = NULL ;
struct gensec_ntlmssp_update_state * state = NULL ;
2006-07-27 23:20:57 +04:00
NTSTATUS status ;
2017-05-11 10:04:02 +03:00
uint32_t i = 0 ;
2006-07-27 23:20:57 +04:00
2017-05-11 10:04:02 +03:00
req = tevent_req_create ( mem_ctx , & state ,
struct gensec_ntlmssp_update_state ) ;
if ( req = = NULL ) {
return NULL ;
}
2006-07-27 23:20:57 +04:00
2017-05-18 09:43:09 +03:00
status = gensec_ntlmssp_update_find ( gensec_security ,
gensec_ntlmssp ,
in , & i ) ;
2017-05-11 10:04:02 +03:00
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
2017-05-17 21:21:19 +03:00
}
2006-07-27 23:20:57 +04:00
2017-06-15 01:34:26 +03:00
if ( ntlmssp_callbacks [ i ] . send_fn ! = NULL ) {
struct tevent_req * subreq = NULL ;
state - > cb = & ntlmssp_callbacks [ i ] ;
subreq = state - > cb - > send_fn ( state , ev ,
gensec_security ,
in ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq ,
gensec_ntlmssp_update_done ,
req ) ;
return req ;
}
2017-05-18 09:43:09 +03:00
status = ntlmssp_callbacks [ i ] . sync_fn ( gensec_security ,
2017-05-11 10:04:02 +03:00
state ,
in , & state - > out ) ;
state - > status = status ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) ) {
tevent_req_done ( req ) ;
return tevent_req_post ( req , ev ) ;
}
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_done ( req ) ;
return tevent_req_post ( req , ev ) ;
}
2017-06-15 01:34:26 +03:00
static void gensec_ntlmssp_update_done ( struct tevent_req * subreq )
{
struct tevent_req * req =
tevent_req_callback_data ( subreq ,
struct tevent_req ) ;
struct gensec_ntlmssp_update_state * state =
tevent_req_data ( req ,
struct gensec_ntlmssp_update_state ) ;
NTSTATUS status ;
status = state - > cb - > recv_fn ( subreq , state , & state - > out ) ;
TALLOC_FREE ( subreq ) ;
if ( GENSEC_UPDATE_IS_NTERROR ( status ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
state - > status = status ;
tevent_req_done ( req ) ;
}
2017-05-11 10:04:02 +03:00
static NTSTATUS gensec_ntlmssp_update_recv ( struct tevent_req * req ,
TALLOC_CTX * out_mem_ctx ,
DATA_BLOB * out )
{
struct gensec_ntlmssp_update_state * state =
tevent_req_data ( req ,
struct gensec_ntlmssp_update_state ) ;
NTSTATUS status ;
* out = data_blob_null ;
if ( tevent_req_is_nterror ( req , & status ) ) {
tevent_req_received ( req ) ;
2017-05-17 21:21:19 +03:00
return status ;
}
2012-01-30 15:42:39 +04:00
2017-05-11 10:04:02 +03:00
* out = state - > out ;
talloc_steal ( out_mem_ctx , state - > out . data ) ;
status = state - > status ;
tevent_req_received ( req ) ;
return status ;
2005-04-25 09:03:50 +04:00
}
2013-12-17 14:49:31 +04:00
static NTSTATUS gensec_ntlmssp_may_reset_crypto ( struct gensec_security * gensec_security ,
bool full_reset )
{
struct gensec_ntlmssp_context * gensec_ntlmssp =
talloc_get_type_abort ( gensec_security - > private_data ,
struct gensec_ntlmssp_context ) ;
struct ntlmssp_state * ntlmssp_state = gensec_ntlmssp - > ntlmssp_state ;
NTSTATUS status ;
bool reset_seqnums = full_reset ;
if ( ! gensec_ntlmssp_have_feature ( gensec_security , GENSEC_FEATURE_SIGN ) ) {
return NT_STATUS_OK ;
}
status = ntlmssp_sign_reset ( ntlmssp_state , reset_seqnums ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 1 , ( " Could not reset NTLMSSP signing/sealing system (error was: %s) \n " ,
nt_errstr ( status ) ) ) ;
return status ;
}
return NT_STATUS_OK ;
}
2017-03-01 06:00:03 +03:00
static const char * gensec_ntlmssp_final_auth_type ( struct gensec_security * gensec_security )
{
return GENSEC_FINAL_AUTH_TYPE_NTLMSSP ;
}
2012-01-30 15:42:39 +04:00
static const char * gensec_ntlmssp_oids [ ] = {
GENSEC_OID_NTLMSSP ,
2005-05-16 03:42:11 +04:00
NULL
} ;
2005-04-25 09:03:50 +04:00
static const struct gensec_security_ops gensec_ntlmssp_security_ops = {
. name = " ntlmssp " ,
2010-06-01 13:12:29 +04:00
. sasl_name = GENSEC_SASL_NAME_NTLMSSP , /* "NTLM" */
2005-04-25 09:03:50 +04:00
. auth_type = DCERPC_AUTH_TYPE_NTLMSSP ,
2019-11-11 18:39:24 +03:00
. weak_crypto = true ,
2005-05-16 03:42:11 +04:00
. oid = gensec_ntlmssp_oids ,
2005-04-25 09:03:50 +04:00
. client_start = gensec_ntlmssp_client_start ,
. server_start = gensec_ntlmssp_server_start ,
2005-06-22 06:12:26 +04:00
. magic = gensec_ntlmssp_magic ,
2017-05-11 10:04:02 +03:00
. update_send = gensec_ntlmssp_update_send ,
. update_recv = gensec_ntlmssp_update_recv ,
2013-12-17 14:49:31 +04:00
. may_reset_crypto = gensec_ntlmssp_may_reset_crypto ,
2005-04-25 09:03:50 +04:00
. sig_size = gensec_ntlmssp_sig_size ,
. sign_packet = gensec_ntlmssp_sign_packet ,
. check_packet = gensec_ntlmssp_check_packet ,
. seal_packet = gensec_ntlmssp_seal_packet ,
. unseal_packet = gensec_ntlmssp_unseal_packet ,
. wrap = gensec_ntlmssp_wrap ,
. unwrap = gensec_ntlmssp_unwrap ,
. session_key = gensec_ntlmssp_session_key ,
. session_info = gensec_ntlmssp_session_info ,
. have_feature = gensec_ntlmssp_have_feature ,
2017-03-01 06:00:03 +03:00
. final_auth_type = gensec_ntlmssp_final_auth_type ,
2007-10-07 02:16:19 +04:00
. enabled = true ,
2006-09-08 10:57:01 +04:00
. priority = GENSEC_NTLMSSP
2005-04-25 09:03:50 +04:00
} ;
2015-11-25 23:41:23 +03:00
static const struct gensec_security_ops gensec_ntlmssp_resume_ccache_ops = {
. name = " ntlmssp_resume_ccache " ,
2019-11-11 18:39:24 +03:00
. weak_crypto = true ,
2015-11-25 23:41:23 +03:00
. client_start = gensec_ntlmssp_resume_ccache_start ,
2017-05-11 10:04:02 +03:00
. update_send = gensec_ntlmssp_update_send ,
. update_recv = gensec_ntlmssp_update_recv ,
2015-11-25 23:41:23 +03:00
. session_key = gensec_ntlmssp_session_key ,
. have_feature = gensec_ntlmssp_have_feature ,
. enabled = true ,
. priority = GENSEC_NTLMSSP
} ;
2005-04-25 09:03:50 +04:00
2017-04-20 22:24:43 +03:00
_PUBLIC_ NTSTATUS gensec_ntlmssp_init ( TALLOC_CTX * ctx )
2005-04-25 09:03:50 +04:00
{
NTSTATUS ret ;
2006-04-06 20:08:46 +04:00
2017-05-12 01:56:29 +03:00
ret = gensec_register ( ctx , & gensec_ntlmssp_security_ops ) ;
2005-04-25 09:03:50 +04:00
if ( ! NT_STATUS_IS_OK ( ret ) ) {
DEBUG ( 0 , ( " Failed to register '%s' gensec backend! \n " ,
gensec_ntlmssp_security_ops . name ) ) ;
return ret ;
}
2017-05-12 01:56:29 +03:00
ret = gensec_register ( ctx , & gensec_ntlmssp_resume_ccache_ops ) ;
2015-11-25 23:41:23 +03:00
if ( ! NT_STATUS_IS_OK ( ret ) ) {
DEBUG ( 0 , ( " Failed to register '%s' gensec backend! \n " ,
gensec_ntlmssp_resume_ccache_ops . name ) ) ;
return ret ;
}
2005-04-25 09:03:50 +04:00
return ret ;
}
2012-02-07 10:02:14 +04:00
2016-03-02 14:06:50 +03:00
static struct gensec_security * gensec_find_child_by_ops ( struct gensec_security * gensec_security ,
const struct gensec_security_ops * ops )
{
struct gensec_security * current = gensec_security ;
while ( current ! = NULL ) {
if ( current - > ops = = ops ) {
return current ;
}
current = current - > child_security ;
}
return NULL ;
}
2012-02-07 10:02:14 +04:00
uint32_t gensec_ntlmssp_neg_flags ( struct gensec_security * gensec_security )
{
struct gensec_ntlmssp_context * gensec_ntlmssp ;
2016-03-02 14:06:50 +03:00
gensec_security = gensec_find_child_by_ops ( gensec_security ,
& gensec_ntlmssp_security_ops ) ;
if ( gensec_security = = NULL ) {
2012-02-07 10:02:14 +04:00
return 0 ;
}
2016-03-02 14:06:50 +03:00
2012-02-07 10:02:14 +04:00
gensec_ntlmssp = talloc_get_type_abort ( gensec_security - > private_data ,
struct gensec_ntlmssp_context ) ;
return gensec_ntlmssp - > ntlmssp_state - > neg_flags ;
}
2016-03-02 14:06:50 +03:00
const char * gensec_ntlmssp_server_domain ( struct gensec_security * gensec_security )
{
struct gensec_ntlmssp_context * gensec_ntlmssp ;
gensec_security = gensec_find_child_by_ops ( gensec_security ,
& gensec_ntlmssp_security_ops ) ;
if ( gensec_security = = NULL ) {
return NULL ;
}
gensec_ntlmssp = talloc_get_type_abort ( gensec_security - > private_data ,
struct gensec_ntlmssp_context ) ;
return gensec_ntlmssp - > ntlmssp_state - > server . netbios_domain ;
}