2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * @brief Authenticate users, retrieving their TGT from a Kerberos V5 TDC.
22 * @copyright 2000,2006,2012-2013 The FreeRADIUS server project
23 * @copyright 2013 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
24 * @copyright 2000 Nathan Neulinger <nneul@umr.edu>
25 * @copyright 2000 Alan DeKok <aland@ox.org>
27 #include <freeradius-devel/ident.h>
30 #include <freeradius-devel/radiusd.h>
31 #include <freeradius-devel/modules.h>
32 #include <freeradius-devel/rad_assert.h>
38 /** Instance configuration for rlm_krb5
40 * Holds the configuration and preparsed data for a instance of rlm_krb5.
42 typedef struct rlm_krb5_t {
43 const char *xlat_name; //!< This module's instance name.
44 const char *keytabname; //!< The keytab to resolve the service in.
45 const char *service_princ; //!< The service name provided by the
48 char *hostname; //!< The hostname component of
49 //!< service_princ, or NULL.
50 char *service; //!< The service component of
51 //!< service_princ, or NULL.
53 krb5_context context; //!< The kerberos context (cloned once per
57 krb5_get_init_creds_opt *gic_options; //!< Options to pass to the get_initial_credentials.
59 krb5_verify_init_creds_opt *vic_options; //!< Options to pass to the validate_initial_creds
62 krb5_principal server; //!< A structure representing the parsed
68 static const CONF_PARSER module_config[] = {
69 { "keytab", PW_TYPE_STRING_PTR, offsetof(rlm_krb5_t, keytabname), NULL, NULL },
70 { "service_principal", PW_TYPE_STRING_PTR, offsetof(rlm_krb5_t,service_princ), NULL, NULL },
71 { NULL, -1, 0, NULL, NULL }
74 static int krb5_detach(void *instance)
76 rlm_krb5_t *inst = instance;
79 talloc_free(inst->vic_options);
81 if (inst->gic_options) {
82 krb5_get_init_creds_opt_free(inst->context, inst->gic_options);
86 /* Don't free hostname, it's just a pointer into service_princ */
87 talloc_free(inst->service);
90 krb5_free_context(inst->context);
96 static int krb5_instantiate(CONF_SECTION *conf, void *instance)
98 rlm_krb5_t *inst = instance;
101 char keytab_name[200];
105 DEBUG("Using Heimdal Kerberos library");
107 DEBUG("Using MIT Kerberos library");
110 #ifndef KRB5_IS_THREAD_SAFE
111 if (!krb5_is_thread_safe()) {
112 DEBUGW("libkrb5 is not threadsafe, recompile it with thread support enabled");
113 DEBUGW("rlm_krb5 will run in single threaded mode");
115 DEBUGW("Build time krb5 library was not threadsafe, but run time library claims to be");
116 DEBUGW("Reconfigure and recompile rlm_krb5 to enable thread support");
120 inst->xlat_name = cf_section_name2(conf);
121 if (!inst->xlat_name) {
122 inst->xlat_name = cf_section_name1(conf);
125 ret = krb5_init_context(&inst->context);
127 DEBUGE("rlm_krb5 (%s): Context initialisation failed: %s", inst->xlat_name, error_message(ret));
132 DEBUG("rlm_krb5 (%s): Context initialised successfully", inst->xlat_name);
135 * Split service principal into service and host components
136 * they're needed to build the server principal in MIT,
137 * and to set the validation service in Heimdal.
139 if (inst->service_princ) {
141 /* Service principal appears to contain a host component */
142 inst->hostname = strchr(inst->service_princ, '/');
143 if (inst->hostname) {
144 len = (inst->hostname - inst->service_princ);
147 len = strlen(inst->service_princ);
151 inst->service = talloc_array(inst, char, (len + 1));
152 strlcpy(inst->service, inst->service_princ, len + 1);
157 if (inst->hostname) {
158 DEBUG("rlm_krb5 (%s): Ignoring hostname component of service principal \"%s\", not "
159 "needed/supported by Heimdal", inst->xlat_name, inst->hostname);
164 * Convert the service principal string to a krb5 principal.
166 ret = krb5_sname_to_principal(inst->context, inst->hostname, inst->service, KRB5_NT_SRV_HST, &(inst->server));
168 DEBUGE("rlm_krb5 (%s): Failed parsing service principal: %s", inst->xlat_name, error_message(ret));
173 ret = krb5_unparse_name(inst->context, inst->server, &princ_name);
176 DEBUGE("rlm_krb5 (%s): Failed constructing service principal string: %s", inst->xlat_name,
183 * Not necessarily the same as the config item
185 DEBUG("rlm_krb5 (%s): Using service principal \"%s\"", inst->xlat_name, princ_name);
187 krb5_free_unparsed_name(inst->context, princ_name);
190 * Setup options for getting credentials and verifying them
193 /* For some reason the 'init' version of this function is deprecated */
194 ret = krb5_get_init_creds_opt_alloc(inst->context, &(inst->gic_options));
196 DEBUGE("rlm_krb5 (%s): Couldn't allocated inital credential options: %s", inst->xlat_name,
203 * Perform basic checks on the keytab
205 ret = inst->keytabname ?
206 krb5_kt_resolve(inst->context, inst->keytabname, &keytab) :
207 krb5_kt_default(inst->context, &keytab);
209 DEBUGE("rlm_krb5 (%s): Resolving keytab failed: %s", inst->xlat_name, error_message(ret));
214 ret = krb5_kt_get_name(inst->context, keytab, keytab_name, sizeof(keytab_name));
215 krb5_kt_close(inst->context, keytab);
217 DEBUGE("rlm_krb5 (%s): Can't retrieve keytab name: %s", inst->xlat_name, error_message(ret));
222 DEBUG("rlm_krb5 (%s): Using keytab \"%s\"", inst->xlat_name, keytab_name);
224 MEM(inst->vic_options = talloc_zero(inst, krb5_verify_init_creds_opt));
226 krb5_verify_init_creds_opt_init(inst->vic_options);
227 krb5_verify_init_creds_opt_set_ap_req_nofail(inst->vic_options, TRUE);
234 static rlm_rcode_t krb5_parse_user(REQUEST *request, krb5_context context, krb5_principal *client)
240 * We can only authenticate user requests which HAVE
241 * a User-Name attribute.
243 if (!request->username) {
244 RDEBUGE("Attribute \"User-Name\" is required for authentication");
246 return RLM_MODULE_INVALID;
250 * We can only authenticate user requests which HAVE
251 * a User-Password attribute.
253 if (!request->password) {
254 RDEBUGE("Attribute \"User-Password\" is required for authentication");
256 return RLM_MODULE_INVALID;
260 * Ensure that we're being passed a plain-text password,
261 * and not anything else.
263 if (request->password->da->attr != PW_USER_PASSWORD) {
264 RDEBUGE("Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".",
265 request->password->da->name);
267 return RLM_MODULE_INVALID;
270 ret = krb5_parse_name(context, request->username->vp_strvalue, client);
272 RDEBUGE("Failed parsing username as principal: %s", error_message(ret));
274 return RLM_MODULE_FAIL;
277 krb5_unparse_name(context, *client, &princ_name);
278 RDEBUG("Using client principal \"%s\"", princ_name);
279 krb5_free_unparsed_name(context, princ_name);
281 return RLM_MODULE_OK;
287 * Validate user/pass (Heimdal)
289 static rlm_rcode_t krb5_auth(void *instance, REQUEST *request)
291 rlm_krb5_t *inst = instance;
296 krb5_principal client;
299 krb5_verify_opt options;
300 krb5_context context;
302 rad_assert(inst->context);
304 #ifdef KRB5_IS_THREAD_SAFE
306 * See above in MIT krb5_auth
308 ret = krb5_copy_context(inst->context, &context);
310 RDEBUGE("Error cloning krb5 context: %s", error_message(ret));
312 return RLM_MODULE_FAIL;
315 context = inst->context;
319 * Zero out local storage
321 memset(&keytab, 0, sizeof(keytab));
322 memset(&client, 0, sizeof(client));
323 memset(&init_creds, 0, sizeof(init_creds));
326 * Setup krb5_verify_user options
328 * Not entirely sure this is necessary, but as we use context
329 * to get the cache handle, we probably do have to do this with
330 * the cloned context.
332 krb5_cc_default(context, &ccache);
334 krb5_verify_opt_init(&options);
335 krb5_verify_opt_set_ccache(&options, ccache);
336 ret = inst->keytabname ?
337 krb5_kt_resolve(context, inst->keytabname, &keytab) :
338 krb5_kt_default(context, &keytab);
340 RDEBUGE("Resolving keytab failed: %s", error_message(ret));
345 krb5_verify_opt_set_keytab(&options, keytab);
346 krb5_verify_opt_set_secure(&options, TRUE);
349 krb5_verify_opt_set_service(&options, inst->service);
352 rcode = krb5_parse_user(request, context, &client);
353 if (rcode != RLM_MODULE_OK) goto cleanup;
356 * Verify the user, using the options we set in instantiate
358 ret = krb5_verify_user_opt(context, client, request->password->vp_strvalue, &options);
361 case KRB5_LIBOS_BADPWDMATCH:
362 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
363 RDEBUGE("Provided password was incorrect (%i): %s", ret, error_message(ret));
364 rcode = RLM_MODULE_REJECT;
367 case KRB5KDC_ERR_KEY_EXP:
368 case KRB5KDC_ERR_CLIENT_REVOKED:
369 case KRB5KDC_ERR_SERVICE_REVOKED:
370 RDEBUGE("Account has been locked out (%i): %s", ret, error_message(ret));
371 rcode = RLM_MODULE_USERLOCK;
374 case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
375 RDEBUG("User not found: %s (%i)", ret, error_message(ret));
376 rcode = RLM_MODULE_NOTFOUND;
379 RDEBUGE("Error verifying credentials (%i): %s", ret, error_message(ret));
380 rcode = RLM_MODULE_FAIL;
390 krb5_free_principal(context, client);
393 krb5_kt_close(context, keytab);
395 #ifdef KRB5_IS_THREAD_SAFE
396 krb5_free_context(context);
404 * Validate userid/passwd (MIT)
406 static rlm_rcode_t krb5_auth(void *instance, REQUEST *request)
408 rlm_krb5_t *inst = instance;
412 krb5_principal client;
413 krb5_creds init_creds;
414 krb5_keytab keytab; /* ktid */
415 krb5_context context;
417 rad_assert(inst->context);
419 #ifdef KRB5_IS_THREAD_SAFE
421 * All the snippets on threadsafety say that individual threads
422 * must each use their own copy of context.
424 * As we don't have any per thread instantiation, we either have
425 * to clone inst->context on every request, or use the connection
428 * @todo Use the connection API (3.0 only).
430 ret = krb5_copy_context(inst->context, &context);
432 RDEBUGE("Error cloning krb5 context: %s", inst->xlat_name, error_message(ret));
434 return RLM_MODULE_FAIL;
436 rad_assert(context != NULL); /* tell coverity copy context copies it */
438 context = inst->context;
442 * Zero out local storage
444 memset(&keytab, 0, sizeof(keytab));
445 memset(&client, 0, sizeof(client));
446 memset(&init_creds, 0, sizeof(init_creds));
449 * Check we have all the required VPs, and convert the username
452 rcode = krb5_parse_user(request, context, &client);
453 if (rcode != RLM_MODULE_OK) goto cleanup;
458 ret = inst->keytabname ?
459 krb5_kt_resolve(context, inst->keytabname, &keytab) :
460 krb5_kt_default(context, &keytab);
462 RDEBUGE("Resolving keytab failed: %s", inst->xlat_name, error_message(ret));
468 * Retrieve the TGT from the TGS/KDC and check we can decrypt it.
470 ret = krb5_get_init_creds_password(context, &init_creds, client, request->password->vp_strvalue,
471 NULL, NULL, 0, NULL, inst->gic_options);
475 case KRB5_LIBOS_BADPWDMATCH:
476 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
477 RDEBUGE("Provided password was incorrect (%i): %s", ret, error_message(ret));
478 rcode = RLM_MODULE_REJECT;
481 case KRB5KDC_ERR_KEY_EXP:
482 case KRB5KDC_ERR_CLIENT_REVOKED:
483 case KRB5KDC_ERR_SERVICE_REVOKED:
484 RDEBUGE("Account has been locked out (%i): %s", ret, error_message(ret));
485 rcode = RLM_MODULE_USERLOCK;
488 case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
489 RDEBUG("User not found (%i): %s", ret, error_message(ret));
490 rcode = RLM_MODULE_NOTFOUND;
494 RDEBUGE("Error retrieving or verifying credentials (%i): %s", ret, error_message(ret));
495 rcode = RLM_MODULE_FAIL;
502 RDEBUG("Successfully retrieved and decrypted TGT");
504 ret = krb5_verify_init_creds(context, &init_creds, inst->server, keytab, NULL, inst->vic_options);
511 krb5_free_principal(context, client);
514 krb5_kt_close(context, keytab);
517 krb5_free_cred_contents(context, &init_creds);
519 #ifdef KRB5_IS_THREAD_SAFE
520 krb5_free_context(context);
527 #endif /* HEIMDAL_KRB5 */
529 module_t rlm_krb5 = {
532 RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE
533 #ifdef KRB5_IS_THREAD_SAFE
534 | RLM_TYPE_THREAD_SAFE
539 krb5_instantiate, /* instantiation */
540 krb5_detach, /* detach */
542 krb5_auth, /* authenticate */
543 NULL, /* authorize */
544 NULL, /* pre-accounting */
545 NULL, /* accounting */
546 NULL, /* checksimul */
547 NULL, /* pre-proxy */
548 NULL, /* post-proxy */