2006-02-04 01:19:41 +03:00
/*
Unix SMB / CIFS implementation .
Winbind daemon - cached credentials funcions
Copyright ( C ) Guenther Deschner 2005
2010-01-09 22:26:46 +03:00
2006-02-04 01:19:41 +03: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-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2006-02-04 01:19:41 +03:00
( at your option ) any later version .
2010-01-09 22:26:46 +03:00
2006-02-04 01:19:41 +03: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 .
2010-01-09 22:26:46 +03:00
2006-02-04 01:19:41 +03:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2006-02-04 01:19:41 +03:00
*/
# include "includes.h"
# include "winbindd.h"
2009-03-16 13:27:58 +03:00
# include "../libcli/auth/libcli_auth.h"
2010-10-12 08:27:50 +04:00
# include "../libcli/security/security.h"
2016-11-29 18:41:27 +03:00
# include "libsmb/samlogon_cache.h"
2006-02-04 01:19:41 +03:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_WINBIND
# define MAX_CACHED_LOGINS 10
NTSTATUS winbindd_get_creds ( struct winbindd_domain * domain ,
TALLOC_CTX * mem_ctx ,
2010-05-21 05:25:01 +04:00
const struct dom_sid * sid ,
2008-02-17 04:08:12 +03:00
struct netr_SamInfo3 * * info3 ,
2015-05-03 07:07:06 +03:00
const uint8_t * cached_nt_pass [ NT_HASH_LEN ] ,
const uint8_t * cred_salt [ NT_HASH_LEN ] )
2006-02-04 01:19:41 +03:00
{
2008-02-17 04:08:12 +03:00
struct netr_SamInfo3 * info ;
2006-02-04 01:19:41 +03:00
NTSTATUS status ;
2006-08-20 07:53:42 +04:00
status = wcache_get_creds ( domain , mem_ctx , sid , cached_nt_pass , cred_salt ) ;
2006-02-04 01:19:41 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
info = netsamlogon_cache_get ( mem_ctx , sid ) ;
if ( info = = NULL ) {
return NT_STATUS_OBJECT_NAME_NOT_FOUND ;
}
* info3 = info ;
return NT_STATUS_OK ;
}
NTSTATUS winbindd_store_creds ( struct winbindd_domain * domain ,
const char * user ,
const char * pass ,
2010-09-09 03:58:21 +04:00
struct netr_SamInfo3 * info3 )
2006-02-04 01:19:41 +03:00
{
NTSTATUS status ;
uchar nt_pass [ NT_HASH_LEN ] ;
2010-05-21 05:25:01 +04:00
struct dom_sid cred_sid ;
2006-02-04 01:19:41 +03:00
if ( info3 ! = NULL ) {
2009-08-03 00:28:49 +04:00
2010-01-09 21:01:35 +03:00
sid_compose ( & cred_sid , info3 - > base . domain_sid ,
info3 - > base . rid ) ;
2008-02-17 04:08:12 +03:00
info3 - > base . user_flags | = NETLOGON_CACHED_ACCOUNT ;
2009-08-03 00:28:49 +04:00
2006-02-04 01:19:41 +03:00
} else if ( user ! = NULL ) {
2009-08-03 00:28:49 +04:00
2006-02-04 01:19:41 +03:00
/* do lookup ourself */
2006-09-08 18:28:06 +04:00
enum lsa_SidType type ;
2009-08-03 00:28:49 +04:00
2018-04-26 13:17:12 +03:00
if ( ! lookup_cached_name ( domain - > name , /* namespace */
domain - > name ,
2006-02-04 01:19:41 +03:00
user ,
& cred_sid ,
& type ) ) {
return NT_STATUS_NO_SUCH_USER ;
}
} else {
return NT_STATUS_INVALID_PARAMETER ;
}
2009-08-03 00:28:49 +04:00
2006-02-04 01:19:41 +03:00
if ( pass ) {
int count = 0 ;
status = wcache_count_cached_creds ( domain , & count ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
DEBUG ( 11 , ( " we have %d cached creds \n " , count ) ) ;
if ( count + 1 > MAX_CACHED_LOGINS ) {
DEBUG ( 10 , ( " need to delete the oldest cached login \n " ) ) ;
status = wcache_remove_oldest_cached_creds ( domain , & cred_sid ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 10 , ( " failed to remove oldest cached cred: %s \n " ,
nt_errstr ( status ) ) ) ;
return status ;
}
}
E_md4hash ( pass , nt_pass ) ;
2007-08-15 00:43:02 +04:00
dump_data_pw ( " nt_pass " , nt_pass , NT_HASH_LEN ) ;
2006-02-04 01:19:41 +03:00
2010-09-09 06:09:21 +04:00
status = wcache_save_creds ( domain , & cred_sid , nt_pass ) ;
2006-02-04 01:19:41 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
}
if ( info3 ! = NULL & & user ! = NULL ) {
if ( ! netsamlogon_cache_store ( user , info3 ) ) {
return NT_STATUS_ACCESS_DENIED ;
}
}
return NT_STATUS_OK ;
}
NTSTATUS winbindd_update_creds_by_info3 ( struct winbindd_domain * domain ,
const char * user ,
const char * pass ,
2008-02-17 04:08:12 +03:00
struct netr_SamInfo3 * info3 )
2006-02-04 01:19:41 +03:00
{
2010-09-09 06:12:21 +04:00
return winbindd_store_creds ( domain , user , pass , info3 ) ;
2006-02-04 01:19:41 +03:00
}
NTSTATUS winbindd_update_creds_by_name ( struct winbindd_domain * domain ,
const char * user ,
const char * pass )
{
2010-09-09 06:12:21 +04:00
return winbindd_store_creds ( domain , user , pass , NULL ) ;
2006-02-04 01:19:41 +03:00
}