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_begin(krb5_context, char *, int,
80 const char **, krb5_principal *, char[static BUFSIZ]);
81 static int verify_krb_v5_tgt(krb5_context, krb5_ccache, char *, int,
82 const char *, krb5_principal, char[static BUFSIZ]);
83 static void verify_krb_v5_tgt_cleanup(krb5_context, int,
84 const char *, krb5_principal, char[static BUFSIZ]);
85 static void cleanup_cache(pam_handle_t *, void *, int);
86 static const char *compat_princ_component(krb5_context, krb5_principal, int);
87 static void compat_free_data_contents(krb5_context, krb5_data *);
89 #define USER_PROMPT "Username: "
90 #define PASSWORD_PROMPT "Password:"
91 #define NEW_PASSWORD_PROMPT "New Password:"
93 #define PAM_OPT_CCACHE "ccache"
94 #define PAM_OPT_DEBUG "debug"
95 #define PAM_OPT_FORWARDABLE "forwardable"
96 #define PAM_OPT_NO_CCACHE "no_ccache"
97 #define PAM_OPT_NO_USER_CHECK "no_user_check"
98 #define PAM_OPT_REUSE_CCACHE "reuse_ccache"
99 #define PAM_OPT_NO_USER_CHECK "no_user_check"
100 #define PAM_OPT_ALLOW_KDC_SPOOF "allow_kdc_spoof"
102 #define PAM_LOG_KRB5_ERR(ctx, rv, fmt, ...) \
104 const char *krb5msg = krb5_get_error_message(ctx, rv); \
105 PAM_LOG(fmt ": %s", ##__VA_ARGS__, krb5msg); \
106 krb5_free_error_message(ctx, krb5msg); \
110 * authentication management
113 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
114 int argc __unused, const char *argv[] __unused)
116 krb5_error_code krbret;
117 krb5_context pam_context;
119 const char *auth_service;
120 krb5_principal auth_princ;
121 char auth_phost[BUFSIZ];
123 krb5_principal princ;
125 krb5_get_init_creds_opt *opts;
128 const void *ccache_data;
129 const char *user, *pass;
130 const void *sourceuser, *service;
131 char *principal, *princ_name, *ccache_name, luser[32], *srvdup;
133 retval = pam_get_user(pamh, &user, USER_PROMPT);
134 if (retval != PAM_SUCCESS)
137 PAM_LOG("Got user: %s", user);
139 retval = pam_get_item(pamh, PAM_RUSER, &sourceuser);
140 if (retval != PAM_SUCCESS)
143 PAM_LOG("Got ruser: %s", (const char *)sourceuser);
146 pam_get_item(pamh, PAM_SERVICE, &service);
150 PAM_LOG("Got service: %s", (const char *)service);
152 if ((srvdup = strdup(service)) == NULL) {
153 retval = PAM_BUF_ERR;
157 krbret = krb5_init_context(&pam_context);
159 PAM_VERBOSE_ERROR("Kerberos 5 error");
160 retval = PAM_SERVICE_ERR;
164 PAM_LOG("Context initialised");
166 debug = openpam_get_option(pamh, PAM_OPT_DEBUG) ? 1 : 0;
167 krbret = verify_krb_v5_tgt_begin(pam_context, srvdup, debug,
168 &auth_service, &auth_princ, auth_phost);
169 if (krbret != 0) { /* failed to find key */
170 /* Keytab or service key does not exist */
172 * Give up now because we can't authenticate the KDC
173 * with a keytab, unless the administrator asked to
174 * have the traditional behaviour of being vulnerable
177 if (!openpam_get_option(pamh, PAM_OPT_ALLOW_KDC_SPOOF)) {
178 retval = PAM_SERVICE_ERR;
183 krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE);
184 if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) {
185 PAM_VERBOSE_ERROR("Kerberos 5 error");
186 retval = PAM_SERVICE_ERR;
190 PAM_LOG("Done krb5_cc_register()");
192 /* Get principal name */
193 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
194 asprintf(&principal, "%s/%s", (const char *)sourceuser, user);
196 principal = strdup(user);
198 PAM_LOG("Created principal: %s", principal);
200 krbret = krb5_parse_name(pam_context, principal, &princ);
203 PAM_LOG_KRB5_ERR(pam_context, krbret, "Error krb5_parse_name()");
204 PAM_VERBOSE_ERROR("Kerberos 5 error");
205 retval = PAM_SERVICE_ERR;
209 PAM_LOG("Done krb5_parse_name()");
211 /* Now convert the principal name into something human readable */
213 krbret = krb5_unparse_name(pam_context, princ, &princ_name);
215 PAM_LOG_KRB5_ERR(pam_context, krbret,
216 "Error krb5_unparse_name()");
217 PAM_VERBOSE_ERROR("Kerberos 5 error");
218 retval = PAM_SERVICE_ERR;
222 PAM_LOG("Got principal: %s", princ_name);
225 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
226 if (retval != PAM_SUCCESS)
229 PAM_LOG("Got password");
231 if (openpam_get_option(pamh, PAM_OPT_NO_USER_CHECK))
232 PAM_LOG("Skipping local user check");
235 /* Verify the local user exists (AFTER getting the password) */
236 if (strchr(user, '@')) {
237 /* get a local account name for this principal */
238 krbret = krb5_aname_to_localname(pam_context, princ,
239 sizeof(luser), luser);
241 PAM_VERBOSE_ERROR("Kerberos 5 error");
242 PAM_LOG_KRB5_ERR(pam_context, krbret,
243 "Error krb5_aname_to_localname()");
244 retval = PAM_USER_UNKNOWN;
248 retval = pam_set_item(pamh, PAM_USER, luser);
249 if (retval != PAM_SUCCESS)
252 PAM_LOG("PAM_USER Redone");
255 if (!openpam_get_option(pamh, PAM_OPT_NO_USER_CHECK)) {
256 pwd = getpwnam(user);
258 retval = PAM_USER_UNKNOWN;
263 PAM_LOG("Done getpwnam()");
266 /* Initialize credentials request options. */
267 krbret = krb5_get_init_creds_opt_alloc(pam_context, &opts);
269 PAM_LOG_KRB5_ERR(pam_context, krbret,
270 "Error krb5_get_init_creds_opt_alloc()");
271 PAM_VERBOSE_ERROR("Kerberos 5 error");
272 retval = PAM_SERVICE_ERR;
275 krb5_get_init_creds_opt_set_default_flags(pam_context,
276 service, NULL, opts);
278 if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
279 krb5_get_init_creds_opt_set_forwardable(opts, 1);
281 PAM_LOG("Credential options initialised");
284 memset(&creds, 0, sizeof(krb5_creds));
285 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
286 pass, NULL, pamh, 0, NULL, opts);
287 krb5_get_init_creds_opt_free(pam_context, opts);
289 PAM_VERBOSE_ERROR("Kerberos 5 error");
290 PAM_LOG_KRB5_ERR(pam_context, krbret,
291 "Error krb5_get_init_creds_password()");
292 retval = PAM_AUTH_ERR;
298 /* Generate a temporary cache */
299 krbret = krb5_cc_new_unique(pam_context, krb5_cc_type_memory, NULL, &ccache);
301 PAM_VERBOSE_ERROR("Kerberos 5 error");
302 PAM_LOG_KRB5_ERR(pam_context, krbret,
303 "Error krb5_cc_new_unique()");
304 retval = PAM_SERVICE_ERR;
307 krbret = krb5_cc_initialize(pam_context, ccache, princ);
309 PAM_VERBOSE_ERROR("Kerberos 5 error");
310 PAM_LOG_KRB5_ERR(pam_context, krbret,
311 "Error krb5_cc_initialize()");
312 retval = PAM_SERVICE_ERR;
315 krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
317 PAM_VERBOSE_ERROR("Kerberos 5 error");
318 PAM_LOG_KRB5_ERR(pam_context, krbret,
319 "Error krb5_cc_store_cred()");
320 krb5_cc_destroy(pam_context, ccache);
321 retval = PAM_SERVICE_ERR;
325 PAM_LOG("Credentials stashed");
328 krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
330 auth_service, auth_princ, auth_phost);
334 PAM_VERBOSE_ERROR("Kerberos 5 error");
335 krb5_cc_destroy(pam_context, ccache);
336 retval = PAM_AUTH_ERR;
340 PAM_LOG("Credentials stash verified");
342 retval = pam_get_data(pamh, "ccache", &ccache_data);
343 if (retval == PAM_SUCCESS) {
344 krb5_cc_destroy(pam_context, ccache);
345 PAM_VERBOSE_ERROR("Kerberos 5 error");
346 retval = PAM_AUTH_ERR;
350 PAM_LOG("Credentials stash not pre-existing");
352 asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
353 ccache), krb5_cc_get_name(pam_context, ccache));
354 if (ccache_name == NULL) {
355 PAM_VERBOSE_ERROR("Kerberos 5 error");
356 retval = PAM_BUF_ERR;
359 retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache);
361 krb5_cc_destroy(pam_context, ccache);
362 PAM_VERBOSE_ERROR("Kerberos 5 error");
363 retval = PAM_SERVICE_ERR;
367 PAM_LOG("Credentials stash saved");
370 krb5_free_cred_contents(pam_context, &creds);
371 PAM_LOG("Done cleanup");
373 krb5_free_principal(pam_context, princ);
376 PAM_LOG("Done cleanup2");
379 krb5_free_context(pam_context);
381 PAM_LOG("Done cleanup3");
384 verify_krb_v5_tgt_cleanup(pam_context, debug,
385 auth_service, auth_princ, auth_phost);
386 PAM_LOG("Done cleanup4");
391 PAM_LOG("Done cleanup5");
394 if (retval != PAM_SUCCESS)
395 PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
396 PAM_LOG("Done cleanup6");
402 pam_sm_setcred(pam_handle_t *pamh, int flags,
403 int argc __unused, const char *argv[] __unused)
405 #ifdef _FREEFALL_CONFIG
406 return (PAM_SUCCESS);
409 krb5_error_code krbret;
410 krb5_context pam_context;
411 krb5_principal princ;
413 krb5_ccache ccache_temp, ccache_perm;
414 krb5_cc_cursor cursor;
415 struct passwd *pwd = NULL;
417 const char *cache_name, *q;
419 const void *cache_data;
420 char *cache_name_buf = NULL, *p;
425 if (flags & PAM_DELETE_CRED)
426 return (PAM_SUCCESS);
428 if (flags & PAM_REFRESH_CRED)
429 return (PAM_SUCCESS);
431 if (flags & PAM_REINITIALIZE_CRED)
432 return (PAM_SUCCESS);
434 if (!(flags & PAM_ESTABLISH_CRED))
435 return (PAM_SERVICE_ERR);
437 /* If a persistent cache isn't desired, stop now. */
438 if (openpam_get_option(pamh, PAM_OPT_NO_CCACHE) ||
439 openpam_get_option(pamh, PAM_OPT_NO_USER_CHECK))
440 return (PAM_SUCCESS);
442 PAM_LOG("Establishing credentials");
445 retval = pam_get_item(pamh, PAM_USER, &user);
446 if (retval != PAM_SUCCESS)
449 PAM_LOG("Got user: %s", (const char *)user);
451 krbret = krb5_init_context(&pam_context);
453 PAM_LOG("Error krb5_init_context() failed");
454 return (PAM_SERVICE_ERR);
457 PAM_LOG("Context initialised");
459 euid = geteuid(); /* Usually 0 */
462 PAM_LOG("Got euid, egid: %d %d", euid, egid);
464 /* Retrieve the temporary cache */
465 retval = pam_get_data(pamh, "ccache", &cache_data);
466 if (retval != PAM_SUCCESS) {
467 retval = PAM_CRED_UNAVAIL;
470 krbret = krb5_cc_resolve(pam_context, cache_data, &ccache_temp);
472 PAM_LOG_KRB5_ERR(pam_context, krbret,
473 "Error krb5_cc_resolve(\"%s\")", (const char *)cache_data);
474 retval = PAM_SERVICE_ERR;
478 /* Get the uid. This should exist. */
479 pwd = getpwnam(user);
481 retval = PAM_USER_UNKNOWN;
485 PAM_LOG("Done getpwnam()");
487 /* Avoid following a symlink as root */
488 if (setegid(pwd->pw_gid)) {
489 retval = PAM_SERVICE_ERR;
492 if (seteuid(pwd->pw_uid)) {
493 retval = PAM_SERVICE_ERR;
497 PAM_LOG("Done setegid() & seteuid()");
499 /* Get the cache name */
500 cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
501 if (cache_name == NULL) {
502 asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
503 cache_name = cache_name_buf;
506 p = calloc(PATH_MAX + 16, sizeof(char));
510 PAM_LOG("Error malloc(): failure");
511 retval = PAM_BUF_ERR;
516 /* convert %u and %p */
521 sprintf(p, "%d", pwd->pw_uid);
524 else if (*q == 'p') {
525 sprintf(p, "%d", getpid());
529 /* Not a special token */
540 PAM_LOG("Got cache_name: %s", cache_name);
542 /* Initialize the new ccache */
543 krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
545 PAM_LOG_KRB5_ERR(pam_context, krbret,
546 "Error krb5_cc_get_principal()");
547 retval = PAM_SERVICE_ERR;
550 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
552 PAM_LOG_KRB5_ERR(pam_context, krbret, "Error krb5_cc_resolve()");
553 retval = PAM_SERVICE_ERR;
556 krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
558 PAM_LOG_KRB5_ERR(pam_context, krbret,
559 "Error krb5_cc_initialize()");
560 retval = PAM_SERVICE_ERR;
564 PAM_LOG("Cache initialised");
566 /* Prepare for iteration over creds */
567 krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
569 PAM_LOG_KRB5_ERR(pam_context, krbret,
570 "Error krb5_cc_start_seq_get()");
571 krb5_cc_destroy(pam_context, ccache_perm);
572 retval = PAM_SERVICE_ERR;
576 PAM_LOG("Prepared for iteration");
578 /* Copy the creds (should be two of them) */
579 while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
580 &cursor, &creds) == 0)) {
581 krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
583 PAM_LOG_KRB5_ERR(pam_context, krbret,
584 "Error krb5_cc_store_cred()");
585 krb5_cc_destroy(pam_context, ccache_perm);
586 krb5_free_cred_contents(pam_context, &creds);
587 retval = PAM_SERVICE_ERR;
590 krb5_free_cred_contents(pam_context, &creds);
591 PAM_LOG("Iteration");
593 krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
595 PAM_LOG("Done iterating");
597 if (strstr(cache_name, "FILE:") == cache_name) {
598 if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) {
599 PAM_LOG("Error chown(): %s", strerror(errno));
600 krb5_cc_destroy(pam_context, ccache_perm);
601 retval = PAM_SERVICE_ERR;
604 PAM_LOG("Done chown()");
606 if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) {
607 PAM_LOG("Error chmod(): %s", strerror(errno));
608 krb5_cc_destroy(pam_context, ccache_perm);
609 retval = PAM_SERVICE_ERR;
612 PAM_LOG("Done chmod()");
615 krb5_cc_close(pam_context, ccache_perm);
617 PAM_LOG("Cache closed");
619 retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1);
620 if (retval != PAM_SUCCESS) {
621 PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
622 krb5_cc_destroy(pam_context, ccache_perm);
623 retval = PAM_SERVICE_ERR;
627 PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
630 krb5_free_principal(pam_context, princ);
631 PAM_LOG("Done cleanup2");
633 krb5_free_context(pam_context);
634 PAM_LOG("Done cleanup3");
639 PAM_LOG("Done seteuid() & setegid()");
641 if (cache_name_buf != NULL)
642 free(cache_name_buf);
652 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
653 int argc __unused, const char *argv[] __unused)
655 krb5_error_code krbret;
656 krb5_context pam_context;
658 krb5_principal princ;
661 const void *ccache_name;
663 retval = pam_get_item(pamh, PAM_USER, &user);
664 if (retval != PAM_SUCCESS)
667 PAM_LOG("Got user: %s", (const char *)user);
669 retval = pam_get_data(pamh, "ccache", &ccache_name);
670 if (retval != PAM_SUCCESS)
671 return (PAM_SUCCESS);
673 PAM_LOG("Got credentials");
675 krbret = krb5_init_context(&pam_context);
677 PAM_LOG("Error krb5_init_context() failed");
678 return (PAM_PERM_DENIED);
681 PAM_LOG("Context initialised");
683 krbret = krb5_cc_resolve(pam_context, (const char *)ccache_name, &ccache);
685 PAM_LOG_KRB5_ERR(pam_context, krbret,
686 "Error krb5_cc_resolve(\"%s\")", (const char *)ccache_name);
687 krb5_free_context(pam_context);
688 return (PAM_PERM_DENIED);
691 PAM_LOG("Got ccache %s", (const char *)ccache_name);
694 krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
696 PAM_LOG_KRB5_ERR(pam_context, krbret,
697 "Error krb5_cc_get_principal()");
698 retval = PAM_PERM_DENIED;
702 PAM_LOG("Got principal");
704 if (krb5_kuserok(pam_context, princ, (const char *)user))
705 retval = PAM_SUCCESS;
707 retval = PAM_PERM_DENIED;
708 krb5_free_principal(pam_context, princ);
710 PAM_LOG("Done kuserok()");
713 krb5_free_context(pam_context);
714 PAM_LOG("Done cleanup");
721 * password management
724 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
725 int argc __unused, const char *argv[] __unused)
727 krb5_error_code krbret;
728 krb5_context pam_context;
730 krb5_principal princ;
731 krb5_get_init_creds_opt *opts;
732 krb5_data result_code_string, result_string;
733 int result_code, retval;
736 char *princ_name, *passdup;
738 if (!(flags & PAM_UPDATE_AUTHTOK))
739 return (PAM_AUTHTOK_ERR);
741 retval = pam_get_item(pamh, PAM_USER, &user);
742 if (retval != PAM_SUCCESS)
745 PAM_LOG("Got user: %s", (const char *)user);
747 krbret = krb5_init_context(&pam_context);
749 PAM_LOG("Error krb5_init_context() failed");
750 return (PAM_SERVICE_ERR);
753 PAM_LOG("Context initialised");
755 /* Get principal name */
756 krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
758 PAM_LOG_KRB5_ERR(pam_context, krbret,
759 "Error krb5_parse_name()");
760 retval = PAM_USER_UNKNOWN;
764 /* Now convert the principal name into something human readable */
766 krbret = krb5_unparse_name(pam_context, princ, &princ_name);
768 PAM_LOG_KRB5_ERR(pam_context, krbret,
769 "Error krb5_unparse_name()");
770 retval = PAM_SERVICE_ERR;
774 PAM_LOG("Got principal: %s", princ_name);
777 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, PASSWORD_PROMPT);
778 if (retval != PAM_SUCCESS)
781 PAM_LOG("Got password");
783 /* Initialize credentials request options. */
784 krbret = krb5_get_init_creds_opt_alloc(pam_context, &opts);
786 PAM_LOG_KRB5_ERR(pam_context, krbret,
787 "Error krb5_get_init_creds_opt_alloc()");
788 PAM_VERBOSE_ERROR("Kerberos 5 error");
789 retval = PAM_SERVICE_ERR;
793 PAM_LOG("Credentials options initialised");
795 memset(&creds, 0, sizeof(krb5_creds));
796 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
797 pass, NULL, pamh, 0, "kadmin/changepw", opts);
798 krb5_get_init_creds_opt_free(pam_context, opts);
800 PAM_LOG_KRB5_ERR(pam_context, krbret,
801 "Error krb5_get_init_creds_password()");
802 retval = PAM_AUTH_ERR;
806 PAM_LOG("Credentials established");
808 /* Now get the new password */
810 retval = pam_get_authtok(pamh,
811 PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
812 if (retval != PAM_TRY_AGAIN)
814 pam_error(pamh, "Mismatch; try again, EOF to quit.");
816 if (retval != PAM_SUCCESS)
819 PAM_LOG("Got new password");
822 if ((passdup = strdup(pass)) == NULL) {
823 retval = PAM_BUF_ERR;
826 krbret = krb5_set_password(pam_context, &creds, passdup, NULL,
827 &result_code, &result_code_string, &result_string);
830 PAM_LOG_KRB5_ERR(pam_context, krbret,
831 "Error krb5_change_password()");
832 retval = PAM_AUTHTOK_ERR;
836 PAM_LOG("Error krb5_change_password(): (result_code)");
837 retval = PAM_AUTHTOK_ERR;
841 PAM_LOG("Password changed");
843 if (result_string.data)
844 free(result_string.data);
845 if (result_code_string.data)
846 free(result_code_string.data);
849 krb5_free_cred_contents(pam_context, &creds);
850 PAM_LOG("Done cleanup");
852 krb5_free_principal(pam_context, princ);
855 PAM_LOG("Done cleanup2");
858 krb5_free_context(pam_context);
860 PAM_LOG("Done cleanup3");
865 PAM_MODULE_ENTRY("pam_krb5");
868 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
869 * Modified by Sam Hartman <hartmans@mit.edu> to support PAM services
872 * Verify the Kerberos ticket-granting ticket just retrieved for the
873 * user. If the Kerberos server doesn't respond, assume the user is
874 * trying to fake us out (since we DID just get a TGT from what is
875 * supposedly our KDC). If the host/<host> service is unknown (i.e.,
876 * the local keytab doesn't have it), and we cannot find another
877 * service we do have, let her in.
879 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
883 verify_krb_v5_tgt_begin(krb5_context context, char *pam_service, int debug,
884 const char **servicep, krb5_principal *princp __unused, char phost[static BUFSIZ])
886 krb5_error_code retval;
887 krb5_principal princ;
888 krb5_keyblock *keyblock;
889 const char *services[3], **service;
894 openlog("pam_krb5", LOG_PID, LOG_AUTHPRIV);
896 /* If possible we want to try and verify the ticket we have
897 * received against a keytab. We will try multiple service
898 * principals, including at least the host principal and the PAM
899 * service principal. The host principal is preferred because access
900 * to that key is generally sufficient to compromise root, while the
901 * service key for this PAM service may be less carefully guarded.
902 * It is important to check the keytab first before the KDC so we do
903 * not get spoofed by a fake KDC.
905 services[0] = "host";
906 services[1] = pam_service;
910 for (service = &services[0]; *service != NULL; service++) {
911 retval = krb5_sname_to_principal(context, NULL, *service,
912 KRB5_NT_SRV_HST, &princ);
915 const char *msg = krb5_get_error_message(
918 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
919 "krb5_sname_to_principal()", msg);
920 krb5_free_error_message(context, msg);
925 /* Extract the name directly. */
926 strncpy(phost, compat_princ_component(context, princ, 1),
928 phost[BUFSIZ - 1] = '\0';
931 * Do we have service/<host> keys?
932 * (use default/configured keytab, kvno IGNORE_VNO to get the
933 * first match, and ignore enctype.)
935 retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
942 krb5_free_keyblock(context, keyblock);
948 verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
949 char *pam_service __unused, int debug,
950 const char *service, krb5_principal princ, char phost[static BUFSIZ])
952 krb5_error_code retval;
953 krb5_auth_context auth_context = NULL;
957 return (0); /* uncertain, can't authenticate KDC */
961 /* Talk to the kdc and construct the ticket. */
963 retval = krb5_mk_req(context, &auth_context, 0, service, phost,
964 NULL, ccache, &packet);
966 krb5_auth_con_free(context, auth_context);
967 auth_context = NULL; /* setup for rd_req */
971 const char *msg = krb5_get_error_message(context,
974 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
975 "krb5_mk_req()", msg);
976 krb5_free_error_message(context, msg);
982 /* Try to use the ticket. */
983 retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
987 const char *msg = krb5_get_error_message(context,
990 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
991 "krb5_rd_req()", msg);
992 krb5_free_error_message(context, msg);
1001 compat_free_data_contents(context, &packet);
1006 verify_krb_v5_tgt_cleanup(krb5_context context, int debug,
1007 const char *service, krb5_principal princ, char phost[static BUFSIZ] __unused)
1011 krb5_free_principal(context, princ);
1017 /* Free the memory for cache_name. Called by pam_end() */
1020 cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
1022 krb5_context pam_context;
1024 krb5_error_code krbret;
1026 if (krb5_init_context(&pam_context))
1029 krbret = krb5_cc_resolve(pam_context, data, &ccache);
1031 krb5_cc_destroy(pam_context, ccache);
1032 krb5_free_context(pam_context);
1036 #ifdef COMPAT_HEIMDAL
1038 #error This cannot be MIT and Heimdal compatible!
1042 #ifndef COMPAT_HEIMDAL
1044 #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
1048 #ifdef COMPAT_HEIMDAL
1051 compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
1053 return princ->name.name_string.val[n];
1058 compat_free_data_contents(krb5_context context __unused, krb5_data * data)
1060 krb5_xfree(data->data);
1066 compat_princ_component(krb5_context context, krb5_principal princ, int n)
1068 return krb5_princ_component(context, princ, n)->data;
1072 compat_free_data_contents(krb5_context context, krb5_data * data)
1074 krb5_free_data_contents(context, data);