1 /* $OpenBSD: auth2.c,v 1.129 2013/05/19 02:42:42 djm Exp $ */ 2 /* 3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "includes.h" 27 __RCSID("$FreeBSD$"); 28 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #include <sys/uio.h> 32 33 #include <fcntl.h> 34 #include <pwd.h> 35 #include <stdarg.h> 36 #include <string.h> 37 #include <unistd.h> 38 39 #include "atomicio.h" 40 #include "xmalloc.h" 41 #include "ssh2.h" 42 #include "packet.h" 43 #include "log.h" 44 #include "buffer.h" 45 #include "servconf.h" 46 #include "compat.h" 47 #include "key.h" 48 #include "hostfile.h" 49 #include "auth.h" 50 #include "dispatch.h" 51 #include "pathnames.h" 52 #include "buffer.h" 53 #include "canohost.h" 54 55 #ifdef GSSAPI 56 #include "ssh-gss.h" 57 #endif 58 #include "monitor_wrap.h" 59 60 /* import */ 61 extern ServerOptions options; 62 extern u_char *session_id2; 63 extern u_int session_id2_len; 64 extern Buffer loginmsg; 65 66 /* methods */ 67 68 extern Authmethod method_none; 69 extern Authmethod method_pubkey; 70 extern Authmethod method_passwd; 71 extern Authmethod method_kbdint; 72 extern Authmethod method_hostbased; 73 #ifdef GSSAPI 74 extern Authmethod method_gssapi; 75 #endif 76 #ifdef JPAKE 77 extern Authmethod method_jpake; 78 #endif 79 80 Authmethod *authmethods[] = { 81 &method_none, 82 &method_pubkey, 83 #ifdef GSSAPI 84 &method_gssapi, 85 #endif 86 #ifdef JPAKE 87 &method_jpake, 88 #endif 89 &method_passwd, 90 &method_kbdint, 91 &method_hostbased, 92 NULL 93 }; 94 95 /* protocol */ 96 97 static void input_service_request(int, u_int32_t, void *); 98 static void input_userauth_request(int, u_int32_t, void *); 99 100 /* helper */ 101 static Authmethod *authmethod_lookup(Authctxt *, const char *); 102 static char *authmethods_get(Authctxt *authctxt); 103 104 #define MATCH_NONE 0 /* method or submethod mismatch */ 105 #define MATCH_METHOD 1 /* method matches (no submethod specified) */ 106 #define MATCH_BOTH 2 /* method and submethod match */ 107 #define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */ 108 static int list_starts_with(const char *, const char *, const char *); 109 110 char * 111 auth2_read_banner(void) 112 { 113 struct stat st; 114 char *banner = NULL; 115 size_t len, n; 116 int fd; 117 118 if ((fd = open(options.banner, O_RDONLY)) == -1) 119 return (NULL); 120 if (fstat(fd, &st) == -1) { 121 close(fd); 122 return (NULL); 123 } 124 if (st.st_size <= 0 || st.st_size > 1*1024*1024) { 125 close(fd); 126 return (NULL); 127 } 128 129 len = (size_t)st.st_size; /* truncate */ 130 banner = xmalloc(len + 1); 131 n = atomicio(read, fd, banner, len); 132 close(fd); 133 134 if (n != len) { 135 free(banner); 136 return (NULL); 137 } 138 banner[n] = '\0'; 139 140 return (banner); 141 } 142 143 void 144 userauth_send_banner(const char *msg) 145 { 146 if (datafellows & SSH_BUG_BANNER) 147 return; 148 149 packet_start(SSH2_MSG_USERAUTH_BANNER); 150 packet_put_cstring(msg); 151 packet_put_cstring(""); /* language, unused */ 152 packet_send(); 153 debug("%s: sent", __func__); 154 } 155 156 static void 157 userauth_banner(void) 158 { 159 char *banner = NULL; 160 161 if (options.banner == NULL || 162 strcasecmp(options.banner, "none") == 0 || 163 (datafellows & SSH_BUG_BANNER) != 0) 164 return; 165 166 if ((banner = PRIVSEP(auth2_read_banner())) == NULL) 167 goto done; 168 userauth_send_banner(banner); 169 170 done: 171 free(banner); 172 } 173 174 /* 175 * loop until authctxt->success == TRUE 176 */ 177 void 178 do_authentication2(Authctxt *authctxt) 179 { 180 dispatch_init(&dispatch_protocol_error); 181 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); 182 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); 183 } 184 185 /*ARGSUSED*/ 186 static void 187 input_service_request(int type, u_int32_t seq, void *ctxt) 188 { 189 Authctxt *authctxt = ctxt; 190 u_int len; 191 int acceptit = 0; 192 char *service = packet_get_cstring(&len); 193 packet_check_eom(); 194 195 if (authctxt == NULL) 196 fatal("input_service_request: no authctxt"); 197 198 if (strcmp(service, "ssh-userauth") == 0) { 199 if (!authctxt->success) { 200 acceptit = 1; 201 /* now we can handle user-auth requests */ 202 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); 203 } 204 } 205 /* XXX all other service requests are denied */ 206 207 if (acceptit) { 208 packet_start(SSH2_MSG_SERVICE_ACCEPT); 209 packet_put_cstring(service); 210 packet_send(); 211 packet_write_wait(); 212 } else { 213 debug("bad service request %s", service); 214 packet_disconnect("bad service request %s", service); 215 } 216 free(service); 217 } 218 219 /*ARGSUSED*/ 220 static void 221 input_userauth_request(int type, u_int32_t seq, void *ctxt) 222 { 223 Authctxt *authctxt = ctxt; 224 Authmethod *m = NULL; 225 char *user, *service, *method, *style = NULL; 226 int authenticated = 0; 227 #ifdef HAVE_LOGIN_CAP 228 login_cap_t *lc; 229 const char *from_host, *from_ip; 230 231 from_host = get_canonical_hostname(options.use_dns); 232 from_ip = get_remote_ipaddr(); 233 #endif 234 235 if (authctxt == NULL) 236 fatal("input_userauth_request: no authctxt"); 237 238 user = packet_get_cstring(NULL); 239 service = packet_get_cstring(NULL); 240 method = packet_get_cstring(NULL); 241 debug("userauth-request for user %s service %s method %s", user, service, method); 242 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); 243 244 if ((style = strchr(user, ':')) != NULL) 245 *style++ = 0; 246 247 if (authctxt->attempt++ == 0) { 248 /* setup auth context */ 249 authctxt->pw = PRIVSEP(getpwnamallow(user)); 250 authctxt->user = xstrdup(user); 251 if (authctxt->pw && strcmp(service, "ssh-connection")==0) { 252 authctxt->valid = 1; 253 debug2("input_userauth_request: setting up authctxt for %s", user); 254 } else { 255 logit("input_userauth_request: invalid user %s", user); 256 authctxt->pw = fakepw(); 257 #ifdef SSH_AUDIT_EVENTS 258 PRIVSEP(audit_event(SSH_INVALID_USER)); 259 #endif 260 } 261 #ifdef USE_PAM 262 if (options.use_pam) 263 PRIVSEP(start_pam(authctxt)); 264 #endif 265 setproctitle("%s%s", authctxt->valid ? user : "unknown", 266 use_privsep ? " [net]" : ""); 267 authctxt->service = xstrdup(service); 268 authctxt->style = style ? xstrdup(style) : NULL; 269 if (use_privsep) 270 mm_inform_authserv(service, style); 271 userauth_banner(); 272 if (auth2_setup_methods_lists(authctxt) != 0) 273 packet_disconnect("no authentication methods enabled"); 274 } else if (strcmp(user, authctxt->user) != 0 || 275 strcmp(service, authctxt->service) != 0) { 276 packet_disconnect("Change of username or service not allowed: " 277 "(%s,%s) -> (%s,%s)", 278 authctxt->user, authctxt->service, user, service); 279 } 280 281 #ifdef HAVE_LOGIN_CAP 282 if (authctxt->pw != NULL) { 283 lc = login_getpwclass(authctxt->pw); 284 if (lc == NULL) 285 lc = login_getclassbyname(NULL, authctxt->pw); 286 if (!auth_hostok(lc, from_host, from_ip)) { 287 logit("Denied connection for %.200s from %.200s [%.200s].", 288 authctxt->pw->pw_name, from_host, from_ip); 289 packet_disconnect("Sorry, you are not allowed to connect."); 290 } 291 if (!auth_timeok(lc, time(NULL))) { 292 logit("LOGIN %.200s REFUSED (TIME) FROM %.200s", 293 authctxt->pw->pw_name, from_host); 294 packet_disconnect("Logins not available right now."); 295 } 296 login_close(lc); 297 lc = NULL; 298 } 299 #endif /* HAVE_LOGIN_CAP */ 300 301 /* reset state */ 302 auth2_challenge_stop(authctxt); 303 #ifdef JPAKE 304 auth2_jpake_stop(authctxt); 305 #endif 306 307 #ifdef GSSAPI 308 /* XXX move to auth2_gssapi_stop() */ 309 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 310 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); 311 #endif 312 313 authctxt->postponed = 0; 314 authctxt->server_caused_failure = 0; 315 316 /* try to authenticate user */ 317 m = authmethod_lookup(authctxt, method); 318 if (m != NULL && authctxt->failures < options.max_authtries) { 319 debug2("input_userauth_request: try method %s", method); 320 authenticated = m->userauth(authctxt); 321 } 322 userauth_finish(authctxt, authenticated, method, NULL); 323 324 free(service); 325 free(user); 326 free(method); 327 } 328 329 void 330 userauth_finish(Authctxt *authctxt, int authenticated, const char *method, 331 const char *submethod) 332 { 333 char *methods; 334 int partial = 0; 335 336 if (!authctxt->valid && authenticated) 337 fatal("INTERNAL ERROR: authenticated invalid user %s", 338 authctxt->user); 339 if (authenticated && authctxt->postponed) 340 fatal("INTERNAL ERROR: authenticated and postponed"); 341 342 /* Special handling for root */ 343 if (authenticated && authctxt->pw->pw_uid == 0 && 344 !auth_root_allowed(method)) { 345 authenticated = 0; 346 #ifdef SSH_AUDIT_EVENTS 347 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); 348 #endif 349 } 350 351 if (authenticated && options.num_auth_methods != 0) { 352 if (!auth2_update_methods_lists(authctxt, method, submethod)) { 353 authenticated = 0; 354 partial = 1; 355 } 356 } 357 358 /* Log before sending the reply */ 359 auth_log(authctxt, authenticated, partial, method, submethod); 360 361 if (authctxt->postponed) 362 return; 363 364 #ifdef USE_PAM 365 if (options.use_pam && authenticated) { 366 if (!PRIVSEP(do_pam_account())) { 367 /* if PAM returned a message, send it to the user */ 368 if (buffer_len(&loginmsg) > 0) { 369 buffer_append(&loginmsg, "\0", 1); 370 userauth_send_banner(buffer_ptr(&loginmsg)); 371 packet_write_wait(); 372 } 373 fatal("Access denied for user %s by PAM account " 374 "configuration", authctxt->user); 375 } 376 } 377 #endif 378 379 #ifdef _UNICOS 380 if (authenticated && cray_access_denied(authctxt->user)) { 381 authenticated = 0; 382 fatal("Access denied for user %s.", authctxt->user); 383 } 384 #endif /* _UNICOS */ 385 386 if (authenticated == 1) { 387 /* turn off userauth */ 388 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); 389 packet_start(SSH2_MSG_USERAUTH_SUCCESS); 390 packet_send(); 391 packet_write_wait(); 392 /* now we can break out */ 393 authctxt->success = 1; 394 } else { 395 396 /* Allow initial try of "none" auth without failure penalty */ 397 if (!authctxt->server_caused_failure && 398 (authctxt->attempt > 1 || strcmp(method, "none") != 0)) 399 authctxt->failures++; 400 if (authctxt->failures >= options.max_authtries) { 401 #ifdef SSH_AUDIT_EVENTS 402 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); 403 #endif 404 packet_disconnect(AUTH_FAIL_MSG, authctxt->user); 405 } 406 methods = authmethods_get(authctxt); 407 debug3("%s: failure partial=%d next methods=\"%s\"", __func__, 408 partial, methods); 409 packet_start(SSH2_MSG_USERAUTH_FAILURE); 410 packet_put_cstring(methods); 411 packet_put_char(partial); 412 packet_send(); 413 packet_write_wait(); 414 free(methods); 415 } 416 } 417 418 /* 419 * Checks whether method is allowed by at least one AuthenticationMethods 420 * methods list. Returns 1 if allowed, or no methods lists configured. 421 * 0 otherwise. 422 */ 423 int 424 auth2_method_allowed(Authctxt *authctxt, const char *method, 425 const char *submethod) 426 { 427 u_int i; 428 429 /* 430 * NB. authctxt->num_auth_methods might be zero as a result of 431 * auth2_setup_methods_lists(), so check the configuration. 432 */ 433 if (options.num_auth_methods == 0) 434 return 1; 435 for (i = 0; i < authctxt->num_auth_methods; i++) { 436 if (list_starts_with(authctxt->auth_methods[i], method, 437 submethod) != MATCH_NONE) 438 return 1; 439 } 440 return 0; 441 } 442 443 static char * 444 authmethods_get(Authctxt *authctxt) 445 { 446 Buffer b; 447 char *list; 448 u_int i; 449 450 buffer_init(&b); 451 for (i = 0; authmethods[i] != NULL; i++) { 452 if (strcmp(authmethods[i]->name, "none") == 0) 453 continue; 454 if (authmethods[i]->enabled == NULL || 455 *(authmethods[i]->enabled) == 0) 456 continue; 457 if (!auth2_method_allowed(authctxt, authmethods[i]->name, 458 NULL)) 459 continue; 460 if (buffer_len(&b) > 0) 461 buffer_append(&b, ",", 1); 462 buffer_append(&b, authmethods[i]->name, 463 strlen(authmethods[i]->name)); 464 } 465 buffer_append(&b, "\0", 1); 466 list = xstrdup(buffer_ptr(&b)); 467 buffer_free(&b); 468 return list; 469 } 470 471 static Authmethod * 472 authmethod_lookup(Authctxt *authctxt, const char *name) 473 { 474 int i; 475 476 if (name != NULL) 477 for (i = 0; authmethods[i] != NULL; i++) 478 if (authmethods[i]->enabled != NULL && 479 *(authmethods[i]->enabled) != 0 && 480 strcmp(name, authmethods[i]->name) == 0 && 481 auth2_method_allowed(authctxt, 482 authmethods[i]->name, NULL)) 483 return authmethods[i]; 484 debug2("Unrecognized authentication method name: %s", 485 name ? name : "NULL"); 486 return NULL; 487 } 488 489 /* 490 * Check a comma-separated list of methods for validity. Is need_enable is 491 * non-zero, then also require that the methods are enabled. 492 * Returns 0 on success or -1 if the methods list is invalid. 493 */ 494 int 495 auth2_methods_valid(const char *_methods, int need_enable) 496 { 497 char *methods, *omethods, *method, *p; 498 u_int i, found; 499 int ret = -1; 500 501 if (*_methods == '\0') { 502 error("empty authentication method list"); 503 return -1; 504 } 505 omethods = methods = xstrdup(_methods); 506 while ((method = strsep(&methods, ",")) != NULL) { 507 for (found = i = 0; !found && authmethods[i] != NULL; i++) { 508 if ((p = strchr(method, ':')) != NULL) 509 *p = '\0'; 510 if (strcmp(method, authmethods[i]->name) != 0) 511 continue; 512 if (need_enable) { 513 if (authmethods[i]->enabled == NULL || 514 *(authmethods[i]->enabled) == 0) { 515 error("Disabled method \"%s\" in " 516 "AuthenticationMethods list \"%s\"", 517 method, _methods); 518 goto out; 519 } 520 } 521 found = 1; 522 break; 523 } 524 if (!found) { 525 error("Unknown authentication method \"%s\" in list", 526 method); 527 goto out; 528 } 529 } 530 ret = 0; 531 out: 532 free(omethods); 533 return ret; 534 } 535 536 /* 537 * Prune the AuthenticationMethods supplied in the configuration, removing 538 * any methods lists that include disabled methods. Note that this might 539 * leave authctxt->num_auth_methods == 0, even when multiple required auth 540 * has been requested. For this reason, all tests for whether multiple is 541 * enabled should consult options.num_auth_methods directly. 542 */ 543 int 544 auth2_setup_methods_lists(Authctxt *authctxt) 545 { 546 u_int i; 547 548 if (options.num_auth_methods == 0) 549 return 0; 550 debug3("%s: checking methods", __func__); 551 authctxt->auth_methods = xcalloc(options.num_auth_methods, 552 sizeof(*authctxt->auth_methods)); 553 authctxt->num_auth_methods = 0; 554 for (i = 0; i < options.num_auth_methods; i++) { 555 if (auth2_methods_valid(options.auth_methods[i], 1) != 0) { 556 logit("Authentication methods list \"%s\" contains " 557 "disabled method, skipping", 558 options.auth_methods[i]); 559 continue; 560 } 561 debug("authentication methods list %d: %s", 562 authctxt->num_auth_methods, options.auth_methods[i]); 563 authctxt->auth_methods[authctxt->num_auth_methods++] = 564 xstrdup(options.auth_methods[i]); 565 } 566 if (authctxt->num_auth_methods == 0) { 567 error("No AuthenticationMethods left after eliminating " 568 "disabled methods"); 569 return -1; 570 } 571 return 0; 572 } 573 574 static int 575 list_starts_with(const char *methods, const char *method, 576 const char *submethod) 577 { 578 size_t l = strlen(method); 579 int match; 580 const char *p; 581 582 if (strncmp(methods, method, l) != 0) 583 return MATCH_NONE; 584 p = methods + l; 585 match = MATCH_METHOD; 586 if (*p == ':') { 587 if (!submethod) 588 return MATCH_PARTIAL; 589 l = strlen(submethod); 590 p += 1; 591 if (strncmp(submethod, p, l)) 592 return MATCH_NONE; 593 p += l; 594 match = MATCH_BOTH; 595 } 596 if (*p != ',' && *p != '\0') 597 return MATCH_NONE; 598 return match; 599 } 600 601 /* 602 * Remove method from the start of a comma-separated list of methods. 603 * Returns 0 if the list of methods did not start with that method or 1 604 * if it did. 605 */ 606 static int 607 remove_method(char **methods, const char *method, const char *submethod) 608 { 609 char *omethods = *methods, *p; 610 size_t l = strlen(method); 611 int match; 612 613 match = list_starts_with(omethods, method, submethod); 614 if (match != MATCH_METHOD && match != MATCH_BOTH) 615 return 0; 616 p = omethods + l; 617 if (submethod && match == MATCH_BOTH) 618 p += 1 + strlen(submethod); /* include colon */ 619 if (*p == ',') 620 p++; 621 *methods = xstrdup(p); 622 free(omethods); 623 return 1; 624 } 625 626 /* 627 * Called after successful authentication. Will remove the successful method 628 * from the start of each list in which it occurs. If it was the last method 629 * in any list, then authentication is deemed successful. 630 * Returns 1 if the method completed any authentication list or 0 otherwise. 631 */ 632 int 633 auth2_update_methods_lists(Authctxt *authctxt, const char *method, 634 const char *submethod) 635 { 636 u_int i, found = 0; 637 638 debug3("%s: updating methods list after \"%s\"", __func__, method); 639 for (i = 0; i < authctxt->num_auth_methods; i++) { 640 if (!remove_method(&(authctxt->auth_methods[i]), method, 641 submethod)) 642 continue; 643 found = 1; 644 if (*authctxt->auth_methods[i] == '\0') { 645 debug2("authentication methods list %d complete", i); 646 return 1; 647 } 648 debug3("authentication methods list %d remaining: \"%s\"", 649 i, authctxt->auth_methods[i]); 650 } 651 /* This should not happen, but would be bad if it did */ 652 if (!found) 653 fatal("%s: method not in AuthenticationMethods", __func__); 654 return 0; 655 } 656 657 658