2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$Id: klogin.c,v 1.27 1999/10/04 16:11:48 bg Exp $");
40 #define VERIFY_SERVICE "rcmd"
43 extern char *krbtkfile_env;
45 static char tkt_location[MaxPathLen];
48 multiple_get_tkt(char *name,
60 /* First try to verify against the supplied realm. */
61 ret = krb_get_pw_in_tkt(name, instance, realm, service, realm, life,
66 /* Verify all local realms, except the supplied realm. */
67 for (n = 1; krb_get_lrealm(rlm, n) == KSUCCESS; n++)
68 if (strcmp(rlm, realm) != 0) {
69 ret = krb_get_pw_in_tkt(name, instance, rlm,service, rlm,life, password);
78 * Attempt to log the user in using Kerberos authentication
80 * return 0 on success (will be logged in)
81 * 1 if Kerberos failed (try local password in login)
84 klogin(struct passwd *pw, char *instance, char *localhost, char *password)
91 char realm[REALM_SZ], savehost[MaxHostNameLen];
92 extern int noticketsdontcomplain;
94 #ifdef KLOGIN_PARANOID
95 noticketsdontcomplain = 0; /* enable warning message */
98 * Root logins don't use Kerberos.
99 * If we have a realm, try getting a ticket-granting ticket
100 * and using it to authenticate. Otherwise, return
101 * failure so that we can try the normal passwd file
102 * for a password. If that's ok, log the user in
103 * without issuing any tickets.
105 if (strcmp(pw->pw_name, "root") == 0 ||
106 krb_get_lrealm(realm, 1) != KSUCCESS)
109 noticketsdontcomplain = 0; /* enable warning message */
112 * get TGT for local realm
113 * tickets are stored in a file named TKT_ROOT plus uid
114 * except for user.root tickets.
117 if (strcmp(instance, "root") != 0)
118 snprintf(tkt_location, sizeof(tkt_location),
120 TKT_ROOT, (unsigned)pw->pw_uid, (unsigned)getpid());
122 snprintf(tkt_location, sizeof(tkt_location),
123 "%s_root_%d", TKT_ROOT,
124 (unsigned)pw->pw_uid);
126 krbtkfile_env = tkt_location;
127 krb_set_tkt_string(tkt_location);
130 * Set real as well as effective ID to 0 for the moment,
131 * to make the kerberos library do the right thing.
141 kerror = multiple_get_tkt(pw->pw_name,
144 KRB_TICKET_GRANTING_TICKET,
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.
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().
158 if (kerror != INTK_OK) {
159 if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
160 syslog(LOG_ERR, "Kerberos intkt error: %s",
161 krb_get_err_text(kerror));
167 if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
168 syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
170 strlcpy(savehost, krb_get_phost(localhost), sizeof(savehost));
172 #ifdef KLOGIN_PARANOID
174 * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
175 * don't allow kerberos login, also log the error condition.
178 kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
179 if (kerror == KDC_PR_UNKNOWN) {
181 "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
182 krb_get_err_text(kerror), VERIFY_SERVICE, savehost);
187 if (kerror != KSUCCESS) {
188 warnx("unable to use TGT: (%s)", krb_get_err_text(kerror));
189 syslog(LOG_NOTICE, "unable to use TGT: (%s)",
190 krb_get_err_text(kerror));
195 if (!(hp = gethostbyname(localhost))) {
196 syslog(LOG_ERR, "couldn't get local host address");
201 memcpy(&faddr, hp->h_addr, sizeof(faddr));
203 kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
206 if (kerror == KSUCCESS) {
211 /* undecipherable: probably didn't have a srvtab on the local host */
212 if (kerror == RD_AP_UNDEC) {
213 syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_get_err_text(kerror));
217 /* failed for some other reason */
218 warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
219 krb_get_err_text(kerror));
220 syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
221 krb_get_err_text(kerror));