Home | History | Annotate | Line # | Download | only in login
k5login.c revision 1.13
      1  1.13   thorpej /*	$NetBSD: k5login.c,v 1.13 2000/08/02 05:58:35 thorpej 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.4     lukem #include <sys/cdefs.h>
     37   1.1    brezak #ifndef lint
     38   1.2       jtc #if 0
     39   1.2       jtc static char sccsid[] = "@(#)klogin.c	5.11 (Berkeley) 7/12/92";
     40   1.2       jtc #endif
     41  1.13   thorpej __RCSID("$NetBSD: k5login.c,v 1.13 2000/08/02 05:58:35 thorpej Exp $");
     42   1.1    brezak #endif /* not lint */
     43   1.1    brezak 
     44   1.1    brezak #ifdef KERBEROS5
     45   1.1    brezak #include <sys/param.h>
     46   1.1    brezak #include <sys/syslog.h>
     47   1.8  christos #include <krb5/krb5.h>
     48  1.11     aidan #include <com_err.h>
     49   1.1    brezak #include <pwd.h>
     50   1.1    brezak #include <netdb.h>
     51   1.1    brezak #include <stdio.h>
     52   1.7     aidan #include <stdlib.h>
     53   1.1    brezak #include <string.h>
     54   1.1    brezak #include <unistd.h>
     55   1.1    brezak 
     56   1.1    brezak #define KRB5_DEFAULT_OPTIONS 0
     57   1.1    brezak #define KRB5_DEFAULT_LIFE 60*60*10 /* 10 hours */
     58   1.1    brezak 
     59   1.5   mycroft krb5_context kcontext;
     60   1.1    brezak 
     61   1.9     aidan int notickets;
     62  1.13   thorpej int krb5_configured;
     63  1.12     aidan char *krb5tkfile_env;
     64   1.1    brezak extern char *tty;
     65  1.12     aidan extern int login_krb5_forwardable_tgt;
     66  1.12     aidan extern int has_ccache;
     67   1.1    brezak 
     68   1.1    brezak static char tkt_location[MAXPATHLEN];
     69   1.7     aidan static krb5_creds forw_creds;
     70   1.7     aidan int have_forward;
     71   1.7     aidan static krb5_principal me, server;
     72   1.7     aidan 
     73   1.8  christos int k5_read_creds __P((char *));
     74   1.8  christos int k5_write_creds __P((void));
     75  1.12     aidan int k5login __P((struct passwd *, char *, char *, char *));
     76  1.12     aidan void k5destroy __P((void));
     77   1.8  christos 
     78  1.11     aidan #ifndef krb5_realm_length
     79  1.11     aidan #define krb5_realm_length(r)	((r).length)
     80  1.11     aidan #endif
     81  1.11     aidan #ifndef krb5_realm_data
     82  1.11     aidan #define krb5_realm_data(r)	((r).data)
     83  1.11     aidan #endif
     84  1.11     aidan 
     85   1.7     aidan /*
     86   1.7     aidan  * Attempt to read forwarded kerberos creds
     87   1.7     aidan  *
     88   1.7     aidan  * return 0 on success (forwarded creds in memory)
     89   1.7     aidan  *        1 if no forwarded creds.
     90   1.7     aidan  */
     91   1.7     aidan int
     92   1.7     aidan k5_read_creds(username)
     93   1.7     aidan 	char *username;
     94   1.7     aidan {
     95   1.7     aidan 	krb5_error_code code;
     96   1.7     aidan 	krb5_creds mcreds;
     97   1.7     aidan 	krb5_ccache ccache;
     98   1.7     aidan 
     99   1.7     aidan 	have_forward = 0;
    100   1.7     aidan 	memset((char*) &mcreds, 0, sizeof(forw_creds));
    101   1.7     aidan 	memset((char*) &forw_creds, 0, sizeof(forw_creds));
    102   1.7     aidan 
    103   1.7     aidan 	code = krb5_cc_default(kcontext, &ccache);
    104   1.7     aidan 	if (code) {
    105   1.7     aidan 		com_err("login", code, "while getting default ccache");
    106   1.7     aidan 		return(1);
    107   1.7     aidan 	}
    108   1.7     aidan 
    109   1.7     aidan 	code = krb5_parse_name(kcontext, username, &me);
    110   1.7     aidan 	if (code) {
    111   1.7     aidan 		com_err("login", code, "when parsing name %s", username);
    112   1.7     aidan 		return(1);
    113   1.7     aidan 	}
    114   1.7     aidan 
    115   1.7     aidan 	mcreds.client = me;
    116   1.7     aidan 	code = krb5_build_principal_ext(kcontext, &mcreds.server,
    117  1.11     aidan 					krb5_realm_length(*krb5_princ_realm(kcontext, me)),
    118  1.11     aidan 					krb5_realm_data(*krb5_princ_realm(kcontext, me)),
    119  1.11     aidan 					KRB5_TGS_NAME_SIZE,
    120  1.11     aidan 					KRB5_TGS_NAME,
    121  1.11     aidan 					krb5_realm_length(*krb5_princ_realm(kcontext, me)),
    122  1.11     aidan 					krb5_realm_data(*krb5_princ_realm(kcontext, me)),
    123   1.7     aidan 					0);
    124   1.7     aidan 	if (code) {
    125   1.7     aidan 		com_err("login", code, "while building server name");
    126   1.7     aidan 		goto nuke_ccache;
    127   1.7     aidan 	}
    128   1.7     aidan 
    129   1.7     aidan 	code = krb5_cc_retrieve_cred(kcontext, ccache, 0,
    130   1.7     aidan 				       &mcreds, &forw_creds);
    131   1.7     aidan 	if (code) {
    132   1.7     aidan 		com_err("login", code, "while retrieving V5 initial ticket for copy");
    133   1.7     aidan 		goto nuke_ccache;
    134   1.7     aidan 	}
    135   1.7     aidan 	have_forward = 1;
    136   1.7     aidan 
    137   1.7     aidan 	strcpy(tkt_location, getenv("KRB5CCNAME"));
    138  1.12     aidan 	krb5tkfile_env = tkt_location;
    139  1.12     aidan 	has_ccache = 1;
    140   1.7     aidan 	notickets = 0;
    141   1.7     aidan 
    142   1.7     aidan nuke_ccache:
    143   1.7     aidan 	krb5_cc_destroy(kcontext, ccache);
    144   1.7     aidan 	return(!have_forward);
    145   1.7     aidan }
    146   1.7     aidan 
    147   1.7     aidan int
    148   1.7     aidan k5_write_creds()
    149   1.7     aidan {
    150   1.7     aidan 	krb5_error_code code;
    151   1.7     aidan 	krb5_ccache ccache;
    152   1.7     aidan 
    153   1.7     aidan 	if (!have_forward)
    154   1.7     aidan 		return(1);
    155   1.7     aidan 	code = krb5_cc_default(kcontext, &ccache);
    156   1.7     aidan 	if (code) {
    157   1.7     aidan 		com_err("login", code, "while getting default ccache");
    158   1.7     aidan 		return(1);
    159   1.7     aidan 	}
    160   1.7     aidan 
    161   1.7     aidan 	code = krb5_cc_initialize(kcontext, ccache, me);
    162   1.7     aidan 	if (code) {
    163   1.7     aidan 		com_err("login", code, "while re-initializing V5 ccache as user");
    164   1.7     aidan 		goto nuke_ccache_contents;
    165   1.7     aidan 	}
    166   1.7     aidan 
    167   1.7     aidan 	code = krb5_cc_store_cred(kcontext, ccache, &forw_creds);
    168   1.7     aidan 	if (code) {
    169   1.7     aidan 		com_err("login", code, "while re-storing V5 ccache as user");
    170   1.7     aidan 		goto nuke_ccache_contents;
    171   1.7     aidan 	}
    172   1.7     aidan 
    173   1.7     aidan nuke_ccache_contents:
    174   1.7     aidan 	krb5_free_cred_contents(kcontext, &forw_creds);
    175   1.7     aidan 	return(code != 0);
    176   1.7     aidan }
    177   1.1    brezak 
    178   1.1    brezak /*
    179   1.1    brezak  * Attempt to log the user in using Kerberos authentication
    180   1.1    brezak  *
    181   1.1    brezak  * return 0 on success (will be logged in)
    182   1.1    brezak  *	  1 if Kerberos failed (try local password in login)
    183   1.1    brezak  */
    184   1.1    brezak int
    185  1.12     aidan k5login(pw, instance, localhost, password)
    186   1.1    brezak 	struct passwd *pw;
    187   1.1    brezak 	char *instance, *localhost, *password;
    188   1.1    brezak {
    189   1.1    brezak         krb5_error_code kerror;
    190   1.1    brezak 	krb5_creds my_creds;
    191   1.1    brezak 	krb5_timestamp now;
    192   1.1    brezak 	krb5_ccache ccache = NULL;
    193   1.1    brezak 	long lifetime = KRB5_DEFAULT_LIFE;
    194   1.1    brezak 	int options = KRB5_DEFAULT_OPTIONS;
    195   1.1    brezak 	char *realm, *client_name;
    196   1.1    brezak 	char *principal;
    197   1.1    brezak 
    198  1.13   thorpej 	krb5_configured = 1;
    199  1.13   thorpej 
    200  1.12     aidan 	if (login_krb5_forwardable_tgt)
    201  1.12     aidan 		options |= KDC_OPT_FORWARDABLE;
    202  1.12     aidan 
    203   1.1    brezak 	/*
    204   1.1    brezak 	 * Root logins don't use Kerberos.
    205   1.1    brezak 	 * If we have a realm, try getting a ticket-granting ticket
    206   1.1    brezak 	 * and using it to authenticate.  Otherwise, return
    207   1.1    brezak 	 * failure so that we can try the normal passwd file
    208   1.1    brezak 	 * for a password.  If that's ok, log the user in
    209   1.1    brezak 	 * without issuing any tickets.
    210   1.1    brezak 	 */
    211  1.12     aidan 	if (strcmp(pw->pw_name, "root") == 0 ||
    212  1.13   thorpej 	    krb5_get_default_realm(kcontext, &realm)) {
    213  1.13   thorpej 		krb5_configured = 0;
    214   1.1    brezak 		return (1);
    215  1.13   thorpej 	}
    216   1.1    brezak 
    217   1.1    brezak 	/*
    218   1.1    brezak 	 * get TGT for local realm
    219   1.1    brezak 	 * tickets are stored in a file named TKT_ROOT plus uid
    220   1.1    brezak 	 * except for user.root tickets.
    221   1.1    brezak 	 */
    222   1.1    brezak 
    223   1.1    brezak 	if (strcmp(instance, "root") != 0)
    224   1.3       mrg 	    (void)snprintf(tkt_location, sizeof tkt_location,
    225   1.3       mrg 		"FILE:/tmp/krb5cc_%d.%s", pw->pw_uid, tty);
    226   1.1    brezak 	else
    227   1.3       mrg 	    (void)snprintf(tkt_location, sizeof tkt_location,
    228   1.3       mrg 		"FILE:/tmp/krb5cc_root_%d.%s", pw->pw_uid, tty);
    229  1.12     aidan 	krb5tkfile_env = tkt_location;
    230  1.12     aidan 	has_ccache = 1;
    231   1.1    brezak 
    232   1.5   mycroft 	principal = (char *)malloc(strlen(pw->pw_name)+strlen(instance)+2);
    233   1.3       mrg 	strcpy(principal, pw->pw_name);	/* XXX strcpy is safe */
    234   1.1    brezak 	if (strlen(instance)) {
    235   1.3       mrg 	    strcat(principal, "/");		/* XXX strcat is safe */
    236   1.3       mrg 	    strcat(principal, instance);	/* XXX strcat is safe */
    237   1.1    brezak 	}
    238   1.1    brezak 
    239   1.8  christos 	if ((kerror = krb5_cc_resolve(kcontext, tkt_location, &ccache)) != 0) {
    240   1.1    brezak 	    syslog(LOG_NOTICE, "warning: %s while getting default ccache",
    241   1.1    brezak 		error_message(kerror));
    242   1.1    brezak 	    return(1);
    243   1.1    brezak 	}
    244   1.1    brezak 
    245   1.8  christos 	if ((kerror = krb5_parse_name(kcontext, principal, &me)) != 0) {
    246   1.1    brezak 	    syslog(LOG_NOTICE, "warning: %s when parsing name %s",
    247   1.1    brezak 		error_message(kerror), principal);
    248   1.1    brezak 	    return(1);
    249   1.1    brezak 	}
    250   1.1    brezak 
    251   1.8  christos 	if ((kerror = krb5_unparse_name(kcontext, me, &client_name)) != 0) {
    252   1.1    brezak 	    syslog(LOG_NOTICE, "warning: %s when unparsing name %s",
    253   1.1    brezak 		error_message(kerror), principal);
    254   1.1    brezak 	    return(1);
    255   1.1    brezak 	}
    256   1.1    brezak 
    257   1.5   mycroft 	kerror = krb5_cc_initialize(kcontext, ccache, me);
    258   1.1    brezak 	if (kerror != 0) {
    259   1.1    brezak 	    syslog(LOG_NOTICE, "%s when initializing cache %s",
    260   1.1    brezak 		error_message(kerror), tkt_location);
    261   1.1    brezak 	    return(1);
    262   1.1    brezak 	}
    263   1.1    brezak 
    264   1.1    brezak 	memset((char *)&my_creds, 0, sizeof(my_creds));
    265   1.1    brezak 
    266   1.1    brezak 	my_creds.client = me;
    267   1.1    brezak 
    268   1.8  christos 	if ((kerror = krb5_build_principal_ext(kcontext,
    269   1.5   mycroft 					&server,
    270  1.11     aidan 					krb5_realm_length(*krb5_princ_realm(kcontext, me)),
    271  1.11     aidan 					krb5_realm_data(*krb5_princ_realm(kcontext, me)),
    272  1.11     aidan 					KRB5_TGS_NAME_SIZE,
    273  1.11     aidan 					KRB5_TGS_NAME,
    274  1.11     aidan 					krb5_realm_length(*krb5_princ_realm(kcontext, me)),
    275  1.11     aidan 					krb5_realm_data(*krb5_princ_realm(kcontext, me)),
    276   1.8  christos 					0)) != 0) {
    277   1.1    brezak 	    syslog(LOG_NOTICE, "%s while building server name",
    278   1.1    brezak 		error_message(kerror));
    279   1.1    brezak 	    return(1);
    280   1.1    brezak 	}
    281   1.1    brezak 
    282   1.1    brezak 	my_creds.server = server;
    283   1.1    brezak 
    284   1.8  christos 	if ((kerror = krb5_timeofday(kcontext, &now)) != 0) {
    285   1.1    brezak 	    syslog(LOG_NOTICE, "%s while getting time of day",
    286   1.1    brezak 		error_message(kerror));
    287   1.1    brezak 	    return(1);
    288   1.1    brezak 	}
    289   1.1    brezak 	my_creds.times.starttime = 0;	/* start timer when request
    290   1.1    brezak 					   gets to KDC */
    291   1.1    brezak 	my_creds.times.endtime = now + lifetime;
    292   1.1    brezak 	my_creds.times.renew_till = 0;
    293   1.1    brezak 
    294   1.5   mycroft 	kerror = krb5_get_in_tkt_with_password(kcontext, options,
    295  1.11     aidan 					       NULL,
    296   1.5   mycroft 					       NULL,
    297   1.5   mycroft 					       NULL,
    298   1.5   mycroft 					       password,
    299   1.5   mycroft 					       ccache,
    300   1.5   mycroft 					       &my_creds, 0);
    301   1.1    brezak 
    302   1.5   mycroft 	krb5_free_principal(kcontext, server);
    303   1.1    brezak 
    304   1.1    brezak 	if (chown(&tkt_location[5], pw->pw_uid, pw->pw_gid) < 0)
    305   1.1    brezak 		syslog(LOG_ERR, "chown tkfile (%s): %m", &tkt_location[5]);
    306   1.1    brezak 
    307   1.1    brezak 	if (kerror) {
    308   1.1    brezak 	    if (kerror == KRB5KRB_AP_ERR_BAD_INTEGRITY)
    309   1.1    brezak 		printf("%s: Kerberos Password incorrect\n", principal);
    310   1.1    brezak 	    else
    311   1.1    brezak 		printf("%s while getting initial credentials\n", error_message(kerror));
    312   1.1    brezak 
    313   1.1    brezak 	    return(1);
    314   1.1    brezak 	}
    315   1.1    brezak 
    316   1.1    brezak 	/* Success */
    317   1.1    brezak 	notickets = 0;
    318   1.1    brezak 	return(0);
    319   1.1    brezak }
    320   1.1    brezak 
    321   1.1    brezak /*
    322   1.1    brezak  * Remove any credentials
    323   1.1    brezak  */
    324   1.1    brezak void
    325  1.12     aidan k5destroy()
    326   1.1    brezak {
    327   1.1    brezak         krb5_error_code code;
    328   1.1    brezak 	krb5_ccache ccache = NULL;
    329   1.1    brezak 
    330  1.12     aidan 	if (krb5tkfile_env == NULL)
    331   1.1    brezak 	    return;
    332   1.1    brezak 
    333  1.12     aidan 	code = krb5_cc_resolve(kcontext, krb5tkfile_env, &ccache);
    334  1.10     aidan 	if (!code)
    335   1.5   mycroft 	    code = krb5_cc_destroy(kcontext, ccache);
    336   1.1    brezak }
    337   1.1    brezak #endif
    338