xref: /csrg-svn/usr.bin/login/klogin.c (revision 43310)
1*43310Skfall /*-
2*43310Skfall  * Copyright (c) 1990 The Regents of the University of California.
3*43310Skfall  * All rights reserved.
4*43310Skfall  *
5*43310Skfall  * %sccs.include.redist.c%
6*43310Skfall  */
7*43310Skfall 
8*43310Skfall #ifndef lint
9*43310Skfall static char sccsid[] = "@(#)klogin.c	5.1 (Berkeley) 06/19/90";
10*43310Skfall #endif /* not lint */
11*43310Skfall 
12*43310Skfall #ifdef KERBEROS
13*43310Skfall #include <sys/param.h>
14*43310Skfall #include <sys/syslog.h>
15*43310Skfall #include <kerberosIV/des.h>
16*43310Skfall #include <kerberosIV/krb.h>
17*43310Skfall #include <pwd.h>
18*43310Skfall #include <netdb.h>
19*43310Skfall 
20*43310Skfall #define	PRINCIPAL_NAME	pw->pw_name
21*43310Skfall #define	PRINCIPAL_INST	""
22*43310Skfall #define	INITIAL_TICKET	"krbtgt"
23*43310Skfall #define	VERIFY_SERVICE	"rcmd"
24*43310Skfall 
25*43310Skfall extern int notickets;
26*43310Skfall 
27*43310Skfall /*
28*43310Skfall  * return 0 on success
29*43310Skfall  *	  1 if Kerberos not around (try local)
30*43310Skfall  *	  2 on failure
31*43310Skfall  */
32*43310Skfall 
33*43310Skfall klogin(pw, localhost, name, tty)
34*43310Skfall 	struct passwd *pw;
35*43310Skfall 	char *localhost, *name, *tty;
36*43310Skfall {
37*43310Skfall 	int kerror;
38*43310Skfall 	AUTH_DAT authdata;
39*43310Skfall 	KTEXT_ST ticket;
40*43310Skfall 	struct hostent *hp;
41*43310Skfall 	unsigned long faddr;
42*43310Skfall 	char tkfile[MAXPATHLEN], realm[REALM_SZ], savehost[MAXHOSTNAMELEN];
43*43310Skfall 
44*43310Skfall 	/*
45*43310Skfall 	 * If we aren't Kerberos-authenticated, try the normal pw file
46*43310Skfall 	 * for a password.  If that's ok, log the user in without issueing
47*43310Skfall 	 * any tickets.
48*43310Skfall 	 */
49*43310Skfall 	if (krb_get_lrealm(realm, 1) != KSUCCESS)
50*43310Skfall 		return(1);
51*43310Skfall 
52*43310Skfall 	/*
53*43310Skfall 	 * get TGT for local realm; by convention, store tickets in file
54*43310Skfall 	 * associated with tty name, which should be available.
55*43310Skfall 	 */
56*43310Skfall 	(void)sprintf(tkfile, "%s_%s", TKT_ROOT, tty);
57*43310Skfall 
58*43310Skfall 	if (setenv("KRBTKFILE", tkfile, 1) < 0) {
59*43310Skfall 		kerror = INTK_ERR;
60*43310Skfall 		syslog(LOG_ERR, "couldn't set tkfile environ");
61*43310Skfall 	} else {
62*43310Skfall 		(void)unlink(tkfile);
63*43310Skfall 		kerror = krb_get_pw_in_tkt(PRINCIPAL_NAME, PRINCIPAL_INST,
64*43310Skfall 		    realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, name);
65*43310Skfall 	}
66*43310Skfall 
67*43310Skfall 	/*
68*43310Skfall 	 * If we got a TGT, get a local "rcmd" ticket and check it so as to
69*43310Skfall 	 * ensure that we are not talking to a bogus Kerberos server.
70*43310Skfall 	 *
71*43310Skfall 	 * There are 2 cases where we still allow a login:
72*43310Skfall 	 *	1: the VERIFY_SERVICE doesn't exist in the KDC
73*43310Skfall 	 *	2: local host has no srvtab, as (hopefully) indicated by a
74*43310Skfall 	 *	   return value of RD_AP_UNDEC from krb_rd_req().
75*43310Skfall 	 */
76*43310Skfall 	if (kerror != INTK_OK) {
77*43310Skfall 		(void)unlink(tkfile);
78*43310Skfall 		if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN)
79*43310Skfall 			syslog(LOG_ERR, "Kerberos intkt error: %s",
80*43310Skfall 			    krb_err_txt[kerror]);
81*43310Skfall 		return(2);
82*43310Skfall 	}
83*43310Skfall 
84*43310Skfall 	if (chown(tkfile, pw->pw_uid, pw->pw_gid) < 0)
85*43310Skfall 		syslog(LOG_ERR, "chown tkfile: %m");
86*43310Skfall 
87*43310Skfall 	(void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
88*43310Skfall 	savehost[sizeof(savehost)-1] = NULL;
89*43310Skfall 	kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
90*43310Skfall 
91*43310Skfall 	/*
92*43310Skfall 	 * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
93*43310Skfall 	 * still allow login with tickets, but log the error condition.
94*43310Skfall 	 */
95*43310Skfall 	if (kerror == KDC_PR_UNKNOWN) {
96*43310Skfall 		syslog(LOG_NOTICE, "warning: TGT not verified (%s)",
97*43310Skfall 		    krb_err_txt[kerror]);
98*43310Skfall 		notickets = 0;
99*43310Skfall 		return(0);
100*43310Skfall 	}
101*43310Skfall 
102*43310Skfall 	if (kerror != KSUCCESS) {
103*43310Skfall 		(void)printf("unable to use TGT: (%s)\n", krb_err_txt[kerror]);
104*43310Skfall 		syslog(LOG_NOTICE, "unable to use TGT: (%s)",
105*43310Skfall 		    krb_err_txt[kerror]);
106*43310Skfall 		dest_tkt();
107*43310Skfall 		return(2);
108*43310Skfall 	}
109*43310Skfall 
110*43310Skfall 	if (!(hp = gethostbyname(localhost))) {
111*43310Skfall 		syslog(LOG_ERR, "couldn't get local host address");
112*43310Skfall 		return(2);
113*43310Skfall 	}
114*43310Skfall 	bcopy((void *)hp->h_addr, (void *)&faddr, sizeof(faddr));
115*43310Skfall 
116*43310Skfall 	kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
117*43310Skfall 	    &authdata, "");
118*43310Skfall 	if (kerror == KSUCCESS) {
119*43310Skfall 		notickets = 0;
120*43310Skfall 		return(0);
121*43310Skfall 	}
122*43310Skfall 	if (kerror = RD_AP_UNDEC) {
123*43310Skfall 		syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]);
124*43310Skfall 		notickets = 0;
125*43310Skfall 		return(0);
126*43310Skfall 	}
127*43310Skfall 	(void)printf("unable to verify %s ticket: (%s)\n", VERIFY_SERVICE,
128*43310Skfall 	    krb_err_txt[kerror]);
129*43310Skfall 	syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
130*43310Skfall 	    krb_err_txt[kerror]);
131*43310Skfall 	return(2);
132*43310Skfall }
133*43310Skfall #endif
134