]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/kerberosIV/appl/bsd/klogin.c
This commit was generated by cvs2svn to compensate for changes in r47148,
[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.20 1997/05/02 14:27:42 assar 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 /*
48  * Attempt to log the user in using Kerberos authentication
49  *
50  * return 0 on success (will be logged in)
51  *        1 if Kerberos failed (try local password in login)
52  */
53 int
54 klogin(struct passwd *pw, char *instance, char *localhost, char *password)
55 {
56     int kerror;
57     AUTH_DAT authdata;
58     KTEXT_ST ticket;
59     struct hostent *hp;
60     u_int32_t faddr;
61     char realm[REALM_SZ], savehost[MaxHostNameLen];
62     extern int noticketsdontcomplain;
63
64 #ifdef KLOGIN_PARANOID
65     noticketsdontcomplain = 0; /* enable warning message */
66 #endif
67     /*
68      * Root logins don't use Kerberos.
69      * If we have a realm, try getting a ticket-granting ticket
70      * and using it to authenticate.  Otherwise, return
71      * failure so that we can try the normal passwd file
72      * for a password.  If that's ok, log the user in
73      * without issuing any tickets.
74      */
75     if (strcmp(pw->pw_name, "root") == 0 ||
76         krb_get_lrealm(realm, 0) != KSUCCESS)
77         return (1);
78
79     noticketsdontcomplain = 0; /* enable warning message */
80
81     /*
82      * get TGT for local realm
83      * tickets are stored in a file named TKT_ROOT plus uid
84      * except for user.root tickets.
85      */
86
87     if (strcmp(instance, "root") != 0)
88         snprintf(tkt_location, sizeof(tkt_location),
89                  "%s%u_%u",
90                 TKT_ROOT, (unsigned)pw->pw_uid, (unsigned)getpid());
91     else {
92         snprintf(tkt_location, sizeof(tkt_location),
93                  "%s_root_%d", TKT_ROOT,
94                 (unsigned)pw->pw_uid);
95     }
96     krbtkfile_env = tkt_location;
97     krb_set_tkt_string(tkt_location);
98
99     kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
100                                realm, KRB_TICKET_GRANTING_TICKET, realm,
101                                DEFAULT_TKT_LIFE, password);
102
103     /*
104      * If we got a TGT, get a local "rcmd" ticket and check it so as to
105      * ensure that we are not talking to a bogus Kerberos server.
106      *
107      * There are 2 cases where we still allow a login:
108      *  1: the VERIFY_SERVICE doesn't exist in the KDC
109      *  2: local host has no srvtab, as (hopefully) indicated by a
110      *     return value of RD_AP_UNDEC from krb_rd_req().
111      */
112     if (kerror != INTK_OK) {
113         if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
114             syslog(LOG_ERR, "Kerberos intkt error: %s",
115                    krb_get_err_text(kerror));
116             dest_tkt();
117         }
118         return (1);
119     }
120
121     if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
122         syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
123
124     strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
125     savehost[sizeof(savehost)-1] = '\0';
126
127 #ifdef KLOGIN_PARANOID
128     /*
129      * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
130      * don't allow kerberos login, also log the error condition.
131      */
132
133     kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
134     if (kerror == KDC_PR_UNKNOWN) {
135         syslog(LOG_NOTICE,
136                "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
137                krb_get_err_text(kerror), VERIFY_SERVICE, savehost);
138         notickets = 0;
139         return (1);
140     }
141
142     if (kerror != KSUCCESS) {
143         warnx("unable to use TGT: (%s)", krb_get_err_text(kerror));
144         syslog(LOG_NOTICE, "unable to use TGT: (%s)",
145                krb_get_err_text(kerror));
146         dest_tkt();
147         return (1);
148     }
149
150     if (!(hp = gethostbyname(localhost))) {
151         syslog(LOG_ERR, "couldn't get local host address");
152         dest_tkt();
153         return (1);
154     }
155
156     memcpy(&faddr, hp->h_addr, sizeof(faddr));
157
158     kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
159                         &authdata, "");
160
161     if (kerror == KSUCCESS) {
162         notickets = 0;
163         return (0);
164     }
165
166     /* undecipherable: probably didn't have a srvtab on the local host */
167     if (kerror == RD_AP_UNDEC) {
168         syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_get_err_text(kerror));
169         dest_tkt();
170         return (1);
171     }
172     /* failed for some other reason */
173     warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
174           krb_get_err_text(kerror));
175     syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
176            krb_get_err_text(kerror));
177     dest_tkt();
178     return (1);
179 #else
180     notickets = 0;
181     return (0);
182 #endif
183 }
184 #endif