xref: /original-bsd/usr.bin/login/klogin.c (revision abe165e9)
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.3 (Berkeley) 06/21/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, 1) != KSUCCESS)
52 		return(1);
53 
54 	/*
55 	 * get TGT for local realm
56 	 * tickets are stored in a file determined by calling tkt_string()
57 	 */
58 
59 	(void)sprintf(tkt_location, "%s%d", TKT_ROOT, pw->pw_uid);
60 	(void)krb_set_tkt_string(tkt_location);
61 	(void)dest_tkt();
62 
63 	kerror = krb_get_pw_in_tkt(PRINCIPAL_NAME, PRINCIPAL_INST,
64 		    realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password);
65 	/*
66 	 * If we got a TGT, get a local "rcmd" ticket and check it so as to
67 	 * ensure that we are not talking to a bogus Kerberos server.
68 	 *
69 	 * There are 2 cases where we still allow a login:
70 	 *	1: the VERIFY_SERVICE doesn't exist in the KDC
71 	 *	2: local host has no srvtab, as (hopefully) indicated by a
72 	 *	   return value of RD_AP_UNDEC from krb_rd_req().
73 	 */
74 	if (kerror != INTK_OK) {
75 		dest_tkt();
76 		if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN)
77 			syslog(LOG_ERR, "Kerberos intkt error: %s",
78 			    krb_err_txt[kerror]);
79 		return(1);
80 	}
81 
82 	if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
83 		syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
84 
85 	(void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
86 	savehost[sizeof(savehost)-1] = NULL;
87 
88 	/*
89 	 * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
90 	 * still allow login with tickets, but log the error condition.
91 	 */
92 
93 	kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
94 	if (kerror == KDC_PR_UNKNOWN) {
95 		syslog(LOG_NOTICE, "warning: TGT not verified (%s)",
96 		    krb_err_txt[kerror]);
97 		notickets = 0;
98 		return(0);
99 	}
100 
101 	if (kerror != KSUCCESS) {
102 		(void)printf("unable to use TGT: (%s)\n", krb_err_txt[kerror]);
103 		syslog(LOG_NOTICE, "unable to use TGT: (%s)",
104 		    krb_err_txt[kerror]);
105 		dest_tkt();
106 		return(1);
107 	}
108 
109 	if (!(hp = gethostbyname(localhost))) {
110 		syslog(LOG_ERR, "couldn't get local host address");
111 		dest_tkt();
112 		return(1);
113 	}
114 
115 	bcopy((void *)hp->h_addr, (void *)&faddr, sizeof(faddr));
116 
117 	kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
118 	    &authdata, "");
119 
120 	if (kerror == KSUCCESS) {
121 		notickets = 0;
122 		return(0);
123 	}
124 
125 	/* undecipherable: probably didn't have a srvtab on the local host */
126 	if (kerror = RD_AP_UNDEC) {
127 		syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]);
128 		dest_tkt();
129 		return(1);
130 	}
131 	/* failed for some other reason */
132 	(void)printf("unable to verify %s ticket: (%s)\n", VERIFY_SERVICE,
133 	    krb_err_txt[kerror]);
134 	syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
135 	    krb_err_txt[kerror]);
136 	dest_tkt();
137 	return(1);
138 }
139 #endif
140