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.24 1999/03/15 13:34:12 bg Exp $");
40 #define VERIFY_SERVICE "rcmd"
43 extern char *krbtkfile_env;
45 static char tkt_location[MaxPathLen];
48 multiple_get_tkt(char *name,
58 #define ERICSSON_COMPAT 1
59 #ifdef ERICSSON_COMPAT
62 f = fopen("/etc/krb.localrealms", "r");
64 while (fgets(rlm, sizeof(rlm), f) != NULL) {
65 if (rlm[strlen(rlm) - 1] == '\n')
66 rlm[strlen(rlm) - 1] = '\0';
68 if (krb_get_pw_in_tkt(name,
74 password) == KSUCCESS) {
79 return krb_get_pw_in_tkt(name,
88 /* First try to verify against the supplied realm. */
89 if (krb_get_pw_in_tkt(name, instance, realm, service, realm, life, password)
93 /* Verify all local realms, except the supplied realm. */
94 for (n = 1; krb_get_lrealm(rlm, n) == KSUCCESS; n++)
95 if (strcmp(rlm, realm) != 0)
96 if (krb_get_pw_in_tkt(name, instance, rlm,service, realm, life, password)
105 * Attempt to log the user in using Kerberos authentication
107 * return 0 on success (will be logged in)
108 * 1 if Kerberos failed (try local password in login)
111 klogin(struct passwd *pw, char *instance, char *localhost, char *password)
118 char realm[REALM_SZ], savehost[MaxHostNameLen];
119 extern int noticketsdontcomplain;
121 #ifdef KLOGIN_PARANOID
122 noticketsdontcomplain = 0; /* enable warning message */
125 * Root logins don't use Kerberos.
126 * If we have a realm, try getting a ticket-granting ticket
127 * and using it to authenticate. Otherwise, return
128 * failure so that we can try the normal passwd file
129 * for a password. If that's ok, log the user in
130 * without issuing any tickets.
132 if (strcmp(pw->pw_name, "root") == 0 ||
133 krb_get_lrealm(realm, 1) != KSUCCESS)
136 noticketsdontcomplain = 0; /* enable warning message */
139 * get TGT for local realm
140 * tickets are stored in a file named TKT_ROOT plus uid
141 * except for user.root tickets.
144 if (strcmp(instance, "root") != 0)
145 snprintf(tkt_location, sizeof(tkt_location),
147 TKT_ROOT, (unsigned)pw->pw_uid, (unsigned)getpid());
149 snprintf(tkt_location, sizeof(tkt_location),
150 "%s_root_%d", TKT_ROOT,
151 (unsigned)pw->pw_uid);
153 krbtkfile_env = tkt_location;
154 krb_set_tkt_string(tkt_location);
157 * Set real as well as effective ID to 0 for the moment,
158 * to make the kerberos library do the right thing.
168 kerror = multiple_get_tkt(pw->pw_name,
171 KRB_TICKET_GRANTING_TICKET,
177 * If we got a TGT, get a local "rcmd" ticket and check it so as to
178 * ensure that we are not talking to a bogus Kerberos server.
180 * There are 2 cases where we still allow a login:
181 * 1: the VERIFY_SERVICE doesn't exist in the KDC
182 * 2: local host has no srvtab, as (hopefully) indicated by a
183 * return value of RD_AP_UNDEC from krb_rd_req().
185 if (kerror != INTK_OK) {
186 if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
187 syslog(LOG_ERR, "Kerberos intkt error: %s",
188 krb_get_err_text(kerror));
194 if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
195 syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
197 strcpy_truncate(savehost, krb_get_phost(localhost), sizeof(savehost));
199 #ifdef KLOGIN_PARANOID
201 * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
202 * don't allow kerberos login, also log the error condition.
205 kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
206 if (kerror == KDC_PR_UNKNOWN) {
208 "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
209 krb_get_err_text(kerror), VERIFY_SERVICE, savehost);
214 if (kerror != KSUCCESS) {
215 warnx("unable to use TGT: (%s)", krb_get_err_text(kerror));
216 syslog(LOG_NOTICE, "unable to use TGT: (%s)",
217 krb_get_err_text(kerror));
222 if (!(hp = gethostbyname(localhost))) {
223 syslog(LOG_ERR, "couldn't get local host address");
228 memcpy(&faddr, hp->h_addr, sizeof(faddr));
230 kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
233 if (kerror == KSUCCESS) {
238 /* undecipherable: probably didn't have a srvtab on the local host */
239 if (kerror == RD_AP_UNDEC) {
240 syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_get_err_text(kerror));
244 /* failed for some other reason */
245 warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
246 krb_get_err_text(kerror));
247 syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
248 krb_get_err_text(kerror));