Annotation of src/usr.bin/login/klogin.c, Revision 1.10.2.1
1.10.2.1! hin 1: /* $OpenBSD: klogin.c,v 1.11 2000/12/02 22:44:36 hin 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.2.1! hin 41: static char rcsid[] = "$OpenBSD: klogin.c,v 1.11 2000/12/02 22:44:36 hin 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: {
1.10.2.1! hin 83: int kerror, fd;
1.1 deraadt 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:
1.10.2.1! hin 100: /* If no srvtab file exists, fail immediatly. This will make
! 101: * login _much_ quicker on systems with sporadical contact with
! 102: * the outside world.
! 103: * We should really change the semantics for enabling kerberos.
! 104: */
! 105: if((fd = open(KEYFILE, O_RDONLY, 0)) < 0)
! 106: return 1;
! 107: close(fd);
! 108:
1.1 deraadt 109: /*
1.4 dm 110: * Root logins don't use Kerberos (or at least shouldn't be
111: * sending kerberos passwords around in cleartext), so don't
112: * allow any root logins here (keeping in mind that we only
113: * get here with a password).
114: *
1.1 deraadt 115: * If we have a realm, try getting a ticket-granting ticket
116: * and using it to authenticate. Otherwise, return
117: * failure so that we can try the normal passwd file
118: * for a password. If that's ok, log the user in
119: * without issuing any tickets.
120: */
1.4 dm 121: if (pw->pw_uid == 0 || krb_get_lrealm(realm, 0) != KSUCCESS)
1.1 deraadt 122: return (1);
123:
124: /*
125: * get TGT for local realm
126: * tickets are stored in a file named TKT_ROOT plus uid plus tty
127: * except for user.root tickets.
128: */
129:
130: if (strcmp(instance, "root") != 0)
1.10.2.1! hin 131: snprintf(tkt_location, sizeof(tkt_location), "%s%d",
! 132: TKT_ROOT, pw->pw_uid);
1.1 deraadt 133: else
1.8 art 134: snprintf(tkt_location, sizeof(tkt_location), "%s_root_%d.%s",
135: TKT_ROOT, pw->pw_uid, tty);
1.1 deraadt 136: krbtkfile_env = tkt_location;
137: (void)krb_set_tkt_string(tkt_location);
138:
139: /*
140: * Set real as well as effective ID to 0 for the moment,
141: * to make the kerberos library do the right thing.
142: */
143: if (setuid(0) < 0) {
144: warnx("setuid");
145: return (1);
146: }
147: kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
148: realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password);
149: /*
150: * If we got a TGT, get a local "rcmd" ticket and check it so as to
151: * ensure that we are not talking to a bogus Kerberos server.
152: *
153: * There are 2 cases where we still allow a login:
154: * 1: the VERIFY_SERVICE doesn't exist in the KDC
155: * 2: local host has no srvtab, as (hopefully) indicated by a
156: * return value of RD_AP_UNDEC from krb_rd_req().
157: */
158: if (kerror != INTK_OK) {
159: if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
160: syslog(LOG_ERR, "Kerberos intkt error: %s",
161: krb_err_txt[kerror]);
162: dest_tkt();
163: }
164: return (1);
165: }
166:
1.10.2.1! hin 167: /*
! 168: * Set the owner of the ticket file to root but bail if someone
! 169: * has nefariously swapped a link in place of the file.
! 170: */
! 171: fd = open(TKT_FILE, O_RDWR|O_NOFOLLOW, 0);
! 172: if (fd == -1) {
! 173: syslog(LOG_ERR, "unable to open ticket file: %m");
! 174: dest_tkt();
! 175: return (1);
! 176: }
! 177: if (fchown(fd, pw->pw_uid, pw->pw_gid) < 0)
! 178: syslog(LOG_ERR, "fchown tkfile (%s): %m", TKT_FILE);
! 179: close(fd);
1.1 deraadt 180:
1.10 millert 181: (void)strlcpy(savehost, krb_get_phost(localhost), sizeof(savehost));
1.1 deraadt 182:
183: /*
184: * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
185: * still allow login with tickets, but log the error condition.
186: */
187:
188: kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
189: if (kerror == KDC_PR_UNKNOWN) {
190: syslog(LOG_NOTICE,
191: "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
192: krb_err_txt[kerror], VERIFY_SERVICE, savehost);
193: notickets = 0;
194: /*
195: * but for security, don't allow root instances in under
196: * this condition!
197: */
198: if (strcmp(instance, "root") == 0) {
1.7 deraadt 199: syslog(LOG_ERR, "Kerberos %s root instance login refused",
1.1 deraadt 200: pw->pw_name);
201: dest_tkt();
202: return (1);
203: }
1.4 dm 204: /* Otherwise, leave ticket around, but make sure
205: * password matches the Unix password. */
206: return (1);
1.1 deraadt 207: }
208:
209: if (kerror != KSUCCESS) {
210: warnx("unable to use TGT: (%s)", krb_err_txt[kerror]);
211: syslog(LOG_NOTICE, "unable to use TGT: (%s)",
212: krb_err_txt[kerror]);
213: dest_tkt();
214: return (1);
215: }
216:
217: if (!(hp = gethostbyname(localhost))) {
218: syslog(LOG_ERR, "couldn't get local host address");
219: dest_tkt();
220: return (1);
221: }
222:
223: memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
224:
225: kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
226: &authdata, "");
227:
228: if (kerror == KSUCCESS) {
229: notickets = 0;
230: return (0);
231: }
232:
233: /* undecipherable: probably didn't have a srvtab on the local host */
1.2 deraadt 234: if (kerror == RD_AP_UNDEC) {
1.7 deraadt 235: syslog(LOG_NOTICE, "krb_rd_req: (%s)", krb_err_txt[kerror]);
1.1 deraadt 236: dest_tkt();
237: return (1);
238: }
239: /* failed for some other reason */
240: warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
241: krb_err_txt[kerror]);
242: syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
243: krb_err_txt[kerror]);
244: dest_tkt();
245: return (1);
246: }
247:
248: void
1.6 art 249: kgettokens(homedir)
250: char *homedir;
251: {
252: /* buy AFS-tokens for homedir */
253: if (k_hasafs()) {
254: char cell[128];
255: k_setpag();
256: if (k_afs_cell_of_file(homedir,
257: cell, sizeof(cell)) == 0)
258: krb_afslog(cell, 0);
259: krb_afslog(0, 0);
260: }
261: }
262:
263: void
1.1 deraadt 264: kdestroy()
265: {
266: char *file = krbtkfile_env;
267: int i, fd;
268: extern int errno;
269: struct stat statb;
270: char buf[BUFSIZ];
1.6 art 271:
272: if (k_hasafs())
273: k_unlog();
1.1 deraadt 274:
275: if (krbtkfile_env == NULL)
276: return;
277:
278: errno = 0;
279: if (lstat(file, &statb) < 0)
280: goto out;
281:
282: if (!(statb.st_mode & S_IFREG)
283: #ifdef notdef
284: || statb.st_mode & 077
285: #endif
286: )
287: goto out;
288:
289: if ((fd = open(file, O_RDWR, 0)) < 0)
290: goto out;
291:
1.8 art 292: bzero(buf, sizeof(buf));
1.1 deraadt 293:
294: for (i = 0; i < statb.st_size; i += BUFSIZ)
295: if (write(fd, buf, BUFSIZ) != BUFSIZ) {
296: (void) fsync(fd);
297: (void) close(fd);
298: goto out;
299: }
300:
301: (void) fsync(fd);
302: (void) close(fd);
303:
304: (void) unlink(file);
305:
306: out:
307: if (errno != 0) return;
1.9 art 308:
1.1 deraadt 309: return;
310: }
311: #endif