Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libpam / modules / pam_kerberosIV / klogin.c
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: src/lib/libpam/modules/pam_kerberosIV/klogin.c,v 1.11 2000/02/24 22:24:37 markm Exp $
34  */
35
36 #ifndef lint
37 static const char sccsid[] = "@(#)klogin.c      8.3 (Berkeley) 4/2/94";
38 #endif /* not lint */
39
40 #ifdef KERBEROS
41 #include <sys/param.h>
42 #include <sys/syslog.h>
43 #include <openssl/des.h>
44 #include <krb.h>
45
46 #include <err.h>
47 #include <netdb.h>
48 #include <pwd.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #define INITIAL_TICKET  "krbtgt"
55 #define VERIFY_SERVICE  "rcmd"
56
57 extern int notickets;
58 extern char *krbtkfile_env;
59
60 /*
61  * Attempt to log the user in using Kerberos authentication
62  *
63  * return 0 on success (will be logged in)
64  *        1 if Kerberos failed (try local password in login)
65  */
66 int
67 klogin(pw, instance, localhost, password)
68         struct passwd *pw;
69         char *instance, *localhost, *password;
70 {
71         int kerror;
72         char realm[REALM_SZ], savehost[MAXHOSTNAMELEN];
73         char tkt_location[MAXPATHLEN];
74         char *krb_get_phost();
75         extern int noticketsdontcomplain;
76
77 #ifdef  KLOGIN_PARANOID
78         AUTH_DAT authdata;
79         KTEXT_ST ticket;
80         struct hostent *hp;
81         unsigned long faddr;
82
83         noticketsdontcomplain = 0; /* enable warning message */
84 #endif
85
86         /*
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.
93          */
94         if (strcmp(pw->pw_name, "root") == 0 ||
95             krb_get_lrealm(realm, 0) != KSUCCESS)
96                 return (1);
97
98         noticketsdontcomplain = 0; /* enable warning message */
99
100         /*
101          * get TGT for local realm
102          * tickets are stored in a file named TKT_ROOT plus uid
103          * except for user.root tickets.
104          */
105
106         if (strcmp(instance, "root") != 0)
107                 (void)sprintf(tkt_location, "%s%d", TKT_ROOT, pw->pw_uid);
108         else {
109                 (void)sprintf(tkt_location, "%s_root_%d", TKT_ROOT, pw->pw_uid);
110                 krbtkfile_env = tkt_location;
111         }
112         (void)krb_set_tkt_string(tkt_location);
113
114         /*
115          * Set real as well as effective ID to 0 for the moment,
116          * to make the kerberos library do the right thing.
117          */
118         if (setuid(0) < 0) {
119                 warnx("setuid");
120                 return (1);
121         }
122         kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
123                     realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password);
124
125         /*
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.
128          *
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().
133          */
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]);
138                         dest_tkt();
139                 }
140                 return (1);
141         }
142
143         if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
144                 syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
145
146         (void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
147         savehost[sizeof(savehost)-1] = NULL;
148
149 #ifdef KLOGIN_PARANOID
150         /*
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.
153          */
154
155         kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
156         if (kerror == KDC_PR_UNKNOWN) {
157                 syslog(LOG_NOTICE,
158                     "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
159                     krb_err_txt[kerror], VERIFY_SERVICE, savehost);
160                 notickets = 0;
161                 return (0);
162         }
163
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]);
168                 dest_tkt();
169                 return (1);
170         }
171
172         if (!(hp = gethostbyname(localhost))) {
173                 syslog(LOG_ERR, "couldn't get local host address");
174                 dest_tkt();
175                 return (1);
176         }
177
178         memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
179
180         kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
181             &authdata, "");
182
183         if (kerror == KSUCCESS) {
184                 notickets = 0;
185                 return (0);
186         }
187
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]);
191                 dest_tkt();
192                 return (1);
193         }
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]);
199         dest_tkt();
200         return (1);
201 #else
202         notickets = 0;
203         return (0);
204 #endif
205 }
206 #endif