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