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