]> andersk Git - openssh.git/blame - auth-krb4.c
- (stevesk) OpenSSH CVS update:
[openssh.git] / auth-krb4.c
CommitLineData
8efc0c15 1/*
bcbf86ec 2 * Copyright (c) 1999 Dug Song. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5260325f 23 */
8efc0c15 24
25#include "includes.h"
26#include "packet.h"
27#include "xmalloc.h"
28#include "ssh.h"
57112b5a 29#include "servconf.h"
8efc0c15 30
94ec8c6b 31RCSID("$OpenBSD: auth-krb4.c,v 1.19 2000/10/03 18:03:02 markus Exp $");
e5a0294f 32
8efc0c15 33#ifdef KRB4
6a17f9c2 34char *ticket = NULL;
35
57112b5a 36extern ServerOptions options;
37
38/*
39 * try krb4 authentication,
40 * return 1 on success, 0 on failure, -1 if krb4 is not available
41 */
42
6ae2364d 43int
57112b5a 44auth_krb4_password(struct passwd * pw, const char *password)
45{
46 AUTH_DAT adata;
47 KTEXT_ST tkt;
48 struct hostent *hp;
49 unsigned long faddr;
50 char localhost[MAXHOSTNAMELEN];
51 char phost[INST_SZ];
52 char realm[REALM_SZ];
53 int r;
54
55 /*
56 * Try Kerberos password authentication only for non-root
57 * users and only if Kerberos is installed.
58 */
59 if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {
60
61 /* Set up our ticket file. */
62 if (!krb4_init(pw->pw_uid)) {
63 log("Couldn't initialize Kerberos ticket file for %s!",
64 pw->pw_name);
65 goto kerberos_auth_failure;
66 }
67 /* Try to get TGT using our password. */
68 r = krb_get_pw_in_tkt((char *) pw->pw_name, "",
69 realm, "krbtgt", realm,
70 DEFAULT_TKT_LIFE, (char *) password);
71 if (r != INTK_OK) {
72 packet_send_debug("Kerberos V4 password "
73 "authentication for %s failed: %s",
74 pw->pw_name, krb_err_txt[r]);
75 goto kerberos_auth_failure;
76 }
77 /* Successful authentication. */
78 chown(tkt_string(), pw->pw_uid, pw->pw_gid);
79
80 /*
81 * Now that we have a TGT, try to get a local
82 * "rcmd" ticket to ensure that we are not talking
83 * to a bogus Kerberos server.
84 */
85 (void) gethostname(localhost, sizeof(localhost));
86 (void) strlcpy(phost, (char *) krb_get_phost(localhost),
87 INST_SZ);
88 r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
89
90 if (r == KSUCCESS) {
91 if (!(hp = gethostbyname(localhost))) {
92 log("Couldn't get local host address!");
93 goto kerberos_auth_failure;
94 }
95 memmove((void *) &faddr, (void *) hp->h_addr,
96 sizeof(faddr));
97
98 /* Verify our "rcmd" ticket. */
99 r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost,
100 faddr, &adata, "");
101 if (r == RD_AP_UNDEC) {
102 /*
103 * Probably didn't have a srvtab on
c345cf9d 104 * localhost. Disallow login.
57112b5a 105 */
106 log("Kerberos V4 TGT for %s unverifiable, "
107 "no srvtab installed? krb_rd_req: %s",
108 pw->pw_name, krb_err_txt[r]);
c345cf9d 109 goto kerberos_auth_failure;
57112b5a 110 } else if (r != KSUCCESS) {
111 log("Kerberos V4 %s ticket unverifiable: %s",
112 KRB4_SERVICE_NAME, krb_err_txt[r]);
113 goto kerberos_auth_failure;
114 }
115 } else if (r == KDC_PR_UNKNOWN) {
116 /*
c345cf9d 117 * Disallow login if no rcmd service exists, and
57112b5a 118 * log the error.
119 */
120 log("Kerberos V4 TGT for %s unverifiable: %s; %s.%s "
121 "not registered, or srvtab is wrong?", pw->pw_name,
122 krb_err_txt[r], KRB4_SERVICE_NAME, phost);
c345cf9d 123 goto kerberos_auth_failure;
57112b5a 124 } else {
125 /*
126 * TGT is bad, forget it. Possibly spoofed!
127 */
128 packet_send_debug("WARNING: Kerberos V4 TGT "
129 "possibly spoofed for %s: %s",
130 pw->pw_name, krb_err_txt[r]);
131 goto kerberos_auth_failure;
132 }
133
134 /* Authentication succeeded. */
135 return 1;
136
137kerberos_auth_failure:
138 krb4_cleanup_proc(NULL);
139
140 if (!options.kerberos_or_local_passwd)
141 return 0;
142 } else {
143 /* Logging in as root or no local Kerberos realm. */
144 packet_send_debug("Unable to authenticate to Kerberos.");
145 }
146 /* Fall back to ordinary passwd authentication. */
147 return -1;
148}
149
6a17f9c2 150void
151krb4_cleanup_proc(void *ignore)
152{
5260325f 153 debug("krb4_cleanup_proc called");
154 if (ticket) {
155 (void) dest_tkt();
156 xfree(ticket);
157 ticket = NULL;
158 }
6a17f9c2 159}
160
6ae2364d 161int
5260325f 162krb4_init(uid_t uid)
8efc0c15 163{
5260325f 164 static int cleanup_registered = 0;
4fe2af09 165 const char *tkt_root = TKT_ROOT;
5260325f 166 struct stat st;
167 int fd;
168
169 if (!ticket) {
170 /* Set unique ticket string manually since we're still root. */
171 ticket = xmalloc(MAXPATHLEN);
8efc0c15 172#ifdef AFS
5260325f 173 if (lstat("/ticket", &st) != -1)
174 tkt_root = "/ticket/";
8efc0c15 175#endif /* AFS */
14a9a859 176 snprintf(ticket, MAXPATHLEN, "%s%u_%d", tkt_root, uid, getpid());
5260325f 177 (void) krb_set_tkt_string(ticket);
178 }
179 /* Register ticket cleanup in case of fatal error. */
180 if (!cleanup_registered) {
181 fatal_add_cleanup(krb4_cleanup_proc, NULL);
182 cleanup_registered = 1;
183 }
184 /* Try to create our ticket file. */
185 if ((fd = mkstemp(ticket)) != -1) {
186 close(fd);
187 return 1;
188 }
189 /* Ticket file exists - make sure user owns it (just passed ticket). */
190 if (lstat(ticket, &st) != -1) {
191 if (st.st_mode == (S_IFREG | S_IRUSR | S_IWUSR) &&
192 st.st_uid == uid)
193 return 1;
194 }
195 /* Failure - cancel cleanup function, leaving bad ticket for inspection. */
196 log("WARNING: bad ticket file %s", ticket);
197 fatal_remove_cleanup(krb4_cleanup_proc, NULL);
198 cleanup_registered = 0;
199 xfree(ticket);
200 ticket = NULL;
201
202 return 0;
8efc0c15 203}
204
6ae2364d 205int
5260325f 206auth_krb4(const char *server_user, KTEXT auth, char **client)
8efc0c15 207{
5260325f 208 AUTH_DAT adat = {0};
209 KTEXT_ST reply;
210 char instance[INST_SZ];
211 int r, s;
610cd5c6 212 socklen_t slen;
5260325f 213 u_int cksum;
214 Key_schedule schedule;
215 struct sockaddr_in local, foreign;
216
217 s = packet_get_connection_in();
218
610cd5c6 219 slen = sizeof(local);
5260325f 220 memset(&local, 0, sizeof(local));
610cd5c6 221 if (getsockname(s, (struct sockaddr *) & local, &slen) < 0)
5260325f 222 debug("getsockname failed: %.100s", strerror(errno));
610cd5c6 223 slen = sizeof(foreign);
5260325f 224 memset(&foreign, 0, sizeof(foreign));
610cd5c6 225 if (getpeername(s, (struct sockaddr *) & foreign, &slen) < 0) {
5260325f 226 debug("getpeername failed: %.100s", strerror(errno));
227 fatal_cleanup();
228 }
229 instance[0] = '*';
230 instance[1] = 0;
231
232 /* Get the encrypted request, challenge, and session key. */
233 if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance, 0, &adat, ""))) {
234 packet_send_debug("Kerberos V4 krb_rd_req: %.100s", krb_err_txt[r]);
235 return 0;
236 }
237 des_key_sched((des_cblock *) adat.session, schedule);
238
239 *client = xmalloc(MAX_K_NAME_SZ);
240 (void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname,
241 *adat.pinst ? "." : "", adat.pinst, adat.prealm);
242
243 /* Check ~/.klogin authorization now. */
244 if (kuserok(&adat, (char *) server_user) != KSUCCESS) {
245 packet_send_debug("Kerberos V4 .klogin authorization failed!");
246 log("Kerberos V4 .klogin authorization failed for %s to account %s",
247 *client, server_user);
248 xfree(*client);
249 return 0;
250 }
251 /* Increment the checksum, and return it encrypted with the
252 session key. */
253 cksum = adat.checksum + 1;
254 cksum = htonl(cksum);
255
256 /* If we can't successfully encrypt the checksum, we send back an
257 empty message, admitting our failure. */
258 if ((r = krb_mk_priv((u_char *) & cksum, reply.dat, sizeof(cksum) + 1,
259 schedule, &adat.session, &local, &foreign)) < 0) {
260 packet_send_debug("Kerberos V4 mk_priv: (%d) %s", r, krb_err_txt[r]);
261 reply.dat[0] = 0;
262 reply.length = 0;
263 } else
264 reply.length = r;
265
266 /* Clear session key. */
267 memset(&adat.session, 0, sizeof(&adat.session));
268
269 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
270 packet_put_string((char *) reply.dat, reply.length);
271 packet_send();
272 packet_write_wait();
273 return 1;
8efc0c15 274}
275#endif /* KRB4 */
276
277#ifdef AFS
6ae2364d 278int
5260325f 279auth_kerberos_tgt(struct passwd *pw, const char *string)
8efc0c15 280{
5260325f 281 CREDENTIALS creds;
282
94ec8c6b 283 if (pw == NULL)
284 goto auth_kerberos_tgt_failure;
5260325f 285 if (!radix_to_creds(string, &creds)) {
286 log("Protocol error decoding Kerberos V4 tgt");
287 packet_send_debug("Protocol error decoding Kerberos V4 tgt");
288 goto auth_kerberos_tgt_failure;
289 }
290 if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
291 strlcpy(creds.service, "krbtgt", sizeof creds.service);
292
293 if (strcmp(creds.service, "krbtgt")) {
294 log("Kerberos V4 tgt (%s%s%s@%s) rejected for %s", creds.pname,
295 creds.pinst[0] ? "." : "", creds.pinst, creds.realm,
296 pw->pw_name);
297 packet_send_debug("Kerberos V4 tgt (%s%s%s@%s) rejected for %s",
298 creds.pname, creds.pinst[0] ? "." : "", creds.pinst,
299 creds.realm, pw->pw_name);
300 goto auth_kerberos_tgt_failure;
301 }
302 if (!krb4_init(pw->pw_uid))
303 goto auth_kerberos_tgt_failure;
304
305 if (in_tkt(creds.pname, creds.pinst) != KSUCCESS)
306 goto auth_kerberos_tgt_failure;
307
308 if (save_credentials(creds.service, creds.instance, creds.realm,
309 creds.session, creds.lifetime, creds.kvno,
310 &creds.ticket_st, creds.issue_date) != KSUCCESS) {
311 packet_send_debug("Kerberos V4 tgt refused: couldn't save credentials");
312 goto auth_kerberos_tgt_failure;
313 }
314 /* Successful authentication, passed all checks. */
315 chown(tkt_string(), pw->pw_uid, pw->pw_gid);
316
317 packet_send_debug("Kerberos V4 tgt accepted (%s.%s@%s, %s%s%s@%s)",
318 creds.service, creds.instance, creds.realm, creds.pname,
319 creds.pinst[0] ? "." : "", creds.pinst, creds.realm);
320 memset(&creds, 0, sizeof(creds));
321 packet_start(SSH_SMSG_SUCCESS);
322 packet_send();
323 packet_write_wait();
324 return 1;
325
326auth_kerberos_tgt_failure:
327 krb4_cleanup_proc(NULL);
328 memset(&creds, 0, sizeof(creds));
329 packet_start(SSH_SMSG_FAILURE);
330 packet_send();
331 packet_write_wait();
332 return 0;
8efc0c15 333}
334
6ae2364d 335int
5260325f 336auth_afs_token(struct passwd *pw, const char *token_string)
8efc0c15 337{
5260325f 338 CREDENTIALS creds;
94ec8c6b 339 uid_t uid;
5260325f 340
94ec8c6b 341 if (pw == NULL) {
342 /* XXX fake protocol error */
343 packet_send_debug("Protocol error decoding AFS token");
344 packet_start(SSH_SMSG_FAILURE);
345 packet_send();
346 packet_write_wait();
347 return 0;
348 }
5260325f 349 if (!radix_to_creds(token_string, &creds)) {
350 log("Protocol error decoding AFS token");
351 packet_send_debug("Protocol error decoding AFS token");
352 packet_start(SSH_SMSG_FAILURE);
353 packet_send();
354 packet_write_wait();
355 return 0;
356 }
357 if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
358 strlcpy(creds.service, "afs", sizeof creds.service);
359
360 if (strncmp(creds.pname, "AFS ID ", 7) == 0)
361 uid = atoi(creds.pname + 7);
94ec8c6b 362 else
363 uid = pw->pw_uid;
5260325f 364
365 if (kafs_settoken(creds.realm, uid, &creds)) {
366 log("AFS token (%s@%s) rejected for %s", creds.pname, creds.realm,
367 pw->pw_name);
368 packet_send_debug("AFS token (%s@%s) rejected for %s", creds.pname,
369 creds.realm, pw->pw_name);
370 memset(&creds, 0, sizeof(creds));
371 packet_start(SSH_SMSG_FAILURE);
372 packet_send();
373 packet_write_wait();
374 return 0;
375 }
376 packet_send_debug("AFS token accepted (%s@%s, %s@%s)", creds.service,
377 creds.realm, creds.pname, creds.realm);
378 memset(&creds, 0, sizeof(creds));
379 packet_start(SSH_SMSG_SUCCESS);
380 packet_send();
381 packet_write_wait();
382 return 1;
8efc0c15 383}
384#endif /* AFS */
This page took 0.374135 seconds and 5 git commands to generate.