Home | History | Annotate | Line # | Download | only in login
k5login.c revision 1.26
      1  1.26   xtraeme /*	$NetBSD: k5login.c,v 1.26 2005/01/20 15:41:14 xtraeme Exp $	*/
      2   1.2       jtc 
      3   1.1    brezak /*-
      4   1.1    brezak  * Copyright (c) 1990 The Regents of the University of California.
      5   1.1    brezak  * All rights reserved.
      6   1.1    brezak  *
      7   1.1    brezak  * Redistribution and use in source and binary forms, with or without
      8   1.1    brezak  * modification, are permitted provided that the following conditions
      9   1.1    brezak  * are met:
     10   1.1    brezak  * 1. Redistributions of source code must retain the above copyright
     11   1.1    brezak  *    notice, this list of conditions and the following disclaimer.
     12   1.1    brezak  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    brezak  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    brezak  *    documentation and/or other materials provided with the distribution.
     15  1.24       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1    brezak  *    may be used to endorse or promote products derived from this software
     17   1.1    brezak  *    without specific prior written permission.
     18   1.1    brezak  *
     19   1.1    brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1    brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1    brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1    brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1    brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1    brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1    brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1    brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1    brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1    brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1    brezak  * SUCH DAMAGE.
     30   1.1    brezak  */
     31   1.1    brezak 
     32  1.15     aidan /*
     33  1.15     aidan  * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
     34  1.15     aidan  * All rights reserved.
     35  1.15     aidan  *
     36  1.15     aidan  * Redistribution and use in source and binary forms are permitted
     37  1.15     aidan  * provided that the above copyright notice and this paragraph are
     38  1.15     aidan  * duplicated in all such forms and that any documentation,
     39  1.15     aidan  * advertising materials, and other materials related to such
     40  1.15     aidan  * distribution and use acknowledge that the software was developed
     41  1.15     aidan  * by the University of California, Berkeley.  The name of the
     42  1.15     aidan  * University may not be used to endorse or promote products derived
     43  1.15     aidan  * from this software without specific prior written permission.
     44  1.15     aidan  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
     45  1.15     aidan  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
     46  1.15     aidan  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     47  1.15     aidan  */
     48  1.15     aidan 
     49   1.4     lukem #include <sys/cdefs.h>
     50   1.1    brezak #ifndef lint
     51   1.2       jtc #if 0
     52   1.2       jtc static char sccsid[] = "@(#)klogin.c	5.11 (Berkeley) 7/12/92";
     53   1.2       jtc #endif
     54  1.26   xtraeme __RCSID("$NetBSD: k5login.c,v 1.26 2005/01/20 15:41:14 xtraeme Exp $");
     55   1.1    brezak #endif /* not lint */
     56   1.1    brezak 
     57   1.1    brezak #ifdef KERBEROS5
     58   1.1    brezak #include <sys/param.h>
     59   1.1    brezak #include <sys/syslog.h>
     60   1.8  christos #include <krb5/krb5.h>
     61  1.25       lha #ifdef KERBEROS
     62  1.20     assar #include <kerberosIV/krb.h>
     63  1.25       lha #endif
     64   1.1    brezak #include <pwd.h>
     65   1.1    brezak #include <netdb.h>
     66   1.1    brezak #include <stdio.h>
     67   1.7     aidan #include <stdlib.h>
     68   1.1    brezak #include <string.h>
     69   1.1    brezak #include <unistd.h>
     70  1.15     aidan #include <errno.h>
     71   1.1    brezak 
     72   1.1    brezak #define KRB5_DEFAULT_OPTIONS 0
     73   1.1    brezak #define KRB5_DEFAULT_LIFE 60*60*10 /* 10 hours */
     74   1.1    brezak 
     75   1.5   mycroft krb5_context kcontext;
     76   1.1    brezak 
     77   1.9     aidan int notickets;
     78  1.13   thorpej int krb5_configured;
     79  1.12     aidan char *krb5tkfile_env;
     80   1.1    brezak extern char *tty;
     81  1.12     aidan extern int login_krb5_forwardable_tgt;
     82  1.12     aidan extern int has_ccache;
     83   1.1    brezak 
     84   1.1    brezak static char tkt_location[MAXPATHLEN];
     85  1.20     assar #ifdef KERBEROS
     86  1.20     assar char krb4tkfile[MAXPATHLEN];
     87  1.20     assar #endif
     88   1.7     aidan static krb5_creds forw_creds;
     89   1.7     aidan int have_forward;
     90   1.7     aidan static krb5_principal me, server;
     91   1.7     aidan 
     92  1.19        pk int k5_read_creds(char *);
     93  1.19        pk int k5_write_creds(void);
     94  1.19        pk int k5_verify_creds(krb5_context, krb5_ccache);
     95  1.19        pk int k5login(struct passwd *, char *, char *, char *);
     96  1.19        pk void k5destroy(void);
     97   1.8  christos 
     98  1.20     assar #ifdef KERBEROS
     99  1.20     assar static krb5_error_code
    100  1.20     assar krb5_to4 (struct passwd *pw, krb5_context context, krb5_ccache id);
    101  1.20     assar #endif
    102  1.20     assar 
    103  1.11     aidan #ifndef krb5_realm_length
    104  1.11     aidan #define krb5_realm_length(r)	((r).length)
    105  1.11     aidan #endif
    106  1.11     aidan #ifndef krb5_realm_data
    107  1.11     aidan #define krb5_realm_data(r)	((r).data)
    108  1.11     aidan #endif
    109  1.11     aidan 
    110   1.7     aidan /*
    111  1.15     aidan  * Verify the Kerberos ticket-granting ticket just retrieved for the
    112  1.15     aidan  * user.  If the Kerberos server doesn't respond, assume the user is
    113  1.15     aidan  * trying to fake us out (since we DID just get a TGT from what is
    114  1.15     aidan  * supposedly our KDC).  If the host/<host> service is unknown (i.e.,
    115  1.15     aidan  * the local keytab doesn't have it), let her in.
    116  1.15     aidan  *
    117  1.15     aidan  * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
    118  1.15     aidan  */
    119  1.15     aidan int
    120  1.26   xtraeme k5_verify_creds(krb5_context c, krb5_ccache ccache)
    121  1.15     aidan {
    122  1.15     aidan 	char phost[MAXHOSTNAMELEN];
    123  1.15     aidan 	int retval, have_keys;
    124  1.15     aidan 	krb5_principal princ;
    125  1.15     aidan 	krb5_keyblock *kb = 0;
    126  1.15     aidan 	krb5_error_code kerror;
    127  1.15     aidan 	krb5_data packet;
    128  1.15     aidan 	krb5_auth_context auth_context = NULL;
    129  1.15     aidan 	krb5_ticket *ticket = NULL;
    130  1.15     aidan 
    131  1.15     aidan 	kerror = krb5_sname_to_principal(c, 0, 0, KRB5_NT_SRV_HST, &princ);
    132  1.15     aidan 	if (kerror) {
    133  1.19        pk 		krb5_warn(kcontext, kerror, "constructing local service name");
    134  1.15     aidan 		return (-1);
    135  1.15     aidan 	}
    136  1.15     aidan 
    137  1.15     aidan 	/* Do we have host/<host> keys? */
    138  1.15     aidan 	/* (use default keytab, kvno IGNORE_VNO to get the first match,
    139  1.15     aidan 	 * and default enctype.) */
    140  1.15     aidan 	kerror = krb5_kt_read_service_key(c, NULL, princ, 0, 0, &kb);
    141  1.15     aidan 	if (kb)
    142  1.15     aidan 		krb5_free_keyblock(c, kb);
    143  1.15     aidan 	/* any failure means we don't have keys at all. */
    144  1.15     aidan 	have_keys = kerror ? 0 : 1;
    145  1.15     aidan 
    146  1.15     aidan 	/* XXX there should be a krb5 function like mk_req, but taking a full
    147  1.15     aidan 	 * principal, instead of a service/hostname.  (Did I miss one?) */
    148  1.15     aidan 	gethostname(phost, sizeof(phost));
    149  1.15     aidan 	phost[sizeof(phost) - 1] = '\0';
    150  1.15     aidan 
    151  1.15     aidan 	/* talk to the kdc and construct the ticket */
    152  1.15     aidan 	kerror = krb5_mk_req(c, &auth_context, 0, "host", phost,
    153  1.15     aidan 	                     0, ccache, &packet);
    154  1.15     aidan 	/* wipe the auth context for rd_req */
    155  1.15     aidan 	if (auth_context) {
    156  1.15     aidan 		krb5_auth_con_free(c, auth_context);
    157  1.15     aidan 		auth_context = NULL;
    158  1.15     aidan 	}
    159  1.15     aidan 	if (kerror == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
    160  1.15     aidan 		/* we have a service key, so something should be
    161  1.15     aidan 		 * in the database, therefore this error packet could
    162  1.15     aidan 		 * have come from an attacker. */
    163  1.15     aidan 		if (have_keys) {
    164  1.15     aidan 			retval = -1;
    165  1.15     aidan 			goto EGRESS;
    166  1.15     aidan 		}
    167  1.15     aidan 		/* but if it is unknown and we've got no key, we don't
    168  1.15     aidan 		 * have any security anyhow, so it is ok. */
    169  1.15     aidan 		else {
    170  1.15     aidan 			retval = 0;
    171  1.15     aidan 			goto EGRESS;
    172  1.15     aidan 		}
    173  1.15     aidan 	}
    174  1.15     aidan 	else if (kerror) {
    175  1.19        pk 		krb5_warn(kcontext, kerror,
    176  1.19        pk 			  "Unable to verify Kerberos V5 TGT: %s", phost);
    177  1.19        pk 		syslog(LOG_NOTICE, "Kerberos V5 TGT bad: %s",
    178  1.16     aidan 		       krb5_get_err_text(kcontext, kerror));
    179  1.15     aidan 		retval = -1;
    180  1.15     aidan 		goto EGRESS;
    181  1.15     aidan 	}
    182  1.15     aidan 	/* got ticket, try to use it */
    183  1.15     aidan 	kerror = krb5_rd_req(c, &auth_context, &packet,
    184  1.15     aidan 	                     princ, NULL, NULL, &ticket);
    185  1.15     aidan 	if (kerror) {
    186  1.15     aidan 		if (!have_keys) {
    187  1.15     aidan 			/* The krb5 errors aren't specified well, but I think
    188  1.15     aidan 			 * these values cover the cases we expect. */
    189  1.15     aidan 			switch (kerror) {
    190  1.15     aidan 			case ENOENT:	/* no keytab */
    191  1.15     aidan 			case KRB5_KT_NOTFOUND:
    192  1.15     aidan 				retval = 0;
    193  1.15     aidan 				break;
    194  1.15     aidan 			default:
    195  1.15     aidan 				/* unexpected error: fail */
    196  1.15     aidan 				retval = -1;
    197  1.15     aidan 				break;
    198  1.15     aidan 			}
    199  1.15     aidan 		}
    200  1.15     aidan 		else {
    201  1.15     aidan 			/* we have keys, so if we got any error, we could be
    202  1.15     aidan 			 * under attack. */
    203  1.15     aidan 			retval = -1;
    204  1.15     aidan 		}
    205  1.19        pk 		krb5_warn(kcontext, kerror, "Unable to verify host ticket");
    206  1.18     lukem 		syslog(LOG_NOTICE, "can't verify v5 ticket: %s; %s\n",
    207  1.16     aidan 		       krb5_get_err_text(kcontext, kerror),
    208  1.15     aidan 		       retval
    209  1.15     aidan 		         ? "keytab found, assuming failure"
    210  1.15     aidan 		         : "no keytab found, assuming success");
    211  1.15     aidan 		goto EGRESS;
    212  1.15     aidan 	}
    213  1.15     aidan 	/*
    214  1.15     aidan 	 * The host/<host> ticket has been received _and_ verified.
    215  1.15     aidan 	 */
    216  1.15     aidan 	retval = 1;
    217  1.15     aidan 
    218  1.15     aidan 	/* do cleanup and return */
    219  1.15     aidan EGRESS:
    220  1.15     aidan 	if (auth_context)
    221  1.15     aidan 		krb5_auth_con_free(c, auth_context);
    222  1.15     aidan 	krb5_free_principal(c, princ);
    223  1.15     aidan 	/* possibly ticket and packet need freeing here as well */
    224  1.15     aidan 	return (retval);
    225  1.15     aidan }
    226  1.15     aidan 
    227  1.15     aidan /*
    228   1.7     aidan  * Attempt to read forwarded kerberos creds
    229   1.7     aidan  *
    230   1.7     aidan  * return 0 on success (forwarded creds in memory)
    231   1.7     aidan  *        1 if no forwarded creds.
    232   1.7     aidan  */
    233   1.7     aidan int
    234  1.26   xtraeme k5_read_creds(char *username)
    235   1.7     aidan {
    236  1.19        pk 	krb5_error_code kerror;
    237   1.7     aidan 	krb5_creds mcreds;
    238   1.7     aidan 	krb5_ccache ccache;
    239   1.7     aidan 
    240   1.7     aidan 	have_forward = 0;
    241   1.7     aidan 	memset((char*) &mcreds, 0, sizeof(forw_creds));
    242   1.7     aidan 	memset((char*) &forw_creds, 0, sizeof(forw_creds));
    243   1.7     aidan 
    244  1.19        pk 	kerror = krb5_cc_default(kcontext, &ccache);
    245  1.19        pk 	if (kerror) {
    246  1.19        pk 		krb5_warn(kcontext, kerror, "while getting default ccache");
    247   1.7     aidan 		return(1);
    248   1.7     aidan 	}
    249   1.7     aidan 
    250  1.19        pk 	kerror = krb5_parse_name(kcontext, username, &me);
    251  1.19        pk 	if (kerror) {
    252  1.19        pk 		krb5_warn(kcontext, kerror, "when parsing name %s", username);
    253   1.7     aidan 		return(1);
    254   1.7     aidan 	}
    255   1.7     aidan 
    256   1.7     aidan 	mcreds.client = me;
    257  1.19        pk 	kerror = krb5_build_principal_ext(kcontext, &mcreds.server,
    258  1.19        pk 			krb5_realm_length(*krb5_princ_realm(kcontext, me)),
    259  1.19        pk 			krb5_realm_data(*krb5_princ_realm(kcontext, me)),
    260  1.19        pk 			KRB5_TGS_NAME_SIZE,
    261  1.19        pk 			KRB5_TGS_NAME,
    262  1.19        pk 			krb5_realm_length(*krb5_princ_realm(kcontext, me)),
    263  1.19        pk 			krb5_realm_data(*krb5_princ_realm(kcontext, me)),
    264  1.19        pk 			0);
    265  1.19        pk 	if (kerror) {
    266  1.19        pk 		krb5_warn(kcontext, kerror, "while building server name");
    267   1.7     aidan 		goto nuke_ccache;
    268   1.7     aidan 	}
    269   1.7     aidan 
    270  1.19        pk 	kerror = krb5_cc_retrieve_cred(kcontext, ccache, 0,
    271   1.7     aidan 				       &mcreds, &forw_creds);
    272  1.19        pk 	if (kerror) {
    273  1.19        pk 		krb5_warn(kcontext, kerror,
    274  1.19        pk 			  "while retrieving V5 initial ticket for copy");
    275   1.7     aidan 		goto nuke_ccache;
    276   1.7     aidan 	}
    277  1.19        pk 
    278   1.7     aidan 	have_forward = 1;
    279   1.7     aidan 
    280  1.17       wiz 	strlcpy(tkt_location, getenv("KRB5CCNAME"), sizeof(tkt_location));
    281  1.12     aidan 	krb5tkfile_env = tkt_location;
    282  1.12     aidan 	has_ccache = 1;
    283   1.7     aidan 	notickets = 0;
    284   1.7     aidan 
    285   1.7     aidan nuke_ccache:
    286   1.7     aidan 	krb5_cc_destroy(kcontext, ccache);
    287   1.7     aidan 	return(!have_forward);
    288   1.7     aidan }
    289   1.7     aidan 
    290   1.7     aidan int
    291  1.26   xtraeme k5_write_creds(void)
    292   1.7     aidan {
    293  1.19        pk 	krb5_error_code kerror;
    294   1.7     aidan 	krb5_ccache ccache;
    295   1.7     aidan 
    296   1.7     aidan 	if (!have_forward)
    297  1.19        pk 		return (1);
    298  1.19        pk 
    299  1.19        pk 	kerror = krb5_cc_default(kcontext, &ccache);
    300  1.19        pk 	if (kerror) {
    301  1.19        pk 		krb5_warn(kcontext, kerror, "while getting default ccache");
    302  1.19        pk 		return (1);
    303   1.7     aidan 	}
    304   1.7     aidan 
    305  1.19        pk 	kerror = krb5_cc_initialize(kcontext, ccache, me);
    306  1.19        pk 	if (kerror) {
    307  1.19        pk 		krb5_warn(kcontext, kerror,
    308  1.19        pk 			  "while re-initializing V5 ccache as user");
    309   1.7     aidan 		goto nuke_ccache_contents;
    310   1.7     aidan 	}
    311   1.7     aidan 
    312  1.19        pk 	kerror = krb5_cc_store_cred(kcontext, ccache, &forw_creds);
    313  1.19        pk 	if (kerror) {
    314  1.19        pk 		krb5_warn(kcontext, kerror,
    315  1.19        pk 			  "while re-storing V5 ccache as user");
    316   1.7     aidan 		goto nuke_ccache_contents;
    317   1.7     aidan 	}
    318   1.7     aidan 
    319   1.7     aidan nuke_ccache_contents:
    320   1.7     aidan 	krb5_free_cred_contents(kcontext, &forw_creds);
    321  1.19        pk 	return (kerror != 0);
    322   1.7     aidan }
    323   1.1    brezak 
    324   1.1    brezak /*
    325  1.20     assar  * Get krb4 credentials if needed
    326  1.20     assar  */
    327  1.20     assar #ifdef KERBEROS
    328  1.20     assar static krb5_error_code
    329  1.20     assar krb5_to4 (struct passwd *pw, krb5_context context, krb5_ccache id)
    330  1.20     assar {
    331  1.20     assar 	if (krb5_config_get_bool(context, NULL,
    332  1.20     assar 				 "libdefaults",
    333  1.20     assar 				 "krb4_get_tickets",
    334  1.20     assar 				 NULL)) {
    335  1.20     assar 		CREDENTIALS c;
    336  1.20     assar 		krb5_creds mcred, cred;
    337  1.20     assar 		krb5_error_code ret;
    338  1.20     assar 		krb5_principal princ;
    339  1.20     assar 
    340  1.20     assar 		ret = krb5_cc_get_principal (context, id, &princ);
    341  1.20     assar 		if (ret)
    342  1.20     assar 			return ret;
    343  1.20     assar 
    344  1.20     assar 		ret = krb5_make_principal(context, &mcred.server,
    345  1.20     assar 					  princ->realm,
    346  1.20     assar 					  "krbtgt",
    347  1.20     assar 					  princ->realm,
    348  1.20     assar 					  NULL);
    349  1.20     assar 		krb5_free_principal (context, princ);
    350  1.20     assar 		if (ret)
    351  1.20     assar 			return ret;
    352  1.20     assar 
    353  1.20     assar 		ret = krb5_cc_retrieve_cred(context, id, 0, &mcred, &cred);
    354  1.20     assar 		if(ret == 0) {
    355  1.21     assar 			ret = krb524_convert_creds_kdc_ccache(context, id,
    356  1.21     assar 							      &cred, &c);
    357  1.20     assar 			if(ret == 0) {
    358  1.20     assar 				snprintf(krb4tkfile, sizeof(krb4tkfile),
    359  1.22      joda 					 "%s%d",TKT_ROOT, pw->pw_uid);
    360  1.20     assar 				krb_set_tkt_string(krb4tkfile);
    361  1.20     assar 				tf_setup(&c, c.pname, c.pinst);
    362  1.20     assar 				if (chown(krb4tkfile, pw->pw_uid,
    363  1.20     assar 					  pw->pw_gid) < 0)
    364  1.20     assar 					syslog(LOG_ERR,
    365  1.20     assar 					       "chown tkfile (%s): %m",
    366  1.20     assar 					       krb4tkfile);
    367  1.20     assar 			}
    368  1.20     assar 			memset(&c, 0, sizeof(c));
    369  1.20     assar 			krb5_free_creds_contents(context, &cred);
    370  1.20     assar 		}
    371  1.20     assar 		krb5_free_principal(context, mcred.server);
    372  1.20     assar 	}
    373  1.20     assar 	return 0;
    374  1.20     assar }
    375  1.20     assar #endif /* KERBEROS */
    376  1.20     assar 
    377  1.20     assar /*
    378   1.1    brezak  * Attempt to log the user in using Kerberos authentication
    379   1.1    brezak  *
    380   1.1    brezak  * return 0 on success (will be logged in)
    381   1.1    brezak  *	  1 if Kerberos failed (try local password in login)
    382   1.1    brezak  */
    383   1.1    brezak int
    384  1.26   xtraeme k5login(struct passwd *pw, char *instance, char *localhost, char *password)
    385   1.1    brezak {
    386   1.1    brezak         krb5_error_code kerror;
    387   1.1    brezak 	krb5_creds my_creds;
    388   1.1    brezak 	krb5_timestamp now;
    389   1.1    brezak 	krb5_ccache ccache = NULL;
    390   1.1    brezak 	long lifetime = KRB5_DEFAULT_LIFE;
    391   1.1    brezak 	int options = KRB5_DEFAULT_OPTIONS;
    392   1.1    brezak 	char *realm, *client_name;
    393   1.1    brezak 	char *principal;
    394   1.1    brezak 
    395  1.13   thorpej 	krb5_configured = 1;
    396  1.13   thorpej 
    397  1.12     aidan 	if (login_krb5_forwardable_tgt)
    398  1.12     aidan 		options |= KDC_OPT_FORWARDABLE;
    399  1.12     aidan 
    400   1.1    brezak 	/*
    401   1.1    brezak 	 * Root logins don't use Kerberos.
    402   1.1    brezak 	 * If we have a realm, try getting a ticket-granting ticket
    403   1.1    brezak 	 * and using it to authenticate.  Otherwise, return
    404   1.1    brezak 	 * failure so that we can try the normal passwd file
    405   1.1    brezak 	 * for a password.  If that's ok, log the user in
    406   1.1    brezak 	 * without issuing any tickets.
    407   1.1    brezak 	 */
    408  1.12     aidan 	if (strcmp(pw->pw_name, "root") == 0 ||
    409  1.13   thorpej 	    krb5_get_default_realm(kcontext, &realm)) {
    410  1.13   thorpej 		krb5_configured = 0;
    411   1.1    brezak 		return (1);
    412  1.13   thorpej 	}
    413   1.1    brezak 
    414   1.1    brezak 	/*
    415   1.1    brezak 	 * get TGT for local realm
    416   1.1    brezak 	 * tickets are stored in a file named TKT_ROOT plus uid
    417   1.1    brezak 	 * except for user.root tickets.
    418   1.1    brezak 	 */
    419   1.1    brezak 
    420   1.1    brezak 	if (strcmp(instance, "root") != 0)
    421  1.19        pk 		(void)snprintf(tkt_location, sizeof tkt_location,
    422  1.19        pk 				"FILE:/tmp/krb5cc_%d.%s", pw->pw_uid, tty);
    423   1.1    brezak 	else
    424  1.19        pk 		(void)snprintf(tkt_location, sizeof tkt_location,
    425  1.19        pk 				"FILE:/tmp/krb5cc_root_%d.%s", pw->pw_uid, tty);
    426  1.12     aidan 	krb5tkfile_env = tkt_location;
    427  1.12     aidan 	has_ccache = 1;
    428   1.1    brezak 
    429  1.23    itojun 	if (strlen(instance))
    430  1.23    itojun 		asprintf(&principal, "%s/%s", pw->pw_name, instance);
    431  1.23    itojun 	else
    432  1.23    itojun 		principal = strdup(pw->pw_name);
    433  1.23    itojun 	if (!principal) {
    434  1.23    itojun 		syslog(LOG_NOTICE, "fatal: %s", strerror(errno));
    435  1.23    itojun 		return (1);
    436   1.1    brezak 	}
    437  1.19        pk 
    438   1.8  christos 	if ((kerror = krb5_cc_resolve(kcontext, tkt_location, &ccache)) != 0) {
    439  1.19        pk 		syslog(LOG_NOTICE, "warning: %s while getting default ccache",
    440  1.19        pk 			krb5_get_err_text(kcontext, kerror));
    441  1.19        pk 		return (1);
    442   1.1    brezak 	}
    443   1.1    brezak 
    444   1.8  christos 	if ((kerror = krb5_parse_name(kcontext, principal, &me)) != 0) {
    445  1.19        pk 		syslog(LOG_NOTICE, "warning: %s when parsing name %s",
    446  1.19        pk 			krb5_get_err_text(kcontext, kerror), principal);
    447  1.19        pk 		return (1);
    448   1.1    brezak 	}
    449  1.19        pk 
    450   1.8  christos 	if ((kerror = krb5_unparse_name(kcontext, me, &client_name)) != 0) {
    451  1.19        pk 		syslog(LOG_NOTICE, "warning: %s when unparsing name %s",
    452  1.19        pk 			krb5_get_err_text(kcontext, kerror), principal);
    453  1.19        pk 		return (1);
    454   1.1    brezak 	}
    455   1.1    brezak 
    456   1.5   mycroft 	kerror = krb5_cc_initialize(kcontext, ccache, me);
    457   1.1    brezak 	if (kerror != 0) {
    458  1.19        pk 		syslog(LOG_NOTICE, "%s when initializing cache %s",
    459  1.19        pk 			krb5_get_err_text(kcontext, kerror), tkt_location);
    460  1.19        pk 		return (1);
    461   1.1    brezak 	}
    462   1.1    brezak 
    463   1.1    brezak 	memset((char *)&my_creds, 0, sizeof(my_creds));
    464  1.19        pk 
    465   1.1    brezak 	my_creds.client = me;
    466   1.1    brezak 
    467   1.8  christos 	if ((kerror = krb5_build_principal_ext(kcontext,
    468  1.19        pk 			&server,
    469  1.19        pk 			krb5_realm_length(*krb5_princ_realm(kcontext, me)),
    470  1.19        pk 			krb5_realm_data(*krb5_princ_realm(kcontext, me)),
    471  1.19        pk 			KRB5_TGS_NAME_SIZE,
    472  1.19        pk 			KRB5_TGS_NAME,
    473  1.19        pk 			krb5_realm_length(*krb5_princ_realm(kcontext, me)),
    474  1.19        pk 			krb5_realm_data(*krb5_princ_realm(kcontext, me)),
    475  1.19        pk 			0)) != 0) {
    476  1.19        pk 		syslog(LOG_NOTICE, "%s while building server name",
    477  1.19        pk 			krb5_get_err_text(kcontext, kerror));
    478  1.19        pk 		return (1);
    479   1.1    brezak 	}
    480   1.1    brezak 
    481   1.1    brezak 	my_creds.server = server;
    482   1.1    brezak 
    483   1.8  christos 	if ((kerror = krb5_timeofday(kcontext, &now)) != 0) {
    484  1.19        pk 		syslog(LOG_NOTICE, "%s while getting time of day",
    485  1.19        pk 			krb5_get_err_text(kcontext, kerror));
    486  1.19        pk 		return (1);
    487   1.1    brezak 	}
    488  1.19        pk 
    489   1.1    brezak 	my_creds.times.starttime = 0;	/* start timer when request
    490   1.1    brezak 					   gets to KDC */
    491   1.1    brezak 	my_creds.times.endtime = now + lifetime;
    492   1.1    brezak 	my_creds.times.renew_till = 0;
    493   1.1    brezak 
    494   1.5   mycroft 	kerror = krb5_get_in_tkt_with_password(kcontext, options,
    495  1.11     aidan 					       NULL,
    496   1.5   mycroft 					       NULL,
    497   1.5   mycroft 					       NULL,
    498   1.5   mycroft 					       password,
    499   1.5   mycroft 					       ccache,
    500   1.5   mycroft 					       &my_creds, 0);
    501   1.1    brezak 
    502  1.14   thorpej 	if (my_creds.server != NULL)
    503  1.14   thorpej 		krb5_free_principal(kcontext, my_creds.server);
    504   1.1    brezak 
    505   1.1    brezak 	if (chown(&tkt_location[5], pw->pw_uid, pw->pw_gid) < 0)
    506   1.1    brezak 		syslog(LOG_ERR, "chown tkfile (%s): %m", &tkt_location[5]);
    507   1.1    brezak 
    508   1.1    brezak 	if (kerror) {
    509  1.19        pk 		if (kerror == KRB5KRB_AP_ERR_BAD_INTEGRITY)
    510  1.19        pk 			printf("%s: Kerberos Password incorrect\n", principal);
    511  1.19        pk 		else
    512  1.19        pk 			krb5_warn(kcontext, kerror,
    513  1.19        pk 				  "while getting initial credentials");
    514   1.1    brezak 
    515  1.19        pk 		return (1);
    516  1.15     aidan 	}
    517  1.19        pk 
    518  1.19        pk 	if (k5_verify_creds(kcontext, ccache) < 0)
    519  1.19        pk 		return (1);
    520  1.15     aidan 
    521  1.20     assar #ifdef KERBEROS
    522  1.20     assar 	if ((kerror = krb5_to4(pw, kcontext, ccache)) != 0)
    523  1.23    itojun 		krb5_warn(kcontext, kerror, "error converting krb4 creds");
    524  1.20     assar #endif
    525   1.1    brezak 
    526   1.1    brezak 	/* Success */
    527   1.1    brezak 	notickets = 0;
    528  1.19        pk 	return (0);
    529   1.1    brezak }
    530   1.1    brezak 
    531   1.1    brezak /*
    532   1.1    brezak  * Remove any credentials
    533   1.1    brezak  */
    534   1.1    brezak void
    535  1.26   xtraeme k5destroy(void)
    536   1.1    brezak {
    537  1.19        pk         krb5_error_code kerror;
    538   1.1    brezak 	krb5_ccache ccache = NULL;
    539   1.1    brezak 
    540  1.12     aidan 	if (krb5tkfile_env == NULL)
    541  1.19        pk 		return;
    542   1.1    brezak 
    543  1.19        pk 	kerror = krb5_cc_resolve(kcontext, krb5tkfile_env, &ccache);
    544  1.19        pk 	if (kerror == 0)
    545  1.19        pk 		(void)krb5_cc_destroy(kcontext, ccache);
    546   1.1    brezak }
    547  1.20     assar #endif /* KERBEROS5 */
    548