blob: 799cf261a70eba780d14fb06c6e678a6cc5f1e9a [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Millere4340be2000-09-16 13:29:08 +11002 * 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.
Damien Miller95def091999-11-25 00:26:21 +110023 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024
25#include "includes.h"
26#include "packet.h"
27#include "xmalloc.h"
28#include "ssh.h"
Damien Milleraae6c611999-12-06 11:47:28 +110029#include "servconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030
Damien Millere4340be2000-09-16 13:29:08 +110031RCSID("$OpenBSD: auth-krb4.c,v 1.18 2000/09/07 20:27:49 deraadt Exp $");
Damien Millerbf7f4662000-06-23 10:16:38 +100032
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033#ifdef KRB4
Damien Miller5ce662a1999-11-11 17:57:39 +110034char *ticket = NULL;
35
Damien Milleraae6c611999-12-06 11:47:28 +110036extern ServerOptions options;
37
38/*
39 * try krb4 authentication,
40 * return 1 on success, 0 on failure, -1 if krb4 is not available
41 */
42
Damien Miller4af51302000-04-16 11:18:38 +100043int
Damien Milleraae6c611999-12-06 11:47:28 +110044auth_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
Damien Miller942da032000-08-18 13:59:06 +1000104 * localhost. Disallow login.
Damien Milleraae6c611999-12-06 11:47:28 +1100105 */
106 log("Kerberos V4 TGT for %s unverifiable, "
107 "no srvtab installed? krb_rd_req: %s",
108 pw->pw_name, krb_err_txt[r]);
Damien Miller942da032000-08-18 13:59:06 +1000109 goto kerberos_auth_failure;
Damien Milleraae6c611999-12-06 11:47:28 +1100110 } 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 /*
Damien Miller942da032000-08-18 13:59:06 +1000117 * Disallow login if no rcmd service exists, and
Damien Milleraae6c611999-12-06 11:47:28 +1100118 * 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);
Damien Miller942da032000-08-18 13:59:06 +1000123 goto kerberos_auth_failure;
Damien Milleraae6c611999-12-06 11:47:28 +1100124 } 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
Damien Miller5ce662a1999-11-11 17:57:39 +1100150void
151krb4_cleanup_proc(void *ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152{
Damien Miller95def091999-11-25 00:26:21 +1100153 debug("krb4_cleanup_proc called");
154 if (ticket) {
155 (void) dest_tkt();
156 xfree(ticket);
157 ticket = NULL;
158 }
Damien Miller5ce662a1999-11-11 17:57:39 +1100159}
160
Damien Miller4af51302000-04-16 11:18:38 +1000161int
Damien Miller95def091999-11-25 00:26:21 +1100162krb4_init(uid_t uid)
Damien Miller5ce662a1999-11-11 17:57:39 +1100163{
Damien Miller95def091999-11-25 00:26:21 +1100164 static int cleanup_registered = 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000165 const char *tkt_root = TKT_ROOT;
Damien Miller95def091999-11-25 00:26:21 +1100166 struct stat st;
167 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
Damien Miller95def091999-11-25 00:26:21 +1100169 if (!ticket) {
170 /* Set unique ticket string manually since we're still root. */
171 ticket = xmalloc(MAXPATHLEN);
Damien Miller5ce662a1999-11-11 17:57:39 +1100172#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100173 if (lstat("/ticket", &st) != -1)
174 tkt_root = "/ticket/";
Damien Miller5ce662a1999-11-11 17:57:39 +1100175#endif /* AFS */
Damien Millercaf6dd62000-08-29 11:33:50 +1100176 snprintf(ticket, MAXPATHLEN, "%s%u_%d", tkt_root, uid, getpid());
Damien Miller95def091999-11-25 00:26:21 +1100177 (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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203}
204
Damien Miller4af51302000-04-16 11:18:38 +1000205int
Damien Miller95def091999-11-25 00:26:21 +1100206auth_krb4(const char *server_user, KTEXT auth, char **client)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207{
Damien Miller95def091999-11-25 00:26:21 +1100208 AUTH_DAT adat = {0};
209 KTEXT_ST reply;
210 char instance[INST_SZ];
211 int r, s;
Damien Miller7684ee12000-03-17 23:40:15 +1100212 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100213 u_int cksum;
214 Key_schedule schedule;
215 struct sockaddr_in local, foreign;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216
Damien Miller95def091999-11-25 00:26:21 +1100217 s = packet_get_connection_in();
218
Damien Miller7684ee12000-03-17 23:40:15 +1100219 slen = sizeof(local);
Damien Miller95def091999-11-25 00:26:21 +1100220 memset(&local, 0, sizeof(local));
Damien Miller7684ee12000-03-17 23:40:15 +1100221 if (getsockname(s, (struct sockaddr *) & local, &slen) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100222 debug("getsockname failed: %.100s", strerror(errno));
Damien Miller7684ee12000-03-17 23:40:15 +1100223 slen = sizeof(foreign);
Damien Miller95def091999-11-25 00:26:21 +1100224 memset(&foreign, 0, sizeof(foreign));
Damien Miller7684ee12000-03-17 23:40:15 +1100225 if (getpeername(s, (struct sockaddr *) & foreign, &slen) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100226 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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274}
275#endif /* KRB4 */
276
277#ifdef AFS
Damien Miller4af51302000-04-16 11:18:38 +1000278int
Damien Miller95def091999-11-25 00:26:21 +1100279auth_kerberos_tgt(struct passwd *pw, const char *string)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280{
Damien Miller95def091999-11-25 00:26:21 +1100281 CREDENTIALS creds;
Damien Miller5ce662a1999-11-11 17:57:39 +1100282
Damien Miller95def091999-11-25 00:26:21 +1100283 if (!radix_to_creds(string, &creds)) {
284 log("Protocol error decoding Kerberos V4 tgt");
285 packet_send_debug("Protocol error decoding Kerberos V4 tgt");
286 goto auth_kerberos_tgt_failure;
287 }
288 if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
289 strlcpy(creds.service, "krbtgt", sizeof creds.service);
290
291 if (strcmp(creds.service, "krbtgt")) {
292 log("Kerberos V4 tgt (%s%s%s@%s) rejected for %s", creds.pname,
293 creds.pinst[0] ? "." : "", creds.pinst, creds.realm,
294 pw->pw_name);
295 packet_send_debug("Kerberos V4 tgt (%s%s%s@%s) rejected for %s",
296 creds.pname, creds.pinst[0] ? "." : "", creds.pinst,
297 creds.realm, pw->pw_name);
298 goto auth_kerberos_tgt_failure;
299 }
300 if (!krb4_init(pw->pw_uid))
301 goto auth_kerberos_tgt_failure;
302
303 if (in_tkt(creds.pname, creds.pinst) != KSUCCESS)
304 goto auth_kerberos_tgt_failure;
305
306 if (save_credentials(creds.service, creds.instance, creds.realm,
307 creds.session, creds.lifetime, creds.kvno,
308 &creds.ticket_st, creds.issue_date) != KSUCCESS) {
309 packet_send_debug("Kerberos V4 tgt refused: couldn't save credentials");
310 goto auth_kerberos_tgt_failure;
311 }
312 /* Successful authentication, passed all checks. */
313 chown(tkt_string(), pw->pw_uid, pw->pw_gid);
314
315 packet_send_debug("Kerberos V4 tgt accepted (%s.%s@%s, %s%s%s@%s)",
316 creds.service, creds.instance, creds.realm, creds.pname,
317 creds.pinst[0] ? "." : "", creds.pinst, creds.realm);
318 memset(&creds, 0, sizeof(creds));
319 packet_start(SSH_SMSG_SUCCESS);
320 packet_send();
321 packet_write_wait();
322 return 1;
323
324auth_kerberos_tgt_failure:
325 krb4_cleanup_proc(NULL);
326 memset(&creds, 0, sizeof(creds));
327 packet_start(SSH_SMSG_FAILURE);
328 packet_send();
329 packet_write_wait();
330 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331}
332
Damien Miller4af51302000-04-16 11:18:38 +1000333int
Damien Miller95def091999-11-25 00:26:21 +1100334auth_afs_token(struct passwd *pw, const char *token_string)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335{
Damien Miller95def091999-11-25 00:26:21 +1100336 CREDENTIALS creds;
337 uid_t uid = pw->pw_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338
Damien Miller95def091999-11-25 00:26:21 +1100339 if (!radix_to_creds(token_string, &creds)) {
340 log("Protocol error decoding AFS token");
341 packet_send_debug("Protocol error decoding AFS token");
342 packet_start(SSH_SMSG_FAILURE);
343 packet_send();
344 packet_write_wait();
345 return 0;
346 }
347 if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
348 strlcpy(creds.service, "afs", sizeof creds.service);
349
350 if (strncmp(creds.pname, "AFS ID ", 7) == 0)
351 uid = atoi(creds.pname + 7);
352
353 if (kafs_settoken(creds.realm, uid, &creds)) {
354 log("AFS token (%s@%s) rejected for %s", creds.pname, creds.realm,
355 pw->pw_name);
356 packet_send_debug("AFS token (%s@%s) rejected for %s", creds.pname,
357 creds.realm, pw->pw_name);
358 memset(&creds, 0, sizeof(creds));
359 packet_start(SSH_SMSG_FAILURE);
360 packet_send();
361 packet_write_wait();
362 return 0;
363 }
364 packet_send_debug("AFS token accepted (%s@%s, %s@%s)", creds.service,
365 creds.realm, creds.pname, creds.realm);
366 memset(&creds, 0, sizeof(creds));
367 packet_start(SSH_SMSG_SUCCESS);
368 packet_send();
369 packet_write_wait();
370 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371}
372#endif /* AFS */