2010-03-29 19:52:38 +04:00
/*
Unix SMB / CIFS implementation .
async implementation of WINBINDD_PAM_AUTH
Copyright ( C ) Volker Lendecke 2010
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
the Free Software Foundation ; either version 3 of the License , or
( 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
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
# include "winbindd.h"
2017-11-29 12:55:25 +03:00
# include "libcli/security/dom_sid.h"
2020-08-07 21:17:34 +03:00
# include "lib/util/string_wrappers.h"
2021-01-03 23:53:49 +03:00
# include "lib/global_contexts.h"
2021-06-14 20:13:48 +03:00
# include "librpc/gen_ndr/ndr_winbind_c.h"
static NTSTATUS fake_password_policy ( struct winbindd_response * r ,
uint16_t validation_level ,
union netr_Validation * validation )
{
const struct netr_SamBaseInfo * bi = NULL ;
NTTIME min_password_age ;
NTTIME max_password_age ;
switch ( validation_level ) {
case 3 :
bi = & validation - > sam3 - > base ;
break ;
case 6 :
bi = & validation - > sam6 - > base ;
break ;
default :
return NT_STATUS_INTERNAL_ERROR ;
}
if ( bi - > allow_password_change > bi - > last_password_change ) {
min_password_age = bi - > allow_password_change -
bi - > last_password_change ;
} else {
min_password_age = 0 ;
}
if ( bi - > force_password_change > bi - > last_password_change ) {
max_password_age = bi - > force_password_change -
bi - > last_password_change ;
} else {
max_password_age = 0 ;
}
r - > data . auth . policy . min_length_password = 0 ;
r - > data . auth . policy . password_history = 0 ;
r - > data . auth . policy . password_properties = 0 ;
r - > data . auth . policy . expire =
nt_time_to_unix_abs ( & max_password_age ) ;
r - > data . auth . policy . min_passwordage =
nt_time_to_unix_abs ( & min_password_age ) ;
return NT_STATUS_OK ;
}
2010-03-29 19:52:38 +04:00
struct winbindd_pam_auth_state {
2021-06-14 20:13:48 +03:00
struct wbint_PamAuth * r ;
2023-10-21 14:24:01 +03:00
char * name_namespace ;
char * name_domain ;
char * name_user ;
2010-03-29 19:52:38 +04:00
} ;
static void winbindd_pam_auth_done ( struct tevent_req * subreq ) ;
struct tevent_req * winbindd_pam_auth_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct winbindd_cli_state * cli ,
struct winbindd_request * request )
{
struct tevent_req * req , * subreq ;
struct winbindd_pam_auth_state * state ;
struct winbindd_domain * domain ;
char * mapped = NULL ;
2023-10-21 14:24:01 +03:00
char * auth_user = NULL ;
2010-03-29 19:52:38 +04:00
NTSTATUS status ;
2018-04-26 18:32:42 +03:00
bool ok ;
2010-03-29 19:52:38 +04:00
req = tevent_req_create ( mem_ctx , & state ,
struct winbindd_pam_auth_state ) ;
if ( req = = NULL ) {
return NULL ;
}
2022-06-24 15:29:35 +03:00
D_NOTICE ( " [%s (%u)] Winbind external command PAM_AUTH start. \n "
" Authenticating user '%s'. \n " ,
cli - > client_name ,
( unsigned int ) cli - > pid ,
request - > data . auth . user ) ;
2010-03-29 19:52:38 +04:00
if ( ! check_request_flags ( request - > flags ) ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_PARAMETER_MIX ) ;
return tevent_req_post ( req , ev ) ;
}
/* Parse domain and username */
status = normalize_name_unmap ( state , request - > data . auth . user , & mapped ) ;
2010-07-29 23:44:00 +04:00
/* If the name normalization changed something, copy it over the given
name */
2010-03-29 19:52:38 +04:00
if ( NT_STATUS_IS_OK ( status )
| | NT_STATUS_EQUAL ( status , NT_STATUS_FILE_RENAMED ) ) {
2010-07-29 23:44:00 +04:00
fstrcpy ( request - > data . auth . user , mapped ) ;
2010-03-29 19:52:38 +04:00
}
2023-10-21 14:24:01 +03:00
auth_user = request - > data . auth . user ;
ok = canonicalize_username ( state ,
& auth_user ,
& state - > name_namespace ,
& state - > name_domain ,
& state - > name_user ) ;
2018-04-26 18:32:42 +03:00
if ( ! ok ) {
2010-03-29 19:52:38 +04:00
tevent_req_nterror ( req , NT_STATUS_NO_SUCH_USER ) ;
return tevent_req_post ( req , ev ) ;
}
2023-10-21 14:24:01 +03:00
fstrcpy ( request - > data . auth . user , auth_user ) ;
2021-06-14 20:13:48 +03:00
domain = find_auth_domain ( request - > flags , state - > name_namespace ) ;
2010-03-29 19:52:38 +04:00
if ( domain = = NULL ) {
tevent_req_nterror ( req , NT_STATUS_NO_SUCH_USER ) ;
return tevent_req_post ( req , ev ) ;
}
2021-06-14 20:13:48 +03:00
state - > r = talloc_zero ( state , struct wbint_PamAuth ) ;
if ( tevent_req_nomem ( state - > r , req ) ) {
return tevent_req_post ( req , ev ) ;
}
state - > r - > in . client_name = talloc_strdup (
state - > r , request - > client_name ) ;
if ( tevent_req_nomem ( state - > r , req ) ) {
return tevent_req_post ( req , ev ) ;
}
state - > r - > in . client_pid = request - > pid ;
state - > r - > in . flags = request - > flags ;
state - > r - > in . info = talloc_zero ( state - > r , struct wbint_AuthUserInfo ) ;
if ( tevent_req_nomem ( state - > r , req ) ) {
return tevent_req_post ( req , ev ) ;
}
state - > r - > in . info - > krb5_cc_type = talloc_strdup (
state - > r , request - > data . auth . krb5_cc_type ) ;
if ( tevent_req_nomem ( state - > r , req ) ) {
return tevent_req_post ( req , ev ) ;
}
state - > r - > in . info - > password = talloc_strdup (
state - > r , request - > data . auth . pass ) ;
if ( tevent_req_nomem ( state - > r , req ) ) {
return tevent_req_post ( req , ev ) ;
}
state - > r - > in . info - > username = talloc_strdup (
state - > r , request - > data . auth . user ) ;
if ( tevent_req_nomem ( state - > r , req ) ) {
return tevent_req_post ( req , ev ) ;
}
state - > r - > in . info - > uid = request - > data . auth . uid ;
status = extra_data_to_sid_array (
request - > data . auth . require_membership_of_sid ,
state - > r ,
& state - > r - > in . require_membership_of_sid ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
subreq = dcerpc_wbint_PamAuth_r_send ( state ,
global_event_context ( ) ,
dom_child_handle ( domain ) ,
state - > r ) ;
2010-03-29 19:52:38 +04:00
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , winbindd_pam_auth_done , req ) ;
return req ;
}
static void winbindd_pam_auth_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct winbindd_pam_auth_state * state = tevent_req_data (
req , struct winbindd_pam_auth_state ) ;
2021-06-14 20:13:48 +03:00
NTSTATUS status ;
2010-03-29 19:52:38 +04:00
2021-06-14 20:13:48 +03:00
status = dcerpc_wbint_PamAuth_r_recv ( subreq , state ) ;
2010-03-29 19:52:38 +04:00
TALLOC_FREE ( subreq ) ;
2021-06-14 20:13:48 +03:00
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
if ( tevent_req_nterror ( req , state - > r - > out . result ) ) {
2010-03-29 19:52:38 +04:00
return ;
}
2021-06-14 20:13:48 +03:00
2010-03-29 19:52:38 +04:00
tevent_req_done ( req ) ;
}
NTSTATUS winbindd_pam_auth_recv ( struct tevent_req * req ,
struct winbindd_response * response )
{
struct winbindd_pam_auth_state * state = tevent_req_data (
req , struct winbindd_pam_auth_state ) ;
NTSTATUS status ;
2022-06-24 15:29:35 +03:00
D_NOTICE ( " Winbind external command PAM_AUTH end. \n " ) ;
2010-03-29 19:52:38 +04:00
if ( tevent_req_is_nterror ( req , & status ) ) {
set_auth_errors ( response , status ) ;
return status ;
}
2021-06-14 20:13:48 +03:00
2010-03-29 19:52:38 +04:00
response - > result = WINBINDD_PENDING ;
2010-04-18 16:14:43 +04:00
2021-06-14 20:13:48 +03:00
status = append_auth_data ( response ,
response ,
state - > r - > in . flags ,
state - > r - > out . validation - > level ,
state - > r - > out . validation - > validation ,
state - > name_domain ,
state - > name_user ) ;
fstrcpy ( response - > data . auth . krb5ccname ,
state - > r - > out . validation - > krb5ccname ) ;
2010-04-18 16:14:43 +04:00
2021-06-14 20:13:48 +03:00
if ( state - > r - > in . flags & WBFLAG_PAM_INFO3_TEXT ) {
2017-11-29 12:55:25 +03:00
bool ok ;
ok = add_trusted_domain_from_auth (
2021-06-14 20:13:48 +03:00
state - > r - > out . validation - > level ,
& response - > data . auth . info3 ,
& response - > data . auth . info6 ) ;
2017-11-29 12:55:25 +03:00
if ( ! ok ) {
DBG_ERR ( " add_trusted_domain_from_auth failed \n " ) ;
set_auth_errors ( response , NT_STATUS_LOGON_FAILURE ) ;
return NT_STATUS_LOGON_FAILURE ;
}
}
2021-06-14 20:13:48 +03:00
if ( state - > r - > in . flags & WBFLAG_PAM_CACHED_LOGIN ) {
2010-04-18 16:14:43 +04:00
/* Store in-memory creds for single-signon using ntlm_auth. */
status = winbindd_add_memory_creds (
2021-06-14 20:13:48 +03:00
state - > r - > in . info - > username ,
state - > r - > in . info - > uid ,
state - > r - > in . info - > password ) ;
2022-06-24 15:29:35 +03:00
D_DEBUG ( " winbindd_add_memory_creds returned: %s \n " ,
nt_errstr ( status ) ) ;
2010-04-18 16:14:43 +04:00
}
2021-06-14 20:13:48 +03:00
if ( state - > r - > in . flags & WBFLAG_PAM_GET_PWD_POLICY ) {
/*
* WBFLAG_PAM_GET_PWD_POLICY is not used within
* any Samba caller anymore .
*
* We just fake this based on the effective values
* for the user , for legacy callers .
*/
status = fake_password_policy ( response ,
state - > r - > out . validation - > level ,
state - > r - > out . validation - > validation ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_ERR ( " Failed to fake password policy: %s \n " ,
nt_errstr ( status ) ) ;
set_auth_errors ( response , status ) ;
return status ;
}
}
return NT_STATUS_OK ;
2010-03-29 19:52:38 +04:00
}