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.20 1997/05/02 14:27:42 assar Exp $");
40 #define VERIFY_SERVICE "rcmd"
43 extern char *krbtkfile_env;
45 static char tkt_location[MaxPathLen];
48 * Attempt to log the user in using Kerberos authentication
50 * return 0 on success (will be logged in)
51 * 1 if Kerberos failed (try local password in login)
54 klogin(struct passwd *pw, char *instance, char *localhost, char *password)
61 char realm[REALM_SZ], savehost[MaxHostNameLen];
62 extern int noticketsdontcomplain;
64 #ifdef KLOGIN_PARANOID
65 noticketsdontcomplain = 0; /* enable warning message */
68 * Root logins don't use Kerberos.
69 * If we have a realm, try getting a ticket-granting ticket
70 * and using it to authenticate. Otherwise, return
71 * failure so that we can try the normal passwd file
72 * for a password. If that's ok, log the user in
73 * without issuing any tickets.
75 if (strcmp(pw->pw_name, "root") == 0 ||
76 krb_get_lrealm(realm, 0) != KSUCCESS)
79 noticketsdontcomplain = 0; /* enable warning message */
82 * get TGT for local realm
83 * tickets are stored in a file named TKT_ROOT plus uid
84 * except for user.root tickets.
87 if (strcmp(instance, "root") != 0)
88 snprintf(tkt_location, sizeof(tkt_location),
90 TKT_ROOT, (unsigned)pw->pw_uid, (unsigned)getpid());
92 snprintf(tkt_location, sizeof(tkt_location),
93 "%s_root_%d", TKT_ROOT,
94 (unsigned)pw->pw_uid);
96 krbtkfile_env = tkt_location;
97 krb_set_tkt_string(tkt_location);
99 kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
100 realm, KRB_TICKET_GRANTING_TICKET, realm,
101 DEFAULT_TKT_LIFE, password);
104 * If we got a TGT, get a local "rcmd" ticket and check it so as to
105 * ensure that we are not talking to a bogus Kerberos server.
107 * There are 2 cases where we still allow a login:
108 * 1: the VERIFY_SERVICE doesn't exist in the KDC
109 * 2: local host has no srvtab, as (hopefully) indicated by a
110 * return value of RD_AP_UNDEC from krb_rd_req().
112 if (kerror != INTK_OK) {
113 if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
114 syslog(LOG_ERR, "Kerberos intkt error: %s",
115 krb_get_err_text(kerror));
121 if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
122 syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
124 strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
125 savehost[sizeof(savehost)-1] = '\0';
127 #ifdef KLOGIN_PARANOID
129 * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
130 * don't allow kerberos login, also log the error condition.
133 kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
134 if (kerror == KDC_PR_UNKNOWN) {
136 "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
137 krb_get_err_text(kerror), VERIFY_SERVICE, savehost);
142 if (kerror != KSUCCESS) {
143 warnx("unable to use TGT: (%s)", krb_get_err_text(kerror));
144 syslog(LOG_NOTICE, "unable to use TGT: (%s)",
145 krb_get_err_text(kerror));
150 if (!(hp = gethostbyname(localhost))) {
151 syslog(LOG_ERR, "couldn't get local host address");
156 memcpy(&faddr, hp->h_addr, sizeof(faddr));
158 kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
161 if (kerror == KSUCCESS) {
166 /* undecipherable: probably didn't have a srvtab on the local host */
167 if (kerror == RD_AP_UNDEC) {
168 syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_get_err_text(kerror));
172 /* failed for some other reason */
173 warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
174 krb_get_err_text(kerror));
175 syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
176 krb_get_err_text(kerror));