2 * This pam_krb5 module contains code that is:
3 * Copyright (c) Derrick J. Brashear, 1996. All rights reserved.
4 * Copyright (c) Frank Cusack, 1999-2001. All rights reserved.
5 * Copyright (c) Jacques A. Vidrine, 2000-2001. All rights reserved.
6 * Copyright (c) Nicolas Williams, 2001. All rights reserved.
7 * Copyright (c) Perot Systems Corporation, 2001. All rights reserved.
8 * Copyright (c) Mark R V Murray, 2001. All rights reserved.
9 * Copyright (c) Networks Associates Technology, Inc., 2002-2005.
10 * All rights reserved.
12 * Portions of this software were developed for the FreeBSD Project by
13 * ThinkSec AS and NAI Labs, the Security Research Division of Network
14 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
15 * ("CBOSS"), as part of the DARPA CHATS research program.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notices, and the entire permission notice in its entirety,
22 * including the disclaimer of warranties.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. The name of the author may not be used to endorse or promote
27 * products derived from this software without specific prior
30 * ALTERNATIVELY, this product may be distributed under the terms of
31 * the GNU Public License, in which case the provisions of the GPL are
32 * required INSTEAD OF the above restrictions. (This clause is
33 * necessary due to a potential bad interaction between the GPL and
34 * the restrictions contained in a BSD-style copyright.)
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
40 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/types.h>
68 #define PAM_SM_ACCOUNT
69 #define PAM_SM_PASSWORD
71 #include <security/pam_appl.h>
72 #include <security/pam_modules.h>
73 #include <security/pam_mod_misc.h>
74 #include <security/openpam.h>
76 #define COMPAT_HEIMDAL
77 /* #define COMPAT_MIT */
79 static int verify_krb_v5_tgt(krb5_context, krb5_ccache, char *, int);
80 static void cleanup_cache(pam_handle_t *, void *, int);
81 static const char *compat_princ_component(krb5_context, krb5_principal, int);
82 static void compat_free_data_contents(krb5_context, krb5_data *);
84 #define USER_PROMPT "Username: "
85 #define PASSWORD_PROMPT "Password:"
86 #define NEW_PASSWORD_PROMPT "New Password:"
88 #define PAM_OPT_CCACHE "ccache"
89 #define PAM_OPT_DEBUG "debug"
90 #define PAM_OPT_FORWARDABLE "forwardable"
91 #define PAM_OPT_NO_CCACHE "no_ccache"
92 #define PAM_OPT_NO_USER_CHECK "no_user_check"
93 #define PAM_OPT_REUSE_CCACHE "reuse_ccache"
94 #define PAM_OPT_NO_USER_CHECK "no_user_check"
96 #define PAM_LOG_KRB5_ERR(ctx, rv, fmt, ...) \
98 const char *krb5msg = krb5_get_error_message(ctx, rv); \
99 PAM_LOG(fmt ": %s", ##__VA_ARGS__, krb5msg); \
100 krb5_free_error_message(ctx, krb5msg); \
104 * authentication management
107 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
108 int argc __unused, const char *argv[] __unused)
110 krb5_error_code krbret;
111 krb5_context pam_context;
113 krb5_principal princ;
115 krb5_get_init_creds_opt *opts;
118 const void *ccache_data;
119 const char *user, *pass;
120 const void *sourceuser, *service;
121 char *principal, *princ_name, *ccache_name, luser[32], *srvdup;
123 retval = pam_get_user(pamh, &user, USER_PROMPT);
124 if (retval != PAM_SUCCESS)
127 PAM_LOG("Got user: %s", user);
129 retval = pam_get_item(pamh, PAM_RUSER, &sourceuser);
130 if (retval != PAM_SUCCESS)
133 PAM_LOG("Got ruser: %s", (const char *)sourceuser);
136 pam_get_item(pamh, PAM_SERVICE, &service);
140 PAM_LOG("Got service: %s", (const char *)service);
142 krbret = krb5_init_context(&pam_context);
144 PAM_VERBOSE_ERROR("Kerberos 5 error");
145 return (PAM_SERVICE_ERR);
148 PAM_LOG("Context initialised");
150 krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE);
151 if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) {
152 PAM_VERBOSE_ERROR("Kerberos 5 error");
153 retval = PAM_SERVICE_ERR;
157 PAM_LOG("Done krb5_cc_register()");
159 /* Get principal name */
160 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
161 asprintf(&principal, "%s/%s", (const char *)sourceuser, user);
163 principal = strdup(user);
165 PAM_LOG("Created principal: %s", principal);
167 krbret = krb5_parse_name(pam_context, principal, &princ);
170 PAM_LOG_KRB5_ERR(pam_context, krbret, "Error krb5_parse_name()");
171 PAM_VERBOSE_ERROR("Kerberos 5 error");
172 retval = PAM_SERVICE_ERR;
176 PAM_LOG("Done krb5_parse_name()");
178 /* Now convert the principal name into something human readable */
180 krbret = krb5_unparse_name(pam_context, princ, &princ_name);
182 PAM_LOG_KRB5_ERR(pam_context, krbret,
183 "Error krb5_unparse_name()");
184 PAM_VERBOSE_ERROR("Kerberos 5 error");
185 retval = PAM_SERVICE_ERR;
189 PAM_LOG("Got principal: %s", princ_name);
192 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
193 if (retval != PAM_SUCCESS)
196 PAM_LOG("Got password");
198 if (openpam_get_option(pamh, PAM_OPT_NO_USER_CHECK))
199 PAM_LOG("Skipping local user check");
202 /* Verify the local user exists (AFTER getting the password) */
203 if (strchr(user, '@')) {
204 /* get a local account name for this principal */
205 krbret = krb5_aname_to_localname(pam_context, princ,
206 sizeof(luser), luser);
208 PAM_VERBOSE_ERROR("Kerberos 5 error");
209 PAM_LOG_KRB5_ERR(pam_context, krbret,
210 "Error krb5_aname_to_localname()");
211 retval = PAM_USER_UNKNOWN;
215 retval = pam_set_item(pamh, PAM_USER, luser);
216 if (retval != PAM_SUCCESS)
219 PAM_LOG("PAM_USER Redone");
222 if (!openpam_get_option(pamh, PAM_OPT_NO_USER_CHECK)) {
223 pwd = getpwnam(user);
225 retval = PAM_USER_UNKNOWN;
230 PAM_LOG("Done getpwnam()");
233 /* Initialize credentials request options. */
234 krbret = krb5_get_init_creds_opt_alloc(pam_context, &opts);
236 PAM_LOG_KRB5_ERR(pam_context, krbret,
237 "Error krb5_get_init_creds_opt_alloc()");
238 PAM_VERBOSE_ERROR("Kerberos 5 error");
239 retval = PAM_SERVICE_ERR;
243 if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
244 krb5_get_init_creds_opt_set_forwardable(opts, 1);
246 PAM_LOG("Credential options initialised");
249 memset(&creds, 0, sizeof(krb5_creds));
250 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
251 pass, NULL, pamh, 0, NULL, opts);
252 krb5_get_init_creds_opt_free(pam_context, opts);
254 PAM_VERBOSE_ERROR("Kerberos 5 error");
255 PAM_LOG_KRB5_ERR(pam_context, krbret,
256 "Error krb5_get_init_creds_password()");
257 retval = PAM_AUTH_ERR;
263 /* Generate a temporary cache */
264 krbret = krb5_cc_new_unique(pam_context, krb5_cc_type_memory, NULL, &ccache);
266 PAM_VERBOSE_ERROR("Kerberos 5 error");
267 PAM_LOG_KRB5_ERR(pam_context, krbret,
268 "Error krb5_cc_new_unique()");
269 retval = PAM_SERVICE_ERR;
272 krbret = krb5_cc_initialize(pam_context, ccache, princ);
274 PAM_VERBOSE_ERROR("Kerberos 5 error");
275 PAM_LOG_KRB5_ERR(pam_context, krbret,
276 "Error krb5_cc_initialize()");
277 retval = PAM_SERVICE_ERR;
280 krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
282 PAM_VERBOSE_ERROR("Kerberos 5 error");
283 PAM_LOG_KRB5_ERR(pam_context, krbret,
284 "Error krb5_cc_store_cred()");
285 krb5_cc_destroy(pam_context, ccache);
286 retval = PAM_SERVICE_ERR;
290 PAM_LOG("Credentials stashed");
293 if ((srvdup = strdup(service)) == NULL) {
294 retval = PAM_BUF_ERR;
297 krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
298 openpam_get_option(pamh, PAM_OPT_DEBUG) ? 1 : 0);
301 PAM_VERBOSE_ERROR("Kerberos 5 error");
302 krb5_cc_destroy(pam_context, ccache);
303 retval = PAM_AUTH_ERR;
307 PAM_LOG("Credentials stash verified");
309 retval = pam_get_data(pamh, "ccache", &ccache_data);
310 if (retval == PAM_SUCCESS) {
311 krb5_cc_destroy(pam_context, ccache);
312 PAM_VERBOSE_ERROR("Kerberos 5 error");
313 retval = PAM_AUTH_ERR;
317 PAM_LOG("Credentials stash not pre-existing");
319 asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
320 ccache), krb5_cc_get_name(pam_context, ccache));
321 if (ccache_name == NULL) {
322 PAM_VERBOSE_ERROR("Kerberos 5 error");
323 retval = PAM_BUF_ERR;
326 retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache);
328 krb5_cc_destroy(pam_context, ccache);
329 PAM_VERBOSE_ERROR("Kerberos 5 error");
330 retval = PAM_SERVICE_ERR;
334 PAM_LOG("Credentials stash saved");
337 krb5_free_cred_contents(pam_context, &creds);
338 PAM_LOG("Done cleanup");
340 krb5_free_principal(pam_context, princ);
343 PAM_LOG("Done cleanup2");
346 krb5_free_context(pam_context);
348 PAM_LOG("Done cleanup3");
350 if (retval != PAM_SUCCESS)
351 PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
357 pam_sm_setcred(pam_handle_t *pamh, int flags,
358 int argc __unused, const char *argv[] __unused)
360 #ifdef _FREEFALL_CONFIG
361 return (PAM_SUCCESS);
364 krb5_error_code krbret;
365 krb5_context pam_context;
366 krb5_principal princ;
368 krb5_ccache ccache_temp, ccache_perm;
369 krb5_cc_cursor cursor;
370 struct passwd *pwd = NULL;
372 const char *cache_name, *q;
374 const void *cache_data;
375 char *cache_name_buf = NULL, *p;
380 if (flags & PAM_DELETE_CRED)
381 return (PAM_SUCCESS);
383 if (flags & PAM_REFRESH_CRED)
384 return (PAM_SUCCESS);
386 if (flags & PAM_REINITIALIZE_CRED)
387 return (PAM_SUCCESS);
389 if (!(flags & PAM_ESTABLISH_CRED))
390 return (PAM_SERVICE_ERR);
392 /* If a persistent cache isn't desired, stop now. */
393 if (openpam_get_option(pamh, PAM_OPT_NO_CCACHE) ||
394 openpam_get_option(pamh, PAM_OPT_NO_USER_CHECK))
395 return (PAM_SUCCESS);
397 PAM_LOG("Establishing credentials");
400 retval = pam_get_item(pamh, PAM_USER, &user);
401 if (retval != PAM_SUCCESS)
404 PAM_LOG("Got user: %s", (const char *)user);
406 krbret = krb5_init_context(&pam_context);
408 PAM_LOG("Error krb5_init_context() failed");
409 return (PAM_SERVICE_ERR);
412 PAM_LOG("Context initialised");
414 euid = geteuid(); /* Usually 0 */
417 PAM_LOG("Got euid, egid: %d %d", euid, egid);
419 /* Retrieve the temporary cache */
420 retval = pam_get_data(pamh, "ccache", &cache_data);
421 if (retval != PAM_SUCCESS) {
422 retval = PAM_CRED_UNAVAIL;
425 krbret = krb5_cc_resolve(pam_context, cache_data, &ccache_temp);
427 PAM_LOG_KRB5_ERR(pam_context, krbret,
428 "Error krb5_cc_resolve(\"%s\")", (const char *)cache_data);
429 retval = PAM_SERVICE_ERR;
433 /* Get the uid. This should exist. */
434 pwd = getpwnam(user);
436 retval = PAM_USER_UNKNOWN;
440 PAM_LOG("Done getpwnam()");
442 /* Avoid following a symlink as root */
443 if (setegid(pwd->pw_gid)) {
444 retval = PAM_SERVICE_ERR;
447 if (seteuid(pwd->pw_uid)) {
448 retval = PAM_SERVICE_ERR;
452 PAM_LOG("Done setegid() & seteuid()");
454 /* Get the cache name */
455 cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
456 if (cache_name == NULL) {
457 asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
458 cache_name = cache_name_buf;
461 p = calloc(PATH_MAX + 16, sizeof(char));
465 PAM_LOG("Error malloc(): failure");
466 retval = PAM_BUF_ERR;
471 /* convert %u and %p */
476 sprintf(p, "%d", pwd->pw_uid);
479 else if (*q == 'p') {
480 sprintf(p, "%d", getpid());
484 /* Not a special token */
495 PAM_LOG("Got cache_name: %s", cache_name);
497 /* Initialize the new ccache */
498 krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
500 PAM_LOG_KRB5_ERR(pam_context, krbret,
501 "Error krb5_cc_get_principal()");
502 retval = PAM_SERVICE_ERR;
505 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
507 PAM_LOG_KRB5_ERR(pam_context, krbret, "Error krb5_cc_resolve()");
508 retval = PAM_SERVICE_ERR;
511 krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
513 PAM_LOG_KRB5_ERR(pam_context, krbret,
514 "Error krb5_cc_initialize()");
515 retval = PAM_SERVICE_ERR;
519 PAM_LOG("Cache initialised");
521 /* Prepare for iteration over creds */
522 krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
524 PAM_LOG_KRB5_ERR(pam_context, krbret,
525 "Error krb5_cc_start_seq_get()");
526 krb5_cc_destroy(pam_context, ccache_perm);
527 retval = PAM_SERVICE_ERR;
531 PAM_LOG("Prepared for iteration");
533 /* Copy the creds (should be two of them) */
534 while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
535 &cursor, &creds) == 0)) {
536 krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
538 PAM_LOG_KRB5_ERR(pam_context, krbret,
539 "Error krb5_cc_store_cred()");
540 krb5_cc_destroy(pam_context, ccache_perm);
541 krb5_free_cred_contents(pam_context, &creds);
542 retval = PAM_SERVICE_ERR;
545 krb5_free_cred_contents(pam_context, &creds);
546 PAM_LOG("Iteration");
548 krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
550 PAM_LOG("Done iterating");
552 if (strstr(cache_name, "FILE:") == cache_name) {
553 if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) {
554 PAM_LOG("Error chown(): %s", strerror(errno));
555 krb5_cc_destroy(pam_context, ccache_perm);
556 retval = PAM_SERVICE_ERR;
559 PAM_LOG("Done chown()");
561 if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) {
562 PAM_LOG("Error chmod(): %s", strerror(errno));
563 krb5_cc_destroy(pam_context, ccache_perm);
564 retval = PAM_SERVICE_ERR;
567 PAM_LOG("Done chmod()");
570 krb5_cc_close(pam_context, ccache_perm);
572 PAM_LOG("Cache closed");
574 retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1);
575 if (retval != PAM_SUCCESS) {
576 PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
577 krb5_cc_destroy(pam_context, ccache_perm);
578 retval = PAM_SERVICE_ERR;
582 PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
585 krb5_free_principal(pam_context, princ);
586 PAM_LOG("Done cleanup2");
588 krb5_free_context(pam_context);
589 PAM_LOG("Done cleanup3");
594 PAM_LOG("Done seteuid() & setegid()");
596 if (cache_name_buf != NULL)
597 free(cache_name_buf);
607 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
608 int argc __unused, const char *argv[] __unused)
610 krb5_error_code krbret;
611 krb5_context pam_context;
613 krb5_principal princ;
616 const void *ccache_name;
618 retval = pam_get_item(pamh, PAM_USER, &user);
619 if (retval != PAM_SUCCESS)
622 PAM_LOG("Got user: %s", (const char *)user);
624 retval = pam_get_data(pamh, "ccache", &ccache_name);
625 if (retval != PAM_SUCCESS)
626 return (PAM_SUCCESS);
628 PAM_LOG("Got credentials");
630 krbret = krb5_init_context(&pam_context);
632 PAM_LOG("Error krb5_init_context() failed");
633 return (PAM_PERM_DENIED);
636 PAM_LOG("Context initialised");
638 krbret = krb5_cc_resolve(pam_context, (const char *)ccache_name, &ccache);
640 PAM_LOG_KRB5_ERR(pam_context, krbret,
641 "Error krb5_cc_resolve(\"%s\")", (const char *)ccache_name);
642 krb5_free_context(pam_context);
643 return (PAM_PERM_DENIED);
646 PAM_LOG("Got ccache %s", (const char *)ccache_name);
649 krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
651 PAM_LOG_KRB5_ERR(pam_context, krbret,
652 "Error krb5_cc_get_principal()");
653 retval = PAM_PERM_DENIED;
657 PAM_LOG("Got principal");
659 if (krb5_kuserok(pam_context, princ, (const char *)user))
660 retval = PAM_SUCCESS;
662 retval = PAM_PERM_DENIED;
663 krb5_free_principal(pam_context, princ);
665 PAM_LOG("Done kuserok()");
668 krb5_free_context(pam_context);
669 PAM_LOG("Done cleanup");
676 * password management
679 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
680 int argc __unused, const char *argv[] __unused)
682 krb5_error_code krbret;
683 krb5_context pam_context;
685 krb5_principal princ;
686 krb5_get_init_creds_opt *opts;
687 krb5_data result_code_string, result_string;
688 int result_code, retval;
691 char *princ_name, *passdup;
693 if (!(flags & PAM_UPDATE_AUTHTOK))
694 return (PAM_AUTHTOK_ERR);
696 retval = pam_get_item(pamh, PAM_USER, &user);
697 if (retval != PAM_SUCCESS)
700 PAM_LOG("Got user: %s", (const char *)user);
702 krbret = krb5_init_context(&pam_context);
704 PAM_LOG("Error krb5_init_context() failed");
705 return (PAM_SERVICE_ERR);
708 PAM_LOG("Context initialised");
710 /* Get principal name */
711 krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
713 PAM_LOG_KRB5_ERR(pam_context, krbret,
714 "Error krb5_parse_name()");
715 retval = PAM_USER_UNKNOWN;
719 /* Now convert the principal name into something human readable */
721 krbret = krb5_unparse_name(pam_context, princ, &princ_name);
723 PAM_LOG_KRB5_ERR(pam_context, krbret,
724 "Error krb5_unparse_name()");
725 retval = PAM_SERVICE_ERR;
729 PAM_LOG("Got principal: %s", princ_name);
732 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, PASSWORD_PROMPT);
733 if (retval != PAM_SUCCESS)
736 PAM_LOG("Got password");
738 /* Initialize credentials request options. */
739 krbret = krb5_get_init_creds_opt_alloc(pam_context, &opts);
741 PAM_LOG_KRB5_ERR(pam_context, krbret,
742 "Error krb5_get_init_creds_opt_alloc()");
743 PAM_VERBOSE_ERROR("Kerberos 5 error");
744 retval = PAM_SERVICE_ERR;
748 PAM_LOG("Credentials options initialised");
750 memset(&creds, 0, sizeof(krb5_creds));
751 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
752 pass, NULL, pamh, 0, "kadmin/changepw", opts);
753 krb5_get_init_creds_opt_free(pam_context, opts);
755 PAM_LOG_KRB5_ERR(pam_context, krbret,
756 "Error krb5_get_init_creds_password()");
757 retval = PAM_AUTH_ERR;
761 PAM_LOG("Credentials established");
763 /* Now get the new password */
765 retval = pam_get_authtok(pamh,
766 PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
767 if (retval != PAM_TRY_AGAIN)
769 pam_error(pamh, "Mismatch; try again, EOF to quit.");
771 if (retval != PAM_SUCCESS)
774 PAM_LOG("Got new password");
777 if ((passdup = strdup(pass)) == NULL) {
778 retval = PAM_BUF_ERR;
781 krbret = krb5_set_password(pam_context, &creds, passdup, NULL,
782 &result_code, &result_code_string, &result_string);
785 PAM_LOG_KRB5_ERR(pam_context, krbret,
786 "Error krb5_change_password()");
787 retval = PAM_AUTHTOK_ERR;
791 PAM_LOG("Error krb5_change_password(): (result_code)");
792 retval = PAM_AUTHTOK_ERR;
796 PAM_LOG("Password changed");
798 if (result_string.data)
799 free(result_string.data);
800 if (result_code_string.data)
801 free(result_code_string.data);
804 krb5_free_cred_contents(pam_context, &creds);
805 PAM_LOG("Done cleanup");
807 krb5_free_principal(pam_context, princ);
810 PAM_LOG("Done cleanup2");
813 krb5_free_context(pam_context);
815 PAM_LOG("Done cleanup3");
820 PAM_MODULE_ENTRY("pam_krb5");
823 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
824 * Modified by Sam Hartman <hartmans@mit.edu> to support PAM services
827 * Verify the Kerberos ticket-granting ticket just retrieved for the
828 * user. If the Kerberos server doesn't respond, assume the user is
829 * trying to fake us out (since we DID just get a TGT from what is
830 * supposedly our KDC). If the host/<host> service is unknown (i.e.,
831 * the local keytab doesn't have it), and we cannot find another
832 * service we do have, let her in.
834 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
838 verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
839 char *pam_service, int debug)
841 krb5_error_code retval;
842 krb5_principal princ;
843 krb5_keyblock *keyblock;
845 krb5_auth_context auth_context;
847 const char *services[3], **service;
851 /* If possible we want to try and verify the ticket we have
852 * received against a keytab. We will try multiple service
853 * principals, including at least the host principal and the PAM
854 * service principal. The host principal is preferred because access
855 * to that key is generally sufficient to compromise root, while the
856 * service key for this PAM service may be less carefully guarded.
857 * It is important to check the keytab first before the KDC so we do
858 * not get spoofed by a fake KDC.
860 services[0] = "host";
861 services[1] = pam_service;
865 for (service = &services[0]; *service != NULL; service++) {
866 retval = krb5_sname_to_principal(context, NULL, *service,
867 KRB5_NT_SRV_HST, &princ);
870 const char *msg = krb5_get_error_message(
873 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
874 "krb5_sname_to_principal()", msg);
875 krb5_free_error_message(context, msg);
880 /* Extract the name directly. */
881 strncpy(phost, compat_princ_component(context, princ, 1),
883 phost[BUFSIZ - 1] = '\0';
886 * Do we have service/<host> keys?
887 * (use default/configured keytab, kvno IGNORE_VNO to get the
888 * first match, and ignore enctype.)
890 retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
896 if (retval != 0) { /* failed to find key */
897 /* Keytab or service key does not exist */
899 const char *msg = krb5_get_error_message(context,
902 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
903 "krb5_kt_read_service_key()", msg);
904 krb5_free_error_message(context, msg);
910 krb5_free_keyblock(context, keyblock);
912 /* Talk to the kdc and construct the ticket. */
914 retval = krb5_mk_req(context, &auth_context, 0, *service, phost,
915 NULL, ccache, &packet);
917 krb5_auth_con_free(context, auth_context);
918 auth_context = NULL; /* setup for rd_req */
922 const char *msg = krb5_get_error_message(context,
925 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
926 "krb5_mk_req()", msg);
927 krb5_free_error_message(context, msg);
933 /* Try to use the ticket. */
934 retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
938 const char *msg = krb5_get_error_message(context,
941 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
942 "krb5_rd_req()", msg);
943 krb5_free_error_message(context, msg);
952 compat_free_data_contents(context, &packet);
953 krb5_free_principal(context, princ);
957 /* Free the memory for cache_name. Called by pam_end() */
960 cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
962 krb5_context pam_context;
964 krb5_error_code krbret;
966 if (krb5_init_context(&pam_context))
969 krbret = krb5_cc_resolve(pam_context, data, &ccache);
971 krb5_cc_destroy(pam_context, ccache);
972 krb5_free_context(pam_context);
976 #ifdef COMPAT_HEIMDAL
978 #error This cannot be MIT and Heimdal compatible!
982 #ifndef COMPAT_HEIMDAL
984 #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
988 #ifdef COMPAT_HEIMDAL
991 compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
993 return princ->name.name_string.val[n];
998 compat_free_data_contents(krb5_context context __unused, krb5_data * data)
1000 krb5_xfree(data->data);
1006 compat_princ_component(krb5_context context, krb5_principal princ, int n)
1008 return krb5_princ_component(context, princ, n)->data;
1012 compat_free_data_contents(krb5_context context, krb5_data * data)
1014 krb5_free_data_contents(context, data);