#include "includes.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_AUTH
extern BOOL global_machine_password_needs_changing;
static NTSTATUS connect_to_domain_password_server(struct cli_state **cli,
const char *domain,
const char *dc_name,
struct in_addr dc_ip,
struct rpc_pipe_client **pipe_ret,
BOOL *retry)
{
NTSTATUS result;
struct rpc_pipe_client *netlogon_pipe = NULL;
*cli = NULL;
*pipe_ret = NULL;
if (!grab_server_mutex(dc_name)) {
return NT_STATUS_NO_LOGON_SERVERS;
}
*retry = True;
result = cli_full_connection(cli, global_myname(), dc_name, &dc_ip, 0,
"IPC$", "IPC", "", "", "", 0, Undefined, retry);
if (!NT_STATUS_IS_OK(result)) {
if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
result = NT_STATUS_NO_LOGON_SERVERS;
}
if (*cli) {
cli_shutdown(*cli);
*cli = NULL;
}
release_server_mutex();
return result;
}
if (lp_client_schannel()) {
netlogon_pipe = cli_rpc_pipe_open_schannel(*cli, PI_NETLOGON,
PIPE_AUTH_LEVEL_PRIVACY, domain, &result);
} else {
netlogon_pipe = cli_rpc_pipe_open_noauth(*cli, PI_NETLOGON, &result);
}
if(!netlogon_pipe) {
DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
machine %s. Error was : %s.\n", dc_name, nt_errstr(result)));
cli_shutdown(*cli);
*cli = NULL;
release_server_mutex();
return result;
}
if (!lp_client_schannel()) {
uint32 neg_flags = NETLOGON_NEG_AUTH2_FLAGS;
uint32 sec_chan_type = 0;
unsigned char machine_pwd[16];
if (!get_trust_pw(domain, machine_pwd, &sec_chan_type)) {
DEBUG(0, ("connect_to_domain_password_server: could not fetch "
"trust account password for domain '%s'\n",
domain));
cli_shutdown(*cli);
*cli = NULL;
release_server_mutex();
return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
}
result = rpccli_netlogon_setup_creds(netlogon_pipe,
dc_name,
domain,
global_myname(),
global_myname(),
machine_pwd,
sec_chan_type,
&neg_flags);
if (!NT_STATUS_IS_OK(result)) {
cli_shutdown(*cli);
*cli = NULL;
release_server_mutex();
return result;
}
}
if(!netlogon_pipe) {
DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
machine %s. Error was : %s.\n", dc_name, cli_errstr(*cli)));
cli_shutdown(*cli);
*cli = NULL;
release_server_mutex();
return NT_STATUS_NO_LOGON_SERVERS;
}
*pipe_ret = netlogon_pipe;
return NT_STATUS_OK;
}
static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
const auth_usersupplied_info *user_info,
const char *domain,
uchar chal[8],
auth_serversupplied_info **server_info,
const char *dc_name,
struct in_addr dc_ip)
{
NET_USER_INFO_3 info3;
struct cli_state *cli = NULL;
struct rpc_pipe_client *netlogon_pipe = NULL;
NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
int i;
BOOL retry = True;
for (i = 0; !NT_STATUS_IS_OK(nt_status) && retry && (i < 3); i++) {
nt_status = connect_to_domain_password_server(&cli,
domain,
dc_name,
dc_ip,
&netlogon_pipe,
&retry);
}
if ( !NT_STATUS_IS_OK(nt_status) ) {
DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
}
return nt_status;
}
saf_store( domain, cli->desthost );
ZERO_STRUCT(info3);
nt_status = rpccli_netlogon_sam_network_logon(netlogon_pipe,
mem_ctx,
user_info->logon_parameters,
dc_name,
user_info->smb_name,
user_info->client_domain,
user_info->wksta_name,
chal,
user_info->lm_resp,
user_info->nt_resp,
&info3);
release_server_mutex();
if (!NT_STATUS_IS_OK(nt_status)) {
DEBUG(0,("domain_client_validate: unable to validate password "
"for user %s in domain %s to Domain controller %s. "
"Error was %s.\n", user_info->smb_name,
user_info->domain, dc_name,
nt_errstr(nt_status)));
if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
nt_status = NT_STATUS_NO_LOGON_SERVERS;
}
} else {
nt_status = make_server_info_info3(mem_ctx,
user_info->smb_name,
domain,
server_info,
&info3);
if (NT_STATUS_IS_OK(nt_status)) {
(*server_info)->was_mapped |= user_info->was_mapped;
if ( ! (*server_info)->guest) {
nt_status = smb_pam_accountcheck((*server_info)->unix_name);
if ( !NT_STATUS_IS_OK(nt_status)) {
DEBUG(1, ("PAM account restriction prevents user login\n"));
cli_shutdown(cli);
return nt_status;
}
}
}
netsamlogon_cache_store( user_info->smb_name, &info3 );
}
cli_shutdown(cli);
return nt_status;
}
static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
void *my_private_data,
TALLOC_CTX *mem_ctx,
const auth_usersupplied_info *user_info,
auth_serversupplied_info **server_info)
{
NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
const char *domain = lp_workgroup();
fstring dc_name;
struct in_addr dc_ip;
if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
"ntdomain auth method when not a member of a domain.\n"));
return NT_STATUS_NOT_IMPLEMENTED;
}
if (!user_info || !server_info || !auth_context) {
DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
return NT_STATUS_INVALID_PARAMETER;
}
if(strequal(get_global_sam_name(), user_info->domain)) {
DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
return NT_STATUS_NOT_IMPLEMENTED;
}
if ( !get_dc_name(domain, NULL, dc_name, &dc_ip) ) {
DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
user_info->domain));
return NT_STATUS_NO_LOGON_SERVERS;
}
nt_status = domain_client_validate(mem_ctx,
user_info,
domain,
(uchar *)auth_context->challenge.data,
server_info,
dc_name,
dc_ip);
return nt_status;
}
static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
{
if (!make_auth_methods(auth_context, auth_method)) {
return NT_STATUS_NO_MEMORY;
}
(*auth_method)->name = "ntdomain";
(*auth_method)->auth = check_ntdomain_security;
return NT_STATUS_OK;
}
static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
void *my_private_data,
TALLOC_CTX *mem_ctx,
const auth_usersupplied_info *user_info,
auth_serversupplied_info **server_info)
{
NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
unsigned char trust_md4_password[16];
char *trust_password;
time_t last_change_time;
DOM_SID sid;
fstring dc_name;
struct in_addr dc_ip;
if (!user_info || !server_info || !auth_context) {
DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
return NT_STATUS_INVALID_PARAMETER;
}
if( strequal(get_global_sam_name(), user_info->domain)) {
DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
user_info->domain));
return NT_STATUS_NOT_IMPLEMENTED;
}
if ( !is_trusted_domain( user_info->domain ) )
return NT_STATUS_NOT_IMPLEMENTED;
if (!secrets_fetch_trusted_domain_password(user_info->domain, &trust_password,
&sid, &last_change_time)) {
DEBUG(0, ("check_trustdomain_security: could not fetch trust "
"account password for domain %s\n",
user_info->domain));
return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
}
#ifdef DEBUG_PASSWORD
DEBUG(100, ("Trust password for domain %s is %s\n", user_info->domain,
trust_password));
#endif
E_md4hash(trust_password, trust_md4_password);
SAFE_FREE(trust_password);
#if 0
if (time(NULL) > last_change_time + (time_t)lp_machine_password_timeout())
{
global_machine_password_needs_changing = True;
}
#endif
if ( !get_dc_name(user_info->domain, NULL, dc_name, &dc_ip) ) {
DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
user_info->domain));
return NT_STATUS_NO_LOGON_SERVERS;
}
nt_status = domain_client_validate(mem_ctx,
user_info,
user_info->domain,
(uchar *)auth_context->challenge.data,
server_info,
dc_name,
dc_ip);
return nt_status;
}
static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
{
if (!make_auth_methods(auth_context, auth_method)) {
return NT_STATUS_NO_MEMORY;
}
(*auth_method)->name = "trustdomain";
(*auth_method)->auth = check_trustdomain_security;
return NT_STATUS_OK;
}
NTSTATUS auth_domain_init(void)
{
smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
return NT_STATUS_OK;
}