xref: /original-bsd/usr.bin/login/klogin.c (revision 6a6b77ee)
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.4 (Berkeley) 09/05/90";
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	""
22 #define	INITIAL_TICKET	"krbtgt"
23 #define	VERIFY_SERVICE	"rcmd"
24 
25 extern int notickets;
26 
27 /*
28  * Attempt to log the user in using Kerberos authentication
29  *
30  * return 0 on success (will be logged in)
31  *	  1 if Kerberos failed (try local password in login)
32  */
33 
34 klogin(pw, localhost, password)
35 	struct passwd *pw;
36 	char *localhost, *password;
37 {
38 	int kerror;
39 	AUTH_DAT authdata;
40 	KTEXT_ST ticket;
41 	struct hostent *hp;
42 	unsigned long faddr;
43 	char realm[REALM_SZ], savehost[MAXHOSTNAMELEN];
44 	char tkt_location[MAXPATHLEN];
45 
46 	/*
47 	 * If we aren't Kerberos-authenticated, try the normal pw file
48 	 * for a password.  If that's ok, log the user in without issueing
49 	 * any tickets.
50 	 */
51 	if (krb_get_lrealm(realm, 0) != KSUCCESS) {
52 		syslog(LOG_ERR, "couldn't get local Kerberos realm");
53 		return(1);
54 	}
55 
56 	/*
57 	 * get TGT for local realm
58 	 * tickets are stored in a file determined by calling tkt_string()
59 	 */
60 
61 	(void)sprintf(tkt_location, "%s%d", TKT_ROOT, pw->pw_uid);
62 	(void)krb_set_tkt_string(tkt_location);
63 	(void)dest_tkt();
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 		dest_tkt();
78 		if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN)
79 			syslog(LOG_ERR, "Kerberos intkt error: %s",
80 			    krb_err_txt[kerror]);
81 		return(1);
82 	}
83 
84 	if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
85 		syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
86 
87 	(void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
88 	savehost[sizeof(savehost)-1] = NULL;
89 
90 	/*
91 	 * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
92 	 * still allow login with tickets, but log the error condition.
93 	 */
94 
95 	kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
96 	if (kerror == KDC_PR_UNKNOWN) {
97 		syslog(LOG_NOTICE, "warning: TGT not verified (%s)",
98 		    krb_err_txt[kerror]);
99 		notickets = 0;
100 		return(0);
101 	}
102 
103 	if (kerror != KSUCCESS) {
104 		(void)printf("unable to use TGT: (%s)\n", krb_err_txt[kerror]);
105 		syslog(LOG_NOTICE, "unable to use TGT: (%s)",
106 		    krb_err_txt[kerror]);
107 		dest_tkt();
108 		return(1);
109 	}
110 
111 	if (!(hp = gethostbyname(localhost))) {
112 		syslog(LOG_ERR, "couldn't get local host address");
113 		dest_tkt();
114 		return(1);
115 	}
116 
117 	bcopy((void *)hp->h_addr, (void *)&faddr, sizeof(faddr));
118 
119 	kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
120 	    &authdata, "");
121 
122 	if (kerror == KSUCCESS) {
123 		notickets = 0;
124 		return(0);
125 	}
126 
127 	/* undecipherable: probably didn't have a srvtab on the local host */
128 	if (kerror = RD_AP_UNDEC) {
129 		syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]);
130 		dest_tkt();
131 		return(1);
132 	}
133 	/* failed for some other reason */
134 	(void)printf("unable to verify %s ticket: (%s)\n", VERIFY_SERVICE,
135 	    krb_err_txt[kerror]);
136 	syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
137 	    krb_err_txt[kerror]);
138 	dest_tkt();
139 	return(1);
140 }
141 #endif
142