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"
96 * authentication management
99 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
100 int argc __unused, const char *argv[] __unused)
102 krb5_error_code krbret;
103 krb5_context pam_context;
105 krb5_principal princ;
107 krb5_get_init_creds_opt opts;
110 const void *ccache_data;
111 const char *user, *pass;
112 const void *sourceuser, *service;
113 char *principal, *princ_name, *ccache_name, luser[32], *srvdup;
115 retval = pam_get_user(pamh, &user, USER_PROMPT);
116 if (retval != PAM_SUCCESS)
119 PAM_LOG("Got user: %s", user);
121 retval = pam_get_item(pamh, PAM_RUSER, &sourceuser);
122 if (retval != PAM_SUCCESS)
125 PAM_LOG("Got ruser: %s", (const char *)sourceuser);
128 pam_get_item(pamh, PAM_SERVICE, &service);
132 PAM_LOG("Got service: %s", (const char *)service);
134 krbret = krb5_init_context(&pam_context);
136 PAM_VERBOSE_ERROR("Kerberos 5 error");
137 return (PAM_SERVICE_ERR);
140 PAM_LOG("Context initialised");
142 krb5_get_init_creds_opt_init(&opts);
144 if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
145 krb5_get_init_creds_opt_set_forwardable(&opts, 1);
147 PAM_LOG("Credentials initialised");
149 krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE);
150 if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) {
151 PAM_VERBOSE_ERROR("Kerberos 5 error");
152 retval = PAM_SERVICE_ERR;
156 PAM_LOG("Done krb5_cc_register()");
158 /* Get principal name */
159 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
160 asprintf(&principal, "%s/%s", (const char *)sourceuser, user);
162 principal = strdup(user);
164 PAM_LOG("Created principal: %s", principal);
166 krbret = krb5_parse_name(pam_context, principal, &princ);
169 PAM_LOG("Error krb5_parse_name(): %s",
170 krb5_get_err_text(pam_context, krbret));
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("Error krb5_unparse_name(): %s",
183 krb5_get_err_text(pam_context, krbret));
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("Error krb5_aname_to_localname(): %s",
210 krb5_get_err_text(pam_context, krbret));
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 pwd = getpwnam(user);
224 retval = PAM_USER_UNKNOWN;
228 PAM_LOG("Done getpwnam()");
232 memset(&creds, 0, sizeof(krb5_creds));
233 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
234 pass, NULL, pamh, 0, NULL, &opts);
236 PAM_VERBOSE_ERROR("Kerberos 5 error");
237 PAM_LOG("Error krb5_get_init_creds_password(): %s",
238 krb5_get_err_text(pam_context, krbret));
239 retval = PAM_AUTH_ERR;
245 /* Generate a temporary cache */
246 krbret = krb5_cc_gen_new(pam_context, &krb5_mcc_ops, &ccache);
248 PAM_VERBOSE_ERROR("Kerberos 5 error");
249 PAM_LOG("Error krb5_cc_gen_new(): %s",
250 krb5_get_err_text(pam_context, krbret));
251 retval = PAM_SERVICE_ERR;
254 krbret = krb5_cc_initialize(pam_context, ccache, princ);
256 PAM_VERBOSE_ERROR("Kerberos 5 error");
257 PAM_LOG("Error krb5_cc_initialize(): %s",
258 krb5_get_err_text(pam_context, krbret));
259 retval = PAM_SERVICE_ERR;
262 krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
264 PAM_VERBOSE_ERROR("Kerberos 5 error");
265 PAM_LOG("Error krb5_cc_store_cred(): %s",
266 krb5_get_err_text(pam_context, krbret));
267 krb5_cc_destroy(pam_context, ccache);
268 retval = PAM_SERVICE_ERR;
272 PAM_LOG("Credentials stashed");
275 if ((srvdup = strdup(service)) == NULL) {
276 retval = PAM_BUF_ERR;
279 krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
280 openpam_get_option(pamh, PAM_OPT_DEBUG) ? 1 : 0);
283 PAM_VERBOSE_ERROR("Kerberos 5 error");
284 krb5_cc_destroy(pam_context, ccache);
285 retval = PAM_AUTH_ERR;
289 PAM_LOG("Credentials stash verified");
291 retval = pam_get_data(pamh, "ccache", &ccache_data);
292 if (retval == PAM_SUCCESS) {
293 krb5_cc_destroy(pam_context, ccache);
294 PAM_VERBOSE_ERROR("Kerberos 5 error");
295 retval = PAM_AUTH_ERR;
299 PAM_LOG("Credentials stash not pre-existing");
301 asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
302 ccache), krb5_cc_get_name(pam_context, ccache));
303 if (ccache_name == NULL) {
304 PAM_VERBOSE_ERROR("Kerberos 5 error");
305 retval = PAM_BUF_ERR;
308 retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache);
310 krb5_cc_destroy(pam_context, ccache);
311 PAM_VERBOSE_ERROR("Kerberos 5 error");
312 retval = PAM_SERVICE_ERR;
316 PAM_LOG("Credentials stash saved");
319 krb5_free_cred_contents(pam_context, &creds);
320 PAM_LOG("Done cleanup");
322 krb5_free_principal(pam_context, princ);
323 PAM_LOG("Done cleanup2");
328 krb5_free_context(pam_context);
330 PAM_LOG("Done cleanup3");
332 if (retval != PAM_SUCCESS)
333 PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
339 pam_sm_setcred(pam_handle_t *pamh, int flags,
340 int argc __unused, const char *argv[] __unused)
342 #ifdef _FREEFALL_CONFIG
343 return (PAM_SUCCESS);
346 krb5_error_code krbret;
347 krb5_context pam_context;
348 krb5_principal princ;
350 krb5_ccache ccache_temp, ccache_perm;
351 krb5_cc_cursor cursor;
352 struct passwd *pwd = NULL;
354 const char *cache_name, *q;
356 const void *cache_data;
357 char *cache_name_buf = NULL, *p;
362 if (flags & PAM_DELETE_CRED)
363 return (PAM_SUCCESS);
365 if (flags & PAM_REFRESH_CRED)
366 return (PAM_SUCCESS);
368 if (flags & PAM_REINITIALIZE_CRED)
369 return (PAM_SUCCESS);
371 if (!(flags & PAM_ESTABLISH_CRED))
372 return (PAM_SERVICE_ERR);
374 /* If a persistent cache isn't desired, stop now. */
375 if (openpam_get_option(pamh, PAM_OPT_NO_CCACHE) ||
376 openpam_get_option(pamh, PAM_OPT_NO_USER_CHECK))
377 return (PAM_SUCCESS);
379 PAM_LOG("Establishing credentials");
382 retval = pam_get_item(pamh, PAM_USER, &user);
383 if (retval != PAM_SUCCESS)
386 PAM_LOG("Got user: %s", (const char *)user);
388 krbret = krb5_init_context(&pam_context);
390 PAM_LOG("Error krb5_init_context() failed");
391 return (PAM_SERVICE_ERR);
394 PAM_LOG("Context initialised");
396 euid = geteuid(); /* Usually 0 */
399 PAM_LOG("Got euid, egid: %d %d", euid, egid);
401 /* Retrieve the temporary cache */
402 retval = pam_get_data(pamh, "ccache", &cache_data);
403 if (retval != PAM_SUCCESS) {
404 retval = PAM_CRED_UNAVAIL;
407 krbret = krb5_cc_resolve(pam_context, cache_data, &ccache_temp);
409 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)cache_data,
410 krb5_get_err_text(pam_context, krbret));
411 retval = PAM_SERVICE_ERR;
415 /* Get the uid. This should exist. */
416 pwd = getpwnam(user);
418 retval = PAM_USER_UNKNOWN;
422 PAM_LOG("Done getpwnam()");
424 /* Avoid following a symlink as root */
425 if (setegid(pwd->pw_gid)) {
426 retval = PAM_SERVICE_ERR;
429 if (seteuid(pwd->pw_uid)) {
430 retval = PAM_SERVICE_ERR;
434 PAM_LOG("Done setegid() & seteuid()");
436 /* Get the cache name */
437 cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
438 if (cache_name == NULL) {
439 asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
440 cache_name = cache_name_buf;
443 p = calloc(PATH_MAX + 16, sizeof(char));
447 PAM_LOG("Error malloc(): failure");
448 retval = PAM_BUF_ERR;
453 /* convert %u and %p */
458 sprintf(p, "%d", pwd->pw_uid);
461 else if (*q == 'p') {
462 sprintf(p, "%d", getpid());
466 /* Not a special token */
477 PAM_LOG("Got cache_name: %s", cache_name);
479 /* Initialize the new ccache */
480 krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
482 PAM_LOG("Error krb5_cc_get_principal(): %s",
483 krb5_get_err_text(pam_context, krbret));
484 retval = PAM_SERVICE_ERR;
487 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
489 PAM_LOG("Error krb5_cc_resolve(): %s",
490 krb5_get_err_text(pam_context, krbret));
491 retval = PAM_SERVICE_ERR;
494 krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
496 PAM_LOG("Error krb5_cc_initialize(): %s",
497 krb5_get_err_text(pam_context, krbret));
498 retval = PAM_SERVICE_ERR;
502 PAM_LOG("Cache initialised");
504 /* Prepare for iteration over creds */
505 krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
507 PAM_LOG("Error krb5_cc_start_seq_get(): %s",
508 krb5_get_err_text(pam_context, krbret));
509 krb5_cc_destroy(pam_context, ccache_perm);
510 retval = PAM_SERVICE_ERR;
514 PAM_LOG("Prepared for iteration");
516 /* Copy the creds (should be two of them) */
517 while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
518 &cursor, &creds) == 0)) {
519 krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
521 PAM_LOG("Error krb5_cc_store_cred(): %s",
522 krb5_get_err_text(pam_context, krbret));
523 krb5_cc_destroy(pam_context, ccache_perm);
524 krb5_free_cred_contents(pam_context, &creds);
525 retval = PAM_SERVICE_ERR;
528 krb5_free_cred_contents(pam_context, &creds);
529 PAM_LOG("Iteration");
531 krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
533 PAM_LOG("Done iterating");
535 if (strstr(cache_name, "FILE:") == cache_name) {
536 if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) {
537 PAM_LOG("Error chown(): %s", strerror(errno));
538 krb5_cc_destroy(pam_context, ccache_perm);
539 retval = PAM_SERVICE_ERR;
542 PAM_LOG("Done chown()");
544 if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) {
545 PAM_LOG("Error chmod(): %s", strerror(errno));
546 krb5_cc_destroy(pam_context, ccache_perm);
547 retval = PAM_SERVICE_ERR;
550 PAM_LOG("Done chmod()");
553 krb5_cc_close(pam_context, ccache_perm);
555 PAM_LOG("Cache closed");
557 retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1);
558 if (retval != PAM_SUCCESS) {
559 PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
560 krb5_cc_destroy(pam_context, ccache_perm);
561 retval = PAM_SERVICE_ERR;
565 PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
568 krb5_free_principal(pam_context, princ);
569 PAM_LOG("Done cleanup2");
571 krb5_free_context(pam_context);
572 PAM_LOG("Done cleanup3");
577 PAM_LOG("Done seteuid() & setegid()");
579 if (cache_name_buf != NULL)
580 free(cache_name_buf);
590 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
591 int argc __unused, const char *argv[] __unused)
593 krb5_error_code krbret;
594 krb5_context pam_context;
596 krb5_principal princ;
599 const void *ccache_name;
601 retval = pam_get_item(pamh, PAM_USER, &user);
602 if (retval != PAM_SUCCESS)
605 PAM_LOG("Got user: %s", (const char *)user);
607 retval = pam_get_data(pamh, "ccache", &ccache_name);
608 if (retval != PAM_SUCCESS)
609 return (PAM_SUCCESS);
611 PAM_LOG("Got credentials");
613 krbret = krb5_init_context(&pam_context);
615 PAM_LOG("Error krb5_init_context() failed");
616 return (PAM_PERM_DENIED);
619 PAM_LOG("Context initialised");
621 krbret = krb5_cc_resolve(pam_context, (const char *)ccache_name, &ccache);
623 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)ccache_name,
624 krb5_get_err_text(pam_context, krbret));
625 krb5_free_context(pam_context);
626 return (PAM_PERM_DENIED);
629 PAM_LOG("Got ccache %s", (const char *)ccache_name);
632 krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
634 PAM_LOG("Error krb5_cc_get_principal(): %s",
635 krb5_get_err_text(pam_context, krbret));
636 retval = PAM_PERM_DENIED;;
640 PAM_LOG("Got principal");
642 if (krb5_kuserok(pam_context, princ, (const char *)user))
643 retval = PAM_SUCCESS;
645 retval = PAM_PERM_DENIED;
646 krb5_free_principal(pam_context, princ);
648 PAM_LOG("Done kuserok()");
651 krb5_free_context(pam_context);
652 PAM_LOG("Done cleanup");
659 * password management
662 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
663 int argc __unused, const char *argv[] __unused)
665 krb5_error_code krbret;
666 krb5_context pam_context;
668 krb5_principal princ;
669 krb5_get_init_creds_opt opts;
670 krb5_data result_code_string, result_string;
671 int result_code, retval;
674 char *princ_name, *passdup;
676 if (!(flags & PAM_UPDATE_AUTHTOK))
677 return (PAM_AUTHTOK_ERR);
679 retval = pam_get_item(pamh, PAM_USER, &user);
680 if (retval != PAM_SUCCESS)
683 PAM_LOG("Got user: %s", (const char *)user);
685 krbret = krb5_init_context(&pam_context);
687 PAM_LOG("Error krb5_init_context() failed");
688 return (PAM_SERVICE_ERR);
691 PAM_LOG("Context initialised");
693 krb5_get_init_creds_opt_init(&opts);
695 PAM_LOG("Credentials options initialised");
697 /* Get principal name */
698 krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
700 PAM_LOG("Error krb5_parse_name(): %s",
701 krb5_get_err_text(pam_context, krbret));
702 retval = PAM_USER_UNKNOWN;
706 /* Now convert the principal name into something human readable */
708 krbret = krb5_unparse_name(pam_context, princ, &princ_name);
710 PAM_LOG("Error krb5_unparse_name(): %s",
711 krb5_get_err_text(pam_context, krbret));
712 retval = PAM_SERVICE_ERR;
716 PAM_LOG("Got principal: %s", princ_name);
719 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, PASSWORD_PROMPT);
720 if (retval != PAM_SUCCESS)
723 PAM_LOG("Got password");
725 memset(&creds, 0, sizeof(krb5_creds));
726 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
727 pass, NULL, pamh, 0, "kadmin/changepw", &opts);
729 PAM_LOG("Error krb5_get_init_creds_password(): %s",
730 krb5_get_err_text(pam_context, krbret));
731 retval = PAM_AUTH_ERR;
735 PAM_LOG("Credentials established");
737 /* Now get the new password */
739 retval = pam_get_authtok(pamh,
740 PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
741 if (retval != PAM_TRY_AGAIN)
743 pam_error(pamh, "Mismatch; try again, EOF to quit.");
745 if (retval != PAM_SUCCESS)
748 PAM_LOG("Got new password");
751 if ((passdup = strdup(pass)) == NULL) {
752 retval = PAM_BUF_ERR;
755 krbret = krb5_change_password(pam_context, &creds, passdup,
756 &result_code, &result_code_string, &result_string);
759 PAM_LOG("Error krb5_change_password(): %s",
760 krb5_get_err_text(pam_context, krbret));
761 retval = PAM_AUTHTOK_ERR;
765 PAM_LOG("Error krb5_change_password(): (result_code)");
766 retval = PAM_AUTHTOK_ERR;
770 PAM_LOG("Password changed");
772 if (result_string.data)
773 free(result_string.data);
774 if (result_code_string.data)
775 free(result_code_string.data);
778 krb5_free_cred_contents(pam_context, &creds);
779 PAM_LOG("Done cleanup");
781 krb5_free_principal(pam_context, princ);
782 PAM_LOG("Done cleanup2");
787 krb5_free_context(pam_context);
789 PAM_LOG("Done cleanup3");
794 PAM_MODULE_ENTRY("pam_krb5");
797 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
798 * Modified by Sam Hartman <hartmans@mit.edu> to support PAM services
801 * Verify the Kerberos ticket-granting ticket just retrieved for the
802 * user. If the Kerberos server doesn't respond, assume the user is
803 * trying to fake us out (since we DID just get a TGT from what is
804 * supposedly our KDC). If the host/<host> service is unknown (i.e.,
805 * the local keytab doesn't have it), and we cannot find another
806 * service we do have, let her in.
808 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
812 verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
813 char *pam_service, int debug)
815 krb5_error_code retval;
816 krb5_principal princ;
817 krb5_keyblock *keyblock;
819 krb5_auth_context auth_context;
821 const char *services[3], **service;
825 /* If possible we want to try and verify the ticket we have
826 * received against a keytab. We will try multiple service
827 * principals, including at least the host principal and the PAM
828 * service principal. The host principal is preferred because access
829 * to that key is generally sufficient to compromise root, while the
830 * service key for this PAM service may be less carefully guarded.
831 * It is important to check the keytab first before the KDC so we do
832 * not get spoofed by a fake KDC.
834 services[0] = "host";
835 services[1] = pam_service;
839 for (service = &services[0]; *service != NULL; service++) {
840 retval = krb5_sname_to_principal(context, NULL, *service,
841 KRB5_NT_SRV_HST, &princ);
845 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
846 "krb5_sname_to_principal()",
847 krb5_get_err_text(context, retval));
851 /* Extract the name directly. */
852 strncpy(phost, compat_princ_component(context, princ, 1),
854 phost[BUFSIZ - 1] = '\0';
857 * Do we have service/<host> keys?
858 * (use default/configured keytab, kvno IGNORE_VNO to get the
859 * first match, and ignore enctype.)
861 retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
867 if (retval != 0) { /* failed to find key */
868 /* Keytab or service key does not exist */
871 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
872 "krb5_kt_read_service_key()",
873 krb5_get_err_text(context, retval));
878 krb5_free_keyblock(context, keyblock);
880 /* Talk to the kdc and construct the ticket. */
882 retval = krb5_mk_req(context, &auth_context, 0, *service, phost,
883 NULL, ccache, &packet);
885 krb5_auth_con_free(context, auth_context);
886 auth_context = NULL; /* setup for rd_req */
891 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
893 krb5_get_err_text(context, retval));
898 /* Try to use the ticket. */
899 retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
904 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
906 krb5_get_err_text(context, retval));
914 compat_free_data_contents(context, &packet);
915 krb5_free_principal(context, princ);
919 /* Free the memory for cache_name. Called by pam_end() */
922 cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
924 krb5_context pam_context;
926 krb5_error_code krbret;
928 if (krb5_init_context(&pam_context))
931 krbret = krb5_cc_resolve(pam_context, data, &ccache);
933 krb5_cc_destroy(pam_context, ccache);
934 krb5_free_context(pam_context);
938 #ifdef COMPAT_HEIMDAL
940 #error This cannot be MIT and Heimdal compatible!
944 #ifndef COMPAT_HEIMDAL
946 #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
950 #ifdef COMPAT_HEIMDAL
953 compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
955 return princ->name.name_string.val[n];
960 compat_free_data_contents(krb5_context context __unused, krb5_data * data)
962 krb5_xfree(data->data);
968 compat_princ_component(krb5_context context, krb5_principal princ, int n)
970 return krb5_princ_component(context, princ, n)->data;
974 compat_free_data_contents(krb5_context context, krb5_data * data)
976 krb5_free_data_contents(context, data);