auth1.c revision 92559
1/* 2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 3 * All rights reserved 4 * 5 * As far as I am concerned, the code I have written for this software 6 * can be used freely for any purpose. Any derived versions of this 7 * software must be clearly marked as such, and if the derived work is 8 * incompatible with the protocol description in the RFC file, it must be 9 * called by a name other than "ssh" or "Secure Shell". 10 */ 11 12#include "includes.h" 13RCSID("$OpenBSD: auth1.c,v 1.35 2002/02/03 17:53:25 markus Exp $"); 14RCSID("$FreeBSD: head/crypto/openssh/auth1.c 92559 2002-03-18 10:09:43Z des $"); 15 16#include "xmalloc.h" 17#include "rsa.h" 18#include "ssh1.h" 19#include "packet.h" 20#include "buffer.h" 21#include "mpaux.h" 22#include "log.h" 23#include "servconf.h" 24#include "compat.h" 25#include "auth.h" 26#include "channels.h" 27#include "session.h" 28#include "canohost.h" 29#include "misc.h" 30#include "uidswap.h" 31 32#include <login_cap.h> 33#include "auth-pam.h" 34#include <security/pam_appl.h> 35 36/* import */ 37extern ServerOptions options; 38 39/* 40 * convert ssh auth msg type into description 41 */ 42static char * 43get_authname(int type) 44{ 45 static char buf[1024]; 46 switch (type) { 47 case SSH_CMSG_AUTH_PASSWORD: 48 return "password"; 49 case SSH_CMSG_AUTH_RSA: 50 return "rsa"; 51 case SSH_CMSG_AUTH_RHOSTS_RSA: 52 return "rhosts-rsa"; 53 case SSH_CMSG_AUTH_RHOSTS: 54 return "rhosts"; 55 case SSH_CMSG_AUTH_TIS: 56 case SSH_CMSG_AUTH_TIS_RESPONSE: 57 return "challenge-response"; 58#if defined(KRB4) || defined(KRB5) 59 case SSH_CMSG_AUTH_KERBEROS: 60 return "kerberos"; 61#endif 62 } 63 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type); 64 return buf; 65} 66 67/* 68 * read packets, try to authenticate the user and 69 * return only if authentication is successful 70 */ 71static void 72do_authloop(Authctxt *authctxt) 73{ 74 int authenticated = 0; 75 u_int bits; 76 Key *client_host_key; 77 BIGNUM *n; 78 char *client_user, *password; 79 char info[1024]; 80 u_int dlen; 81 u_int ulen; 82 int type = 0; 83 struct passwd *pw = authctxt->pw; 84 void (*authlog) (const char *fmt,...) = verbose; 85#ifdef HAVE_LOGIN_CAP 86 login_cap_t *lc; 87#endif /* HAVE_LOGIN_CAP */ 88#ifdef USE_PAM 89 struct inverted_pam_cookie *pam_cookie; 90#endif /* USE_PAM */ 91#if defined(HAVE_LOGIN_CAP) || defined(LOGIN_ACCESS) 92 const char *from_host, *from_ip; 93 94 from_host = get_canonical_hostname(options.verify_reverse_mapping); 95 from_ip = get_remote_ipaddr(); 96#endif /* HAVE_LOGIN_CAP || LOGIN_ACCESS */ 97 98 debug("Attempting authentication for %s%.100s.", 99 authctxt->valid ? "" : "illegal user ", authctxt->user); 100 101 /* If the user has no password, accept authentication immediately. */ 102 if (options.password_authentication && 103#if defined(KRB4) || defined(KRB5) 104 (!options.kerberos_authentication || options.kerberos_or_local_passwd) && 105#endif 106#ifdef USE_PAM 107 auth_pam_password(authctxt, "")) { 108#else 109 auth_password(authctxt, "")) { 110#endif 111 auth_log(authctxt, 1, "without authentication", ""); 112 return; 113 } 114 115 /* Indicate that authentication is needed. */ 116 packet_start(SSH_SMSG_FAILURE); 117 packet_send(); 118 packet_write_wait(); 119 120 client_user = NULL; 121 122 for (;;) { 123 /* default to fail */ 124 authenticated = 0; 125 126 info[0] = '\0'; 127 128 /* Get a packet from the client. */ 129 type = packet_read(); 130 131 /* Process the packet. */ 132 switch (type) { 133 134#if defined(KRB4) || defined(KRB5) 135 case SSH_CMSG_AUTH_KERBEROS: 136 if (!options.kerberos_authentication) { 137 verbose("Kerberos authentication disabled."); 138 } else { 139 char *kdata = packet_get_string(&dlen); 140 packet_check_eom(); 141 142 if (kdata[0] == 4) { /* KRB_PROT_VERSION */ 143#ifdef KRB4 144 KTEXT_ST tkt; 145 146 tkt.length = dlen; 147 if (tkt.length < MAX_KTXT_LEN) 148 memcpy(tkt.dat, kdata, tkt.length); 149 150 if (auth_krb4(authctxt, &tkt, &client_user)) { 151 authenticated = 1; 152 snprintf(info, sizeof(info), 153 " tktuser %.100s", 154 client_user); 155 xfree(client_user); 156 } 157#endif /* KRB4 */ 158 } else { 159#ifdef KRB5 160 krb5_data tkt; 161 tkt.length = dlen; 162 tkt.data = kdata; 163 164 if (auth_krb5(authctxt, &tkt, &client_user)) { 165 authenticated = 1; 166 snprintf(info, sizeof(info), 167 " tktuser %.100s", 168 client_user); 169 xfree(client_user); 170 } 171#endif /* KRB5 */ 172 } 173 xfree(kdata); 174 } 175 break; 176#endif /* KRB4 || KRB5 */ 177 178#if defined(AFS) || defined(KRB5) 179 /* XXX - punt on backward compatibility here. */ 180 case SSH_CMSG_HAVE_KERBEROS_TGT: 181 packet_send_debug("Kerberos TGT passing disabled before authentication."); 182 break; 183#ifdef AFS 184 case SSH_CMSG_HAVE_AFS_TOKEN: 185 packet_send_debug("AFS token passing disabled before authentication."); 186 break; 187#endif /* AFS */ 188#endif /* AFS || KRB5 */ 189 190 case SSH_CMSG_AUTH_RHOSTS: 191 if (!options.rhosts_authentication) { 192 verbose("Rhosts authentication disabled."); 193 break; 194 } 195 /* 196 * Get client user name. Note that we just have to 197 * trust the client; this is one reason why rhosts 198 * authentication is insecure. (Another is 199 * IP-spoofing on a local network.) 200 */ 201 client_user = packet_get_string(&ulen); 202 packet_check_eom(); 203 204 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */ 205 authenticated = auth_rhosts(pw, client_user); 206 207 snprintf(info, sizeof info, " ruser %.100s", client_user); 208 break; 209 210 case SSH_CMSG_AUTH_RHOSTS_RSA: 211 if (!options.rhosts_rsa_authentication) { 212 verbose("Rhosts with RSA authentication disabled."); 213 break; 214 } 215 /* 216 * Get client user name. Note that we just have to 217 * trust the client; root on the client machine can 218 * claim to be any user. 219 */ 220 client_user = packet_get_string(&ulen); 221 222 /* Get the client host key. */ 223 client_host_key = key_new(KEY_RSA1); 224 bits = packet_get_int(); 225 packet_get_bignum(client_host_key->rsa->e); 226 packet_get_bignum(client_host_key->rsa->n); 227 228 if (bits != BN_num_bits(client_host_key->rsa->n)) 229 verbose("Warning: keysize mismatch for client_host_key: " 230 "actual %d, announced %d", 231 BN_num_bits(client_host_key->rsa->n), bits); 232 packet_check_eom(); 233 234 authenticated = auth_rhosts_rsa(pw, client_user, 235 client_host_key); 236 key_free(client_host_key); 237 238 snprintf(info, sizeof info, " ruser %.100s", client_user); 239 break; 240 241 case SSH_CMSG_AUTH_RSA: 242 if (!options.rsa_authentication) { 243 verbose("RSA authentication disabled."); 244 break; 245 } 246 /* RSA authentication requested. */ 247 if ((n = BN_new()) == NULL) 248 fatal("do_authloop: BN_new failed"); 249 packet_get_bignum(n); 250 packet_check_eom(); 251 authenticated = auth_rsa(pw, n); 252 BN_clear_free(n); 253 break; 254 255 case SSH_CMSG_AUTH_PASSWORD: 256 if (!options.password_authentication) { 257 verbose("Password authentication disabled."); 258 break; 259 } 260 /* 261 * Read user password. It is in plain text, but was 262 * transmitted over the encrypted channel so it is 263 * not visible to an outside observer. 264 */ 265 password = packet_get_string(&dlen); 266 packet_check_eom(); 267 268#ifdef USE_PAM 269 /* Do PAM auth with password */ 270 authenticated = auth_pam_password(authctxt, password); 271#else /* !USE_PAM */ 272 /* Try authentication with the password. */ 273 authenticated = auth_password(authctxt, password); 274#endif /* USE_PAM */ 275 276 memset(password, 0, strlen(password)); 277 xfree(password); 278 break; 279 280#ifdef USE_PAM 281 case SSH_CMSG_AUTH_TIS: 282 debug("rcvd SSH_CMSG_AUTH_TIS: Trying PAM"); 283 pam_cookie = ipam_start_auth("csshd", pw->pw_name); 284 /* We now have data available to send as a challenge */ 285 if (pam_cookie->num_msg != 1 || 286 (pam_cookie->msg[0]->msg_style != PAM_PROMPT_ECHO_OFF && 287 pam_cookie->msg[0]->msg_style != PAM_PROMPT_ECHO_ON)) { 288 /* We got several challenges or an unknown challenge type */ 289 ipam_free_cookie(pam_cookie); 290 pam_cookie = NULL; 291 break; 292 } 293 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); 294 packet_put_string(pam_cookie->msg[0]->msg, strlen(pam_cookie->msg[0]->msg)); 295 packet_send(); 296 packet_write_wait(); 297 continue; 298 case SSH_CMSG_AUTH_TIS_RESPONSE: 299 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE"); 300 if (pam_cookie != NULL) { 301 char *response = packet_get_string(&dlen); 302 303 pam_cookie->resp[0]->resp = strdup(response); 304 xfree(response); 305 authenticated = ipam_complete_auth(pam_cookie); 306 ipam_free_cookie(pam_cookie); 307 pam_cookie = NULL; 308 } 309 break; 310#elif defined(SKEY) 311 case SSH_CMSG_AUTH_TIS: 312 debug("rcvd SSH_CMSG_AUTH_TIS"); 313 if (options.challenge_response_authentication == 1) { 314 char *challenge = get_challenge(authctxt); 315 if (challenge != NULL) { 316 debug("sending challenge '%s'", challenge); 317 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); 318 packet_put_cstring(challenge); 319 xfree(challenge); 320 packet_send(); 321 packet_write_wait(); 322 continue; 323 } 324 } 325 break; 326 case SSH_CMSG_AUTH_TIS_RESPONSE: 327 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE"); 328 if (options.challenge_response_authentication == 1) { 329 char *response = packet_get_string(&dlen); 330 debug("got response '%s'", response); 331 packet_check_eom(); 332 authenticated = verify_response(authctxt, response); 333 memset(response, 'r', dlen); 334 xfree(response); 335 } 336 break; 337#else 338 case SSH_CMSG_AUTH_TIS: 339 /* TIS Authentication is unsupported */ 340 log("TIS authentication unsupported."); 341 break; 342#endif 343 344 default: 345 /* 346 * Any unknown messages will be ignored (and failure 347 * returned) during authentication. 348 */ 349 log("Unknown message during authentication: type %d", type); 350 break; 351 } 352 353#ifdef HAVE_LOGIN_CAP 354 if (pw != NULL) { 355 lc = login_getpwclass(pw); 356 if (lc == NULL) 357 lc = login_getclassbyname(NULL, pw); 358 if (!auth_hostok(lc, from_host, from_ip)) { 359 log("Denied connection for %.200s from %.200s [%.200s].", 360 pw->pw_name, from_host, from_ip); 361 packet_disconnect("Sorry, you are not allowed to connect."); 362 } 363 if (!auth_timeok(lc, time(NULL))) { 364 log("LOGIN %.200s REFUSED (TIME) FROM %.200s", 365 pw->pw_name, from_host); 366 packet_disconnect("Logins not available right now."); 367 } 368 login_close(lc); 369 lc = NULL; 370 } 371#endif /* HAVE_LOGIN_CAP */ 372#ifdef LOGIN_ACCESS 373 if (pw != NULL && !login_access(pw->pw_name, from_host)) { 374 log("Denied connection for %.200s from %.200s [%.200s].", 375 pw->pw_name, from_host, from_ip); 376 packet_disconnect("Sorry, you are not allowed to connect."); 377 } 378#endif /* LOGIN_ACCESS */ 379#ifdef BSD_AUTH 380 if (authctxt->as) { 381 auth_close(authctxt->as); 382 authctxt->as = NULL; 383 } 384#endif 385 if (!authctxt->valid && authenticated) 386 fatal("INTERNAL ERROR: authenticated invalid user %s", 387 authctxt->user); 388 389 /* Special handling for root */ 390 if (authenticated && authctxt->pw->pw_uid == 0 && 391 !auth_root_allowed(get_authname(type))) 392 authenticated = 0; 393 394 if (pw != NULL && pw->pw_uid == 0) 395 log("ROOT LOGIN as '%.100s' from %.100s", 396 pw->pw_name, 397 get_canonical_hostname(options.verify_reverse_mapping)); 398 399 /* Log before sending the reply */ 400 auth_log(authctxt, authenticated, get_authname(type), info); 401 402#ifdef USE_PAM 403 if (authenticated && !do_pam_account(pw->pw_name, client_user)) 404 authenticated = 0; 405#endif 406 407 if (client_user != NULL) { 408 xfree(client_user); 409 client_user = NULL; 410 } 411 412 if (authenticated) 413 return; 414 415 if (authctxt->failures++ > AUTH_FAIL_MAX) 416 packet_disconnect(AUTH_FAIL_MSG, authctxt->user); 417 418 packet_start(SSH_SMSG_FAILURE); 419 packet_send(); 420 packet_write_wait(); 421 } 422} 423 424/* 425 * Performs authentication of an incoming connection. Session key has already 426 * been exchanged and encryption is enabled. 427 */ 428void 429do_authentication(void) 430{ 431 Authctxt *authctxt; 432 struct passwd *pw; 433 u_int ulen; 434 char *p, *user, *style = NULL; 435 436 /* Get the name of the user that we wish to log in as. */ 437 packet_read_expect(SSH_CMSG_USER); 438 439 /* Get the user name. */ 440 user = packet_get_string(&ulen); 441 packet_check_eom(); 442 443 if ((style = strchr(user, ':')) != NULL) 444 *style++ = '\0'; 445 446 /* XXX - SSH.com Kerberos v5 braindeath. */ 447 if ((p = strchr(user, '@')) != NULL) 448 *p = '\0'; 449 450 authctxt = authctxt_new(); 451 authctxt->user = user; 452 authctxt->style = style; 453 454 /* Verify that the user is a valid user. */ 455 pw = getpwnam(user); 456 if (pw && allowed_user(pw)) { 457 authctxt->valid = 1; 458 pw = pwcopy(pw); 459 } else { 460 debug("do_authentication: illegal user %s", user); 461 pw = NULL; 462 } 463 authctxt->pw = pw; 464 465#ifdef USE_PAM 466 if (pw != NULL) 467 start_pam(pw); 468#endif 469 setproctitle("%s", pw ? user : "unknown"); 470 471 /* 472 * If we are not running as root, the user must have the same uid as 473 * the server. 474 */ 475 if (getuid() != 0 && pw && pw->pw_uid != getuid()) 476 packet_disconnect("Cannot change user when server not running as root."); 477 478 /* 479 * Loop until the user has been authenticated or the connection is 480 * closed, do_authloop() returns only if authentication is successful 481 */ 482 do_authloop(authctxt); 483 484 /* The user has been authenticated and accepted. */ 485 packet_start(SSH_SMSG_SUCCESS); 486 packet_send(); 487 packet_write_wait(); 488 489 /* Perform session preparation. */ 490 do_authenticated(authctxt); 491} 492