2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
Winbind authentication mechnism
Copyright ( C ) Tim Potter 2000
Copyright ( C ) Andrew Bartlett 2001 - 2002
2005-01-09 15:55:25 +03:00
Copyright ( C ) Stefan Metzmacher 2005
2003-08-13 05:53:07 +04:00
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
2003-08-13 05:53:07 +04:00
( at your option ) any later version .
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 .
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/>.
2003-08-13 05:53:07 +04:00
*/
# include "includes.h"
2017-06-17 01:56:09 +03:00
# include <tevent.h>
# include "../lib/util/tevent_ntstatus.h"
2004-11-02 05:57:18 +03:00
# include "auth/auth.h"
2008-05-05 13:28:38 +04:00
# include "auth/ntlm/auth_proto.h"
2010-09-03 18:59:05 +04:00
# include "librpc/gen_ndr/ndr_winbind_c.h"
2006-07-31 19:42:27 +04:00
# include "lib/messaging/irpc.h"
2008-01-02 07:05:13 +03:00
# include "param/param.h"
2009-06-25 21:16:02 +04:00
# include "nsswitch/libwbclient/wbclient.h"
2014-03-28 01:56:02 +04:00
# include "auth/auth_sam_reply.h"
2010-09-20 11:42:13 +04:00
# include "libcli/security/security.h"
2017-03-23 13:57:49 +03:00
# include "dsdb/samdb/samdb.h"
2017-04-03 06:26:12 +03:00
# include "auth/auth_sam.h"
2003-08-13 05:53:07 +04:00
2017-04-20 22:24:43 +03:00
_PUBLIC_ NTSTATUS auth4_winbind_init ( TALLOC_CTX * ) ;
2011-03-19 02:44:28 +03:00
2006-07-27 15:24:18 +04:00
static NTSTATUS winbind_want_check ( struct auth_method_context * ctx ,
TALLOC_CTX * mem_ctx ,
const struct auth_usersupplied_info * user_info )
{
if ( ! user_info - > mapped . account_name | | ! * user_info - > mapped . account_name ) {
return NT_STATUS_NOT_IMPLEMENTED ;
}
/* TODO: maybe limit the user scope to remote users only */
return NT_STATUS_OK ;
}
2006-07-31 19:42:27 +04:00
struct winbind_check_password_state {
2017-06-17 01:56:09 +03:00
struct auth_method_context * ctx ;
const struct auth_usersupplied_info * user_info ;
2006-07-31 19:42:27 +04:00
struct winbind_SamLogon req ;
2017-06-17 01:56:09 +03:00
struct auth_user_info_dc * user_info_dc ;
bool authoritative ;
2006-07-31 19:42:27 +04:00
} ;
2017-06-17 01:56:09 +03:00
static void winbind_check_password_done ( struct tevent_req * subreq ) ;
2006-07-31 19:42:27 +04:00
/*
Authenticate a user with a challenge / response
using IRPC to the winbind task
*/
2017-06-17 01:56:09 +03:00
static struct tevent_req * winbind_check_password_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct auth_method_context * ctx ,
const struct auth_usersupplied_info * user_info )
2006-07-31 19:42:27 +04:00
{
2017-06-17 01:56:09 +03:00
struct tevent_req * req = NULL ;
2017-06-17 01:29:25 +03:00
struct winbind_check_password_state * state = NULL ;
2006-07-31 19:42:27 +04:00
NTSTATUS status ;
2010-09-03 18:59:05 +04:00
struct dcerpc_binding_handle * irpc_handle ;
2006-07-31 19:42:27 +04:00
const struct auth_usersupplied_info * user_info_new ;
struct netr_IdentityInfo * identity_info ;
2017-06-17 01:56:09 +03:00
struct imessaging_context * msg_ctx ;
struct tevent_req * subreq = NULL ;
2006-07-31 19:42:27 +04:00
2017-06-17 01:56:09 +03:00
req = tevent_req_create ( mem_ctx , & state ,
struct winbind_check_password_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > ctx = ctx ;
state - > user_info = user_info ;
state - > authoritative = true ;
msg_ctx = imessaging_client_init ( state , ctx - > auth_ctx - > lp_ctx , ev ) ;
if ( msg_ctx = = NULL ) {
DEBUG ( 1 , ( " imessaging_init failed \n " ) ) ;
tevent_req_nterror ( req , NT_STATUS_INVALID_SERVER_STATE ) ;
return tevent_req_post ( req , ev ) ;
2010-04-19 09:43:33 +04:00
}
2017-06-17 01:56:09 +03:00
irpc_handle = irpc_binding_handle_by_name ( state , msg_ctx ,
2010-09-03 18:59:05 +04:00
" winbind_server " ,
& ndr_table_winbind ) ;
if ( irpc_handle = = NULL ) {
2006-07-31 19:42:27 +04:00
DEBUG ( 0 , ( " Winbind authentication for [%s] \\ [%s] failed, "
" no winbind_server running! \n " ,
user_info - > client . domain_name , user_info - > client . account_name ) ) ;
2017-06-17 01:56:09 +03:00
tevent_req_nterror ( req , NT_STATUS_NO_LOGON_SERVERS ) ;
return tevent_req_post ( req , ev ) ;
2006-07-31 19:42:27 +04:00
}
2017-06-17 01:56:09 +03:00
/*
* 120 seconds should be enough even for trusted domains .
*
* Currently winbindd has a much lower limit .
* And tests with Windows RODCs show that it
* returns NO_LOGON_SERVERS after 90 - 100 seconds
* if it can ' t reach any RWDC .
*/
dcerpc_binding_handle_set_timeout ( irpc_handle , 120 ) ;
2006-07-31 19:42:27 +04:00
if ( user_info - > flags & USER_INFO_INTERACTIVE_LOGON ) {
struct netr_PasswordInfo * password_info ;
2017-06-17 01:29:25 +03:00
status = encrypt_user_info ( state , ctx - > auth_ctx , AUTH_PASSWORD_HASH ,
2006-07-31 19:42:27 +04:00
user_info , & user_info_new ) ;
2017-06-17 01:56:09 +03:00
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
2006-07-31 19:42:27 +04:00
user_info = user_info_new ;
2017-06-17 01:29:25 +03:00
password_info = talloc_zero ( state , struct netr_PasswordInfo ) ;
2017-06-17 01:56:09 +03:00
if ( tevent_req_nomem ( password_info , req ) ) {
return tevent_req_post ( req , ev ) ;
}
2006-07-31 19:42:27 +04:00
password_info - > lmpassword = * user_info - > password . hash . lanman ;
password_info - > ntpassword = * user_info - > password . hash . nt ;
identity_info = & password_info - > identity_info ;
2017-06-17 01:29:25 +03:00
state - > req . in . logon_level = 1 ;
state - > req . in . logon . password = password_info ;
2006-07-31 19:42:27 +04:00
} else {
struct netr_NetworkInfo * network_info ;
2009-12-14 12:32:47 +03:00
uint8_t chal [ 8 ] ;
2006-07-31 19:42:27 +04:00
2017-06-17 01:29:25 +03:00
status = encrypt_user_info ( state , ctx - > auth_ctx , AUTH_PASSWORD_RESPONSE ,
2006-07-31 19:42:27 +04:00
user_info , & user_info_new ) ;
2017-06-17 01:56:09 +03:00
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
2006-07-31 19:42:27 +04:00
user_info = user_info_new ;
2017-06-17 01:29:25 +03:00
network_info = talloc_zero ( state , struct netr_NetworkInfo ) ;
2017-06-17 01:56:09 +03:00
if ( tevent_req_nomem ( network_info , req ) ) {
return tevent_req_post ( req , ev ) ;
}
2006-07-31 19:42:27 +04:00
2009-12-14 12:32:47 +03:00
status = auth_get_challenge ( ctx - > auth_ctx , chal ) ;
2017-06-17 01:56:09 +03:00
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
2006-07-31 19:42:27 +04:00
2009-12-14 12:32:47 +03:00
memcpy ( network_info - > challenge , chal , sizeof ( network_info - > challenge ) ) ;
2006-07-31 19:42:27 +04:00
network_info - > nt . length = user_info - > password . response . nt . length ;
network_info - > nt . data = user_info - > password . response . nt . data ;
2007-04-30 01:40:48 +04:00
network_info - > lm . length = user_info - > password . response . lanman . length ;
network_info - > lm . data = user_info - > password . response . lanman . data ;
2006-07-31 19:42:27 +04:00
identity_info = & network_info - > identity_info ;
2017-06-17 01:29:25 +03:00
state - > req . in . logon_level = 2 ;
state - > req . in . logon . network = network_info ;
2006-07-31 19:42:27 +04:00
}
identity_info - > domain_name . string = user_info - > client . domain_name ;
identity_info - > parameter_control = user_info - > logon_parameters ; /* see MSV1_0_* */
identity_info - > logon_id_low = 0 ;
identity_info - > logon_id_high = 0 ;
identity_info - > account_name . string = user_info - > client . account_name ;
identity_info - > workstation . string = user_info - > workstation_name ;
2018-01-23 19:58:07 +03:00
state - > req . in . validation_level = 6 ;
2007-04-30 01:40:48 +04:00
2017-06-17 01:56:09 +03:00
subreq = dcerpc_winbind_SamLogon_r_send ( state , ev , irpc_handle ,
& state - > req ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq ,
winbind_check_password_done ,
req ) ;
return req ;
}
static void winbind_check_password_done ( struct tevent_req * subreq )
{
struct tevent_req * req =
tevent_req_callback_data ( subreq ,
struct tevent_req ) ;
struct winbind_check_password_state * state =
tevent_req_data ( req ,
struct winbind_check_password_state ) ;
struct auth_method_context * ctx = state - > ctx ;
const struct auth_usersupplied_info * user_info = state - > user_info ;
struct ldb_dn * domain_dn = NULL ;
2018-01-09 11:23:26 +03:00
const char * nt4_domain = NULL ;
const char * nt4_account = NULL ;
2017-06-17 01:56:09 +03:00
struct ldb_message * msg = NULL ;
NTSTATUS status ;
2006-07-31 19:42:27 +04:00
2017-06-17 01:56:09 +03:00
status = dcerpc_winbind_SamLogon_r_recv ( subreq , state ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
status = NT_STATUS_NO_LOGON_SERVERS ;
}
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
status = state - > req . out . result ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2017-06-17 01:29:25 +03:00
if ( ! state - > req . out . authoritative ) {
2017-06-17 01:56:09 +03:00
state - > authoritative = false ;
2017-06-17 01:26:18 +03:00
}
2017-06-17 01:56:09 +03:00
tevent_req_nterror ( req , status ) ;
return ;
2017-02-25 12:55:28 +03:00
}
2018-01-09 11:23:26 +03:00
status = make_user_info_dc_netlogon_validation ( state ,
user_info - > client . account_name ,
state - > req . in . validation_level ,
& state - > req . out . validation ,
true , /* This user was authenticated */
& state - > user_info_dc ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
nt4_domain = state - > user_info_dc - > info - > domain_name ;
nt4_account = state - > user_info_dc - > info - > account_name ;
if ( lpcfg_is_mydomain ( ctx - > auth_ctx - > lp_ctx , nt4_domain ) ) {
domain_dn = ldb_get_default_basedn ( ctx - > auth_ctx - > sam_ctx ) ;
2017-06-27 13:09:41 +03:00
}
2017-04-03 06:26:12 +03:00
2017-06-27 13:09:41 +03:00
if ( domain_dn ! = NULL ) {
2018-01-09 11:23:26 +03:00
/*
* At best , reset the badPwdCount to 0 if the account exists .
* This means that lockouts happen at a badPwdCount earlier than
* normal , but makes it more fault tolerant .
*/
2017-06-17 01:56:09 +03:00
status = authsam_search_account ( state , ctx - > auth_ctx - > sam_ctx ,
2018-01-09 11:23:26 +03:00
nt4_account , domain_dn , & msg ) ;
2017-06-27 13:09:41 +03:00
if ( NT_STATUS_IS_OK ( status ) ) {
authsam_logon_success_accounting (
ctx - > auth_ctx - > sam_ctx , msg ,
domain_dn ,
user_info - > flags & USER_INFO_INTERACTIVE_LOGON ,
NULL ) ;
2017-04-03 06:26:12 +03:00
}
}
2017-06-17 01:56:09 +03:00
tevent_req_done ( req ) ;
}
static NTSTATUS winbind_check_password_recv ( struct tevent_req * req ,
TALLOC_CTX * mem_ctx ,
struct auth_user_info_dc * * user_info_dc ,
bool * pauthoritative )
{
struct winbind_check_password_state * state =
tevent_req_data ( req ,
struct winbind_check_password_state ) ;
NTSTATUS status = NT_STATUS_OK ;
* pauthoritative = state - > authoritative ;
if ( tevent_req_is_nterror ( req , & status ) ) {
tevent_req_received ( req ) ;
return status ;
}
* user_info_dc = talloc_move ( mem_ctx , & state - > user_info_dc ) ;
tevent_req_received ( req ) ;
2006-07-31 19:42:27 +04:00
return NT_STATUS_OK ;
}
2005-01-09 15:55:25 +03:00
static const struct auth_operations winbind_ops = {
2017-06-17 01:56:09 +03:00
. name = " winbind " ,
. want_check = winbind_want_check ,
. check_password_send = winbind_check_password_send ,
. check_password_recv = winbind_check_password_recv
2005-01-09 15:55:25 +03:00
} ;
2004-10-25 03:54:00 +04:00
2017-04-20 22:24:43 +03:00
_PUBLIC_ NTSTATUS auth4_winbind_init ( TALLOC_CTX * ctx )
2004-10-25 03:54:00 +04:00
{
NTSTATUS ret ;
2017-05-09 22:39:14 +03:00
ret = auth_register ( ctx , & winbind_ops ) ;
2004-10-25 03:54:00 +04:00
if ( ! NT_STATUS_IS_OK ( ret ) ) {
2005-01-09 15:55:25 +03:00
DEBUG ( 0 , ( " Failed to register 'winbind' auth backend! \n " ) ) ;
return ret ;
2004-10-25 03:54:00 +04:00
}
2006-07-31 19:42:27 +04:00
return NT_STATUS_OK ;
2004-10-25 03:54:00 +04:00
}