Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * $DragonFly: src/lib/libpam/modules/pam_kerberosIV/Attic/klogin.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
35  *
36  * @(#)klogin.c 8.3 (Berkeley) 4/2/94
37  */
38
39 #ifdef KERBEROS
40 #include <sys/param.h>
41 #include <sys/syslog.h>
42 #include <openssl/des.h>
43 #include <krb.h>
44
45 #include <err.h>
46 #include <netdb.h>
47 #include <pwd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #define INITIAL_TICKET  "krbtgt"
54 #define VERIFY_SERVICE  "rcmd"
55
56 extern int notickets;
57 extern char *krbtkfile_env;
58
59 /*
60  * Attempt to log the user in using Kerberos authentication
61  *
62  * return 0 on success (will be logged in)
63  *        1 if Kerberos failed (try local password in login)
64  */
65 int
66 klogin(pw, instance, localhost, password)
67         struct passwd *pw;
68         char *instance, *localhost, *password;
69 {
70         int kerror;
71         char realm[REALM_SZ], savehost[MAXHOSTNAMELEN];
72         char tkt_location[MAXPATHLEN];
73         char *krb_get_phost();
74         extern int noticketsdontcomplain;
75
76 #ifdef  KLOGIN_PARANOID
77         AUTH_DAT authdata;
78         KTEXT_ST ticket;
79         struct hostent *hp;
80         unsigned long faddr;
81
82         noticketsdontcomplain = 0; /* enable warning message */
83 #endif
84
85         /*
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.
92          */
93         if (strcmp(pw->pw_name, "root") == 0 ||
94             krb_get_lrealm(realm, 0) != KSUCCESS)
95                 return (1);
96
97         noticketsdontcomplain = 0; /* enable warning message */
98
99         /*
100          * get TGT for local realm
101          * tickets are stored in a file named TKT_ROOT plus uid
102          * except for user.root tickets.
103          */
104
105         if (strcmp(instance, "root") != 0)
106                 (void)sprintf(tkt_location, "%s%d", TKT_ROOT, pw->pw_uid);
107         else {
108                 (void)sprintf(tkt_location, "%s_root_%d", TKT_ROOT, pw->pw_uid);
109                 krbtkfile_env = tkt_location;
110         }
111         (void)krb_set_tkt_string(tkt_location);
112
113         /*
114          * Set real as well as effective ID to 0 for the moment,
115          * to make the kerberos library do the right thing.
116          */
117         if (setuid(0) < 0) {
118                 warnx("setuid");
119                 return (1);
120         }
121         kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
122                     realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password);
123
124         /*
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.
127          *
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().
132          */
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]);
137                         dest_tkt();
138                 }
139                 return (1);
140         }
141
142         if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
143                 syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
144
145         (void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
146         savehost[sizeof(savehost)-1] = NULL;
147
148 #ifdef KLOGIN_PARANOID
149         /*
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.
152          */
153
154         kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
155         if (kerror == KDC_PR_UNKNOWN) {
156                 syslog(LOG_NOTICE,
157                     "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
158                     krb_err_txt[kerror], VERIFY_SERVICE, savehost);
159                 notickets = 0;
160                 return (0);
161         }
162
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]);
167                 dest_tkt();
168                 return (1);
169         }
170
171         if (!(hp = gethostbyname(localhost))) {
172                 syslog(LOG_ERR, "couldn't get local host address");
173                 dest_tkt();
174                 return (1);
175         }
176
177         memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
178
179         kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
180             &authdata, "");
181
182         if (kerror == KSUCCESS) {
183                 notickets = 0;
184                 return (0);
185         }
186
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]);
190                 dest_tkt();
191                 return (1);
192         }
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]);
198         dest_tkt();
199         return (1);
200 #else
201         notickets = 0;
202         return (0);
203 #endif
204 }
205 #endif