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.
48 * $FreeBSD: src/lib/libpam/modules/pam_krb5/pam_krb5.c,v 1.23 2005/07/07 14:16:38 kensmith Exp $
49 * $DragonFly: src/lib/pam_module/pam_krb5/pam_krb5.c,v 1.2 2006/08/03 16:40:46 swildner Exp $
52 #include <sys/types.h>
67 #define PAM_SM_ACCOUNT
68 #define PAM_SM_PASSWORD
70 #include <security/pam_appl.h>
71 #include <security/pam_modules.h>
72 #include <security/pam_mod_misc.h>
73 #include <security/openpam.h>
75 #define COMPAT_HEIMDAL
76 /* #define COMPAT_MIT */
78 static int verify_krb_v5_tgt(krb5_context
, krb5_ccache
, char *, int);
79 static void cleanup_cache(pam_handle_t
*, void *, int);
80 static const char *compat_princ_component(krb5_context
, krb5_principal
, int);
81 static void compat_free_data_contents(krb5_context
, krb5_data
*);
83 #define USER_PROMPT "Username: "
84 #define PASSWORD_PROMPT "Password:"
85 #define NEW_PASSWORD_PROMPT "New Password:"
87 #define PAM_OPT_CCACHE "ccache"
88 #define PAM_OPT_DEBUG "debug"
89 #define PAM_OPT_FORWARDABLE "forwardable"
90 #define PAM_OPT_NO_CCACHE "no_ccache"
91 #define PAM_OPT_REUSE_CCACHE "reuse_ccache"
94 * authentication management
97 pam_sm_authenticate(pam_handle_t
*pamh
, int flags __unused
,
98 int argc __unused
, const char *argv
[] __unused
)
100 krb5_error_code krbret
;
101 krb5_context pam_context
;
103 krb5_principal princ
;
105 krb5_get_init_creds_opt opts
;
108 const void *ccache_data
;
109 const char *user
, *pass
;
110 const void *sourceuser
, *service
;
111 char *principal
, *princ_name
, *ccache_name
, luser
[32], *srvdup
;
113 retval
= pam_get_user(pamh
, &user
, USER_PROMPT
);
114 if (retval
!= PAM_SUCCESS
)
117 PAM_LOG("Got user: %s", user
);
119 retval
= pam_get_item(pamh
, PAM_RUSER
, &sourceuser
);
120 if (retval
!= PAM_SUCCESS
)
123 PAM_LOG("Got ruser: %s", (const char *)sourceuser
);
126 pam_get_item(pamh
, PAM_SERVICE
, &service
);
130 PAM_LOG("Got service: %s", (const char *)service
);
132 krbret
= krb5_init_context(&pam_context
);
134 PAM_VERBOSE_ERROR("Kerberos 5 error");
135 return (PAM_SERVICE_ERR
);
138 PAM_LOG("Context initialised");
140 krb5_get_init_creds_opt_init(&opts
);
142 if (openpam_get_option(pamh
, PAM_OPT_FORWARDABLE
))
143 krb5_get_init_creds_opt_set_forwardable(&opts
, 1);
145 PAM_LOG("Credentials initialised");
147 krbret
= krb5_cc_register(pam_context
, &krb5_mcc_ops
, FALSE
);
148 if (krbret
!= 0 && krbret
!= KRB5_CC_TYPE_EXISTS
) {
149 PAM_VERBOSE_ERROR("Kerberos 5 error");
150 retval
= PAM_SERVICE_ERR
;
154 PAM_LOG("Done krb5_cc_register()");
156 /* Get principal name */
157 if (openpam_get_option(pamh
, PAM_OPT_AUTH_AS_SELF
))
158 asprintf(&principal
, "%s/%s", (const char *)sourceuser
, user
);
160 principal
= strdup(user
);
162 PAM_LOG("Created principal: %s", principal
);
164 krbret
= krb5_parse_name(pam_context
, principal
, &princ
);
167 PAM_LOG("Error krb5_parse_name(): %s",
168 krb5_get_err_text(pam_context
, krbret
));
169 PAM_VERBOSE_ERROR("Kerberos 5 error");
170 retval
= PAM_SERVICE_ERR
;
174 PAM_LOG("Done krb5_parse_name()");
176 /* Now convert the principal name into something human readable */
178 krbret
= krb5_unparse_name(pam_context
, princ
, &princ_name
);
180 PAM_LOG("Error krb5_unparse_name(): %s",
181 krb5_get_err_text(pam_context
, krbret
));
182 PAM_VERBOSE_ERROR("Kerberos 5 error");
183 retval
= PAM_SERVICE_ERR
;
187 PAM_LOG("Got principal: %s", princ_name
);
190 retval
= pam_get_authtok(pamh
, PAM_AUTHTOK
, &pass
, PASSWORD_PROMPT
);
191 if (retval
!= PAM_SUCCESS
)
194 PAM_LOG("Got password");
196 /* Verify the local user exists (AFTER getting the password) */
197 if (strchr(user
, '@')) {
198 /* get a local account name for this principal */
199 krbret
= krb5_aname_to_localname(pam_context
, princ
,
200 sizeof(luser
), luser
);
202 PAM_VERBOSE_ERROR("Kerberos 5 error");
203 PAM_LOG("Error krb5_aname_to_localname(): %s",
204 krb5_get_err_text(pam_context
, krbret
));
205 retval
= PAM_USER_UNKNOWN
;
209 retval
= pam_set_item(pamh
, PAM_USER
, luser
);
210 if (retval
!= PAM_SUCCESS
)
213 PAM_LOG("PAM_USER Redone");
216 pwd
= getpwnam(user
);
218 retval
= PAM_USER_UNKNOWN
;
222 PAM_LOG("Done getpwnam()");
225 memset(&creds
, 0, sizeof(krb5_creds
));
226 krbret
= krb5_get_init_creds_password(pam_context
, &creds
, princ
,
227 pass
, NULL
, pamh
, 0, NULL
, &opts
);
229 PAM_VERBOSE_ERROR("Kerberos 5 error");
230 PAM_LOG("Error krb5_get_init_creds_password(): %s",
231 krb5_get_err_text(pam_context
, krbret
));
232 retval
= PAM_AUTH_ERR
;
238 /* Generate a temporary cache */
239 krbret
= krb5_cc_gen_new(pam_context
, &krb5_mcc_ops
, &ccache
);
241 PAM_VERBOSE_ERROR("Kerberos 5 error");
242 PAM_LOG("Error krb5_cc_gen_new(): %s",
243 krb5_get_err_text(pam_context
, krbret
));
244 retval
= PAM_SERVICE_ERR
;
247 krbret
= krb5_cc_initialize(pam_context
, ccache
, princ
);
249 PAM_VERBOSE_ERROR("Kerberos 5 error");
250 PAM_LOG("Error krb5_cc_initialize(): %s",
251 krb5_get_err_text(pam_context
, krbret
));
252 retval
= PAM_SERVICE_ERR
;
255 krbret
= krb5_cc_store_cred(pam_context
, ccache
, &creds
);
257 PAM_VERBOSE_ERROR("Kerberos 5 error");
258 PAM_LOG("Error krb5_cc_store_cred(): %s",
259 krb5_get_err_text(pam_context
, krbret
));
260 krb5_cc_destroy(pam_context
, ccache
);
261 retval
= PAM_SERVICE_ERR
;
265 PAM_LOG("Credentials stashed");
268 if ((srvdup
= strdup(service
)) == NULL
) {
269 retval
= PAM_BUF_ERR
;
272 krbret
= verify_krb_v5_tgt(pam_context
, ccache
, srvdup
,
273 openpam_get_option(pamh
, PAM_OPT_DEBUG
) ? 1 : 0);
276 PAM_VERBOSE_ERROR("Kerberos 5 error");
277 krb5_cc_destroy(pam_context
, ccache
);
278 retval
= PAM_AUTH_ERR
;
282 PAM_LOG("Credentials stash verified");
284 retval
= pam_get_data(pamh
, "ccache", &ccache_data
);
285 if (retval
== PAM_SUCCESS
) {
286 krb5_cc_destroy(pam_context
, ccache
);
287 PAM_VERBOSE_ERROR("Kerberos 5 error");
288 retval
= PAM_AUTH_ERR
;
292 PAM_LOG("Credentials stash not pre-existing");
294 asprintf(&ccache_name
, "%s:%s", krb5_cc_get_type(pam_context
,
295 ccache
), krb5_cc_get_name(pam_context
, ccache
));
296 if (ccache_name
== NULL
) {
297 PAM_VERBOSE_ERROR("Kerberos 5 error");
298 retval
= PAM_BUF_ERR
;
301 retval
= pam_set_data(pamh
, "ccache", ccache_name
, cleanup_cache
);
303 krb5_cc_destroy(pam_context
, ccache
);
304 PAM_VERBOSE_ERROR("Kerberos 5 error");
305 retval
= PAM_SERVICE_ERR
;
309 PAM_LOG("Credentials stash saved");
312 krb5_free_cred_contents(pam_context
, &creds
);
313 PAM_LOG("Done cleanup");
315 krb5_free_principal(pam_context
, princ
);
316 PAM_LOG("Done cleanup2");
321 krb5_free_context(pam_context
);
323 PAM_LOG("Done cleanup3");
325 if (retval
!= PAM_SUCCESS
)
326 PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
332 pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
333 int argc __unused
, const char *argv
[] __unused
)
335 #ifdef _FREEFALL_CONFIG
336 return (PAM_SUCCESS
);
339 krb5_error_code krbret
;
340 krb5_context pam_context
;
341 krb5_principal princ
;
343 krb5_ccache ccache_temp
, ccache_perm
;
344 krb5_cc_cursor cursor
;
345 struct passwd
*pwd
= NULL
;
347 const char *cache_name
, *q
;
349 const void *cache_data
;
350 char *cache_name_buf
= NULL
, *p
;
355 if (flags
& PAM_DELETE_CRED
)
356 return (PAM_SUCCESS
);
358 if (flags
& PAM_REFRESH_CRED
)
359 return (PAM_SUCCESS
);
361 if (flags
& PAM_REINITIALIZE_CRED
)
362 return (PAM_SUCCESS
);
364 if (!(flags
& PAM_ESTABLISH_CRED
))
365 return (PAM_SERVICE_ERR
);
367 /* If a persistent cache isn't desired, stop now. */
368 if (openpam_get_option(pamh
, PAM_OPT_NO_CCACHE
))
369 return (PAM_SUCCESS
);
371 PAM_LOG("Establishing credentials");
374 retval
= pam_get_item(pamh
, PAM_USER
, &user
);
375 if (retval
!= PAM_SUCCESS
)
378 PAM_LOG("Got user: %s", (const char *)user
);
380 krbret
= krb5_init_context(&pam_context
);
382 PAM_LOG("Error krb5_init_context() failed");
383 return (PAM_SERVICE_ERR
);
386 PAM_LOG("Context initialised");
388 euid
= geteuid(); /* Usually 0 */
391 PAM_LOG("Got euid, egid: %d %d", euid
, egid
);
393 /* Retrieve the temporary cache */
394 retval
= pam_get_data(pamh
, "ccache", &cache_data
);
395 if (retval
!= PAM_SUCCESS
) {
396 retval
= PAM_CRED_UNAVAIL
;
399 krbret
= krb5_cc_resolve(pam_context
, cache_data
, &ccache_temp
);
401 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)cache_data
,
402 krb5_get_err_text(pam_context
, krbret
));
403 retval
= PAM_SERVICE_ERR
;
407 /* Get the uid. This should exist. */
408 pwd
= getpwnam(user
);
410 retval
= PAM_USER_UNKNOWN
;
414 PAM_LOG("Done getpwnam()");
416 /* Avoid following a symlink as root */
417 if (setegid(pwd
->pw_gid
)) {
418 retval
= PAM_SERVICE_ERR
;
421 if (seteuid(pwd
->pw_uid
)) {
422 retval
= PAM_SERVICE_ERR
;
426 PAM_LOG("Done setegid() & seteuid()");
428 /* Get the cache name */
429 cache_name
= openpam_get_option(pamh
, PAM_OPT_CCACHE
);
430 if (cache_name
== NULL
) {
431 asprintf(&cache_name_buf
, "FILE:/tmp/krb5cc_%d", pwd
->pw_uid
);
432 cache_name
= cache_name_buf
;
435 p
= calloc(PATH_MAX
+ 16, sizeof(char));
439 PAM_LOG("Error malloc(): failure");
440 retval
= PAM_BUF_ERR
;
445 /* convert %u and %p */
450 sprintf(p
, "%d", pwd
->pw_uid
);
453 else if (*q
== 'p') {
454 sprintf(p
, "%d", getpid());
458 /* Not a special token */
469 PAM_LOG("Got cache_name: %s", cache_name
);
471 /* Initialize the new ccache */
472 krbret
= krb5_cc_get_principal(pam_context
, ccache_temp
, &princ
);
474 PAM_LOG("Error krb5_cc_get_principal(): %s",
475 krb5_get_err_text(pam_context
, krbret
));
476 retval
= PAM_SERVICE_ERR
;
479 krbret
= krb5_cc_resolve(pam_context
, cache_name
, &ccache_perm
);
481 PAM_LOG("Error krb5_cc_resolve(): %s",
482 krb5_get_err_text(pam_context
, krbret
));
483 retval
= PAM_SERVICE_ERR
;
486 krbret
= krb5_cc_initialize(pam_context
, ccache_perm
, princ
);
488 PAM_LOG("Error krb5_cc_initialize(): %s",
489 krb5_get_err_text(pam_context
, krbret
));
490 retval
= PAM_SERVICE_ERR
;
494 PAM_LOG("Cache initialised");
496 /* Prepare for iteration over creds */
497 krbret
= krb5_cc_start_seq_get(pam_context
, ccache_temp
, &cursor
);
499 PAM_LOG("Error krb5_cc_start_seq_get(): %s",
500 krb5_get_err_text(pam_context
, krbret
));
501 krb5_cc_destroy(pam_context
, ccache_perm
);
502 retval
= PAM_SERVICE_ERR
;
506 PAM_LOG("Prepared for iteration");
508 /* Copy the creds (should be two of them) */
509 while ((krbret
= krb5_cc_next_cred(pam_context
, ccache_temp
,
510 &cursor
, &creds
) == 0)) {
511 krbret
= krb5_cc_store_cred(pam_context
, ccache_perm
, &creds
);
513 PAM_LOG("Error krb5_cc_store_cred(): %s",
514 krb5_get_err_text(pam_context
, krbret
));
515 krb5_cc_destroy(pam_context
, ccache_perm
);
516 krb5_free_cred_contents(pam_context
, &creds
);
517 retval
= PAM_SERVICE_ERR
;
520 krb5_free_cred_contents(pam_context
, &creds
);
521 PAM_LOG("Iteration");
523 krb5_cc_end_seq_get(pam_context
, ccache_temp
, &cursor
);
525 PAM_LOG("Done iterating");
527 if (strstr(cache_name
, "FILE:") == cache_name
) {
528 if (chown(&cache_name
[5], pwd
->pw_uid
, pwd
->pw_gid
) == -1) {
529 PAM_LOG("Error chown(): %s", strerror(errno
));
530 krb5_cc_destroy(pam_context
, ccache_perm
);
531 retval
= PAM_SERVICE_ERR
;
534 PAM_LOG("Done chown()");
536 if (chmod(&cache_name
[5], (S_IRUSR
| S_IWUSR
)) == -1) {
537 PAM_LOG("Error chmod(): %s", strerror(errno
));
538 krb5_cc_destroy(pam_context
, ccache_perm
);
539 retval
= PAM_SERVICE_ERR
;
542 PAM_LOG("Done chmod()");
545 krb5_cc_close(pam_context
, ccache_perm
);
547 PAM_LOG("Cache closed");
549 retval
= pam_setenv(pamh
, "KRB5CCNAME", cache_name
, 1);
550 if (retval
!= PAM_SUCCESS
) {
551 PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh
, retval
));
552 krb5_cc_destroy(pam_context
, ccache_perm
);
553 retval
= PAM_SERVICE_ERR
;
557 PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name
);
560 krb5_free_principal(pam_context
, princ
);
561 PAM_LOG("Done cleanup2");
563 krb5_free_context(pam_context
);
564 PAM_LOG("Done cleanup3");
569 PAM_LOG("Done seteuid() & setegid()");
571 if (cache_name_buf
!= NULL
)
572 free(cache_name_buf
);
582 pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags __unused
,
583 int argc __unused
, const char *argv
[] __unused
)
585 krb5_error_code krbret
;
586 krb5_context pam_context
;
588 krb5_principal princ
;
591 const void *ccache_name
;
593 retval
= pam_get_item(pamh
, PAM_USER
, &user
);
594 if (retval
!= PAM_SUCCESS
)
597 PAM_LOG("Got user: %s", (const char *)user
);
599 retval
= pam_get_data(pamh
, "ccache", &ccache_name
);
600 if (retval
!= PAM_SUCCESS
)
601 return (PAM_SUCCESS
);
603 PAM_LOG("Got credentials");
605 krbret
= krb5_init_context(&pam_context
);
607 PAM_LOG("Error krb5_init_context() failed");
608 return (PAM_PERM_DENIED
);
611 PAM_LOG("Context initialised");
613 krbret
= krb5_cc_resolve(pam_context
, (const char *)ccache_name
, &ccache
);
615 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)ccache_name
,
616 krb5_get_err_text(pam_context
, krbret
));
617 krb5_free_context(pam_context
);
618 return (PAM_PERM_DENIED
);
621 PAM_LOG("Got ccache %s", (const char *)ccache_name
);
624 krbret
= krb5_cc_get_principal(pam_context
, ccache
, &princ
);
626 PAM_LOG("Error krb5_cc_get_principal(): %s",
627 krb5_get_err_text(pam_context
, krbret
));
628 retval
= PAM_PERM_DENIED
;
632 PAM_LOG("Got principal");
634 if (krb5_kuserok(pam_context
, princ
, (const char *)user
))
635 retval
= PAM_SUCCESS
;
637 retval
= PAM_PERM_DENIED
;
638 krb5_free_principal(pam_context
, princ
);
640 PAM_LOG("Done kuserok()");
643 krb5_free_context(pam_context
);
644 PAM_LOG("Done cleanup");
651 * password management
654 pam_sm_chauthtok(pam_handle_t
*pamh
, int flags
,
655 int argc __unused
, const char *argv
[] __unused
)
657 krb5_error_code krbret
;
658 krb5_context pam_context
;
660 krb5_principal princ
;
661 krb5_get_init_creds_opt opts
;
662 krb5_data result_code_string
, result_string
;
663 int result_code
, retval
;
666 char *princ_name
, *passdup
;
668 if (!(flags
& PAM_UPDATE_AUTHTOK
))
669 return (PAM_AUTHTOK_ERR
);
671 retval
= pam_get_item(pamh
, PAM_USER
, &user
);
672 if (retval
!= PAM_SUCCESS
)
675 PAM_LOG("Got user: %s", (const char *)user
);
677 krbret
= krb5_init_context(&pam_context
);
679 PAM_LOG("Error krb5_init_context() failed");
680 return (PAM_SERVICE_ERR
);
683 PAM_LOG("Context initialised");
685 krb5_get_init_creds_opt_init(&opts
);
687 PAM_LOG("Credentials options initialised");
689 /* Get principal name */
690 krbret
= krb5_parse_name(pam_context
, (const char *)user
, &princ
);
692 PAM_LOG("Error krb5_parse_name(): %s",
693 krb5_get_err_text(pam_context
, krbret
));
694 retval
= PAM_USER_UNKNOWN
;
698 /* Now convert the principal name into something human readable */
700 krbret
= krb5_unparse_name(pam_context
, princ
, &princ_name
);
702 PAM_LOG("Error krb5_unparse_name(): %s",
703 krb5_get_err_text(pam_context
, krbret
));
704 retval
= PAM_SERVICE_ERR
;
708 PAM_LOG("Got principal: %s", princ_name
);
711 retval
= pam_get_authtok(pamh
, PAM_OLDAUTHTOK
, &pass
, PASSWORD_PROMPT
);
712 if (retval
!= PAM_SUCCESS
)
715 PAM_LOG("Got password");
717 memset(&creds
, 0, sizeof(krb5_creds
));
718 krbret
= krb5_get_init_creds_password(pam_context
, &creds
, princ
,
719 pass
, NULL
, pamh
, 0, "kadmin/changepw", &opts
);
721 PAM_LOG("Error krb5_get_init_creds_password(): %s",
722 krb5_get_err_text(pam_context
, krbret
));
723 retval
= PAM_AUTH_ERR
;
727 PAM_LOG("Credentials established");
729 /* Now get the new password */
731 retval
= pam_get_authtok(pamh
,
732 PAM_AUTHTOK
, &pass
, NEW_PASSWORD_PROMPT
);
733 if (retval
!= PAM_TRY_AGAIN
)
735 pam_error(pamh
, "Mismatch; try again, EOF to quit.");
737 if (retval
!= PAM_SUCCESS
)
740 PAM_LOG("Got new password");
743 if ((passdup
= strdup(pass
)) == NULL
) {
744 retval
= PAM_BUF_ERR
;
747 krbret
= krb5_change_password(pam_context
, &creds
, passdup
,
748 &result_code
, &result_code_string
, &result_string
);
751 PAM_LOG("Error krb5_change_password(): %s",
752 krb5_get_err_text(pam_context
, krbret
));
753 retval
= PAM_AUTHTOK_ERR
;
757 PAM_LOG("Error krb5_change_password(): (result_code)");
758 retval
= PAM_AUTHTOK_ERR
;
762 PAM_LOG("Password changed");
764 if (result_string
.data
)
765 free(result_string
.data
);
766 if (result_code_string
.data
)
767 free(result_code_string
.data
);
770 krb5_free_cred_contents(pam_context
, &creds
);
771 PAM_LOG("Done cleanup");
773 krb5_free_principal(pam_context
, princ
);
774 PAM_LOG("Done cleanup2");
779 krb5_free_context(pam_context
);
781 PAM_LOG("Done cleanup3");
786 PAM_MODULE_ENTRY("pam_krb5");
789 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
790 * Modified by Sam Hartman <hartmans@mit.edu> to support PAM services
793 * Verify the Kerberos ticket-granting ticket just retrieved for the
794 * user. If the Kerberos server doesn't respond, assume the user is
795 * trying to fake us out (since we DID just get a TGT from what is
796 * supposedly our KDC). If the host/<host> service is unknown (i.e.,
797 * the local keytab doesn't have it), and we cannot find another
798 * service we do have, let her in.
800 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
804 verify_krb_v5_tgt(krb5_context context
, krb5_ccache ccache
,
805 char *pam_service
, int debug
)
807 krb5_error_code retval
;
808 krb5_principal princ
;
809 krb5_keyblock
*keyblock
;
811 krb5_auth_context auth_context
;
813 const char *services
[3], **service
;
817 /* If possible we want to try and verify the ticket we have
818 * received against a keytab. We will try multiple service
819 * principals, including at least the host principal and the PAM
820 * service principal. The host principal is preferred because access
821 * to that key is generally sufficient to compromise root, while the
822 * service key for this PAM service may be less carefully guarded.
823 * It is important to check the keytab first before the KDC so we do
824 * not get spoofed by a fake KDC.
826 services
[0] = "host";
827 services
[1] = pam_service
;
831 for (service
= &services
[0]; *service
!= NULL
; service
++) {
832 retval
= krb5_sname_to_principal(context
, NULL
, *service
,
833 KRB5_NT_SRV_HST
, &princ
);
837 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
838 "krb5_sname_to_principal()",
839 krb5_get_err_text(context
, retval
));
843 /* Extract the name directly. */
844 strncpy(phost
, compat_princ_component(context
, princ
, 1),
846 phost
[BUFSIZ
- 1] = '\0';
849 * Do we have service/<host> keys?
850 * (use default/configured keytab, kvno IGNORE_VNO to get the
851 * first match, and ignore enctype.)
853 retval
= krb5_kt_read_service_key(context
, NULL
, princ
, 0, 0,
859 if (retval
!= 0) { /* failed to find key */
860 /* Keytab or service key does not exist */
863 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
864 "krb5_kt_read_service_key()",
865 krb5_get_err_text(context
, retval
));
870 krb5_free_keyblock(context
, keyblock
);
872 /* Talk to the kdc and construct the ticket. */
874 retval
= krb5_mk_req(context
, &auth_context
, 0, *service
, phost
,
875 NULL
, ccache
, &packet
);
877 krb5_auth_con_free(context
, auth_context
);
878 auth_context
= NULL
; /* setup for rd_req */
883 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
885 krb5_get_err_text(context
, retval
));
890 /* Try to use the ticket. */
891 retval
= krb5_rd_req(context
, &auth_context
, &packet
, princ
, NULL
,
896 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
898 krb5_get_err_text(context
, retval
));
906 compat_free_data_contents(context
, &packet
);
907 krb5_free_principal(context
, princ
);
911 /* Free the memory for cache_name. Called by pam_end() */
914 cleanup_cache(pam_handle_t
*pamh __unused
, void *data
, int pam_end_status __unused
)
916 krb5_context pam_context
;
918 krb5_error_code krbret
;
920 if (krb5_init_context(&pam_context
))
923 krbret
= krb5_cc_resolve(pam_context
, data
, &ccache
);
925 krb5_cc_destroy(pam_context
, ccache
);
926 krb5_free_context(pam_context
);
930 #ifdef COMPAT_HEIMDAL
932 #error This cannot be MIT and Heimdal compatible!
936 #ifndef COMPAT_HEIMDAL
938 #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
942 #ifdef COMPAT_HEIMDAL
945 compat_princ_component(krb5_context context __unused
, krb5_principal princ
, int n
)
947 return princ
->name
.name_string
.val
[n
];
952 compat_free_data_contents(krb5_context context __unused
, krb5_data
* data
)
954 krb5_xfree(data
->data
);
960 compat_princ_component(krb5_context context
, krb5_principal princ
, int n
)
962 return krb5_princ_component(context
, princ
, n
)->data
;
966 compat_free_data_contents(krb5_context context
, krb5_data
* data
)
968 krb5_free_data_contents(context
, data
);