k5login.c revision 1.11 1 /* $NetBSD: k5login.c,v 1.11 2000/02/14 03:17:43 aidan Exp $ */
2
3 /*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)klogin.c 5.11 (Berkeley) 7/12/92";
40 #endif
41 __RCSID("$NetBSD: k5login.c,v 1.11 2000/02/14 03:17:43 aidan Exp $");
42 #endif /* not lint */
43
44 #ifdef KERBEROS5
45 #include <sys/param.h>
46 #include <sys/syslog.h>
47 #include <krb5/krb5.h>
48 #include <com_err.h>
49 #include <pwd.h>
50 #include <netdb.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #define KRB5_DEFAULT_OPTIONS 0
57 #define KRB5_DEFAULT_LIFE 60*60*10 /* 10 hours */
58
59 krb5_context kcontext;
60
61 int notickets;
62 char *krbtkfile_env;
63 extern char *tty;
64
65 static char tkt_location[MAXPATHLEN];
66 static krb5_creds forw_creds;
67 int have_forward;
68 static krb5_principal me, server;
69 int use_krb5;
70
71 int k5_read_creds __P((char *));
72 int k5_write_creds __P((void));
73 int klogin __P((struct passwd *, char *, char *, char *));
74 void kdestroy __P((void));
75
76 #ifndef krb5_realm_length
77 #define krb5_realm_length(r) ((r).length)
78 #endif
79 #ifndef krb5_realm_data
80 #define krb5_realm_data(r) ((r).data)
81 #endif
82
83 /*
84 * Attempt to read forwarded kerberos creds
85 *
86 * return 0 on success (forwarded creds in memory)
87 * 1 if no forwarded creds.
88 */
89 int
90 k5_read_creds(username)
91 char *username;
92 {
93 krb5_error_code code;
94 krb5_creds mcreds;
95 krb5_ccache ccache;
96
97 if (! use_krb5)
98 return(1);
99
100 have_forward = 0;
101 memset((char*) &mcreds, 0, sizeof(forw_creds));
102 memset((char*) &forw_creds, 0, sizeof(forw_creds));
103
104 code = krb5_cc_default(kcontext, &ccache);
105 if (code) {
106 com_err("login", code, "while getting default ccache");
107 return(1);
108 }
109
110 code = krb5_parse_name(kcontext, username, &me);
111 if (code) {
112 com_err("login", code, "when parsing name %s", username);
113 return(1);
114 }
115
116 mcreds.client = me;
117 code = krb5_build_principal_ext(kcontext, &mcreds.server,
118 krb5_realm_length(*krb5_princ_realm(kcontext, me)),
119 krb5_realm_data(*krb5_princ_realm(kcontext, me)),
120 KRB5_TGS_NAME_SIZE,
121 KRB5_TGS_NAME,
122 krb5_realm_length(*krb5_princ_realm(kcontext, me)),
123 krb5_realm_data(*krb5_princ_realm(kcontext, me)),
124 0);
125 if (code) {
126 com_err("login", code, "while building server name");
127 goto nuke_ccache;
128 }
129
130 code = krb5_cc_retrieve_cred(kcontext, ccache, 0,
131 &mcreds, &forw_creds);
132 if (code) {
133 com_err("login", code, "while retrieving V5 initial ticket for copy");
134 goto nuke_ccache;
135 }
136 have_forward = 1;
137
138 strcpy(tkt_location, getenv("KRB5CCNAME"));
139 krbtkfile_env = tkt_location;
140 notickets = 0;
141
142 nuke_ccache:
143 krb5_cc_destroy(kcontext, ccache);
144 return(!have_forward);
145 }
146
147 int
148 k5_write_creds()
149 {
150 krb5_error_code code;
151 krb5_ccache ccache;
152
153 if (! use_krb5)
154 return(1);
155 if (!have_forward)
156 return(1);
157 code = krb5_cc_default(kcontext, &ccache);
158 if (code) {
159 com_err("login", code, "while getting default ccache");
160 return(1);
161 }
162
163 code = krb5_cc_initialize(kcontext, ccache, me);
164 if (code) {
165 com_err("login", code, "while re-initializing V5 ccache as user");
166 goto nuke_ccache_contents;
167 }
168
169 code = krb5_cc_store_cred(kcontext, ccache, &forw_creds);
170 if (code) {
171 com_err("login", code, "while re-storing V5 ccache as user");
172 goto nuke_ccache_contents;
173 }
174
175 nuke_ccache_contents:
176 krb5_free_cred_contents(kcontext, &forw_creds);
177 return(code != 0);
178 }
179
180 /*
181 * Attempt to log the user in using Kerberos authentication
182 *
183 * return 0 on success (will be logged in)
184 * 1 if Kerberos failed (try local password in login)
185 */
186 int
187 klogin(pw, instance, localhost, password)
188 struct passwd *pw;
189 char *instance, *localhost, *password;
190 {
191 krb5_error_code kerror;
192 krb5_creds my_creds;
193 krb5_timestamp now;
194 krb5_ccache ccache = NULL;
195 long lifetime = KRB5_DEFAULT_LIFE;
196 int options = KRB5_DEFAULT_OPTIONS;
197 char *realm, *client_name;
198 char *principal;
199
200 /*
201 * Root logins don't use Kerberos.
202 * If we have a realm, try getting a ticket-granting ticket
203 * and using it to authenticate. Otherwise, return
204 * failure so that we can try the normal passwd file
205 * for a password. If that's ok, log the user in
206 * without issuing any tickets.
207 */
208 if (! use_krb5 ||
209 strcmp(pw->pw_name, "root") == 0 ||
210 krb5_get_default_realm(kcontext, &realm))
211 return (1);
212
213 /*
214 * get TGT for local realm
215 * tickets are stored in a file named TKT_ROOT plus uid
216 * except for user.root tickets.
217 */
218
219 if (strcmp(instance, "root") != 0)
220 (void)snprintf(tkt_location, sizeof tkt_location,
221 "FILE:/tmp/krb5cc_%d.%s", pw->pw_uid, tty);
222 else
223 (void)snprintf(tkt_location, sizeof tkt_location,
224 "FILE:/tmp/krb5cc_root_%d.%s", pw->pw_uid, tty);
225 krbtkfile_env = tkt_location;
226
227 principal = (char *)malloc(strlen(pw->pw_name)+strlen(instance)+2);
228 strcpy(principal, pw->pw_name); /* XXX strcpy is safe */
229 if (strlen(instance)) {
230 strcat(principal, "/"); /* XXX strcat is safe */
231 strcat(principal, instance); /* XXX strcat is safe */
232 }
233
234 if ((kerror = krb5_cc_resolve(kcontext, tkt_location, &ccache)) != 0) {
235 syslog(LOG_NOTICE, "warning: %s while getting default ccache",
236 error_message(kerror));
237 return(1);
238 }
239
240 if ((kerror = krb5_parse_name(kcontext, principal, &me)) != 0) {
241 syslog(LOG_NOTICE, "warning: %s when parsing name %s",
242 error_message(kerror), principal);
243 return(1);
244 }
245
246 if ((kerror = krb5_unparse_name(kcontext, me, &client_name)) != 0) {
247 syslog(LOG_NOTICE, "warning: %s when unparsing name %s",
248 error_message(kerror), principal);
249 return(1);
250 }
251
252 kerror = krb5_cc_initialize(kcontext, ccache, me);
253 if (kerror != 0) {
254 syslog(LOG_NOTICE, "%s when initializing cache %s",
255 error_message(kerror), tkt_location);
256 return(1);
257 }
258
259 memset((char *)&my_creds, 0, sizeof(my_creds));
260
261 my_creds.client = me;
262
263 if ((kerror = krb5_build_principal_ext(kcontext,
264 &server,
265 krb5_realm_length(*krb5_princ_realm(kcontext, me)),
266 krb5_realm_data(*krb5_princ_realm(kcontext, me)),
267 KRB5_TGS_NAME_SIZE,
268 KRB5_TGS_NAME,
269 krb5_realm_length(*krb5_princ_realm(kcontext, me)),
270 krb5_realm_data(*krb5_princ_realm(kcontext, me)),
271 0)) != 0) {
272 syslog(LOG_NOTICE, "%s while building server name",
273 error_message(kerror));
274 return(1);
275 }
276
277 my_creds.server = server;
278
279 if ((kerror = krb5_timeofday(kcontext, &now)) != 0) {
280 syslog(LOG_NOTICE, "%s while getting time of day",
281 error_message(kerror));
282 return(1);
283 }
284 my_creds.times.starttime = 0; /* start timer when request
285 gets to KDC */
286 my_creds.times.endtime = now + lifetime;
287 my_creds.times.renew_till = 0;
288
289 kerror = krb5_get_in_tkt_with_password(kcontext, options,
290 NULL,
291 NULL,
292 NULL,
293 password,
294 ccache,
295 &my_creds, 0);
296
297 krb5_free_principal(kcontext, server);
298
299 if (chown(&tkt_location[5], pw->pw_uid, pw->pw_gid) < 0)
300 syslog(LOG_ERR, "chown tkfile (%s): %m", &tkt_location[5]);
301
302 if (kerror) {
303 if (kerror == KRB5KRB_AP_ERR_BAD_INTEGRITY)
304 printf("%s: Kerberos Password incorrect\n", principal);
305 else
306 printf("%s while getting initial credentials\n", error_message(kerror));
307
308 return(1);
309 }
310
311 /* Success */
312 notickets = 0;
313 return(0);
314 }
315
316 /*
317 * Remove any credentials
318 */
319 void
320 kdestroy()
321 {
322 krb5_error_code code;
323 krb5_ccache ccache = NULL;
324
325 if (! use_krb5 || krbtkfile_env == NULL)
326 return;
327
328 code = krb5_cc_resolve(kcontext, krbtkfile_env, &ccache);
329 if (!code)
330 code = krb5_cc_destroy(kcontext, ccache);
331 }
332 #endif
333