Annotation of src/usr.bin/login/klogin.c, Revision 1.10
1.10 ! millert 1: /* $OpenBSD: klogin.c,v 1.9 1999/11/24 16:08:18 art Exp $ */
1.2 deraadt 2: /* $NetBSD: klogin.c,v 1.7 1996/05/21 22:07:04 mrg Exp $ */
1.1 deraadt 3:
4: /*-
5: * Copyright (c) 1990, 1993, 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)klogin.c 8.3 (Berkeley) 4/2/94";
40: #endif
1.10 ! millert 41: static char rcsid[] = "$OpenBSD: klogin.c,v 1.9 1999/11/24 16:08:18 art Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: #ifdef KERBEROS
45: #include <sys/param.h>
46: #include <sys/syslog.h>
1.5 provos 47: #include <des.h>
1.1 deraadt 48: #include <kerberosIV/krb.h>
1.6 art 49: #include <kerberosIV/kafs.h>
1.1 deraadt 50:
51: #include <err.h>
52: #include <netdb.h>
53: #include <pwd.h>
54: #include <stdio.h>
55: #include <stdlib.h>
56: #include <string.h>
57: #include <unistd.h>
58:
59: #include <sys/types.h>
60: #include <sys/stat.h>
61: #include <fcntl.h>
62:
63: #define INITIAL_TICKET "krbtgt"
64: #define VERIFY_SERVICE "rcmd"
65:
66: extern int notickets;
67: extern char *krbtkfile_env;
68: extern char *tty;
69:
70: static char tkt_location[MAXPATHLEN]; /* a pointer to this is returned... */
71:
72: /*
73: * Attempt to log the user in using Kerberos authentication
74: *
75: * return 0 on success (will be logged in)
76: * 1 if Kerberos failed (try local password in login)
77: */
78: int
79: klogin(pw, instance, localhost, password)
80: struct passwd *pw;
81: char *instance, *localhost, *password;
82: {
83: int kerror;
84: AUTH_DAT authdata;
85: KTEXT_ST ticket;
86: struct hostent *hp;
87: unsigned long faddr;
88: char realm[REALM_SZ], savehost[MAXHOSTNAMELEN];
89: char *krb_get_phost();
90:
91: #ifdef SKEY
92: /*
93: * We don't do s/key challenge and Kerberos at the same time
94: */
95: if (strcasecmp(password, "s/key") == 0) {
96: return (1);
97: }
98: #endif
99:
100: /*
1.4 dm 101: * Root logins don't use Kerberos (or at least shouldn't be
102: * sending kerberos passwords around in cleartext), so don't
103: * allow any root logins here (keeping in mind that we only
104: * get here with a password).
105: *
1.1 deraadt 106: * If we have a realm, try getting a ticket-granting ticket
107: * and using it to authenticate. Otherwise, return
108: * failure so that we can try the normal passwd file
109: * for a password. If that's ok, log the user in
110: * without issuing any tickets.
111: */
1.4 dm 112: if (pw->pw_uid == 0 || krb_get_lrealm(realm, 0) != KSUCCESS)
1.1 deraadt 113: return (1);
114:
115: /*
116: * get TGT for local realm
117: * tickets are stored in a file named TKT_ROOT plus uid plus tty
118: * except for user.root tickets.
119: */
120:
121: if (strcmp(instance, "root") != 0)
1.8 art 122: snprintf(tkt_location, sizeof(tkt_location), "%s%d.%s",
123: TKT_ROOT, pw->pw_uid, tty);
1.1 deraadt 124: else
1.8 art 125: snprintf(tkt_location, sizeof(tkt_location), "%s_root_%d.%s",
126: TKT_ROOT, pw->pw_uid, tty);
1.1 deraadt 127: krbtkfile_env = tkt_location;
128: (void)krb_set_tkt_string(tkt_location);
129:
130: /*
131: * Set real as well as effective ID to 0 for the moment,
132: * to make the kerberos library do the right thing.
133: */
134: if (setuid(0) < 0) {
135: warnx("setuid");
136: return (1);
137: }
138: kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
139: realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password);
140: /*
141: * If we got a TGT, get a local "rcmd" ticket and check it so as to
142: * ensure that we are not talking to a bogus Kerberos server.
143: *
144: * There are 2 cases where we still allow a login:
145: * 1: the VERIFY_SERVICE doesn't exist in the KDC
146: * 2: local host has no srvtab, as (hopefully) indicated by a
147: * return value of RD_AP_UNDEC from krb_rd_req().
148: */
149: if (kerror != INTK_OK) {
150: if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
151: syslog(LOG_ERR, "Kerberos intkt error: %s",
152: krb_err_txt[kerror]);
153: dest_tkt();
154: }
155: return (1);
156: }
157:
158: if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
159: syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
160:
1.10 ! millert 161: (void)strlcpy(savehost, krb_get_phost(localhost), sizeof(savehost));
1.1 deraadt 162:
163: /*
164: * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
165: * still allow login with tickets, but log the error condition.
166: */
167:
168: kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
169: if (kerror == KDC_PR_UNKNOWN) {
170: syslog(LOG_NOTICE,
171: "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
172: krb_err_txt[kerror], VERIFY_SERVICE, savehost);
173: notickets = 0;
174: /*
175: * but for security, don't allow root instances in under
176: * this condition!
177: */
178: if (strcmp(instance, "root") == 0) {
1.7 deraadt 179: syslog(LOG_ERR, "Kerberos %s root instance login refused",
1.1 deraadt 180: pw->pw_name);
181: dest_tkt();
182: return (1);
183: }
1.4 dm 184: /* Otherwise, leave ticket around, but make sure
185: * password matches the Unix password. */
186: return (1);
1.1 deraadt 187: }
188:
189: if (kerror != KSUCCESS) {
190: warnx("unable to use TGT: (%s)", krb_err_txt[kerror]);
191: syslog(LOG_NOTICE, "unable to use TGT: (%s)",
192: krb_err_txt[kerror]);
193: dest_tkt();
194: return (1);
195: }
196:
197: if (!(hp = gethostbyname(localhost))) {
198: syslog(LOG_ERR, "couldn't get local host address");
199: dest_tkt();
200: return (1);
201: }
202:
203: memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
204:
205: kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
206: &authdata, "");
207:
208: if (kerror == KSUCCESS) {
209: notickets = 0;
210: return (0);
211: }
212:
213: /* undecipherable: probably didn't have a srvtab on the local host */
1.2 deraadt 214: if (kerror == RD_AP_UNDEC) {
1.7 deraadt 215: syslog(LOG_NOTICE, "krb_rd_req: (%s)", krb_err_txt[kerror]);
1.1 deraadt 216: dest_tkt();
217: return (1);
218: }
219: /* failed for some other reason */
220: warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
221: krb_err_txt[kerror]);
222: syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
223: krb_err_txt[kerror]);
224: dest_tkt();
225: return (1);
226: }
227:
228: void
1.6 art 229: kgettokens(homedir)
230: char *homedir;
231: {
232: /* buy AFS-tokens for homedir */
233: if (k_hasafs()) {
234: char cell[128];
235: k_setpag();
236: if (k_afs_cell_of_file(homedir,
237: cell, sizeof(cell)) == 0)
238: krb_afslog(cell, 0);
239: krb_afslog(0, 0);
240: }
241: }
242:
243: void
1.1 deraadt 244: kdestroy()
245: {
246: char *file = krbtkfile_env;
247: int i, fd;
248: extern int errno;
249: struct stat statb;
250: char buf[BUFSIZ];
1.6 art 251:
252: if (k_hasafs())
253: k_unlog();
1.1 deraadt 254:
255: if (krbtkfile_env == NULL)
256: return;
257:
258: errno = 0;
259: if (lstat(file, &statb) < 0)
260: goto out;
261:
262: if (!(statb.st_mode & S_IFREG)
263: #ifdef notdef
264: || statb.st_mode & 077
265: #endif
266: )
267: goto out;
268:
269: if ((fd = open(file, O_RDWR, 0)) < 0)
270: goto out;
271:
1.8 art 272: bzero(buf, sizeof(buf));
1.1 deraadt 273:
274: for (i = 0; i < statb.st_size; i += BUFSIZ)
275: if (write(fd, buf, BUFSIZ) != BUFSIZ) {
276: (void) fsync(fd);
277: (void) close(fd);
278: goto out;
279: }
280:
281: (void) fsync(fd);
282: (void) close(fd);
283:
284: (void) unlink(file);
285:
286: out:
287: if (errno != 0) return;
1.9 art 288:
1.1 deraadt 289: return;
290: }
291: #endif