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 $
34 * $DragonFly: src/lib/libpam/modules/pam_kerberosIV/Attic/klogin.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
36 * @(#)klogin.c 8.3 (Berkeley) 4/2/94
40 #include <sys/param.h>
41 #include <sys/syslog.h>
42 #include <openssl/des.h>
53 #define INITIAL_TICKET "krbtgt"
54 #define VERIFY_SERVICE "rcmd"
57 extern char *krbtkfile_env;
60 * Attempt to log the user in using Kerberos authentication
62 * return 0 on success (will be logged in)
63 * 1 if Kerberos failed (try local password in login)
66 klogin(pw, instance, localhost, password)
68 char *instance, *localhost, *password;
71 char realm[REALM_SZ], savehost[MAXHOSTNAMELEN];
72 char tkt_location[MAXPATHLEN];
73 char *krb_get_phost();
74 extern int noticketsdontcomplain;
76 #ifdef KLOGIN_PARANOID
82 noticketsdontcomplain = 0; /* enable warning message */
86 * Root logins don't use Kerberos.
87 * If we have a realm, try getting a ticket-granting ticket
88 * and using it to authenticate. Otherwise, return
89 * failure so that we can try the normal passwd file
90 * for a password. If that's ok, log the user in
91 * without issuing any tickets.
93 if (strcmp(pw->pw_name, "root") == 0 ||
94 krb_get_lrealm(realm, 0) != KSUCCESS)
97 noticketsdontcomplain = 0; /* enable warning message */
100 * get TGT for local realm
101 * tickets are stored in a file named TKT_ROOT plus uid
102 * except for user.root tickets.
105 if (strcmp(instance, "root") != 0)
106 (void)sprintf(tkt_location, "%s%d", TKT_ROOT, pw->pw_uid);
108 (void)sprintf(tkt_location, "%s_root_%d", TKT_ROOT, pw->pw_uid);
109 krbtkfile_env = tkt_location;
111 (void)krb_set_tkt_string(tkt_location);
114 * Set real as well as effective ID to 0 for the moment,
115 * to make the kerberos library do the right thing.
121 kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
122 realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password);
125 * If we got a TGT, get a local "rcmd" ticket and check it so as to
126 * ensure that we are not talking to a bogus Kerberos server.
128 * There are 2 cases where we still allow a login:
129 * 1: the VERIFY_SERVICE doesn't exist in the KDC
130 * 2: local host has no srvtab, as (hopefully) indicated by a
131 * return value of RD_AP_UNDEC from krb_rd_req().
133 if (kerror != INTK_OK) {
134 if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
135 syslog(LOG_ERR, "Kerberos intkt error: %s",
136 krb_err_txt[kerror]);
142 if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
143 syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
145 (void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
146 savehost[sizeof(savehost)-1] = NULL;
148 #ifdef KLOGIN_PARANOID
150 * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
151 * still allow login with tickets, but log the error condition.
154 kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
155 if (kerror == KDC_PR_UNKNOWN) {
157 "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
158 krb_err_txt[kerror], VERIFY_SERVICE, savehost);
163 if (kerror != KSUCCESS) {
164 warnx("unable to use TGT: (%s)", krb_err_txt[kerror]);
165 syslog(LOG_NOTICE, "unable to use TGT: (%s)",
166 krb_err_txt[kerror]);
171 if (!(hp = gethostbyname(localhost))) {
172 syslog(LOG_ERR, "couldn't get local host address");
177 memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
179 kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
182 if (kerror == KSUCCESS) {
187 /* undecipherable: probably didn't have a srvtab on the local host */
188 if (kerror == RD_AP_UNDEC) {
189 syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]);
193 /* failed for some other reason */
194 warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
195 krb_err_txt[kerror]);
196 syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
197 krb_err_txt[kerror]);