1 /* $NetBSD: pam_krb5.c,v 1.21 2008/01/27 01:23:20 christos Exp $ */
4 * This pam_krb5 module contains code that is:
5 * Copyright (c) Derrick J. Brashear, 1996. All rights reserved.
6 * Copyright (c) Frank Cusack, 1999-2001. All rights reserved.
7 * Copyright (c) Jacques A. Vidrine, 2000-2001. All rights reserved.
8 * Copyright (c) Nicolas Williams, 2001. All rights reserved.
9 * Copyright (c) Perot Systems Corporation, 2001. All rights reserved.
10 * Copyright (c) Mark R V Murray, 2001. All rights reserved.
11 * Copyright (c) Networks Associates Technology, Inc., 2002-2005.
12 * All rights reserved.
14 * Portions of this software were developed for the FreeBSD Project by
15 * ThinkSec AS and NAI Labs, the Security Research Division of Network
16 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
17 * ("CBOSS"), as part of the DARPA CHATS research program.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notices, and the entire permission notice in its entirety,
24 * including the disclaimer of warranties.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. The name of the author may not be used to endorse or promote
29 * products derived from this software without specific prior
32 * ALTERNATIVELY, this product may be distributed under the terms of
33 * the GNU Public License, in which case the provisions of the GPL are
34 * required INSTEAD OF the above restrictions. (This clause is
35 * necessary due to a potential bad interaction between the GPL and
36 * the restrictions contained in a BSD-style copyright.)
38 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
42 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
44 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_krb5/pam_krb5.c,v 1.22 2005/01/24 16:49:50 rwatson Exp $");
56 __RCSID("$NetBSD: pam_krb5.c,v 1.21 2008/01/27 01:23:20 christos Exp $");
59 #include <sys/types.h>
70 #include <krb5/krb5.h>
71 #include <krb5/com_err.h>
72 #include <krb5/parse_time.h>
75 #define PAM_SM_ACCOUNT
76 #define PAM_SM_PASSWORD
78 #include <security/pam_appl.h>
79 #include <security/pam_modules.h>
80 #include <security/pam_mod_misc.h>
81 #include <security/openpam.h>
83 #define COMPAT_HEIMDAL
84 /* #define COMPAT_MIT */
86 static int verify_krb_v5_tgt(krb5_context
, krb5_ccache
, char *, int);
87 static void cleanup_cache(pam_handle_t
*, void *, int);
88 static const char *compat_princ_component(krb5_context
, krb5_principal
, int);
89 static void compat_free_data_contents(krb5_context
, krb5_data
*);
91 #define USER_PROMPT "Username: "
92 #define PASSWORD_PROMPT "%s's password:"
93 #define NEW_PASSWORD_PROMPT "New Password:"
95 #define PAM_OPT_CCACHE "ccache"
96 #define PAM_OPT_DEBUG "debug"
97 #define PAM_OPT_FORWARDABLE "forwardable"
98 #define PAM_OPT_RENEWABLE "renewable"
99 #define PAM_OPT_NO_CCACHE "no_ccache"
100 #define PAM_OPT_REUSE_CCACHE "reuse_ccache"
103 * authentication management
106 pam_sm_authenticate(pam_handle_t
*pamh
, int flags __unused
,
107 int argc __unused
, const char *argv
[] __unused
)
109 krb5_error_code krbret
;
110 krb5_context pam_context
;
112 krb5_principal princ
;
114 krb5_get_init_creds_opt opts
;
115 struct passwd
*pwd
, pwres
;
117 const void *ccache_data
;
118 const char *user
, *pass
;
119 const void *sourceuser
, *service
;
120 char *principal
, *princ_name
, *ccache_name
, luser
[32], *srvdup
;
121 char password_prompt
[80];
126 retval
= pam_get_user(pamh
, &user
, USER_PROMPT
);
127 if (retval
!= PAM_SUCCESS
)
130 PAM_LOG("Got user: %s", user
);
132 retval
= pam_get_item(pamh
, PAM_RUSER
, &sourceuser
);
133 if (retval
!= PAM_SUCCESS
)
136 PAM_LOG("Got ruser: %s", (const char *)sourceuser
);
139 pam_get_item(pamh
, PAM_SERVICE
, &service
);
143 PAM_LOG("Got service: %s", (const char *)service
);
145 krbret
= krb5_init_context(&pam_context
);
147 PAM_VERBOSE_ERROR("Kerberos 5 error");
148 return (PAM_SERVICE_ERR
);
151 PAM_LOG("Context initialised");
153 krb5_get_init_creds_opt_init(&opts
);
155 if (openpam_get_option(pamh
, PAM_OPT_FORWARDABLE
))
156 krb5_get_init_creds_opt_set_forwardable(&opts
, 1);
158 if ((rtime
= openpam_get_option(pamh
, PAM_OPT_RENEWABLE
)) != NULL
) {
163 (void)strlcpy(rbuf
, rtime
, sizeof(rbuf
));
165 for (rp
= rbuf
; *rp
; rp
++)
171 renew
= parse_time(rtime
, "s");
172 krb5_get_init_creds_opt_set_renew_life(&opts
, renew
);
177 PAM_LOG("Credentials initialised");
179 krbret
= krb5_cc_register(pam_context
, &krb5_mcc_ops
, FALSE
);
180 if (krbret
!= 0 && krbret
!= KRB5_CC_TYPE_EXISTS
) {
181 PAM_VERBOSE_ERROR("Kerberos 5 error");
182 retval
= PAM_SERVICE_ERR
;
186 PAM_LOG("Done krb5_cc_register()");
188 /* Get principal name */
189 if (openpam_get_option(pamh
, PAM_OPT_AUTH_AS_SELF
))
190 asprintf(&principal
, "%s/%s", (const char *)sourceuser
, user
);
192 principal
= strdup(user
);
194 PAM_LOG("Created principal: %s", principal
);
196 krbret
= krb5_parse_name(pam_context
, principal
, &princ
);
199 PAM_LOG("Error krb5_parse_name(): %s",
200 krb5_get_err_text(pam_context
, krbret
));
201 PAM_VERBOSE_ERROR("Kerberos 5 error");
202 retval
= PAM_SERVICE_ERR
;
206 PAM_LOG("Done krb5_parse_name()");
208 /* Now convert the principal name into something human readable */
209 krbret
= krb5_unparse_name(pam_context
, princ
, &princ_name
);
211 PAM_LOG("Error krb5_unparse_name(): %s",
212 krb5_get_err_text(pam_context
, krbret
));
213 PAM_VERBOSE_ERROR("Kerberos 5 error");
214 retval
= PAM_SERVICE_ERR
;
218 PAM_LOG("Got principal: %s", princ_name
);
221 (void) snprintf(password_prompt
, sizeof(password_prompt
),
222 PASSWORD_PROMPT
, princ_name
);
223 retval
= pam_get_authtok(pamh
, PAM_AUTHTOK
, &pass
, password_prompt
);
224 if (retval
!= PAM_SUCCESS
)
227 PAM_LOG("Got password");
229 /* Verify the local user exists (AFTER getting the password) */
230 if (strchr(user
, '@')) {
231 /* get a local account name for this principal */
232 krbret
= krb5_aname_to_localname(pam_context
, princ
,
233 sizeof(luser
), luser
);
235 PAM_VERBOSE_ERROR("Kerberos 5 error");
236 PAM_LOG("Error krb5_aname_to_localname(): %s",
237 krb5_get_err_text(pam_context
, krbret
));
238 retval
= PAM_USER_UNKNOWN
;
242 retval
= pam_set_item(pamh
, PAM_USER
, luser
);
243 if (retval
!= PAM_SUCCESS
)
246 PAM_LOG("PAM_USER Redone");
249 if (getpwnam_r(user
, &pwres
, pwbuf
, sizeof(pwbuf
), &pwd
) != 0 ||
251 retval
= PAM_USER_UNKNOWN
;
255 PAM_LOG("Done getpwnam_r()");
258 memset(&creds
, 0, sizeof(krb5_creds
));
259 krbret
= krb5_get_init_creds_password(pam_context
, &creds
, princ
,
260 pass
, NULL
, pamh
, 0, NULL
, &opts
);
262 PAM_VERBOSE_ERROR("Kerberos 5 error");
263 PAM_LOG("Error krb5_get_init_creds_password(): %s",
264 krb5_get_err_text(pam_context
, krbret
));
265 retval
= PAM_AUTH_ERR
;
271 /* Generate a temporary cache */
272 krbret
= krb5_cc_gen_new(pam_context
, &krb5_mcc_ops
, &ccache
);
274 PAM_VERBOSE_ERROR("Kerberos 5 error");
275 PAM_LOG("Error krb5_cc_gen_new(): %s",
276 krb5_get_err_text(pam_context
, krbret
));
277 retval
= PAM_SERVICE_ERR
;
280 krbret
= krb5_cc_initialize(pam_context
, ccache
, princ
);
282 PAM_VERBOSE_ERROR("Kerberos 5 error");
283 PAM_LOG("Error krb5_cc_initialize(): %s",
284 krb5_get_err_text(pam_context
, krbret
));
285 retval
= PAM_SERVICE_ERR
;
288 krbret
= krb5_cc_store_cred(pam_context
, ccache
, &creds
);
290 PAM_VERBOSE_ERROR("Kerberos 5 error");
291 PAM_LOG("Error krb5_cc_store_cred(): %s",
292 krb5_get_err_text(pam_context
, krbret
));
293 krb5_cc_destroy(pam_context
, ccache
);
294 retval
= PAM_SERVICE_ERR
;
298 PAM_LOG("Credentials stashed");
301 if ((srvdup
= strdup(service
)) == NULL
) {
302 retval
= PAM_BUF_ERR
;
305 krbret
= verify_krb_v5_tgt(pam_context
, ccache
, srvdup
,
306 openpam_get_option(pamh
, PAM_OPT_DEBUG
) ? 1 : 0);
309 PAM_VERBOSE_ERROR("Kerberos 5 error");
310 krb5_cc_destroy(pam_context
, ccache
);
311 retval
= PAM_AUTH_ERR
;
315 PAM_LOG("Credentials stash verified");
317 retval
= pam_get_data(pamh
, "ccache", &ccache_data
);
318 if (retval
== PAM_SUCCESS
) {
319 krb5_cc_destroy(pam_context
, ccache
);
320 PAM_VERBOSE_ERROR("Kerberos 5 error");
321 retval
= PAM_AUTH_ERR
;
325 PAM_LOG("Credentials stash not pre-existing");
327 asprintf(&ccache_name
, "%s:%s", krb5_cc_get_type(pam_context
,
328 ccache
), krb5_cc_get_name(pam_context
, ccache
));
329 if (ccache_name
== NULL
) {
330 PAM_VERBOSE_ERROR("Kerberos 5 error");
331 retval
= PAM_BUF_ERR
;
334 retval
= pam_set_data(pamh
, "ccache", ccache_name
, cleanup_cache
);
336 krb5_cc_destroy(pam_context
, ccache
);
337 PAM_VERBOSE_ERROR("Kerberos 5 error");
338 retval
= PAM_SERVICE_ERR
;
342 PAM_LOG("Credentials stash saved");
345 krb5_free_cred_contents(pam_context
, &creds
);
346 PAM_LOG("Done cleanup");
348 krb5_free_principal(pam_context
, princ
);
349 PAM_LOG("Done cleanup2");
354 krb5_free_context(pam_context
);
356 PAM_LOG("Done cleanup3");
358 if (retval
!= PAM_SUCCESS
)
359 PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
365 pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
366 int argc __unused
, const char *argv
[] __unused
)
369 krb5_error_code krbret
;
370 krb5_context pam_context
;
371 krb5_principal princ
;
373 krb5_ccache ccache_temp
, ccache_perm
;
374 krb5_cc_cursor cursor
;
375 struct passwd
*pwd
= NULL
, pwres
;
377 const char *cache_name
, *q
;
379 const void *cache_data
;
380 char *cache_name_buf
= NULL
, *p
, *cache_name_buf2
= NULL
;
386 if (flags
& PAM_DELETE_CRED
)
387 return (PAM_SUCCESS
); /* XXX */
389 if (!(flags
& (PAM_REFRESH_CRED
|PAM_REINITIALIZE_CRED
|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 return (PAM_SUCCESS
);
396 PAM_LOG("Establishing credentials");
399 retval
= pam_get_item(pamh
, PAM_USER
, &user
);
400 if (retval
!= PAM_SUCCESS
)
403 PAM_LOG("Got user: %s", (const char *)user
);
405 krbret
= krb5_init_context(&pam_context
);
407 PAM_LOG("Error krb5_init_context() failed");
408 return (PAM_SERVICE_ERR
);
411 PAM_LOG("Context initialised");
413 euid
= geteuid(); /* Usually 0 */
416 PAM_LOG("Got euid, egid: %d %d", euid
, egid
);
418 /* Retrieve the temporary cache */
419 retval
= pam_get_data(pamh
, "ccache", &cache_data
);
420 if (retval
!= PAM_SUCCESS
) {
421 retval
= PAM_CRED_UNAVAIL
;
424 krbret
= krb5_cc_resolve(pam_context
, cache_data
, &ccache_temp
);
426 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)cache_data
,
427 krb5_get_err_text(pam_context
, krbret
));
428 retval
= PAM_SERVICE_ERR
;
432 /* Get the uid. This should exist. */
433 if (getpwnam_r(user
, &pwres
, pwbuf
, sizeof(pwbuf
), &pwd
) != 0 ||
435 retval
= PAM_USER_UNKNOWN
;
439 PAM_LOG("Done getpwnam_r()");
441 /* Avoid following a symlink as root */
442 if (setegid(pwd
->pw_gid
)) {
443 retval
= PAM_SERVICE_ERR
;
446 if (seteuid(pwd
->pw_uid
)) {
447 retval
= PAM_SERVICE_ERR
;
451 PAM_LOG("Done setegid() & seteuid()");
453 if (flags
& (PAM_REFRESH_CRED
|PAM_REINITIALIZE_CRED
)) {
454 cache_name
= getenv("KRB5CCNAME");
458 /* Get the cache name */
459 cache_name
= openpam_get_option(pamh
, PAM_OPT_CCACHE
);
460 if (cache_name
== NULL
) {
461 asprintf(&cache_name_buf
, "FILE:/tmp/krb5cc_%d", pwd
->pw_uid
);
462 cache_name
= cache_name_buf
;
465 /* XXX potential overflow */
466 cache_name_buf2
= p
= calloc(PATH_MAX
+ 16, sizeof(char));
470 PAM_LOG("Error malloc(): failure");
471 retval
= PAM_BUF_ERR
;
476 /* convert %u and %p */
481 sprintf(p
, "%d", pwd
->pw_uid
);
484 else if (*q
== 'p') {
485 sprintf(p
, "%d", getpid());
489 /* Not a special token */
501 PAM_LOG("Got cache_name: %s", cache_name
);
503 /* Initialize the new ccache */
504 krbret
= krb5_cc_get_principal(pam_context
, ccache_temp
, &princ
);
506 PAM_LOG("Error krb5_cc_get_principal(): %s",
507 krb5_get_err_text(pam_context
, krbret
));
508 retval
= PAM_SERVICE_ERR
;
511 krbret
= krb5_cc_resolve(pam_context
, cache_name
, &ccache_perm
);
513 PAM_LOG("Error krb5_cc_resolve(): %s",
514 krb5_get_err_text(pam_context
, krbret
));
515 retval
= PAM_SERVICE_ERR
;
519 krbret
= krb5_cc_initialize(pam_context
, ccache_perm
, princ
);
521 PAM_LOG("Error krb5_cc_initialize(): %s",
522 krb5_get_err_text(pam_context
, krbret
));
523 retval
= PAM_SERVICE_ERR
;
527 PAM_LOG("Cache initialised");
529 /* Prepare for iteration over creds */
530 krbret
= krb5_cc_start_seq_get(pam_context
, ccache_temp
, &cursor
);
532 PAM_LOG("Error krb5_cc_start_seq_get(): %s",
533 krb5_get_err_text(pam_context
, krbret
));
534 krb5_cc_destroy(pam_context
, ccache_perm
);
535 retval
= PAM_SERVICE_ERR
;
539 PAM_LOG("Prepared for iteration");
541 /* Copy the creds (should be two of them) */
542 while ((krbret
= krb5_cc_next_cred(pam_context
, ccache_temp
,
543 &cursor
, &creds
) == 0)) {
545 krbret
= krb5_cc_store_cred(pam_context
, ccache_perm
, &creds
);
547 PAM_LOG("Error krb5_cc_store_cred(): %s",
548 krb5_get_err_text(pam_context
, krbret
));
549 krb5_cc_destroy(pam_context
, ccache_perm
);
550 krb5_free_cred_contents(pam_context
, &creds
);
551 retval
= PAM_SERVICE_ERR
;
555 krb5_free_cred_contents(pam_context
, &creds
);
556 PAM_LOG("Iteration");
559 krb5_cc_end_seq_get(pam_context
, ccache_temp
, &cursor
);
561 PAM_LOG("Done iterating");
563 if (flags
& PAM_ESTABLISH_CRED
) {
564 if (strstr(cache_name
, "FILE:") == cache_name
) {
565 if (chown(&cache_name
[5], pwd
->pw_uid
, pwd
->pw_gid
) == -1) {
566 PAM_LOG("Error chown(): %s", strerror(errno
));
567 krb5_cc_destroy(pam_context
, ccache_perm
);
568 retval
= PAM_SERVICE_ERR
;
571 PAM_LOG("Done chown()");
573 if (chmod(&cache_name
[5], (S_IRUSR
| S_IWUSR
)) == -1) {
574 PAM_LOG("Error chmod(): %s", strerror(errno
));
575 krb5_cc_destroy(pam_context
, ccache_perm
);
576 retval
= PAM_SERVICE_ERR
;
579 PAM_LOG("Done chmod()");
583 krb5_cc_close(pam_context
, ccache_perm
);
585 PAM_LOG("Cache closed");
587 retval
= pam_setenv(pamh
, "KRB5CCNAME", cache_name
, 1);
588 if (retval
!= PAM_SUCCESS
) {
589 PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh
, retval
));
590 retval
= PAM_SERVICE_ERR
;
594 PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name
);
597 krb5_free_principal(pam_context
, princ
);
598 PAM_LOG("Done cleanup2");
600 krb5_free_context(pam_context
);
601 PAM_LOG("Done cleanup3");
606 PAM_LOG("Done seteuid() & setegid()");
608 if (cache_name_buf
!= NULL
)
609 free(cache_name_buf
);
610 if (cache_name_buf2
!= NULL
)
611 free(cache_name_buf2
);
620 pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags __unused
,
621 int argc __unused
, const char *argv
[] __unused
)
623 krb5_error_code krbret
;
624 krb5_context pam_context
;
626 krb5_principal princ
;
629 const void *ccache_name
;
631 retval
= pam_get_item(pamh
, PAM_USER
, &user
);
632 if (retval
!= PAM_SUCCESS
)
635 PAM_LOG("Got user: %s", (const char *)user
);
637 retval
= pam_get_data(pamh
, "ccache", &ccache_name
);
638 if (retval
!= PAM_SUCCESS
)
639 return (PAM_SUCCESS
);
641 PAM_LOG("Got credentials");
643 krbret
= krb5_init_context(&pam_context
);
645 PAM_LOG("Error krb5_init_context() failed");
646 return (PAM_PERM_DENIED
);
649 PAM_LOG("Context initialised");
651 krbret
= krb5_cc_resolve(pam_context
, (const char *)ccache_name
, &ccache
);
653 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)ccache_name
,
654 krb5_get_err_text(pam_context
, krbret
));
655 krb5_free_context(pam_context
);
656 return (PAM_PERM_DENIED
);
659 PAM_LOG("Got ccache %s", (const char *)ccache_name
);
662 krbret
= krb5_cc_get_principal(pam_context
, ccache
, &princ
);
664 PAM_LOG("Error krb5_cc_get_principal(): %s",
665 krb5_get_err_text(pam_context
, krbret
));
666 retval
= PAM_PERM_DENIED
;;
670 PAM_LOG("Got principal");
672 if (krb5_kuserok(pam_context
, princ
, (const char *)user
))
673 retval
= PAM_SUCCESS
;
675 retval
= PAM_PERM_DENIED
;
676 krb5_free_principal(pam_context
, princ
);
678 PAM_LOG("Done kuserok()");
681 krb5_free_context(pam_context
);
682 PAM_LOG("Done cleanup");
689 * password management
692 pam_sm_chauthtok(pam_handle_t
*pamh
, int flags
,
693 int argc __unused
, const char *argv
[] __unused
)
695 krb5_error_code krbret
;
696 krb5_context pam_context
;
698 krb5_principal princ
;
699 krb5_get_init_creds_opt opts
;
700 krb5_data result_code_string
, result_string
;
701 int result_code
, retval
;
704 char *princ_name
, *passdup
;
705 char password_prompt
[80];
708 if (flags
& PAM_PRELIM_CHECK
) {
709 /* Nothing to do here. */
710 return (PAM_SUCCESS
);
713 if (!(flags
& PAM_UPDATE_AUTHTOK
)) {
714 PAM_LOG("Illegal flags argument");
718 retval
= pam_get_item(pamh
, PAM_USER
, &user
);
719 if (retval
!= PAM_SUCCESS
)
722 PAM_LOG("Got user: %s", (const char *)user
);
724 krbret
= krb5_init_context(&pam_context
);
726 PAM_LOG("Error krb5_init_context() failed");
727 return (PAM_SERVICE_ERR
);
730 PAM_LOG("Context initialised");
732 krb5_get_init_creds_opt_init(&opts
);
734 krb5_get_init_creds_opt_set_tkt_life(&opts
, 300);
735 krb5_get_init_creds_opt_set_forwardable(&opts
, FALSE
);
736 krb5_get_init_creds_opt_set_proxiable(&opts
, FALSE
);
738 PAM_LOG("Credentials options initialised");
740 /* Get principal name */
741 krbret
= krb5_parse_name(pam_context
, (const char *)user
, &princ
);
743 PAM_LOG("Error krb5_parse_name(): %s",
744 krb5_get_err_text(pam_context
, krbret
));
745 retval
= PAM_USER_UNKNOWN
;
749 /* Now convert the principal name into something human readable */
750 krbret
= krb5_unparse_name(pam_context
, princ
, &princ_name
);
752 PAM_LOG("Error krb5_unparse_name(): %s",
753 krb5_get_err_text(pam_context
, krbret
));
754 retval
= PAM_SERVICE_ERR
;
758 PAM_LOG("Got principal: %s", princ_name
);
761 (void) snprintf(password_prompt
, sizeof(password_prompt
),
762 PASSWORD_PROMPT
, princ_name
);
763 retval
= pam_get_authtok(pamh
, PAM_OLDAUTHTOK
, &pass
, password_prompt
);
764 if (retval
!= PAM_SUCCESS
)
767 PAM_LOG("Got password");
769 memset(&creds
, 0, sizeof(krb5_creds
));
770 krbret
= krb5_get_init_creds_password(pam_context
, &creds
, princ
,
771 pass
, NULL
, pamh
, 0, "kadmin/changepw", &opts
);
773 PAM_LOG("Error krb5_get_init_creds_password(): %s",
774 krb5_get_err_text(pam_context
, krbret
));
775 retval
= PAM_AUTH_ERR
;
779 PAM_LOG("Credentials established");
781 /* Now get the new password */
783 retval
= pam_get_authtok(pamh
,
784 PAM_AUTHTOK
, &pass
, NEW_PASSWORD_PROMPT
);
785 if (retval
!= PAM_TRY_AGAIN
)
787 pam_error(pamh
, "Mismatch; try again, EOF to quit.");
789 if (retval
!= PAM_SUCCESS
)
792 PAM_LOG("Got new password");
795 if ((passdup
= strdup(pass
)) == NULL
) {
796 retval
= PAM_BUF_ERR
;
800 krb5_data_zero(&result_code_string
);
801 krb5_data_zero(&result_string
);
803 krbret
= krb5_change_password(pam_context
, &creds
, passdup
,
804 &result_code
, &result_code_string
, &result_string
);
807 pam_error(pamh
, "Unable to set password: %s",
808 krb5_get_err_text(pam_context
, krbret
));
809 retval
= PAM_AUTHTOK_ERR
;
813 pam_info(pamh
, "%s%s%.*s",
814 krb5_passwd_result_to_string(pam_context
, result_code
),
815 result_string
.length
> 0 ? ": " : "",
816 (int)result_string
.length
,
817 result_string
.length
> 0 ? (char *)result_string
.data
: "");
818 retval
= PAM_AUTHTOK_ERR
;
820 PAM_LOG("Password changed");
823 krb5_data_free(&result_string
);
824 krb5_data_free(&result_code_string
);
827 krb5_free_cred_contents(pam_context
, &creds
);
828 PAM_LOG("Done cleanup");
830 krb5_free_principal(pam_context
, princ
);
831 PAM_LOG("Done cleanup2");
836 krb5_free_context(pam_context
);
838 PAM_LOG("Done cleanup3");
843 PAM_MODULE_ENTRY("pam_krb5");
846 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
847 * Modified by Sam Hartman <hartmans@mit.edu> to support PAM services
850 * Verify the Kerberos ticket-granting ticket just retrieved for the
851 * user. If the Kerberos server doesn't respond, assume the user is
852 * trying to fake us out (since we DID just get a TGT from what is
853 * supposedly our KDC). If the host/<host> service is unknown (i.e.,
854 * the local keytab doesn't have it), and we cannot find another
855 * service we do have, let her in.
857 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
861 verify_krb_v5_tgt(krb5_context context
, krb5_ccache ccache
,
862 char *pam_service
, int debug
)
864 krb5_error_code retval
;
865 krb5_principal princ
;
866 krb5_keyblock
*keyblock
;
868 krb5_auth_context auth_context
= NULL
;
870 const char *services
[3], **service
;
871 struct syslog_data data
= SYSLOG_DATA_INIT
;
876 openlog_r("pam_krb5", LOG_PID
, LOG_AUTHPRIV
, &data
);
878 /* If possible we want to try and verify the ticket we have
879 * received against a keytab. We will try multiple service
880 * principals, including at least the host principal and the PAM
881 * service principal. The host principal is preferred because access
882 * to that key is generally sufficient to compromise root, while the
883 * service key for this PAM service may be less carefully guarded.
884 * It is important to check the keytab first before the KDC so we do
885 * not get spoofed by a fake KDC.
887 services
[0] = "host";
888 services
[1] = pam_service
;
892 for (service
= &services
[0]; *service
!= NULL
; service
++) {
893 retval
= krb5_sname_to_principal(context
, NULL
, *service
,
894 KRB5_NT_SRV_HST
, &princ
);
897 syslog_r(LOG_DEBUG
, &data
,
898 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
899 "krb5_sname_to_principal()",
900 krb5_get_err_text(context
, retval
));
904 /* Extract the name directly. */
905 strncpy(phost
, compat_princ_component(context
, princ
, 1),
907 phost
[BUFSIZ
- 1] = '\0';
910 * Do we have service/<host> keys?
911 * (use default/configured keytab, kvno IGNORE_VNO to get the
912 * first match, and ignore enctype.)
914 retval
= krb5_kt_read_service_key(context
, NULL
, princ
, 0, 0,
920 if (retval
!= 0) { /* failed to find key */
921 /* Keytab or service key does not exist */
923 syslog_r(LOG_DEBUG
, &data
,
924 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
925 "krb5_kt_read_service_key()",
926 krb5_get_err_text(context
, retval
));
931 krb5_free_keyblock(context
, keyblock
);
933 /* Talk to the kdc and construct the ticket. */
935 retval
= krb5_mk_req(context
, &auth_context
, 0, *service
, phost
,
936 NULL
, ccache
, &packet
);
938 krb5_auth_con_free(context
, auth_context
);
939 auth_context
= NULL
; /* setup for rd_req */
943 syslog_r(LOG_DEBUG
, &data
,
944 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
946 krb5_get_err_text(context
, retval
));
951 /* Try to use the ticket. */
952 retval
= krb5_rd_req(context
, &auth_context
, &packet
, princ
, NULL
,
956 syslog_r(LOG_DEBUG
, &data
,
957 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
959 krb5_get_err_text(context
, retval
));
969 compat_free_data_contents(context
, &packet
);
971 krb5_auth_con_free(context
, auth_context
);
972 auth_context
= NULL
; /* setup for rd_req */
974 krb5_free_principal(context
, princ
);
978 /* Free the memory for cache_name. Called by pam_end() */
981 cleanup_cache(pam_handle_t
*pamh __unused
, void *data
, int pam_end_status __unused
)
983 krb5_context pam_context
;
985 krb5_error_code krbret
;
987 if (krb5_init_context(&pam_context
))
990 krbret
= krb5_cc_resolve(pam_context
, data
, &ccache
);
992 krb5_cc_destroy(pam_context
, ccache
);
993 krb5_free_context(pam_context
);
997 #ifdef COMPAT_HEIMDAL
999 #error This cannot be MIT and Heimdal compatible!
1003 #ifndef COMPAT_HEIMDAL
1005 #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
1009 #ifdef COMPAT_HEIMDAL
1012 compat_princ_component(krb5_context context __unused
, krb5_principal princ
, int n
)
1014 return princ
->name
.name_string
.val
[n
];
1019 compat_free_data_contents(krb5_context context __unused
, krb5_data
* data
)
1021 krb5_xfree(data
->data
);
1027 compat_princ_component(krb5_context context
, krb5_principal princ
, int n
)
1029 return krb5_princ_component(context
, princ
, n
)->data
;
1033 compat_free_data_contents(krb5_context context
, krb5_data
* data
)
1035 krb5_free_data_contents(context
, data
);