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>
29 #include <freeradius-devel/radiusd.h>
30 #include <freeradius-devel/modules.h>
31 #include <freeradius-devel/rad_assert.h>
37 # include <et/com_err.h>
42 /** Instance configuration for rlm_krb5
44 * Holds the configuration and preparsed data for a instance of rlm_krb5.
46 typedef struct rlm_krb5_t {
47 char const *xlat_name; //!< This module's instance name.
48 char const *keytabname; //!< The keytab to resolve the service in.
49 char const *service_princ; //!< The service name provided by the
52 char *hostname; //!< The hostname component of
53 //!< service_princ, or NULL.
54 char *service; //!< The service component of service_princ, or NULL.
56 krb5_context context; //!< The kerberos context (cloned once per request).
59 krb5_get_init_creds_opt *gic_options; //!< Options to pass to the get_initial_credentials
61 krb5_verify_init_creds_opt *vic_options; //!< Options to pass to the validate_initial_creds
64 krb5_principal server; //!< A structure representing the parsed
70 static const CONF_PARSER module_config[] = {
71 { "keytab", PW_TYPE_STRING_PTR, offsetof(rlm_krb5_t, keytabname), NULL, NULL },
72 { "service_principal", PW_TYPE_STRING_PTR, offsetof(rlm_krb5_t,service_princ), NULL, NULL },
73 { NULL, -1, 0, NULL, NULL }
76 static int krb5_detach(void *instance)
78 rlm_krb5_t *inst = instance;
81 talloc_free(inst->vic_options);
83 if (inst->gic_options) {
84 krb5_get_init_creds_opt_free(inst->context, inst->gic_options);
88 /* Don't free hostname, it's just a pointer into service_princ */
89 talloc_free(inst->service);
92 krb5_free_context(inst->context);
98 static int krb5_instantiate(CONF_SECTION *conf, void *instance)
100 rlm_krb5_t *inst = instance;
103 char keytab_name[200];
107 DEBUG("Using Heimdal Kerberos library");
109 DEBUG("Using MIT Kerberos library");
112 #ifndef KRB5_IS_THREAD_SAFE
113 if (!krb5_is_thread_safe()) {
114 DEBUGI("libkrb5 is not threadsafe, recompile it with thread support enabled");
115 WDEBUG("rlm_krb5 will run in single threaded mode, performance may be degraded");
117 WDEBUG("Build time libkrb5 was not threadsafe, but run time library claims to be");
118 WDEBUG("Reconfigure and recompile rlm_krb5 to enable thread support");
122 inst->xlat_name = cf_section_name2(conf);
123 if (!inst->xlat_name) {
124 inst->xlat_name = cf_section_name1(conf);
127 ret = krb5_init_context(&inst->context);
129 EDEBUG("rlm_krb5 (%s): Context initialisation failed: %s", inst->xlat_name, error_message(ret));
134 DEBUG("rlm_krb5 (%s): Context initialised successfully", inst->xlat_name);
137 * Split service principal into service and host components
138 * they're needed to build the server principal in MIT,
139 * and to set the validation service in Heimdal.
141 if (inst->service_princ) {
143 /* Service principal appears to contain a host component */
144 inst->hostname = strchr(inst->service_princ, '/');
145 if (inst->hostname) {
146 len = (inst->hostname - inst->service_princ);
149 len = strlen(inst->service_princ);
153 inst->service = talloc_array(inst, char, (len + 1));
154 strlcpy(inst->service, inst->service_princ, len + 1);
159 if (inst->hostname) {
160 DEBUG("rlm_krb5 (%s): Ignoring hostname component of service principal \"%s\", not "
161 "needed/supported by Heimdal", inst->xlat_name, inst->hostname);
166 * Convert the service principal string to a krb5 principal.
168 ret = krb5_sname_to_principal(inst->context, inst->hostname, inst->service, KRB5_NT_SRV_HST, &(inst->server));
170 EDEBUG("rlm_krb5 (%s): Failed parsing service principal: %s", inst->xlat_name, error_message(ret));
175 ret = krb5_unparse_name(inst->context, inst->server, &princ_name);
178 EDEBUG("rlm_krb5 (%s): Failed constructing service principal string: %s", inst->xlat_name,
185 * Not necessarily the same as the config item
187 DEBUG("rlm_krb5 (%s): Using service principal \"%s\"", inst->xlat_name, princ_name);
189 krb5_free_unparsed_name(inst->context, princ_name);
192 * Setup options for getting credentials and verifying them
195 /* For some reason the 'init' version of this function is deprecated */
196 ret = krb5_get_init_creds_opt_alloc(inst->context, &(inst->gic_options));
198 EDEBUG("rlm_krb5 (%s): Couldn't allocated inital credential options: %s", inst->xlat_name,
205 * Perform basic checks on the keytab
207 ret = inst->keytabname ?
208 krb5_kt_resolve(inst->context, inst->keytabname, &keytab) :
209 krb5_kt_default(inst->context, &keytab);
211 EDEBUG("rlm_krb5 (%s): Resolving keytab failed: %s", inst->xlat_name, error_message(ret));
216 ret = krb5_kt_get_name(inst->context, keytab, keytab_name, sizeof(keytab_name));
217 krb5_kt_close(inst->context, keytab);
219 EDEBUG("rlm_krb5 (%s): Can't retrieve keytab name: %s", inst->xlat_name, error_message(ret));
224 DEBUG("rlm_krb5 (%s): Using keytab \"%s\"", inst->xlat_name, keytab_name);
226 MEM(inst->vic_options = talloc_zero(inst, krb5_verify_init_creds_opt));
228 krb5_verify_init_creds_opt_init(inst->vic_options);
229 krb5_verify_init_creds_opt_set_ap_req_nofail(inst->vic_options, true);
236 static rlm_rcode_t krb5_parse_user(REQUEST *request, krb5_context context, krb5_principal *client)
242 * We can only authenticate user requests which HAVE
243 * a User-Name attribute.
245 if (!request->username) {
246 REDEBUG("Attribute \"User-Name\" is required for authentication");
248 return RLM_MODULE_INVALID;
252 * We can only authenticate user requests which HAVE
253 * a User-Password attribute.
255 if (!request->password) {
256 REDEBUG("Attribute \"User-Password\" is required for authentication");
258 return RLM_MODULE_INVALID;
262 * Ensure that we're being passed a plain-text password,
263 * and not anything else.
265 if (request->password->da->attr != PW_USER_PASSWORD) {
266 REDEBUG("Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".",
267 request->password->da->name);
269 return RLM_MODULE_INVALID;
272 ret = krb5_parse_name(context, request->username->vp_strvalue, client);
274 REDEBUG("Failed parsing username as principal: %s", error_message(ret));
276 return RLM_MODULE_FAIL;
279 krb5_unparse_name(context, *client, &princ_name);
280 RDEBUG("Using client principal \"%s\"", princ_name);
281 krb5_free_unparsed_name(context, princ_name);
283 return RLM_MODULE_OK;
289 * Validate user/pass (Heimdal)
291 static rlm_rcode_t krb5_auth(void *instance, REQUEST *request)
293 rlm_krb5_t *inst = instance;
298 krb5_principal client;
301 krb5_verify_opt options;
302 krb5_context context;
304 rad_assert(inst->context);
306 #ifdef KRB5_IS_THREAD_SAFE
308 * See above in MIT krb5_auth
310 ret = krb5_copy_context(inst->context, &context);
312 REDEBUG("Error cloning krb5 context: %s", error_message(ret));
314 return RLM_MODULE_FAIL;
317 context = inst->context;
321 * Zero out local storage
323 memset(&keytab, 0, sizeof(keytab));
324 memset(&client, 0, sizeof(client));
325 memset(&init_creds, 0, sizeof(init_creds));
328 * Setup krb5_verify_user options
330 * Not entirely sure this is necessary, but as we use context
331 * to get the cache handle, we probably do have to do this with
332 * the cloned context.
334 krb5_cc_default(context, &ccache);
336 krb5_verify_opt_init(&options);
337 krb5_verify_opt_set_ccache(&options, ccache);
338 ret = inst->keytabname ?
339 krb5_kt_resolve(context, inst->keytabname, &keytab) :
340 krb5_kt_default(context, &keytab);
342 REDEBUG("Resolving keytab failed: %s", error_message(ret));
347 krb5_verify_opt_set_keytab(&options, keytab);
348 krb5_verify_opt_set_secure(&options, true);
351 krb5_verify_opt_set_service(&options, inst->service);
354 rcode = krb5_parse_user(request, context, &client);
355 if (rcode != RLM_MODULE_OK) goto cleanup;
358 * Verify the user, using the options we set in instantiate
360 ret = krb5_verify_user_opt(context, client, request->password->vp_strvalue, &options);
363 case KRB5_LIBOS_BADPWDMATCH:
364 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
365 REDEBUG("Provided password was incorrect (%i): %s", ret, error_message(ret));
366 rcode = RLM_MODULE_REJECT;
369 case KRB5KDC_ERR_KEY_EXP:
370 case KRB5KDC_ERR_CLIENT_REVOKED:
371 case KRB5KDC_ERR_SERVICE_REVOKED:
372 REDEBUG("Account has been locked out (%i): %s", ret, error_message(ret));
373 rcode = RLM_MODULE_USERLOCK;
376 case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
377 RDEBUG("User not found: %s (%i)", ret, error_message(ret));
378 rcode = RLM_MODULE_NOTFOUND;
381 REDEBUG("Error verifying credentials (%i): %s", ret, error_message(ret));
382 rcode = RLM_MODULE_FAIL;
392 krb5_free_principal(context, client);
395 krb5_kt_close(context, keytab);
397 #ifdef KRB5_IS_THREAD_SAFE
398 krb5_free_context(context);
406 * Validate userid/passwd (MIT)
408 static rlm_rcode_t krb5_auth(void *instance, REQUEST *request)
410 rlm_krb5_t *inst = instance;
414 krb5_principal client;
415 krb5_creds init_creds;
416 krb5_keytab keytab; /* ktid */
417 krb5_context context;
418 char *password; /* compiler warnings */
420 rad_assert(inst->context);
422 #ifdef KRB5_IS_THREAD_SAFE
424 * All the snippets on threadsafety say that individual threads
425 * must each use their own copy of context.
427 * As we don't have any per thread instantiation, we either have
428 * to clone inst->context on every request, or use the connection
431 * @todo Use the connection API (3.0 only).
433 ret = krb5_copy_context(inst->context, &context);
435 REDEBUG("Error cloning krb5 context: %s", inst->xlat_name, error_message(ret));
437 return RLM_MODULE_FAIL;
439 rad_assert(context != NULL); /* tell coverity copy context copies it */
441 context = inst->context;
445 * Zero out local storage
447 memset(&keytab, 0, sizeof(keytab));
448 memset(&client, 0, sizeof(client));
449 memset(&init_creds, 0, sizeof(init_creds));
452 * Check we have all the required VPs, and convert the username
455 rcode = krb5_parse_user(request, context, &client);
456 if (rcode != RLM_MODULE_OK) goto cleanup;
461 ret = inst->keytabname ?
462 krb5_kt_resolve(context, inst->keytabname, &keytab) :
463 krb5_kt_default(context, &keytab);
465 REDEBUG("Resolving keytab failed: %s", inst->xlat_name, error_message(ret));
471 * Retrieve the TGT from the TGS/KDC and check we can decrypt it.
473 memcpy(&password, request->password->vp_strvalue, sizeof(password));
474 ret = krb5_get_init_creds_password(context, &init_creds, client, password,
475 NULL, NULL, 0, NULL, inst->gic_options);
480 case KRB5_LIBOS_BADPWDMATCH:
481 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
482 REDEBUG("Provided password was incorrect (%i): %s", ret, error_message(ret));
483 rcode = RLM_MODULE_REJECT;
486 case KRB5KDC_ERR_KEY_EXP:
487 case KRB5KDC_ERR_CLIENT_REVOKED:
488 case KRB5KDC_ERR_SERVICE_REVOKED:
489 REDEBUG("Account has been locked out (%i): %s", ret, error_message(ret));
490 rcode = RLM_MODULE_USERLOCK;
493 case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
494 RDEBUG("User not found (%i): %s", ret, error_message(ret));
495 rcode = RLM_MODULE_NOTFOUND;
499 REDEBUG("Error retrieving or verifying credentials (%i): %s", ret, error_message(ret));
500 rcode = RLM_MODULE_FAIL;
507 RDEBUG("Successfully retrieved and decrypted TGT");
509 ret = krb5_verify_init_creds(context, &init_creds, inst->server, keytab, NULL, inst->vic_options);
516 krb5_free_principal(context, client);
519 krb5_kt_close(context, keytab);
522 krb5_free_cred_contents(context, &init_creds);
524 #ifdef KRB5_IS_THREAD_SAFE
525 krb5_free_context(context);
532 #endif /* HEIMDAL_KRB5 */
534 module_t rlm_krb5 = {
537 RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE
538 #ifdef KRB5_IS_THREAD_SAFE
539 | RLM_TYPE_THREAD_SAFE
544 krb5_instantiate, /* instantiation */
545 krb5_detach, /* detach */
547 krb5_auth, /* authenticate */
548 NULL, /* authorize */
549 NULL, /* pre-accounting */
550 NULL, /* accounting */
551 NULL, /* checksimul */
552 NULL, /* pre-proxy */
553 NULL, /* post-proxy */