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