1 /* $OpenBSD: auth2.c,v 1.161 2021/04/03 06:18:40 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/types.h>
55 #include "pathnames.h"
57 #include "blacklist_client.h"
61 #include "monitor_wrap.h"
65 extern ServerOptions options;
66 extern struct sshbuf *loginmsg;
70 extern Authmethod method_none;
71 extern Authmethod method_pubkey;
72 extern Authmethod method_passwd;
73 extern Authmethod method_kbdint;
74 extern Authmethod method_hostbased;
76 extern Authmethod method_gssapi;
79 Authmethod *authmethods[] = {
93 static int input_service_request(int, u_int32_t, struct ssh *);
94 static int input_userauth_request(int, u_int32_t, struct ssh *);
97 static Authmethod *authmethod_lookup(Authctxt *, const char *);
98 static char *authmethods_get(Authctxt *authctxt);
100 #define MATCH_NONE 0 /* method or submethod mismatch */
101 #define MATCH_METHOD 1 /* method matches (no submethod specified) */
102 #define MATCH_BOTH 2 /* method and submethod match */
103 #define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */
104 static int list_starts_with(const char *, const char *, const char *);
107 auth2_read_banner(void)
114 if ((fd = open(options.banner, O_RDONLY)) == -1)
116 if (fstat(fd, &st) == -1) {
120 if (st.st_size <= 0 || st.st_size > 1*1024*1024) {
125 len = (size_t)st.st_size; /* truncate */
126 banner = xmalloc(len + 1);
127 n = atomicio(read, fd, banner, len);
140 userauth_send_banner(struct ssh *ssh, const char *msg)
144 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_BANNER)) != 0 ||
145 (r = sshpkt_put_cstring(ssh, msg)) != 0 ||
146 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language, unused */
147 (r = sshpkt_send(ssh)) != 0)
148 fatal_fr(r, "send packet");
149 debug("%s: sent", __func__);
153 userauth_banner(struct ssh *ssh)
157 if (options.banner == NULL)
160 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
162 userauth_send_banner(ssh, banner);
169 * loop until authctxt->success == TRUE
172 do_authentication2(struct ssh *ssh)
174 Authctxt *authctxt = ssh->authctxt;
176 ssh_dispatch_init(ssh, &dispatch_protocol_error);
177 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request);
178 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success);
179 ssh->authctxt = NULL;
184 input_service_request(int type, u_int32_t seq, struct ssh *ssh)
186 Authctxt *authctxt = ssh->authctxt;
187 char *service = NULL;
190 if ((r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
191 (r = sshpkt_get_end(ssh)) != 0)
194 if (authctxt == NULL)
195 fatal("input_service_request: no authctxt");
197 if (strcmp(service, "ssh-userauth") == 0) {
198 if (!authctxt->success) {
200 /* now we can handle user-auth requests */
201 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
202 &input_userauth_request);
205 /* XXX all other service requests are denied */
208 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_ACCEPT)) != 0 ||
209 (r = sshpkt_put_cstring(ssh, service)) != 0 ||
210 (r = sshpkt_send(ssh)) != 0 ||
211 (r = ssh_packet_write_wait(ssh)) != 0)
214 debug("bad service request %s", service);
215 ssh_packet_disconnect(ssh, "bad service request %s", service);
223 #define MIN_FAIL_DELAY_SECONDS 0.005
225 user_specific_delay(const char *user)
228 size_t len = ssh_digest_bytes(SSH_DIGEST_SHA512);
229 u_char *hash = xmalloc(len);
232 (void)snprintf(b, sizeof b, "%llu%s",
233 (unsigned long long)options.timing_secret, user);
234 if (ssh_digest_memory(SSH_DIGEST_SHA512, b, strlen(b), hash, len) != 0)
235 fatal_f("ssh_digest_memory");
236 /* 0-4.2 ms of delay */
237 delay = (double)PEEK_U32(hash) / 1000 / 1000 / 1000 / 1000;
239 debug3_f("user specific delay %0.3lfms", delay/1000);
240 return MIN_FAIL_DELAY_SECONDS + delay;
244 ensure_minimum_time_since(double start, double seconds)
247 double elapsed = monotime_double() - start, req = seconds, remain;
249 /* if we've already passed the requested time, scale up */
250 while ((remain = seconds - elapsed) < 0.0)
254 ts.tv_nsec = (remain - ts.tv_sec) * 1000000000;
255 debug3_f("elapsed %0.3lfms, delaying %0.3lfms (requested %0.3lfms)",
256 elapsed*1000, remain*1000, req*1000);
257 nanosleep(&ts, NULL);
262 input_userauth_request(int type, u_int32_t seq, struct ssh *ssh)
264 Authctxt *authctxt = ssh->authctxt;
265 Authmethod *m = NULL;
266 char *user = NULL, *service = NULL, *method = NULL, *style = NULL;
267 int r, authenticated = 0;
268 double tstart = monotime_double();
270 if (authctxt == NULL)
271 fatal("input_userauth_request: no authctxt");
273 if ((r = sshpkt_get_cstring(ssh, &user, NULL)) != 0 ||
274 (r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
275 (r = sshpkt_get_cstring(ssh, &method, NULL)) != 0)
277 debug("userauth-request for user %s service %s method %s", user, service, method);
278 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
280 if ((style = strchr(user, ':')) != NULL)
283 if (authctxt->attempt++ == 0) {
284 /* setup auth context */
285 authctxt->pw = PRIVSEP(getpwnamallow(ssh, user));
286 authctxt->user = xstrdup(user);
287 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
289 debug2_f("setting up authctxt for %s", user);
291 /* Invalid user, fake password information */
292 authctxt->pw = fakepw();
293 #ifdef SSH_AUDIT_EVENTS
294 PRIVSEP(audit_event(ssh, SSH_INVALID_USER));
299 PRIVSEP(start_pam(ssh));
301 ssh_packet_set_log_preamble(ssh, "%suser %s",
302 authctxt->valid ? "authenticating " : "invalid ", user);
303 setproctitle("%s%s", authctxt->valid ? user : "unknown",
304 use_privsep ? " [net]" : "");
305 authctxt->service = xstrdup(service);
306 authctxt->style = style ? xstrdup(style) : NULL;
308 mm_inform_authserv(service, style);
309 userauth_banner(ssh);
310 if (auth2_setup_methods_lists(authctxt) != 0)
311 ssh_packet_disconnect(ssh,
312 "no authentication methods enabled");
313 } else if (strcmp(user, authctxt->user) != 0 ||
314 strcmp(service, authctxt->service) != 0) {
315 ssh_packet_disconnect(ssh, "Change of username or service "
316 "not allowed: (%s,%s) -> (%s,%s)",
317 authctxt->user, authctxt->service, user, service);
320 auth2_challenge_stop(ssh);
323 /* XXX move to auth2_gssapi_stop() */
324 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
325 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
328 auth2_authctxt_reset_info(authctxt);
329 authctxt->postponed = 0;
330 authctxt->server_caused_failure = 0;
332 /* try to authenticate user */
333 m = authmethod_lookup(authctxt, method);
334 if (m != NULL && authctxt->failures < options.max_authtries) {
335 debug2("input_userauth_request: try method %s", method);
336 authenticated = m->userauth(ssh);
338 if (!authctxt->authenticated)
339 ensure_minimum_time_since(tstart,
340 user_specific_delay(authctxt->user));
341 userauth_finish(ssh, authenticated, method, NULL);
351 userauth_finish(struct ssh *ssh, int authenticated, const char *method,
352 const char *submethod)
354 Authctxt *authctxt = ssh->authctxt;
358 if (!authctxt->valid && authenticated)
359 fatal("INTERNAL ERROR: authenticated invalid user %s",
361 if (authenticated && authctxt->postponed)
362 fatal("INTERNAL ERROR: authenticated and postponed");
364 /* Special handling for root */
365 if (authenticated && authctxt->pw->pw_uid == 0 &&
366 !auth_root_allowed(ssh, method)) {
368 #ifdef SSH_AUDIT_EVENTS
369 PRIVSEP(audit_event(ssh, SSH_LOGIN_ROOT_DENIED));
373 if (authenticated && options.num_auth_methods != 0) {
374 if (!auth2_update_methods_lists(authctxt, method, submethod)) {
380 /* Log before sending the reply */
381 auth_log(ssh, authenticated, partial, method, submethod);
383 /* Update information exposed to session */
384 if (authenticated || partial)
385 auth2_update_session_info(authctxt, method, submethod);
387 if (authctxt->postponed)
391 if (options.use_pam && authenticated) {
392 int r, success = PRIVSEP(do_pam_account());
394 /* If PAM returned a message, send it to the user. */
395 if (sshbuf_len(loginmsg) > 0) {
396 if ((r = sshbuf_put(loginmsg, "\0", 1)) != 0)
397 fatal("%s: buffer error: %s",
398 __func__, ssh_err(r));
399 userauth_send_banner(ssh, sshbuf_ptr(loginmsg));
400 if ((r = ssh_packet_write_wait(ssh)) != 0) {
402 "%s: send PAM banner", __func__);
406 fatal("Access denied for user %s by PAM account "
407 "configuration", authctxt->user);
412 if (authenticated == 1) {
413 /* turn off userauth */
414 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
415 &dispatch_protocol_ignore);
416 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_SUCCESS)) != 0 ||
417 (r = sshpkt_send(ssh)) != 0 ||
418 (r = ssh_packet_write_wait(ssh)) != 0)
419 fatal_fr(r, "send success packet");
420 /* now we can break out */
421 authctxt->success = 1;
422 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
424 /* Allow initial try of "none" auth without failure penalty */
425 if (!partial && !authctxt->server_caused_failure &&
426 (authctxt->attempt > 1 || strcmp(method, "none") != 0)) {
427 authctxt->failures++;
428 BLACKLIST_NOTIFY(ssh, BLACKLIST_AUTH_FAIL, "ssh");
430 if (authctxt->failures >= options.max_authtries) {
431 #ifdef SSH_AUDIT_EVENTS
432 PRIVSEP(audit_event(ssh, SSH_LOGIN_EXCEED_MAXTRIES));
434 auth_maxtries_exceeded(ssh);
436 methods = authmethods_get(authctxt);
437 debug3_f("failure partial=%d next methods=\"%s\"",
439 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_FAILURE)) != 0 ||
440 (r = sshpkt_put_cstring(ssh, methods)) != 0 ||
441 (r = sshpkt_put_u8(ssh, partial)) != 0 ||
442 (r = sshpkt_send(ssh)) != 0 ||
443 (r = ssh_packet_write_wait(ssh)) != 0)
444 fatal_fr(r, "send failure packet");
450 * Checks whether method is allowed by at least one AuthenticationMethods
451 * methods list. Returns 1 if allowed, or no methods lists configured.
455 auth2_method_allowed(Authctxt *authctxt, const char *method,
456 const char *submethod)
461 * NB. authctxt->num_auth_methods might be zero as a result of
462 * auth2_setup_methods_lists(), so check the configuration.
464 if (options.num_auth_methods == 0)
466 for (i = 0; i < authctxt->num_auth_methods; i++) {
467 if (list_starts_with(authctxt->auth_methods[i], method,
468 submethod) != MATCH_NONE)
475 authmethods_get(Authctxt *authctxt)
481 if ((b = sshbuf_new()) == NULL)
482 fatal_f("sshbuf_new failed");
483 for (i = 0; authmethods[i] != NULL; i++) {
484 if (strcmp(authmethods[i]->name, "none") == 0)
486 if (authmethods[i]->enabled == NULL ||
487 *(authmethods[i]->enabled) == 0)
489 if (!auth2_method_allowed(authctxt, authmethods[i]->name,
492 if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) ? "," : "",
493 authmethods[i]->name)) != 0)
494 fatal_fr(r, "buffer error");
496 if ((list = sshbuf_dup_string(b)) == NULL)
497 fatal_f("sshbuf_dup_string failed");
503 authmethod_lookup(Authctxt *authctxt, const char *name)
508 for (i = 0; authmethods[i] != NULL; i++)
509 if (authmethods[i]->enabled != NULL &&
510 *(authmethods[i]->enabled) != 0 &&
511 strcmp(name, authmethods[i]->name) == 0 &&
512 auth2_method_allowed(authctxt,
513 authmethods[i]->name, NULL))
514 return authmethods[i];
515 debug2("Unrecognized authentication method name: %s",
516 name ? name : "NULL");
521 * Check a comma-separated list of methods for validity. Is need_enable is
522 * non-zero, then also require that the methods are enabled.
523 * Returns 0 on success or -1 if the methods list is invalid.
526 auth2_methods_valid(const char *_methods, int need_enable)
528 char *methods, *omethods, *method, *p;
532 if (*_methods == '\0') {
533 error("empty authentication method list");
536 omethods = methods = xstrdup(_methods);
537 while ((method = strsep(&methods, ",")) != NULL) {
538 for (found = i = 0; !found && authmethods[i] != NULL; i++) {
539 if ((p = strchr(method, ':')) != NULL)
541 if (strcmp(method, authmethods[i]->name) != 0)
544 if (authmethods[i]->enabled == NULL ||
545 *(authmethods[i]->enabled) == 0) {
546 error("Disabled method \"%s\" in "
547 "AuthenticationMethods list \"%s\"",
556 error("Unknown authentication method \"%s\" in list",
568 * Prune the AuthenticationMethods supplied in the configuration, removing
569 * any methods lists that include disabled methods. Note that this might
570 * leave authctxt->num_auth_methods == 0, even when multiple required auth
571 * has been requested. For this reason, all tests for whether multiple is
572 * enabled should consult options.num_auth_methods directly.
575 auth2_setup_methods_lists(Authctxt *authctxt)
579 /* First, normalise away the "any" pseudo-method */
580 if (options.num_auth_methods == 1 &&
581 strcmp(options.auth_methods[0], "any") == 0) {
582 free(options.auth_methods[0]);
583 options.auth_methods[0] = NULL;
584 options.num_auth_methods = 0;
587 if (options.num_auth_methods == 0)
589 debug3_f("checking methods");
590 authctxt->auth_methods = xcalloc(options.num_auth_methods,
591 sizeof(*authctxt->auth_methods));
592 authctxt->num_auth_methods = 0;
593 for (i = 0; i < options.num_auth_methods; i++) {
594 if (auth2_methods_valid(options.auth_methods[i], 1) != 0) {
595 logit("Authentication methods list \"%s\" contains "
596 "disabled method, skipping",
597 options.auth_methods[i]);
600 debug("authentication methods list %d: %s",
601 authctxt->num_auth_methods, options.auth_methods[i]);
602 authctxt->auth_methods[authctxt->num_auth_methods++] =
603 xstrdup(options.auth_methods[i]);
605 if (authctxt->num_auth_methods == 0) {
606 error("No AuthenticationMethods left after eliminating "
614 list_starts_with(const char *methods, const char *method,
615 const char *submethod)
617 size_t l = strlen(method);
621 if (strncmp(methods, method, l) != 0)
624 match = MATCH_METHOD;
627 return MATCH_PARTIAL;
628 l = strlen(submethod);
630 if (strncmp(submethod, p, l))
635 if (*p != ',' && *p != '\0')
641 * Remove method from the start of a comma-separated list of methods.
642 * Returns 0 if the list of methods did not start with that method or 1
646 remove_method(char **methods, const char *method, const char *submethod)
648 char *omethods = *methods, *p;
649 size_t l = strlen(method);
652 match = list_starts_with(omethods, method, submethod);
653 if (match != MATCH_METHOD && match != MATCH_BOTH)
656 if (submethod && match == MATCH_BOTH)
657 p += 1 + strlen(submethod); /* include colon */
660 *methods = xstrdup(p);
666 * Called after successful authentication. Will remove the successful method
667 * from the start of each list in which it occurs. If it was the last method
668 * in any list, then authentication is deemed successful.
669 * Returns 1 if the method completed any authentication list or 0 otherwise.
672 auth2_update_methods_lists(Authctxt *authctxt, const char *method,
673 const char *submethod)
677 debug3_f("updating methods list after \"%s\"", method);
678 for (i = 0; i < authctxt->num_auth_methods; i++) {
679 if (!remove_method(&(authctxt->auth_methods[i]), method,
683 if (*authctxt->auth_methods[i] == '\0') {
684 debug2("authentication methods list %d complete", i);
687 debug3("authentication methods list %d remaining: \"%s\"",
688 i, authctxt->auth_methods[i]);
690 /* This should not happen, but would be bad if it did */
692 fatal_f("method not in AuthenticationMethods");
696 /* Reset method-specific information */
697 void auth2_authctxt_reset_info(Authctxt *authctxt)
699 sshkey_free(authctxt->auth_method_key);
700 free(authctxt->auth_method_info);
701 authctxt->auth_method_key = NULL;
702 authctxt->auth_method_info = NULL;
705 /* Record auth method-specific information for logs */
707 auth2_record_info(Authctxt *authctxt, const char *fmt, ...)
712 free(authctxt->auth_method_info);
713 authctxt->auth_method_info = NULL;
716 i = vasprintf(&authctxt->auth_method_info, fmt, ap);
720 fatal_f("vasprintf failed");
724 * Records a public key used in authentication. This is used for logging
725 * and to ensure that the same key is not subsequently accepted again for
726 * multiple authentication.
729 auth2_record_key(Authctxt *authctxt, int authenticated,
730 const struct sshkey *key)
732 struct sshkey **tmp, *dup;
735 if ((r = sshkey_from_private(key, &dup)) != 0)
736 fatal_fr(r, "copy key");
737 sshkey_free(authctxt->auth_method_key);
738 authctxt->auth_method_key = dup;
743 /* If authenticated, make sure we don't accept this key again */
744 if ((r = sshkey_from_private(key, &dup)) != 0)
745 fatal_fr(r, "copy key");
746 if (authctxt->nprev_keys >= INT_MAX ||
747 (tmp = recallocarray(authctxt->prev_keys, authctxt->nprev_keys,
748 authctxt->nprev_keys + 1, sizeof(*authctxt->prev_keys))) == NULL)
749 fatal_f("reallocarray failed");
750 authctxt->prev_keys = tmp;
751 authctxt->prev_keys[authctxt->nprev_keys] = dup;
752 authctxt->nprev_keys++;
756 /* Checks whether a key has already been previously used for authentication */
758 auth2_key_already_used(Authctxt *authctxt, const struct sshkey *key)
763 for (i = 0; i < authctxt->nprev_keys; i++) {
764 if (sshkey_equal_public(key, authctxt->prev_keys[i])) {
765 fp = sshkey_fingerprint(authctxt->prev_keys[i],
766 options.fingerprint_hash, SSH_FP_DEFAULT);
767 debug3_f("key already used: %s %s",
768 sshkey_type(authctxt->prev_keys[i]),
769 fp == NULL ? "UNKNOWN" : fp);
778 * Updates authctxt->session_info with details of authentication. Should be
779 * whenever an authentication method succeeds.
782 auth2_update_session_info(Authctxt *authctxt, const char *method,
783 const char *submethod)
787 if (authctxt->session_info == NULL) {
788 if ((authctxt->session_info = sshbuf_new()) == NULL)
789 fatal_f("sshbuf_new");
792 /* Append method[/submethod] */
793 if ((r = sshbuf_putf(authctxt->session_info, "%s%s%s",
794 method, submethod == NULL ? "" : "/",
795 submethod == NULL ? "" : submethod)) != 0)
796 fatal_fr(r, "append method");
798 /* Append key if present */
799 if (authctxt->auth_method_key != NULL) {
800 if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
801 (r = sshkey_format_text(authctxt->auth_method_key,
802 authctxt->session_info)) != 0)
803 fatal_fr(r, "append key");
806 if (authctxt->auth_method_info != NULL) {
807 /* Ensure no ambiguity here */
808 if (strchr(authctxt->auth_method_info, '\n') != NULL)
809 fatal_f("auth_method_info contains \\n");
810 if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
811 (r = sshbuf_putf(authctxt->session_info, "%s",
812 authctxt->auth_method_info)) != 0) {
813 fatal_fr(r, "append method info");
816 if ((r = sshbuf_put_u8(authctxt->session_info, '\n')) != 0)
817 fatal_fr(r, "append");