1 /* $OpenBSD: auth2.c,v 1.137 2017/02/03 23:05:57 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 27 #include <sys/types.h> 28 #include <sys/stat.h> 29 #include <sys/uio.h> 30 31 #include <fcntl.h> 32 #include <pwd.h> 33 #include <stdarg.h> 34 #include <string.h> 35 #include <unistd.h> 36 37 #include "atomicio.h" 38 #include "xmalloc.h" 39 #include "ssh2.h" 40 #include "packet.h" 41 #include "log.h" 42 #include "buffer.h" 43 #include "misc.h" 44 #include "servconf.h" 45 #include "compat.h" 46 #include "key.h" 47 #include "hostfile.h" 48 #include "auth.h" 49 #include "dispatch.h" 50 #include "pathnames.h" 51 #ifdef GSSAPI 52 #include "ssh-gss.h" 53 #endif 54 #include "monitor_wrap.h" 55 56 /* import */ 57 extern ServerOptions options; 58 extern u_char *session_id2; 59 extern u_int session_id2_len; 60 61 /* methods */ 62 63 extern Authmethod method_none; 64 extern Authmethod method_pubkey; 65 extern Authmethod method_passwd; 66 extern Authmethod method_kbdint; 67 extern Authmethod method_hostbased; 68 #ifdef GSSAPI 69 extern Authmethod method_gssapi; 70 #endif 71 72 Authmethod *authmethods[] = { 73 &method_none, 74 &method_pubkey, 75 #ifdef GSSAPI 76 &method_gssapi, 77 #endif 78 &method_passwd, 79 &method_kbdint, 80 &method_hostbased, 81 NULL 82 }; 83 84 /* protocol */ 85 86 static int input_service_request(int, u_int32_t, void *); 87 static int input_userauth_request(int, u_int32_t, void *); 88 89 /* helper */ 90 static Authmethod *authmethod_lookup(Authctxt *, const char *); 91 static char *authmethods_get(Authctxt *authctxt); 92 93 #define MATCH_NONE 0 /* method or submethod mismatch */ 94 #define MATCH_METHOD 1 /* method matches (no submethod specified) */ 95 #define MATCH_BOTH 2 /* method and submethod match */ 96 #define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */ 97 static int list_starts_with(const char *, const char *, const char *); 98 99 char * 100 auth2_read_banner(void) 101 { 102 struct stat st; 103 char *banner = NULL; 104 size_t len, n; 105 int fd; 106 107 if ((fd = open(options.banner, O_RDONLY)) == -1) 108 return (NULL); 109 if (fstat(fd, &st) == -1) { 110 close(fd); 111 return (NULL); 112 } 113 if (st.st_size <= 0 || st.st_size > 1*1024*1024) { 114 close(fd); 115 return (NULL); 116 } 117 118 len = (size_t)st.st_size; /* truncate */ 119 banner = xmalloc(len + 1); 120 n = atomicio(read, fd, banner, len); 121 close(fd); 122 123 if (n != len) { 124 free(banner); 125 return (NULL); 126 } 127 banner[n] = '\0'; 128 129 return (banner); 130 } 131 132 static void 133 userauth_banner(void) 134 { 135 char *banner = NULL; 136 137 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER) != 0) 138 return; 139 140 if ((banner = PRIVSEP(auth2_read_banner())) == NULL) 141 goto done; 142 143 packet_start(SSH2_MSG_USERAUTH_BANNER); 144 packet_put_cstring(banner); 145 packet_put_cstring(""); /* language, unused */ 146 packet_send(); 147 debug("userauth_banner: sent"); 148 done: 149 free(banner); 150 } 151 152 /* 153 * loop until authctxt->success == TRUE 154 */ 155 void 156 do_authentication2(Authctxt *authctxt) 157 { 158 dispatch_init(&dispatch_protocol_error); 159 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); 160 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); 161 } 162 163 /*ARGSUSED*/ 164 static int 165 input_service_request(int type, u_int32_t seq, void *ctxt) 166 { 167 Authctxt *authctxt = ctxt; 168 u_int len; 169 int acceptit = 0; 170 char *service = packet_get_cstring(&len); 171 packet_check_eom(); 172 173 if (authctxt == NULL) 174 fatal("input_service_request: no authctxt"); 175 176 if (strcmp(service, "ssh-userauth") == 0) { 177 if (!authctxt->success) { 178 acceptit = 1; 179 /* now we can handle user-auth requests */ 180 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); 181 } 182 } 183 /* XXX all other service requests are denied */ 184 185 if (acceptit) { 186 packet_start(SSH2_MSG_SERVICE_ACCEPT); 187 packet_put_cstring(service); 188 packet_send(); 189 packet_write_wait(); 190 } else { 191 debug("bad service request %s", service); 192 packet_disconnect("bad service request %s", service); 193 } 194 free(service); 195 return 0; 196 } 197 198 /*ARGSUSED*/ 199 static int 200 input_userauth_request(int type, u_int32_t seq, void *ctxt) 201 { 202 struct ssh *ssh = active_state; /* XXX */ 203 Authctxt *authctxt = ctxt; 204 Authmethod *m = NULL; 205 char *user, *service, *method, *style = NULL; 206 int authenticated = 0; 207 208 if (authctxt == NULL) 209 fatal("input_userauth_request: no authctxt"); 210 211 user = packet_get_cstring(NULL); 212 service = packet_get_cstring(NULL); 213 method = packet_get_cstring(NULL); 214 debug("userauth-request for user %s service %s method %s", user, service, method); 215 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); 216 217 if ((style = strchr(user, ':')) != NULL) 218 *style++ = 0; 219 220 if (authctxt->attempt++ == 0) { 221 /* setup auth context */ 222 authctxt->pw = PRIVSEP(getpwnamallow(user)); 223 if (authctxt->pw && strcmp(service, "ssh-connection")==0) { 224 authctxt->valid = 1; 225 debug2("%s: setting up authctxt for %s", 226 __func__, user); 227 } else { 228 /* Invalid user, fake password information */ 229 authctxt->pw = fakepw(); 230 } 231 ssh_packet_set_log_preamble(ssh, "%suser %s", 232 authctxt->valid ? "authenticating " : "invalid ", user); 233 setproctitle("%s%s", authctxt->valid ? user : "unknown", 234 use_privsep ? " [net]" : ""); 235 authctxt->user = xstrdup(user); 236 authctxt->service = xstrdup(service); 237 authctxt->style = style ? xstrdup(style) : NULL; 238 if (use_privsep) 239 mm_inform_authserv(service, style); 240 userauth_banner(); 241 if (auth2_setup_methods_lists(authctxt) != 0) 242 packet_disconnect("no authentication methods enabled"); 243 } else if (strcmp(user, authctxt->user) != 0 || 244 strcmp(service, authctxt->service) != 0) { 245 packet_disconnect("Change of username or service not allowed: " 246 "(%s,%s) -> (%s,%s)", 247 authctxt->user, authctxt->service, user, service); 248 } 249 /* reset state */ 250 auth2_challenge_stop(authctxt); 251 252 #ifdef GSSAPI 253 /* XXX move to auth2_gssapi_stop() */ 254 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 255 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); 256 #endif 257 258 authctxt->postponed = 0; 259 authctxt->server_caused_failure = 0; 260 261 /* try to authenticate user */ 262 m = authmethod_lookup(authctxt, method); 263 if (m != NULL && authctxt->failures < options.max_authtries) { 264 debug2("input_userauth_request: try method %s", method); 265 authenticated = m->userauth(authctxt); 266 } 267 userauth_finish(authctxt, authenticated, method, NULL); 268 269 free(service); 270 free(user); 271 free(method); 272 return 0; 273 } 274 275 void 276 userauth_finish(Authctxt *authctxt, int authenticated, const char *method, 277 const char *submethod) 278 { 279 struct ssh *ssh = active_state; /* XXX */ 280 char *methods; 281 int partial = 0; 282 283 if (!authctxt->valid && authenticated) 284 fatal("INTERNAL ERROR: authenticated invalid user %s", 285 authctxt->user); 286 if (authenticated && authctxt->postponed) 287 fatal("INTERNAL ERROR: authenticated and postponed"); 288 289 /* Special handling for root */ 290 if (authenticated && authctxt->pw->pw_uid == 0 && 291 !auth_root_allowed(method)) 292 authenticated = 0; 293 294 if (authenticated && options.num_auth_methods != 0) { 295 if (!auth2_update_methods_lists(authctxt, method, submethod)) { 296 authenticated = 0; 297 partial = 1; 298 } 299 } 300 301 /* Log before sending the reply */ 302 auth_log(authctxt, authenticated, partial, method, submethod); 303 304 if (authctxt->postponed) 305 return; 306 307 if (authenticated == 1) { 308 /* turn off userauth */ 309 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); 310 packet_start(SSH2_MSG_USERAUTH_SUCCESS); 311 packet_send(); 312 packet_write_wait(); 313 /* now we can break out */ 314 authctxt->success = 1; 315 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user); 316 } else { 317 /* Allow initial try of "none" auth without failure penalty */ 318 if (!partial && !authctxt->server_caused_failure && 319 (authctxt->attempt > 1 || strcmp(method, "none") != 0)) 320 authctxt->failures++; 321 if (authctxt->failures >= options.max_authtries) 322 auth_maxtries_exceeded(authctxt); 323 methods = authmethods_get(authctxt); 324 debug3("%s: failure partial=%d next methods=\"%s\"", __func__, 325 partial, methods); 326 packet_start(SSH2_MSG_USERAUTH_FAILURE); 327 packet_put_cstring(methods); 328 packet_put_char(partial); 329 packet_send(); 330 packet_write_wait(); 331 free(methods); 332 } 333 } 334 335 /* 336 * Checks whether method is allowed by at least one AuthenticationMethods 337 * methods list. Returns 1 if allowed, or no methods lists configured. 338 * 0 otherwise. 339 */ 340 int 341 auth2_method_allowed(Authctxt *authctxt, const char *method, 342 const char *submethod) 343 { 344 u_int i; 345 346 /* 347 * NB. authctxt->num_auth_methods might be zero as a result of 348 * auth2_setup_methods_lists(), so check the configuration. 349 */ 350 if (options.num_auth_methods == 0) 351 return 1; 352 for (i = 0; i < authctxt->num_auth_methods; i++) { 353 if (list_starts_with(authctxt->auth_methods[i], method, 354 submethod) != MATCH_NONE) 355 return 1; 356 } 357 return 0; 358 } 359 360 static char * 361 authmethods_get(Authctxt *authctxt) 362 { 363 Buffer b; 364 char *list; 365 u_int i; 366 367 buffer_init(&b); 368 for (i = 0; authmethods[i] != NULL; i++) { 369 if (strcmp(authmethods[i]->name, "none") == 0) 370 continue; 371 if (authmethods[i]->enabled == NULL || 372 *(authmethods[i]->enabled) == 0) 373 continue; 374 if (!auth2_method_allowed(authctxt, authmethods[i]->name, 375 NULL)) 376 continue; 377 if (buffer_len(&b) > 0) 378 buffer_append(&b, ",", 1); 379 buffer_append(&b, authmethods[i]->name, 380 strlen(authmethods[i]->name)); 381 } 382 if ((list = sshbuf_dup_string(&b)) == NULL) 383 fatal("%s: sshbuf_dup_string failed", __func__); 384 buffer_free(&b); 385 return list; 386 } 387 388 static Authmethod * 389 authmethod_lookup(Authctxt *authctxt, const char *name) 390 { 391 int i; 392 393 if (name != NULL) 394 for (i = 0; authmethods[i] != NULL; i++) 395 if (authmethods[i]->enabled != NULL && 396 *(authmethods[i]->enabled) != 0 && 397 strcmp(name, authmethods[i]->name) == 0 && 398 auth2_method_allowed(authctxt, 399 authmethods[i]->name, NULL)) 400 return authmethods[i]; 401 debug2("Unrecognized authentication method name: %s", 402 name ? name : "NULL"); 403 return NULL; 404 } 405 406 /* 407 * Check a comma-separated list of methods for validity. Is need_enable is 408 * non-zero, then also require that the methods are enabled. 409 * Returns 0 on success or -1 if the methods list is invalid. 410 */ 411 int 412 auth2_methods_valid(const char *_methods, int need_enable) 413 { 414 char *methods, *omethods, *method, *p; 415 u_int i, found; 416 int ret = -1; 417 418 if (*_methods == '\0') { 419 error("empty authentication method list"); 420 return -1; 421 } 422 omethods = methods = xstrdup(_methods); 423 while ((method = strsep(&methods, ",")) != NULL) { 424 for (found = i = 0; !found && authmethods[i] != NULL; i++) { 425 if ((p = strchr(method, ':')) != NULL) 426 *p = '\0'; 427 if (strcmp(method, authmethods[i]->name) != 0) 428 continue; 429 if (need_enable) { 430 if (authmethods[i]->enabled == NULL || 431 *(authmethods[i]->enabled) == 0) { 432 error("Disabled method \"%s\" in " 433 "AuthenticationMethods list \"%s\"", 434 method, _methods); 435 goto out; 436 } 437 } 438 found = 1; 439 break; 440 } 441 if (!found) { 442 error("Unknown authentication method \"%s\" in list", 443 method); 444 goto out; 445 } 446 } 447 ret = 0; 448 out: 449 free(omethods); 450 return ret; 451 } 452 453 /* 454 * Prune the AuthenticationMethods supplied in the configuration, removing 455 * any methods lists that include disabled methods. Note that this might 456 * leave authctxt->num_auth_methods == 0, even when multiple required auth 457 * has been requested. For this reason, all tests for whether multiple is 458 * enabled should consult options.num_auth_methods directly. 459 */ 460 int 461 auth2_setup_methods_lists(Authctxt *authctxt) 462 { 463 u_int i; 464 465 if (options.num_auth_methods == 0) 466 return 0; 467 debug3("%s: checking methods", __func__); 468 authctxt->auth_methods = xcalloc(options.num_auth_methods, 469 sizeof(*authctxt->auth_methods)); 470 authctxt->num_auth_methods = 0; 471 for (i = 0; i < options.num_auth_methods; i++) { 472 if (auth2_methods_valid(options.auth_methods[i], 1) != 0) { 473 logit("Authentication methods list \"%s\" contains " 474 "disabled method, skipping", 475 options.auth_methods[i]); 476 continue; 477 } 478 debug("authentication methods list %d: %s", 479 authctxt->num_auth_methods, options.auth_methods[i]); 480 authctxt->auth_methods[authctxt->num_auth_methods++] = 481 xstrdup(options.auth_methods[i]); 482 } 483 if (authctxt->num_auth_methods == 0) { 484 error("No AuthenticationMethods left after eliminating " 485 "disabled methods"); 486 return -1; 487 } 488 return 0; 489 } 490 491 static int 492 list_starts_with(const char *methods, const char *method, 493 const char *submethod) 494 { 495 size_t l = strlen(method); 496 int match; 497 const char *p; 498 499 if (strncmp(methods, method, l) != 0) 500 return MATCH_NONE; 501 p = methods + l; 502 match = MATCH_METHOD; 503 if (*p == ':') { 504 if (!submethod) 505 return MATCH_PARTIAL; 506 l = strlen(submethod); 507 p += 1; 508 if (strncmp(submethod, p, l)) 509 return MATCH_NONE; 510 p += l; 511 match = MATCH_BOTH; 512 } 513 if (*p != ',' && *p != '\0') 514 return MATCH_NONE; 515 return match; 516 } 517 518 /* 519 * Remove method from the start of a comma-separated list of methods. 520 * Returns 0 if the list of methods did not start with that method or 1 521 * if it did. 522 */ 523 static int 524 remove_method(char **methods, const char *method, const char *submethod) 525 { 526 char *omethods = *methods, *p; 527 size_t l = strlen(method); 528 int match; 529 530 match = list_starts_with(omethods, method, submethod); 531 if (match != MATCH_METHOD && match != MATCH_BOTH) 532 return 0; 533 p = omethods + l; 534 if (submethod && match == MATCH_BOTH) 535 p += 1 + strlen(submethod); /* include colon */ 536 if (*p == ',') 537 p++; 538 *methods = xstrdup(p); 539 free(omethods); 540 return 1; 541 } 542 543 /* 544 * Called after successful authentication. Will remove the successful method 545 * from the start of each list in which it occurs. If it was the last method 546 * in any list, then authentication is deemed successful. 547 * Returns 1 if the method completed any authentication list or 0 otherwise. 548 */ 549 int 550 auth2_update_methods_lists(Authctxt *authctxt, const char *method, 551 const char *submethod) 552 { 553 u_int i, found = 0; 554 555 debug3("%s: updating methods list after \"%s\"", __func__, method); 556 for (i = 0; i < authctxt->num_auth_methods; i++) { 557 if (!remove_method(&(authctxt->auth_methods[i]), method, 558 submethod)) 559 continue; 560 found = 1; 561 if (*authctxt->auth_methods[i] == '\0') { 562 debug2("authentication methods list %d complete", i); 563 return 1; 564 } 565 debug3("authentication methods list %d remaining: \"%s\"", 566 i, authctxt->auth_methods[i]); 567 } 568 /* This should not happen, but would be bad if it did */ 569 if (!found) 570 fatal("%s: method not in AuthenticationMethods", __func__); 571 return 0; 572 } 573 574 575