Annotation of src/usr.bin/login/klogin.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD: klogin.c,v 1.7 1996/05/21 22:07:04 mrg 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.3 ! deraadt 41: static char rcsid[] = "$OpenBSD: klogin.c,v 1.7 1996/05/21 22:07:04 mrg Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: #ifdef KERBEROS
45: #include <sys/param.h>
46: #include <sys/syslog.h>
47: #include <kerberosIV/des.h>
48: #include <kerberosIV/krb.h>
49:
50: #include <err.h>
51: #include <netdb.h>
52: #include <pwd.h>
53: #include <stdio.h>
54: #include <stdlib.h>
55: #include <string.h>
56: #include <unistd.h>
57:
58: #include <sys/types.h>
59: #include <sys/stat.h>
60: #include <fcntl.h>
61:
62: #define INITIAL_TICKET "krbtgt"
63: #define VERIFY_SERVICE "rcmd"
64:
65: extern int notickets;
66: extern char *krbtkfile_env;
67: extern char *tty;
68:
69: static char tkt_location[MAXPATHLEN]; /* a pointer to this is returned... */
70:
71: /*
72: * Attempt to log the user in using Kerberos authentication
73: *
74: * return 0 on success (will be logged in)
75: * 1 if Kerberos failed (try local password in login)
76: */
77: int
78: klogin(pw, instance, localhost, password)
79: struct passwd *pw;
80: char *instance, *localhost, *password;
81: {
82: int kerror;
83: AUTH_DAT authdata;
84: KTEXT_ST ticket;
85: struct hostent *hp;
86: unsigned long faddr;
87: char realm[REALM_SZ], savehost[MAXHOSTNAMELEN];
88: char *krb_get_phost();
89:
90: #ifdef SKEY
91: /*
92: * We don't do s/key challenge and Kerberos at the same time
93: */
94: if (strcasecmp(password, "s/key") == 0) {
95: return (1);
96: }
97: #endif
98:
99: /*
100: * Root logins don't use Kerberos.
101: * If we have a realm, try getting a ticket-granting ticket
102: * and using it to authenticate. Otherwise, return
103: * failure so that we can try the normal passwd file
104: * for a password. If that's ok, log the user in
105: * without issuing any tickets.
106: */
107: if (strcmp(pw->pw_name, "root") == 0 ||
108: krb_get_lrealm(realm, 0) != KSUCCESS)
109: return (1);
110:
111: /*
112: * get TGT for local realm
113: * tickets are stored in a file named TKT_ROOT plus uid plus tty
114: * except for user.root tickets.
115: */
116:
117: if (strcmp(instance, "root") != 0)
118: (void)sprintf(tkt_location, "%s%d.%s",
119: TKT_ROOT, pw->pw_uid, tty);
120: else
121: (void)sprintf(tkt_location, "%s_root_%d.%s",
122: TKT_ROOT, pw->pw_uid, tty);
123: krbtkfile_env = tkt_location;
124: (void)krb_set_tkt_string(tkt_location);
125:
126: /*
127: * Set real as well as effective ID to 0 for the moment,
128: * to make the kerberos library do the right thing.
129: */
130: if (setuid(0) < 0) {
131: warnx("setuid");
132: return (1);
133: }
134: kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
135: realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password);
136: /*
137: * If we got a TGT, get a local "rcmd" ticket and check it so as to
138: * ensure that we are not talking to a bogus Kerberos server.
139: *
140: * There are 2 cases where we still allow a login:
141: * 1: the VERIFY_SERVICE doesn't exist in the KDC
142: * 2: local host has no srvtab, as (hopefully) indicated by a
143: * return value of RD_AP_UNDEC from krb_rd_req().
144: */
145: if (kerror != INTK_OK) {
146: if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
147: syslog(LOG_ERR, "Kerberos intkt error: %s",
148: krb_err_txt[kerror]);
149: dest_tkt();
150: }
151: return (1);
152: }
153:
154: if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
155: syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
156:
157: (void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
158: savehost[sizeof(savehost)-1] = NULL;
159:
160: /*
161: * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
162: * still allow login with tickets, but log the error condition.
163: */
164:
165: kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
166: if (kerror == KDC_PR_UNKNOWN) {
167: syslog(LOG_NOTICE,
168: "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
169: krb_err_txt[kerror], VERIFY_SERVICE, savehost);
170: notickets = 0;
171: /*
172: * but for security, don't allow root instances in under
173: * this condition!
174: */
175: if (strcmp(instance, "root") == 0) {
176: syslog(LOG_ERR, "Kerberos %s root instance login refused\n",
177: pw->pw_name);
178: dest_tkt();
179: return (1);
180: }
181: return (0);
182: }
183:
184: if (kerror != KSUCCESS) {
185: warnx("unable to use TGT: (%s)", krb_err_txt[kerror]);
186: syslog(LOG_NOTICE, "unable to use TGT: (%s)",
187: krb_err_txt[kerror]);
188: dest_tkt();
189: return (1);
190: }
191:
192: if (!(hp = gethostbyname(localhost))) {
193: syslog(LOG_ERR, "couldn't get local host address");
194: dest_tkt();
195: return (1);
196: }
197:
198: memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
199:
200: kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
201: &authdata, "");
202:
203: if (kerror == KSUCCESS) {
204: notickets = 0;
205: return (0);
206: }
207:
208: /* undecipherable: probably didn't have a srvtab on the local host */
1.2 deraadt 209: if (kerror == RD_AP_UNDEC) {
1.1 deraadt 210: syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]);
211: dest_tkt();
212: return (1);
213: }
214: /* failed for some other reason */
215: warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
216: krb_err_txt[kerror]);
217: syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
218: krb_err_txt[kerror]);
219: dest_tkt();
220: return (1);
221: }
222:
223: void
224: kdestroy()
225: {
226: char *file = krbtkfile_env;
227: int i, fd;
228: extern int errno;
229: struct stat statb;
230: char buf[BUFSIZ];
231: #ifdef TKT_SHMEM
232: char shmidname[MAXPATHLEN];
233: #endif /* TKT_SHMEM */
234:
235: if (krbtkfile_env == NULL)
236: return;
237:
238: errno = 0;
239: if (lstat(file, &statb) < 0)
240: goto out;
241:
242: if (!(statb.st_mode & S_IFREG)
243: #ifdef notdef
244: || statb.st_mode & 077
245: #endif
246: )
247: goto out;
248:
249: if ((fd = open(file, O_RDWR, 0)) < 0)
250: goto out;
251:
252: bzero(buf, BUFSIZ);
253:
254: for (i = 0; i < statb.st_size; i += BUFSIZ)
255: if (write(fd, buf, BUFSIZ) != BUFSIZ) {
256: (void) fsync(fd);
257: (void) close(fd);
258: goto out;
259: }
260:
261: (void) fsync(fd);
262: (void) close(fd);
263:
264: (void) unlink(file);
265:
266: out:
267: if (errno != 0) return;
268: #ifdef TKT_SHMEM
269: /*
270: * handle the shared memory case
271: */
272: (void) strcpy(shmidname, file);
273: (void) strcat(shmidname, ".shm");
274: if (krb_shm_dest(shmidname) != KSUCCESS)
275: return;
276: #endif /* TKT_SHMEM */
277: return;
278: }
279: #endif