Home | History | Annotate | Line # | Download | only in dist
      1  1.33  christos /*	$NetBSD: auth2.c,v 1.35 2026/04/08 18:58:40 christos Exp $	*/
      2  1.35  christos /* $OpenBSD: auth2.c,v 1.173 2026/03/03 09:57:25 dtucker Exp $ */
      3  1.28  christos 
      4   1.1  christos /*
      5   1.1  christos  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * Redistribution and use in source and binary forms, with or without
      8   1.1  christos  * modification, are permitted provided that the following conditions
      9   1.1  christos  * are met:
     10   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  christos  *    documentation and/or other materials provided with the distribution.
     15   1.1  christos  *
     16   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1  christos  */
     27   1.1  christos 
     28   1.2  christos #include "includes.h"
     29  1.33  christos __RCSID("$NetBSD: auth2.c,v 1.35 2026/04/08 18:58:40 christos Exp $");
     30  1.20  christos 
     31   1.1  christos #include <sys/types.h>
     32   1.1  christos #include <sys/stat.h>
     33   1.1  christos #include <sys/uio.h>
     34   1.1  christos 
     35   1.1  christos #include <fcntl.h>
     36  1.15  christos #include <limits.h>
     37   1.1  christos #include <pwd.h>
     38   1.3  christos #include <stdarg.h>
     39   1.1  christos #include <string.h>
     40   1.1  christos #include <unistd.h>
     41  1.19  christos #include <time.h>
     42   1.1  christos 
     43  1.20  christos #include "stdlib.h"
     44   1.1  christos #include "atomicio.h"
     45   1.1  christos #include "xmalloc.h"
     46   1.1  christos #include "ssh2.h"
     47   1.1  christos #include "packet.h"
     48   1.1  christos #include "log.h"
     49  1.17  christos #include "sshbuf.h"
     50   1.9  christos #include "misc.h"
     51   1.1  christos #include "servconf.h"
     52  1.17  christos #include "sshkey.h"
     53   1.1  christos #include "hostfile.h"
     54   1.1  christos #include "auth.h"
     55   1.1  christos #include "dispatch.h"
     56   1.1  christos #include "pathnames.h"
     57   1.2  christos 
     58  1.35  christos #include "ssherr.h"
     59   1.1  christos #ifdef GSSAPI
     60   1.1  christos #include "ssh-gss.h"
     61   1.1  christos #endif
     62   1.2  christos 
     63   1.1  christos #include "monitor_wrap.h"
     64  1.17  christos #include "digest.h"
     65  1.29  christos #include "kex.h"
     66   1.1  christos 
     67   1.1  christos /* import */
     68   1.1  christos extern ServerOptions options;
     69  1.17  christos extern struct sshbuf *loginmsg;
     70   1.1  christos 
     71   1.1  christos /* methods */
     72   1.1  christos 
     73   1.1  christos extern Authmethod method_none;
     74   1.1  christos extern Authmethod method_pubkey;
     75   1.1  christos extern Authmethod method_passwd;
     76   1.1  christos extern Authmethod method_kbdint;
     77   1.1  christos extern Authmethod method_hostbased;
     78   1.2  christos #ifdef KRB5
     79   1.2  christos extern Authmethod method_kerberos;
     80   1.2  christos #endif
     81   1.1  christos #ifdef GSSAPI
     82   1.1  christos extern Authmethod method_gssapi;
     83   1.1  christos #endif
     84   1.1  christos 
     85   1.2  christos static int log_flag = 0;
     86   1.2  christos 
     87   1.1  christos Authmethod *authmethods[] = {
     88   1.1  christos 	&method_none,
     89   1.1  christos 	&method_pubkey,
     90   1.1  christos #ifdef GSSAPI
     91   1.1  christos 	&method_gssapi,
     92   1.1  christos #endif
     93   1.1  christos 	&method_passwd,
     94   1.1  christos 	&method_kbdint,
     95   1.1  christos 	&method_hostbased,
     96   1.2  christos #ifdef KRB5
     97   1.2  christos 	&method_kerberos,
     98   1.2  christos #endif
     99   1.1  christos 	NULL
    100   1.1  christos };
    101   1.1  christos 
    102   1.1  christos /* protocol */
    103   1.1  christos 
    104  1.35  christos static int input_service_request(int, uint32_t, struct ssh *);
    105  1.35  christos static int input_userauth_request(int, uint32_t, struct ssh *);
    106   1.1  christos 
    107   1.1  christos /* helper */
    108  1.25  christos static Authmethod *authmethod_byname(const char *);
    109   1.7  christos static Authmethod *authmethod_lookup(Authctxt *, const char *);
    110   1.7  christos static char *authmethods_get(Authctxt *authctxt);
    111   1.8  christos 
    112   1.8  christos #define MATCH_NONE	0	/* method or submethod mismatch */
    113   1.8  christos #define MATCH_METHOD	1	/* method matches (no submethod specified) */
    114   1.8  christos #define MATCH_BOTH	2	/* method and submethod match */
    115   1.8  christos #define MATCH_PARTIAL	3	/* method matches, submethod can't be checked */
    116   1.8  christos static int list_starts_with(const char *, const char *, const char *);
    117   1.1  christos 
    118   1.1  christos char *
    119   1.1  christos auth2_read_banner(void)
    120   1.1  christos {
    121   1.1  christos 	struct stat st;
    122   1.1  christos 	char *banner = NULL;
    123   1.1  christos 	size_t len, n;
    124   1.1  christos 	int fd;
    125   1.1  christos 
    126   1.1  christos 	if ((fd = open(options.banner, O_RDONLY)) == -1)
    127   1.1  christos 		return (NULL);
    128   1.1  christos 	if (fstat(fd, &st) == -1) {
    129   1.1  christos 		close(fd);
    130   1.1  christos 		return (NULL);
    131   1.1  christos 	}
    132   1.6  christos 	if (st.st_size <= 0 || st.st_size > 1*1024*1024) {
    133   1.1  christos 		close(fd);
    134   1.1  christos 		return (NULL);
    135   1.1  christos 	}
    136   1.1  christos 
    137   1.1  christos 	len = (size_t)st.st_size;		/* truncate */
    138   1.1  christos 	banner = xmalloc(len + 1);
    139   1.1  christos 	n = atomicio(read, fd, banner, len);
    140   1.1  christos 	close(fd);
    141   1.1  christos 
    142   1.1  christos 	if (n != len) {
    143   1.8  christos 		free(banner);
    144   1.1  christos 		return (NULL);
    145   1.1  christos 	}
    146   1.1  christos 	banner[n] = '\0';
    147   1.1  christos 
    148   1.1  christos 	return (banner);
    149   1.1  christos }
    150   1.1  christos 
    151   1.7  christos static void
    152  1.19  christos userauth_send_banner(struct ssh *ssh, const char *msg)
    153   1.2  christos {
    154  1.19  christos 	int r;
    155  1.19  christos 
    156  1.19  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_BANNER)) != 0 ||
    157  1.19  christos 	    (r = sshpkt_put_cstring(ssh, msg)) != 0 ||
    158  1.19  christos 	    (r = sshpkt_put_cstring(ssh, "")) != 0 ||	/* language, unused */
    159  1.19  christos 	    (r = sshpkt_send(ssh)) != 0)
    160  1.26       kre 		fatal_fr(r, "send packet");
    161   1.2  christos 	debug("%s: sent", __func__);
    162   1.2  christos }
    163   1.2  christos 
    164   1.1  christos static void
    165  1.19  christos userauth_banner(struct ssh *ssh)
    166   1.1  christos {
    167   1.1  christos 	char *banner = NULL;
    168   1.1  christos 
    169  1.16  christos 	if (options.banner == NULL)
    170   1.1  christos 		return;
    171   1.1  christos 
    172  1.31  christos 	if ((banner = mm_auth2_read_banner()) == NULL)
    173   1.1  christos 		goto done;
    174  1.19  christos 	userauth_send_banner(ssh, banner);
    175   1.1  christos 
    176   1.1  christos done:
    177   1.8  christos 	free(banner);
    178   1.1  christos }
    179   1.1  christos 
    180   1.1  christos /*
    181   1.1  christos  * loop until authctxt->success == TRUE
    182   1.1  christos  */
    183   1.1  christos void
    184  1.19  christos do_authentication2(struct ssh *ssh)
    185   1.1  christos {
    186  1.19  christos 	Authctxt *authctxt = ssh->authctxt;
    187  1.19  christos 
    188  1.15  christos 	ssh_dispatch_init(ssh, &dispatch_protocol_error);
    189  1.29  christos 	if (ssh->kex->ext_info_c)
    190  1.29  christos 		ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_input_ext_info);
    191  1.15  christos 	ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request);
    192  1.15  christos 	ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success);
    193  1.15  christos 	ssh->authctxt = NULL;
    194   1.1  christos }
    195   1.1  christos 
    196  1.11  christos static int
    197  1.35  christos input_service_request(int type, uint32_t seq, struct ssh *ssh)
    198   1.1  christos {
    199  1.15  christos 	Authctxt *authctxt = ssh->authctxt;
    200  1.19  christos 	char *service = NULL;
    201  1.19  christos 	int r, acceptit = 0;
    202  1.19  christos 
    203  1.19  christos 	if ((r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
    204  1.19  christos 	    (r = sshpkt_get_end(ssh)) != 0)
    205  1.19  christos 		goto out;
    206   1.1  christos 
    207   1.1  christos 	if (authctxt == NULL)
    208   1.1  christos 		fatal("input_service_request: no authctxt");
    209   1.1  christos 
    210   1.1  christos 	if (strcmp(service, "ssh-userauth") == 0) {
    211   1.1  christos 		if (!authctxt->success) {
    212   1.1  christos 			acceptit = 1;
    213   1.1  christos 			/* now we can handle user-auth requests */
    214  1.19  christos 			ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
    215  1.19  christos 			    &input_userauth_request);
    216   1.1  christos 		}
    217   1.1  christos 	}
    218   1.1  christos 	/* XXX all other service requests are denied */
    219   1.1  christos 
    220   1.1  christos 	if (acceptit) {
    221  1.19  christos 		if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_ACCEPT)) != 0 ||
    222  1.19  christos 		    (r = sshpkt_put_cstring(ssh, service)) != 0 ||
    223  1.19  christos 		    (r = sshpkt_send(ssh)) != 0 ||
    224  1.19  christos 		    (r = ssh_packet_write_wait(ssh)) < 0)
    225  1.19  christos 			goto out;
    226   1.1  christos 	} else {
    227   1.1  christos 		debug("bad service request %s", service);
    228  1.19  christos 		ssh_packet_disconnect(ssh, "bad service request %s", service);
    229   1.1  christos 	}
    230  1.29  christos 	ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &dispatch_protocol_error);
    231  1.19  christos 	r = 0;
    232  1.19  christos  out:
    233   1.8  christos 	free(service);
    234  1.22  christos 	return r;
    235   1.1  christos }
    236   1.1  christos 
    237  1.17  christos #define MIN_FAIL_DELAY_SECONDS 0.005
    238  1.28  christos #define MAX_FAIL_DELAY_SECONDS 5.0
    239  1.17  christos static double
    240  1.17  christos user_specific_delay(const char *user)
    241  1.17  christos {
    242  1.17  christos 	char b[512];
    243  1.17  christos 	size_t len = ssh_digest_bytes(SSH_DIGEST_SHA512);
    244  1.17  christos 	u_char *hash = xmalloc(len);
    245  1.17  christos 	double delay;
    246  1.17  christos 
    247  1.17  christos 	(void)snprintf(b, sizeof b, "%llu%s",
    248  1.24  christos 	    (unsigned long long)options.timing_secret, user);
    249  1.17  christos 	if (ssh_digest_memory(SSH_DIGEST_SHA512, b, strlen(b), hash, len) != 0)
    250  1.23  christos 		fatal_f("ssh_digest_memory");
    251  1.17  christos 	/* 0-4.2 ms of delay */
    252  1.17  christos 	delay = (double)PEEK_U32(hash) / 1000 / 1000 / 1000 / 1000;
    253  1.17  christos 	freezero(hash, len);
    254  1.34  christos 	debug3_f("user specific delay %0.3lfms", delay*1000);
    255  1.17  christos 	return MIN_FAIL_DELAY_SECONDS + delay;
    256  1.17  christos }
    257  1.17  christos 
    258  1.17  christos static void
    259  1.17  christos ensure_minimum_time_since(double start, double seconds)
    260  1.17  christos {
    261  1.17  christos 	struct timespec ts;
    262  1.17  christos 	double elapsed = monotime_double() - start, req = seconds, remain;
    263  1.17  christos 
    264  1.28  christos 	if (elapsed > MAX_FAIL_DELAY_SECONDS) {
    265  1.28  christos 		debug3_f("elapsed %0.3lfms exceeded the max delay "
    266  1.28  christos 		    "requested %0.3lfms)", elapsed*1000, req*1000);
    267  1.28  christos 		return;
    268  1.28  christos 	}
    269  1.28  christos 
    270  1.17  christos 	/* if we've already passed the requested time, scale up */
    271  1.17  christos 	while ((remain = seconds - elapsed) < 0.0)
    272  1.17  christos 		seconds *= 2;
    273  1.17  christos 
    274  1.17  christos 	ts.tv_sec = remain;
    275  1.17  christos 	ts.tv_nsec = (remain - ts.tv_sec) * 1000000000;
    276  1.23  christos 	debug3_f("elapsed %0.3lfms, delaying %0.3lfms (requested %0.3lfms)",
    277  1.23  christos 	    elapsed*1000, remain*1000, req*1000);
    278  1.17  christos 	nanosleep(&ts, NULL);
    279  1.17  christos }
    280  1.17  christos 
    281  1.11  christos static int
    282  1.35  christos input_userauth_request(int type, uint32_t seq, struct ssh *ssh)
    283   1.1  christos {
    284  1.15  christos 	Authctxt *authctxt = ssh->authctxt;
    285   1.1  christos 	Authmethod *m = NULL;
    286  1.19  christos 	char *user = NULL, *service = NULL, *method = NULL, *style = NULL;
    287  1.19  christos 	int r, authenticated = 0;
    288  1.17  christos 	double tstart = monotime_double();
    289   1.1  christos 
    290   1.1  christos 	if (authctxt == NULL)
    291   1.1  christos 		fatal("input_userauth_request: no authctxt");
    292   1.1  christos 
    293  1.19  christos 	if ((r = sshpkt_get_cstring(ssh, &user, NULL)) != 0 ||
    294  1.19  christos 	    (r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
    295  1.19  christos 	    (r = sshpkt_get_cstring(ssh, &method, NULL)) != 0)
    296  1.19  christos 		goto out;
    297   1.1  christos 	debug("userauth-request for user %s service %s method %s", user, service, method);
    298   1.2  christos 	if (!log_flag) {
    299   1.2  christos 		logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
    300  1.12  christos 		      ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), user);
    301   1.2  christos 		log_flag = 1;
    302   1.2  christos 	}
    303   1.1  christos 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
    304   1.1  christos 
    305   1.1  christos 	if ((style = strchr(user, ':')) != NULL)
    306   1.1  christos 		*style++ = 0;
    307   1.1  christos 
    308  1.25  christos 	if (authctxt->attempt >= 1024)
    309  1.25  christos 		auth_maxtries_exceeded(ssh);
    310   1.1  christos 	if (authctxt->attempt++ == 0) {
    311   1.1  christos 		/* setup auth context */
    312  1.31  christos 		authctxt->pw = mm_getpwnamallow(ssh, user);
    313  1.35  christos 		authctxt->user = xstrdup(user);
    314  1.35  christos 		authctxt->service = xstrdup(service);
    315  1.35  christos 		authctxt->style = style ? xstrdup(style) : NULL;
    316   1.1  christos 		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
    317   1.1  christos 			authctxt->valid = 1;
    318  1.23  christos 			debug2_f("setting up authctxt for %s", user);
    319   1.1  christos 		} else {
    320  1.25  christos 			authctxt->valid = 0;
    321  1.14  christos 			/* Invalid user, fake password information */
    322   1.1  christos 			authctxt->pw = fakepw();
    323   1.1  christos 		}
    324   1.2  christos #ifdef USE_PAM
    325   1.2  christos 		if (options.use_pam)
    326  1.31  christos 			mm_start_pam(ssh);
    327   1.2  christos #endif
    328  1.14  christos 		ssh_packet_set_log_preamble(ssh, "%suser %s",
    329  1.14  christos 		    authctxt->valid ? "authenticating " : "invalid ", user);
    330  1.31  christos 		setproctitle("%s [net]", authctxt->valid ? user : "unknown");
    331  1.31  christos 		mm_inform_authserv(service, style);
    332  1.19  christos 		userauth_banner(ssh);
    333  1.29  christos 		if ((r = kex_server_update_ext_info(ssh)) != 0)
    334  1.29  christos 			fatal_fr(r, "kex_server_update_ext_info failed");
    335   1.7  christos 		if (auth2_setup_methods_lists(authctxt) != 0)
    336  1.19  christos 			ssh_packet_disconnect(ssh,
    337  1.19  christos 			    "no authentication methods enabled");
    338   1.1  christos 	} else if (strcmp(user, authctxt->user) != 0 ||
    339   1.1  christos 	    strcmp(service, authctxt->service) != 0) {
    340  1.19  christos 		ssh_packet_disconnect(ssh, "Change of username or service "
    341  1.19  christos 		    "not allowed: (%s,%s) -> (%s,%s)",
    342   1.1  christos 		    authctxt->user, authctxt->service, user, service);
    343   1.1  christos 	}
    344   1.1  christos 	/* reset state */
    345  1.15  christos 	auth2_challenge_stop(ssh);
    346   1.1  christos 
    347   1.1  christos #ifdef GSSAPI
    348   1.1  christos 	/* XXX move to auth2_gssapi_stop() */
    349  1.15  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
    350  1.15  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
    351   1.1  christos #endif
    352   1.1  christos 
    353  1.15  christos 	auth2_authctxt_reset_info(authctxt);
    354   1.1  christos 	authctxt->postponed = 0;
    355   1.5  christos 	authctxt->server_caused_failure = 0;
    356   1.1  christos 
    357   1.1  christos 	/* try to authenticate user */
    358   1.7  christos 	m = authmethod_lookup(authctxt, method);
    359   1.1  christos 	if (m != NULL && authctxt->failures < options.max_authtries) {
    360   1.1  christos 		debug2("input_userauth_request: try method %s", method);
    361  1.25  christos 		authenticated =	m->userauth(ssh, method);
    362   1.1  christos 	}
    363  1.28  christos 	if (!authctxt->authenticated && strcmp(method, "none") != 0)
    364  1.17  christos 		ensure_minimum_time_since(tstart,
    365  1.17  christos 		    user_specific_delay(authctxt->user));
    366  1.15  christos 	userauth_finish(ssh, authenticated, method, NULL);
    367  1.19  christos 	r = 0;
    368  1.19  christos  out:
    369   1.8  christos 	free(service);
    370   1.8  christos 	free(user);
    371   1.8  christos 	free(method);
    372  1.19  christos 	return r;
    373   1.1  christos }
    374   1.1  christos 
    375   1.1  christos void
    376  1.25  christos userauth_finish(struct ssh *ssh, int authenticated, const char *packet_method,
    377   1.7  christos     const char *submethod)
    378   1.1  christos {
    379  1.15  christos 	Authctxt *authctxt = ssh->authctxt;
    380  1.25  christos 	Authmethod *m = NULL;
    381  1.25  christos 	const char *method = packet_method;
    382   1.1  christos 	char *methods;
    383  1.19  christos 	int r, partial = 0;
    384   1.1  christos 
    385  1.25  christos 	if (authenticated) {
    386  1.25  christos 		if (!authctxt->valid) {
    387  1.25  christos 			fatal("INTERNAL ERROR: authenticated invalid user %s",
    388  1.25  christos 			    authctxt->user);
    389  1.25  christos 		}
    390  1.25  christos 		if (authctxt->postponed)
    391  1.25  christos 			fatal("INTERNAL ERROR: authenticated and postponed");
    392  1.25  christos 		/* prefer primary authmethod name to possible synonym */
    393  1.25  christos 		if ((m = authmethod_byname(method)) == NULL)
    394  1.25  christos 			fatal("INTERNAL ERROR: bad method %s", method);
    395  1.31  christos 		method = m->cfg->name;
    396  1.25  christos 	}
    397   1.1  christos 
    398   1.1  christos 	/* Special handling for root */
    399   1.1  christos 	if (authenticated && authctxt->pw->pw_uid == 0 &&
    400  1.16  christos 	    !auth_root_allowed(ssh, method)) {
    401   1.1  christos 		authenticated = 0;
    402   1.2  christos #ifdef SSH_AUDIT_EVENTS
    403   1.2  christos 		PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
    404   1.2  christos #endif
    405   1.2  christos 	}
    406   1.2  christos 
    407   1.2  christos #ifdef USE_PAM
    408   1.2  christos 	if (options.use_pam && authenticated) {
    409  1.31  christos 		int success = mm_do_pam_account();
    410  1.31  christos 
    411  1.31  christos 		/* if PAM returned a message, send it to the user */
    412  1.31  christos 		if (sshbuf_len(loginmsg) > 0) {
    413  1.31  christos 			if ((r = sshbuf_put(loginmsg, "\0", 1)) != 0)
    414  1.31  christos 				fatal("%s: buffer error: %s",
    415  1.31  christos 				    __func__, ssh_err(r));
    416  1.31  christos 			userauth_send_banner(ssh,
    417  1.31  christos 			    (const char *)sshbuf_ptr(loginmsg));
    418  1.31  christos 			if ((r = ssh_packet_write_wait(ssh)) < 0) {
    419  1.31  christos 				sshpkt_fatal(ssh, r,
    420  1.31  christos 				    "%s: send PAM banner", __func__);
    421   1.2  christos 			}
    422  1.31  christos 		}
    423  1.31  christos 		if (!success) {
    424  1.31  christos 		    fatal("Access denied for user %s by PAM account "
    425  1.31  christos 			"configuration", authctxt->user);
    426   1.2  christos 		}
    427   1.2  christos 	}
    428   1.2  christos #endif
    429   1.1  christos 
    430   1.7  christos 	if (authenticated && options.num_auth_methods != 0) {
    431   1.8  christos 		if (!auth2_update_methods_lists(authctxt, method, submethod)) {
    432   1.7  christos 			authenticated = 0;
    433   1.7  christos 			partial = 1;
    434   1.7  christos 		}
    435   1.7  christos 	}
    436   1.7  christos 
    437   1.1  christos 	/* Log before sending the reply */
    438  1.19  christos 	auth_log(ssh, authenticated, partial, method, submethod);
    439   1.1  christos 
    440  1.15  christos 	/* Update information exposed to session */
    441  1.15  christos 	if (authenticated || partial)
    442  1.15  christos 		auth2_update_session_info(authctxt, method, submethod);
    443  1.15  christos 
    444   1.1  christos 	if (authctxt->postponed)
    445   1.1  christos 		return;
    446   1.1  christos 
    447   1.1  christos 	if (authenticated == 1) {
    448   1.1  christos 		/* turn off userauth */
    449  1.19  christos 		ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
    450  1.19  christos 		    &dispatch_protocol_ignore);
    451  1.19  christos 		if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_SUCCESS)) != 0 ||
    452  1.19  christos 		    (r = sshpkt_send(ssh)) != 0 ||
    453  1.19  christos 		    (r = ssh_packet_write_wait(ssh)) < 0)
    454  1.23  christos 			fatal_fr(r, "send success packet");
    455   1.1  christos 		/* now we can break out */
    456   1.1  christos 		authctxt->success = 1;
    457  1.14  christos 		ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
    458   1.1  christos 	} else {
    459   1.1  christos 		/* Allow initial try of "none" auth without failure penalty */
    460  1.11  christos 		if (!partial && !authctxt->server_caused_failure &&
    461  1.32       rin 		    (authctxt->attempt > 1 || strcmp(method, "none") != 0))
    462   1.1  christos 			authctxt->failures++;
    463   1.9  christos 		if (authctxt->failures >= options.max_authtries)
    464  1.19  christos 			auth_maxtries_exceeded(ssh);
    465   1.7  christos 		methods = authmethods_get(authctxt);
    466  1.23  christos 		debug3_f("failure partial=%d next methods=\"%s\"",
    467   1.7  christos 		    partial, methods);
    468  1.19  christos 		if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_FAILURE)) != 0 ||
    469  1.19  christos 		    (r = sshpkt_put_cstring(ssh, methods)) != 0 ||
    470  1.19  christos 		    (r = sshpkt_put_u8(ssh, partial)) != 0 ||
    471  1.19  christos 		    (r = sshpkt_send(ssh)) != 0 ||
    472  1.19  christos 		    (r = ssh_packet_write_wait(ssh)) < 0)
    473  1.23  christos 			fatal_fr(r, "send failure packet");
    474   1.8  christos 		free(methods);
    475   1.1  christos 	}
    476   1.1  christos }
    477   1.1  christos 
    478   1.7  christos /*
    479   1.7  christos  * Checks whether method is allowed by at least one AuthenticationMethods
    480   1.7  christos  * methods list. Returns 1 if allowed, or no methods lists configured.
    481   1.7  christos  * 0 otherwise.
    482   1.7  christos  */
    483   1.8  christos int
    484   1.8  christos auth2_method_allowed(Authctxt *authctxt, const char *method,
    485   1.8  christos     const char *submethod)
    486   1.7  christos {
    487   1.7  christos 	u_int i;
    488   1.7  christos 
    489   1.7  christos 	/*
    490   1.7  christos 	 * NB. authctxt->num_auth_methods might be zero as a result of
    491   1.7  christos 	 * auth2_setup_methods_lists(), so check the configuration.
    492   1.7  christos 	 */
    493   1.7  christos 	if (options.num_auth_methods == 0)
    494   1.7  christos 		return 1;
    495   1.7  christos 	for (i = 0; i < authctxt->num_auth_methods; i++) {
    496   1.8  christos 		if (list_starts_with(authctxt->auth_methods[i], method,
    497   1.8  christos 		    submethod) != MATCH_NONE)
    498   1.7  christos 			return 1;
    499   1.7  christos 	}
    500   1.7  christos 	return 0;
    501   1.7  christos }
    502   1.7  christos 
    503   1.1  christos static char *
    504   1.7  christos authmethods_get(Authctxt *authctxt)
    505   1.1  christos {
    506  1.17  christos 	struct sshbuf *b;
    507   1.1  christos 	char *list;
    508  1.17  christos 	int i, r;
    509   1.1  christos 
    510  1.17  christos 	if ((b = sshbuf_new()) == NULL)
    511  1.23  christos 		fatal_f("sshbuf_new failed");
    512   1.1  christos 	for (i = 0; authmethods[i] != NULL; i++) {
    513  1.31  christos 		if (strcmp(authmethods[i]->cfg->name, "none") == 0)
    514   1.1  christos 			continue;
    515  1.31  christos 		if (authmethods[i]->cfg->enabled == NULL ||
    516  1.31  christos 		    *(authmethods[i]->cfg->enabled) == 0)
    517   1.7  christos 			continue;
    518  1.31  christos 		if (!auth2_method_allowed(authctxt, authmethods[i]->cfg->name,
    519   1.8  christos 		    NULL))
    520   1.7  christos 			continue;
    521  1.17  christos 		if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) ? "," : "",
    522  1.31  christos 		    authmethods[i]->cfg->name)) != 0)
    523  1.23  christos 			fatal_fr(r, "buffer error");
    524   1.1  christos 	}
    525  1.17  christos 	if ((list = sshbuf_dup_string(b)) == NULL)
    526  1.23  christos 		fatal_f("sshbuf_dup_string failed");
    527  1.17  christos 	sshbuf_free(b);
    528   1.1  christos 	return list;
    529   1.1  christos }
    530   1.1  christos 
    531   1.1  christos static Authmethod *
    532  1.25  christos authmethod_byname(const char *name)
    533   1.1  christos {
    534   1.1  christos 	int i;
    535   1.1  christos 
    536  1.25  christos 	if (name == NULL)
    537  1.25  christos 		fatal_f("NULL authentication method name");
    538  1.25  christos 	for (i = 0; authmethods[i] != NULL; i++) {
    539  1.31  christos 		if (strcmp(name, authmethods[i]->cfg->name) == 0 ||
    540  1.31  christos 		    (authmethods[i]->cfg->synonym != NULL &&
    541  1.31  christos 		    strcmp(name, authmethods[i]->cfg->synonym) == 0))
    542  1.25  christos 			return authmethods[i];
    543  1.25  christos 	}
    544  1.25  christos 	debug_f("unrecognized authentication method name: %s", name);
    545   1.1  christos 	return NULL;
    546   1.1  christos }
    547   1.1  christos 
    548  1.25  christos static Authmethod *
    549  1.25  christos authmethod_lookup(Authctxt *authctxt, const char *name)
    550  1.25  christos {
    551  1.25  christos 	Authmethod *method;
    552  1.25  christos 
    553  1.25  christos 	if ((method = authmethod_byname(name)) == NULL)
    554  1.25  christos 		return NULL;
    555  1.25  christos 
    556  1.31  christos 	if (method->cfg->enabled == NULL || *(method->cfg->enabled) == 0) {
    557  1.25  christos 		debug3_f("method %s not enabled", name);
    558  1.25  christos 		return NULL;
    559  1.25  christos 	}
    560  1.31  christos 	if (!auth2_method_allowed(authctxt, method->cfg->name, NULL)) {
    561  1.25  christos 		debug3_f("method %s not allowed "
    562  1.25  christos 		    "by AuthenticationMethods", name);
    563  1.25  christos 		return NULL;
    564  1.25  christos 	}
    565  1.25  christos 	return method;
    566  1.25  christos }
    567  1.25  christos 
    568   1.7  christos /*
    569   1.7  christos  * Prune the AuthenticationMethods supplied in the configuration, removing
    570   1.7  christos  * any methods lists that include disabled methods. Note that this might
    571   1.7  christos  * leave authctxt->num_auth_methods == 0, even when multiple required auth
    572   1.7  christos  * has been requested. For this reason, all tests for whether multiple is
    573   1.7  christos  * enabled should consult options.num_auth_methods directly.
    574   1.7  christos  */
    575   1.7  christos int
    576   1.7  christos auth2_setup_methods_lists(Authctxt *authctxt)
    577   1.7  christos {
    578   1.7  christos 	u_int i;
    579   1.7  christos 
    580  1.19  christos 	/* First, normalise away the "any" pseudo-method */
    581  1.19  christos 	if (options.num_auth_methods == 1 &&
    582  1.19  christos 	    strcmp(options.auth_methods[0], "any") == 0) {
    583  1.19  christos 		free(options.auth_methods[0]);
    584  1.19  christos 		options.auth_methods[0] = NULL;
    585  1.19  christos 		options.num_auth_methods = 0;
    586  1.19  christos 	}
    587  1.19  christos 
    588   1.7  christos 	if (options.num_auth_methods == 0)
    589   1.7  christos 		return 0;
    590  1.23  christos 	debug3_f("checking methods");
    591   1.7  christos 	authctxt->auth_methods = xcalloc(options.num_auth_methods,
    592   1.7  christos 	    sizeof(*authctxt->auth_methods));
    593   1.7  christos 	authctxt->num_auth_methods = 0;
    594   1.7  christos 	for (i = 0; i < options.num_auth_methods; i++) {
    595   1.7  christos 		if (auth2_methods_valid(options.auth_methods[i], 1) != 0) {
    596   1.7  christos 			logit("Authentication methods list \"%s\" contains "
    597   1.7  christos 			    "disabled method, skipping",
    598   1.7  christos 			    options.auth_methods[i]);
    599   1.7  christos 			continue;
    600   1.7  christos 		}
    601   1.7  christos 		debug("authentication methods list %d: %s",
    602   1.7  christos 		    authctxt->num_auth_methods, options.auth_methods[i]);
    603   1.7  christos 		authctxt->auth_methods[authctxt->num_auth_methods++] =
    604   1.7  christos 		    xstrdup(options.auth_methods[i]);
    605   1.7  christos 	}
    606   1.7  christos 	if (authctxt->num_auth_methods == 0) {
    607   1.7  christos 		error("No AuthenticationMethods left after eliminating "
    608   1.7  christos 		    "disabled methods");
    609   1.7  christos 		return -1;
    610   1.7  christos 	}
    611   1.7  christos 	return 0;
    612   1.7  christos }
    613   1.7  christos 
    614   1.7  christos static int
    615   1.8  christos list_starts_with(const char *methods, const char *method,
    616   1.8  christos     const char *submethod)
    617   1.7  christos {
    618   1.7  christos 	size_t l = strlen(method);
    619   1.8  christos 	int match;
    620   1.8  christos 	const char *p;
    621   1.7  christos 
    622   1.7  christos 	if (strncmp(methods, method, l) != 0)
    623   1.8  christos 		return MATCH_NONE;
    624   1.8  christos 	p = methods + l;
    625   1.8  christos 	match = MATCH_METHOD;
    626   1.8  christos 	if (*p == ':') {
    627   1.8  christos 		if (!submethod)
    628   1.8  christos 			return MATCH_PARTIAL;
    629   1.8  christos 		l = strlen(submethod);
    630   1.8  christos 		p += 1;
    631   1.8  christos 		if (strncmp(submethod, p, l))
    632   1.8  christos 			return MATCH_NONE;
    633   1.8  christos 		p += l;
    634   1.8  christos 		match = MATCH_BOTH;
    635   1.8  christos 	}
    636   1.8  christos 	if (*p != ',' && *p != '\0')
    637   1.8  christos 		return MATCH_NONE;
    638   1.8  christos 	return match;
    639   1.7  christos }
    640   1.7  christos 
    641   1.7  christos /*
    642   1.7  christos  * Remove method from the start of a comma-separated list of methods.
    643   1.7  christos  * Returns 0 if the list of methods did not start with that method or 1
    644   1.7  christos  * if it did.
    645   1.7  christos  */
    646   1.7  christos static int
    647   1.8  christos remove_method(char **methods, const char *method, const char *submethod)
    648   1.7  christos {
    649   1.8  christos 	char *omethods = *methods, *p;
    650   1.7  christos 	size_t l = strlen(method);
    651   1.8  christos 	int match;
    652   1.7  christos 
    653   1.8  christos 	match = list_starts_with(omethods, method, submethod);
    654   1.8  christos 	if (match != MATCH_METHOD && match != MATCH_BOTH)
    655   1.7  christos 		return 0;
    656   1.8  christos 	p = omethods + l;
    657   1.8  christos 	if (submethod && match == MATCH_BOTH)
    658   1.8  christos 		p += 1 + strlen(submethod); /* include colon */
    659   1.8  christos 	if (*p == ',')
    660   1.8  christos 		p++;
    661   1.8  christos 	*methods = xstrdup(p);
    662   1.7  christos 	free(omethods);
    663   1.7  christos 	return 1;
    664   1.7  christos }
    665   1.7  christos 
    666   1.7  christos /*
    667   1.7  christos  * Called after successful authentication. Will remove the successful method
    668   1.7  christos  * from the start of each list in which it occurs. If it was the last method
    669   1.7  christos  * in any list, then authentication is deemed successful.
    670   1.7  christos  * Returns 1 if the method completed any authentication list or 0 otherwise.
    671   1.7  christos  */
    672   1.7  christos int
    673   1.8  christos auth2_update_methods_lists(Authctxt *authctxt, const char *method,
    674   1.8  christos     const char *submethod)
    675   1.7  christos {
    676   1.7  christos 	u_int i, found = 0;
    677   1.7  christos 
    678  1.23  christos 	debug3_f("updating methods list after \"%s\"", method);
    679   1.7  christos 	for (i = 0; i < authctxt->num_auth_methods; i++) {
    680   1.8  christos 		if (!remove_method(&(authctxt->auth_methods[i]), method,
    681   1.8  christos 		    submethod))
    682   1.7  christos 			continue;
    683   1.7  christos 		found = 1;
    684   1.7  christos 		if (*authctxt->auth_methods[i] == '\0') {
    685   1.7  christos 			debug2("authentication methods list %d complete", i);
    686   1.7  christos 			return 1;
    687   1.7  christos 		}
    688   1.7  christos 		debug3("authentication methods list %d remaining: \"%s\"",
    689   1.7  christos 		    i, authctxt->auth_methods[i]);
    690   1.7  christos 	}
    691   1.7  christos 	/* This should not happen, but would be bad if it did */
    692   1.7  christos 	if (!found)
    693  1.23  christos 		fatal_f("method not in AuthenticationMethods");
    694   1.7  christos 	return 0;
    695   1.7  christos }
    696   1.7  christos 
    697  1.15  christos /* Reset method-specific information */
    698  1.15  christos void auth2_authctxt_reset_info(Authctxt *authctxt)
    699  1.15  christos {
    700  1.15  christos 	sshkey_free(authctxt->auth_method_key);
    701  1.15  christos 	free(authctxt->auth_method_info);
    702  1.15  christos 	authctxt->auth_method_key = NULL;
    703  1.15  christos 	authctxt->auth_method_info = NULL;
    704  1.15  christos }
    705  1.15  christos 
    706  1.15  christos /* Record auth method-specific information for logs */
    707  1.15  christos void
    708  1.15  christos auth2_record_info(Authctxt *authctxt, const char *fmt, ...)
    709  1.15  christos {
    710  1.15  christos 	va_list ap;
    711  1.24  christos 	int i;
    712  1.15  christos 
    713  1.15  christos 	free(authctxt->auth_method_info);
    714  1.15  christos 	authctxt->auth_method_info = NULL;
    715  1.15  christos 
    716  1.15  christos 	va_start(ap, fmt);
    717  1.15  christos 	i = vasprintf(&authctxt->auth_method_info, fmt, ap);
    718  1.15  christos 	va_end(ap);
    719  1.15  christos 
    720  1.20  christos 	if (i == -1)
    721  1.23  christos 		fatal_f("vasprintf failed");
    722  1.15  christos }
    723  1.15  christos 
    724  1.15  christos /*
    725  1.15  christos  * Records a public key used in authentication. This is used for logging
    726  1.15  christos  * and to ensure that the same key is not subsequently accepted again for
    727  1.15  christos  * multiple authentication.
    728  1.15  christos  */
    729  1.15  christos void
    730  1.15  christos auth2_record_key(Authctxt *authctxt, int authenticated,
    731  1.15  christos     const struct sshkey *key)
    732  1.15  christos {
    733  1.15  christos 	struct sshkey **tmp, *dup;
    734  1.15  christos 	int r;
    735  1.15  christos 
    736  1.19  christos 	if ((r = sshkey_from_private(key, &dup)) != 0)
    737  1.23  christos 		fatal_fr(r, "copy key");
    738  1.15  christos 	sshkey_free(authctxt->auth_method_key);
    739  1.15  christos 	authctxt->auth_method_key = dup;
    740  1.15  christos 
    741  1.15  christos 	if (!authenticated)
    742  1.15  christos 		return;
    743  1.15  christos 
    744  1.15  christos 	/* If authenticated, make sure we don't accept this key again */
    745  1.19  christos 	if ((r = sshkey_from_private(key, &dup)) != 0)
    746  1.23  christos 		fatal_fr(r, "copy key");
    747  1.15  christos 	if (authctxt->nprev_keys >= INT_MAX ||
    748  1.15  christos 	    (tmp = recallocarray(authctxt->prev_keys, authctxt->nprev_keys,
    749  1.15  christos 	    authctxt->nprev_keys + 1, sizeof(*authctxt->prev_keys))) == NULL)
    750  1.23  christos 		fatal_f("reallocarray failed");
    751  1.15  christos 	authctxt->prev_keys = tmp;
    752  1.15  christos 	authctxt->prev_keys[authctxt->nprev_keys] = dup;
    753  1.15  christos 	authctxt->nprev_keys++;
    754  1.15  christos 
    755  1.15  christos }
    756  1.15  christos 
    757  1.15  christos /* Checks whether a key has already been previously used for authentication */
    758  1.15  christos int
    759  1.15  christos auth2_key_already_used(Authctxt *authctxt, const struct sshkey *key)
    760  1.15  christos {
    761  1.15  christos 	u_int i;
    762  1.15  christos 	char *fp;
    763  1.15  christos 
    764  1.15  christos 	for (i = 0; i < authctxt->nprev_keys; i++) {
    765  1.15  christos 		if (sshkey_equal_public(key, authctxt->prev_keys[i])) {
    766  1.15  christos 			fp = sshkey_fingerprint(authctxt->prev_keys[i],
    767  1.15  christos 			    options.fingerprint_hash, SSH_FP_DEFAULT);
    768  1.23  christos 			debug3_f("key already used: %s %s",
    769  1.15  christos 			    sshkey_type(authctxt->prev_keys[i]),
    770  1.15  christos 			    fp == NULL ? "UNKNOWN" : fp);
    771  1.15  christos 			free(fp);
    772  1.15  christos 			return 1;
    773  1.15  christos 		}
    774  1.15  christos 	}
    775  1.15  christos 	return 0;
    776  1.15  christos }
    777  1.15  christos 
    778  1.15  christos /*
    779  1.15  christos  * Updates authctxt->session_info with details of authentication. Should be
    780  1.15  christos  * whenever an authentication method succeeds.
    781  1.15  christos  */
    782  1.15  christos void
    783  1.15  christos auth2_update_session_info(Authctxt *authctxt, const char *method,
    784  1.15  christos     const char *submethod)
    785  1.15  christos {
    786  1.15  christos 	int r;
    787  1.15  christos 
    788  1.15  christos 	if (authctxt->session_info == NULL) {
    789  1.15  christos 		if ((authctxt->session_info = sshbuf_new()) == NULL)
    790  1.23  christos 			fatal_f("sshbuf_new");
    791  1.15  christos 	}
    792  1.15  christos 
    793  1.15  christos 	/* Append method[/submethod] */
    794  1.15  christos 	if ((r = sshbuf_putf(authctxt->session_info, "%s%s%s",
    795  1.15  christos 	    method, submethod == NULL ? "" : "/",
    796  1.15  christos 	    submethod == NULL ? "" : submethod)) != 0)
    797  1.23  christos 		fatal_fr(r, "append method");
    798  1.15  christos 
    799  1.15  christos 	/* Append key if present */
    800  1.15  christos 	if (authctxt->auth_method_key != NULL) {
    801  1.15  christos 		if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
    802  1.15  christos 		    (r = sshkey_format_text(authctxt->auth_method_key,
    803  1.15  christos 		    authctxt->session_info)) != 0)
    804  1.23  christos 			fatal_fr(r, "append key");
    805  1.15  christos 	}
    806  1.15  christos 
    807  1.15  christos 	if (authctxt->auth_method_info != NULL) {
    808  1.15  christos 		/* Ensure no ambiguity here */
    809  1.15  christos 		if (strchr(authctxt->auth_method_info, '\n') != NULL)
    810  1.23  christos 			fatal_f("auth_method_info contains \\n");
    811  1.15  christos 		if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
    812  1.15  christos 		    (r = sshbuf_putf(authctxt->session_info, "%s",
    813  1.15  christos 		    authctxt->auth_method_info)) != 0) {
    814  1.23  christos 			fatal_fr(r, "append method info");
    815  1.15  christos 		}
    816  1.15  christos 	}
    817  1.15  christos 	if ((r = sshbuf_put_u8(authctxt->session_info, '\n')) != 0)
    818  1.23  christos 		fatal_fr(r, "append");
    819  1.15  christos }
    820   1.7  christos 
    821