Home | History | Annotate | Line # | Download | only in pam_krb5
pam_krb5.c revision 1.31
      1  1.31  riastrad /*	$NetBSD: pam_krb5.c,v 1.31 2023/06/20 22:17:18 riastradh Exp $	*/
      2   1.2  christos 
      3   1.1  christos /*-
      4   1.1  christos  * This pam_krb5 module contains code that is:
      5   1.1  christos  *   Copyright (c) Derrick J. Brashear, 1996. All rights reserved.
      6   1.1  christos  *   Copyright (c) Frank Cusack, 1999-2001. All rights reserved.
      7   1.1  christos  *   Copyright (c) Jacques A. Vidrine, 2000-2001. All rights reserved.
      8   1.1  christos  *   Copyright (c) Nicolas Williams, 2001. All rights reserved.
      9   1.1  christos  *   Copyright (c) Perot Systems Corporation, 2001. All rights reserved.
     10   1.1  christos  *   Copyright (c) Mark R V Murray, 2001.  All rights reserved.
     11   1.3   thorpej  *   Copyright (c) Networks Associates Technology, Inc., 2002-2005.
     12   1.1  christos  *       All rights reserved.
     13   1.1  christos  *
     14   1.1  christos  * Portions of this software were developed for the FreeBSD Project by
     15   1.1  christos  * ThinkSec AS and NAI Labs, the Security Research Division of Network
     16   1.1  christos  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
     17   1.1  christos  * ("CBOSS"), as part of the DARPA CHATS research program.
     18   1.1  christos  *
     19   1.1  christos  * Redistribution and use in source and binary forms, with or without
     20   1.1  christos  * modification, are permitted provided that the following conditions
     21   1.1  christos  * are met:
     22   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     23   1.1  christos  *    notices, and the entire permission notice in its entirety,
     24   1.1  christos  *    including the disclaimer of warranties.
     25   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     26   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     27   1.1  christos  *    documentation and/or other materials provided with the distribution.
     28   1.1  christos  * 3. The name of the author may not be used to endorse or promote
     29   1.1  christos  *    products derived from this software without specific prior
     30   1.1  christos  *    written permission.
     31   1.1  christos  *
     32   1.1  christos  * ALTERNATIVELY, this product may be distributed under the terms of
     33   1.1  christos  * the GNU Public License, in which case the provisions of the GPL are
     34   1.1  christos  * required INSTEAD OF the above restrictions.  (This clause is
     35   1.1  christos  * necessary due to a potential bad interaction between the GPL and
     36   1.1  christos  * the restrictions contained in a BSD-style copyright.)
     37   1.1  christos  *
     38   1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
     39   1.1  christos  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     40   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     41   1.1  christos  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     42   1.1  christos  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     43   1.1  christos  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     44   1.1  christos  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     45   1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     46   1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     47   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
     48   1.1  christos  * OF THE POSSIBILITY OF SUCH DAMAGE.
     49   1.1  christos  *
     50   1.1  christos  */
     51   1.1  christos 
     52   1.1  christos #include <sys/cdefs.h>
     53   1.2  christos #ifdef __FreeBSD__
     54   1.3   thorpej __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_krb5/pam_krb5.c,v 1.22 2005/01/24 16:49:50 rwatson Exp $");
     55   1.2  christos #else
     56  1.31  riastrad __RCSID("$NetBSD: pam_krb5.c,v 1.31 2023/06/20 22:17:18 riastradh Exp $");
     57   1.2  christos #endif
     58   1.1  christos 
     59   1.1  christos #include <sys/types.h>
     60   1.1  christos #include <sys/stat.h>
     61   1.1  christos #include <errno.h>
     62   1.1  christos #include <limits.h>
     63   1.1  christos #include <pwd.h>
     64   1.1  christos #include <stdio.h>
     65   1.1  christos #include <stdlib.h>
     66   1.1  christos #include <string.h>
     67   1.1  christos #include <syslog.h>
     68   1.1  christos #include <unistd.h>
     69   1.1  christos 
     70   1.2  christos #include <krb5/krb5.h>
     71   1.2  christos #include <krb5/com_err.h>
     72  1.19  christos #include <krb5/parse_time.h>
     73   1.1  christos 
     74   1.1  christos #define	PAM_SM_AUTH
     75   1.1  christos #define	PAM_SM_ACCOUNT
     76   1.1  christos #define	PAM_SM_PASSWORD
     77   1.1  christos 
     78   1.1  christos #include <security/pam_appl.h>
     79   1.1  christos #include <security/pam_modules.h>
     80   1.1  christos #include <security/pam_mod_misc.h>
     81   1.1  christos #include <security/openpam.h>
     82   1.1  christos 
     83   1.1  christos #define	COMPAT_HEIMDAL
     84   1.1  christos /* #define	COMPAT_MIT */
     85   1.1  christos 
     86  1.25  christos static void	log_krb5(krb5_context, krb5_error_code, struct syslog_data *,
     87  1.25  christos     const char *, ...) __printflike(4, 5);
     88  1.31  riastrad static int	verify_krb_v5_tgt_begin(krb5_context, char *, int,
     89  1.31  riastrad     const char **, krb5_principal *, char[static BUFSIZ], struct syslog_data *);
     90  1.31  riastrad static int	verify_krb_v5_tgt(krb5_context, krb5_ccache, char *, int,
     91  1.31  riastrad     const char *, krb5_principal, char[static BUFSIZ], struct syslog_data *);
     92  1.31  riastrad static void	verify_krb_v5_tgt_cleanup(krb5_context, int,
     93  1.31  riastrad     const char *, krb5_principal, char[static BUFSIZ], struct syslog_data *);
     94   1.1  christos static void	cleanup_cache(pam_handle_t *, void *, int);
     95   1.1  christos static const	char *compat_princ_component(krb5_context, krb5_principal, int);
     96   1.1  christos static void	compat_free_data_contents(krb5_context, krb5_data *);
     97   1.1  christos 
     98   1.1  christos #define USER_PROMPT		"Username: "
     99  1.22  christos #define PASSWORD_PROMPT		"%s's password:"
    100   1.1  christos #define NEW_PASSWORD_PROMPT	"New Password:"
    101   1.1  christos 
    102   1.1  christos #define PAM_OPT_CCACHE		"ccache"
    103   1.3   thorpej #define PAM_OPT_DEBUG		"debug"
    104   1.1  christos #define PAM_OPT_FORWARDABLE	"forwardable"
    105  1.19  christos #define PAM_OPT_RENEWABLE	"renewable"
    106   1.1  christos #define PAM_OPT_NO_CCACHE	"no_ccache"
    107   1.1  christos #define PAM_OPT_REUSE_CCACHE	"reuse_ccache"
    108  1.31  riastrad #define PAM_OPT_ALLOW_KDC_SPOOF	"allow_kdc_spoof"
    109   1.1  christos 
    110   1.1  christos /*
    111   1.1  christos  * authentication management
    112   1.1  christos  */
    113   1.1  christos PAM_EXTERN int
    114   1.1  christos pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
    115   1.1  christos     int argc __unused, const char *argv[] __unused)
    116   1.1  christos {
    117   1.1  christos 	krb5_error_code krbret;
    118   1.1  christos 	krb5_context pam_context;
    119  1.31  riastrad 	int debug;
    120  1.31  riastrad 	const char *auth_service;
    121  1.31  riastrad 	krb5_principal auth_princ;
    122  1.31  riastrad 	char auth_phost[BUFSIZ];
    123  1.31  riastrad 	struct syslog_data auth_data = SYSLOG_DATA_INIT;
    124   1.1  christos 	krb5_creds creds;
    125   1.1  christos 	krb5_principal princ;
    126   1.1  christos 	krb5_ccache ccache;
    127  1.24     elric 	krb5_get_init_creds_opt *opts = NULL;
    128   1.7   thorpej 	struct passwd *pwd, pwres;
    129   1.1  christos 	int retval;
    130  1.21  christos 	const void *ccache_data;
    131   1.1  christos 	const char *user, *pass;
    132   1.1  christos 	const void *sourceuser, *service;
    133   1.1  christos 	char *principal, *princ_name, *ccache_name, luser[32], *srvdup;
    134   1.5   thorpej 	char password_prompt[80];
    135   1.7   thorpej 	char pwbuf[1024];
    136  1.19  christos 	const char *rtime;
    137   1.1  christos 
    138  1.12  christos 	princ_name = NULL;
    139   1.1  christos 	retval = pam_get_user(pamh, &user, USER_PROMPT);
    140   1.1  christos 	if (retval != PAM_SUCCESS)
    141   1.1  christos 		return (retval);
    142   1.1  christos 
    143   1.1  christos 	PAM_LOG("Got user: %s", user);
    144   1.1  christos 
    145   1.1  christos 	retval = pam_get_item(pamh, PAM_RUSER, &sourceuser);
    146   1.1  christos 	if (retval != PAM_SUCCESS)
    147   1.1  christos 		return (retval);
    148   1.1  christos 
    149   1.1  christos 	PAM_LOG("Got ruser: %s", (const char *)sourceuser);
    150   1.1  christos 
    151   1.1  christos 	service = NULL;
    152   1.1  christos 	pam_get_item(pamh, PAM_SERVICE, &service);
    153   1.1  christos 	if (service == NULL)
    154   1.1  christos 		service = "unknown";
    155   1.1  christos 
    156   1.1  christos 	PAM_LOG("Got service: %s", (const char *)service);
    157   1.1  christos 
    158  1.31  riastrad 	if ((srvdup = strdup(service)) == NULL) {
    159  1.31  riastrad 		retval = PAM_BUF_ERR;
    160  1.31  riastrad 		goto cleanup6;
    161  1.31  riastrad 	}
    162  1.31  riastrad 
    163   1.1  christos 	krbret = krb5_init_context(&pam_context);
    164   1.1  christos 	if (krbret != 0) {
    165   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    166  1.31  riastrad 		retval = PAM_SERVICE_ERR;
    167  1.31  riastrad 		goto cleanup5;
    168   1.1  christos 	}
    169   1.1  christos 
    170   1.1  christos 	PAM_LOG("Context initialised");
    171   1.1  christos 
    172  1.31  riastrad 	debug = openpam_get_option(pamh, PAM_OPT_DEBUG) ? 1 : 0;
    173  1.31  riastrad 	krbret = verify_krb_v5_tgt_begin(pam_context, srvdup, debug,
    174  1.31  riastrad 	    &auth_service, &auth_princ, auth_phost, &auth_data);
    175  1.31  riastrad 	if (krbret != 0) {	/* failed to find key */
    176  1.31  riastrad 		/* Keytab or service key does not exist */
    177  1.31  riastrad 		if (debug)
    178  1.31  riastrad 			log_krb5(pam_context, krbret, &auth_data,
    179  1.31  riastrad 			    "pam_krb5: verify_krb_v5_tgt: "
    180  1.31  riastrad 			    "krb5_kt_read_service_key");
    181  1.31  riastrad 		/*
    182  1.31  riastrad 		 * Give up now because we can't authenticate the KDC
    183  1.31  riastrad 		 * with a keytab, unless the administrator asked to
    184  1.31  riastrad 		 * have the traditional behaviour of being vulnerable
    185  1.31  riastrad 		 * to spoofed KDCs.
    186  1.31  riastrad 		 */
    187  1.31  riastrad 		if (!openpam_get_option(pamh, PAM_OPT_ALLOW_KDC_SPOOF)) {
    188  1.31  riastrad 			retval = PAM_SERVICE_ERR;
    189  1.31  riastrad 			goto cleanup4;
    190  1.31  riastrad 		}
    191  1.31  riastrad 	}
    192  1.31  riastrad 
    193  1.24     elric 	krbret = krb5_get_init_creds_opt_alloc(pam_context, &opts);
    194  1.24     elric 	if (krbret != 0) {
    195  1.24     elric 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    196  1.31  riastrad 		retval = PAM_SERVICE_ERR;
    197  1.31  riastrad 		goto cleanup4;
    198  1.24     elric 	}
    199   1.1  christos 
    200   1.1  christos 	if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
    201  1.24     elric 		krb5_get_init_creds_opt_set_forwardable(opts, 1);
    202   1.1  christos 
    203  1.19  christos 	if ((rtime = openpam_get_option(pamh, PAM_OPT_RENEWABLE)) != NULL) {
    204  1.19  christos 		krb5_deltat renew;
    205  1.19  christos 		char rbuf[80], *rp;
    206  1.19  christos 
    207  1.19  christos 		if (*rtime) {
    208  1.19  christos 			(void)strlcpy(rbuf, rtime, sizeof(rbuf));
    209  1.19  christos 			rtime = rbuf;
    210  1.19  christos 			for (rp = rbuf; *rp; rp++)
    211  1.19  christos 				if (*rp == '_')
    212  1.20  christos 					*rp = ' ';
    213  1.19  christos 		}
    214  1.19  christos 		else
    215  1.19  christos 			rtime = "1 month";
    216  1.19  christos 		renew = parse_time(rtime, "s");
    217  1.24     elric 		krb5_get_init_creds_opt_set_renew_life(opts, renew);
    218  1.19  christos 	}
    219  1.19  christos 
    220  1.19  christos 
    221  1.19  christos 
    222   1.1  christos 	PAM_LOG("Credentials initialised");
    223   1.1  christos 
    224   1.1  christos 	krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE);
    225   1.1  christos 	if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) {
    226   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    227   1.1  christos 		retval = PAM_SERVICE_ERR;
    228   1.1  christos 		goto cleanup3;
    229   1.1  christos 	}
    230   1.1  christos 
    231   1.1  christos 	PAM_LOG("Done krb5_cc_register()");
    232   1.1  christos 
    233   1.1  christos 	/* Get principal name */
    234   1.1  christos 	if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
    235   1.1  christos 		asprintf(&principal, "%s/%s", (const char *)sourceuser, user);
    236   1.1  christos 	else
    237   1.1  christos 		principal = strdup(user);
    238   1.1  christos 
    239   1.1  christos 	PAM_LOG("Created principal: %s", principal);
    240   1.1  christos 
    241   1.1  christos 	krbret = krb5_parse_name(pam_context, principal, &princ);
    242   1.1  christos 	free(principal);
    243   1.1  christos 	if (krbret != 0) {
    244  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_parse_name");
    245   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    246   1.1  christos 		retval = PAM_SERVICE_ERR;
    247   1.1  christos 		goto cleanup3;
    248   1.1  christos 	}
    249   1.1  christos 
    250   1.1  christos 	PAM_LOG("Done krb5_parse_name()");
    251   1.1  christos 
    252   1.1  christos 	/* Now convert the principal name into something human readable */
    253   1.1  christos 	krbret = krb5_unparse_name(pam_context, princ, &princ_name);
    254   1.1  christos 	if (krbret != 0) {
    255  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_unparse_name");
    256   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    257   1.1  christos 		retval = PAM_SERVICE_ERR;
    258   1.1  christos 		goto cleanup2;
    259   1.1  christos 	}
    260   1.1  christos 
    261   1.1  christos 	PAM_LOG("Got principal: %s", princ_name);
    262   1.1  christos 
    263   1.1  christos 	/* Get password */
    264   1.5   thorpej 	(void) snprintf(password_prompt, sizeof(password_prompt),
    265   1.5   thorpej 	    PASSWORD_PROMPT, princ_name);
    266   1.5   thorpej 	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, password_prompt);
    267   1.1  christos 	if (retval != PAM_SUCCESS)
    268   1.1  christos 		goto cleanup2;
    269   1.1  christos 
    270   1.1  christos 	PAM_LOG("Got password");
    271   1.1  christos 
    272   1.1  christos 	/* Verify the local user exists (AFTER getting the password) */
    273   1.1  christos 	if (strchr(user, '@')) {
    274   1.1  christos 		/* get a local account name for this principal */
    275   1.1  christos 		krbret = krb5_aname_to_localname(pam_context, princ,
    276   1.1  christos 		    sizeof(luser), luser);
    277   1.1  christos 		if (krbret != 0) {
    278   1.1  christos 			PAM_VERBOSE_ERROR("Kerberos 5 error");
    279  1.25  christos 			log_krb5(pam_context, krbret, NULL,
    280  1.25  christos 			    "krb5_aname_to_localname");
    281   1.1  christos 			retval = PAM_USER_UNKNOWN;
    282   1.1  christos 			goto cleanup2;
    283   1.1  christos 		}
    284   1.1  christos 
    285   1.1  christos 		retval = pam_set_item(pamh, PAM_USER, luser);
    286   1.1  christos 		if (retval != PAM_SUCCESS)
    287   1.1  christos 			goto cleanup2;
    288   1.1  christos 
    289   1.1  christos 		PAM_LOG("PAM_USER Redone");
    290   1.1  christos 	}
    291   1.1  christos 
    292   1.8  christos 	if (getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
    293   1.8  christos 	    pwd == NULL) {
    294   1.1  christos 		retval = PAM_USER_UNKNOWN;
    295   1.1  christos 		goto cleanup2;
    296   1.1  christos 	}
    297   1.1  christos 
    298   1.7   thorpej 	PAM_LOG("Done getpwnam_r()");
    299   1.1  christos 
    300   1.1  christos 	/* Get a TGT */
    301   1.1  christos 	memset(&creds, 0, sizeof(krb5_creds));
    302   1.1  christos 	krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
    303  1.24     elric 	    pass, NULL, pamh, 0, NULL, opts);
    304   1.1  christos 	if (krbret != 0) {
    305   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    306  1.25  christos 		log_krb5(pam_context, krbret, NULL,
    307  1.25  christos 		    "krb5_get_init_creds_password");
    308   1.1  christos 		retval = PAM_AUTH_ERR;
    309   1.1  christos 		goto cleanup2;
    310   1.1  christos 	}
    311   1.1  christos 
    312   1.1  christos 	PAM_LOG("Got TGT");
    313   1.1  christos 
    314   1.1  christos 	/* Generate a temporary cache */
    315  1.24     elric 	krbret = krb5_cc_new_unique(pam_context, "MEMORY", NULL, &ccache);
    316   1.1  christos 	if (krbret != 0) {
    317   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    318  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_cc_gen_new");
    319   1.1  christos 		retval = PAM_SERVICE_ERR;
    320   1.1  christos 		goto cleanup;
    321   1.1  christos 	}
    322   1.1  christos 	krbret = krb5_cc_initialize(pam_context, ccache, princ);
    323   1.1  christos 	if (krbret != 0) {
    324   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    325  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_cc_initialize");
    326   1.1  christos 		retval = PAM_SERVICE_ERR;
    327   1.1  christos 		goto cleanup;
    328   1.1  christos 	}
    329   1.1  christos 	krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
    330   1.1  christos 	if (krbret != 0) {
    331   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    332  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_cc_store_cred");
    333   1.1  christos 		krb5_cc_destroy(pam_context, ccache);
    334   1.1  christos 		retval = PAM_SERVICE_ERR;
    335   1.1  christos 		goto cleanup;
    336   1.1  christos 	}
    337   1.1  christos 
    338   1.1  christos 	PAM_LOG("Credentials stashed");
    339   1.1  christos 
    340   1.1  christos 	/* Verify them */
    341   1.1  christos 	krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
    342  1.31  riastrad 	    debug,
    343  1.31  riastrad 	    auth_service, auth_princ, auth_phost, &auth_data);
    344   1.1  christos 	free(srvdup);
    345   1.1  christos 	if (krbret == -1) {
    346   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    347   1.1  christos 		krb5_cc_destroy(pam_context, ccache);
    348   1.1  christos 		retval = PAM_AUTH_ERR;
    349   1.1  christos 		goto cleanup;
    350   1.1  christos 	}
    351   1.1  christos 
    352   1.1  christos 	PAM_LOG("Credentials stash verified");
    353   1.1  christos 
    354   1.1  christos 	retval = pam_get_data(pamh, "ccache", &ccache_data);
    355   1.1  christos 	if (retval == PAM_SUCCESS) {
    356   1.1  christos 		krb5_cc_destroy(pam_context, ccache);
    357   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    358   1.1  christos 		retval = PAM_AUTH_ERR;
    359   1.1  christos 		goto cleanup;
    360   1.1  christos 	}
    361   1.1  christos 
    362   1.1  christos 	PAM_LOG("Credentials stash not pre-existing");
    363   1.1  christos 
    364   1.1  christos 	asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
    365   1.1  christos 		ccache), krb5_cc_get_name(pam_context, ccache));
    366   1.1  christos 	if (ccache_name == NULL) {
    367   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    368   1.1  christos 		retval = PAM_BUF_ERR;
    369   1.1  christos 		goto cleanup;
    370   1.1  christos 	}
    371   1.1  christos 	retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache);
    372   1.1  christos 	if (retval != 0) {
    373   1.1  christos 		krb5_cc_destroy(pam_context, ccache);
    374   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 error");
    375   1.1  christos 		retval = PAM_SERVICE_ERR;
    376   1.1  christos 		goto cleanup;
    377   1.1  christos 	}
    378   1.1  christos 
    379   1.1  christos 	PAM_LOG("Credentials stash saved");
    380   1.1  christos 
    381   1.1  christos cleanup:
    382   1.1  christos 	krb5_free_cred_contents(pam_context, &creds);
    383   1.1  christos 	PAM_LOG("Done cleanup");
    384   1.1  christos cleanup2:
    385   1.1  christos 	krb5_free_principal(pam_context, princ);
    386   1.1  christos 	PAM_LOG("Done cleanup2");
    387   1.1  christos cleanup3:
    388   1.1  christos 	if (princ_name)
    389   1.1  christos 		free(princ_name);
    390   1.1  christos 
    391  1.24     elric 	if (opts)
    392  1.24     elric 		krb5_get_init_creds_opt_free(pam_context, opts);
    393  1.31  riastrad cleanup4:
    394  1.31  riastrad 	verify_krb_v5_tgt_cleanup(pam_context, debug,
    395  1.31  riastrad 	    auth_service, auth_princ, auth_phost, &auth_data);
    396  1.24     elric 
    397   1.1  christos 	krb5_free_context(pam_context);
    398  1.31  riastrad cleanup5:
    399  1.31  riastrad 	free(srvdup);
    400   1.1  christos 
    401  1.31  riastrad 	PAM_LOG("Done cleanup5");
    402  1.31  riastrad cleanup6:
    403   1.1  christos 	if (retval != PAM_SUCCESS)
    404   1.1  christos 		PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
    405   1.1  christos 
    406   1.1  christos 	return (retval);
    407   1.1  christos }
    408   1.1  christos 
    409   1.1  christos PAM_EXTERN int
    410   1.1  christos pam_sm_setcred(pam_handle_t *pamh, int flags,
    411   1.1  christos     int argc __unused, const char *argv[] __unused)
    412   1.1  christos {
    413   1.1  christos 
    414   1.1  christos 	krb5_error_code krbret;
    415   1.1  christos 	krb5_context pam_context;
    416   1.1  christos 	krb5_principal princ;
    417   1.1  christos 	krb5_creds creds;
    418   1.1  christos 	krb5_ccache ccache_temp, ccache_perm;
    419   1.1  christos 	krb5_cc_cursor cursor;
    420   1.7   thorpej 	struct passwd *pwd = NULL, pwres;
    421   1.1  christos 	int retval;
    422   1.1  christos 	const char *cache_name, *q;
    423   1.1  christos 	const void *user;
    424  1.21  christos 	const void *cache_data;
    425  1.14  christos 	char *cache_name_buf = NULL, *p, *cache_name_buf2 = NULL;
    426   1.7   thorpej 	char pwbuf[1024];
    427   1.1  christos 
    428   1.1  christos 	uid_t euid;
    429   1.1  christos 	gid_t egid;
    430   1.1  christos 
    431   1.1  christos 	if (flags & PAM_DELETE_CRED)
    432  1.10    tsarna 		return (PAM_SUCCESS); /* XXX */
    433   1.1  christos 
    434  1.10    tsarna 	if (!(flags & (PAM_REFRESH_CRED|PAM_REINITIALIZE_CRED|PAM_ESTABLISH_CRED)))
    435   1.1  christos 		return (PAM_SERVICE_ERR);
    436   1.1  christos 
    437   1.3   thorpej 	/* If a persistent cache isn't desired, stop now. */
    438   1.3   thorpej 	if (openpam_get_option(pamh, PAM_OPT_NO_CCACHE))
    439   1.3   thorpej 		return (PAM_SUCCESS);
    440   1.3   thorpej 
    441   1.1  christos 	PAM_LOG("Establishing credentials");
    442   1.1  christos 
    443   1.1  christos 	/* Get username */
    444   1.1  christos 	retval = pam_get_item(pamh, PAM_USER, &user);
    445   1.1  christos 	if (retval != PAM_SUCCESS)
    446   1.1  christos 		return (retval);
    447   1.1  christos 
    448   1.1  christos 	PAM_LOG("Got user: %s", (const char *)user);
    449   1.1  christos 
    450   1.1  christos 	krbret = krb5_init_context(&pam_context);
    451   1.1  christos 	if (krbret != 0) {
    452   1.1  christos 		PAM_LOG("Error krb5_init_context() failed");
    453   1.1  christos 		return (PAM_SERVICE_ERR);
    454   1.1  christos 	}
    455   1.1  christos 
    456   1.1  christos 	PAM_LOG("Context initialised");
    457   1.1  christos 
    458   1.1  christos 	euid = geteuid();	/* Usually 0 */
    459   1.1  christos 	egid = getegid();
    460   1.1  christos 
    461   1.1  christos 	PAM_LOG("Got euid, egid: %d %d", euid, egid);
    462   1.1  christos 
    463   1.1  christos 	/* Retrieve the temporary cache */
    464   1.1  christos 	retval = pam_get_data(pamh, "ccache", &cache_data);
    465   1.1  christos 	if (retval != PAM_SUCCESS) {
    466   1.1  christos 		retval = PAM_CRED_UNAVAIL;
    467   1.1  christos 		goto cleanup3;
    468   1.1  christos 	}
    469   1.1  christos 	krbret = krb5_cc_resolve(pam_context, cache_data, &ccache_temp);
    470   1.1  christos 	if (krbret != 0) {
    471  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_cc_resolve(\"%s\")",
    472  1.25  christos 		    (const char *)cache_data);
    473   1.1  christos 		retval = PAM_SERVICE_ERR;
    474   1.1  christos 		goto cleanup3;
    475   1.1  christos 	}
    476   1.1  christos 
    477   1.1  christos 	/* Get the uid. This should exist. */
    478   1.9     lukem 	if (getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
    479   1.9     lukem 	    pwd == NULL) {
    480   1.1  christos 		retval = PAM_USER_UNKNOWN;
    481   1.1  christos 		goto cleanup3;
    482   1.1  christos 	}
    483   1.1  christos 
    484   1.7   thorpej 	PAM_LOG("Done getpwnam_r()");
    485   1.1  christos 
    486   1.1  christos 	/* Avoid following a symlink as root */
    487   1.1  christos 	if (setegid(pwd->pw_gid)) {
    488   1.1  christos 		retval = PAM_SERVICE_ERR;
    489   1.1  christos 		goto cleanup3;
    490   1.1  christos 	}
    491   1.1  christos 	if (seteuid(pwd->pw_uid)) {
    492   1.1  christos 		retval = PAM_SERVICE_ERR;
    493   1.1  christos 		goto cleanup3;
    494   1.1  christos 	}
    495   1.1  christos 
    496   1.1  christos 	PAM_LOG("Done setegid() & seteuid()");
    497   1.1  christos 
    498  1.10    tsarna 	if (flags & (PAM_REFRESH_CRED|PAM_REINITIALIZE_CRED)) {
    499  1.10    tsarna                 cache_name = getenv("KRB5CCNAME");
    500  1.10    tsarna                 if (!cache_name)
    501  1.10    tsarna                 	goto cleanup3;
    502  1.10    tsarna 	} else {
    503  1.27  christos 		size_t len = PATH_MAX + 16;
    504  1.10    tsarna 		/* Get the cache name */
    505  1.10    tsarna 		cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
    506  1.10    tsarna 		if (cache_name == NULL) {
    507  1.10    tsarna 			asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
    508  1.10    tsarna 			cache_name = cache_name_buf;
    509  1.10    tsarna 		}
    510   1.1  christos 
    511  1.27  christos 		cache_name_buf2 = p = calloc(len, sizeof(char));
    512  1.10    tsarna 		q = cache_name;
    513  1.10    tsarna 
    514  1.10    tsarna 		if (p == NULL) {
    515  1.10    tsarna 			PAM_LOG("Error malloc(): failure");
    516  1.10    tsarna 			retval = PAM_BUF_ERR;
    517  1.10    tsarna 			goto cleanup3;
    518  1.10    tsarna 		}
    519  1.10    tsarna 		cache_name = p;
    520   1.1  christos 
    521  1.10    tsarna 		/* convert %u and %p */
    522  1.10    tsarna 		while (*q) {
    523  1.27  christos 			int l;
    524  1.10    tsarna 			if (*q == '%') {
    525  1.10    tsarna 				q++;
    526  1.10    tsarna 				if (*q == 'u') {
    527  1.27  christos 					l = snprintf(p, len, "%d", pwd->pw_uid);
    528  1.10    tsarna 				}
    529  1.10    tsarna 				else if (*q == 'p') {
    530  1.27  christos 					l = snprintf(p, len, "%d", getpid());
    531  1.10    tsarna 				}
    532  1.10    tsarna 				else {
    533  1.10    tsarna 					/* Not a special token */
    534  1.27  christos 					if (!len)
    535  1.27  christos 						goto truncated;
    536  1.27  christos 					*p = '%';
    537  1.27  christos 					l = 1;
    538  1.10    tsarna 					q--;
    539  1.10    tsarna 				}
    540  1.27  christos 				if ((size_t)l > len) {
    541  1.27  christos truncated:				PAM_LOG("string truncation failure");
    542  1.27  christos 					retval = PAM_BUF_ERR;
    543  1.27  christos 					goto cleanup3;
    544  1.27  christos 				}
    545  1.10    tsarna 				q++;
    546   1.1  christos 			}
    547   1.1  christos 			else {
    548  1.27  christos 				if (!len)
    549  1.27  christos 					goto truncated;
    550  1.27  christos 				*p = *q++;
    551  1.27  christos 				l = 1;
    552   1.1  christos 			}
    553  1.27  christos 			p += l;
    554  1.27  christos 			len -= (size_t)l;
    555   1.1  christos 		}
    556  1.27  christos 		if (!len)
    557  1.27  christos 			goto truncated;
    558  1.27  christos 		*p = '\0';
    559   1.1  christos 	}
    560   1.1  christos 
    561   1.1  christos 	PAM_LOG("Got cache_name: %s", cache_name);
    562   1.1  christos 
    563   1.1  christos 	/* Initialize the new ccache */
    564   1.1  christos 	krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
    565   1.1  christos 	if (krbret != 0) {
    566  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_cc_get_principal");
    567   1.1  christos 		retval = PAM_SERVICE_ERR;
    568   1.1  christos 		goto cleanup3;
    569   1.1  christos 	}
    570   1.1  christos 	krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
    571   1.1  christos 	if (krbret != 0) {
    572  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_cc_resolve");
    573   1.1  christos 		retval = PAM_SERVICE_ERR;
    574   1.1  christos 		goto cleanup2;
    575   1.1  christos 	}
    576  1.10    tsarna 
    577   1.1  christos 	krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
    578   1.1  christos 	if (krbret != 0) {
    579  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_cc_initialize");
    580   1.1  christos 		retval = PAM_SERVICE_ERR;
    581   1.1  christos 		goto cleanup2;
    582   1.1  christos 	}
    583   1.1  christos 
    584   1.1  christos 	PAM_LOG("Cache initialised");
    585   1.1  christos 
    586   1.1  christos 	/* Prepare for iteration over creds */
    587   1.1  christos 	krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
    588   1.1  christos 	if (krbret != 0) {
    589  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_cc_start_seq_get");
    590   1.1  christos 		krb5_cc_destroy(pam_context, ccache_perm);
    591   1.1  christos 		retval = PAM_SERVICE_ERR;
    592   1.1  christos 		goto cleanup2;
    593   1.1  christos 	}
    594   1.1  christos 
    595   1.1  christos 	PAM_LOG("Prepared for iteration");
    596   1.1  christos 
    597   1.1  christos 	/* Copy the creds (should be two of them) */
    598   1.1  christos 	while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
    599  1.23   mbalmer 				&cursor, &creds)) == 0) {
    600  1.10    tsarna 
    601   1.1  christos 		krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
    602   1.1  christos 		if (krbret != 0) {
    603  1.25  christos 			log_krb5(pam_context, krbret, NULL,
    604  1.25  christos 			    "krb5_cc_store_cred");
    605   1.1  christos 			krb5_cc_destroy(pam_context, ccache_perm);
    606   1.1  christos 			krb5_free_cred_contents(pam_context, &creds);
    607   1.1  christos 			retval = PAM_SERVICE_ERR;
    608   1.1  christos 			goto cleanup2;
    609   1.1  christos 		}
    610  1.10    tsarna 
    611   1.1  christos 		krb5_free_cred_contents(pam_context, &creds);
    612   1.1  christos 		PAM_LOG("Iteration");
    613   1.1  christos 	}
    614  1.10    tsarna 
    615   1.1  christos 	krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
    616   1.1  christos 
    617   1.1  christos 	PAM_LOG("Done iterating");
    618   1.1  christos 
    619  1.10    tsarna 	if (flags & PAM_ESTABLISH_CRED) {
    620  1.10    tsarna 		if (strstr(cache_name, "FILE:") == cache_name) {
    621  1.10    tsarna 			if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) {
    622  1.10    tsarna 				PAM_LOG("Error chown(): %s", strerror(errno));
    623  1.10    tsarna 				krb5_cc_destroy(pam_context, ccache_perm);
    624  1.10    tsarna 				retval = PAM_SERVICE_ERR;
    625  1.10    tsarna 				goto cleanup2;
    626  1.10    tsarna 			}
    627  1.10    tsarna 			PAM_LOG("Done chown()");
    628   1.1  christos 
    629  1.10    tsarna 			if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) {
    630  1.10    tsarna 				PAM_LOG("Error chmod(): %s", strerror(errno));
    631  1.10    tsarna 				krb5_cc_destroy(pam_context, ccache_perm);
    632  1.10    tsarna 				retval = PAM_SERVICE_ERR;
    633  1.10    tsarna 				goto cleanup2;
    634  1.10    tsarna 			}
    635  1.10    tsarna 			PAM_LOG("Done chmod()");
    636   1.1  christos 		}
    637   1.1  christos 	}
    638   1.1  christos 
    639   1.1  christos 	krb5_cc_close(pam_context, ccache_perm);
    640   1.1  christos 
    641   1.1  christos 	PAM_LOG("Cache closed");
    642   1.1  christos 
    643   1.1  christos 	retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1);
    644   1.1  christos 	if (retval != PAM_SUCCESS) {
    645   1.1  christos 		PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
    646   1.1  christos 		retval = PAM_SERVICE_ERR;
    647   1.1  christos 		goto cleanup2;
    648   1.1  christos 	}
    649   1.1  christos 
    650   1.1  christos 	PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
    651   1.1  christos 
    652   1.1  christos cleanup2:
    653   1.1  christos 	krb5_free_principal(pam_context, princ);
    654   1.1  christos 	PAM_LOG("Done cleanup2");
    655   1.1  christos cleanup3:
    656   1.1  christos 	krb5_free_context(pam_context);
    657   1.1  christos 	PAM_LOG("Done cleanup3");
    658   1.1  christos 
    659   1.1  christos 	seteuid(euid);
    660   1.1  christos 	setegid(egid);
    661   1.1  christos 
    662   1.1  christos 	PAM_LOG("Done seteuid() & setegid()");
    663   1.1  christos 
    664   1.1  christos 	if (cache_name_buf != NULL)
    665   1.1  christos 		free(cache_name_buf);
    666  1.14  christos 	if (cache_name_buf2 != NULL)
    667  1.14  christos 		free(cache_name_buf2);
    668   1.1  christos 
    669   1.1  christos 	return (retval);
    670   1.1  christos }
    671   1.1  christos 
    672   1.1  christos /*
    673   1.1  christos  * account management
    674   1.1  christos  */
    675   1.1  christos PAM_EXTERN int
    676   1.1  christos pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
    677   1.1  christos     int argc __unused, const char *argv[] __unused)
    678   1.1  christos {
    679   1.1  christos 	krb5_error_code krbret;
    680   1.1  christos 	krb5_context pam_context;
    681   1.1  christos 	krb5_ccache ccache;
    682   1.1  christos 	krb5_principal princ;
    683   1.1  christos 	int retval;
    684   1.1  christos 	const void *user;
    685  1.21  christos 	const void *ccache_name;
    686   1.1  christos 
    687   1.1  christos 	retval = pam_get_item(pamh, PAM_USER, &user);
    688   1.1  christos 	if (retval != PAM_SUCCESS)
    689   1.1  christos 		return (retval);
    690   1.1  christos 
    691   1.1  christos 	PAM_LOG("Got user: %s", (const char *)user);
    692   1.1  christos 
    693   1.1  christos 	retval = pam_get_data(pamh, "ccache", &ccache_name);
    694   1.1  christos 	if (retval != PAM_SUCCESS)
    695   1.1  christos 		return (PAM_SUCCESS);
    696   1.1  christos 
    697   1.1  christos 	PAM_LOG("Got credentials");
    698   1.1  christos 
    699   1.1  christos 	krbret = krb5_init_context(&pam_context);
    700   1.1  christos 	if (krbret != 0) {
    701   1.1  christos 		PAM_LOG("Error krb5_init_context() failed");
    702   1.1  christos 		return (PAM_PERM_DENIED);
    703   1.1  christos 	}
    704   1.1  christos 
    705   1.1  christos 	PAM_LOG("Context initialised");
    706   1.1  christos 
    707   1.1  christos 	krbret = krb5_cc_resolve(pam_context, (const char *)ccache_name, &ccache);
    708   1.1  christos 	if (krbret != 0) {
    709  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_cc_resolve(\"%s\")",
    710  1.25  christos 		    (const char *)ccache_name);
    711   1.1  christos 		krb5_free_context(pam_context);
    712   1.1  christos 		return (PAM_PERM_DENIED);
    713   1.1  christos 	}
    714   1.1  christos 
    715   1.1  christos 	PAM_LOG("Got ccache %s", (const char *)ccache_name);
    716   1.1  christos 
    717   1.1  christos 
    718   1.1  christos 	krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
    719   1.1  christos 	if (krbret != 0) {
    720  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_cc_get_principal");
    721  1.30    rillig 		retval = PAM_PERM_DENIED;
    722   1.1  christos 		goto cleanup;
    723   1.1  christos 	}
    724   1.1  christos 
    725   1.1  christos 	PAM_LOG("Got principal");
    726   1.1  christos 
    727   1.1  christos 	if (krb5_kuserok(pam_context, princ, (const char *)user))
    728   1.1  christos 		retval = PAM_SUCCESS;
    729   1.1  christos 	else
    730   1.1  christos 		retval = PAM_PERM_DENIED;
    731   1.1  christos 	krb5_free_principal(pam_context, princ);
    732   1.1  christos 
    733   1.1  christos 	PAM_LOG("Done kuserok()");
    734   1.1  christos 
    735   1.1  christos cleanup:
    736   1.1  christos 	krb5_free_context(pam_context);
    737   1.1  christos 	PAM_LOG("Done cleanup");
    738   1.1  christos 
    739   1.1  christos 	return (retval);
    740   1.1  christos 
    741   1.1  christos }
    742   1.1  christos 
    743   1.1  christos /*
    744   1.1  christos  * password management
    745   1.1  christos  */
    746   1.1  christos PAM_EXTERN int
    747   1.1  christos pam_sm_chauthtok(pam_handle_t *pamh, int flags,
    748   1.1  christos     int argc __unused, const char *argv[] __unused)
    749   1.1  christos {
    750   1.1  christos 	krb5_error_code krbret;
    751   1.1  christos 	krb5_context pam_context;
    752   1.1  christos 	krb5_creds creds;
    753   1.1  christos 	krb5_principal princ;
    754  1.24     elric 	krb5_get_init_creds_opt *opts;
    755   1.1  christos 	krb5_data result_code_string, result_string;
    756   1.1  christos 	int result_code, retval;
    757   1.1  christos 	const char *pass;
    758   1.1  christos 	const void *user;
    759   1.1  christos 	char *princ_name, *passdup;
    760   1.5   thorpej 	char password_prompt[80];
    761   1.1  christos 
    762  1.13  christos 	princ_name = NULL;
    763   1.4   thorpej 	if (flags & PAM_PRELIM_CHECK) {
    764   1.4   thorpej 		/* Nothing to do here. */
    765   1.4   thorpej 		return (PAM_SUCCESS);
    766   1.4   thorpej 	}
    767   1.4   thorpej 
    768   1.4   thorpej 	if (!(flags & PAM_UPDATE_AUTHTOK)) {
    769   1.4   thorpej 		PAM_LOG("Illegal flags argument");
    770   1.4   thorpej 		return (PAM_ABORT);
    771   1.4   thorpej 	}
    772   1.1  christos 
    773   1.1  christos 	retval = pam_get_item(pamh, PAM_USER, &user);
    774   1.1  christos 	if (retval != PAM_SUCCESS)
    775   1.1  christos 		return (retval);
    776   1.1  christos 
    777   1.1  christos 	PAM_LOG("Got user: %s", (const char *)user);
    778   1.1  christos 
    779   1.1  christos 	krbret = krb5_init_context(&pam_context);
    780   1.1  christos 	if (krbret != 0) {
    781   1.1  christos 		PAM_LOG("Error krb5_init_context() failed");
    782   1.1  christos 		return (PAM_SERVICE_ERR);
    783   1.1  christos 	}
    784   1.1  christos 
    785   1.1  christos 	PAM_LOG("Context initialised");
    786   1.1  christos 
    787  1.24     elric 	krbret = krb5_get_init_creds_opt_alloc(pam_context, &opts);
    788  1.24     elric 	if (krbret != 0) {
    789  1.24     elric 		PAM_LOG("Error krb5_init_context() failed");
    790  1.24     elric 		return (PAM_SERVICE_ERR);
    791  1.24     elric 	}
    792   1.1  christos 
    793  1.24     elric 	krb5_get_init_creds_opt_set_tkt_life(opts, 300);
    794  1.24     elric 	krb5_get_init_creds_opt_set_forwardable(opts, FALSE);
    795  1.24     elric 	krb5_get_init_creds_opt_set_proxiable(opts, FALSE);
    796   1.6   thorpej 
    797   1.1  christos 	PAM_LOG("Credentials options initialised");
    798   1.1  christos 
    799   1.1  christos 	/* Get principal name */
    800   1.1  christos 	krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
    801   1.1  christos 	if (krbret != 0) {
    802  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_parse_name");
    803   1.1  christos 		retval = PAM_USER_UNKNOWN;
    804   1.1  christos 		goto cleanup3;
    805   1.1  christos 	}
    806   1.1  christos 
    807   1.1  christos 	/* Now convert the principal name into something human readable */
    808   1.1  christos 	krbret = krb5_unparse_name(pam_context, princ, &princ_name);
    809   1.1  christos 	if (krbret != 0) {
    810  1.25  christos 		log_krb5(pam_context, krbret, NULL, "krb5_unparse_name");
    811   1.1  christos 		retval = PAM_SERVICE_ERR;
    812   1.1  christos 		goto cleanup2;
    813   1.1  christos 	}
    814   1.1  christos 
    815   1.1  christos 	PAM_LOG("Got principal: %s", princ_name);
    816   1.1  christos 
    817   1.1  christos 	/* Get password */
    818   1.5   thorpej 	(void) snprintf(password_prompt, sizeof(password_prompt),
    819   1.5   thorpej 	    PASSWORD_PROMPT, princ_name);
    820   1.5   thorpej 	retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, password_prompt);
    821   1.1  christos 	if (retval != PAM_SUCCESS)
    822   1.1  christos 		goto cleanup2;
    823   1.1  christos 
    824   1.1  christos 	PAM_LOG("Got password");
    825   1.1  christos 
    826   1.1  christos 	memset(&creds, 0, sizeof(krb5_creds));
    827   1.1  christos 	krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
    828  1.24     elric 	    pass, NULL, pamh, 0, "kadmin/changepw", opts);
    829   1.1  christos 	if (krbret != 0) {
    830  1.25  christos 		log_krb5(pam_context, krbret, NULL,
    831  1.25  christos 		    "krb5_get_init_creds_password");
    832   1.1  christos 		retval = PAM_AUTH_ERR;
    833   1.1  christos 		goto cleanup2;
    834   1.1  christos 	}
    835   1.1  christos 
    836   1.1  christos 	PAM_LOG("Credentials established");
    837   1.1  christos 
    838   1.1  christos 	/* Now get the new password */
    839   1.1  christos 	for (;;) {
    840   1.1  christos 		retval = pam_get_authtok(pamh,
    841   1.1  christos 		    PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
    842   1.1  christos 		if (retval != PAM_TRY_AGAIN)
    843   1.1  christos 			break;
    844   1.1  christos 		pam_error(pamh, "Mismatch; try again, EOF to quit.");
    845   1.1  christos 	}
    846   1.1  christos 	if (retval != PAM_SUCCESS)
    847   1.1  christos 		goto cleanup;
    848   1.1  christos 
    849   1.1  christos 	PAM_LOG("Got new password");
    850   1.1  christos 
    851   1.1  christos 	/* Change it */
    852   1.1  christos 	if ((passdup = strdup(pass)) == NULL) {
    853   1.1  christos 		retval = PAM_BUF_ERR;
    854   1.1  christos 		goto cleanup;
    855   1.1  christos 	}
    856   1.6   thorpej 
    857   1.6   thorpej 	krb5_data_zero(&result_code_string);
    858   1.6   thorpej 	krb5_data_zero(&result_string);
    859   1.6   thorpej 
    860  1.24     elric 	krbret = krb5_set_password(pam_context, &creds, passdup, princ,
    861   1.1  christos 	    &result_code, &result_code_string, &result_string);
    862   1.1  christos 	free(passdup);
    863   1.1  christos 	if (krbret != 0) {
    864  1.25  christos 		log_krb5(pam_context, krbret, NULL, "Unable to set password");
    865   1.1  christos 		retval = PAM_AUTHTOK_ERR;
    866   1.1  christos 		goto cleanup;
    867   1.1  christos 	}
    868   1.1  christos 	if (result_code) {
    869   1.6   thorpej 		pam_info(pamh, "%s%s%.*s",
    870   1.6   thorpej 		    krb5_passwd_result_to_string(pam_context, result_code),
    871   1.6   thorpej 		    result_string.length > 0 ? ": " : "",
    872   1.6   thorpej 		    (int)result_string.length,
    873   1.6   thorpej 		    result_string.length > 0 ? (char *)result_string.data : "");
    874   1.1  christos 		retval = PAM_AUTHTOK_ERR;
    875   1.6   thorpej 	} else {
    876   1.6   thorpej 		PAM_LOG("Password changed");
    877   1.1  christos 	}
    878   1.1  christos 
    879   1.6   thorpej 	krb5_data_free(&result_string);
    880   1.6   thorpej 	krb5_data_free(&result_code_string);
    881   1.1  christos 
    882   1.1  christos cleanup:
    883   1.1  christos 	krb5_free_cred_contents(pam_context, &creds);
    884   1.1  christos 	PAM_LOG("Done cleanup");
    885   1.1  christos cleanup2:
    886   1.1  christos 	krb5_free_principal(pam_context, princ);
    887   1.1  christos 	PAM_LOG("Done cleanup2");
    888   1.1  christos cleanup3:
    889   1.1  christos 	if (princ_name)
    890   1.1  christos 		free(princ_name);
    891   1.1  christos 
    892  1.24     elric 	if (opts)
    893  1.24     elric 		krb5_get_init_creds_opt_free(pam_context, opts);
    894  1.24     elric 
    895   1.1  christos 	krb5_free_context(pam_context);
    896   1.1  christos 
    897   1.1  christos 	PAM_LOG("Done cleanup3");
    898   1.1  christos 
    899   1.1  christos 	return (retval);
    900   1.1  christos }
    901   1.1  christos 
    902   1.1  christos PAM_MODULE_ENTRY("pam_krb5");
    903   1.1  christos 
    904  1.24     elric static void
    905  1.25  christos log_krb5(krb5_context ctx, krb5_error_code err,
    906  1.25  christos     struct syslog_data *data, const char *fmt, ...)
    907  1.24     elric {
    908  1.25  christos 	char b1[1024], b2[1024];
    909  1.25  christos 	const char *errtxt;
    910  1.25  christos 	va_list ap;
    911  1.24     elric 
    912  1.25  christos 	va_start(ap, fmt);
    913  1.25  christos 	vsnprintf(b1, sizeof(b1), fmt, ap);
    914  1.25  christos 	va_end(ap);
    915  1.25  christos 	if (ctx)
    916  1.25  christos 		errtxt = krb5_get_error_message(ctx, err);
    917  1.25  christos 	else
    918  1.25  christos 		errtxt = NULL;
    919  1.24     elric 	if (errtxt != NULL) {
    920  1.26  christos 		snprintf(b2, sizeof(b2), "%s", errtxt);
    921  1.24     elric 		krb5_free_error_message(ctx, errtxt);
    922  1.24     elric 	} else {
    923  1.26  christos 		snprintf(b2, sizeof(b2), "unknown %d", (int)err);
    924  1.24     elric 	}
    925  1.25  christos 	if (data)
    926  1.26  christos 		syslog_r(LOG_DEBUG, data, "%s (%s)", b1, b2);
    927  1.25  christos 	else
    928  1.26  christos 		PAM_LOG("%s (%s)", b1, b2);
    929  1.24     elric }
    930  1.24     elric 
    931   1.1  christos /*
    932   1.1  christos  * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
    933   1.1  christos  * Modified by Sam Hartman <hartmans (at) mit.edu> to support PAM services
    934   1.1  christos  * for Debian.
    935   1.1  christos  *
    936   1.1  christos  * Verify the Kerberos ticket-granting ticket just retrieved for the
    937   1.1  christos  * user.  If the Kerberos server doesn't respond, assume the user is
    938   1.1  christos  * trying to fake us out (since we DID just get a TGT from what is
    939   1.1  christos  * supposedly our KDC).  If the host/<host> service is unknown (i.e.,
    940   1.1  christos  * the local keytab doesn't have it), and we cannot find another
    941   1.1  christos  * service we do have, let her in.
    942   1.1  christos  *
    943  1.31  riastrad  * - verify_krb_v5_tgt_begin returns a krb5 error code.
    944  1.31  riastrad  * - verify_krb_v5_tgt returns 0 on success, -1 on failure.
    945   1.1  christos  */
    946   1.1  christos /* ARGSUSED */
    947   1.1  christos static int
    948  1.31  riastrad verify_krb_v5_tgt_begin(krb5_context context, char *pam_service, int debug,
    949  1.31  riastrad     const char **servicep, krb5_principal *princp, char phost[static BUFSIZ],
    950  1.31  riastrad     struct syslog_data *datap)
    951   1.1  christos {
    952   1.1  christos 	krb5_error_code retval;
    953   1.1  christos 	krb5_principal princ;
    954   1.1  christos 	krb5_keyblock *keyblock;
    955   1.1  christos 	const char *services[3], **service;
    956   1.1  christos 
    957  1.31  riastrad 	*servicep = NULL;
    958   1.1  christos 
    959  1.17  christos 	if (debug)
    960  1.31  riastrad 		openlog_r("pam_krb5", LOG_PID, LOG_AUTHPRIV, datap);
    961  1.17  christos 
    962   1.1  christos 	/* If possible we want to try and verify the ticket we have
    963   1.1  christos 	 * received against a keytab.  We will try multiple service
    964   1.1  christos 	 * principals, including at least the host principal and the PAM
    965   1.1  christos 	 * service principal.  The host principal is preferred because access
    966   1.1  christos 	 * to that key is generally sufficient to compromise root, while the
    967   1.1  christos 	 * service key for this PAM service may be less carefully guarded.
    968   1.1  christos 	 * It is important to check the keytab first before the KDC so we do
    969   1.1  christos 	 * not get spoofed by a fake KDC.
    970   1.1  christos 	 */
    971   1.1  christos 	services[0] = "host";
    972   1.1  christos 	services[1] = pam_service;
    973   1.1  christos 	services[2] = NULL;
    974   1.1  christos 	keyblock = 0;
    975   1.1  christos 	retval = -1;
    976   1.1  christos 	for (service = &services[0]; *service != NULL; service++) {
    977   1.1  christos 		retval = krb5_sname_to_principal(context, NULL, *service,
    978   1.1  christos 		    KRB5_NT_SRV_HST, &princ);
    979  1.25  christos 		if (retval != 0 && debug)
    980  1.31  riastrad 			log_krb5(context, retval, datap,
    981  1.25  christos 			    "pam_krb5: verify_krb_v5_tgt: "
    982  1.25  christos 			    "krb5_sname_to_principal");
    983  1.24     elric 		if (retval != 0)
    984  1.31  riastrad 			return (retval);
    985   1.1  christos 
    986   1.1  christos 		/* Extract the name directly. */
    987  1.29       fox 		strlcpy(phost, compat_princ_component(context, princ, 1),
    988  1.31  riastrad 		    BUFSIZ);
    989   1.1  christos 
    990   1.1  christos 		/*
    991   1.1  christos 		 * Do we have service/<host> keys?
    992   1.1  christos 		 * (use default/configured keytab, kvno IGNORE_VNO to get the
    993   1.1  christos 		 * first match, and ignore enctype.)
    994   1.1  christos 		 */
    995   1.1  christos 		retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
    996   1.1  christos 		    &keyblock);
    997   1.1  christos 		if (retval != 0)
    998   1.1  christos 			continue;
    999   1.1  christos 		break;
   1000   1.1  christos 	}
   1001   1.1  christos 	if (keyblock)
   1002   1.1  christos 		krb5_free_keyblock(context, keyblock);
   1003   1.1  christos 
   1004  1.31  riastrad 	return (retval);
   1005  1.31  riastrad }
   1006  1.31  riastrad 
   1007  1.31  riastrad static int
   1008  1.31  riastrad verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
   1009  1.31  riastrad     char *pam_service, int debug,
   1010  1.31  riastrad     const char *service, krb5_principal princ, char phost[static BUFSIZ],
   1011  1.31  riastrad     struct syslog_data *datap)
   1012  1.31  riastrad {
   1013  1.31  riastrad 	krb5_error_code retval;
   1014  1.31  riastrad 	krb5_auth_context auth_context = NULL;
   1015  1.31  riastrad 	krb5_data packet;
   1016  1.31  riastrad 
   1017  1.31  riastrad 	if (service == NULL)
   1018  1.31  riastrad 		return (0);	/* uncertain, can't authenticate KDC */
   1019  1.31  riastrad 
   1020  1.31  riastrad 	packet.data = 0;
   1021  1.31  riastrad 
   1022   1.1  christos 	/* Talk to the kdc and construct the ticket. */
   1023   1.1  christos 	auth_context = NULL;
   1024  1.31  riastrad 	retval = krb5_mk_req(context, &auth_context, 0, service, phost,
   1025   1.1  christos 		NULL, ccache, &packet);
   1026   1.1  christos 	if (auth_context) {
   1027   1.1  christos 		krb5_auth_con_free(context, auth_context);
   1028   1.1  christos 		auth_context = NULL;	/* setup for rd_req */
   1029   1.1  christos 	}
   1030   1.1  christos 	if (retval) {
   1031  1.25  christos 		if (debug)
   1032  1.31  riastrad 			log_krb5(context, retval, datap,
   1033  1.25  christos 			    "pam_krb5: verify_krb_v5_tgt: "
   1034  1.25  christos 			    "krb5_mk_req");
   1035   1.1  christos 		retval = -1;
   1036   1.1  christos 		goto cleanup;
   1037   1.1  christos 	}
   1038   1.1  christos 
   1039   1.1  christos 	/* Try to use the ticket. */
   1040   1.1  christos 	retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
   1041   1.1  christos 	    NULL, NULL);
   1042   1.1  christos 	if (retval) {
   1043  1.25  christos 		if (debug)
   1044  1.31  riastrad 			log_krb5(context, retval, datap,
   1045  1.25  christos 			    "pam_krb5: verify_krb_v5_tgt: "
   1046  1.25  christos 			    "krb5_rd_req");
   1047   1.1  christos 		retval = -1;
   1048   1.1  christos 	}
   1049   1.1  christos 	else
   1050   1.1  christos 		retval = 1;
   1051   1.1  christos 
   1052   1.1  christos cleanup:
   1053   1.1  christos 	if (packet.data)
   1054   1.1  christos 		compat_free_data_contents(context, &packet);
   1055  1.15  christos 	if (auth_context) {
   1056  1.15  christos 		krb5_auth_con_free(context, auth_context);
   1057  1.15  christos 		auth_context = NULL;	/* setup for rd_req */
   1058  1.15  christos 	}
   1059  1.31  riastrad 	return (retval);
   1060  1.31  riastrad }
   1061  1.31  riastrad 
   1062  1.31  riastrad static void
   1063  1.31  riastrad verify_krb_v5_tgt_cleanup(krb5_context context, int debug,
   1064  1.31  riastrad     const char *service, krb5_principal princ, char phost[static BUFSIZ],
   1065  1.31  riastrad     struct syslog_data *datap)
   1066  1.31  riastrad {
   1067  1.31  riastrad 
   1068  1.31  riastrad 	if (service)
   1069  1.31  riastrad 		krb5_free_principal(context, princ);
   1070  1.31  riastrad 	if (debug)
   1071  1.31  riastrad 		closelog_r(datap);
   1072   1.1  christos }
   1073   1.1  christos 
   1074   1.1  christos /* Free the memory for cache_name. Called by pam_end() */
   1075   1.1  christos /* ARGSUSED */
   1076   1.1  christos static void
   1077   1.1  christos cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
   1078   1.1  christos {
   1079   1.1  christos 	krb5_context pam_context;
   1080   1.1  christos 	krb5_ccache ccache;
   1081   1.1  christos 	krb5_error_code krbret;
   1082   1.1  christos 
   1083   1.1  christos 	if (krb5_init_context(&pam_context))
   1084   1.1  christos 		return;
   1085   1.1  christos 
   1086   1.1  christos 	krbret = krb5_cc_resolve(pam_context, data, &ccache);
   1087   1.1  christos 	if (krbret == 0)
   1088   1.1  christos 		krb5_cc_destroy(pam_context, ccache);
   1089   1.1  christos 	krb5_free_context(pam_context);
   1090   1.1  christos 	free(data);
   1091   1.1  christos }
   1092   1.1  christos 
   1093   1.1  christos #ifdef COMPAT_HEIMDAL
   1094   1.1  christos #ifdef COMPAT_MIT
   1095   1.1  christos #error This cannot be MIT and Heimdal compatible!
   1096   1.1  christos #endif
   1097   1.1  christos #endif
   1098   1.1  christos 
   1099   1.1  christos #ifndef COMPAT_HEIMDAL
   1100   1.1  christos #ifndef COMPAT_MIT
   1101   1.1  christos #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
   1102   1.1  christos #endif
   1103   1.1  christos #endif
   1104   1.1  christos 
   1105   1.1  christos #ifdef COMPAT_HEIMDAL
   1106   1.1  christos /* ARGSUSED */
   1107   1.1  christos static const char *
   1108   1.1  christos compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
   1109   1.1  christos {
   1110   1.1  christos 	return princ->name.name_string.val[n];
   1111   1.1  christos }
   1112   1.1  christos 
   1113   1.1  christos /* ARGSUSED */
   1114   1.1  christos static void
   1115   1.1  christos compat_free_data_contents(krb5_context context __unused, krb5_data * data)
   1116   1.1  christos {
   1117   1.1  christos 	krb5_xfree(data->data);
   1118   1.1  christos }
   1119   1.1  christos #endif
   1120   1.1  christos 
   1121   1.1  christos #ifdef COMPAT_MIT
   1122   1.1  christos static const char *
   1123   1.1  christos compat_princ_component(krb5_context context, krb5_principal princ, int n)
   1124   1.1  christos {
   1125   1.1  christos 	return krb5_princ_component(context, princ, n)->data;
   1126   1.1  christos }
   1127   1.1  christos 
   1128   1.1  christos static void
   1129   1.1  christos compat_free_data_contents(krb5_context context, krb5_data * data)
   1130   1.1  christos {
   1131   1.1  christos 	krb5_free_data_contents(context, data);
   1132   1.1  christos }
   1133   1.1  christos #endif
   1134