Annotation of src/usr.bin/ssh/auth-krb4.c, Revision 1.26.2.1
1.1 deraadt 1: /*
1.18 deraadt 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.
1.10 deraadt 23: */
1.1 deraadt 24:
25: #include "includes.h"
1.26.2.1! jason 26: RCSID("$OpenBSD: auth-krb4.c,v 1.27 2002/06/11 05:46:20 mpech Exp $");
1.22 markus 27:
28: #include "ssh.h"
29: #include "ssh1.h"
1.1 deraadt 30: #include "packet.h"
31: #include "xmalloc.h"
1.22 markus 32: #include "log.h"
1.11 markus 33: #include "servconf.h"
1.24 dugsong 34: #include "uidswap.h"
1.22 markus 35: #include "auth.h"
1.23 markus 36:
37: #ifdef AFS
1.22 markus 38: #include "radix.h"
1.23 markus 39: #endif
1.15 djm 40:
1.1 deraadt 41: #ifdef KRB4
1.24 dugsong 42: extern ServerOptions options;
1.11 markus 43:
1.24 dugsong 44: static int
45: krb4_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;
1.25 deraadt 52:
1.24 dugsong 53: 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 */
1.26.2.1! jason 60: snprintf(authctxt->krb4_ticket_file, MAXPATHLEN, "%s%u_%ld",
! 61: tkt_root, authctxt->pw->pw_uid, (long)getpid());
1.24 dugsong 62: 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);
1.25 deraadt 82:
1.24 dugsong 83: fatal_remove_cleanup(krb4_cleanup_proc, authctxt);
84: cleanup_registered = 0;
1.25 deraadt 85:
1.24 dugsong 86: xfree(authctxt->krb4_ticket_file);
87: authctxt->krb4_ticket_file = NULL;
1.25 deraadt 88:
1.24 dugsong 89: return (0);
90: }
1.11 markus 91:
92: /*
93: * try krb4 authentication,
94: * return 1 on success, 0 on failure, -1 if krb4 is not available
95: */
1.14 markus 96: int
1.24 dugsong 97: auth_krb4_password(Authctxt *authctxt, const char *password)
1.11 markus 98: {
99: AUTH_DAT adata;
100: KTEXT_ST tkt;
101: struct hostent *hp;
1.24 dugsong 102: struct passwd *pw;
103: char localhost[MAXHOSTNAMELEN], phost[INST_SZ], realm[REALM_SZ];
104: u_int32_t faddr;
1.11 markus 105: int r;
1.25 deraadt 106:
1.24 dugsong 107: if ((pw = authctxt->pw) == NULL)
108: return (0);
1.25 deraadt 109:
1.11 markus 110: /*
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) {
115: /* Set up our ticket file. */
1.24 dugsong 116: if (!krb4_init(authctxt)) {
1.11 markus 117: log("Couldn't initialize Kerberos ticket file for %s!",
118: pw->pw_name);
1.24 dugsong 119: goto failure;
1.11 markus 120: }
121: /* Try to get TGT using our password. */
1.24 dugsong 122: r = krb_get_pw_in_tkt((char *) pw->pw_name, "", realm,
123: "krbtgt", realm, DEFAULT_TKT_LIFE, (char *)password);
1.11 markus 124: if (r != INTK_OK) {
1.24 dugsong 125: debug("Kerberos v4 password authentication for %s "
126: "failed: %s", pw->pw_name, krb_err_txt[r]);
127: goto failure;
1.11 markus 128: }
129: /* Successful authentication. */
130: chown(tkt_string(), pw->pw_uid, pw->pw_gid);
1.25 deraadt 131:
1.11 markus 132: /*
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: */
1.24 dugsong 137: gethostname(localhost, sizeof(localhost));
138: strlcpy(phost, (char *)krb_get_phost(localhost),
139: sizeof(phost));
1.11 markus 140: r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
1.25 deraadt 141:
1.11 markus 142: if (r == KSUCCESS) {
1.24 dugsong 143: if ((hp = gethostbyname(localhost)) == NULL) {
1.11 markus 144: log("Couldn't get local host address!");
1.24 dugsong 145: goto failure;
1.11 markus 146: }
1.24 dugsong 147: memmove((void *)&faddr, (void *)hp->h_addr,
1.11 markus 148: sizeof(faddr));
1.25 deraadt 149:
1.11 markus 150: /* 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
1.16 provos 156: * localhost. Disallow login.
1.11 markus 157: */
1.24 dugsong 158: log("Kerberos v4 TGT for %s unverifiable, "
1.11 markus 159: "no srvtab installed? krb_rd_req: %s",
160: pw->pw_name, krb_err_txt[r]);
1.24 dugsong 161: goto failure;
1.11 markus 162: } else if (r != KSUCCESS) {
1.24 dugsong 163: log("Kerberos v4 %s ticket unverifiable: %s",
1.11 markus 164: KRB4_SERVICE_NAME, krb_err_txt[r]);
1.24 dugsong 165: goto failure;
1.11 markus 166: }
167: } else if (r == KDC_PR_UNKNOWN) {
168: /*
1.16 provos 169: * Disallow login if no rcmd service exists, and
1.11 markus 170: * log the error.
171: */
1.24 dugsong 172: log("Kerberos v4 TGT for %s unverifiable: %s; %s.%s "
1.11 markus 173: "not registered, or srvtab is wrong?", pw->pw_name,
1.24 dugsong 174: krb_err_txt[r], KRB4_SERVICE_NAME, phost);
175: goto failure;
1.11 markus 176: } else {
177: /*
178: * TGT is bad, forget it. Possibly spoofed!
179: */
1.24 dugsong 180: debug("WARNING: Kerberos v4 TGT possibly spoofed "
181: "for %s: %s", pw->pw_name, krb_err_txt[r]);
182: goto failure;
1.11 markus 183: }
184: /* Authentication succeeded. */
1.24 dugsong 185: return (1);
186: } else
1.11 markus 187: /* Logging in as root or no local Kerberos realm. */
1.24 dugsong 188: debug("Unable to authenticate to Kerberos.");
1.25 deraadt 189:
1.24 dugsong 190: failure:
191: krb4_cleanup_proc(authctxt);
1.25 deraadt 192:
1.24 dugsong 193: if (!options.kerberos_or_local_passwd)
194: return (0);
1.25 deraadt 195:
1.11 markus 196: /* Fall back to ordinary passwd authentication. */
1.24 dugsong 197: return (-1);
1.11 markus 198: }
1.6 markus 199:
200: void
1.24 dugsong 201: krb4_cleanup_proc(void *context)
1.1 deraadt 202: {
1.24 dugsong 203: Authctxt *authctxt = (Authctxt *)context;
1.9 markus 204: debug("krb4_cleanup_proc called");
1.24 dugsong 205: if (authctxt->krb4_ticket_file) {
1.9 markus 206: (void) dest_tkt();
1.24 dugsong 207: xfree(authctxt->krb4_ticket_file);
208: authctxt->krb4_ticket_file = NULL;
1.9 markus 209: }
1.1 deraadt 210: }
211:
1.14 markus 212: int
1.24 dugsong 213: auth_krb4(Authctxt *authctxt, KTEXT auth, char **client)
1.1 deraadt 214: {
1.9 markus 215: AUTH_DAT adat = {0};
216: KTEXT_ST reply;
1.24 dugsong 217: Key_schedule schedule;
218: struct sockaddr_in local, foreign;
1.9 markus 219: char instance[INST_SZ];
1.12 markus 220: socklen_t slen;
1.9 markus 221: u_int cksum;
1.24 dugsong 222: int r, s;
1.25 deraadt 223:
1.9 markus 224: s = packet_get_connection_in();
1.25 deraadt 225:
1.12 markus 226: slen = sizeof(local);
1.9 markus 227: memset(&local, 0, sizeof(local));
1.12 markus 228: if (getsockname(s, (struct sockaddr *) & local, &slen) < 0)
1.9 markus 229: debug("getsockname failed: %.100s", strerror(errno));
1.12 markus 230: slen = sizeof(foreign);
1.9 markus 231: memset(&foreign, 0, sizeof(foreign));
1.12 markus 232: if (getpeername(s, (struct sockaddr *) & foreign, &slen) < 0) {
1.9 markus 233: debug("getpeername failed: %.100s", strerror(errno));
234: fatal_cleanup();
235: }
236: instance[0] = '*';
237: instance[1] = 0;
1.25 deraadt 238:
1.9 markus 239: /* Get the encrypted request, challenge, and session key. */
1.24 dugsong 240: 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);
1.9 markus 244: }
245: des_key_sched((des_cblock *) adat.session, schedule);
1.25 deraadt 246:
1.9 markus 247: *client = xmalloc(MAX_K_NAME_SZ);
248: (void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname,
1.10 deraadt 249: *adat.pinst ? "." : "", adat.pinst, adat.prealm);
1.25 deraadt 250:
1.9 markus 251: /* Check ~/.klogin authorization now. */
1.24 dugsong 252: if (kuserok(&adat, authctxt->user) != KSUCCESS) {
253: log("Kerberos v4 .klogin authorization failed for %s to "
254: "account %s", *client, authctxt->user);
1.9 markus 255: xfree(*client);
1.26 dugsong 256: *client = NULL;
1.24 dugsong 257: return (0);
1.9 markus 258: }
259: /* Increment the checksum, and return it encrypted with the
260: session key. */
261: cksum = adat.checksum + 1;
262: cksum = htonl(cksum);
1.25 deraadt 263:
1.9 markus 264: /* 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,
1.10 deraadt 267: schedule, &adat.session, &local, &foreign)) < 0) {
1.24 dugsong 268: debug("Kerberos v4 mk_priv: (%d) %s", r, krb_err_txt[r]);
1.9 markus 269: reply.dat[0] = 0;
270: reply.length = 0;
271: } else
272: reply.length = r;
1.25 deraadt 273:
1.9 markus 274: /* Clear session key. */
275: memset(&adat.session, 0, sizeof(&adat.session));
1.25 deraadt 276:
1.9 markus 277: packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
278: packet_put_string((char *) reply.dat, reply.length);
279: packet_send();
280: packet_write_wait();
1.24 dugsong 281: return (1);
1.1 deraadt 282: }
283: #endif /* KRB4 */
284:
285: #ifdef AFS
1.14 markus 286: int
1.24 dugsong 287: auth_krb4_tgt(Authctxt *authctxt, const char *string)
1.1 deraadt 288: {
1.9 markus 289: CREDENTIALS creds;
1.24 dugsong 290: struct passwd *pw;
1.25 deraadt 291:
1.24 dugsong 292: if ((pw = authctxt->pw) == NULL)
293: goto failure;
1.25 deraadt 294:
1.24 dugsong 295: temporarily_use_uid(pw);
1.25 deraadt 296:
1.9 markus 297: if (!radix_to_creds(string, &creds)) {
1.24 dugsong 298: log("Protocol error decoding Kerberos v4 TGT");
299: goto failure;
1.9 markus 300: }
301: if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
302: strlcpy(creds.service, "krbtgt", sizeof creds.service);
1.25 deraadt 303:
1.9 markus 304: if (strcmp(creds.service, "krbtgt")) {
1.24 dugsong 305: log("Kerberos v4 TGT (%s%s%s@%s) rejected for %s",
1.10 deraadt 306: creds.pname, creds.pinst[0] ? "." : "", creds.pinst,
307: creds.realm, pw->pw_name);
1.24 dugsong 308: goto failure;
1.9 markus 309: }
1.24 dugsong 310: if (!krb4_init(authctxt))
311: goto failure;
1.25 deraadt 312:
1.9 markus 313: if (in_tkt(creds.pname, creds.pinst) != KSUCCESS)
1.24 dugsong 314: goto failure;
1.25 deraadt 315:
1.9 markus 316: if (save_credentials(creds.service, creds.instance, creds.realm,
1.24 dugsong 317: 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;
1.9 markus 321: }
322: /* Successful authentication, passed all checks. */
323: chown(tkt_string(), pw->pw_uid, pw->pw_gid);
1.25 deraadt 324:
1.24 dugsong 325: debug("Kerberos v4 TGT accepted (%s%s%s@%s)",
326: creds.pname, creds.pinst[0] ? "." : "", creds.pinst, creds.realm);
1.9 markus 327: memset(&creds, 0, sizeof(creds));
1.25 deraadt 328:
1.24 dugsong 329: restore_uid();
1.25 deraadt 330:
1.24 dugsong 331: return (1);
1.25 deraadt 332:
1.24 dugsong 333: failure:
334: krb4_cleanup_proc(authctxt);
1.9 markus 335: memset(&creds, 0, sizeof(creds));
1.24 dugsong 336: restore_uid();
1.25 deraadt 337:
1.24 dugsong 338: return (0);
1.1 deraadt 339: }
340:
1.14 markus 341: int
1.24 dugsong 342: auth_afs_token(Authctxt *authctxt, const char *token_string)
1.1 deraadt 343: {
1.9 markus 344: CREDENTIALS creds;
1.24 dugsong 345: struct passwd *pw;
1.19 markus 346: uid_t uid;
1.25 deraadt 347:
1.24 dugsong 348: if ((pw = authctxt->pw) == NULL)
349: return (0);
1.25 deraadt 350:
1.9 markus 351: if (!radix_to_creds(token_string, &creds)) {
352: log("Protocol error decoding AFS token");
1.24 dugsong 353: return (0);
1.9 markus 354: }
355: if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
356: strlcpy(creds.service, "afs", sizeof creds.service);
1.25 deraadt 357:
1.9 markus 358: if (strncmp(creds.pname, "AFS ID ", 7) == 0)
359: uid = atoi(creds.pname + 7);
1.19 markus 360: else
361: uid = pw->pw_uid;
1.25 deraadt 362:
1.9 markus 363: if (kafs_settoken(creds.realm, uid, &creds)) {
1.24 dugsong 364: log("AFS token (%s@%s) rejected for %s",
365: creds.pname, creds.realm, pw->pw_name);
1.9 markus 366: memset(&creds, 0, sizeof(creds));
1.24 dugsong 367: return (0);
1.9 markus 368: }
1.24 dugsong 369: debug("AFS token accepted (%s@%s)", creds.pname, creds.realm);
1.9 markus 370: memset(&creds, 0, sizeof(creds));
1.25 deraadt 371:
1.24 dugsong 372: return (1);
1.1 deraadt 373: }
374: #endif /* AFS */