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