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