xrdp/sesman/verify_user_kerberos.c

246 lines
6.3 KiB
C
Raw Normal View History

/**
* xrdp: A Remote Desktop Protocol server.
*
2013-06-08 21:51:53 +04:00
* Copyright (C) Jay Sorg 2004-2013
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2005-11-16 07:32:21 +03:00
2006-05-26 00:34:32 +04:00
/**
*
* @file verify_user_kerberos.c
* @brief Authenticate user using kerberos
* @author Jay Sorg
*
2006-05-26 00:34:32 +04:00
*/
#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif
2005-11-16 07:32:21 +03:00
#include "arch.h"
#include "auth.h"
2005-11-16 07:32:21 +03:00
#include "os_calls.h"
2020-12-29 12:48:01 +03:00
#include "string_calls.h"
2022-12-02 19:30:53 +03:00
#include "log.h"
2005-11-16 07:32:21 +03:00
#include <krb5.h>
2022-12-02 19:30:53 +03:00
struct auth_info
2005-11-16 07:32:21 +03:00
{
krb5_context ctx;
krb5_ccache cc;
krb5_principal me;
2005-11-16 07:32:21 +03:00
};
2022-12-02 19:30:53 +03:00
/******************************************************************************/
/* Logs a kerberos error code */
static void
log_kerberos_failure(krb5_context ctx, krb5_error_code code, const char *where)
{
2022-12-02 19:30:53 +03:00
const char *errstr = krb5_get_error_message(ctx, code);
LOG(LOG_LEVEL_ERROR, "Kerberos call to %s failed [%s]", where, errstr);
krb5_free_error_message(ctx, errstr);
}
2005-11-16 07:32:21 +03:00
/******************************************************************************/
2022-12-02 19:30:53 +03:00
int
auth_end(struct auth_info *auth_info)
2005-11-16 07:32:21 +03:00
{
2022-12-02 19:30:53 +03:00
if (auth_info != NULL)
2005-11-16 07:32:21 +03:00
{
2022-12-02 19:30:53 +03:00
if (auth_info->me)
{
2022-12-02 19:30:53 +03:00
krb5_free_principal(auth_info->ctx, auth_info->me);
}
2022-12-02 19:30:53 +03:00
if (auth_info->cc)
{
2022-12-02 19:30:53 +03:00
krb5_cc_close(auth_info->ctx, auth_info->cc);
}
2022-12-02 19:30:53 +03:00
if (auth_info->ctx)
{
2022-12-02 19:30:53 +03:00
krb5_free_context(auth_info->ctx);
}
2022-12-02 19:30:53 +03:00
g_memset(auth_info, 0, sizeof(*auth_info));
g_free(auth_info);
}
2022-12-02 19:30:53 +03:00
return 0;
2005-11-16 07:32:21 +03:00
}
/******************************************************************************/
2022-12-02 19:30:53 +03:00
/* Checks Kerberos can be used
*
* If all is well, an auth_info struct is returned */
static struct auth_info *
k5_begin(const char *username)
2005-11-16 07:32:21 +03:00
{
2022-12-02 19:30:53 +03:00
int ok = 0;
struct auth_info *auth_info = g_new0(struct auth_info, 1);
krb5_error_code code;
if (auth_info == NULL)
{
2022-12-02 19:30:53 +03:00
LOG(LOG_LEVEL_ERROR, "Out of memory in k5_begin()");
}
2022-12-02 19:30:53 +03:00
else if ((code = krb5_init_context(&auth_info->ctx)) != 0)
{
2022-12-02 19:30:53 +03:00
LOG(LOG_LEVEL_ERROR, "Can't init Kerberos context");
}
2022-12-02 19:30:53 +03:00
/* Determine the credentials cache to use */
else if ((code = krb5_cc_default(auth_info->ctx, &auth_info->cc)) != 0)
{
2022-12-02 19:30:53 +03:00
log_kerberos_failure(auth_info->ctx, code, "krb5_cc_default");
}
2022-12-02 19:30:53 +03:00
/* Parse the username into a full principal */
else if ((code = krb5_parse_name(auth_info->ctx,
username, &auth_info->me)) != 0)
{
2022-12-02 19:30:53 +03:00
log_kerberos_failure(auth_info->ctx, code, "krb5_parse_name");
}
else
{
ok = 1;
}
2022-12-02 19:30:53 +03:00
if (!ok)
2005-11-16 07:32:21 +03:00
{
2022-12-02 19:30:53 +03:00
auth_end(auth_info);
auth_info = NULL;
2005-11-16 07:32:21 +03:00
}
2022-12-02 19:30:53 +03:00
return auth_info;
2005-11-16 07:32:21 +03:00
}
2022-12-02 19:30:53 +03:00
2005-11-16 07:32:21 +03:00
/******************************************************************************/
/* returns boolean */
static enum scp_login_status
2022-12-02 19:30:53 +03:00
k5_kinit(struct auth_info *auth_info, const char *password)
2005-11-16 07:32:21 +03:00
{
enum scp_login_status status = E_SCP_LOGIN_GENERAL_ERROR;
krb5_creds my_creds;
krb5_error_code code = 0;
2022-12-02 19:30:53 +03:00
code = krb5_get_init_creds_password(auth_info->ctx,
&my_creds, auth_info->me,
password, NULL, NULL,
0,
NULL,
NULL);
if (code != 0)
2005-11-16 07:32:21 +03:00
{
2022-12-02 19:30:53 +03:00
log_kerberos_failure(auth_info->ctx, code,
"krb5_get_init_creds_password");
status = E_SCP_LOGIN_NOT_AUTHENTICATED;
}
2022-12-02 19:30:53 +03:00
else
{
2022-12-02 19:30:53 +03:00
/*
* Try to store the creds in the credentials cache
*/
if ((code = krb5_cc_initialize(auth_info->ctx, auth_info->cc,
auth_info->me)) != 0)
{
2022-12-02 19:30:53 +03:00
log_kerberos_failure(auth_info->ctx, code, "krb5_cc_initialize");
}
2022-12-02 19:30:53 +03:00
else if ((code = krb5_cc_store_cred(auth_info->ctx, auth_info->cc,
&my_creds)) != 0)
{
2022-12-02 19:30:53 +03:00
log_kerberos_failure(auth_info->ctx, code, "krb5_cc_store_cred");
}
2022-12-02 19:30:53 +03:00
else
{
status = E_SCP_LOGIN_OK;
}
2022-12-02 19:30:53 +03:00
/* Prevent double-free of the client principal */
if (my_creds.client == auth_info->me)
{
2022-12-02 19:30:53 +03:00
my_creds.client = NULL;
}
2022-12-02 19:30:53 +03:00
krb5_free_cred_contents(auth_info->ctx, &my_creds);
}
return status;
2005-11-16 07:32:21 +03:00
}
/******************************************************************************/
/* returns non-NULL for success */
struct auth_info *
auth_userpass(const char *user, const char *pass,
const char *client_ip, enum scp_login_status *errorcode)
2005-11-16 07:32:21 +03:00
{
enum scp_login_status status = E_SCP_LOGIN_GENERAL_ERROR;
2022-12-02 19:30:53 +03:00
struct auth_info *auth_info = k5_begin(user);
2022-12-02 19:30:53 +03:00
if (auth_info)
{
status = k5_kinit(auth_info, pass);
if (status != E_SCP_LOGIN_OK)
{
2022-12-02 19:30:53 +03:00
auth_end(auth_info);
auth_info = NULL;
}
2022-12-02 19:30:53 +03:00
}
if (errorcode != NULL)
{
*errorcode = status;
}
return auth_info;
}
/******************************************************************************/
/* returns non-NULL for success */
struct auth_info *
auth_uds(const char *user, enum scp_login_status *errorcode)
{
struct auth_info *auth_info = k5_begin(user);
if (errorcode != NULL)
{
*errorcode =
(auth_info != NULL) ? E_SCP_LOGIN_OK : E_SCP_LOGIN_GENERAL_ERROR;
}
return auth_info;
2005-11-16 07:32:21 +03:00
}
/******************************************************************************/
/* returns error */
2017-03-12 19:35:00 +03:00
int
auth_start_session(struct auth_info *auth_info, int display_num)
2005-11-16 07:32:21 +03:00
{
return 0;
2005-11-16 07:32:21 +03:00
}
2013-10-01 21:42:00 +04:00
/******************************************************************************/
/* returns error */
2017-03-12 19:35:00 +03:00
int
auth_stop_session(struct auth_info *auth_info)
2013-10-01 21:42:00 +04:00
{
return 0;
}
2005-11-16 07:32:21 +03:00
/******************************************************************************/
2017-03-12 19:35:00 +03:00
int
auth_set_env(struct auth_info *auth_info)
2005-11-16 07:32:21 +03:00
{
return 0;
2005-11-16 07:32:21 +03:00
}