*/
RCSID("$Id$")
-#include <freeradius-devel/radiusd.h>
-#include <freeradius-devel/modules.h>
-#include <freeradius-devel/rad_assert.h>
-
-/* krb5 includes */
-#include <krb5.h>
-
-#ifdef ET_COMM_ERR
-# include <et/com_err.h>
-#else
-# include <com_err.h>
-#endif
-
-/*
- * Work around bug in krb5_copy_context which attempts to copy the list
- * of tgs_kytpes and tkt_ktypes associated with a context... except by
- * default the pointers to those lists are NULL, and so it SEGVs
- *
- * The functions sigs below are not provided by krb5.h, but are available
- * in the library.
- */
-#if !defined(HEIMDAL_KRB5) && KRB5_IS_THREAD_SAFE
-krb5_error_code
-krb5_set_default_in_tkt_ktypes(krb5_context context, const krb5_enctype *etypes);
-
-krb5_error_code
-krb5_get_default_in_tkt_ktypes(krb5_context context, krb5_enctype **ktypes);
-
-krb5_error_code
-krb5_get_tgs_ktypes(krb5_context context, krb5_const_principal princ, krb5_enctype **ktypes);
-
-krb5_error_code
-krb5_set_default_tgs_ktypes(krb5_context context, const krb5_enctype *etypes);
-
-void
-krb5_free_ktypes(krb5_context context, krb5_enctype *val);
-#endif
-
-/** Instance configuration for rlm_krb5
- *
- * Holds the configuration and preparsed data for a instance of rlm_krb5.
- */
-typedef struct rlm_krb5_t {
- char const *xlat_name; //!< This module's instance name.
- char const *keytabname; //!< The keytab to resolve the service in.
- char const *service_princ; //!< The service name provided by the
- //!< config parser.
-
- char *hostname; //!< The hostname component of
- //!< service_princ, or NULL.
- char *service; //!< The service component of service_princ, or NULL.
-
- krb5_context context; //!< The kerberos context (cloned once per request).
-
-#ifndef HEIMDAL_KRB5
- krb5_get_init_creds_opt *gic_options; //!< Options to pass to the get_initial_credentials
- //!< function.
- krb5_verify_init_creds_opt *vic_options; //!< Options to pass to the validate_initial_creds
- //!< function.
-
- krb5_principal server; //!< A structure representing the parsed
- //!< service_princ.
-#endif
-
-} rlm_krb5_t;
+#include <freeradius-devel/radiusd.h>
+#include <freeradius-devel/modules.h>
+#include <freeradius-devel/rad_assert.h>
+#include "krb5.h"
static const CONF_PARSER module_config[] = {
- { "keytab", PW_TYPE_STRING_PTR, offsetof(rlm_krb5_t, keytabname), NULL, NULL },
- { "service_principal", PW_TYPE_STRING_PTR, offsetof(rlm_krb5_t,service_princ), NULL, NULL },
+ { "keytab", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_krb5_t, keytabname), NULL },
+ { "service_principal", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_krb5_t, service_princ), NULL },
{ NULL, -1, 0, NULL, NULL }
};
-static int krb5_detach(void *instance)
+static int mod_detach(void *instance)
{
rlm_krb5_t *inst = instance;
if (inst->gic_options) {
krb5_get_init_creds_opt_free(inst->context, inst->gic_options);
}
+
+ if (inst->server) {
+ krb5_free_principal(inst->context, inst->server);
+ }
#endif
/* Don't free hostname, it's just a pointer into service_princ */
if (inst->context) {
krb5_free_context(inst->context);
}
+#ifdef KRB5_IS_THREAD_SAFE
+ fr_connection_pool_delete(inst->pool);
+#endif
return 0;
}
-static int krb5_instantiate(CONF_SECTION *conf, void *instance)
+static int mod_instantiate(CONF_SECTION *conf, void *instance)
{
rlm_krb5_t *inst = instance;
krb5_error_code ret;
DEBUG("Using MIT Kerberos library");
#endif
-#ifndef KRB5_IS_THREAD_SAFE
if (!krb5_is_thread_safe()) {
- DEBUGI("libkrb5 is not threadsafe, recompile it with thread support enabled");
- WDEBUG("rlm_krb5 will run in single threaded mode, performance may be degraded");
+/*
+ * rlm_krb5 was built as threadsafe
+ */
+#ifdef KRB5_IS_THREAD_SAFE
+ ERROR("Build time libkrb5 was threadsafe, but run time library claims not to be");
+ ERROR("Modify runtime linker path (LD_LIBRARY_PATH on most systems), to prefer threadsafe libkrb5");
+ return -1;
+/*
+ * rlm_krb5 was not built as threadsafe
+ */
+#else
+ WARN("libkrb5 is not threadsafe, recompile it with thread support enabled ("
+# ifdef HEIMDAL_KRB5
+ "--enable-pthread-support"
+# else
+ "--disable-thread-support=no"
+# endif
+ ")");
+ WARN("rlm_krb5 will run in single threaded mode, performance may be degraded");
} else {
- WDEBUG("Build time libkrb5 was not threadsafe, but run time library claims to be");
- WDEBUG("Reconfigure and recompile rlm_krb5 to enable thread support");
- }
+ WARN("Build time libkrb5 was not threadsafe, but run time library claims to be");
+ WARN("Reconfigure and recompile rlm_krb5 to enable thread support");
#endif
+ }
inst->xlat_name = cf_section_name2(conf);
if (!inst->xlat_name) {
ret = krb5_init_context(&inst->context);
if (ret) {
- EDEBUG("rlm_krb5 (%s): Context initialisation failed: %s", inst->xlat_name, error_message(ret));
+ ERROR("rlm_krb5 (%s): context initialisation failed: %s", inst->xlat_name,
+ rlm_krb5_error(NULL, ret));
return -1;
}
- DEBUG("rlm_krb5 (%s): Context initialised successfully", inst->xlat_name);
-
/*
* Split service principal into service and host components
* they're needed to build the server principal in MIT,
#ifdef HEIMDAL_KRB5
if (inst->hostname) {
DEBUG("rlm_krb5 (%s): Ignoring hostname component of service principal \"%s\", not "
- "needed/supported by Heimdal", inst->xlat_name, inst->hostname);
+ "needed/supported by Heimdal", inst->xlat_name, inst->hostname);
}
#else
*/
ret = krb5_sname_to_principal(inst->context, inst->hostname, inst->service, KRB5_NT_SRV_HST, &(inst->server));
if (ret) {
- EDEBUG("rlm_krb5 (%s): Failed parsing service principal: %s", inst->xlat_name, error_message(ret));
+ ERROR("rlm_krb5 (%s): Failed parsing service principal: %s", inst->xlat_name,
+ rlm_krb5_error(inst->context, ret));
return -1;
}
ret = krb5_unparse_name(inst->context, inst->server, &princ_name);
if (ret) {
/* Uh? */
- EDEBUG("rlm_krb5 (%s): Failed constructing service principal string: %s", inst->xlat_name,
- error_message(ret));
+ ERROR("rlm_krb5 (%s): Failed constructing service principal string: %s", inst->xlat_name,
+ rlm_krb5_error(inst->context, ret));
return -1;
}
/* For some reason the 'init' version of this function is deprecated */
ret = krb5_get_init_creds_opt_alloc(inst->context, &(inst->gic_options));
if (ret) {
- EDEBUG("rlm_krb5 (%s): Couldn't allocated inital credential options: %s", inst->xlat_name,
- error_message(ret));
+ ERROR("rlm_krb5 (%s): Couldn't allocated inital credential options: %s", inst->xlat_name,
+ rlm_krb5_error(inst->context, ret));
return -1;
}
krb5_kt_resolve(inst->context, inst->keytabname, &keytab) :
krb5_kt_default(inst->context, &keytab);
if (ret) {
- EDEBUG("rlm_krb5 (%s): Resolving keytab failed: %s", inst->xlat_name, error_message(ret));
+ ERROR("rlm_krb5 (%s): Resolving keytab failed: %s", inst->xlat_name,
+ rlm_krb5_error(inst->context, ret));
return -1;
}
ret = krb5_kt_get_name(inst->context, keytab, keytab_name, sizeof(keytab_name));
krb5_kt_close(inst->context, keytab);
if (ret) {
- EDEBUG("rlm_krb5 (%s): Can't retrieve keytab name: %s", inst->xlat_name, error_message(ret));
+ ERROR("rlm_krb5 (%s): Can't retrieve keytab name: %s", inst->xlat_name,
+ rlm_krb5_error(inst->context, ret));
return -1;
}
DEBUG("rlm_krb5 (%s): Using keytab \"%s\"", inst->xlat_name, keytab_name);
MEM(inst->vic_options = talloc_zero(inst, krb5_verify_init_creds_opt));
-
krb5_verify_init_creds_opt_init(inst->vic_options);
- krb5_verify_init_creds_opt_set_ap_req_nofail(inst->vic_options, true);
+#endif
+#ifdef KRB5_IS_THREAD_SAFE
/*
- * Explicitly set enctypes to work around bug in krb5_copy_context
+ * Initialize the socket pool.
*/
- {
- krb5_enctype *enctypes;
-
- krb5_get_default_in_tkt_ktypes(inst->context, &enctypes);
- krb5_set_default_in_tkt_ktypes(inst->context, enctypes);
- krb5_free_ktypes(inst->context, enctypes);
-
- krb5_get_tgs_ktypes(inst->context, inst->server, &enctypes);
- krb5_set_default_tgs_ktypes(inst->context, enctypes);
- krb5_free_ktypes(inst->context, enctypes);
-
+ inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, NULL, NULL);
+ if (!inst->pool) {
+ return -1;
+ }
+#else
+ inst->conn = mod_conn_create(inst, inst);
+ if (!inst->conn) {
+ return -1;
}
#endif
-
return 0;
}
-static rlm_rcode_t krb5_parse_user(REQUEST *request, krb5_context context, krb5_principal *client)
+/** Common function for transforming a User-Name string into a principal.
+ *
+ * @param[out] client Where to write the client principal.
+ * @param[in] request Current request.
+ * @param[in] context Kerberos context.
+ */
+static rlm_rcode_t krb5_parse_user(krb5_principal *client, REQUEST *request, krb5_context context)
{
krb5_error_code ret;
char *princ_name;
*/
if (request->password->da->attr != PW_USER_PASSWORD) {
REDEBUG("Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".",
- request->password->da->name);
+ request->password->da->name);
return RLM_MODULE_INVALID;
}
ret = krb5_parse_name(context, request->username->vp_strvalue, client);
if (ret) {
- REDEBUG("Failed parsing username as principal: %s", error_message(ret));
+ REDEBUG("Failed parsing username as principal: %s", rlm_krb5_error(context, ret));
return RLM_MODULE_FAIL;
}
return RLM_MODULE_OK;
}
+/** Log error message and return appropriate rcode
+ *
+ * Translate kerberos error codes into return codes.
+ * @param request Current request.
+ * @param ret code from kerberos.
+ * @param conn used in the last operation.
+ */
+static rlm_rcode_t krb5_process_error(REQUEST *request, rlm_krb5_handle_t *conn, int ret)
+{
+ rad_assert(ret != 0);
+ rad_assert(conn); /* Silences warnings */
+
+ switch (ret) {
+ case KRB5_LIBOS_BADPWDMATCH:
+ case KRB5KRB_AP_ERR_BAD_INTEGRITY:
+ REDEBUG("Provided password was incorrect (%i): %s", ret, rlm_krb5_error(conn->context, ret));
+ return RLM_MODULE_REJECT;
+
+ case KRB5KDC_ERR_KEY_EXP:
+ case KRB5KDC_ERR_CLIENT_REVOKED:
+ case KRB5KDC_ERR_SERVICE_REVOKED:
+ REDEBUG("Account has been locked out (%i): %s", ret, rlm_krb5_error(conn->context, ret));
+ return RLM_MODULE_USERLOCK;
+
+ case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
+ RDEBUG("User not found (%i): %s", ret, rlm_krb5_error(conn->context, ret));
+ return RLM_MODULE_NOTFOUND;
+
+ default:
+ REDEBUG("Error verifying credentials (%i): %s", ret, rlm_krb5_error(conn->context, ret));
+ return RLM_MODULE_FAIL;
+ }
+}
+
#ifdef HEIMDAL_KRB5
/*
* Validate user/pass (Heimdal)
*/
-static rlm_rcode_t krb5_auth(void *instance, REQUEST *request)
+static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
{
rlm_krb5_t *inst = instance;
rlm_rcode_t rcode;
-
krb5_error_code ret;
- krb5_principal client;
- krb5_ccache ccache;
- krb5_keytab keytab;
- krb5_verify_opt options;
- krb5_context context;
+ rlm_krb5_handle_t *conn;
- rad_assert(inst->context);
+ krb5_principal client;
#ifdef KRB5_IS_THREAD_SAFE
- /*
- * See above in MIT krb5_auth
- */
- ret = krb5_copy_context(inst->context, &context);
- if (ret) {
- REDEBUG("Error cloning krb5 context: %s", error_message(ret));
-
- return RLM_MODULE_FAIL;
- }
+ conn = fr_connection_get(inst->pool);
+ if (!conn) return RLM_MODULE_FAIL;
#else
- context = inst->context;
+ conn = inst->conn;
#endif
/*
* Zero out local storage
*/
- memset(&keytab, 0, sizeof(keytab));
memset(&client, 0, sizeof(client));
+ rcode = krb5_parse_user(&client, request, conn->context);
+ if (rcode != RLM_MODULE_OK) goto cleanup;
+
/*
- * Setup krb5_verify_user options
- *
- * Not entirely sure this is necessary, but as we use context
- * to get the cache handle, we probably do have to do this with
- * the cloned context.
+ * Verify the user, using the options we set in instantiate
*/
- krb5_cc_default(context, &ccache);
-
- krb5_verify_opt_init(&options);
- krb5_verify_opt_set_ccache(&options, ccache);
- ret = inst->keytabname ?
- krb5_kt_resolve(context, inst->keytabname, &keytab) :
- krb5_kt_default(context, &keytab);
+ ret = krb5_verify_user_opt(conn->context, client, request->password->vp_strvalue, &conn->options);
if (ret) {
- REDEBUG("Resolving keytab failed: %s", error_message(ret));
- rcode = RLM_MODULE_FAIL;
-
+ rcode = krb5_process_error(request, conn, ret);
goto cleanup;
}
- krb5_verify_opt_set_keytab(&options, keytab);
- krb5_verify_opt_set_secure(&options, true);
-
- if (inst->service) {
- krb5_verify_opt_set_service(&options, inst->service);
- }
-
- rcode = krb5_parse_user(request, context, &client);
- if (rcode != RLM_MODULE_OK) goto cleanup;
-
/*
- * Verify the user, using the options we set in instantiate
+ * krb5_verify_user_opt adds the credentials to the ccache
+ * we specified with krb5_verify_opt_set_ccache.
+ *
+ * To make sure we don't accumulate thousands of sets of
+ * credentials, remove them again here.
+ *
+ * @todo This should definitely be optional, which means writing code for the MIT
+ * variant as well.
*/
- ret = krb5_verify_user_opt(context, client, request->password->vp_strvalue, &options);
- if (ret) {
- switch (ret) {
- case KRB5_LIBOS_BADPWDMATCH:
- case KRB5KRB_AP_ERR_BAD_INTEGRITY:
- REDEBUG("Provided password was incorrect (%i): %s", ret, error_message(ret));
- rcode = RLM_MODULE_REJECT;
-
- break;
- case KRB5KDC_ERR_KEY_EXP:
- case KRB5KDC_ERR_CLIENT_REVOKED:
- case KRB5KDC_ERR_SERVICE_REVOKED:
- REDEBUG("Account has been locked out (%i): %s", ret, error_message(ret));
- rcode = RLM_MODULE_USERLOCK;
-
- break;
- case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
- RDEBUG("User not found: %s (%i)", ret, error_message(ret));
- rcode = RLM_MODULE_NOTFOUND;
-
- default:
- REDEBUG("Error verifying credentials (%i): %s", ret, error_message(ret));
- rcode = RLM_MODULE_FAIL;
-
- break;
+ {
+ krb5_cc_cursor cursor;
+ krb5_creds cred;
+
+ krb5_cc_start_seq_get(conn->context, conn->ccache, &cursor);
+ for ((ret = krb5_cc_next_cred(conn->context, conn->ccache, &cursor, &cred));
+ ret == 0;
+ (ret = krb5_cc_next_cred(conn->context, conn->ccache, &cursor, &cred))) {
+ krb5_cc_remove_cred(conn->context, conn->ccache, 0, &cred);
}
-
- goto cleanup;
+ krb5_cc_end_seq_get(conn->context, conn->ccache, &cursor);
}
- cleanup:
+cleanup:
if (client) {
- krb5_free_principal(context, client);
- }
- if (keytab) {
- krb5_kt_close(context, keytab);
+ krb5_free_principal(conn->context, client);
}
+
#ifdef KRB5_IS_THREAD_SAFE
- krb5_free_context(context);
+ fr_connection_release(inst->pool, conn);
#endif
return rcode;
}
/*
* Validate userid/passwd (MIT)
*/
-static rlm_rcode_t krb5_auth(void *instance, REQUEST *request)
+static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
{
rlm_krb5_t *inst = instance;
rlm_rcode_t rcode;
krb5_error_code ret;
+ rlm_krb5_handle_t *conn;
+
krb5_principal client;
krb5_creds init_creds;
- krb5_keytab keytab; /* ktid */
- krb5_context context;
char *password; /* compiler warnings */
rad_assert(inst->context);
#ifdef KRB5_IS_THREAD_SAFE
- /*
- * All the snippets on threadsafety say that individual threads
- * must each use their own copy of context.
- *
- * As we don't have any per thread instantiation, we either have
- * to clone inst->context on every request, or use the connection
- * API.
- *
- * @todo Use the connection API (3.0 only).
- */
- ret = krb5_copy_context(inst->context, &context);
- if (ret) {
- REDEBUG("Error cloning krb5 context: %s", error_message(ret));
-
- return RLM_MODULE_FAIL;
- }
- rad_assert(context != NULL); /* tell coverity copy context copies it */
+ conn = fr_connection_get(inst->pool);
+ if (!conn) return RLM_MODULE_FAIL;
#else
- context = inst->context;
+ conn = inst->conn;
#endif
/*
* Zero out local storage
*/
- memset(&keytab, 0, sizeof(keytab));
memset(&client, 0, sizeof(client));
memset(&init_creds, 0, sizeof(init_creds));
* Check we have all the required VPs, and convert the username
* into a principal.
*/
- rcode = krb5_parse_user(request, context, &client);
+ rcode = krb5_parse_user(&client, request, conn->context);
if (rcode != RLM_MODULE_OK) goto cleanup;
/*
- * Setup the keytab
- */
- ret = inst->keytabname ?
- krb5_kt_resolve(context, inst->keytabname, &keytab) :
- krb5_kt_default(context, &keytab);
- if (ret) {
- REDEBUG("Resolving keytab failed: %s", error_message(ret));
-
- goto cleanup;
- }
-
- /*
* Retrieve the TGT from the TGS/KDC and check we can decrypt it.
*/
memcpy(&password, &request->password->vp_strvalue, sizeof(password));
- ret = krb5_get_init_creds_password(context, &init_creds, client, password,
+ RDEBUG("Retrieving and decrypting TGT");
+ ret = krb5_get_init_creds_password(conn->context, &init_creds, client, password,
NULL, NULL, 0, NULL, inst->gic_options);
- password = NULL;
if (ret) {
- error:
- switch (ret) {
- case KRB5_LIBOS_BADPWDMATCH:
- case KRB5KRB_AP_ERR_BAD_INTEGRITY:
- REDEBUG("Provided password was incorrect (%i): %s", ret, error_message(ret));
- rcode = RLM_MODULE_REJECT;
- break;
-
- case KRB5KDC_ERR_KEY_EXP:
- case KRB5KDC_ERR_CLIENT_REVOKED:
- case KRB5KDC_ERR_SERVICE_REVOKED:
- REDEBUG("Account has been locked out (%i): %s", ret, error_message(ret));
- rcode = RLM_MODULE_USERLOCK;
- break;
-
- case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
- REDEBUG("User not found (%i): %s", ret, error_message(ret));
- rcode = RLM_MODULE_NOTFOUND;
- break;
-
- default:
- REDEBUG("Error retrieving or verifying credentials (%i): %s", ret, error_message(ret));
- rcode = RLM_MODULE_FAIL;
- break;
- }
-
+ rcode = krb5_process_error(request, conn, ret);
goto cleanup;
}
- RDEBUG("Successfully retrieved and decrypted TGT");
-
- ret = krb5_verify_init_creds(context, &init_creds, inst->server, keytab, NULL, inst->vic_options);
- if (ret) goto error;
-
- cleanup:
-
- if (context) {
- if (client) {
- krb5_free_principal(context, client);
- }
- if (keytab) {
- krb5_kt_close(context, keytab);
- }
+ RDEBUG("Attempting to authenticate against service principal");
+ ret = krb5_verify_init_creds(conn->context, &init_creds, inst->server, conn->keytab, NULL, inst->vic_options);
+ if (ret) {
+ rcode = krb5_process_error(request, conn, ret);
+ }
- krb5_free_cred_contents(context, &init_creds);
+cleanup:
+ if (client) {
+ krb5_free_principal(conn->context, client);
+ }
+ krb5_free_cred_contents(conn->context, &init_creds);
#ifdef KRB5_IS_THREAD_SAFE
- krb5_free_context(context);
+ fr_connection_release(inst->pool, conn);
#endif
- }
-
return rcode;
}
module_t rlm_krb5 = {
RLM_MODULE_INIT,
"krb5",
- RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE
+ RLM_TYPE_HUP_SAFE
#ifdef KRB5_IS_THREAD_SAFE
| RLM_TYPE_THREAD_SAFE
#endif
,
sizeof(rlm_krb5_t),
module_config,
- krb5_instantiate, /* instantiation */
- krb5_detach, /* detach */
+ mod_instantiate, /* instantiation */
+ mod_detach, /* detach */
{
- krb5_auth, /* authenticate */
+ mod_authenticate, /* authenticate */
NULL, /* authorize */
NULL, /* pre-accounting */
NULL, /* accounting */