xref: /openssh-portable/auth2.c (revision 31d8d231)
1*31d8d231Sdjm@openbsd.org /* $OpenBSD: auth2.c,v 1.161 2021/04/03 06:18:40 djm Exp $ */
2eba71babSDamien Miller /*
3eba71babSDamien Miller  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4eba71babSDamien Miller  *
5eba71babSDamien Miller  * Redistribution and use in source and binary forms, with or without
6eba71babSDamien Miller  * modification, are permitted provided that the following conditions
7eba71babSDamien Miller  * are met:
8eba71babSDamien Miller  * 1. Redistributions of source code must retain the above copyright
9eba71babSDamien Miller  *    notice, this list of conditions and the following disclaimer.
10eba71babSDamien Miller  * 2. Redistributions in binary form must reproduce the above copyright
11eba71babSDamien Miller  *    notice, this list of conditions and the following disclaimer in the
12eba71babSDamien Miller  *    documentation and/or other materials provided with the distribution.
13eba71babSDamien Miller  *
14eba71babSDamien Miller  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15eba71babSDamien Miller  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16eba71babSDamien Miller  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17eba71babSDamien Miller  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18eba71babSDamien Miller  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19eba71babSDamien Miller  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20eba71babSDamien Miller  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21eba71babSDamien Miller  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22eba71babSDamien Miller  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23eba71babSDamien Miller  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24eba71babSDamien Miller  */
25e4340be5SDamien Miller 
26eba71babSDamien Miller #include "includes.h"
27eba71babSDamien Miller 
289f2abc47SDamien Miller #include <sys/types.h>
294230a5dcSDarren Tucker #include <sys/stat.h>
304230a5dcSDarren Tucker #include <sys/uio.h>
319f2abc47SDamien Miller 
324230a5dcSDarren Tucker #include <fcntl.h>
338f574959Sdjm@openbsd.org #include <limits.h>
349f2abc47SDamien Miller #include <pwd.h>
35d7834353SDamien Miller #include <stdarg.h>
36e3476ed0SDamien Miller #include <string.h>
374230a5dcSDarren Tucker #include <unistd.h>
38943d0965Sdjm@openbsd.org #include <time.h>
399f2abc47SDamien Miller 
40be02d7cbSdjm@openbsd.org #include "stdlib.h"
410b4d48baSDamien Miller #include "atomicio.h"
4243e7a358SDarren Tucker #include "xmalloc.h"
43d7834353SDamien Miller #include "ssh2.h"
44eba71babSDamien Miller #include "packet.h"
45226cfa03SBen Lindstrom #include "log.h"
46c7d39ac8Smarkus@openbsd.org #include "sshbuf.h"
477acefbbcSDamien Miller #include "misc.h"
48eba71babSDamien Miller #include "servconf.h"
49eba71babSDamien Miller #include "compat.h"
505467fbcbSmarkus@openbsd.org #include "sshkey.h"
51d7834353SDamien Miller #include "hostfile.h"
52eba71babSDamien Miller #include "auth.h"
53eba71babSDamien Miller #include "dispatch.h"
54226cfa03SBen Lindstrom #include "pathnames.h"
55120a1ec7SDamien Miller #include "ssherr.h"
560efd155cSDarren Tucker #ifdef GSSAPI
570efd155cSDarren Tucker #include "ssh-gss.h"
580efd155cSDarren Tucker #endif
59d7834353SDamien Miller #include "monitor_wrap.h"
60e9d910b0Sdtucker@openbsd.org #include "digest.h"
610efd155cSDarren Tucker 
62eba71babSDamien Miller /* import */
63eba71babSDamien Miller extern ServerOptions options;
64120a1ec7SDamien Miller extern struct sshbuf *loginmsg;
65eba71babSDamien Miller 
66511bb24cSBen Lindstrom /* methods */
67511bb24cSBen Lindstrom 
68511bb24cSBen Lindstrom extern Authmethod method_none;
69511bb24cSBen Lindstrom extern Authmethod method_pubkey;
70511bb24cSBen Lindstrom extern Authmethod method_passwd;
71511bb24cSBen Lindstrom extern Authmethod method_kbdint;
72511bb24cSBen Lindstrom extern Authmethod method_hostbased;
730efd155cSDarren Tucker #ifdef GSSAPI
740efd155cSDarren Tucker extern Authmethod method_gssapi;
750efd155cSDarren Tucker #endif
76511bb24cSBen Lindstrom 
77511bb24cSBen Lindstrom Authmethod *authmethods[] = {
78511bb24cSBen Lindstrom 	&method_none,
79511bb24cSBen Lindstrom 	&method_pubkey,
800efd155cSDarren Tucker #ifdef GSSAPI
810efd155cSDarren Tucker 	&method_gssapi,
820efd155cSDarren Tucker #endif
83511bb24cSBen Lindstrom 	&method_passwd,
84511bb24cSBen Lindstrom 	&method_kbdint,
85511bb24cSBen Lindstrom 	&method_hostbased,
86511bb24cSBen Lindstrom 	NULL
87874d77bbSDamien Miller };
88874d77bbSDamien Miller 
89eba71babSDamien Miller /* protocol */
90eba71babSDamien Miller 
912ae666a8Smarkus@openbsd.org static int input_service_request(int, u_int32_t, struct ssh *);
922ae666a8Smarkus@openbsd.org static int input_userauth_request(int, u_int32_t, struct ssh *);
93eba71babSDamien Miller 
94eba71babSDamien Miller /* helper */
95a6e3f01dSDamien Miller static Authmethod *authmethod_lookup(Authctxt *, const char *);
96a6e3f01dSDamien Miller static char *authmethods_get(Authctxt *authctxt);
9791a55f28SDamien Miller 
9891a55f28SDamien Miller #define MATCH_NONE	0	/* method or submethod mismatch */
9991a55f28SDamien Miller #define MATCH_METHOD	1	/* method matches (no submethod specified) */
10091a55f28SDamien Miller #define MATCH_BOTH	2	/* method and submethod match */
10191a55f28SDamien Miller #define MATCH_PARTIAL	3	/* method matches, submethod can't be checked */
10291a55f28SDamien Miller static int list_starts_with(const char *, const char *, const char *);
103eba71babSDamien Miller 
1044230a5dcSDarren Tucker char *
auth2_read_banner(void)1054230a5dcSDarren Tucker auth2_read_banner(void)
1064230a5dcSDarren Tucker {
1074230a5dcSDarren Tucker 	struct stat st;
1084230a5dcSDarren Tucker 	char *banner = NULL;
1094230a5dcSDarren Tucker 	size_t len, n;
1104230a5dcSDarren Tucker 	int fd;
1114230a5dcSDarren Tucker 
1124230a5dcSDarren Tucker 	if ((fd = open(options.banner, O_RDONLY)) == -1)
1134230a5dcSDarren Tucker 		return (NULL);
1144230a5dcSDarren Tucker 	if (fstat(fd, &st) == -1) {
1154230a5dcSDarren Tucker 		close(fd);
1164230a5dcSDarren Tucker 		return (NULL);
1174230a5dcSDarren Tucker 	}
1188ed4de8fSDamien Miller 	if (st.st_size <= 0 || st.st_size > 1*1024*1024) {
1194230a5dcSDarren Tucker 		close(fd);
1204230a5dcSDarren Tucker 		return (NULL);
1214230a5dcSDarren Tucker 	}
1224230a5dcSDarren Tucker 
1234230a5dcSDarren Tucker 	len = (size_t)st.st_size;		/* truncate */
1244230a5dcSDarren Tucker 	banner = xmalloc(len + 1);
1254230a5dcSDarren Tucker 	n = atomicio(read, fd, banner, len);
1264230a5dcSDarren Tucker 	close(fd);
1274230a5dcSDarren Tucker 
1284230a5dcSDarren Tucker 	if (n != len) {
129a627d42eSDarren Tucker 		free(banner);
1304230a5dcSDarren Tucker 		return (NULL);
1314230a5dcSDarren Tucker 	}
1324230a5dcSDarren Tucker 	banner[n] = '\0';
1334230a5dcSDarren Tucker 
1344230a5dcSDarren Tucker 	return (banner);
1354230a5dcSDarren Tucker }
1364230a5dcSDarren Tucker 
1375ebce136SDamien Miller static void
userauth_send_banner(struct ssh * ssh,const char * msg)1385ebce136SDamien Miller userauth_send_banner(struct ssh *ssh, const char *msg)
1394230a5dcSDarren Tucker {
1405ebce136SDamien Miller 	int r;
1415ebce136SDamien Miller 
1425ebce136SDamien Miller 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_BANNER)) != 0 ||
1435ebce136SDamien Miller 	    (r = sshpkt_put_cstring(ssh, msg)) != 0 ||
1445ebce136SDamien Miller 	    (r = sshpkt_put_cstring(ssh, "")) != 0 ||	/* language, unused */
1455ebce136SDamien Miller 	    (r = sshpkt_send(ssh)) != 0)
146816036f1Sdjm@openbsd.org 		fatal_fr(r, "send packet");
1474230a5dcSDarren Tucker 	debug("%s: sent", __func__);
1484230a5dcSDarren Tucker }
1494230a5dcSDarren Tucker 
1504230a5dcSDarren Tucker static void
userauth_banner(struct ssh * ssh)1515ebce136SDamien Miller userauth_banner(struct ssh *ssh)
1524230a5dcSDarren Tucker {
1534230a5dcSDarren Tucker 	char *banner = NULL;
1544230a5dcSDarren Tucker 
15514b5c635Sdjm@openbsd.org 	if (options.banner == NULL)
1564230a5dcSDarren Tucker 		return;
1574230a5dcSDarren Tucker 
1584230a5dcSDarren Tucker 	if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
1594230a5dcSDarren Tucker 		goto done;
1605ebce136SDamien Miller 	userauth_send_banner(ssh, banner);
1614230a5dcSDarren Tucker 
1624230a5dcSDarren Tucker done:
163a627d42eSDarren Tucker 	free(banner);
1644230a5dcSDarren Tucker }
1654230a5dcSDarren Tucker 
166eba71babSDamien Miller /*
167874d77bbSDamien Miller  * loop until authctxt->success == TRUE
168eba71babSDamien Miller  */
1693e33cecfSDarren Tucker void
do_authentication2(struct ssh * ssh)1705ebce136SDamien Miller do_authentication2(struct ssh *ssh)
171eba71babSDamien Miller {
1725ebce136SDamien Miller 	Authctxt *authctxt = ssh->authctxt;
1735ebce136SDamien Miller 
174eb272ea4Smarkus@openbsd.org 	ssh_dispatch_init(ssh, &dispatch_protocol_error);
175eb272ea4Smarkus@openbsd.org 	ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request);
17692e9fe63Smarkus@openbsd.org 	ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success);
1775f4082d8Smarkus@openbsd.org 	ssh->authctxt = NULL;
178eba71babSDamien Miller }
179eba71babSDamien Miller 
18091d4b12fSDamien Miller /*ARGSUSED*/
1813fdc88a0Smarkus@openbsd.org static int
input_service_request(int type,u_int32_t seq,struct ssh * ssh)1822ae666a8Smarkus@openbsd.org input_service_request(int type, u_int32_t seq, struct ssh *ssh)
183eba71babSDamien Miller {
1845f4082d8Smarkus@openbsd.org 	Authctxt *authctxt = ssh->authctxt;
1855ebce136SDamien Miller 	char *service = NULL;
1865ebce136SDamien Miller 	int r, acceptit = 0;
1875ebce136SDamien Miller 
1885ebce136SDamien Miller 	if ((r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
1895ebce136SDamien Miller 	    (r = sshpkt_get_end(ssh)) != 0)
1905ebce136SDamien Miller 		goto out;
191eba71babSDamien Miller 
192874d77bbSDamien Miller 	if (authctxt == NULL)
193874d77bbSDamien Miller 		fatal("input_service_request: no authctxt");
194874d77bbSDamien Miller 
195eba71babSDamien Miller 	if (strcmp(service, "ssh-userauth") == 0) {
196874d77bbSDamien Miller 		if (!authctxt->success) {
1975a9d0eabSBen Lindstrom 			acceptit = 1;
198eba71babSDamien Miller 			/* now we can handle user-auth requests */
1995ebce136SDamien Miller 			ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
2005ebce136SDamien Miller 			    &input_userauth_request);
201eba71babSDamien Miller 		}
202eba71babSDamien Miller 	}
203eba71babSDamien Miller 	/* XXX all other service requests are denied */
204eba71babSDamien Miller 
2055a9d0eabSBen Lindstrom 	if (acceptit) {
2065ebce136SDamien Miller 		if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_ACCEPT)) != 0 ||
2075ebce136SDamien Miller 		    (r = sshpkt_put_cstring(ssh, service)) != 0 ||
2085ebce136SDamien Miller 		    (r = sshpkt_send(ssh)) != 0 ||
2095ebce136SDamien Miller 		    (r = ssh_packet_write_wait(ssh)) != 0)
2105ebce136SDamien Miller 			goto out;
211eba71babSDamien Miller 	} else {
212eba71babSDamien Miller 		debug("bad service request %s", service);
2135ebce136SDamien Miller 		ssh_packet_disconnect(ssh, "bad service request %s", service);
214eba71babSDamien Miller 	}
2155ebce136SDamien Miller 	r = 0;
2165ebce136SDamien Miller  out:
217a627d42eSDarren Tucker 	free(service);
21816d4f996Smarkus@openbsd.org 	return r;
219eba71babSDamien Miller }
220eba71babSDamien Miller 
221e9d910b0Sdtucker@openbsd.org #define MIN_FAIL_DELAY_SECONDS 0.005
222e9d910b0Sdtucker@openbsd.org static double
user_specific_delay(const char * user)223e9d910b0Sdtucker@openbsd.org user_specific_delay(const char *user)
224e9d910b0Sdtucker@openbsd.org {
225e9d910b0Sdtucker@openbsd.org 	char b[512];
226e9d910b0Sdtucker@openbsd.org 	size_t len = ssh_digest_bytes(SSH_DIGEST_SHA512);
227e9d910b0Sdtucker@openbsd.org 	u_char *hash = xmalloc(len);
228e9d910b0Sdtucker@openbsd.org 	double delay;
229e9d910b0Sdtucker@openbsd.org 
230fdba503fSdtucker@openbsd.org 	(void)snprintf(b, sizeof b, "%llu%s",
231fdba503fSdtucker@openbsd.org 	    (unsigned long long)options.timing_secret, user);
232e9d910b0Sdtucker@openbsd.org 	if (ssh_digest_memory(SSH_DIGEST_SHA512, b, strlen(b), hash, len) != 0)
233816036f1Sdjm@openbsd.org 		fatal_f("ssh_digest_memory");
234e9d910b0Sdtucker@openbsd.org 	/* 0-4.2 ms of delay */
235e9d910b0Sdtucker@openbsd.org 	delay = (double)PEEK_U32(hash) / 1000 / 1000 / 1000 / 1000;
236e9d910b0Sdtucker@openbsd.org 	freezero(hash, len);
237816036f1Sdjm@openbsd.org 	debug3_f("user specific delay %0.3lfms", delay/1000);
238e9d910b0Sdtucker@openbsd.org 	return MIN_FAIL_DELAY_SECONDS + delay;
239e9d910b0Sdtucker@openbsd.org }
240e9d910b0Sdtucker@openbsd.org 
241e9d910b0Sdtucker@openbsd.org static void
ensure_minimum_time_since(double start,double seconds)242e9d910b0Sdtucker@openbsd.org ensure_minimum_time_since(double start, double seconds)
243e9d910b0Sdtucker@openbsd.org {
244e9d910b0Sdtucker@openbsd.org 	struct timespec ts;
245e9d910b0Sdtucker@openbsd.org 	double elapsed = monotime_double() - start, req = seconds, remain;
246e9d910b0Sdtucker@openbsd.org 
247e9d910b0Sdtucker@openbsd.org 	/* if we've already passed the requested time, scale up */
248e9d910b0Sdtucker@openbsd.org 	while ((remain = seconds - elapsed) < 0.0)
249e9d910b0Sdtucker@openbsd.org 		seconds *= 2;
250e9d910b0Sdtucker@openbsd.org 
251e9d910b0Sdtucker@openbsd.org 	ts.tv_sec = remain;
252e9d910b0Sdtucker@openbsd.org 	ts.tv_nsec = (remain - ts.tv_sec) * 1000000000;
253816036f1Sdjm@openbsd.org 	debug3_f("elapsed %0.3lfms, delaying %0.3lfms (requested %0.3lfms)",
254816036f1Sdjm@openbsd.org 	    elapsed*1000, remain*1000, req*1000);
255e9d910b0Sdtucker@openbsd.org 	nanosleep(&ts, NULL);
256e9d910b0Sdtucker@openbsd.org }
257e9d910b0Sdtucker@openbsd.org 
25891d4b12fSDamien Miller /*ARGSUSED*/
2593fdc88a0Smarkus@openbsd.org static int
input_userauth_request(int type,u_int32_t seq,struct ssh * ssh)2602ae666a8Smarkus@openbsd.org input_userauth_request(int type, u_int32_t seq, struct ssh *ssh)
261eba71babSDamien Miller {
2625f4082d8Smarkus@openbsd.org 	Authctxt *authctxt = ssh->authctxt;
263874d77bbSDamien Miller 	Authmethod *m = NULL;
2645ebce136SDamien Miller 	char *user = NULL, *service = NULL, *method = NULL, *style = NULL;
2655ebce136SDamien Miller 	int r, authenticated = 0;
266e9d910b0Sdtucker@openbsd.org 	double tstart = monotime_double();
267eba71babSDamien Miller 
268874d77bbSDamien Miller 	if (authctxt == NULL)
269874d77bbSDamien Miller 		fatal("input_userauth_request: no authctxt");
270874d77bbSDamien Miller 
2715ebce136SDamien Miller 	if ((r = sshpkt_get_cstring(ssh, &user, NULL)) != 0 ||
2725ebce136SDamien Miller 	    (r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
2735ebce136SDamien Miller 	    (r = sshpkt_get_cstring(ssh, &method, NULL)) != 0)
2745ebce136SDamien Miller 		goto out;
275eba71babSDamien Miller 	debug("userauth-request for user %s service %s method %s", user, service, method);
2764dccfa5fSBen Lindstrom 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
277eba71babSDamien Miller 
278db65e8fdSBen Lindstrom 	if ((style = strchr(user, ':')) != NULL)
279db65e8fdSBen Lindstrom 		*style++ = 0;
280db65e8fdSBen Lindstrom 
2814dccfa5fSBen Lindstrom 	if (authctxt->attempt++ == 0) {
282874d77bbSDamien Miller 		/* setup auth context */
2833a00a921Sdjm@openbsd.org 		authctxt->pw = PRIVSEP(getpwnamallow(ssh, user));
284dbf7a74eSDarren Tucker 		authctxt->user = xstrdup(user);
285205cc1efSKevin Steves 		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
286874d77bbSDamien Miller 			authctxt->valid = 1;
287816036f1Sdjm@openbsd.org 			debug2_f("setting up authctxt for %s", user);
288874d77bbSDamien Miller 		} else {
289f1a19346Sdjm@openbsd.org 			/* Invalid user, fake password information */
290856f0be6SDamien Miller 			authctxt->pw = fakepw();
2912e0cf0dcSDarren Tucker #ifdef SSH_AUDIT_EVENTS
2929b655dc9SDamien Miller 			PRIVSEP(audit_event(ssh, SSH_INVALID_USER));
293269a1ea1SDarren Tucker #endif
294874d77bbSDamien Miller 		}
295d3eff2bfSDarren Tucker #ifdef USE_PAM
296d3eff2bfSDarren Tucker 		if (options.use_pam)
2973f0786bbSDamien Miller 			PRIVSEP(start_pam(ssh));
298d3eff2bfSDarren Tucker #endif
299f1a19346Sdjm@openbsd.org 		ssh_packet_set_log_preamble(ssh, "%suser %s",
300f1a19346Sdjm@openbsd.org 		    authctxt->valid ? "authenticating " : "invalid ", user);
30130d1f849SDamien Miller 		setproctitle("%s%s", authctxt->valid ? user : "unknown",
3027a2073c5SBen Lindstrom 		    use_privsep ? " [net]" : "");
303874d77bbSDamien Miller 		authctxt->service = xstrdup(service);
3049d0c0666SBen Lindstrom 		authctxt->style = style ? xstrdup(style) : NULL;
3057a2073c5SBen Lindstrom 		if (use_privsep)
3067a2073c5SBen Lindstrom 			mm_inform_authserv(service, style);
3075ebce136SDamien Miller 		userauth_banner(ssh);
308a6e3f01dSDamien Miller 		if (auth2_setup_methods_lists(authctxt) != 0)
3095ebce136SDamien Miller 			ssh_packet_disconnect(ssh,
3105ebce136SDamien Miller 			    "no authentication methods enabled");
3119d0c0666SBen Lindstrom 	} else if (strcmp(user, authctxt->user) != 0 ||
312874d77bbSDamien Miller 	    strcmp(service, authctxt->service) != 0) {
3135ebce136SDamien Miller 		ssh_packet_disconnect(ssh, "Change of username or service "
3145ebce136SDamien Miller 		    "not allowed: (%s,%s) -> (%s,%s)",
3159d0c0666SBen Lindstrom 		    authctxt->user, authctxt->service, user, service);
316874d77bbSDamien Miller 	}
317db65e8fdSBen Lindstrom 	/* reset state */
318eb272ea4Smarkus@openbsd.org 	auth2_challenge_stop(ssh);
3190efd155cSDarren Tucker 
3200efd155cSDarren Tucker #ifdef GSSAPI
32101ed2272SDamien Miller 	/* XXX move to auth2_gssapi_stop() */
322eb272ea4Smarkus@openbsd.org 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
323eb272ea4Smarkus@openbsd.org 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
3240efd155cSDarren Tucker #endif
3250efd155cSDarren Tucker 
3268f574959Sdjm@openbsd.org 	auth2_authctxt_reset_info(authctxt);
327db65e8fdSBen Lindstrom 	authctxt->postponed = 0;
3283fcdfd55SDamien Miller 	authctxt->server_caused_failure = 0;
329b70b61f5SDamien Miller 
330db65e8fdSBen Lindstrom 	/* try to authenticate user */
331a6e3f01dSDamien Miller 	m = authmethod_lookup(authctxt, method);
3324230a5dcSDarren Tucker 	if (m != NULL && authctxt->failures < options.max_authtries) {
333874d77bbSDamien Miller 		debug2("input_userauth_request: try method %s", method);
334eb272ea4Smarkus@openbsd.org 		authenticated =	m->userauth(ssh);
335874d77bbSDamien Miller 	}
336e9d910b0Sdtucker@openbsd.org 	if (!authctxt->authenticated)
337e9d910b0Sdtucker@openbsd.org 		ensure_minimum_time_since(tstart,
338e9d910b0Sdtucker@openbsd.org 		    user_specific_delay(authctxt->user));
339eb272ea4Smarkus@openbsd.org 	userauth_finish(ssh, authenticated, method, NULL);
3405ebce136SDamien Miller 	r = 0;
3415ebce136SDamien Miller  out:
342a627d42eSDarren Tucker 	free(service);
343a627d42eSDarren Tucker 	free(user);
344a627d42eSDarren Tucker 	free(method);
3455ebce136SDamien Miller 	return r;
3465d57e507SDamien Miller }
3475d57e507SDamien Miller 
3485d57e507SDamien Miller void
userauth_finish(struct ssh * ssh,int authenticated,const char * method,const char * submethod)349eb272ea4Smarkus@openbsd.org userauth_finish(struct ssh *ssh, int authenticated, const char *method,
35015b05cfaSDamien Miller     const char *submethod)
3515d57e507SDamien Miller {
352eb272ea4Smarkus@openbsd.org 	Authctxt *authctxt = ssh->authctxt;
353cd4349f9SBen Lindstrom 	char *methods;
3545ebce136SDamien Miller 	int r, partial = 0;
355cd4349f9SBen Lindstrom 
356db65e8fdSBen Lindstrom 	if (!authctxt->valid && authenticated)
357db65e8fdSBen Lindstrom 		fatal("INTERNAL ERROR: authenticated invalid user %s",
358db65e8fdSBen Lindstrom 		    authctxt->user);
35915b05cfaSDamien Miller 	if (authenticated && authctxt->postponed)
36015b05cfaSDamien Miller 		fatal("INTERNAL ERROR: authenticated and postponed");
361874d77bbSDamien Miller 
362874d77bbSDamien Miller 	/* Special handling for root */
363556f9315SDamien Miller 	if (authenticated && authctxt->pw->pw_uid == 0 &&
3647c856857Sdjm@openbsd.org 	    !auth_root_allowed(ssh, method)) {
365874d77bbSDamien Miller 		authenticated = 0;
3662e0cf0dcSDarren Tucker #ifdef SSH_AUDIT_EVENTS
3679b655dc9SDamien Miller 		PRIVSEP(audit_event(ssh, SSH_LOGIN_ROOT_DENIED));
368269a1ea1SDarren Tucker #endif
369269a1ea1SDarren Tucker 	}
370eba71babSDamien Miller 
37115b05cfaSDamien Miller 	if (authenticated && options.num_auth_methods != 0) {
37291a55f28SDamien Miller 		if (!auth2_update_methods_lists(authctxt, method, submethod)) {
37315b05cfaSDamien Miller 			authenticated = 0;
37415b05cfaSDamien Miller 			partial = 1;
37515b05cfaSDamien Miller 		}
37615b05cfaSDamien Miller 	}
37715b05cfaSDamien Miller 
37815b05cfaSDamien Miller 	/* Log before sending the reply */
3793a00a921Sdjm@openbsd.org 	auth_log(ssh, authenticated, partial, method, submethod);
38015b05cfaSDamien Miller 
3818f574959Sdjm@openbsd.org 	/* Update information exposed to session */
3828f574959Sdjm@openbsd.org 	if (authenticated || partial)
3838f574959Sdjm@openbsd.org 		auth2_update_session_info(authctxt, method, submethod);
3848f574959Sdjm@openbsd.org 
38515b05cfaSDamien Miller 	if (authctxt->postponed)
38615b05cfaSDamien Miller 		return;
38715b05cfaSDamien Miller 
3881f499fd3SDamien Miller #ifdef USE_PAM
38977fc29eeSDarren Tucker 	if (options.use_pam && authenticated) {
390ed6bef77SDarren Tucker 		int r, success = PRIVSEP(do_pam_account());
391120a1ec7SDamien Miller 
392ed6bef77SDarren Tucker 		/* If PAM returned a message, send it to the user. */
393120a1ec7SDamien Miller 		if (sshbuf_len(loginmsg) > 0) {
394120a1ec7SDamien Miller 			if ((r = sshbuf_put(loginmsg, "\0", 1)) != 0)
395120a1ec7SDamien Miller 				fatal("%s: buffer error: %s",
396120a1ec7SDamien Miller 				    __func__, ssh_err(r));
3975ebce136SDamien Miller 			userauth_send_banner(ssh, sshbuf_ptr(loginmsg));
39808f66d9fSDamien Miller 			if ((r = ssh_packet_write_wait(ssh)) != 0) {
39908f66d9fSDamien Miller 				sshpkt_fatal(ssh, r,
40008f66d9fSDamien Miller 				    "%s: send PAM banner", __func__);
40108f66d9fSDamien Miller 			}
40277fc29eeSDarren Tucker 		}
403ed6bef77SDarren Tucker 		if (!success) {
404c1386671SDarren Tucker 			fatal("Access denied for user %s by PAM account "
405c1386671SDarren Tucker 			    "configuration", authctxt->user);
40677fc29eeSDarren Tucker 		}
40777fc29eeSDarren Tucker 	}
4081f499fd3SDamien Miller #endif
4091f499fd3SDamien Miller 
410cd4349f9SBen Lindstrom 	if (authenticated == 1) {
411cd4349f9SBen Lindstrom 		/* turn off userauth */
4125ebce136SDamien Miller 		ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
4135ebce136SDamien Miller 		    &dispatch_protocol_ignore);
4145ebce136SDamien Miller 		if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_SUCCESS)) != 0 ||
4155ebce136SDamien Miller 		    (r = sshpkt_send(ssh)) != 0 ||
4165ebce136SDamien Miller 		    (r = ssh_packet_write_wait(ssh)) != 0)
417816036f1Sdjm@openbsd.org 			fatal_fr(r, "send success packet");
418cd4349f9SBen Lindstrom 		/* now we can break out */
419cd4349f9SBen Lindstrom 		authctxt->success = 1;
420f1a19346Sdjm@openbsd.org 		ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
421cd4349f9SBen Lindstrom 	} else {
4220b4d48baSDamien Miller 		/* Allow initial try of "none" auth without failure penalty */
423058f839fSdjm@openbsd.org 		if (!partial && !authctxt->server_caused_failure &&
4243fcdfd55SDamien Miller 		    (authctxt->attempt > 1 || strcmp(method, "none") != 0))
4250b4d48baSDamien Miller 			authctxt->failures++;
426