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