Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / appl / bsd / 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
34 #include "bsd_locl.h"
35
36 RCSID("$Id: klogin.c,v 1.27 1999/10/04 16:11:48 bg Exp $");
37
38 #ifdef KERBEROS
39
40 #define VERIFY_SERVICE  "rcmd"
41
42 extern int notickets;
43 extern char *krbtkfile_env;
44
45 static char tkt_location[MaxPathLen];
46
47 static int
48 multiple_get_tkt(char *name,
49                  char *instance,
50                  char *realm,
51                  char *service,
52                  char *sinstance,
53                  int life,
54                  char *password)
55 {
56   int ret;
57   int n;
58   char rlm[256];
59
60   /* First try to verify against the supplied realm. */
61   ret = krb_get_pw_in_tkt(name, instance, realm, service, realm, life,
62                           password);
63   if(ret == KSUCCESS)
64     return KSUCCESS;
65
66   /* Verify all local realms, except the supplied realm. */
67   for (n = 1; krb_get_lrealm(rlm, n) == KSUCCESS; n++)
68     if (strcmp(rlm, realm) != 0) {
69       ret = krb_get_pw_in_tkt(name, instance, rlm,service, rlm,life, password);
70       if (ret == KSUCCESS)
71         return KSUCCESS;
72     }
73
74   return ret;
75 }
76
77 /*
78  * Attempt to log the user in using Kerberos authentication
79  *
80  * return 0 on success (will be logged in)
81  *        1 if Kerberos failed (try local password in login)
82  */
83 int
84 klogin(struct passwd *pw, char *instance, char *localhost, char *password)
85 {
86     int kerror;
87     AUTH_DAT authdata;
88     KTEXT_ST ticket;
89     struct hostent *hp;
90     u_int32_t faddr;
91     char realm[REALM_SZ], savehost[MaxHostNameLen];
92     extern int noticketsdontcomplain;
93
94 #ifdef KLOGIN_PARANOID
95     noticketsdontcomplain = 0; /* enable warning message */
96 #endif
97     /*
98      * Root logins don't use Kerberos.
99      * If we have a realm, try getting a ticket-granting ticket
100      * and using it to authenticate.  Otherwise, return
101      * failure so that we can try the normal passwd file
102      * for a password.  If that's ok, log the user in
103      * without issuing any tickets.
104      */
105     if (strcmp(pw->pw_name, "root") == 0 ||
106         krb_get_lrealm(realm, 1) != KSUCCESS)
107         return (1);
108
109     noticketsdontcomplain = 0; /* enable warning message */
110
111     /*
112      * get TGT for local realm
113      * tickets are stored in a file named TKT_ROOT plus uid
114      * except for user.root tickets.
115      */
116
117     if (strcmp(instance, "root") != 0)
118         snprintf(tkt_location, sizeof(tkt_location),
119                  "%s%u_%u",
120                 TKT_ROOT, (unsigned)pw->pw_uid, (unsigned)getpid());
121     else {
122         snprintf(tkt_location, sizeof(tkt_location),
123                  "%s_root_%d", TKT_ROOT,
124                 (unsigned)pw->pw_uid);
125     }
126     krbtkfile_env = tkt_location;
127     krb_set_tkt_string(tkt_location);
128
129     /*
130      * Set real as well as effective ID to 0 for the moment,
131      * to make the kerberos library do the right thing.
132      */
133     if (setuid(0) < 0) {
134         warnx("setuid");
135         return (1);
136     }
137
138     /*
139      * Get ticket
140      */
141     kerror = multiple_get_tkt(pw->pw_name,
142                               instance,
143                               realm,
144                               KRB_TICKET_GRANTING_TICKET,
145                               realm,
146                               DEFAULT_TKT_LIFE,
147                               password);
148
149     /*
150      * If we got a TGT, get a local "rcmd" ticket and check it so as to
151      * ensure that we are not talking to a bogus Kerberos server.
152      *
153      * There are 2 cases where we still allow a login:
154      *  1: the VERIFY_SERVICE doesn't exist in the KDC
155      *  2: local host has no srvtab, as (hopefully) indicated by a
156      *     return value of RD_AP_UNDEC from krb_rd_req().
157      */
158     if (kerror != INTK_OK) {
159         if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
160             syslog(LOG_ERR, "Kerberos intkt error: %s",
161                    krb_get_err_text(kerror));
162             dest_tkt();
163         }
164         return (1);
165     }
166
167     if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
168         syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
169
170     strlcpy(savehost, krb_get_phost(localhost), sizeof(savehost));
171
172 #ifdef KLOGIN_PARANOID
173     /*
174      * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
175      * don't allow kerberos login, also log the error condition.
176      */
177
178     kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
179     if (kerror == KDC_PR_UNKNOWN) {
180         syslog(LOG_NOTICE,
181                "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
182                krb_get_err_text(kerror), VERIFY_SERVICE, savehost);
183         notickets = 0;
184         return (1);
185     }
186
187     if (kerror != KSUCCESS) {
188         warnx("unable to use TGT: (%s)", krb_get_err_text(kerror));
189         syslog(LOG_NOTICE, "unable to use TGT: (%s)",
190                krb_get_err_text(kerror));
191         dest_tkt();
192         return (1);
193     }
194
195     if (!(hp = gethostbyname(localhost))) {
196         syslog(LOG_ERR, "couldn't get local host address");
197         dest_tkt();
198         return (1);
199     }
200
201     memcpy(&faddr, hp->h_addr, sizeof(faddr));
202
203     kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
204                         &authdata, "");
205
206     if (kerror == KSUCCESS) {
207         notickets = 0;
208         return (0);
209     }
210
211     /* undecipherable: probably didn't have a srvtab on the local host */
212     if (kerror == RD_AP_UNDEC) {
213         syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_get_err_text(kerror));
214         dest_tkt();
215         return (1);
216     }
217     /* failed for some other reason */
218     warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
219           krb_get_err_text(kerror));
220     syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
221            krb_get_err_text(kerror));
222     dest_tkt();
223     return (1);
224 #else
225     notickets = 0;
226     return (0);
227 #endif
228 }
229 #endif