Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Dug Song <dugsong@UMICH.EDU> |
| 3 | * Kerberos v4 authentication and ticket-passing routines. |
| 4 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 5 | |
| 6 | #include "includes.h" |
| 7 | #include "packet.h" |
| 8 | #include "xmalloc.h" |
| 9 | #include "ssh.h" |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 10 | #include "servconf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 11 | |
| 12 | #ifdef KRB4 |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 13 | char *ticket = NULL; |
| 14 | |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 15 | extern ServerOptions options; |
| 16 | |
| 17 | /* |
| 18 | * try krb4 authentication, |
| 19 | * return 1 on success, 0 on failure, -1 if krb4 is not available |
| 20 | */ |
| 21 | |
| 22 | int |
| 23 | auth_krb4_password(struct passwd * pw, const char *password) |
| 24 | { |
| 25 | AUTH_DAT adata; |
| 26 | KTEXT_ST tkt; |
| 27 | struct hostent *hp; |
| 28 | unsigned long faddr; |
| 29 | char localhost[MAXHOSTNAMELEN]; |
| 30 | char phost[INST_SZ]; |
| 31 | char realm[REALM_SZ]; |
| 32 | int r; |
| 33 | |
| 34 | /* |
| 35 | * Try Kerberos password authentication only for non-root |
| 36 | * users and only if Kerberos is installed. |
| 37 | */ |
| 38 | if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) { |
| 39 | |
| 40 | /* Set up our ticket file. */ |
| 41 | if (!krb4_init(pw->pw_uid)) { |
| 42 | log("Couldn't initialize Kerberos ticket file for %s!", |
| 43 | pw->pw_name); |
| 44 | goto kerberos_auth_failure; |
| 45 | } |
| 46 | /* Try to get TGT using our password. */ |
| 47 | r = krb_get_pw_in_tkt((char *) pw->pw_name, "", |
| 48 | realm, "krbtgt", realm, |
| 49 | DEFAULT_TKT_LIFE, (char *) password); |
| 50 | if (r != INTK_OK) { |
| 51 | packet_send_debug("Kerberos V4 password " |
| 52 | "authentication for %s failed: %s", |
| 53 | pw->pw_name, krb_err_txt[r]); |
| 54 | goto kerberos_auth_failure; |
| 55 | } |
| 56 | /* Successful authentication. */ |
| 57 | chown(tkt_string(), pw->pw_uid, pw->pw_gid); |
| 58 | |
| 59 | /* |
| 60 | * Now that we have a TGT, try to get a local |
| 61 | * "rcmd" ticket to ensure that we are not talking |
| 62 | * to a bogus Kerberos server. |
| 63 | */ |
| 64 | (void) gethostname(localhost, sizeof(localhost)); |
| 65 | (void) strlcpy(phost, (char *) krb_get_phost(localhost), |
| 66 | INST_SZ); |
| 67 | r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33); |
| 68 | |
| 69 | if (r == KSUCCESS) { |
| 70 | if (!(hp = gethostbyname(localhost))) { |
| 71 | log("Couldn't get local host address!"); |
| 72 | goto kerberos_auth_failure; |
| 73 | } |
| 74 | memmove((void *) &faddr, (void *) hp->h_addr, |
| 75 | sizeof(faddr)); |
| 76 | |
| 77 | /* Verify our "rcmd" ticket. */ |
| 78 | r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost, |
| 79 | faddr, &adata, ""); |
| 80 | if (r == RD_AP_UNDEC) { |
| 81 | /* |
| 82 | * Probably didn't have a srvtab on |
| 83 | * localhost. Allow login. |
| 84 | */ |
| 85 | log("Kerberos V4 TGT for %s unverifiable, " |
| 86 | "no srvtab installed? krb_rd_req: %s", |
| 87 | pw->pw_name, krb_err_txt[r]); |
| 88 | } else if (r != KSUCCESS) { |
| 89 | log("Kerberos V4 %s ticket unverifiable: %s", |
| 90 | KRB4_SERVICE_NAME, krb_err_txt[r]); |
| 91 | goto kerberos_auth_failure; |
| 92 | } |
| 93 | } else if (r == KDC_PR_UNKNOWN) { |
| 94 | /* |
| 95 | * Allow login if no rcmd service exists, but |
| 96 | * log the error. |
| 97 | */ |
| 98 | log("Kerberos V4 TGT for %s unverifiable: %s; %s.%s " |
| 99 | "not registered, or srvtab is wrong?", pw->pw_name, |
| 100 | krb_err_txt[r], KRB4_SERVICE_NAME, phost); |
| 101 | } else { |
| 102 | /* |
| 103 | * TGT is bad, forget it. Possibly spoofed! |
| 104 | */ |
| 105 | packet_send_debug("WARNING: Kerberos V4 TGT " |
| 106 | "possibly spoofed for %s: %s", |
| 107 | pw->pw_name, krb_err_txt[r]); |
| 108 | goto kerberos_auth_failure; |
| 109 | } |
| 110 | |
| 111 | /* Authentication succeeded. */ |
| 112 | return 1; |
| 113 | |
| 114 | kerberos_auth_failure: |
| 115 | krb4_cleanup_proc(NULL); |
| 116 | |
| 117 | if (!options.kerberos_or_local_passwd) |
| 118 | return 0; |
| 119 | } else { |
| 120 | /* Logging in as root or no local Kerberos realm. */ |
| 121 | packet_send_debug("Unable to authenticate to Kerberos."); |
| 122 | } |
| 123 | /* Fall back to ordinary passwd authentication. */ |
| 124 | return -1; |
| 125 | } |
| 126 | |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 127 | void |
| 128 | krb4_cleanup_proc(void *ignore) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 130 | debug("krb4_cleanup_proc called"); |
| 131 | if (ticket) { |
| 132 | (void) dest_tkt(); |
| 133 | xfree(ticket); |
| 134 | ticket = NULL; |
| 135 | } |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 136 | } |
| 137 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 138 | int |
| 139 | krb4_init(uid_t uid) |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 140 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | static int cleanup_registered = 0; |
| 142 | char *tkt_root = TKT_ROOT; |
| 143 | struct stat st; |
| 144 | int fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 145 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | if (!ticket) { |
| 147 | /* Set unique ticket string manually since we're still root. */ |
| 148 | ticket = xmalloc(MAXPATHLEN); |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 149 | #ifdef AFS |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | if (lstat("/ticket", &st) != -1) |
| 151 | tkt_root = "/ticket/"; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 152 | #endif /* AFS */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 153 | snprintf(ticket, MAXPATHLEN, "%s%d_%d", tkt_root, uid, getpid()); |
| 154 | (void) krb_set_tkt_string(ticket); |
| 155 | } |
| 156 | /* Register ticket cleanup in case of fatal error. */ |
| 157 | if (!cleanup_registered) { |
| 158 | fatal_add_cleanup(krb4_cleanup_proc, NULL); |
| 159 | cleanup_registered = 1; |
| 160 | } |
| 161 | /* Try to create our ticket file. */ |
| 162 | if ((fd = mkstemp(ticket)) != -1) { |
| 163 | close(fd); |
| 164 | return 1; |
| 165 | } |
| 166 | /* Ticket file exists - make sure user owns it (just passed ticket). */ |
| 167 | if (lstat(ticket, &st) != -1) { |
| 168 | if (st.st_mode == (S_IFREG | S_IRUSR | S_IWUSR) && |
| 169 | st.st_uid == uid) |
| 170 | return 1; |
| 171 | } |
| 172 | /* Failure - cancel cleanup function, leaving bad ticket for inspection. */ |
| 173 | log("WARNING: bad ticket file %s", ticket); |
| 174 | fatal_remove_cleanup(krb4_cleanup_proc, NULL); |
| 175 | cleanup_registered = 0; |
| 176 | xfree(ticket); |
| 177 | ticket = NULL; |
| 178 | |
| 179 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 180 | } |
| 181 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 182 | int |
| 183 | auth_krb4(const char *server_user, KTEXT auth, char **client) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 184 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 185 | AUTH_DAT adat = {0}; |
| 186 | KTEXT_ST reply; |
| 187 | char instance[INST_SZ]; |
| 188 | int r, s; |
| 189 | u_int cksum; |
| 190 | Key_schedule schedule; |
| 191 | struct sockaddr_in local, foreign; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 192 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 193 | s = packet_get_connection_in(); |
| 194 | |
| 195 | r = sizeof(local); |
| 196 | memset(&local, 0, sizeof(local)); |
| 197 | if (getsockname(s, (struct sockaddr *) & local, &r) < 0) |
| 198 | debug("getsockname failed: %.100s", strerror(errno)); |
| 199 | r = sizeof(foreign); |
| 200 | memset(&foreign, 0, sizeof(foreign)); |
| 201 | if (getpeername(s, (struct sockaddr *) & foreign, &r) < 0) { |
| 202 | debug("getpeername failed: %.100s", strerror(errno)); |
| 203 | fatal_cleanup(); |
| 204 | } |
| 205 | instance[0] = '*'; |
| 206 | instance[1] = 0; |
| 207 | |
| 208 | /* Get the encrypted request, challenge, and session key. */ |
| 209 | if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance, 0, &adat, ""))) { |
| 210 | packet_send_debug("Kerberos V4 krb_rd_req: %.100s", krb_err_txt[r]); |
| 211 | return 0; |
| 212 | } |
| 213 | des_key_sched((des_cblock *) adat.session, schedule); |
| 214 | |
| 215 | *client = xmalloc(MAX_K_NAME_SZ); |
| 216 | (void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname, |
| 217 | *adat.pinst ? "." : "", adat.pinst, adat.prealm); |
| 218 | |
| 219 | /* Check ~/.klogin authorization now. */ |
| 220 | if (kuserok(&adat, (char *) server_user) != KSUCCESS) { |
| 221 | packet_send_debug("Kerberos V4 .klogin authorization failed!"); |
| 222 | log("Kerberos V4 .klogin authorization failed for %s to account %s", |
| 223 | *client, server_user); |
| 224 | xfree(*client); |
| 225 | return 0; |
| 226 | } |
| 227 | /* Increment the checksum, and return it encrypted with the |
| 228 | session key. */ |
| 229 | cksum = adat.checksum + 1; |
| 230 | cksum = htonl(cksum); |
| 231 | |
| 232 | /* If we can't successfully encrypt the checksum, we send back an |
| 233 | empty message, admitting our failure. */ |
| 234 | if ((r = krb_mk_priv((u_char *) & cksum, reply.dat, sizeof(cksum) + 1, |
| 235 | schedule, &adat.session, &local, &foreign)) < 0) { |
| 236 | packet_send_debug("Kerberos V4 mk_priv: (%d) %s", r, krb_err_txt[r]); |
| 237 | reply.dat[0] = 0; |
| 238 | reply.length = 0; |
| 239 | } else |
| 240 | reply.length = r; |
| 241 | |
| 242 | /* Clear session key. */ |
| 243 | memset(&adat.session, 0, sizeof(&adat.session)); |
| 244 | |
| 245 | packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE); |
| 246 | packet_put_string((char *) reply.dat, reply.length); |
| 247 | packet_send(); |
| 248 | packet_write_wait(); |
| 249 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 250 | } |
| 251 | #endif /* KRB4 */ |
| 252 | |
| 253 | #ifdef AFS |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 254 | int |
| 255 | auth_kerberos_tgt(struct passwd *pw, const char *string) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 256 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 257 | CREDENTIALS creds; |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 258 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 259 | if (!radix_to_creds(string, &creds)) { |
| 260 | log("Protocol error decoding Kerberos V4 tgt"); |
| 261 | packet_send_debug("Protocol error decoding Kerberos V4 tgt"); |
| 262 | goto auth_kerberos_tgt_failure; |
| 263 | } |
| 264 | if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */ |
| 265 | strlcpy(creds.service, "krbtgt", sizeof creds.service); |
| 266 | |
| 267 | if (strcmp(creds.service, "krbtgt")) { |
| 268 | log("Kerberos V4 tgt (%s%s%s@%s) rejected for %s", creds.pname, |
| 269 | creds.pinst[0] ? "." : "", creds.pinst, creds.realm, |
| 270 | pw->pw_name); |
| 271 | packet_send_debug("Kerberos V4 tgt (%s%s%s@%s) rejected for %s", |
| 272 | creds.pname, creds.pinst[0] ? "." : "", creds.pinst, |
| 273 | creds.realm, pw->pw_name); |
| 274 | goto auth_kerberos_tgt_failure; |
| 275 | } |
| 276 | if (!krb4_init(pw->pw_uid)) |
| 277 | goto auth_kerberos_tgt_failure; |
| 278 | |
| 279 | if (in_tkt(creds.pname, creds.pinst) != KSUCCESS) |
| 280 | goto auth_kerberos_tgt_failure; |
| 281 | |
| 282 | if (save_credentials(creds.service, creds.instance, creds.realm, |
| 283 | creds.session, creds.lifetime, creds.kvno, |
| 284 | &creds.ticket_st, creds.issue_date) != KSUCCESS) { |
| 285 | packet_send_debug("Kerberos V4 tgt refused: couldn't save credentials"); |
| 286 | goto auth_kerberos_tgt_failure; |
| 287 | } |
| 288 | /* Successful authentication, passed all checks. */ |
| 289 | chown(tkt_string(), pw->pw_uid, pw->pw_gid); |
| 290 | |
| 291 | packet_send_debug("Kerberos V4 tgt accepted (%s.%s@%s, %s%s%s@%s)", |
| 292 | creds.service, creds.instance, creds.realm, creds.pname, |
| 293 | creds.pinst[0] ? "." : "", creds.pinst, creds.realm); |
| 294 | memset(&creds, 0, sizeof(creds)); |
| 295 | packet_start(SSH_SMSG_SUCCESS); |
| 296 | packet_send(); |
| 297 | packet_write_wait(); |
| 298 | return 1; |
| 299 | |
| 300 | auth_kerberos_tgt_failure: |
| 301 | krb4_cleanup_proc(NULL); |
| 302 | memset(&creds, 0, sizeof(creds)); |
| 303 | packet_start(SSH_SMSG_FAILURE); |
| 304 | packet_send(); |
| 305 | packet_write_wait(); |
| 306 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 307 | } |
| 308 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 309 | int |
| 310 | auth_afs_token(struct passwd *pw, const char *token_string) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 311 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 312 | CREDENTIALS creds; |
| 313 | uid_t uid = pw->pw_uid; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 314 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 315 | if (!radix_to_creds(token_string, &creds)) { |
| 316 | log("Protocol error decoding AFS token"); |
| 317 | packet_send_debug("Protocol error decoding AFS token"); |
| 318 | packet_start(SSH_SMSG_FAILURE); |
| 319 | packet_send(); |
| 320 | packet_write_wait(); |
| 321 | return 0; |
| 322 | } |
| 323 | if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */ |
| 324 | strlcpy(creds.service, "afs", sizeof creds.service); |
| 325 | |
| 326 | if (strncmp(creds.pname, "AFS ID ", 7) == 0) |
| 327 | uid = atoi(creds.pname + 7); |
| 328 | |
| 329 | if (kafs_settoken(creds.realm, uid, &creds)) { |
| 330 | log("AFS token (%s@%s) rejected for %s", creds.pname, creds.realm, |
| 331 | pw->pw_name); |
| 332 | packet_send_debug("AFS token (%s@%s) rejected for %s", creds.pname, |
| 333 | creds.realm, pw->pw_name); |
| 334 | memset(&creds, 0, sizeof(creds)); |
| 335 | packet_start(SSH_SMSG_FAILURE); |
| 336 | packet_send(); |
| 337 | packet_write_wait(); |
| 338 | return 0; |
| 339 | } |
| 340 | packet_send_debug("AFS token accepted (%s@%s, %s@%s)", creds.service, |
| 341 | creds.realm, creds.pname, creds.realm); |
| 342 | memset(&creds, 0, sizeof(creds)); |
| 343 | packet_start(SSH_SMSG_SUCCESS); |
| 344 | packet_send(); |
| 345 | packet_write_wait(); |
| 346 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 347 | } |
| 348 | #endif /* AFS */ |