blob: 1cc528aa0a9dfb1ff5da92884aca8cae5c3986d6 [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"
Ben Lindstromce0f6342002-06-11 16:42:49 +000026RCSID("$OpenBSD: auth-krb4.c,v 1.27 2002/06/11 05:46:20 mpech Exp $");
Damien Millerbf7f4662000-06-23 10:16:38 +100027
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "ssh.h"
29#include "ssh1.h"
30#include "packet.h"
31#include "xmalloc.h"
32#include "log.h"
33#include "servconf.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000034#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "auth.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036
Ben Lindstromb1985f72001-01-23 00:19:15 +000037#ifdef AFS
38#include "radix.h"
39#endif
40
41#ifdef KRB4
Damien Milleraae6c611999-12-06 11:47:28 +110042extern ServerOptions options;
43
Ben Lindstromec95ed92001-07-04 04:21:14 +000044static int
45krb4_init(void *context)
46{
47 static int cleanup_registered = 0;
48 Authctxt *authctxt = (Authctxt *)context;
49 const char *tkt_root = TKT_ROOT;
50 struct stat st;
51 int fd;
Damien Miller9f0f5c62001-12-21 14:45:46 +110052
Ben Lindstromec95ed92001-07-04 04:21:14 +000053 if (!authctxt->krb4_ticket_file) {
54 /* Set unique ticket string manually since we're still root. */
55 authctxt->krb4_ticket_file = xmalloc(MAXPATHLEN);
56#ifdef AFS
57 if (lstat("/ticket", &st) != -1)
58 tkt_root = "/ticket/";
59#endif /* AFS */
Ben Lindstromce0f6342002-06-11 16:42:49 +000060 snprintf(authctxt->krb4_ticket_file, MAXPATHLEN, "%s%u_%ld",
61 tkt_root, authctxt->pw->pw_uid, (long)getpid());
Ben Lindstromec95ed92001-07-04 04:21:14 +000062 krb_set_tkt_string(authctxt->krb4_ticket_file);
63 }
64 /* Register ticket cleanup in case of fatal error. */
65 if (!cleanup_registered) {
66 fatal_add_cleanup(krb4_cleanup_proc, authctxt);
67 cleanup_registered = 1;
68 }
69 /* Try to create our ticket file. */
70 if ((fd = mkstemp(authctxt->krb4_ticket_file)) != -1) {
71 close(fd);
72 return (1);
73 }
74 /* Ticket file exists - make sure user owns it (just passed ticket). */
75 if (lstat(authctxt->krb4_ticket_file, &st) != -1) {
76 if (st.st_mode == (S_IFREG | S_IRUSR | S_IWUSR) &&
77 st.st_uid == authctxt->pw->pw_uid)
78 return (1);
79 }
80 /* Failure - cancel cleanup function, leaving ticket for inspection. */
81 log("WARNING: bad ticket file %s", authctxt->krb4_ticket_file);
Damien Miller9f0f5c62001-12-21 14:45:46 +110082
Ben Lindstromec95ed92001-07-04 04:21:14 +000083 fatal_remove_cleanup(krb4_cleanup_proc, authctxt);
84 cleanup_registered = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +110085
Ben Lindstromec95ed92001-07-04 04:21:14 +000086 xfree(authctxt->krb4_ticket_file);
87 authctxt->krb4_ticket_file = NULL;
Damien Miller9f0f5c62001-12-21 14:45:46 +110088
Ben Lindstromec95ed92001-07-04 04:21:14 +000089 return (0);
90}
91
Damien Milleraae6c611999-12-06 11:47:28 +110092/*
93 * try krb4 authentication,
94 * return 1 on success, 0 on failure, -1 if krb4 is not available
95 */
Damien Miller4af51302000-04-16 11:18:38 +100096int
Ben Lindstromec95ed92001-07-04 04:21:14 +000097auth_krb4_password(Authctxt *authctxt, const char *password)
Damien Milleraae6c611999-12-06 11:47:28 +110098{
99 AUTH_DAT adata;
100 KTEXT_ST tkt;
101 struct hostent *hp;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000102 struct passwd *pw;
103 char localhost[MAXHOSTNAMELEN], phost[INST_SZ], realm[REALM_SZ];
104 u_int32_t faddr;
Damien Milleraae6c611999-12-06 11:47:28 +1100105 int r;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100106
Ben Lindstromec95ed92001-07-04 04:21:14 +0000107 if ((pw = authctxt->pw) == NULL)
108 return (0);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100109
Damien Milleraae6c611999-12-06 11:47:28 +1100110 /*
111 * Try Kerberos password authentication only for non-root
112 * users and only if Kerberos is installed.
113 */
114 if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {
Damien Milleraae6c611999-12-06 11:47:28 +1100115 /* Set up our ticket file. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000116 if (!krb4_init(authctxt)) {
Damien Milleraae6c611999-12-06 11:47:28 +1100117 log("Couldn't initialize Kerberos ticket file for %s!",
118 pw->pw_name);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000119 goto failure;
Damien Milleraae6c611999-12-06 11:47:28 +1100120 }
121 /* Try to get TGT using our password. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000122 r = krb_get_pw_in_tkt((char *) pw->pw_name, "", realm,
123 "krbtgt", realm, DEFAULT_TKT_LIFE, (char *)password);
Damien Milleraae6c611999-12-06 11:47:28 +1100124 if (r != INTK_OK) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000125 debug("Kerberos v4 password authentication for %s "
126 "failed: %s", pw->pw_name, krb_err_txt[r]);
127 goto failure;
Damien Milleraae6c611999-12-06 11:47:28 +1100128 }
129 /* Successful authentication. */
130 chown(tkt_string(), pw->pw_uid, pw->pw_gid);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100131
Damien Milleraae6c611999-12-06 11:47:28 +1100132 /*
133 * Now that we have a TGT, try to get a local
134 * "rcmd" ticket to ensure that we are not talking
135 * to a bogus Kerberos server.
136 */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000137 gethostname(localhost, sizeof(localhost));
138 strlcpy(phost, (char *)krb_get_phost(localhost),
139 sizeof(phost));
Damien Milleraae6c611999-12-06 11:47:28 +1100140 r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100141
Damien Milleraae6c611999-12-06 11:47:28 +1100142 if (r == KSUCCESS) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000143 if ((hp = gethostbyname(localhost)) == NULL) {
Damien Milleraae6c611999-12-06 11:47:28 +1100144 log("Couldn't get local host address!");
Ben Lindstromec95ed92001-07-04 04:21:14 +0000145 goto failure;
Damien Milleraae6c611999-12-06 11:47:28 +1100146 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000147 memmove((void *)&faddr, (void *)hp->h_addr,
Damien Milleraae6c611999-12-06 11:47:28 +1100148 sizeof(faddr));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100149
Damien Milleraae6c611999-12-06 11:47:28 +1100150 /* Verify our "rcmd" ticket. */
151 r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost,
152 faddr, &adata, "");
153 if (r == RD_AP_UNDEC) {
154 /*
155 * Probably didn't have a srvtab on
Damien Miller942da032000-08-18 13:59:06 +1000156 * localhost. Disallow login.
Damien Milleraae6c611999-12-06 11:47:28 +1100157 */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000158 log("Kerberos v4 TGT for %s unverifiable, "
Damien Milleraae6c611999-12-06 11:47:28 +1100159 "no srvtab installed? krb_rd_req: %s",
160 pw->pw_name, krb_err_txt[r]);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000161 goto failure;
Damien Milleraae6c611999-12-06 11:47:28 +1100162 } else if (r != KSUCCESS) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000163 log("Kerberos v4 %s ticket unverifiable: %s",
Damien Milleraae6c611999-12-06 11:47:28 +1100164 KRB4_SERVICE_NAME, krb_err_txt[r]);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000165 goto failure;
Damien Milleraae6c611999-12-06 11:47:28 +1100166 }
167 } else if (r == KDC_PR_UNKNOWN) {
168 /*
Damien Miller942da032000-08-18 13:59:06 +1000169 * Disallow login if no rcmd service exists, and
Damien Milleraae6c611999-12-06 11:47:28 +1100170 * log the error.
171 */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000172 log("Kerberos v4 TGT for %s unverifiable: %s; %s.%s "
Damien Milleraae6c611999-12-06 11:47:28 +1100173 "not registered, or srvtab is wrong?", pw->pw_name,
Ben Lindstromec95ed92001-07-04 04:21:14 +0000174 krb_err_txt[r], KRB4_SERVICE_NAME, phost);
175 goto failure;
Damien Milleraae6c611999-12-06 11:47:28 +1100176 } else {
177 /*
178 * TGT is bad, forget it. Possibly spoofed!
179 */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000180 debug("WARNING: Kerberos v4 TGT possibly spoofed "
181 "for %s: %s", pw->pw_name, krb_err_txt[r]);
182 goto failure;
Damien Milleraae6c611999-12-06 11:47:28 +1100183 }
Damien Milleraae6c611999-12-06 11:47:28 +1100184 /* Authentication succeeded. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000185 return (1);
186 } else
Damien Milleraae6c611999-12-06 11:47:28 +1100187 /* Logging in as root or no local Kerberos realm. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000188 debug("Unable to authenticate to Kerberos.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100189
Ben Lindstromec95ed92001-07-04 04:21:14 +0000190 failure:
191 krb4_cleanup_proc(authctxt);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100192
Ben Lindstromec95ed92001-07-04 04:21:14 +0000193 if (!options.kerberos_or_local_passwd)
194 return (0);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100195
Damien Milleraae6c611999-12-06 11:47:28 +1100196 /* Fall back to ordinary passwd authentication. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000197 return (-1);
Damien Milleraae6c611999-12-06 11:47:28 +1100198}
199
Damien Miller5ce662a1999-11-11 17:57:39 +1100200void
Ben Lindstromec95ed92001-07-04 04:21:14 +0000201krb4_cleanup_proc(void *context)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202{
Ben Lindstromec95ed92001-07-04 04:21:14 +0000203 Authctxt *authctxt = (Authctxt *)context;
Damien Miller95def091999-11-25 00:26:21 +1100204 debug("krb4_cleanup_proc called");
Ben Lindstromec95ed92001-07-04 04:21:14 +0000205 if (authctxt->krb4_ticket_file) {
Damien Miller95def091999-11-25 00:26:21 +1100206 (void) dest_tkt();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000207 xfree(authctxt->krb4_ticket_file);
208 authctxt->krb4_ticket_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100209 }
Damien Miller5ce662a1999-11-11 17:57:39 +1100210}
211
Damien Miller4af51302000-04-16 11:18:38 +1000212int
Ben Lindstromec95ed92001-07-04 04:21:14 +0000213auth_krb4(Authctxt *authctxt, KTEXT auth, char **client)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214{
Damien Miller95def091999-11-25 00:26:21 +1100215 AUTH_DAT adat = {0};
216 KTEXT_ST reply;
Damien Miller95def091999-11-25 00:26:21 +1100217 Key_schedule schedule;
218 struct sockaddr_in local, foreign;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000219 char instance[INST_SZ];
220 socklen_t slen;
221 u_int cksum;
222 int r, s;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100223
Damien Miller95def091999-11-25 00:26:21 +1100224 s = packet_get_connection_in();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100225
Damien Miller7684ee12000-03-17 23:40:15 +1100226 slen = sizeof(local);
Damien Miller95def091999-11-25 00:26:21 +1100227 memset(&local, 0, sizeof(local));
Damien Miller7684ee12000-03-17 23:40:15 +1100228 if (getsockname(s, (struct sockaddr *) & local, &slen) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100229 debug("getsockname failed: %.100s", strerror(errno));
Damien Miller7684ee12000-03-17 23:40:15 +1100230 slen = sizeof(foreign);
Damien Miller95def091999-11-25 00:26:21 +1100231 memset(&foreign, 0, sizeof(foreign));
Damien Miller7684ee12000-03-17 23:40:15 +1100232 if (getpeername(s, (struct sockaddr *) & foreign, &slen) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100233 debug("getpeername failed: %.100s", strerror(errno));
234 fatal_cleanup();
235 }
236 instance[0] = '*';
237 instance[1] = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100238
Damien Miller95def091999-11-25 00:26:21 +1100239 /* Get the encrypted request, challenge, and session key. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000240 if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance,
241 0, &adat, ""))) {
242 debug("Kerberos v4 krb_rd_req: %.100s", krb_err_txt[r]);
243 return (0);
Damien Miller95def091999-11-25 00:26:21 +1100244 }
245 des_key_sched((des_cblock *) adat.session, schedule);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100246
Damien Miller95def091999-11-25 00:26:21 +1100247 *client = xmalloc(MAX_K_NAME_SZ);
248 (void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname,
249 *adat.pinst ? "." : "", adat.pinst, adat.prealm);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100250
Damien Miller95def091999-11-25 00:26:21 +1100251 /* Check ~/.klogin authorization now. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000252 if (kuserok(&adat, authctxt->user) != KSUCCESS) {
253 log("Kerberos v4 .klogin authorization failed for %s to "
254 "account %s", *client, authctxt->user);
Damien Miller95def091999-11-25 00:26:21 +1100255 xfree(*client);
Ben Lindstromabf31442002-03-22 01:30:40 +0000256 *client = NULL;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000257 return (0);
Damien Miller95def091999-11-25 00:26:21 +1100258 }
259 /* Increment the checksum, and return it encrypted with the
260 session key. */
261 cksum = adat.checksum + 1;
262 cksum = htonl(cksum);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100263
Damien Miller95def091999-11-25 00:26:21 +1100264 /* If we can't successfully encrypt the checksum, we send back an
265 empty message, admitting our failure. */
266 if ((r = krb_mk_priv((u_char *) & cksum, reply.dat, sizeof(cksum) + 1,
267 schedule, &adat.session, &local, &foreign)) < 0) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000268 debug("Kerberos v4 mk_priv: (%d) %s", r, krb_err_txt[r]);
Damien Miller95def091999-11-25 00:26:21 +1100269 reply.dat[0] = 0;
270 reply.length = 0;
271 } else
272 reply.length = r;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100273
Damien Miller95def091999-11-25 00:26:21 +1100274 /* Clear session key. */
275 memset(&adat.session, 0, sizeof(&adat.session));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100276
Damien Miller95def091999-11-25 00:26:21 +1100277 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
278 packet_put_string((char *) reply.dat, reply.length);
279 packet_send();
280 packet_write_wait();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000281 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282}
283#endif /* KRB4 */
284
285#ifdef AFS
Damien Miller4af51302000-04-16 11:18:38 +1000286int
Ben Lindstromec95ed92001-07-04 04:21:14 +0000287auth_krb4_tgt(Authctxt *authctxt, const char *string)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288{
Damien Miller95def091999-11-25 00:26:21 +1100289 CREDENTIALS creds;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000290 struct passwd *pw;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100291
Ben Lindstromec95ed92001-07-04 04:21:14 +0000292 if ((pw = authctxt->pw) == NULL)
293 goto failure;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100294
Ben Lindstromec95ed92001-07-04 04:21:14 +0000295 temporarily_use_uid(pw);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100296
Damien Miller95def091999-11-25 00:26:21 +1100297 if (!radix_to_creds(string, &creds)) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000298 log("Protocol error decoding Kerberos v4 TGT");
299 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100300 }
301 if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
302 strlcpy(creds.service, "krbtgt", sizeof creds.service);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100303
Damien Miller95def091999-11-25 00:26:21 +1100304 if (strcmp(creds.service, "krbtgt")) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000305 log("Kerberos v4 TGT (%s%s%s@%s) rejected for %s",
Damien Miller95def091999-11-25 00:26:21 +1100306 creds.pname, creds.pinst[0] ? "." : "", creds.pinst,
307 creds.realm, pw->pw_name);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000308 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100309 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000310 if (!krb4_init(authctxt))
311 goto failure;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100312
Damien Miller95def091999-11-25 00:26:21 +1100313 if (in_tkt(creds.pname, creds.pinst) != KSUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000314 goto failure;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100315
Damien Miller95def091999-11-25 00:26:21 +1100316 if (save_credentials(creds.service, creds.instance, creds.realm,
Ben Lindstromec95ed92001-07-04 04:21:14 +0000317 creds.session, creds.lifetime, creds.kvno, &creds.ticket_st,
318 creds.issue_date) != KSUCCESS) {
319 debug("Kerberos v4 TGT refused: couldn't save credentials");
320 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100321 }
322 /* Successful authentication, passed all checks. */
323 chown(tkt_string(), pw->pw_uid, pw->pw_gid);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100324
Ben Lindstromec95ed92001-07-04 04:21:14 +0000325 debug("Kerberos v4 TGT accepted (%s%s%s@%s)",
326 creds.pname, creds.pinst[0] ? "." : "", creds.pinst, creds.realm);
Damien Miller95def091999-11-25 00:26:21 +1100327 memset(&creds, 0, sizeof(creds));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100328
Ben Lindstromec95ed92001-07-04 04:21:14 +0000329 restore_uid();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100330
Ben Lindstromec95ed92001-07-04 04:21:14 +0000331 return (1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100332
Ben Lindstromec95ed92001-07-04 04:21:14 +0000333 failure:
334 krb4_cleanup_proc(authctxt);
Damien Miller95def091999-11-25 00:26:21 +1100335 memset(&creds, 0, sizeof(creds));
Ben Lindstromec95ed92001-07-04 04:21:14 +0000336 restore_uid();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100337
Ben Lindstromec95ed92001-07-04 04:21:14 +0000338 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339}
340
Damien Miller4af51302000-04-16 11:18:38 +1000341int
Ben Lindstromec95ed92001-07-04 04:21:14 +0000342auth_afs_token(Authctxt *authctxt, const char *token_string)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343{
Damien Miller95def091999-11-25 00:26:21 +1100344 CREDENTIALS creds;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000345 struct passwd *pw;
Damien Miller874d77b2000-10-14 16:23:11 +1100346 uid_t uid;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100347
Ben Lindstromec95ed92001-07-04 04:21:14 +0000348 if ((pw = authctxt->pw) == NULL)
349 return (0);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100350
Damien Miller95def091999-11-25 00:26:21 +1100351 if (!radix_to_creds(token_string, &creds)) {
352 log("Protocol error decoding AFS token");
Ben Lindstromec95ed92001-07-04 04:21:14 +0000353 return (0);
Damien Miller95def091999-11-25 00:26:21 +1100354 }
355 if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
356 strlcpy(creds.service, "afs", sizeof creds.service);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100357
Damien Miller95def091999-11-25 00:26:21 +1100358 if (strncmp(creds.pname, "AFS ID ", 7) == 0)
359 uid = atoi(creds.pname + 7);
Damien Miller874d77b2000-10-14 16:23:11 +1100360 else
361 uid = pw->pw_uid;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100362
Damien Miller95def091999-11-25 00:26:21 +1100363 if (kafs_settoken(creds.realm, uid, &creds)) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000364 log("AFS token (%s@%s) rejected for %s",
365 creds.pname, creds.realm, pw->pw_name);
Damien Miller95def091999-11-25 00:26:21 +1100366 memset(&creds, 0, sizeof(creds));
Ben Lindstromec95ed92001-07-04 04:21:14 +0000367 return (0);
Damien Miller95def091999-11-25 00:26:21 +1100368 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000369 debug("AFS token accepted (%s@%s)", creds.pname, creds.realm);
Damien Miller95def091999-11-25 00:26:21 +1100370 memset(&creds, 0, sizeof(creds));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100371
Ben Lindstromec95ed92001-07-04 04:21:14 +0000372 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373}
374#endif /* AFS */