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
33 * $FreeBSD: src/lib/libpam/modules/pam_kerberosIV/klogin.c,v 1.11 2000/02/24 22:24:37 markm Exp $
37 static const char sccsid[] = "@(#)klogin.c 8.3 (Berkeley) 4/2/94";
41 #include <sys/param.h>
42 #include <sys/syslog.h>
43 #include <openssl/des.h>
54 #define INITIAL_TICKET "krbtgt"
55 #define VERIFY_SERVICE "rcmd"
58 extern char *krbtkfile_env;
61 * Attempt to log the user in using Kerberos authentication
63 * return 0 on success (will be logged in)
64 * 1 if Kerberos failed (try local password in login)
67 klogin(pw, instance, localhost, password)
69 char *instance, *localhost, *password;
72 char realm[REALM_SZ], savehost[MAXHOSTNAMELEN];
73 char tkt_location[MAXPATHLEN];
74 char *krb_get_phost();
75 extern int noticketsdontcomplain;
77 #ifdef KLOGIN_PARANOID
83 noticketsdontcomplain = 0; /* enable warning message */
87 * Root logins don't use Kerberos.
88 * If we have a realm, try getting a ticket-granting ticket
89 * and using it to authenticate. Otherwise, return
90 * failure so that we can try the normal passwd file
91 * for a password. If that's ok, log the user in
92 * without issuing any tickets.
94 if (strcmp(pw->pw_name, "root") == 0 ||
95 krb_get_lrealm(realm, 0) != KSUCCESS)
98 noticketsdontcomplain = 0; /* enable warning message */
101 * get TGT for local realm
102 * tickets are stored in a file named TKT_ROOT plus uid
103 * except for user.root tickets.
106 if (strcmp(instance, "root") != 0)
107 (void)sprintf(tkt_location, "%s%d", TKT_ROOT, pw->pw_uid);
109 (void)sprintf(tkt_location, "%s_root_%d", TKT_ROOT, pw->pw_uid);
110 krbtkfile_env = tkt_location;
112 (void)krb_set_tkt_string(tkt_location);
115 * Set real as well as effective ID to 0 for the moment,
116 * to make the kerberos library do the right thing.
122 kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
123 realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password);
126 * If we got a TGT, get a local "rcmd" ticket and check it so as to
127 * ensure that we are not talking to a bogus Kerberos server.
129 * There are 2 cases where we still allow a login:
130 * 1: the VERIFY_SERVICE doesn't exist in the KDC
131 * 2: local host has no srvtab, as (hopefully) indicated by a
132 * return value of RD_AP_UNDEC from krb_rd_req().
134 if (kerror != INTK_OK) {
135 if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
136 syslog(LOG_ERR, "Kerberos intkt error: %s",
137 krb_err_txt[kerror]);
143 if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
144 syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
146 (void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
147 savehost[sizeof(savehost)-1] = NULL;
149 #ifdef KLOGIN_PARANOID
151 * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
152 * still allow login with tickets, but log the error condition.
155 kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
156 if (kerror == KDC_PR_UNKNOWN) {
158 "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
159 krb_err_txt[kerror], VERIFY_SERVICE, savehost);
164 if (kerror != KSUCCESS) {
165 warnx("unable to use TGT: (%s)", krb_err_txt[kerror]);
166 syslog(LOG_NOTICE, "unable to use TGT: (%s)",
167 krb_err_txt[kerror]);
172 if (!(hp = gethostbyname(localhost))) {
173 syslog(LOG_ERR, "couldn't get local host address");
178 memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
180 kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
183 if (kerror == KSUCCESS) {
188 /* undecipherable: probably didn't have a srvtab on the local host */
189 if (kerror == RD_AP_UNDEC) {
190 syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]);
194 /* failed for some other reason */
195 warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
196 krb_err_txt[kerror]);
197 syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
198 krb_err_txt[kerror]);