Home | History | Annotate | Line # | Download | only in dist
sshd-session.c revision 1.13
      1  1.13  christos /*	$NetBSD: sshd-session.c,v 1.13 2026/05/16 15:08:30 christos Exp $	*/
      2  1.12  christos /* $OpenBSD: sshd-session.c,v 1.23 2026/03/11 09:10:59 dtucker Exp $ */
      3   1.2  christos 
      4   1.1  christos /*
      5   1.1  christos  * SSH2 implementation:
      6   1.1  christos  * Privilege Separation:
      7   1.1  christos  *
      8   1.1  christos  * Copyright (c) 2000, 2001, 2002 Markus Friedl.  All rights reserved.
      9   1.1  christos  * Copyright (c) 2002 Niels Provos.  All rights reserved.
     10   1.1  christos  *
     11   1.1  christos  * Redistribution and use in source and binary forms, with or without
     12   1.1  christos  * modification, are permitted provided that the following conditions
     13   1.1  christos  * are met:
     14   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     16   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  christos  *    documentation and/or other materials provided with the distribution.
     19   1.1  christos  *
     20   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.1  christos  */
     31   1.1  christos 
     32   1.2  christos #include "includes.h"
     33  1.13  christos __RCSID("$NetBSD: sshd-session.c,v 1.13 2026/05/16 15:08:30 christos Exp $");
     34   1.2  christos 
     35   1.1  christos #include <sys/types.h>
     36   1.2  christos #include <sys/param.h>
     37   1.1  christos #include <sys/ioctl.h>
     38   1.1  christos #include <sys/wait.h>
     39   1.1  christos #include <sys/tree.h>
     40   1.1  christos #include <sys/stat.h>
     41   1.1  christos #include <sys/socket.h>
     42   1.1  christos #include <sys/time.h>
     43   1.1  christos #include <sys/queue.h>
     44   1.1  christos 
     45  1.12  christos #ifdef WITH_OPENSSL
     46  1.12  christos #include <openssl/bn.h>
     47  1.12  christos #include <openssl/evp.h>
     48  1.12  christos #endif
     49  1.12  christos 
     50  1.12  christos #include <netinet/in.h>
     51  1.12  christos 
     52   1.1  christos #include <errno.h>
     53   1.1  christos #include <fcntl.h>
     54   1.1  christos #include <netdb.h>
     55   1.1  christos #include <paths.h>
     56   1.1  christos #include <pwd.h>
     57   1.1  christos #include <signal.h>
     58   1.1  christos #include <stdio.h>
     59   1.1  christos #include <stdlib.h>
     60   1.1  christos #include <string.h>
     61   1.1  christos #include <stdarg.h>
     62   1.1  christos #include <unistd.h>
     63   1.1  christos #include <limits.h>
     64   1.1  christos 
     65   1.1  christos #include "xmalloc.h"
     66   1.1  christos #include "ssh.h"
     67   1.1  christos #include "ssh2.h"
     68   1.1  christos #include "sshpty.h"
     69   1.1  christos #include "packet.h"
     70   1.1  christos #include "log.h"
     71   1.1  christos #include "sshbuf.h"
     72   1.1  christos #include "misc.h"
     73   1.1  christos #include "match.h"
     74   1.1  christos #include "servconf.h"
     75   1.1  christos #include "uidswap.h"
     76   1.1  christos #include "compat.h"
     77   1.1  christos #include "cipher.h"
     78   1.1  christos #include "digest.h"
     79   1.1  christos #include "sshkey.h"
     80   1.1  christos #include "kex.h"
     81   1.1  christos #include "authfile.h"
     82   1.1  christos #include "pathnames.h"
     83   1.1  christos #include "atomicio.h"
     84   1.1  christos #include "canohost.h"
     85   1.1  christos #include "hostfile.h"
     86   1.1  christos #include "auth.h"
     87   1.1  christos #include "authfd.h"
     88   1.1  christos #include "msg.h"
     89   1.1  christos #include "dispatch.h"
     90   1.1  christos #include "channels.h"
     91   1.1  christos #include "session.h"
     92   1.1  christos #include "monitor.h"
     93   1.1  christos #ifdef GSSAPI
     94   1.1  christos #include "ssh-gss.h"
     95   1.1  christos #endif
     96   1.1  christos #include "monitor_wrap.h"
     97   1.1  christos #include "auth-options.h"
     98   1.1  christos #include "version.h"
     99   1.1  christos #include "ssherr.h"
    100   1.1  christos #include "sk-api.h"
    101   1.1  christos #include "srclimit.h"
    102   1.1  christos #include "dh.h"
    103   1.1  christos 
    104   1.2  christos #include "pfilter.h"
    105   1.2  christos 
    106   1.2  christos #ifdef LIBWRAP
    107   1.2  christos #include <tcpd.h>
    108   1.2  christos #include <syslog.h>
    109   1.2  christos int allow_severity = LOG_INFO;
    110   1.2  christos int deny_severity = LOG_WARNING;
    111   1.6  christos 
    112   1.6  christos static void
    113   1.6  christos check_connection(const char *argv0, int sock_in)
    114   1.6  christos {
    115   1.6  christos 	struct request_info req;
    116   1.6  christos 
    117   1.6  christos 	request_init(&req, RQ_DAEMON, argv0, RQ_FILE, sock_in, 0);
    118   1.6  christos 	fromhost(&req);
    119   1.6  christos 
    120   1.6  christos 	if (hosts_access(&req))
    121   1.6  christos 		return;
    122   1.6  christos 	debug("Connection refused by tcp wrapper");
    123   1.6  christos 	/* n.b. hosts_access(3) has logged and notified blocklistd */
    124   1.6  christos 	refuse(&req);
    125   1.6  christos 	/* NOTREACHED */
    126   1.6  christos 	fatal("libwrap refuse returns");
    127   1.6  christos }
    128   1.2  christos #endif /* LIBWRAP */
    129   1.2  christos 
    130   1.2  christos #ifdef WITH_LDAP_PUBKEY
    131   1.2  christos #include "ldapauth.h"
    132   1.2  christos #endif
    133   1.2  christos 
    134   1.2  christos #ifndef HOST_NAME_MAX
    135   1.2  christos #define HOST_NAME_MAX MAXHOSTNAMELEN
    136   1.2  christos #endif
    137   1.2  christos 
    138   1.1  christos /* Re-exec fds */
    139   1.1  christos #define REEXEC_DEVCRYPTO_RESERVED_FD	(STDERR_FILENO + 1)
    140   1.9  christos #define REEXEC_CONFIG_PASS_FD		(STDERR_FILENO + 2)
    141   1.9  christos #define REEXEC_MIN_FREE_FD		(STDERR_FILENO + 3)
    142   1.9  christos 
    143   1.9  christos /* Privsep fds */
    144   1.9  christos #define PRIVSEP_MONITOR_FD		(STDERR_FILENO + 1)
    145   1.9  christos #define PRIVSEP_LOG_FD			(STDERR_FILENO + 2)
    146   1.9  christos #define PRIVSEP_MIN_FREE_FD		(STDERR_FILENO + 3)
    147   1.1  christos 
    148   1.1  christos extern char *__progname;
    149   1.1  christos 
    150   1.1  christos /* Server configuration options. */
    151   1.1  christos ServerOptions options;
    152   1.1  christos 
    153   1.1  christos /* Name of the server configuration file. */
    154   1.2  christos const char *config_file_name = _PATH_SERVER_CONFIG_FILE;
    155   1.1  christos 
    156   1.1  christos /*
    157   1.1  christos  * Debug mode flag.  This can be set on the command line.  If debug
    158   1.1  christos  * mode is enabled, extra debugging output will be sent to the system
    159   1.1  christos  * log, the daemon will not go to background, and will exit after processing
    160   1.1  christos  * the first connection.
    161   1.1  christos  */
    162   1.1  christos int debug_flag = 0;
    163   1.1  christos 
    164   1.1  christos /* Flag indicating that the daemon is being started from inetd. */
    165   1.1  christos static int inetd_flag = 0;
    166   1.1  christos 
    167   1.1  christos /* debug goes to stderr unless inetd_flag is set */
    168   1.1  christos static int log_stderr = 0;
    169   1.1  christos 
    170   1.1  christos /* Saved arguments to main(). */
    171   1.1  christos static char **saved_argv;
    172   1.1  christos 
    173   1.1  christos /* Daemon's agent connection */
    174   1.1  christos int auth_sock = -1;
    175   1.1  christos static int have_agent = 0;
    176   1.1  christos 
    177   1.1  christos /*
    178   1.1  christos  * Any really sensitive data in the application is contained in this
    179   1.1  christos  * structure. The idea is that this structure could be locked into memory so
    180   1.1  christos  * that the pages do not get written into swap.  However, there are some
    181   1.1  christos  * problems. The private key contains BIGNUMs, and we do not (in principle)
    182   1.1  christos  * have access to the internals of them, and locking just the structure is
    183   1.1  christos  * not very useful.  Currently, memory locking is not implemented.
    184   1.1  christos  */
    185   1.1  christos struct {
    186   1.1  christos 	u_int		num_hostkeys;
    187   1.1  christos 	struct sshkey	**host_keys;		/* all private host keys */
    188   1.1  christos 	struct sshkey	**host_pubkeys;		/* all public host keys */
    189   1.1  christos 	struct sshkey	**host_certificates;	/* all public host certificates */
    190   1.1  christos } sensitive_data;
    191   1.1  christos 
    192   1.1  christos /* record remote hostname or ip */
    193   1.1  christos u_int utmp_len = HOST_NAME_MAX+1;
    194   1.1  christos 
    195   1.1  christos static int startup_pipe = -1;		/* in child */
    196   1.1  christos 
    197   1.1  christos /* variables used for privilege separation */
    198   1.1  christos struct monitor *pmonitor = NULL;
    199   1.1  christos int privsep_is_preauth = 1;
    200   1.1  christos 
    201   1.1  christos /* global connection state and authentication contexts */
    202   1.1  christos Authctxt *the_authctxt = NULL;
    203   1.1  christos struct ssh *the_active_state;
    204   1.1  christos 
    205   1.1  christos /* global key/cert auth options. XXX move to permanent ssh->authctxt? */
    206   1.1  christos struct sshauthopt *auth_opts = NULL;
    207   1.1  christos 
    208   1.1  christos /* sshd_config buffer */
    209   1.1  christos struct sshbuf *cfg;
    210   1.1  christos 
    211   1.1  christos /* Included files from the configuration file */
    212   1.1  christos struct include_list includes = TAILQ_HEAD_INITIALIZER(includes);
    213   1.1  christos 
    214   1.1  christos /* message to be displayed after login */
    215   1.1  christos struct sshbuf *loginmsg;
    216   1.1  christos 
    217   1.1  christos /* Prototypes for various functions defined later in this file. */
    218   1.1  christos void destroy_sensitive_data(void);
    219   1.1  christos void demote_sensitive_data(void);
    220   1.9  christos 
    221   1.9  christos /* XXX reduce to stub once postauth split */
    222   1.9  christos int
    223   1.9  christos mm_is_monitor(void)
    224   1.9  christos {
    225   1.9  christos 	/*
    226   1.9  christos 	 * m_pid is only set in the privileged part, and
    227   1.9  christos 	 * points to the unprivileged child.
    228   1.9  christos 	 */
    229   1.9  christos 	return (pmonitor && pmonitor->m_pid > 0);
    230   1.9  christos }
    231   1.1  christos 
    232   1.1  christos /*
    233   1.1  christos  * Signal handler for the alarm after the login grace period has expired.
    234   1.1  christos  * As usual, this may only take signal-safe actions, even though it is
    235   1.1  christos  * terminal.
    236   1.1  christos  */
    237   1.3  riastrad __dead
    238   1.1  christos static void
    239   1.1  christos grace_alarm_handler(int sig)
    240   1.1  christos {
    241   1.2  christos 	pfilter_notify(1);
    242   1.1  christos 	/*
    243   1.1  christos 	 * Try to kill any processes that we have spawned, E.g. authorized
    244   1.1  christos 	 * keys command helpers or privsep children.
    245   1.1  christos 	 */
    246   1.1  christos 	if (getpgid(0) == getpid()) {
    247   1.1  christos 		struct sigaction sa;
    248   1.1  christos 
    249   1.1  christos 		/* mask all other signals while in handler */
    250   1.1  christos 		memset(&sa, 0, sizeof(sa));
    251   1.1  christos 		sa.sa_handler = SIG_IGN;
    252   1.1  christos 		sigfillset(&sa.sa_mask);
    253   1.1  christos 		sa.sa_flags = SA_RESTART;
    254   1.1  christos 		(void)sigaction(SIGTERM, &sa, NULL);
    255   1.1  christos 		kill(0, SIGTERM);
    256   1.1  christos 	}
    257   1.1  christos 	_exit(EXIT_LOGIN_GRACE);
    258   1.1  christos }
    259   1.1  christos 
    260   1.1  christos /* Destroy the host and server keys.  They will no longer be needed. */
    261   1.1  christos void
    262   1.1  christos destroy_sensitive_data(void)
    263   1.1  christos {
    264   1.1  christos 	u_int i;
    265   1.1  christos 
    266   1.1  christos 	for (i = 0; i < options.num_host_key_files; i++) {
    267   1.1  christos 		if (sensitive_data.host_keys[i]) {
    268   1.1  christos 			sshkey_free(sensitive_data.host_keys[i]);
    269   1.1  christos 			sensitive_data.host_keys[i] = NULL;
    270   1.1  christos 		}
    271   1.1  christos 		if (sensitive_data.host_certificates[i]) {
    272   1.1  christos 			sshkey_free(sensitive_data.host_certificates[i]);
    273   1.1  christos 			sensitive_data.host_certificates[i] = NULL;
    274   1.1  christos 		}
    275   1.1  christos 	}
    276   1.1  christos }
    277   1.1  christos 
    278   1.1  christos /* Demote private to public keys for network child */
    279   1.1  christos void
    280   1.1  christos demote_sensitive_data(void)
    281   1.1  christos {
    282   1.1  christos 	struct sshkey *tmp;
    283   1.1  christos 	u_int i;
    284   1.1  christos 	int r;
    285   1.1  christos 
    286   1.1  christos 	for (i = 0; i < options.num_host_key_files; i++) {
    287   1.1  christos 		if (sensitive_data.host_keys[i]) {
    288   1.1  christos 			if ((r = sshkey_from_private(
    289   1.1  christos 			    sensitive_data.host_keys[i], &tmp)) != 0)
    290   1.1  christos 				fatal_r(r, "could not demote host %s key",
    291   1.1  christos 				    sshkey_type(sensitive_data.host_keys[i]));
    292   1.1  christos 			sshkey_free(sensitive_data.host_keys[i]);
    293   1.1  christos 			sensitive_data.host_keys[i] = tmp;
    294   1.1  christos 		}
    295   1.1  christos 		/* Certs do not need demotion */
    296   1.1  christos 	}
    297   1.1  christos }
    298   1.1  christos 
    299   1.9  christos struct sshbuf *
    300   1.9  christos pack_hostkeys(void)
    301   1.1  christos {
    302   1.9  christos 	struct sshbuf *keybuf = NULL, *hostkeys = NULL;
    303   1.9  christos 	int r;
    304   1.9  christos 	u_int i;
    305   1.1  christos 
    306   1.9  christos 	if ((hostkeys = sshbuf_new()) == NULL)
    307   1.9  christos 		fatal_f("sshbuf_new failed");
    308   1.1  christos 
    309   1.9  christos 	/* pack hostkeys into a string. Empty key slots get empty strings */
    310   1.9  christos 	for (i = 0; i < options.num_host_key_files; i++) {
    311   1.9  christos 		/* public key */
    312   1.9  christos 		if (sensitive_data.host_pubkeys[i] != NULL) {
    313   1.9  christos 			if ((r = sshkey_puts(sensitive_data.host_pubkeys[i],
    314   1.9  christos 			    hostkeys)) != 0)
    315   1.9  christos 				fatal_fr(r, "compose hostkey public");
    316   1.9  christos 		} else {
    317   1.9  christos 			if ((r = sshbuf_put_string(hostkeys, NULL, 0)) != 0)
    318   1.9  christos 				fatal_fr(r, "compose hostkey empty public");
    319   1.9  christos 		}
    320   1.9  christos 		/* cert */
    321   1.9  christos 		if (sensitive_data.host_certificates[i] != NULL) {
    322   1.9  christos 			if ((r = sshkey_puts(
    323   1.9  christos 			    sensitive_data.host_certificates[i],
    324   1.9  christos 			    hostkeys)) != 0)
    325   1.9  christos 				fatal_fr(r, "compose host cert");
    326   1.9  christos 		} else {
    327   1.9  christos 			if ((r = sshbuf_put_string(hostkeys, NULL, 0)) != 0)
    328   1.9  christos 				fatal_fr(r, "compose host cert empty");
    329   1.9  christos 		}
    330   1.9  christos 	}
    331   1.1  christos 
    332   1.9  christos 	sshbuf_free(keybuf);
    333   1.9  christos 	return hostkeys;
    334   1.1  christos }
    335   1.1  christos 
    336   1.1  christos static int
    337   1.1  christos privsep_preauth(struct ssh *ssh)
    338   1.1  christos {
    339  1.11  christos 	int r;
    340   1.1  christos 	pid_t pid;
    341   1.1  christos 
    342   1.1  christos 	/* Set up unprivileged child process to deal with network data */
    343   1.1  christos 	pmonitor = monitor_init();
    344   1.1  christos 	/* Store a pointer to the kex for later rekeying */
    345   1.1  christos 	pmonitor->m_pkex = &ssh->kex;
    346   1.1  christos 
    347   1.9  christos 	if ((pid = fork()) == -1)
    348   1.1  christos 		fatal("fork of unprivileged child failed");
    349   1.9  christos 	else if (pid != 0) {
    350   1.1  christos 		debug2("Network child is on pid %ld", (long)pid);
    351   1.1  christos 		pmonitor->m_pid = pid;
    352   1.1  christos 		if (have_agent) {
    353   1.1  christos 			r = ssh_get_authentication_socket(&auth_sock);
    354   1.1  christos 			if (r != 0) {
    355   1.1  christos 				error_r(r, "Could not get agent socket");
    356   1.1  christos 				have_agent = 0;
    357   1.1  christos 			}
    358   1.1  christos 		}
    359   1.1  christos 		monitor_child_preauth(ssh, pmonitor);
    360   1.1  christos 		privsep_is_preauth = 0;
    361   1.1  christos 		return 1;
    362   1.1  christos 	} else {
    363   1.1  christos 		/* child */
    364   1.1  christos 		close(pmonitor->m_sendfd);
    365   1.1  christos 		close(pmonitor->m_log_recvfd);
    366   1.1  christos 
    367   1.9  christos 		/*
    368   1.9  christos 		 * Arrange unpriv-preauth child process fds:
    369   1.9  christos 		 * 0, 1 network socket
    370   1.9  christos 		 * 2 optional stderr
    371   1.9  christos 		 * 3 reserved
    372   1.9  christos 		 * 4 monitor message socket
    373   1.9  christos 		 * 5 monitor logging socket
    374   1.9  christos 		 *
    375   1.9  christos 		 * We know that the monitor sockets will have fds > 4 because
    376   1.9  christos 		 * of the reserved fds in main()
    377   1.9  christos 		 */
    378   1.9  christos 
    379   1.9  christos 		if (ssh_packet_get_connection_in(ssh) != STDIN_FILENO &&
    380   1.9  christos 		    dup2(ssh_packet_get_connection_in(ssh), STDIN_FILENO) == -1)
    381   1.9  christos 			fatal("dup2 stdin failed: %s", strerror(errno));
    382   1.9  christos 		if (ssh_packet_get_connection_out(ssh) != STDOUT_FILENO &&
    383   1.9  christos 		    dup2(ssh_packet_get_connection_out(ssh),
    384   1.9  christos 		    STDOUT_FILENO) == -1)
    385   1.9  christos 			fatal("dup2 stdout failed: %s", strerror(errno));
    386   1.9  christos 		/* leave stderr as-is */
    387   1.9  christos 		log_redirect_stderr_to(NULL); /* dup can clobber log fd */
    388   1.9  christos 		if (pmonitor->m_recvfd != PRIVSEP_MONITOR_FD &&
    389   1.9  christos 		    dup2(pmonitor->m_recvfd, PRIVSEP_MONITOR_FD) == -1)
    390   1.9  christos 			fatal("dup2 monitor fd: %s", strerror(errno));
    391   1.9  christos 		if (pmonitor->m_log_sendfd != PRIVSEP_LOG_FD &&
    392   1.9  christos 		    dup2(pmonitor->m_log_sendfd, PRIVSEP_LOG_FD) == -1)
    393   1.9  christos 			fatal("dup2 log fd: %s", strerror(errno));
    394   1.9  christos 		closefrom(PRIVSEP_MIN_FREE_FD);
    395   1.1  christos 
    396   1.9  christos 		saved_argv[0] = options.sshd_auth_path;
    397   1.9  christos 		execv(options.sshd_auth_path, saved_argv);
    398   1.1  christos 
    399   1.9  christos 		fatal_f("exec of %s failed: %s",
    400   1.9  christos 		    options.sshd_auth_path, strerror(errno));
    401   1.1  christos 	}
    402   1.1  christos }
    403   1.1  christos 
    404   1.1  christos static void
    405   1.1  christos privsep_postauth(struct ssh *ssh, Authctxt *authctxt)
    406   1.1  christos {
    407   1.1  christos 	/* New socket pair */
    408   1.1  christos 	monitor_reinit(pmonitor);
    409   1.1  christos 
    410   1.1  christos 	pmonitor->m_pid = fork();
    411   1.1  christos 	if (pmonitor->m_pid == -1)
    412   1.1  christos 		fatal("fork of unprivileged child failed");
    413   1.1  christos 	else if (pmonitor->m_pid != 0) {
    414   1.1  christos 		verbose("User child is on pid %ld", (long)pmonitor->m_pid);
    415   1.1  christos 		sshbuf_reset(loginmsg);
    416   1.1  christos 		monitor_clear_keystate(ssh, pmonitor);
    417   1.1  christos 		monitor_child_postauth(ssh, pmonitor);
    418   1.1  christos 
    419   1.1  christos 		/* NEVERREACHED */
    420   1.1  christos 		exit(0);
    421   1.1  christos 	}
    422   1.1  christos 
    423   1.1  christos 	/* child */
    424   1.1  christos 
    425   1.1  christos 	close(pmonitor->m_sendfd);
    426   1.1  christos 	pmonitor->m_sendfd = -1;
    427   1.1  christos 
    428   1.1  christos 	/* Demote the private keys to public keys. */
    429   1.1  christos 	demote_sensitive_data();
    430   1.1  christos 
    431   1.1  christos 	/* Drop privileges */
    432   1.1  christos 	do_setusercontext(authctxt->pw);
    433   1.1  christos 
    434   1.1  christos 	/* It is safe now to apply the key state */
    435   1.1  christos 	monitor_apply_keystate(ssh, pmonitor);
    436   1.1  christos 
    437   1.1  christos 	/*
    438   1.1  christos 	 * Tell the packet layer that authentication was successful, since
    439   1.1  christos 	 * this information is not part of the key state.
    440   1.1  christos 	 */
    441   1.1  christos 	ssh_packet_set_authenticated(ssh);
    442   1.1  christos }
    443   1.1  christos 
    444   1.1  christos static struct sshkey *
    445   1.1  christos get_hostkey_by_type(int type, int nid, int need_private, struct ssh *ssh)
    446   1.1  christos {
    447   1.1  christos 	u_int i;
    448   1.1  christos 	struct sshkey *key;
    449   1.1  christos 
    450   1.1  christos 	for (i = 0; i < options.num_host_key_files; i++) {
    451   1.1  christos 		switch (type) {
    452   1.1  christos 		case KEY_RSA_CERT:
    453   1.1  christos 		case KEY_ECDSA_CERT:
    454   1.1  christos 		case KEY_ED25519_CERT:
    455   1.1  christos 		case KEY_ECDSA_SK_CERT:
    456   1.1  christos 		case KEY_ED25519_SK_CERT:
    457   1.1  christos 			key = sensitive_data.host_certificates[i];
    458   1.1  christos 			break;
    459   1.1  christos 		default:
    460   1.1  christos 			key = sensitive_data.host_keys[i];
    461   1.1  christos 			if (key == NULL && !need_private)
    462   1.1  christos 				key = sensitive_data.host_pubkeys[i];
    463   1.1  christos 			break;
    464   1.1  christos 		}
    465   1.1  christos 		if (key == NULL || key->type != type)
    466   1.1  christos 			continue;
    467   1.1  christos 		switch (type) {
    468   1.1  christos 		case KEY_ECDSA:
    469   1.1  christos 		case KEY_ECDSA_SK:
    470   1.1  christos 		case KEY_ECDSA_CERT:
    471   1.1  christos 		case KEY_ECDSA_SK_CERT:
    472   1.1  christos 			if (key->ecdsa_nid != nid)
    473   1.1  christos 				continue;
    474   1.1  christos 			/* FALLTHROUGH */
    475   1.1  christos 		default:
    476   1.1  christos 			return need_private ?
    477   1.1  christos 			    sensitive_data.host_keys[i] : key;
    478   1.1  christos 		}
    479   1.1  christos 	}
    480   1.1  christos 	return NULL;
    481   1.1  christos }
    482   1.1  christos 
    483   1.1  christos struct sshkey *
    484   1.1  christos get_hostkey_public_by_type(int type, int nid, struct ssh *ssh)
    485   1.1  christos {
    486   1.1  christos 	return get_hostkey_by_type(type, nid, 0, ssh);
    487   1.1  christos }
    488   1.1  christos 
    489   1.1  christos struct sshkey *
    490   1.1  christos get_hostkey_private_by_type(int type, int nid, struct ssh *ssh)
    491   1.1  christos {
    492   1.1  christos 	return get_hostkey_by_type(type, nid, 1, ssh);
    493   1.1  christos }
    494   1.1  christos 
    495   1.1  christos struct sshkey *
    496   1.1  christos get_hostkey_by_index(int ind)
    497   1.1  christos {
    498   1.1  christos 	if (ind < 0 || (u_int)ind >= options.num_host_key_files)
    499   1.1  christos 		return (NULL);
    500   1.1  christos 	return (sensitive_data.host_keys[ind]);
    501   1.1  christos }
    502   1.1  christos 
    503   1.1  christos struct sshkey *
    504   1.1  christos get_hostkey_public_by_index(int ind, struct ssh *ssh)
    505   1.1  christos {
    506   1.1  christos 	if (ind < 0 || (u_int)ind >= options.num_host_key_files)
    507   1.1  christos 		return (NULL);
    508   1.1  christos 	return (sensitive_data.host_pubkeys[ind]);
    509   1.1  christos }
    510   1.1  christos 
    511   1.1  christos int
    512   1.1  christos get_hostkey_index(struct sshkey *key, int compare, struct ssh *ssh)
    513   1.1  christos {
    514   1.1  christos 	u_int i;
    515   1.1  christos 
    516   1.1  christos 	for (i = 0; i < options.num_host_key_files; i++) {
    517   1.1  christos 		if (sshkey_is_cert(key)) {
    518   1.1  christos 			if (key == sensitive_data.host_certificates[i] ||
    519   1.1  christos 			    (compare && sensitive_data.host_certificates[i] &&
    520   1.1  christos 			    sshkey_equal(key,
    521   1.1  christos 			    sensitive_data.host_certificates[i])))
    522   1.1  christos 				return (i);
    523   1.1  christos 		} else {
    524   1.1  christos 			if (key == sensitive_data.host_keys[i] ||
    525   1.1  christos 			    (compare && sensitive_data.host_keys[i] &&
    526   1.1  christos 			    sshkey_equal(key, sensitive_data.host_keys[i])))
    527   1.1  christos 				return (i);
    528   1.1  christos 			if (key == sensitive_data.host_pubkeys[i] ||
    529   1.1  christos 			    (compare && sensitive_data.host_pubkeys[i] &&
    530   1.1  christos 			    sshkey_equal(key, sensitive_data.host_pubkeys[i])))
    531   1.1  christos 				return (i);
    532   1.1  christos 		}
    533   1.1  christos 	}
    534   1.1  christos 	return (-1);
    535   1.1  christos }
    536   1.1  christos 
    537   1.1  christos /* Inform the client of all hostkeys */
    538   1.1  christos static void
    539   1.1  christos notify_hostkeys(struct ssh *ssh)
    540   1.1  christos {
    541   1.1  christos 	struct sshbuf *buf;
    542   1.1  christos 	struct sshkey *key;
    543   1.1  christos 	u_int i, nkeys;
    544   1.1  christos 	int r;
    545   1.1  christos 	char *fp;
    546   1.1  christos 
    547   1.1  christos 	/* Some clients cannot cope with the hostkeys message, skip those. */
    548   1.1  christos 	if (ssh->compat & SSH_BUG_HOSTKEYS)
    549   1.1  christos 		return;
    550   1.1  christos 
    551   1.1  christos 	if ((buf = sshbuf_new()) == NULL)
    552   1.1  christos 		fatal_f("sshbuf_new");
    553   1.1  christos 	for (i = nkeys = 0; i < options.num_host_key_files; i++) {
    554   1.1  christos 		key = get_hostkey_public_by_index(i, ssh);
    555   1.1  christos 		if (key == NULL || key->type == KEY_UNSPEC ||
    556   1.1  christos 		    sshkey_is_cert(key))
    557   1.1  christos 			continue;
    558   1.1  christos 		fp = sshkey_fingerprint(key, options.fingerprint_hash,
    559   1.1  christos 		    SSH_FP_DEFAULT);
    560   1.1  christos 		debug3_f("key %d: %s %s", i, sshkey_ssh_name(key), fp);
    561   1.1  christos 		free(fp);
    562   1.1  christos 		if (nkeys == 0) {
    563   1.1  christos 			/*
    564   1.1  christos 			 * Start building the request when we find the
    565   1.1  christos 			 * first usable key.
    566   1.1  christos 			 */
    567   1.1  christos 			if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
    568   1.1  christos 			    (r = sshpkt_put_cstring(ssh, "hostkeys-00 (at) openssh.com")) != 0 ||
    569   1.1  christos 			    (r = sshpkt_put_u8(ssh, 0)) != 0) /* want reply */
    570   1.1  christos 				sshpkt_fatal(ssh, r, "%s: start request", __func__);
    571   1.1  christos 		}
    572   1.1  christos 		/* Append the key to the request */
    573   1.1  christos 		sshbuf_reset(buf);
    574   1.1  christos 		if ((r = sshkey_putb(key, buf)) != 0)
    575   1.1  christos 			fatal_fr(r, "couldn't put hostkey %d", i);
    576   1.1  christos 		if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
    577   1.1  christos 			sshpkt_fatal(ssh, r, "%s: append key", __func__);
    578   1.1  christos 		nkeys++;
    579   1.1  christos 	}
    580   1.1  christos 	debug3_f("sent %u hostkeys", nkeys);
    581   1.1  christos 	if (nkeys == 0)
    582   1.1  christos 		fatal_f("no hostkeys");
    583   1.1  christos 	if ((r = sshpkt_send(ssh)) != 0)
    584   1.1  christos 		sshpkt_fatal(ssh, r, "%s: send", __func__);
    585   1.1  christos 	sshbuf_free(buf);
    586   1.1  christos }
    587   1.1  christos 
    588   1.2  christos __dead static void
    589   1.1  christos usage(void)
    590   1.1  christos {
    591   1.1  christos 	fprintf(stderr, "%s, %s\n", SSH_VERSION, SSH_OPENSSL_VERSION);
    592   1.1  christos 	fprintf(stderr,
    593   1.1  christos "usage: sshd [-46DdeGiqTtV] [-C connection_spec] [-c host_cert_file]\n"
    594   1.1  christos "            [-E log_file] [-f config_file] [-g login_grace_time]\n"
    595   1.1  christos "            [-h host_key_file] [-o option] [-p port] [-u len]\n"
    596   1.1  christos 	);
    597   1.1  christos 	exit(1);
    598   1.1  christos }
    599   1.1  christos 
    600   1.1  christos static void
    601   1.1  christos parse_hostkeys(struct sshbuf *hostkeys)
    602   1.1  christos {
    603   1.1  christos 	int r;
    604   1.1  christos 	u_int num_keys = 0;
    605   1.1  christos 	struct sshkey *k;
    606   1.1  christos 	struct sshbuf *kbuf;
    607   1.1  christos 	const u_char *cp;
    608   1.1  christos 	size_t len;
    609   1.1  christos 
    610   1.1  christos 	while (sshbuf_len(hostkeys) != 0) {
    611   1.1  christos 		if (num_keys > 2048)
    612   1.1  christos 			fatal_f("too many hostkeys");
    613   1.1  christos 		sensitive_data.host_keys = xrecallocarray(
    614   1.1  christos 		    sensitive_data.host_keys, num_keys, num_keys + 1,
    615   1.1  christos 		    sizeof(*sensitive_data.host_pubkeys));
    616   1.1  christos 		sensitive_data.host_pubkeys = xrecallocarray(
    617   1.1  christos 		    sensitive_data.host_pubkeys, num_keys, num_keys + 1,
    618   1.1  christos 		    sizeof(*sensitive_data.host_pubkeys));
    619   1.1  christos 		sensitive_data.host_certificates = xrecallocarray(
    620   1.1  christos 		    sensitive_data.host_certificates, num_keys, num_keys + 1,
    621   1.1  christos 		    sizeof(*sensitive_data.host_certificates));
    622   1.1  christos 		/* private key */
    623   1.1  christos 		k = NULL;
    624   1.1  christos 		if ((r = sshbuf_froms(hostkeys, &kbuf)) != 0)
    625   1.1  christos 			fatal_fr(r, "extract privkey");
    626   1.1  christos 		if (sshbuf_len(kbuf) != 0 &&
    627   1.1  christos 		    (r = sshkey_private_deserialize(kbuf, &k)) != 0)
    628   1.1  christos 			fatal_fr(r, "parse pubkey");
    629   1.1  christos 		sensitive_data.host_keys[num_keys] = k;
    630   1.1  christos 		sshbuf_free(kbuf);
    631   1.1  christos 		if (k)
    632   1.1  christos 			debug2_f("privkey %u: %s", num_keys, sshkey_ssh_name(k));
    633   1.1  christos 		/* public key */
    634   1.1  christos 		k = NULL;
    635   1.1  christos 		if ((r = sshbuf_get_string_direct(hostkeys, &cp, &len)) != 0)
    636   1.1  christos 			fatal_fr(r, "extract pubkey");
    637   1.1  christos 		if (len != 0 && (r = sshkey_from_blob(cp, len, &k)) != 0)
    638   1.1  christos 			fatal_fr(r, "parse pubkey");
    639   1.1  christos 		sensitive_data.host_pubkeys[num_keys] = k;
    640   1.1  christos 		if (k)
    641   1.1  christos 			debug2_f("pubkey %u: %s", num_keys, sshkey_ssh_name(k));
    642   1.1  christos 		/* certificate */
    643   1.1  christos 		k = NULL;
    644   1.1  christos 		if ((r = sshbuf_get_string_direct(hostkeys, &cp, &len)) != 0)
    645   1.1  christos 			fatal_fr(r, "extract pubkey");
    646   1.1  christos 		if (len != 0 && (r = sshkey_from_blob(cp, len, &k)) != 0)
    647   1.1  christos 			fatal_fr(r, "parse pubkey");
    648   1.1  christos 		sensitive_data.host_certificates[num_keys] = k;
    649   1.1  christos 		if (k)
    650   1.1  christos 			debug2_f("cert %u: %s", num_keys, sshkey_ssh_name(k));
    651   1.1  christos 		num_keys++;
    652   1.1  christos 	}
    653   1.1  christos 	sensitive_data.num_hostkeys = num_keys;
    654   1.1  christos }
    655   1.1  christos 
    656   1.1  christos static void
    657   1.1  christos recv_rexec_state(int fd, struct sshbuf *conf, uint64_t *timing_secretp)
    658   1.1  christos {
    659   1.1  christos 	struct sshbuf *m, *inc, *hostkeys;
    660   1.1  christos 	u_char *cp, ver;
    661   1.1  christos 	size_t len;
    662   1.1  christos 	int r;
    663   1.1  christos 	struct include_item *item;
    664   1.1  christos 
    665   1.1  christos 	debug3_f("entering fd = %d", fd);
    666   1.1  christos 
    667   1.1  christos 	if ((m = sshbuf_new()) == NULL || (inc = sshbuf_new()) == NULL)
    668   1.1  christos 		fatal_f("sshbuf_new failed");
    669   1.9  christos 
    670   1.9  christos 	/* receive config */
    671   1.1  christos 	if (ssh_msg_recv(fd, m) == -1)
    672   1.1  christos 		fatal_f("ssh_msg_recv failed");
    673   1.1  christos 	if ((r = sshbuf_get_u8(m, &ver)) != 0)
    674   1.1  christos 		fatal_fr(r, "parse version");
    675   1.1  christos 	if (ver != 0)
    676   1.1  christos 		fatal_f("rexec version mismatch");
    677   1.1  christos 	if ((r = sshbuf_get_string(m, &cp, &len)) != 0 || /* XXX _direct */
    678   1.1  christos 	    (r = sshbuf_get_u64(m, timing_secretp)) != 0 ||
    679   1.1  christos 	    (r = sshbuf_get_stringb(m, inc)) != 0)
    680   1.1  christos 		fatal_fr(r, "parse config");
    681   1.1  christos 
    682   1.1  christos 	if (conf != NULL && (r = sshbuf_put(conf, cp, len)))
    683   1.1  christos 		fatal_fr(r, "sshbuf_put");
    684   1.1  christos 
    685   1.1  christos 	while (sshbuf_len(inc) != 0) {
    686   1.1  christos 		item = xcalloc(1, sizeof(*item));
    687   1.1  christos 		if ((item->contents = sshbuf_new()) == NULL)
    688   1.1  christos 			fatal_f("sshbuf_new failed");
    689   1.1  christos 		if ((r = sshbuf_get_cstring(inc, &item->selector, NULL)) != 0 ||
    690   1.1  christos 		    (r = sshbuf_get_cstring(inc, &item->filename, NULL)) != 0 ||
    691   1.1  christos 		    (r = sshbuf_get_stringb(inc, item->contents)) != 0)
    692   1.1  christos 			fatal_fr(r, "parse includes");
    693   1.1  christos 		TAILQ_INSERT_TAIL(&includes, item, entry);
    694   1.1  christos 	}
    695   1.1  christos 
    696   1.9  christos 	/* receive hostkeys */
    697   1.9  christos 	sshbuf_reset(m);
    698   1.9  christos 	if (ssh_msg_recv(fd, m) == -1)
    699   1.9  christos 		fatal_f("ssh_msg_recv failed");
    700   1.9  christos 	if ((r = sshbuf_get_u8(m, NULL)) != 0 ||
    701   1.9  christos 	    (r = sshbuf_froms(m, &hostkeys)) != 0)
    702   1.9  christos 		fatal_fr(r, "parse config");
    703   1.1  christos 	parse_hostkeys(hostkeys);
    704   1.1  christos 
    705   1.1  christos 	free(cp);
    706   1.1  christos 	sshbuf_free(m);
    707   1.1  christos 	sshbuf_free(hostkeys);
    708   1.1  christos 	sshbuf_free(inc);
    709   1.1  christos 
    710   1.1  christos 	debug3_f("done");
    711   1.1  christos }
    712   1.1  christos 
    713   1.1  christos /*
    714   1.1  christos  * If IP options are supported, make sure there are none (log and
    715   1.1  christos  * return an error if any are found).  Basically we are worried about
    716   1.1  christos  * source routing; it can be used to pretend you are somebody
    717   1.1  christos  * (ip-address) you are not. That itself may be "almost acceptable"
    718   1.1  christos  * under certain circumstances, but rhosts authentication is useless
    719   1.1  christos  * if source routing is accepted. Notice also that if we just dropped
    720   1.1  christos  * source routing here, the other side could use IP spoofing to do
    721   1.1  christos  * rest of the interaction and could still bypass security.  So we
    722   1.1  christos  * exit here if we detect any IP options.
    723   1.1  christos  */
    724   1.1  christos static void
    725   1.1  christos check_ip_options(struct ssh *ssh)
    726   1.1  christos {
    727   1.1  christos 	int sock_in = ssh_packet_get_connection_in(ssh);
    728   1.1  christos 	struct sockaddr_storage from;
    729   1.1  christos 	u_char opts[200];
    730   1.1  christos 	socklen_t i, option_size = sizeof(opts), fromlen = sizeof(from);
    731   1.1  christos 	char text[sizeof(opts) * 3 + 1];
    732   1.1  christos 
    733   1.1  christos 	memset(&from, 0, sizeof(from));
    734   1.1  christos 	if (getpeername(sock_in, (struct sockaddr *)&from,
    735   1.1  christos 	    &fromlen) == -1)
    736   1.1  christos 		return;
    737   1.1  christos 	if (from.ss_family != AF_INET)
    738   1.1  christos 		return;
    739   1.1  christos 	/* XXX IPv6 options? */
    740   1.1  christos 
    741   1.1  christos 	if (getsockopt(sock_in, IPPROTO_IP, IP_OPTIONS, opts,
    742   1.1  christos 	    &option_size) >= 0 && option_size != 0) {
    743   1.1  christos 		text[0] = '\0';
    744   1.1  christos 		for (i = 0; i < option_size; i++)
    745   1.1  christos 			snprintf(text + i*3, sizeof(text) - i*3,
    746   1.1  christos 			    " %2.2x", opts[i]);
    747   1.1  christos 		fatal("Connection from %.100s port %d with IP opts: %.800s",
    748   1.1  christos 		    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), text);
    749   1.1  christos 	}
    750   1.1  christos }
    751   1.1  christos 
    752   1.2  christos #ifdef __OpenBSD__
    753   1.1  christos /* Set the routing domain for this process */
    754   1.1  christos static void
    755   1.1  christos set_process_rdomain(struct ssh *ssh, const char *name)
    756   1.1  christos {
    757   1.1  christos 	int rtable, ortable = getrtable();
    758   1.1  christos 	const char *errstr;
    759   1.1  christos 
    760   1.1  christos 	if (name == NULL)
    761   1.1  christos 		return; /* default */
    762   1.1  christos 
    763   1.1  christos 	if (strcmp(name, "%D") == 0) {
    764   1.1  christos 		/* "expands" to routing domain of connection */
    765   1.1  christos 		if ((name = ssh_packet_rdomain_in(ssh)) == NULL)
    766   1.1  christos 			return;
    767   1.1  christos 	}
    768   1.1  christos 
    769   1.1  christos 	rtable = (int)strtonum(name, 0, 255, &errstr);
    770   1.1  christos 	if (errstr != NULL) /* Shouldn't happen */
    771   1.1  christos 		fatal("Invalid routing domain \"%s\": %s", name, errstr);
    772   1.1  christos 	if (rtable != ortable && setrtable(rtable) != 0)
    773   1.1  christos 		fatal("Unable to set routing domain %d: %s",
    774   1.1  christos 		    rtable, strerror(errno));
    775   1.1  christos 	debug_f("set routing domain %d (was %d)", rtable, ortable);
    776   1.1  christos }
    777   1.2  christos #endif
    778   1.1  christos 
    779   1.1  christos /*
    780   1.1  christos  * Main program for the daemon.
    781   1.1  christos  */
    782   1.1  christos int
    783   1.1  christos main(int ac, char **av)
    784   1.1  christos {
    785   1.1  christos 	struct ssh *ssh = NULL;
    786   1.1  christos 	extern char *optarg;
    787   1.1  christos 	extern int optind;
    788   1.9  christos 	int devnull, r, opt, on = 1, remote_port;
    789   1.1  christos 	int sock_in = -1, sock_out = -1, rexeced_flag = 0, have_key = 0;
    790   1.1  christos 	const char *remote_ip, *rdomain;
    791   1.1  christos 	char *line, *laddr, *logfile = NULL;
    792   1.1  christos 	u_int i;
    793   1.1  christos 	u_int64_t ibytes, obytes;
    794   1.1  christos 	mode_t new_umask;
    795   1.1  christos 	Authctxt *authctxt;
    796   1.1  christos 	struct connection_info *connection_info = NULL;
    797   1.1  christos 	sigset_t sigmask;
    798   1.1  christos 	uint64_t timing_secret = 0;
    799   1.4  christos 	struct itimerval itv;
    800   1.1  christos 
    801   1.1  christos 	sigemptyset(&sigmask);
    802   1.1  christos 	sigprocmask(SIG_SETMASK, &sigmask, NULL);
    803   1.1  christos 
    804   1.1  christos 	/* Save argv. */
    805   1.1  christos 	saved_argv = av;
    806   1.1  christos 
    807   1.1  christos 	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
    808   1.1  christos 	sanitise_stdfd();
    809   1.1  christos 
    810   1.1  christos 	/* Initialize configuration options to their default values. */
    811   1.1  christos 	initialize_server_options(&options);
    812   1.1  christos 
    813   1.1  christos 	/* Parse command-line arguments. */
    814   1.1  christos 	while ((opt = getopt(ac, av,
    815   1.1  christos 	    "C:E:b:c:f:g:h:k:o:p:u:46DGQRTdeiqrtV")) != -1) {
    816   1.1  christos 		switch (opt) {
    817   1.1  christos 		case '4':
    818   1.1  christos 			options.address_family = AF_INET;
    819   1.1  christos 			break;
    820   1.1  christos 		case '6':
    821   1.1  christos 			options.address_family = AF_INET6;
    822   1.1  christos 			break;
    823   1.1  christos 		case 'f':
    824   1.1  christos 			config_file_name = optarg;
    825   1.1  christos 			break;
    826   1.1  christos 		case 'c':
    827   1.1  christos 			servconf_add_hostcert("[command-line]", 0,
    828   1.1  christos 			    &options, optarg);
    829   1.1  christos 			break;
    830   1.1  christos 		case 'd':
    831   1.1  christos 			if (debug_flag == 0) {
    832   1.1  christos 				debug_flag = 1;
    833   1.1  christos 				options.log_level = SYSLOG_LEVEL_DEBUG1;
    834   1.1  christos 			} else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
    835   1.1  christos 				options.log_level++;
    836   1.1  christos 			break;
    837   1.1  christos 		case 'D':
    838   1.1  christos 			/* ignore */
    839   1.1  christos 			break;
    840   1.1  christos 		case 'E':
    841   1.1  christos 			logfile = optarg;
    842   1.1  christos 			/* FALLTHROUGH */
    843   1.1  christos 		case 'e':
    844   1.1  christos 			log_stderr = 1;
    845   1.1  christos 			break;
    846   1.1  christos 		case 'i':
    847   1.1  christos 			inetd_flag = 1;
    848   1.1  christos 			break;
    849   1.1  christos 		case 'r':
    850   1.1  christos 			/* ignore */
    851   1.1  christos 			break;
    852   1.1  christos 		case 'R':
    853   1.1  christos 			rexeced_flag = 1;
    854   1.1  christos 			break;
    855   1.1  christos 		case 'Q':
    856   1.1  christos 			/* ignored */
    857   1.1  christos 			break;
    858   1.1  christos 		case 'q':
    859   1.1  christos 			options.log_level = SYSLOG_LEVEL_QUIET;
    860   1.1  christos 			break;
    861   1.1  christos 		case 'b':
    862   1.1  christos 			/* protocol 1, ignored */
    863   1.1  christos 			break;
    864   1.1  christos 		case 'p':
    865   1.1  christos 			options.ports_from_cmdline = 1;
    866   1.1  christos 			if (options.num_ports >= MAX_PORTS) {
    867   1.1  christos 				fprintf(stderr, "too many ports.\n");
    868   1.1  christos 				exit(1);
    869   1.1  christos 			}
    870   1.1  christos 			options.ports[options.num_ports++] = a2port(optarg);
    871   1.1  christos 			if (options.ports[options.num_ports-1] <= 0) {
    872   1.1  christos 				fprintf(stderr, "Bad port number.\n");
    873   1.1  christos 				exit(1);
    874   1.1  christos 			}
    875   1.1  christos 			break;
    876   1.1  christos 		case 'g':
    877   1.1  christos 			if ((options.login_grace_time = convtime(optarg)) == -1) {
    878   1.1  christos 				fprintf(stderr, "Invalid login grace time.\n");
    879   1.1  christos 				exit(1);
    880   1.1  christos 			}
    881   1.1  christos 			break;
    882   1.1  christos 		case 'k':
    883   1.1  christos 			/* protocol 1, ignored */
    884   1.1  christos 			break;
    885   1.1  christos 		case 'h':
    886   1.1  christos 			servconf_add_hostkey("[command-line]", 0,
    887   1.1  christos 			    &options, optarg, 1);
    888   1.1  christos 			break;
    889   1.1  christos 		case 't':
    890   1.1  christos 		case 'T':
    891   1.1  christos 		case 'G':
    892   1.1  christos 			fatal("test/dump modes not supported");
    893   1.1  christos 			break;
    894   1.1  christos 		case 'C':
    895   1.1  christos 			connection_info = server_get_connection_info(ssh, 0, 0);
    896   1.1  christos 			if (parse_server_match_testspec(connection_info,
    897   1.1  christos 			    optarg) == -1)
    898   1.1  christos 				exit(1);
    899   1.1  christos 			break;
    900   1.1  christos 		case 'u':
    901   1.1  christos 			utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL);
    902   1.1  christos 			if (utmp_len > HOST_NAME_MAX+1) {
    903   1.1  christos 				fprintf(stderr, "Invalid utmp length.\n");
    904   1.1  christos 				exit(1);
    905   1.1  christos 			}
    906   1.1  christos 			break;
    907   1.1  christos 		case 'o':
    908   1.1  christos 			line = xstrdup(optarg);
    909   1.1  christos 			if (process_server_config_line(&options, line,
    910   1.1  christos 			    "command-line", 0, NULL, NULL, &includes) != 0)
    911   1.1  christos 				exit(1);
    912   1.1  christos 			free(line);
    913   1.1  christos 			break;
    914   1.1  christos 		case 'V':
    915   1.1  christos 			fprintf(stderr, "%s, %s\n",
    916   1.1  christos 			    SSH_VERSION, SSH_OPENSSL_VERSION);
    917   1.1  christos 			exit(0);
    918   1.1  christos 		default:
    919   1.1  christos 			usage();
    920   1.1  christos 			break;
    921   1.1  christos 		}
    922   1.1  christos 	}
    923   1.1  christos 
    924   1.1  christos 	/* Check that there are no remaining arguments. */
    925   1.1  christos 	if (optind < ac) {
    926   1.1  christos 		fprintf(stderr, "Extra argument %s.\n", av[optind]);
    927   1.1  christos 		exit(1);
    928   1.1  christos 	}
    929   1.1  christos 
    930   1.1  christos 	debug("sshd version %s, %s", SSH_VERSION, SSH_OPENSSL_VERSION);
    931   1.1  christos 
    932   1.1  christos 	if (!rexeced_flag)
    933   1.1  christos 		fatal("sshd-session should not be executed directly");
    934   1.1  christos 
    935   1.1  christos 	closefrom(REEXEC_MIN_FREE_FD);
    936   1.1  christos 
    937   1.9  christos 	/* Reserve fds we'll need later for reexec things */
    938   1.9  christos 	if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
    939   1.9  christos 		fatal("open %s: %s", _PATH_DEVNULL, strerror(errno));
    940   1.9  christos 	while (devnull < PRIVSEP_MIN_FREE_FD) {
    941   1.9  christos 		if ((devnull = dup(devnull)) == -1)
    942   1.9  christos 			fatal("dup %s: %s", _PATH_DEVNULL, strerror(errno));
    943   1.9  christos 	}
    944   1.9  christos 
    945   1.1  christos #ifdef WITH_OPENSSL
    946   1.1  christos 	OpenSSL_add_all_algorithms();
    947   1.1  christos #endif
    948   1.1  christos 	/* If requested, redirect the logs to the specified logfile. */
    949   1.1  christos 	if (logfile != NULL) {
    950   1.1  christos 		char *cp, pid_s[32];
    951   1.1  christos 
    952   1.1  christos 		snprintf(pid_s, sizeof(pid_s), "%ld", (unsigned long)getpid());
    953   1.1  christos 		cp = percent_expand(logfile,
    954   1.1  christos 		    "p", pid_s,
    955   1.1  christos 		    "P", "sshd-session",
    956   1.1  christos 		    (char *)NULL);
    957   1.1  christos 		log_redirect_stderr_to(cp);
    958   1.1  christos 		free(cp);
    959   1.1  christos 	}
    960   1.1  christos 
    961   1.1  christos 	/*
    962   1.1  christos 	 * Force logging to stderr until we have loaded the private host
    963   1.1  christos 	 * key (unless started from inetd)
    964   1.1  christos 	 */
    965   1.1  christos 	log_init(__progname,
    966   1.1  christos 	    options.log_level == SYSLOG_LEVEL_NOT_SET ?
    967   1.1  christos 	    SYSLOG_LEVEL_INFO : options.log_level,
    968   1.1  christos 	    options.log_facility == SYSLOG_FACILITY_NOT_SET ?
    969   1.1  christos 	    SYSLOG_FACILITY_AUTH : options.log_facility,
    970   1.1  christos 	    log_stderr || !inetd_flag || debug_flag);
    971   1.1  christos 
    972   1.1  christos 	/* Fetch our configuration */
    973   1.1  christos 	if ((cfg = sshbuf_new()) == NULL)
    974   1.1  christos 		fatal("sshbuf_new config buf failed");
    975   1.1  christos 	setproctitle("%s", "[rexeced]");
    976   1.1  christos 	recv_rexec_state(REEXEC_CONFIG_PASS_FD, cfg, &timing_secret);
    977   1.1  christos 	parse_server_config(&options, "rexec", cfg, &includes, NULL, 1);
    978   1.1  christos 	/* Fill in default values for those options not explicitly set. */
    979   1.1  christos 	fill_default_server_options(&options);
    980   1.1  christos 	options.timing_secret = timing_secret;
    981   1.1  christos 
    982   1.9  christos 	/* Reinit logging in case config set Level, Facility or Verbose. */
    983   1.9  christos 	log_init(__progname, options.log_level, options.log_facility,
    984   1.9  christos 	    log_stderr || !inetd_flag || debug_flag);
    985   1.9  christos 
    986   1.9  christos 	debug("sshd-session version %s, %s", SSH_VERSION, SSH_OPENSSL_VERSION);
    987   1.9  christos 
    988  1.10  christos #ifdef WITH_LDAP_PUBKEY
    989  1.10  christos 	/* ldap_options_print(&options.lpk); */
    990  1.10  christos 	/* XXX initialize/check ldap connection and set *LD */
    991  1.10  christos 	if (options.lpk.on) {
    992  1.10  christos 	    if (options.lpk.l_conf && (ldap_parse_lconf(&options.lpk) < 0) )
    993  1.10  christos 		error("[LDAP] could not parse %s", options.lpk.l_conf);
    994  1.10  christos 	    if (ldap_xconnect(&options.lpk) < 0)
    995  1.10  christos 		error("[LDAP] could not initialize ldap connection");
    996  1.10  christos 	}
    997  1.10  christos #endif
    998   1.9  christos 	if (!debug_flag && !inetd_flag) {
    999   1.9  christos 		if ((startup_pipe = dup(REEXEC_CONFIG_PASS_FD)) == -1)
   1000   1.9  christos 			fatal("internal error: no startup pipe");
   1001   1.9  christos 
   1002   1.1  christos 		/*
   1003   1.1  christos 		 * Signal parent that this child is at a point where
   1004   1.1  christos 		 * they can go away if they have a SIGHUP pending.
   1005   1.1  christos 		 */
   1006   1.2  christos 		(void)atomicio(vwrite, startup_pipe, __UNCONST("\0"), 1);
   1007   1.1  christos 	}
   1008   1.9  christos 	/* close the fd, but keep the slot reserved */
   1009   1.9  christos 	if (dup2(devnull, REEXEC_CONFIG_PASS_FD) == -1)
   1010   1.9  christos 		fatal("dup2 devnull->config fd: %s", strerror(errno));
   1011   1.1  christos 
   1012   1.1  christos 	/* Check that options are sensible */
   1013   1.1  christos 	if (options.authorized_keys_command_user == NULL &&
   1014   1.1  christos 	    (options.authorized_keys_command != NULL &&
   1015   1.1  christos 	    strcasecmp(options.authorized_keys_command, "none") != 0))
   1016   1.1  christos 		fatal("AuthorizedKeysCommand set without "
   1017   1.1  christos 		    "AuthorizedKeysCommandUser");
   1018   1.1  christos 	if (options.authorized_principals_command_user == NULL &&
   1019   1.1  christos 	    (options.authorized_principals_command != NULL &&
   1020   1.1  christos 	    strcasecmp(options.authorized_principals_command, "none") != 0))
   1021   1.1  christos 		fatal("AuthorizedPrincipalsCommand set without "
   1022   1.1  christos 		    "AuthorizedPrincipalsCommandUser");
   1023   1.1  christos 
   1024   1.1  christos 	/*
   1025   1.1  christos 	 * Check whether there is any path through configured auth methods.
   1026   1.1  christos 	 * Unfortunately it is not possible to verify this generally before
   1027   1.1  christos 	 * daemonisation in the presence of Match block, but this catches
   1028   1.1  christos 	 * and warns for trivial misconfigurations that could break login.
   1029   1.1  christos 	 */
   1030   1.1  christos 	if (options.num_auth_methods != 0) {
   1031   1.1  christos 		for (i = 0; i < options.num_auth_methods; i++) {
   1032   1.1  christos 			if (auth2_methods_valid(options.auth_methods[i],
   1033   1.1  christos 			    1) == 0)
   1034   1.1  christos 				break;
   1035   1.1  christos 		}
   1036   1.1  christos 		if (i >= options.num_auth_methods)
   1037   1.1  christos 			fatal("AuthenticationMethods cannot be satisfied by "
   1038   1.1  christos 			    "enabled authentication methods");
   1039   1.1  christos 	}
   1040   1.1  christos 
   1041   1.1  christos #ifdef WITH_OPENSSL
   1042   1.1  christos 	if (options.moduli_file != NULL)
   1043   1.1  christos 		dh_set_moduli_file(options.moduli_file);
   1044   1.1  christos #endif
   1045   1.1  christos 
   1046   1.1  christos 	if (options.host_key_agent) {
   1047   1.1  christos 		if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME))
   1048   1.1  christos 			setenv(SSH_AUTHSOCKET_ENV_NAME,
   1049   1.1  christos 			    options.host_key_agent, 1);
   1050   1.1  christos 		if ((r = ssh_get_authentication_socket(NULL)) == 0)
   1051   1.1  christos 			have_agent = 1;
   1052   1.1  christos 		else
   1053   1.1  christos 			error_r(r, "Could not connect to agent \"%s\"",
   1054   1.1  christos 			    options.host_key_agent);
   1055   1.1  christos 	}
   1056   1.1  christos 
   1057   1.1  christos 	if (options.num_host_key_files != sensitive_data.num_hostkeys) {
   1058   1.1  christos 		fatal("internal error: hostkeys confused (config %u recvd %u)",
   1059   1.1  christos 		    options.num_host_key_files, sensitive_data.num_hostkeys);
   1060   1.1  christos 	}
   1061   1.1  christos 
   1062   1.1  christos 	for (i = 0; i < options.num_host_key_files; i++) {
   1063   1.1  christos 		if (sensitive_data.host_keys[i] != NULL ||
   1064   1.1  christos 		    (have_agent && sensitive_data.host_pubkeys[i] != NULL)) {
   1065   1.1  christos 			have_key = 1;
   1066   1.1  christos 			break;
   1067   1.1  christos 		}
   1068   1.1  christos 	}
   1069   1.1  christos 	if (!have_key)
   1070   1.1  christos 		fatal("internal error: monitor received no hostkeys");
   1071   1.1  christos 
   1072   1.1  christos 	/* Ensure that umask disallows at least group and world write */
   1073   1.1  christos 	new_umask = umask(0077) | 0022;
   1074   1.1  christos 	(void) umask(new_umask);
   1075   1.1  christos 
   1076   1.1  christos 	/* Initialize the log (it is reinitialized below in case we forked). */
   1077   1.1  christos 	if (debug_flag)
   1078   1.1  christos 		log_stderr = 1;
   1079   1.1  christos 	log_init(__progname, options.log_level,
   1080   1.1  christos 	    options.log_facility, log_stderr);
   1081   1.1  christos 	for (i = 0; i < options.num_log_verbose; i++)
   1082   1.1  christos 		log_verbose_add(options.log_verbose[i]);
   1083   1.1  christos 
   1084   1.1  christos 	/* Reinitialize the log (because of the fork above). */
   1085   1.1  christos 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
   1086   1.1  christos 
   1087   1.1  christos 	/*
   1088   1.1  christos 	 * Chdir to the root directory so that the current disk can be
   1089   1.1  christos 	 * unmounted if desired.
   1090   1.1  christos 	 */
   1091   1.1  christos 	if (chdir("/") == -1)
   1092   1.1  christos 		error("chdir(\"/\"): %s", strerror(errno));
   1093   1.1  christos 
   1094   1.1  christos 	/* ignore SIGPIPE */
   1095   1.1  christos 	ssh_signal(SIGPIPE, SIG_IGN);
   1096   1.1  christos 
   1097   1.1  christos 	/* Get a connection, either from inetd or rexec */
   1098   1.1  christos 	if (inetd_flag) {
   1099   1.1  christos 		/*
   1100   1.1  christos 		 * NB. must be different fd numbers for the !socket case,
   1101   1.1  christos 		 * as packet_connection_is_on_socket() depends on this.
   1102   1.1  christos 		 */
   1103   1.1  christos 		sock_in = dup(STDIN_FILENO);
   1104   1.1  christos 		sock_out = dup(STDOUT_FILENO);
   1105   1.1  christos 	} else {
   1106   1.1  christos 		/* rexec case; accept()ed socket in ancestor listener */
   1107   1.1  christos 		sock_in = sock_out = dup(STDIN_FILENO);
   1108   1.1  christos 	}
   1109   1.1  christos 
   1110   1.1  christos 	/*
   1111   1.1  christos 	 * We intentionally do not close the descriptors 0, 1, and 2
   1112   1.1  christos 	 * as our code for setting the descriptors won't work if
   1113   1.1  christos 	 * ttyfd happens to be one of those.
   1114   1.1  christos 	 */
   1115   1.1  christos 	if (stdfd_devnull(1, 1, !log_stderr) == -1)
   1116   1.1  christos 		error("stdfd_devnull failed");
   1117   1.1  christos 	debug("network sockets: %d, %d", sock_in, sock_out);
   1118   1.1  christos 
   1119   1.1  christos 	/* This is the child processing a new connection. */
   1120   1.1  christos 	setproctitle("%s", "[accepted]");
   1121   1.1  christos 
   1122   1.1  christos 	/* Executed child processes don't need these. */
   1123  1.12  christos 	FD_CLOSEONEXEC(sock_out);
   1124  1.12  christos 	FD_CLOSEONEXEC(sock_in);
   1125   1.1  christos 
   1126   1.1  christos 	/* We will not restart on SIGHUP since it no longer makes sense. */
   1127   1.1  christos 	ssh_signal(SIGALRM, SIG_DFL);
   1128   1.1  christos 	ssh_signal(SIGHUP, SIG_DFL);
   1129   1.1  christos 	ssh_signal(SIGTERM, SIG_DFL);
   1130   1.1  christos 	ssh_signal(SIGQUIT, SIG_DFL);
   1131   1.1  christos 	ssh_signal(SIGCHLD, SIG_DFL);
   1132   1.1  christos 
   1133   1.2  christos 	pfilter_init();
   1134   1.2  christos 
   1135   1.1  christos 	/*
   1136   1.1  christos 	 * Register our connection.  This turns encryption off because we do
   1137   1.1  christos 	 * not have a key.
   1138   1.1  christos 	 */
   1139   1.1  christos 	if ((ssh = ssh_packet_set_connection(NULL, sock_in, sock_out)) == NULL)
   1140   1.1  christos 		fatal("Unable to create connection");
   1141   1.1  christos 	the_active_state = ssh;
   1142   1.1  christos 	ssh_packet_set_server(ssh);
   1143  1.11  christos 	ssh_packet_set_qos(ssh, options.ip_qos_interactive,
   1144  1.11  christos 	    options.ip_qos_bulk);
   1145   1.1  christos 
   1146   1.1  christos 	check_ip_options(ssh);
   1147   1.1  christos 
   1148   1.1  christos 	/* Prepare the channels layer */
   1149   1.1  christos 	channel_init_channels(ssh);
   1150   1.1  christos 	channel_set_af(ssh, options.address_family);
   1151   1.1  christos 	server_process_channel_timeouts(ssh);
   1152   1.1  christos 	server_process_permitopen(ssh);
   1153   1.1  christos 
   1154   1.1  christos 	/* Set SO_KEEPALIVE if requested. */
   1155   1.1  christos 	if (options.tcp_keep_alive && ssh_packet_connection_is_on_socket(ssh) &&
   1156   1.1  christos 	    setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) == -1)
   1157   1.1  christos 		error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
   1158   1.1  christos 
   1159   1.1  christos 	if ((remote_port = ssh_remote_port(ssh)) < 0) {
   1160   1.1  christos 		debug("ssh_remote_port failed");
   1161   1.1  christos 		cleanup_exit(255);
   1162   1.1  christos 	}
   1163   1.1  christos 
   1164   1.1  christos 	/*
   1165   1.1  christos 	 * The rest of the code depends on the fact that
   1166   1.1  christos 	 * ssh_remote_ipaddr() caches the remote ip, even if
   1167   1.1  christos 	 * the socket goes away.
   1168   1.1  christos 	 */
   1169   1.1  christos 	remote_ip = ssh_remote_ipaddr(ssh);
   1170   1.1  christos 
   1171   1.5    buhrow #ifdef LIBWRAP
   1172   1.5    buhrow 	/* Check whether logins are denied from this host. */
   1173   1.5    buhrow 	if (ssh_packet_connection_is_on_socket(ssh)) {
   1174   1.5    buhrow 		/* First, try with the value stored in __progname */
   1175   1.6  christos 		check_connection(__progname, sock_in);
   1176   1.5    buhrow 		/*
   1177   1.5    buhrow 		 * Test with "sshd" as well, since that is what most people
   1178   1.5    buhrow 		 * will have in their hosts.allow and hosts.deny files.
   1179   1.5    buhrow 		 */
   1180   1.6  christos 		check_connection("sshd", sock_in);
   1181   1.5    buhrow 	}
   1182   1.5    buhrow #endif /* LIBWRAP */
   1183   1.5    buhrow 
   1184   1.1  christos 	rdomain = ssh_packet_rdomain_in(ssh);
   1185   1.1  christos 
   1186   1.1  christos 	/* Log the connection. */
   1187   1.1  christos 	laddr = get_local_ipaddr(sock_in);
   1188   1.1  christos 	verbose("Connection from %s port %d on %s port %d%s%s%s",
   1189   1.1  christos 	    remote_ip, remote_port, laddr,  ssh_local_port(ssh),
   1190   1.1  christos 	    rdomain == NULL ? "" : " rdomain \"",
   1191   1.1  christos 	    rdomain == NULL ? "" : rdomain,
   1192   1.1  christos 	    rdomain == NULL ? "" : "\"");
   1193   1.1  christos 	free(laddr);
   1194   1.1  christos 
   1195   1.1  christos 	/*
   1196   1.1  christos 	 * We don't want to listen forever unless the other side
   1197   1.1  christos 	 * successfully authenticates itself.  So we set up an alarm which is
   1198   1.1  christos 	 * cleared after successful authentication.  A limit of zero
   1199   1.1  christos 	 * indicates no limit. Note that we don't set the alarm in debugging
   1200   1.1  christos 	 * mode; it is just annoying to have the server exit just when you
   1201   1.1  christos 	 * are about to discover the bug.
   1202   1.1  christos 	 */
   1203   1.1  christos 	ssh_signal(SIGALRM, grace_alarm_handler);
   1204   1.4  christos 	if (!debug_flag && options.login_grace_time > 0) {
   1205   1.4  christos 		int ujitter = arc4random_uniform(4 * 1000000);
   1206   1.4  christos 
   1207   1.4  christos 		timerclear(&itv.it_interval);
   1208   1.4  christos 		itv.it_value.tv_sec = options.login_grace_time;
   1209   1.4  christos 		itv.it_value.tv_sec += ujitter / 1000000;
   1210   1.4  christos 		itv.it_value.tv_usec = ujitter % 1000000;
   1211   1.4  christos 
   1212   1.4  christos 		if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
   1213   1.4  christos 			fatal("login grace time setitimer failed");
   1214   1.4  christos 	}
   1215   1.1  christos 
   1216   1.1  christos 	ssh_packet_set_nonblocking(ssh);
   1217   1.1  christos 
   1218   1.1  christos 	/* allocate authentication context */
   1219   1.1  christos 	authctxt = xcalloc(1, sizeof(*authctxt));
   1220   1.1  christos 	ssh->authctxt = authctxt;
   1221   1.1  christos 
   1222   1.1  christos 	/* XXX global for cleanup, access from other modules */
   1223   1.1  christos 	the_authctxt = authctxt;
   1224   1.1  christos 
   1225   1.1  christos 	/* Set default key authentication options */
   1226   1.1  christos 	if ((auth_opts = sshauthopt_new_with_keys_defaults()) == NULL)
   1227   1.1  christos 		fatal("allocation failed");
   1228   1.1  christos 
   1229   1.1  christos 	/* prepare buffer to collect messages to display to user after login */
   1230   1.1  christos 	if ((loginmsg = sshbuf_new()) == NULL)
   1231   1.1  christos 		fatal("sshbuf_new loginmsg failed");
   1232   1.1  christos 	auth_debug_reset();
   1233   1.1  christos 
   1234   1.9  christos 	if (privsep_preauth(ssh) != 1)
   1235   1.9  christos 		fatal("privsep_preauth failed");
   1236   1.1  christos 
   1237   1.9  christos 	/* Now user is authenticated */
   1238   1.1  christos 
   1239   1.1  christos 	/*
   1240   1.1  christos 	 * Cancel the alarm we set to limit the time taken for
   1241   1.1  christos 	 * authentication.
   1242   1.1  christos 	 */
   1243   1.4  christos 	timerclear(&itv.it_interval);
   1244   1.4  christos 	timerclear(&itv.it_value);
   1245   1.4  christos 	if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
   1246   1.4  christos 		fatal("login grace time clear failed");
   1247   1.1  christos 	ssh_signal(SIGALRM, SIG_DFL);
   1248   1.1  christos 	authctxt->authenticated = 1;
   1249   1.1  christos 	if (startup_pipe != -1) {
   1250   1.1  christos 		/* signal listener that authentication completed successfully */
   1251   1.2  christos 		(void)atomicio(vwrite, startup_pipe, __UNCONST("\001"), 1);
   1252   1.1  christos 		close(startup_pipe);
   1253   1.1  christos 		startup_pipe = -1;
   1254   1.1  christos 	}
   1255   1.1  christos 
   1256   1.2  christos #ifdef __OpenBSD__
   1257   1.1  christos 	if (options.routing_domain != NULL)
   1258   1.1  christos 		set_process_rdomain(ssh, options.routing_domain);
   1259   1.2  christos #endif
   1260   1.2  christos 
   1261   1.2  christos #ifdef GSSAPI
   1262   1.2  christos 	if (options.gss_authentication) {
   1263   1.2  christos 		temporarily_use_uid(authctxt->pw);
   1264   1.2  christos 		ssh_gssapi_storecreds();
   1265   1.2  christos 		restore_uid();
   1266   1.2  christos 	}
   1267   1.2  christos #endif
   1268   1.2  christos #ifdef USE_PAM
   1269   1.2  christos 	if (options.use_pam) {
   1270   1.2  christos 		do_pam_setcred();
   1271   1.2  christos 		do_pam_session(ssh);
   1272   1.2  christos 	}
   1273   1.2  christos #endif
   1274   1.1  christos 
   1275   1.1  christos 	/*
   1276   1.1  christos 	 * In privilege separation, we fork another child and prepare
   1277   1.1  christos 	 * file descriptor passing.
   1278   1.1  christos 	 */
   1279   1.1  christos 	privsep_postauth(ssh, authctxt);
   1280   1.1  christos 	/* the monitor process [priv] will not return */
   1281   1.1  christos 
   1282   1.1  christos 	ssh_packet_set_timeout(ssh, options.client_alive_interval,
   1283   1.1  christos 	    options.client_alive_count_max);
   1284   1.1  christos 
   1285   1.1  christos 	/* Try to send all our hostkeys to the client */
   1286   1.1  christos 	notify_hostkeys(ssh);
   1287   1.1  christos 
   1288   1.1  christos 	/* Start session. */
   1289   1.1  christos 	do_authenticated(ssh, authctxt);
   1290   1.1  christos 
   1291   1.1  christos 	/* The connection has been terminated. */
   1292   1.1  christos 	ssh_packet_get_bytes(ssh, &ibytes, &obytes);
   1293   1.1  christos 	verbose("Transferred: sent %llu, received %llu bytes",
   1294   1.1  christos 	    (unsigned long long)obytes, (unsigned long long)ibytes);
   1295   1.1  christos 
   1296   1.2  christos #ifdef USE_PAM
   1297   1.2  christos 	if (options.use_pam)
   1298   1.2  christos 		finish_pam();
   1299   1.2  christos #endif /* USE_PAM */
   1300   1.2  christos 
   1301   1.1  christos 	verbose("Closing connection to %.500s port %d", remote_ip, remote_port);
   1302   1.1  christos 	ssh_packet_close(ssh);
   1303   1.1  christos 
   1304   1.1  christos 	mm_terminate();
   1305   1.1  christos 
   1306   1.1  christos 	exit(0);
   1307   1.1  christos }
   1308   1.1  christos 
   1309   1.1  christos int
   1310   1.1  christos sshd_hostkey_sign(struct ssh *ssh, struct sshkey *privkey,
   1311   1.1  christos     struct sshkey *pubkey, u_char **signature, size_t *slenp,
   1312   1.1  christos     const u_char *data, size_t dlen, const char *alg)
   1313   1.1  christos {
   1314   1.1  christos 	if (privkey) {
   1315   1.1  christos 		if (mm_sshkey_sign(ssh, privkey, signature, slenp,
   1316   1.1  christos 		    data, dlen, alg, options.sk_provider, NULL,
   1317   1.1  christos 		    ssh->compat) < 0)
   1318   1.1  christos 			fatal_f("privkey sign failed");
   1319   1.1  christos 	} else {
   1320   1.1  christos 		if (mm_sshkey_sign(ssh, pubkey, signature, slenp,
   1321   1.1  christos 		    data, dlen, alg, options.sk_provider, NULL,
   1322   1.1  christos 		    ssh->compat) < 0)
   1323   1.1  christos 			fatal_f("pubkey sign failed");
   1324   1.1  christos 	}
   1325   1.1  christos 	return 0;
   1326   1.1  christos }
   1327   1.1  christos 
   1328   1.1  christos /* server specific fatal cleanup */
   1329   1.1  christos void
   1330   1.1  christos cleanup_exit(int i)
   1331   1.1  christos {
   1332   1.1  christos 	if (the_active_state != NULL && the_authctxt != NULL) {
   1333   1.1  christos 		do_cleanup(the_active_state, the_authctxt);
   1334   1.1  christos 		if (privsep_is_preauth &&
   1335   1.1  christos 		    pmonitor != NULL && pmonitor->m_pid > 1) {
   1336   1.1  christos 			debug("Killing privsep child %d", pmonitor->m_pid);
   1337   1.1  christos 			if (kill(pmonitor->m_pid, SIGKILL) != 0 &&
   1338   1.1  christos 			    errno != ESRCH) {
   1339   1.1  christos 				error_f("kill(%d): %s", pmonitor->m_pid,
   1340   1.1  christos 				    strerror(errno));
   1341   1.1  christos 			}
   1342   1.1  christos 		}
   1343   1.1  christos 	}
   1344   1.1  christos 	/* Override default fatal exit value when auth was attempted */
   1345  1.13  christos 	if (i == 255 && monitor_auth_attempted()) {
   1346  1.13  christos 		pfilter_notify(1);
   1347   1.1  christos 		_exit(EXIT_AUTH_ATTEMPTED);
   1348  1.13  christos 	}
   1349  1.12  christos 	if (i == 255 && monitor_invalid_user())
   1350  1.12  christos 		_exit(EXIT_INVALID_USER);
   1351   1.1  christos 	_exit(i);
   1352   1.1  christos }
   1353