[BACK]Return to sshd.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sshd.c, Revision 1.477

1.477   ! dtucker     1: /* $OpenBSD: sshd.c,v 1.476 2016/09/28 16:33:07 djm Exp $ */
1.86      markus      2: /*
1.65      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
1.126     deraadt     6:  * This program is the ssh daemon.  It listens for connections from clients,
                      7:  * and performs authentication, executes use commands or shell, and forwards
1.65      deraadt     8:  * information to/from the application to the user client over an encrypted
1.126     deraadt     9:  * connection.  This can also handle forwarding of X11, TCP/IP, and
                     10:  * authentication agent connections.
1.98      markus     11:  *
1.126     deraadt    12:  * As far as I am concerned, the code I have written for this software
                     13:  * can be used freely for any purpose.  Any derived versions of this
                     14:  * software must be clearly marked as such, and if the derived work is
                     15:  * incompatible with the protocol description in the RFC file, it must be
                     16:  * called by a name other than "ssh" or "Secure Shell".
                     17:  *
                     18:  * SSH2 implementation:
1.231     provos     19:  * Privilege Separation:
1.126     deraadt    20:  *
1.231     provos     21:  * Copyright (c) 2000, 2001, 2002 Markus Friedl.  All rights reserved.
                     22:  * Copyright (c) 2002 Niels Provos.  All rights reserved.
1.126     deraadt    23:  *
                     24:  * Redistribution and use in source and binary forms, with or without
                     25:  * modification, are permitted provided that the following conditions
                     26:  * are met:
                     27:  * 1. Redistributions of source code must retain the above copyright
                     28:  *    notice, this list of conditions and the following disclaimer.
                     29:  * 2. Redistributions in binary form must reproduce the above copyright
                     30:  *    notice, this list of conditions and the following disclaimer in the
                     31:  *    documentation and/or other materials provided with the distribution.
                     32:  *
                     33:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     34:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     35:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     36:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     37:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     38:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     39:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     40:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     41:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     42:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.65      deraadt    43:  */
1.1       deraadt    44:
1.343     deraadt    45: #include <sys/types.h>
1.320     stevesk    46: #include <sys/ioctl.h>
1.321     stevesk    47: #include <sys/wait.h>
1.343     deraadt    48: #include <sys/tree.h>
1.323     stevesk    49: #include <sys/stat.h>
1.334     stevesk    50: #include <sys/socket.h>
1.340     stevesk    51: #include <sys/time.h>
1.357     djm        52: #include <sys/queue.h>
1.319     stevesk    53:
1.336     stevesk    54: #include <errno.h>
1.335     stevesk    55: #include <fcntl.h>
1.338     stevesk    56: #include <netdb.h>
1.319     stevesk    57: #include <paths.h>
1.333     stevesk    58: #include <pwd.h>
1.322     stevesk    59: #include <signal.h>
1.342     stevesk    60: #include <stdio.h>
1.341     stevesk    61: #include <stdlib.h>
1.339     stevesk    62: #include <string.h>
1.344     dtucker    63: #include <unistd.h>
1.438     deraadt    64: #include <limits.h>
1.1       deraadt    65:
1.426     markus     66: #ifdef WITH_OPENSSL
1.155     markus     67: #include <openssl/bn.h>
1.426     markus     68: #endif
1.155     markus     69:
1.343     deraadt    70: #include "xmalloc.h"
1.155     markus     71: #include "ssh.h"
                     72: #include "ssh2.h"
1.1       deraadt    73: #include "rsa.h"
1.171     djm        74: #include "sshpty.h"
1.1       deraadt    75: #include "packet.h"
1.155     markus     76: #include "log.h"
1.343     deraadt    77: #include "buffer.h"
1.428     millert    78: #include "misc.h"
1.454     markus     79: #include "match.h"
1.1       deraadt    80: #include "servconf.h"
                     81: #include "uidswap.h"
1.33      markus     82: #include "compat.h"
1.155     markus     83: #include "cipher.h"
1.415     markus     84: #include "digest.h"
1.343     deraadt    85: #include "key.h"
1.98      markus     86: #include "kex.h"
                     87: #include "myproposal.h"
1.108     markus     88: #include "authfile.h"
1.154     markus     89: #include "pathnames.h"
1.155     markus     90: #include "atomicio.h"
                     91: #include "canohost.h"
1.343     deraadt    92: #include "hostfile.h"
1.155     markus     93: #include "auth.h"
1.404     markus     94: #include "authfd.h"
1.294     djm        95: #include "msg.h"
1.186     markus     96: #include "dispatch.h"
1.206     stevesk    97: #include "channels.h"
1.230     provos     98: #include "session.h"
1.231     provos     99: #include "monitor.h"
1.343     deraadt   100: #ifdef GSSAPI
                    101: #include "ssh-gss.h"
                    102: #endif
1.231     provos    103: #include "monitor_wrap.h"
1.385     djm       104: #include "ssh-sandbox.h"
1.332     stevesk   105: #include "version.h"
1.432     djm       106: #include "ssherr.h"
1.1       deraadt   107:
1.296     djm       108: /* Re-exec fds */
                    109: #define REEXEC_DEVCRYPTO_RESERVED_FD   (STDERR_FILENO + 1)
                    110: #define REEXEC_STARTUP_PIPE_FD         (STDERR_FILENO + 2)
                    111: #define REEXEC_CONFIG_PASS_FD          (STDERR_FILENO + 3)
                    112: #define REEXEC_MIN_FREE_FD             (STDERR_FILENO + 4)
                    113:
1.138     markus    114: extern char *__progname;
                    115:
1.1       deraadt   116: /* Server configuration options. */
                    117: ServerOptions options;
                    118:
                    119: /* Name of the server configuration file. */
1.154     markus    120: char *config_file_name = _PATH_SERVER_CONFIG_FILE;
1.1       deraadt   121:
1.105     markus    122: /*
1.65      deraadt   123:  * Debug mode flag.  This can be set on the command line.  If debug
                    124:  * mode is enabled, extra debugging output will be sent to the system
                    125:  * log, the daemon will not go to background, and will exit after processing
                    126:  * the first connection.
                    127:  */
1.1       deraadt   128: int debug_flag = 0;
                    129:
1.203     stevesk   130: /* Flag indicating that the daemon should only test the configuration and keys. */
                    131: int test_flag = 0;
                    132:
1.1       deraadt   133: /* Flag indicating that the daemon is being started from inetd. */
                    134: int inetd_flag = 0;
                    135:
1.135     markus    136: /* Flag indicating that sshd should not detach and become a daemon. */
                    137: int no_daemon_flag = 0;
                    138:
1.47      markus    139: /* debug goes to stderr unless inetd_flag is set */
                    140: int log_stderr = 0;
                    141:
1.1       deraadt   142: /* Saved arguments to main(). */
                    143: char **saved_argv;
                    144:
1.294     djm       145: /* re-exec */
                    146: int rexeced_flag = 0;
                    147: int rexec_flag = 1;
                    148: int rexec_argc = 0;
                    149: char **rexec_argv;
                    150:
1.66      markus    151: /*
1.75      markus    152:  * The sockets that the server is listening; this is used in the SIGHUP
                    153:  * signal handler.
1.66      markus    154:  */
1.75      markus    155: #define        MAX_LISTEN_SOCKS        16
                    156: int listen_socks[MAX_LISTEN_SOCKS];
                    157: int num_listen_socks = 0;
1.1       deraadt   158:
1.66      markus    159: /*
                    160:  * the client's version string, passed by sshd2 in compat mode. if != NULL,
                    161:  * sshd will skip the version-number exchange
                    162:  */
1.61      markus    163: char *client_version_string = NULL;
1.96      markus    164: char *server_version_string = NULL;
1.1       deraadt   165:
1.404     markus    166: /* Daemon's agent connection */
1.432     djm       167: int auth_sock = -1;
1.404     markus    168: int have_agent = 0;
                    169:
1.66      markus    170: /*
                    171:  * Any really sensitive data in the application is contained in this
                    172:  * structure. The idea is that this structure could be locked into memory so
                    173:  * that the pages do not get written into swap.  However, there are some
                    174:  * problems. The private key contains BIGNUMs, and we do not (in principle)
                    175:  * have access to the internals of them, and locking just the structure is
                    176:  * not very useful.  Currently, memory locking is not implemented.
                    177:  */
1.64      markus    178: struct {
1.134     markus    179:        Key     **host_keys;            /* all private host keys */
1.404     markus    180:        Key     **host_pubkeys;         /* all public host keys */
1.373     djm       181:        Key     **host_certificates;    /* all public host certificates */
1.134     markus    182:        int     have_ssh2_key;
1.1       deraadt   183: } sensitive_data;
                    184:
1.199     markus    185: /* This is set to true when a signal is received. */
1.212     markus    186: static volatile sig_atomic_t received_sighup = 0;
                    187: static volatile sig_atomic_t received_sigterm = 0;
1.1       deraadt   188:
1.96      markus    189: /* session identifier, used by RSA-auth */
1.140     markus    190: u_char session_id[16];
1.96      markus    191:
1.108     markus    192: /* same for ssh2 */
1.140     markus    193: u_char *session_id2 = NULL;
1.269     markus    194: u_int session_id2_len = 0;
1.108     markus    195:
1.125     markus    196: /* record remote hostname or ip */
1.438     deraadt   197: u_int utmp_len = HOST_NAME_MAX+1;
1.125     markus    198:
1.211     markus    199: /* options.max_startup sized array of fd ints */
                    200: int *startup_pipes = NULL;
                    201: int startup_pipe;              /* in child */
                    202:
1.231     provos    203: /* variables used for privilege separation */
1.337     dtucker   204: int use_privsep = -1;
1.285     dtucker   205: struct monitor *pmonitor = NULL;
1.386     djm       206: int privsep_is_preauth = 1;
1.231     provos    207:
1.278     markus    208: /* global authentication context */
                    209: Authctxt *the_authctxt = NULL;
                    210:
1.337     dtucker   211: /* sshd_config buffer */
                    212: Buffer cfg;
                    213:
1.299     dtucker   214: /* message to be displayed after login */
                    215: Buffer loginmsg;
                    216:
1.1       deraadt   217: /* Prototypes for various functions defined later in this file. */
1.200     itojun    218: void destroy_sensitive_data(void);
1.231     provos    219: void demote_sensitive_data(void);
1.200     itojun    220: static void do_ssh2_kex(void);
1.129     provos    221:
1.87      markus    222: /*
1.75      markus    223:  * Close all listening sockets
                    224:  */
1.200     itojun    225: static void
1.75      markus    226: close_listen_socks(void)
                    227: {
                    228:        int i;
1.250     deraadt   229:
1.75      markus    230:        for (i = 0; i < num_listen_socks; i++)
                    231:                close(listen_socks[i]);
                    232:        num_listen_socks = -1;
                    233: }
                    234:
1.211     markus    235: static void
                    236: close_startup_pipes(void)
                    237: {
                    238:        int i;
1.250     deraadt   239:
1.211     markus    240:        if (startup_pipes)
                    241:                for (i = 0; i < options.max_startups; i++)
                    242:                        if (startup_pipes[i] != -1)
                    243:                                close(startup_pipes[i]);
                    244: }
                    245:
1.75      markus    246: /*
1.65      deraadt   247:  * Signal handler for SIGHUP.  Sshd execs itself when it receives SIGHUP;
                    248:  * the effect is to reread the configuration file (and to regenerate
                    249:  * the server key).
                    250:  */
1.327     deraadt   251:
                    252: /*ARGSUSED*/
1.200     itojun    253: static void
1.64      markus    254: sighup_handler(int sig)
1.1       deraadt   255: {
1.210     deraadt   256:        int save_errno = errno;
                    257:
1.64      markus    258:        received_sighup = 1;
                    259:        signal(SIGHUP, sighup_handler);
1.210     deraadt   260:        errno = save_errno;
1.1       deraadt   261: }
                    262:
1.65      deraadt   263: /*
                    264:  * Called from the main program after receiving SIGHUP.
                    265:  * Restarts the server.
                    266:  */
1.200     itojun    267: static void
1.165     itojun    268: sighup_restart(void)
1.1       deraadt   269: {
1.264     itojun    270:        logit("Received SIGHUP; restarting.");
1.75      markus    271:        close_listen_socks();
1.211     markus    272:        close_startup_pipes();
1.349     dtucker   273:        alarm(0);  /* alarm timer persists across exec */
1.369     dtucker   274:        signal(SIGHUP, SIG_IGN); /* will be restored after exec */
1.64      markus    275:        execv(saved_argv[0], saved_argv);
1.264     itojun    276:        logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
1.250     deraadt   277:            strerror(errno));
1.64      markus    278:        exit(1);
1.1       deraadt   279: }
                    280:
1.65      deraadt   281: /*
                    282:  * Generic signal handler for terminating signals in the master daemon.
                    283:  */
1.327     deraadt   284: /*ARGSUSED*/
1.200     itojun    285: static void
1.64      markus    286: sigterm_handler(int sig)
1.1       deraadt   287: {
1.199     markus    288:        received_sigterm = sig;
1.1       deraadt   289: }
                    290:
1.65      deraadt   291: /*
                    292:  * SIGCHLD handler.  This is called whenever a child dies.  This will then
1.199     markus    293:  * reap any zombies left by exited children.
1.65      deraadt   294:  */
1.327     deraadt   295: /*ARGSUSED*/
1.200     itojun    296: static void
1.64      markus    297: main_sigchld_handler(int sig)
1.1       deraadt   298: {
1.250     deraadt   299:        int save_errno = errno;
1.239     markus    300:        pid_t pid;
1.64      markus    301:        int status;
1.60      deraadt   302:
1.239     markus    303:        while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
                    304:            (pid < 0 && errno == EINTR))
1.64      markus    305:                ;
1.60      deraadt   306:
1.64      markus    307:        signal(SIGCHLD, main_sigchld_handler);
                    308:        errno = save_errno;
1.1       deraadt   309: }
                    310:
1.65      deraadt   311: /*
                    312:  * Signal handler for the alarm after the login grace period has expired.
                    313:  */
1.327     deraadt   314: /*ARGSUSED*/
1.200     itojun    315: static void
1.64      markus    316: grace_alarm_handler(int sig)
1.1       deraadt   317: {
1.285     dtucker   318:        if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
                    319:                kill(pmonitor->m_pid, SIGALRM);
1.394     djm       320:
                    321:        /*
                    322:         * Try to kill any processes that we have spawned, E.g. authorized
                    323:         * keys command helpers.
                    324:         */
                    325:        if (getpgid(0) == getpid()) {
                    326:                signal(SIGTERM, SIG_IGN);
1.416     djm       327:                kill(0, SIGTERM);
1.394     djm       328:        }
1.285     dtucker   329:
1.64      markus    330:        /* Log error and exit. */
1.466     djm       331:        sigdie("Timeout before authentication for %s port %d",
                    332:            ssh_remote_ipaddr(active_state), ssh_remote_port(active_state));
1.62      markus    333: }
                    334:
1.200     itojun    335: static void
1.466     djm       336: sshd_exchange_identification(struct ssh *ssh, int sock_in, int sock_out)
1.96      markus    337: {
1.311     djm       338:        u_int i;
1.96      markus    339:        int remote_major, remote_minor;
1.363     dtucker   340:        char *s, *newline = "\n";
1.96      markus    341:        char buf[256];                  /* Must not be larger than remote_version. */
                    342:        char remote_version[256];       /* Must be at least as big as buf. */
                    343:
1.390     djm       344:        xasprintf(&server_version_string, "SSH-%d.%d-%.100s%s%s%s",
1.473     naddy     345:            PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION,
1.390     djm       346:            *options.version_addendum == '\0' ? "" : " ",
                    347:            options.version_addendum, newline);
1.96      markus    348:
1.272     markus    349:        /* Send our protocol version identification. */
1.463     markus    350:        if (atomicio(vwrite, sock_out, server_version_string,
1.272     markus    351:            strlen(server_version_string))
                    352:            != strlen(server_version_string)) {
1.466     djm       353:                logit("Could not write ident string to %s port %d",
                    354:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.278     markus    355:                cleanup_exit(255);
1.272     markus    356:        }
                    357:
                    358:        /* Read other sides version identification. */
                    359:        memset(buf, 0, sizeof(buf));
                    360:        for (i = 0; i < sizeof(buf) - 1; i++) {
1.463     markus    361:                if (atomicio(read, sock_in, &buf[i], 1) != 1) {
1.466     djm       362:                        logit("Did not receive identification string "
                    363:                            "from %s port %d",
                    364:                            ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.278     markus    365:                        cleanup_exit(255);
1.96      markus    366:                }
1.272     markus    367:                if (buf[i] == '\r') {
                    368:                        buf[i] = 0;
                    369:                        /* Kludge for F-Secure Macintosh < 1.0.2 */
                    370:                        if (i == 12 &&
                    371:                            strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
1.96      markus    372:                                break;
1.272     markus    373:                        continue;
                    374:                }
                    375:                if (buf[i] == '\n') {
                    376:                        buf[i] = 0;
                    377:                        break;
1.96      markus    378:                }
                    379:        }
1.272     markus    380:        buf[sizeof(buf) - 1] = 0;
                    381:        client_version_string = xstrdup(buf);
1.96      markus    382:
                    383:        /*
                    384:         * Check that the versions match.  In future this might accept
                    385:         * several versions and set appropriate flags to handle them.
                    386:         */
                    387:        if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
                    388:            &remote_major, &remote_minor, remote_version) != 3) {
1.105     markus    389:                s = "Protocol mismatch.\n";
1.271     deraadt   390:                (void) atomicio(vwrite, sock_out, s, strlen(s));
1.408     djm       391:                logit("Bad protocol version identification '%.100s' "
                    392:                    "from %s port %d", client_version_string,
1.466     djm       393:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.411     djm       394:                close(sock_in);
                    395:                close(sock_out);
1.278     markus    396:                cleanup_exit(255);
1.96      markus    397:        }
                    398:        debug("Client protocol version %d.%d; client software version %.100s",
1.217     deraadt   399:            remote_major, remote_minor, remote_version);
1.96      markus    400:
1.466     djm       401:        ssh->compat = compat_datafellows(remote_version);
1.260     mickey    402:
1.466     djm       403:        if ((ssh->compat & SSH_BUG_PROBE) != 0) {
                    404:                logit("probed from %s port %d with %s.  Don't panic.",
                    405:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
                    406:                    client_version_string);
1.278     markus    407:                cleanup_exit(255);
1.260     mickey    408:        }
1.466     djm       409:        if ((ssh->compat & SSH_BUG_SCANNER) != 0) {
                    410:                logit("scanned from %s port %d with %s.  Don't panic.",
                    411:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
                    412:                    client_version_string);
1.278     markus    413:                cleanup_exit(255);
1.175     deraadt   414:        }
1.466     djm       415:        if ((ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
1.413     djm       416:                logit("Client version \"%.100s\" uses unsafe RSA signature "
                    417:                    "scheme; disabling use of RSA keys", remote_version);
1.414     djm       418:        }
1.466     djm       419:        if ((ssh->compat & SSH_BUG_DERIVEKEY) != 0) {
1.414     djm       420:                fatal("Client version \"%.100s\" uses unsafe key agreement; "
                    421:                    "refusing connection", remote_version);
                    422:        }
1.98      markus    423:
1.102     markus    424:        chop(server_version_string);
                    425:        debug("Local version string %.200s", server_version_string);
                    426:
1.472     markus    427:        if (remote_major == 2 ||
                    428:            (remote_major == 1 && remote_minor == 99)) {
                    429:                enable_compat20();
                    430:        } else {
1.96      markus    431:                s = "Protocol major versions differ.\n";
1.271     deraadt   432:                (void) atomicio(vwrite, sock_out, s, strlen(s));
1.96      markus    433:                close(sock_in);
                    434:                close(sock_out);
1.466     djm       435:                logit("Protocol major versions differ for %s port %d: "
                    436:                    "%.200s vs. %.200s",
                    437:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1.102     markus    438:                    server_version_string, client_version_string);
1.278     markus    439:                cleanup_exit(255);
1.96      markus    440:        }
1.108     markus    441: }
                    442:
1.134     markus    443: /* Destroy the host and server keys.  They will no longer be needed. */
1.108     markus    444: void
                    445: destroy_sensitive_data(void)
                    446: {
1.134     markus    447:        int i;
                    448:
1.217     deraadt   449:        for (i = 0; i < options.num_host_key_files; i++) {
1.134     markus    450:                if (sensitive_data.host_keys[i]) {
                    451:                        key_free(sensitive_data.host_keys[i]);
                    452:                        sensitive_data.host_keys[i] = NULL;
                    453:                }
1.373     djm       454:                if (sensitive_data.host_certificates[i]) {
                    455:                        key_free(sensitive_data.host_certificates[i]);
                    456:                        sensitive_data.host_certificates[i] = NULL;
                    457:                }
1.134     markus    458:        }
                    459: }
                    460:
1.231     provos    461: /* Demote private to public keys for network child */
                    462: void
                    463: demote_sensitive_data(void)
                    464: {
                    465:        Key *tmp;
                    466:        int i;
                    467:
                    468:        for (i = 0; i < options.num_host_key_files; i++) {
                    469:                if (sensitive_data.host_keys[i]) {
                    470:                        tmp = key_demote(sensitive_data.host_keys[i]);
                    471:                        key_free(sensitive_data.host_keys[i]);
                    472:                        sensitive_data.host_keys[i] = tmp;
                    473:                }
1.373     djm       474:                /* Certs do not need demotion */
1.231     provos    475:        }
                    476: }
                    477:
1.233     markus    478: static void
1.231     provos    479: privsep_preauth_child(void)
                    480: {
1.253     deraadt   481:        gid_t gidset[1];
1.250     deraadt   482:        struct passwd *pw;
1.231     provos    483:
                    484:        /* Enable challenge-response authentication for privilege separation */
                    485:        privsep_challenge_enable();
1.419     djm       486:
1.420     markus    487: #ifdef GSSAPI
1.419     djm       488:        /* Cache supported mechanism OIDs for later use */
                    489:        if (options.gss_authentication)
                    490:                ssh_gssapi_prepare_supported_oids();
1.420     markus    491: #endif
1.231     provos    492:
                    493:        /* Demote the private keys to public keys. */
                    494:        demote_sensitive_data();
                    495:
1.460     djm       496:        /* Demote the child */
                    497:        if (getuid() == 0 || geteuid() == 0) {
                    498:                if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
                    499:                        fatal("Privilege separation user %s does not exist",
                    500:                            SSH_PRIVSEP_USER);
                    501:                explicit_bzero(pw->pw_passwd, strlen(pw->pw_passwd));
                    502:                endpwent();
                    503:
                    504:                /* Change our root directory */
                    505:                if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
                    506:                        fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
                    507:                            strerror(errno));
                    508:                if (chdir("/") == -1)
                    509:                        fatal("chdir(\"/\"): %s", strerror(errno));
                    510:
                    511:                /*
                    512:                 * Drop our privileges
                    513:                 * NB. Can't use setusercontext() after chroot.
                    514:                 */
                    515:                debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
                    516:                    (u_int)pw->pw_gid);
                    517:                gidset[0] = pw->pw_gid;
                    518:                if (setgroups(1, gidset) < 0)
                    519:                        fatal("setgroups: %.100s", strerror(errno));
                    520:                permanently_set_uid(pw);
                    521:        }
1.231     provos    522: }
                    523:
1.278     markus    524: static int
                    525: privsep_preauth(Authctxt *authctxt)
1.237     markus    526: {
1.432     djm       527:        int status, r;
1.237     markus    528:        pid_t pid;
1.384     djm       529:        struct ssh_sandbox *box = NULL;
1.237     markus    530:
                    531:        /* Set up unprivileged child process to deal with network data */
1.242     mouring   532:        pmonitor = monitor_init();
1.237     markus    533:        /* Store a pointer to the kex for later rekeying */
1.434     markus    534:        pmonitor->m_pkex = &active_state->kex;
1.237     markus    535:
1.393     djm       536:        if (use_privsep == PRIVSEP_ON)
1.384     djm       537:                box = ssh_sandbox_init();
1.237     markus    538:        pid = fork();
                    539:        if (pid == -1) {
                    540:                fatal("fork of unprivileged child failed");
                    541:        } else if (pid != 0) {
1.245     mpech     542:                debug2("Network child is on pid %ld", (long)pid);
1.237     markus    543:
1.392     markus    544:                pmonitor->m_pid = pid;
1.432     djm       545:                if (have_agent) {
                    546:                        r = ssh_get_authentication_socket(&auth_sock);
                    547:                        if (r != 0) {
                    548:                                error("Could not get agent socket: %s",
                    549:                                    ssh_err(r));
                    550:                                have_agent = 0;
                    551:                        }
                    552:                }
1.384     djm       553:                if (box != NULL)
                    554:                        ssh_sandbox_parent_preauth(box, pid);
1.278     markus    555:                monitor_child_preauth(authctxt, pmonitor);
1.237     markus    556:
                    557:                /* Wait for the child's exit status */
1.384     djm       558:                while (waitpid(pid, &status, 0) < 0) {
1.386     djm       559:                        if (errno == EINTR)
                    560:                                continue;
                    561:                        pmonitor->m_pid = -1;
                    562:                        fatal("%s: waitpid: %s", __func__, strerror(errno));
1.384     djm       563:                }
1.386     djm       564:                privsep_is_preauth = 0;
                    565:                pmonitor->m_pid = -1;
1.384     djm       566:                if (WIFEXITED(status)) {
                    567:                        if (WEXITSTATUS(status) != 0)
                    568:                                fatal("%s: preauth child exited with status %d",
                    569:                                    __func__, WEXITSTATUS(status));
                    570:                } else if (WIFSIGNALED(status))
                    571:                        fatal("%s: preauth child terminated by signal %d",
                    572:                            __func__, WTERMSIG(status));
                    573:                if (box != NULL)
                    574:                        ssh_sandbox_parent_finish(box);
                    575:                return 1;
1.237     markus    576:        } else {
                    577:                /* child */
1.383     djm       578:                close(pmonitor->m_sendfd);
                    579:                close(pmonitor->m_log_recvfd);
1.237     markus    580:
1.383     djm       581:                /* Arrange for logging to be sent to the monitor */
                    582:                set_log_handler(mm_log_handler, pmonitor);
1.237     markus    583:
1.460     djm       584:                privsep_preauth_child();
1.238     stevesk   585:                setproctitle("%s", "[net]");
1.384     djm       586:                if (box != NULL)
                    587:                        ssh_sandbox_child(box);
                    588:
                    589:                return 0;
1.237     markus    590:        }
                    591: }
                    592:
1.233     markus    593: static void
1.237     markus    594: privsep_postauth(Authctxt *authctxt)
1.231     provos    595: {
1.474     djm       596:        if (authctxt->pw->pw_uid == 0) {
1.231     provos    597:                /* File descriptor passing is broken or root login */
                    598:                use_privsep = 0;
1.315     djm       599:                goto skip;
1.231     provos    600:        }
1.234     markus    601:
1.231     provos    602:        /* New socket pair */
1.242     mouring   603:        monitor_reinit(pmonitor);
1.231     provos    604:
1.242     mouring   605:        pmonitor->m_pid = fork();
                    606:        if (pmonitor->m_pid == -1)
1.231     provos    607:                fatal("fork of unprivileged child failed");
1.242     mouring   608:        else if (pmonitor->m_pid != 0) {
1.364     markus    609:                verbose("User child is on pid %ld", (long)pmonitor->m_pid);
1.307     otto      610:                buffer_clear(&loginmsg);
1.242     mouring   611:                monitor_child_postauth(pmonitor);
1.231     provos    612:
                    613:                /* NEVERREACHED */
                    614:                exit(0);
                    615:        }
                    616:
1.383     djm       617:        /* child */
                    618:
1.242     mouring   619:        close(pmonitor->m_sendfd);
1.383     djm       620:        pmonitor->m_sendfd = -1;
1.231     provos    621:
                    622:        /* Demote the private keys to public keys. */
                    623:        demote_sensitive_data();
1.354     djm       624:
1.231     provos    625:        /* Drop privileges */
                    626:        do_setusercontext(authctxt->pw);
                    627:
1.315     djm       628:  skip:
1.231     provos    629:        /* It is safe now to apply the key state */
1.242     mouring   630:        monitor_apply_keystate(pmonitor);
1.312     markus    631:
                    632:        /*
                    633:         * Tell the packet layer that authentication was successful, since
                    634:         * this information is not part of the key state.
                    635:         */
                    636:        packet_set_authenticated();
1.231     provos    637: }
                    638:
1.200     itojun    639: static char *
1.134     markus    640: list_hostkey_types(void)
                    641: {
1.223     markus    642:        Buffer b;
1.281     jakob     643:        const char *p;
                    644:        char *ret;
1.134     markus    645:        int i;
1.373     djm       646:        Key *key;
1.223     markus    647:
                    648:        buffer_init(&b);
1.217     deraadt   649:        for (i = 0; i < options.num_host_key_files; i++) {
1.373     djm       650:                key = sensitive_data.host_keys[i];
1.443     djm       651:                if (key == NULL)
1.404     markus    652:                        key = sensitive_data.host_pubkeys[i];
1.472     markus    653:                if (key == NULL)
1.134     markus    654:                        continue;
1.454     markus    655:                /* Check that the key is accepted in HostkeyAlgorithms */
                    656:                if (match_pattern_list(sshkey_ssh_name(key),
                    657:                    options.hostkeyalgorithms, 0) != 1) {
                    658:                        debug3("%s: %s key not permitted by HostkeyAlgorithms",
                    659:                            __func__, sshkey_ssh_name(key));
                    660:                        continue;
                    661:                }
1.214     deraadt   662:                switch (key->type) {
1.134     markus    663:                case KEY_RSA:
                    664:                case KEY_DSA:
1.378     djm       665:                case KEY_ECDSA:
1.412     markus    666:                case KEY_ED25519:
1.223     markus    667:                        if (buffer_len(&b) > 0)
                    668:                                buffer_append(&b, ",", 1);
                    669:                        p = key_ssh_name(key);
                    670:                        buffer_append(&b, p, strlen(p));
1.461     markus    671:
                    672:                        /* for RSA we also support SHA2 signatures */
                    673:                        if (key->type == KEY_RSA) {
                    674:                                p = ",rsa-sha2-512,rsa-sha2-256";
                    675:                                buffer_append(&b, p, strlen(p));
                    676:                        }
1.134     markus    677:                        break;
                    678:                }
1.373     djm       679:                /* If the private key has a cert peer, then list that too */
                    680:                key = sensitive_data.host_certificates[i];
                    681:                if (key == NULL)
                    682:                        continue;
                    683:                switch (key->type) {
                    684:                case KEY_RSA_CERT:
                    685:                case KEY_DSA_CERT:
1.378     djm       686:                case KEY_ECDSA_CERT:
1.412     markus    687:                case KEY_ED25519_CERT:
1.373     djm       688:                        if (buffer_len(&b) > 0)
                    689:                                buffer_append(&b, ",", 1);
                    690:                        p = key_ssh_name(key);
                    691:                        buffer_append(&b, p, strlen(p));
                    692:                        break;
                    693:                }
1.134     markus    694:        }
1.467     djm       695:        if ((ret = sshbuf_dup_string(&b)) == NULL)
                    696:                fatal("%s: sshbuf_dup_string failed", __func__);
1.223     markus    697:        buffer_free(&b);
1.281     jakob     698:        debug("list_hostkey_types: %s", ret);
                    699:        return ret;
1.134     markus    700: }
                    701:
1.373     djm       702: static Key *
1.440     djm       703: get_hostkey_by_type(int type, int nid, int need_private, struct ssh *ssh)
1.134     markus    704: {
                    705:        int i;
1.373     djm       706:        Key *key;
1.250     deraadt   707:
1.217     deraadt   708:        for (i = 0; i < options.num_host_key_files; i++) {
1.375     djm       709:                switch (type) {
                    710:                case KEY_RSA_CERT:
                    711:                case KEY_DSA_CERT:
1.378     djm       712:                case KEY_ECDSA_CERT:
1.412     markus    713:                case KEY_ED25519_CERT:
1.373     djm       714:                        key = sensitive_data.host_certificates[i];
1.375     djm       715:                        break;
                    716:                default:
1.373     djm       717:                        key = sensitive_data.host_keys[i];
1.404     markus    718:                        if (key == NULL && !need_private)
                    719:                                key = sensitive_data.host_pubkeys[i];
1.375     djm       720:                        break;
                    721:                }
1.440     djm       722:                if (key != NULL && key->type == type &&
                    723:                    (key->type != KEY_ECDSA || key->ecdsa_nid == nid))
1.373     djm       724:                        return need_private ?
                    725:                            sensitive_data.host_keys[i] : key;
1.134     markus    726:        }
                    727:        return NULL;
1.96      markus    728: }
                    729:
1.231     provos    730: Key *
1.440     djm       731: get_hostkey_public_by_type(int type, int nid, struct ssh *ssh)
1.373     djm       732: {
1.440     djm       733:        return get_hostkey_by_type(type, nid, 0, ssh);
1.373     djm       734: }
                    735:
                    736: Key *
1.440     djm       737: get_hostkey_private_by_type(int type, int nid, struct ssh *ssh)
1.373     djm       738: {
1.440     djm       739:        return get_hostkey_by_type(type, nid, 1, ssh);
1.373     djm       740: }
                    741:
                    742: Key *
1.231     provos    743: get_hostkey_by_index(int ind)
                    744: {
                    745:        if (ind < 0 || ind >= options.num_host_key_files)
                    746:                return (NULL);
                    747:        return (sensitive_data.host_keys[ind]);
                    748: }
                    749:
1.404     markus    750: Key *
1.435     markus    751: get_hostkey_public_by_index(int ind, struct ssh *ssh)
1.404     markus    752: {
                    753:        if (ind < 0 || ind >= options.num_host_key_files)
                    754:                return (NULL);
                    755:        return (sensitive_data.host_pubkeys[ind]);
                    756: }
                    757:
1.231     provos    758: int
1.442     djm       759: get_hostkey_index(Key *key, int compare, struct ssh *ssh)
1.231     provos    760: {
                    761:        int i;
1.250     deraadt   762:
1.231     provos    763:        for (i = 0; i < options.num_host_key_files; i++) {
1.373     djm       764:                if (key_is_cert(key)) {
1.442     djm       765:                        if (key == sensitive_data.host_certificates[i] ||
                    766:                            (compare && sensitive_data.host_certificates[i] &&
                    767:                            sshkey_equal(key,
                    768:                            sensitive_data.host_certificates[i])))
1.373     djm       769:                                return (i);
                    770:                } else {
1.442     djm       771:                        if (key == sensitive_data.host_keys[i] ||
                    772:                            (compare && sensitive_data.host_keys[i] &&
                    773:                            sshkey_equal(key, sensitive_data.host_keys[i])))
1.373     djm       774:                                return (i);
1.442     djm       775:                        if (key == sensitive_data.host_pubkeys[i] ||
                    776:                            (compare && sensitive_data.host_pubkeys[i] &&
                    777:                            sshkey_equal(key, sensitive_data.host_pubkeys[i])))
1.404     markus    778:                                return (i);
1.373     djm       779:                }
1.231     provos    780:        }
                    781:        return (-1);
                    782: }
                    783:
1.439     djm       784: /* Inform the client of all hostkeys */
                    785: static void
                    786: notify_hostkeys(struct ssh *ssh)
                    787: {
                    788:        struct sshbuf *buf;
                    789:        struct sshkey *key;
                    790:        int i, nkeys, r;
                    791:        char *fp;
1.446     dtucker   792:
                    793:        /* Some clients cannot cope with the hostkeys message, skip those. */
                    794:        if (datafellows & SSH_BUG_HOSTKEYS)
                    795:                return;
1.439     djm       796:
                    797:        if ((buf = sshbuf_new()) == NULL)
                    798:                fatal("%s: sshbuf_new", __func__);
                    799:        for (i = nkeys = 0; i < options.num_host_key_files; i++) {
                    800:                key = get_hostkey_public_by_index(i, ssh);
                    801:                if (key == NULL || key->type == KEY_UNSPEC ||
1.472     markus    802:                    sshkey_is_cert(key))
1.439     djm       803:                        continue;
                    804:                fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    805:                    SSH_FP_DEFAULT);
                    806:                debug3("%s: key %d: %s %s", __func__, i,
                    807:                    sshkey_ssh_name(key), fp);
                    808:                free(fp);
1.442     djm       809:                if (nkeys == 0) {
                    810:                        packet_start(SSH2_MSG_GLOBAL_REQUEST);
1.444     djm       811:                        packet_put_cstring("hostkeys-00@openssh.com");
1.442     djm       812:                        packet_put_char(0); /* want-reply */
                    813:                }
                    814:                sshbuf_reset(buf);
                    815:                if ((r = sshkey_putb(key, buf)) != 0)
1.439     djm       816:                        fatal("%s: couldn't put hostkey %d: %s",
                    817:                            __func__, i, ssh_err(r));
1.442     djm       818:                packet_put_string(sshbuf_ptr(buf), sshbuf_len(buf));
1.439     djm       819:                nkeys++;
                    820:        }
1.442     djm       821:        debug3("%s: sent %d hostkeys", __func__, nkeys);
1.439     djm       822:        if (nkeys == 0)
                    823:                fatal("%s: no hostkeys", __func__);
                    824:        packet_send();
1.442     djm       825:        sshbuf_free(buf);
1.439     djm       826: }
                    827:
1.124     markus    828: /*
                    829:  * returns 1 if connection should be dropped, 0 otherwise.
                    830:  * dropping starts at connection #max_startups_begin with a probability
                    831:  * of (max_startups_rate/100). the probability increases linearly until
                    832:  * all connections are dropped for startups > max_startups
                    833:  */
1.200     itojun    834: static int
1.124     markus    835: drop_connection(int startups)
                    836: {
1.303     mickey    837:        int p, r;
1.124     markus    838:
                    839:        if (startups < options.max_startups_begin)
                    840:                return 0;
                    841:        if (startups >= options.max_startups)
                    842:                return 1;
                    843:        if (options.max_startups_rate == 100)
                    844:                return 1;
                    845:
                    846:        p  = 100 - options.max_startups_rate;
                    847:        p *= startups - options.max_startups_begin;
1.303     mickey    848:        p /= options.max_startups - options.max_startups_begin;
1.124     markus    849:        p += options.max_startups_rate;
1.356     djm       850:        r = arc4random_uniform(100);
1.124     markus    851:
1.304     djm       852:        debug("drop_connection: p %d, r %d", p, r);
1.124     markus    853:        return (r < p) ? 1 : 0;
                    854: }
                    855:
1.215     markus    856: static void
                    857: usage(void)
                    858: {
1.290     markus    859:        fprintf(stderr, "%s, %s\n",
1.426     markus    860:            SSH_VERSION,
                    861: #ifdef WITH_OPENSSL
                    862:            SSLeay_version(SSLEAY_VERSION)
                    863: #else
                    864:            "without OpenSSL"
                    865: #endif
                    866:        );
1.289     markus    867:        fprintf(stderr,
1.473     naddy     868: "usage: sshd [-46DdeiqTt] [-C connection_spec] [-c host_cert_file]\n"
1.399     dtucker   869: "            [-E log_file] [-f config_file] [-g login_grace_time]\n"
1.473     naddy     870: "            [-h host_key_file] [-o option] [-p port] [-u len]\n"
1.289     markus    871:        );
1.215     markus    872:        exit(1);
                    873: }
                    874:
1.294     djm       875: static void
1.467     djm       876: send_rexec_state(int fd, struct sshbuf *conf)
1.294     djm       877: {
1.467     djm       878:        struct sshbuf *m;
                    879:        int r;
1.294     djm       880:
1.467     djm       881:        debug3("%s: entering fd = %d config len %zu", __func__, fd,
                    882:            sshbuf_len(conf));
1.294     djm       883:
                    884:        /*
                    885:         * Protocol from reexec master to child:
                    886:         *      string  configuration
                    887:         */
1.467     djm       888:        if ((m = sshbuf_new()) == NULL)
                    889:                fatal("%s: sshbuf_new failed", __func__);
                    890:        if ((r = sshbuf_put_stringb(m, conf)) != 0)
                    891:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                    892:        if (ssh_msg_send(fd, 0, m) == -1)
1.294     djm       893:                fatal("%s: ssh_msg_send failed", __func__);
                    894:
1.467     djm       895:        sshbuf_free(m);
1.294     djm       896:
                    897:        debug3("%s: done", __func__);
                    898: }
                    899:
                    900: static void
                    901: recv_rexec_state(int fd, Buffer *conf)
                    902: {
                    903:        Buffer m;
                    904:        char *cp;
                    905:        u_int len;
                    906:
                    907:        debug3("%s: entering fd = %d", __func__, fd);
                    908:
                    909:        buffer_init(&m);
                    910:
                    911:        if (ssh_msg_recv(fd, &m) == -1)
                    912:                fatal("%s: ssh_msg_recv failed", __func__);
                    913:        if (buffer_get_char(&m) != 0)
                    914:                fatal("%s: rexec version mismatch", __func__);
                    915:
                    916:        cp = buffer_get_string(&m, &len);
                    917:        if (conf != NULL)
1.469     djm       918:                buffer_append(conf, cp, len);
1.402     djm       919:        free(cp);
1.294     djm       920:
                    921:        buffer_free(&m);
                    922:
                    923:        debug3("%s: done", __func__);
                    924: }
                    925:
1.345     djm       926: /* Accept a connection from inetd */
                    927: static void
                    928: server_accept_inetd(int *sock_in, int *sock_out)
                    929: {
                    930:        int fd;
                    931:
                    932:        startup_pipe = -1;
                    933:        if (rexeced_flag) {
                    934:                close(REEXEC_CONFIG_PASS_FD);
                    935:                *sock_in = *sock_out = dup(STDIN_FILENO);
                    936:                if (!debug_flag) {
                    937:                        startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
                    938:                        close(REEXEC_STARTUP_PIPE_FD);
                    939:                }
                    940:        } else {
                    941:                *sock_in = dup(STDIN_FILENO);
                    942:                *sock_out = dup(STDOUT_FILENO);
                    943:        }
                    944:        /*
                    945:         * We intentionally do not close the descriptors 0, 1, and 2
                    946:         * as our code for setting the descriptors won't work if
                    947:         * ttyfd happens to be one of those.
                    948:         */
                    949:        if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
                    950:                dup2(fd, STDIN_FILENO);
                    951:                dup2(fd, STDOUT_FILENO);
1.403     dtucker   952:                if (!log_stderr)
                    953:                        dup2(fd, STDERR_FILENO);
                    954:                if (fd > (log_stderr ? STDERR_FILENO : STDOUT_FILENO))
1.345     djm       955:                        close(fd);
                    956:        }
                    957:        debug("inetd sockets after dupping: %d, %d", *sock_in, *sock_out);
                    958: }
                    959:
                    960: /*
                    961:  * Listen for TCP connections
                    962:  */
                    963: static void
                    964: server_listen(void)
                    965: {
                    966:        int ret, listen_sock, on = 1;
                    967:        struct addrinfo *ai;
                    968:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
                    969:
                    970:        for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
                    971:                if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                    972:                        continue;
                    973:                if (num_listen_socks >= MAX_LISTEN_SOCKS)
                    974:                        fatal("Too many listen sockets. "
                    975:                            "Enlarge MAX_LISTEN_SOCKS");
                    976:                if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
                    977:                    ntop, sizeof(ntop), strport, sizeof(strport),
                    978:                    NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
                    979:                        error("getnameinfo failed: %.100s",
1.352     dtucker   980:                            ssh_gai_strerror(ret));
1.345     djm       981:                        continue;
                    982:                }
                    983:                /* Create socket for listening. */
1.370     dtucker   984:                listen_sock = socket(ai->ai_family, ai->ai_socktype,
                    985:                    ai->ai_protocol);
1.345     djm       986:                if (listen_sock < 0) {
                    987:                        /* kernel may not support ipv6 */
                    988:                        verbose("socket: %.100s", strerror(errno));
                    989:                        continue;
                    990:                }
                    991:                if (set_nonblock(listen_sock) == -1) {
                    992:                        close(listen_sock);
                    993:                        continue;
                    994:                }
                    995:                /*
                    996:                 * Set socket options.
                    997:                 * Allow local port reuse in TIME_WAIT.
                    998:                 */
                    999:                if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
                   1000:                    &on, sizeof(on)) == -1)
                   1001:                        error("setsockopt SO_REUSEADDR: %s", strerror(errno));
                   1002:
                   1003:                debug("Bind to port %s on %s.", strport, ntop);
                   1004:
                   1005:                /* Bind the socket to the desired port. */
                   1006:                if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
                   1007:                        error("Bind to port %s on %s failed: %.200s.",
                   1008:                            strport, ntop, strerror(errno));
                   1009:                        close(listen_sock);
                   1010:                        continue;
                   1011:                }
                   1012:                listen_socks[num_listen_socks] = listen_sock;
                   1013:                num_listen_socks++;
                   1014:
                   1015:                /* Start listening on the port. */
                   1016:                if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
                   1017:                        fatal("listen on [%s]:%s: %.100s",
                   1018:                            ntop, strport, strerror(errno));
                   1019:                logit("Server listening on %s port %s.", ntop, strport);
                   1020:        }
                   1021:        freeaddrinfo(options.listen_addrs);
                   1022:
                   1023:        if (!num_listen_socks)
                   1024:                fatal("Cannot bind any address.");
                   1025: }
                   1026:
                   1027: /*
                   1028:  * The main TCP accept loop. Note that, for the non-debug case, returns
                   1029:  * from this function are in a forked subprocess.
                   1030:  */
                   1031: static void
                   1032: server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
                   1033: {
                   1034:        fd_set *fdset;
                   1035:        int i, j, ret, maxfd;
1.472     markus   1036:        int startups = 0;
1.345     djm      1037:        int startup_p[2] = { -1 , -1 };
                   1038:        struct sockaddr_storage from;
                   1039:        socklen_t fromlen;
                   1040:        pid_t pid;
                   1041:
                   1042:        /* setup fd set for accept */
                   1043:        fdset = NULL;
                   1044:        maxfd = 0;
                   1045:        for (i = 0; i < num_listen_socks; i++)
                   1046:                if (listen_socks[i] > maxfd)
                   1047:                        maxfd = listen_socks[i];
                   1048:        /* pipes connected to unauthenticated childs */
                   1049:        startup_pipes = xcalloc(options.max_startups, sizeof(int));
                   1050:        for (i = 0; i < options.max_startups; i++)
                   1051:                startup_pipes[i] = -1;
                   1052:
                   1053:        /*
                   1054:         * Stay listening for connections until the system crashes or
                   1055:         * the daemon is killed with a signal.
                   1056:         */
                   1057:        for (;;) {
                   1058:                if (received_sighup)
                   1059:                        sighup_restart();
1.462     mmcc     1060:                free(fdset);
1.458     deraadt  1061:                fdset = xcalloc(howmany(maxfd + 1, NFDBITS),
1.345     djm      1062:                    sizeof(fd_mask));
                   1063:
                   1064:                for (i = 0; i < num_listen_socks; i++)
                   1065:                        FD_SET(listen_socks[i], fdset);
                   1066:                for (i = 0; i < options.max_startups; i++)
                   1067:                        if (startup_pipes[i] != -1)
                   1068:                                FD_SET(startup_pipes[i], fdset);
                   1069:
                   1070:                /* Wait in select until there is a connection. */
                   1071:                ret = select(maxfd+1, fdset, NULL, NULL, NULL);
                   1072:                if (ret < 0 && errno != EINTR)
                   1073:                        error("select: %.100s", strerror(errno));
                   1074:                if (received_sigterm) {
                   1075:                        logit("Received signal %d; terminating.",
                   1076:                            (int) received_sigterm);
                   1077:                        close_listen_socks();
1.430     djm      1078:                        if (options.pid_file != NULL)
                   1079:                                unlink(options.pid_file);
1.382     djm      1080:                        exit(received_sigterm == SIGTERM ? 0 : 255);
1.345     djm      1081:                }
                   1082:                if (ret < 0)
                   1083:                        continue;
                   1084:
                   1085:                for (i = 0; i < options.max_startups; i++)
                   1086:                        if (startup_pipes[i] != -1 &&
                   1087:                            FD_ISSET(startup_pipes[i], fdset)) {
                   1088:                                /*
                   1089:                                 * the read end of the pipe is ready
                   1090:                                 * if the child has closed the pipe
                   1091:                                 * after successful authentication
                   1092:                                 * or if the child has died
                   1093:                                 */
                   1094:                                close(startup_pipes[i]);
                   1095:                                startup_pipes[i] = -1;
                   1096:                                startups--;
                   1097:                        }
                   1098:                for (i = 0; i < num_listen_socks; i++) {
                   1099:                        if (!FD_ISSET(listen_socks[i], fdset))
                   1100:                                continue;
                   1101:                        fromlen = sizeof(from);
                   1102:                        *newsock = accept(listen_socks[i],
                   1103:                            (struct sockaddr *)&from, &fromlen);
                   1104:                        if (*newsock < 0) {
1.398     markus   1105:                                if (errno != EINTR && errno != EWOULDBLOCK &&
                   1106:                                    errno != ECONNABORTED)
1.389     djm      1107:                                        error("accept: %.100s",
                   1108:                                            strerror(errno));
                   1109:                                if (errno == EMFILE || errno == ENFILE)
                   1110:                                        usleep(100 * 1000);
1.345     djm      1111:                                continue;
                   1112:                        }
                   1113:                        if (unset_nonblock(*newsock) == -1) {
                   1114:                                close(*newsock);
                   1115:                                continue;
                   1116:                        }
                   1117:                        if (drop_connection(startups) == 1) {
                   1118:                                debug("drop connection #%d", startups);
                   1119:                                close(*newsock);
                   1120:                                continue;
                   1121:                        }
                   1122:                        if (pipe(startup_p) == -1) {
                   1123:                                close(*newsock);
                   1124:                                continue;
                   1125:                        }
                   1126:
                   1127:                        if (rexec_flag && socketpair(AF_UNIX,
                   1128:                            SOCK_STREAM, 0, config_s) == -1) {
                   1129:                                error("reexec socketpair: %s",
                   1130:                                    strerror(errno));
                   1131:                                close(*newsock);
                   1132:                                close(startup_p[0]);
                   1133:                                close(startup_p[1]);
                   1134:                                continue;
                   1135:                        }
                   1136:
                   1137:                        for (j = 0; j < options.max_startups; j++)
                   1138:                                if (startup_pipes[j] == -1) {
                   1139:                                        startup_pipes[j] = startup_p[0];
                   1140:                                        if (maxfd < startup_p[0])
                   1141:                                                maxfd = startup_p[0];
                   1142:                                        startups++;
                   1143:                                        break;
                   1144:                                }
                   1145:
                   1146:                        /*
                   1147:                         * Got connection.  Fork a child to handle it, unless
                   1148:                         * we are in debugging mode.
                   1149:                         */
                   1150:                        if (debug_flag) {
                   1151:                                /*
                   1152:                                 * In debugging mode.  Close the listening
                   1153:                                 * socket, and start processing the
                   1154:                                 * connection without forking.
                   1155:                                 */
                   1156:                                debug("Server will not fork when running in debugging mode.");
                   1157:                                close_listen_socks();
                   1158:                                *sock_in = *newsock;
                   1159:                                *sock_out = *newsock;
                   1160:                                close(startup_p[0]);
                   1161:                                close(startup_p[1]);
                   1162:                                startup_pipe = -1;
                   1163:                                pid = getpid();
                   1164:                                if (rexec_flag) {
                   1165:                                        send_rexec_state(config_s[0],
                   1166:                                            &cfg);
                   1167:                                        close(config_s[0]);
                   1168:                                }
                   1169:                                break;
                   1170:                        }
                   1171:
                   1172:                        /*
                   1173:                         * Normal production daemon.  Fork, and have
                   1174:                         * the child process the connection. The
                   1175:                         * parent continues listening.
                   1176:                         */
                   1177:                        if ((pid = fork()) == 0) {
                   1178:                                /*
                   1179:                                 * Child.  Close the listening and
                   1180:                                 * max_startup sockets.  Start using
                   1181:                                 * the accepted socket. Reinitialize
                   1182:                                 * logging (since our pid has changed).
                   1183:                                 * We break out of the loop to handle
                   1184:                                 * the connection.
                   1185:                                 */
                   1186:                                startup_pipe = startup_p[1];
                   1187:                                close_startup_pipes();
                   1188:                                close_listen_socks();
                   1189:                                *sock_in = *newsock;
                   1190:                                *sock_out = *newsock;
                   1191:                                log_init(__progname,
                   1192:                                    options.log_level,
                   1193:                                    options.log_facility,
                   1194:                                    log_stderr);
                   1195:                                if (rexec_flag)
                   1196:                                        close(config_s[0]);
                   1197:                                break;
                   1198:                        }
                   1199:
                   1200:                        /* Parent.  Stay in the loop. */
                   1201:                        if (pid < 0)
                   1202:                                error("fork: %.100s", strerror(errno));
                   1203:                        else
                   1204:                                debug("Forked child %ld.", (long)pid);
                   1205:
                   1206:                        close(startup_p[1]);
                   1207:
                   1208:                        if (rexec_flag) {
                   1209:                                send_rexec_state(config_s[0], &cfg);
                   1210:                                close(config_s[0]);
                   1211:                                close(config_s[1]);
                   1212:                        }
                   1213:                        close(*newsock);
                   1214:                }
                   1215:
                   1216:                /* child process check (or debug mode) */
                   1217:                if (num_listen_socks < 0)
                   1218:                        break;
                   1219:        }
                   1220: }
                   1221:
1.466     djm      1222: /*
                   1223:  * If IP options are supported, make sure there are none (log and
                   1224:  * return an error if any are found).  Basically we are worried about
                   1225:  * source routing; it can be used to pretend you are somebody
                   1226:  * (ip-address) you are not. That itself may be "almost acceptable"
                   1227:  * under certain circumstances, but rhosts autentication is useless
                   1228:  * if source routing is accepted. Notice also that if we just dropped
                   1229:  * source routing here, the other side could use IP spoofing to do
                   1230:  * rest of the interaction and could still bypass security.  So we
                   1231:  * exit here if we detect any IP options.
                   1232:  */
                   1233: static void
                   1234: check_ip_options(struct ssh *ssh)
                   1235: {
                   1236:        int sock_in = ssh_packet_get_connection_in(ssh);
                   1237:        struct sockaddr_storage from;
                   1238:        u_char opts[200];
1.475     djm      1239:        socklen_t i, option_size = sizeof(opts), fromlen = sizeof(from);
1.466     djm      1240:        char text[sizeof(opts) * 3 + 1];
                   1241:
                   1242:        memset(&from, 0, sizeof(from));
                   1243:        if (getpeername(sock_in, (struct sockaddr *)&from,
                   1244:            &fromlen) < 0)
                   1245:                return;
                   1246:        if (from.ss_family != AF_INET)
                   1247:                return;
                   1248:        /* XXX IPv6 options? */
                   1249:
                   1250:        if (getsockopt(sock_in, IPPROTO_IP, IP_OPTIONS, opts,
                   1251:            &option_size) >= 0 && option_size != 0) {
                   1252:                text[0] = '\0';
                   1253:                for (i = 0; i < option_size; i++)
                   1254:                        snprintf(text + i*3, sizeof(text) - i*3,
                   1255:                            " %2.2x", opts[i]);
                   1256:                fatal("Connection from %.100s port %d with IP opts: %.800s",
                   1257:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), text);
                   1258:        }
                   1259:        return;
                   1260: }
1.345     djm      1261:
1.65      deraadt  1262: /*
                   1263:  * Main program for the daemon.
                   1264:  */
1.2       provos   1265: int
                   1266: main(int ac, char **av)
1.1       deraadt  1267: {
1.466     djm      1268:        struct ssh *ssh = NULL;
1.64      markus   1269:        extern char *optarg;
                   1270:        extern int optind;
1.432     djm      1271:        int r, opt, i, j, on = 1;
1.297     avsm     1272:        int sock_in = -1, sock_out = -1, newsock = -1;
1.64      markus   1273:        const char *remote_ip;
                   1274:        int remote_port;
1.447     dtucker  1275:        char *fp, *line, *laddr, *logfile = NULL;
1.345     djm      1276:        int config_s[2] = { -1 , -1 };
1.396     djm      1277:        u_int n;
1.364     markus   1278:        u_int64_t ibytes, obytes;
1.362     dtucker  1279:        mode_t new_umask;
1.278     markus   1280:        Key *key;
1.404     markus   1281:        Key *pubkey;
                   1282:        int keytype;
1.230     provos   1283:        Authctxt *authctxt;
1.391     dtucker  1284:        struct connection_info *connection_info = get_connection_info(0, 0);
1.64      markus   1285:
1.465     dtucker  1286:        ssh_malloc_init();      /* must be called before any mallocs */
1.138     markus   1287:        /* Save argv. */
1.64      markus   1288:        saved_argv = av;
1.294     djm      1289:        rexec_argc = ac;
1.313     djm      1290:
                   1291:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                   1292:        sanitise_stdfd();
1.64      markus   1293:
                   1294:        /* Initialize configuration options to their default values. */
                   1295:        initialize_server_options(&options);
                   1296:
                   1297:        /* Parse command-line arguments. */
1.450     djm      1298:        while ((opt = getopt(ac, av,
                   1299:            "C:E:b:c:f:g:h:k:o:p:u:46DQRTdeiqrt")) != -1) {
1.64      markus   1300:                switch (opt) {
1.75      markus   1301:                case '4':
1.305     djm      1302:                        options.address_family = AF_INET;
1.75      markus   1303:                        break;
                   1304:                case '6':
1.305     djm      1305:                        options.address_family = AF_INET6;
1.75      markus   1306:                        break;
1.64      markus   1307:                case 'f':
                   1308:                        config_file_name = optarg;
                   1309:                        break;
1.373     djm      1310:                case 'c':
                   1311:                        if (options.num_host_cert_files >= MAX_HOSTCERTS) {
                   1312:                                fprintf(stderr, "too many host certificates.\n");
                   1313:                                exit(1);
                   1314:                        }
                   1315:                        options.host_cert_files[options.num_host_cert_files++] =
                   1316:                           derelativise_path(optarg);
                   1317:                        break;
1.64      markus   1318:                case 'd':
1.273     markus   1319:                        if (debug_flag == 0) {
1.127     markus   1320:                                debug_flag = 1;
                   1321:                                options.log_level = SYSLOG_LEVEL_DEBUG1;
1.273     markus   1322:                        } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
1.127     markus   1323:                                options.log_level++;
1.64      markus   1324:                        break;
1.135     markus   1325:                case 'D':
                   1326:                        no_daemon_flag = 1;
1.192     lebel    1327:                        break;
1.399     dtucker  1328:                case 'E':
1.459     dtucker  1329:                        logfile = optarg;
1.399     dtucker  1330:                        /* FALLTHROUGH */
1.192     lebel    1331:                case 'e':
                   1332:                        log_stderr = 1;
1.135     markus   1333:                        break;
1.64      markus   1334:                case 'i':
                   1335:                        inetd_flag = 1;
                   1336:                        break;
1.294     djm      1337:                case 'r':
                   1338:                        rexec_flag = 0;
                   1339:                        break;
                   1340:                case 'R':
                   1341:                        rexeced_flag = 1;
                   1342:                        inetd_flag = 1;
                   1343:                        break;
1.64      markus   1344:                case 'Q':
1.158     markus   1345:                        /* ignored */
1.64      markus   1346:                        break;
                   1347:                case 'q':
                   1348:                        options.log_level = SYSLOG_LEVEL_QUIET;
                   1349:                        break;
                   1350:                case 'b':
1.473     naddy    1351:                        /* protocol 1, ignored */
1.64      markus   1352:                        break;
                   1353:                case 'p':
1.75      markus   1354:                        options.ports_from_cmdline = 1;
1.127     markus   1355:                        if (options.num_ports >= MAX_PORTS) {
                   1356:                                fprintf(stderr, "too many ports.\n");
                   1357:                                exit(1);
                   1358:                        }
1.193     stevesk  1359:                        options.ports[options.num_ports++] = a2port(optarg);
1.366     djm      1360:                        if (options.ports[options.num_ports-1] <= 0) {
1.193     stevesk  1361:                                fprintf(stderr, "Bad port number.\n");
                   1362:                                exit(1);
                   1363:                        }
1.64      markus   1364:                        break;
                   1365:                case 'g':
1.197     stevesk  1366:                        if ((options.login_grace_time = convtime(optarg)) == -1) {
                   1367:                                fprintf(stderr, "Invalid login grace time.\n");
                   1368:                                exit(1);
                   1369:                        }
1.64      markus   1370:                        break;
                   1371:                case 'k':
1.473     naddy    1372:                        /* protocol 1, ignored */
1.64      markus   1373:                        break;
                   1374:                case 'h':
1.134     markus   1375:                        if (options.num_host_key_files >= MAX_HOSTKEYS) {
                   1376:                                fprintf(stderr, "too many host keys.\n");
                   1377:                                exit(1);
                   1378:                        }
1.371     djm      1379:                        options.host_key_files[options.num_host_key_files++] =
                   1380:                           derelativise_path(optarg);
1.64      markus   1381:                        break;
1.203     stevesk  1382:                case 't':
                   1383:                        test_flag = 1;
                   1384:                        break;
1.358     dtucker  1385:                case 'T':
                   1386:                        test_flag = 2;
                   1387:                        break;
                   1388:                case 'C':
1.391     dtucker  1389:                        if (parse_server_match_testspec(connection_info,
                   1390:                            optarg) == -1)
                   1391:                                exit(1);
1.358     dtucker  1392:                        break;
1.125     markus   1393:                case 'u':
1.438     deraadt  1394:                        utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL);
                   1395:                        if (utmp_len > HOST_NAME_MAX+1) {
1.257     stevesk  1396:                                fprintf(stderr, "Invalid utmp length.\n");
                   1397:                                exit(1);
                   1398:                        }
1.125     markus   1399:                        break;
1.215     markus   1400:                case 'o':
1.283     markus   1401:                        line = xstrdup(optarg);
                   1402:                        if (process_server_config_line(&options, line,
1.391     dtucker  1403:                            "command-line", 0, NULL, NULL) != 0)
1.217     deraadt  1404:                                exit(1);
1.402     djm      1405:                        free(line);
1.215     markus   1406:                        break;
1.64      markus   1407:                case '?':
                   1408:                default:
1.215     markus   1409:                        usage();
                   1410:                        break;
1.64      markus   1411:                }
                   1412:        }
1.294     djm      1413:        if (rexeced_flag || inetd_flag)
                   1414:                rexec_flag = 0;
1.355     mbalmer  1415:        if (!test_flag && (rexec_flag && (av[0] == NULL || *av[0] != '/')))
1.294     djm      1416:                fatal("sshd re-exec requires execution with an absolute path");
                   1417:        if (rexeced_flag)
1.296     djm      1418:                closefrom(REEXEC_MIN_FREE_FD);
                   1419:        else
                   1420:                closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
1.294     djm      1421:
1.426     markus   1422: #ifdef WITH_OPENSSL
1.379     djm      1423:        OpenSSL_add_all_algorithms();
1.426     markus   1424: #endif
1.64      markus   1425:
1.399     dtucker  1426:        /* If requested, redirect the logs to the specified logfile. */
1.459     dtucker  1427:        if (logfile != NULL)
1.399     dtucker  1428:                log_redirect_stderr_to(logfile);
1.75      markus   1429:        /*
                   1430:         * Force logging to stderr until we have loaded the private host
                   1431:         * key (unless started from inetd)
                   1432:         */
1.138     markus   1433:        log_init(__progname,
1.224     markus   1434:            options.log_level == SYSLOG_LEVEL_NOT_SET ?
                   1435:            SYSLOG_LEVEL_INFO : options.log_level,
                   1436:            options.log_facility == SYSLOG_FACILITY_NOT_SET ?
                   1437:            SYSLOG_FACILITY_AUTH : options.log_facility,
1.261     markus   1438:            log_stderr || !inetd_flag);
1.75      markus   1439:
1.294     djm      1440:        sensitive_data.have_ssh2_key = 0;
                   1441:
1.358     dtucker  1442:        /*
                   1443:         * If we're doing an extended config test, make sure we have all of
                   1444:         * the parameters we need.  If we're not doing an extended test,
                   1445:         * do not silently ignore connection test params.
                   1446:         */
1.391     dtucker  1447:        if (test_flag >= 2 && server_match_spec_complete(connection_info) == 0)
1.358     dtucker  1448:                fatal("user, host and addr are all required when testing "
                   1449:                   "Match configs");
1.391     dtucker  1450:        if (test_flag < 2 && server_match_spec_complete(connection_info) >= 0)
1.358     dtucker  1451:                fatal("Config test connection parameter (-C) provided without "
                   1452:                   "test mode (-T)");
                   1453:
1.294     djm      1454:        /* Fetch our configuration */
                   1455:        buffer_init(&cfg);
                   1456:        if (rexeced_flag)
1.296     djm      1457:                recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
1.448     djm      1458:        else if (strcasecmp(config_file_name, "none") != 0)
1.294     djm      1459:                load_server_config(config_file_name, &cfg);
                   1460:
1.337     dtucker  1461:        parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name,
1.391     dtucker  1462:            &cfg, NULL);
1.64      markus   1463:
                   1464:        /* Fill in default values for those options not explicitly set. */
                   1465:        fill_default_server_options(&options);
1.350     dtucker  1466:
                   1467:        /* challenge-response is implemented via keyboard interactive */
                   1468:        if (options.challenge_response_authentication)
                   1469:                options.kbd_interactive_authentication = 1;
1.395     djm      1470:
                   1471:        /* Check that options are sensible */
                   1472:        if (options.authorized_keys_command_user == NULL &&
                   1473:            (options.authorized_keys_command != NULL &&
                   1474:            strcasecmp(options.authorized_keys_command, "none") != 0))
                   1475:                fatal("AuthorizedKeysCommand set without "
                   1476:                    "AuthorizedKeysCommandUser");
1.449     djm      1477:        if (options.authorized_principals_command_user == NULL &&
                   1478:            (options.authorized_principals_command != NULL &&
                   1479:            strcasecmp(options.authorized_principals_command, "none") != 0))
                   1480:                fatal("AuthorizedPrincipalsCommand set without "
                   1481:                    "AuthorizedPrincipalsCommandUser");
1.396     djm      1482:
                   1483:        /*
                   1484:         * Check whether there is any path through configured auth methods.
                   1485:         * Unfortunately it is not possible to verify this generally before
                   1486:         * daemonisation in the presence of Match block, but this catches
                   1487:         * and warns for trivial misconfigurations that could break login.
                   1488:         */
                   1489:        if (options.num_auth_methods != 0) {
                   1490:                for (n = 0; n < options.num_auth_methods; n++) {
                   1491:                        if (auth2_methods_valid(options.auth_methods[n],
                   1492:                            1) == 0)
                   1493:                                break;
                   1494:                }
                   1495:                if (n >= options.num_auth_methods)
                   1496:                        fatal("AuthenticationMethods cannot be satisfied by "
                   1497:                            "enabled authentication methods");
                   1498:        }
1.305     djm      1499:
1.370     dtucker  1500:        /* set default channel AF */
1.305     djm      1501:        channel_set_af(options.address_family);
1.64      markus   1502:
                   1503:        /* Check that there are no remaining arguments. */
                   1504:        if (optind < ac) {
                   1505:                fprintf(stderr, "Extra argument %s.\n", av[optind]);
                   1506:                exit(1);
                   1507:        }
                   1508:
1.397     dtucker  1509:        debug("sshd version %s, %s", SSH_VERSION,
1.426     markus   1510: #ifdef WITH_OPENSSL
                   1511:            SSLeay_version(SSLEAY_VERSION)
                   1512: #else
                   1513:            "without OpenSSL"
                   1514: #endif
                   1515:        );
1.64      markus   1516:
1.404     markus   1517:        /* load host keys */
1.329     djm      1518:        sensitive_data.host_keys = xcalloc(options.num_host_key_files,
1.255     deraadt  1519:            sizeof(Key *));
1.404     markus   1520:        sensitive_data.host_pubkeys = xcalloc(options.num_host_key_files,
                   1521:            sizeof(Key *));
                   1522:
                   1523:        if (options.host_key_agent) {
                   1524:                if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME))
                   1525:                        setenv(SSH_AUTHSOCKET_ENV_NAME,
                   1526:                            options.host_key_agent, 1);
1.433     djm      1527:                if ((r = ssh_get_authentication_socket(NULL)) == 0)
                   1528:                        have_agent = 1;
                   1529:                else
                   1530:                        error("Could not connect to agent \"%s\": %s",
                   1531:                            options.host_key_agent, ssh_err(r));
1.404     markus   1532:        }
1.134     markus   1533:
1.217     deraadt  1534:        for (i = 0; i < options.num_host_key_files; i++) {
1.430     djm      1535:                if (options.host_key_files[i] == NULL)
                   1536:                        continue;
1.179     markus   1537:                key = key_load_private(options.host_key_files[i], "", NULL);
1.404     markus   1538:                pubkey = key_load_public(options.host_key_files[i], NULL);
1.439     djm      1539:                if (pubkey == NULL && key != NULL)
                   1540:                        pubkey = key_demote(key);
1.179     markus   1541:                sensitive_data.host_keys[i] = key;
1.404     markus   1542:                sensitive_data.host_pubkeys[i] = pubkey;
                   1543:
1.472     markus   1544:                if (key == NULL && pubkey != NULL && have_agent) {
1.443     djm      1545:                        debug("will rely on agent for hostkey %s",
                   1546:                            options.host_key_files[i]);
1.404     markus   1547:                        keytype = pubkey->type;
                   1548:                } else if (key != NULL) {
                   1549:                        keytype = key->type;
                   1550:                } else {
1.195     markus   1551:                        error("Could not load host key: %s",
                   1552:                            options.host_key_files[i]);
1.179     markus   1553:                        sensitive_data.host_keys[i] = NULL;
1.404     markus   1554:                        sensitive_data.host_pubkeys[i] = NULL;
1.134     markus   1555:                        continue;
                   1556:                }
1.404     markus   1557:
                   1558:                switch (keytype) {
1.134     markus   1559:                case KEY_RSA:
                   1560:                case KEY_DSA:
1.378     djm      1561:                case KEY_ECDSA:
1.412     markus   1562:                case KEY_ED25519:
1.441     djm      1563:                        if (have_agent || key != NULL)
                   1564:                                sensitive_data.have_ssh2_key = 1;
1.134     markus   1565:                        break;
                   1566:                }
1.441     djm      1567:                if ((fp = sshkey_fingerprint(pubkey, options.fingerprint_hash,
                   1568:                    SSH_FP_DEFAULT)) == NULL)
                   1569:                        fatal("sshkey_fingerprint failed");
                   1570:                debug("%s host key #%d: %s %s",
1.472     markus   1571:                    key ? "private" : "agent", i, sshkey_ssh_name(pubkey), fp);
1.441     djm      1572:                free(fp);
1.134     markus   1573:        }
1.472     markus   1574:        if (!sensitive_data.have_ssh2_key) {
1.264     itojun   1575:                logit("sshd: no hostkeys available -- exiting.");
1.64      markus   1576:                exit(1);
                   1577:        }
                   1578:
1.373     djm      1579:        /*
                   1580:         * Load certificates. They are stored in an array at identical
                   1581:         * indices to the public keys that they relate to.
                   1582:         */
                   1583:        sensitive_data.host_certificates = xcalloc(options.num_host_key_files,
                   1584:            sizeof(Key *));
                   1585:        for (i = 0; i < options.num_host_key_files; i++)
                   1586:                sensitive_data.host_certificates[i] = NULL;
                   1587:
                   1588:        for (i = 0; i < options.num_host_cert_files; i++) {
1.430     djm      1589:                if (options.host_cert_files[i] == NULL)
                   1590:                        continue;
1.373     djm      1591:                key = key_load_public(options.host_cert_files[i], NULL);
                   1592:                if (key == NULL) {
                   1593:                        error("Could not load host certificate: %s",
                   1594:                            options.host_cert_files[i]);
                   1595:                        continue;
                   1596:                }
                   1597:                if (!key_is_cert(key)) {
                   1598:                        error("Certificate file is not a certificate: %s",
                   1599:                            options.host_cert_files[i]);
                   1600:                        key_free(key);
                   1601:                        continue;
                   1602:                }
                   1603:                /* Find matching private key */
                   1604:                for (j = 0; j < options.num_host_key_files; j++) {
                   1605:                        if (key_equal_public(key,
                   1606:                            sensitive_data.host_keys[j])) {
                   1607:                                sensitive_data.host_certificates[j] = key;
                   1608:                                break;
                   1609:                        }
                   1610:                }
                   1611:                if (j >= options.num_host_key_files) {
                   1612:                        error("No matching private key for certificate: %s",
                   1613:                            options.host_cert_files[i]);
                   1614:                        key_free(key);
                   1615:                        continue;
                   1616:                }
                   1617:                sensitive_data.host_certificates[j] = key;
                   1618:                debug("host certificate: #%d type %d %s", j, key->type,
                   1619:                    key_type(key));
                   1620:        }
1.426     markus   1621:
1.244     markus   1622:        if (use_privsep) {
                   1623:                struct stat st;
                   1624:
1.327     deraadt  1625:                if (getpwnam(SSH_PRIVSEP_USER) == NULL)
1.244     markus   1626:                        fatal("Privilege separation user %s does not exist",
                   1627:                            SSH_PRIVSEP_USER);
                   1628:                if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
                   1629:                    (S_ISDIR(st.st_mode) == 0))
                   1630:                        fatal("Missing privilege separation directory: %s",
1.247     stevesk  1631:                            _PATH_PRIVSEP_CHROOT_DIR);
                   1632:                if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
1.262     markus   1633:                        fatal("%s must be owned by root and not group or "
                   1634:                            "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
1.358     dtucker  1635:        }
                   1636:
                   1637:        if (test_flag > 1) {
1.391     dtucker  1638:                if (server_match_spec_complete(connection_info) == 1)
                   1639:                        parse_server_match_config(&options, connection_info);
1.358     dtucker  1640:                dump_config(&options);
1.108     markus   1641:        }
1.203     stevesk  1642:
                   1643:        /* Configuration looks good, so exit if in test mode. */
                   1644:        if (test_flag)
                   1645:                exit(0);
1.108     markus   1646:
1.294     djm      1647:        if (rexec_flag) {
1.329     djm      1648:                rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
1.294     djm      1649:                for (i = 0; i < rexec_argc; i++) {
                   1650:                        debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
                   1651:                        rexec_argv[i] = saved_argv[i];
                   1652:                }
                   1653:                rexec_argv[rexec_argc] = "-R";
                   1654:                rexec_argv[rexec_argc + 1] = NULL;
                   1655:        }
1.362     dtucker  1656:
                   1657:        /* Ensure that umask disallows at least group and world write */
                   1658:        new_umask = umask(0077) | 0022;
                   1659:        (void) umask(new_umask);
1.294     djm      1660:
1.108     markus   1661:        /* Initialize the log (it is reinitialized below in case we forked). */
1.306     dtucker  1662:        if (debug_flag && (!inetd_flag || rexeced_flag))
1.64      markus   1663:                log_stderr = 1;
1.138     markus   1664:        log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64      markus   1665:
1.108     markus   1666:        /*
                   1667:         * If not in debugging mode, and not started from inetd, disconnect
                   1668:         * from the controlling terminal, and fork.  The original process
                   1669:         * exits.
                   1670:         */
1.135     markus   1671:        if (!(debug_flag || inetd_flag || no_daemon_flag)) {
1.345     djm      1672:
1.64      markus   1673:                if (daemon(0, 0) < 0)
                   1674:                        fatal("daemon() failed: %.200s", strerror(errno));
                   1675:
1.477   ! dtucker  1676:                disconnect_controlling_tty();
1.64      markus   1677:        }
                   1678:        /* Reinitialize the log (because of the fork above). */
1.138     markus   1679:        log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64      markus   1680:
                   1681:        /* Chdir to the root directory so that the current disk can be
                   1682:           unmounted if desired. */
1.401     dtucker  1683:        if (chdir("/") == -1)
                   1684:                error("chdir(\"/\"): %s", strerror(errno));
1.217     deraadt  1685:
1.178     markus   1686:        /* ignore SIGPIPE */
                   1687:        signal(SIGPIPE, SIG_IGN);
1.64      markus   1688:
1.345     djm      1689:        /* Get a connection, either from inetd or a listening TCP socket */
1.64      markus   1690:        if (inetd_flag) {
1.345     djm      1691:                server_accept_inetd(&sock_in, &sock_out);
1.64      markus   1692:        } else {
1.345     djm      1693:                server_listen();
1.75      markus   1694:
1.201     markus   1695:                signal(SIGHUP, sighup_handler);
1.345     djm      1696:                signal(SIGCHLD, main_sigchld_handler);
1.201     markus   1697:                signal(SIGTERM, sigterm_handler);
                   1698:                signal(SIGQUIT, sigterm_handler);
                   1699:
1.345     djm      1700:                /*
                   1701:                 * Write out the pid file after the sigterm handler
                   1702:                 * is setup and the listen sockets are bound
                   1703:                 */
1.430     djm      1704:                if (options.pid_file != NULL && !debug_flag) {
1.345     djm      1705:                        FILE *f = fopen(options.pid_file, "w");
1.201     markus   1706:
1.270     djm      1707:                        if (f == NULL) {
                   1708:                                error("Couldn't create pid file \"%s\": %s",
                   1709:                                    options.pid_file, strerror(errno));
                   1710:                        } else {
1.245     mpech    1711:                                fprintf(f, "%ld\n", (long) getpid());
1.64      markus   1712:                                fclose(f);
                   1713:                        }
                   1714:                }
                   1715:
1.345     djm      1716:                /* Accept a connection and return in a forked child */
                   1717:                server_accept_loop(&sock_in, &sock_out,
                   1718:                    &newsock, config_s);
1.1       deraadt  1719:        }
                   1720:
1.64      markus   1721:        /* This is the child processing a new connection. */
1.288     markus   1722:        setproctitle("%s", "[accepted]");
1.294     djm      1723:
1.300     markus   1724:        /*
                   1725:         * Create a new session and process group since the 4.4BSD
                   1726:         * setlogin() affects the entire process group.  We don't
                   1727:         * want the child to be able to affect the parent.
                   1728:         */
                   1729:        if (!debug_flag && !inetd_flag && setsid() < 0)
                   1730:                error("setsid: %.100s", strerror(errno));
                   1731:
1.294     djm      1732:        if (rexec_flag) {
                   1733:                int fd;
                   1734:
1.296     djm      1735:                debug("rexec start in %d out %d newsock %d pipe %d sock %d",
                   1736:                    sock_in, sock_out, newsock, startup_pipe, config_s[0]);
1.294     djm      1737:                dup2(newsock, STDIN_FILENO);
                   1738:                dup2(STDIN_FILENO, STDOUT_FILENO);
                   1739:                if (startup_pipe == -1)
1.296     djm      1740:                        close(REEXEC_STARTUP_PIPE_FD);
1.407     djm      1741:                else if (startup_pipe != REEXEC_STARTUP_PIPE_FD) {
1.296     djm      1742:                        dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
1.407     djm      1743:                        close(startup_pipe);
                   1744:                        startup_pipe = REEXEC_STARTUP_PIPE_FD;
                   1745:                }
1.294     djm      1746:
1.296     djm      1747:                dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
1.294     djm      1748:                close(config_s[1]);
1.296     djm      1749:
1.294     djm      1750:                execv(rexec_argv[0], rexec_argv);
                   1751:
                   1752:                /* Reexec has failed, fall back and continue */
                   1753:                error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
1.296     djm      1754:                recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
1.294     djm      1755:                log_init(__progname, options.log_level,
                   1756:                    options.log_facility, log_stderr);
                   1757:
                   1758:                /* Clean up fds */
1.296     djm      1759:                close(REEXEC_CONFIG_PASS_FD);
                   1760:                newsock = sock_out = sock_in = dup(STDIN_FILENO);
1.294     djm      1761:                if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
                   1762:                        dup2(fd, STDIN_FILENO);
                   1763:                        dup2(fd, STDOUT_FILENO);
                   1764:                        if (fd > STDERR_FILENO)
                   1765:                                close(fd);
                   1766:                }
1.296     djm      1767:                debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
                   1768:                    sock_in, sock_out, newsock, startup_pipe, config_s[0]);
1.294     djm      1769:        }
1.372     djm      1770:
                   1771:        /* Executed child processes don't need these. */
                   1772:        fcntl(sock_out, F_SETFD, FD_CLOEXEC);
                   1773:        fcntl(sock_in, F_SETFD, FD_CLOEXEC);
1.64      markus   1774:
1.66      markus   1775:        /*
                   1776:         * Disable the key regeneration alarm.  We will not regenerate the
                   1777:         * key since we are no longer in a position to give it to anyone. We
                   1778:         * will not restart on SIGHUP since it no longer makes sense.
                   1779:         */
1.64      markus   1780:        alarm(0);
                   1781:        signal(SIGALRM, SIG_DFL);
                   1782:        signal(SIGHUP, SIG_DFL);
                   1783:        signal(SIGTERM, SIG_DFL);
                   1784:        signal(SIGQUIT, SIG_DFL);
                   1785:        signal(SIGCHLD, SIG_DFL);
1.150     markus   1786:
1.66      markus   1787:        /*
                   1788:         * Register our connection.  This turns encryption off because we do
                   1789:         * not have a key.
                   1790:         */
1.64      markus   1791:        packet_set_connection(sock_in, sock_out);
1.312     markus   1792:        packet_set_server();
1.466     djm      1793:        ssh = active_state; /* XXX */
                   1794:        check_ip_options(ssh);
1.309     djm      1795:
                   1796:        /* Set SO_KEEPALIVE if requested. */
                   1797:        if (options.tcp_keep_alive && packet_connection_is_on_socket() &&
                   1798:            setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
                   1799:                error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.1       deraadt  1800:
1.466     djm      1801:        if ((remote_port = ssh_remote_port(ssh)) < 0) {
                   1802:                debug("ssh_remote_port failed");
1.310     markus   1803:                cleanup_exit(255);
                   1804:        }
1.316     dtucker  1805:
                   1806:        /*
1.331     markus   1807:         * The rest of the code depends on the fact that
1.466     djm      1808:         * ssh_remote_ipaddr() caches the remote ip, even if
1.331     markus   1809:         * the socket goes away.
                   1810:         */
1.466     djm      1811:        remote_ip = ssh_remote_ipaddr(ssh);
1.209     markus   1812:
1.64      markus   1813:        /* Log the connection. */
1.447     dtucker  1814:        laddr = get_local_ipaddr(sock_in);
1.409     djm      1815:        verbose("Connection from %s port %d on %s port %d",
1.466     djm      1816:            remote_ip, remote_port, laddr,  ssh_local_port(ssh));
1.447     dtucker  1817:        free(laddr);
1.1       deraadt  1818:
1.66      markus   1819:        /*
1.317     djm      1820:         * We don't want to listen forever unless the other side
1.66      markus   1821:         * successfully authenticates itself.  So we set up an alarm which is
                   1822:         * cleared after successful authentication.  A limit of zero
1.317     djm      1823:         * indicates no limit. Note that we don't set the alarm in debugging
1.66      markus   1824:         * mode; it is just annoying to have the server exit just when you
                   1825:         * are about to discover the bug.
                   1826:         */
1.64      markus   1827:        signal(SIGALRM, grace_alarm_handler);
                   1828:        if (!debug_flag)
                   1829:                alarm(options.login_grace_time);
                   1830:
1.466     djm      1831:        sshd_exchange_identification(ssh, sock_in, sock_out);
1.64      markus   1832:        packet_set_nonblocking();
1.1       deraadt  1833:
1.278     markus   1834:        /* allocate authentication context */
1.329     djm      1835:        authctxt = xcalloc(1, sizeof(*authctxt));
1.278     markus   1836:
                   1837:        /* XXX global for cleanup, access from other modules */
                   1838:        the_authctxt = authctxt;
                   1839:
1.307     otto     1840:        /* prepare buffer to collect messages to display to user after login */
                   1841:        buffer_init(&loginmsg);
1.374     dtucker  1842:        auth_debug_reset();
1.307     otto     1843:
1.404     markus   1844:        if (use_privsep) {
1.278     markus   1845:                if (privsep_preauth(authctxt) == 1)
1.237     markus   1846:                        goto authenticated;
1.472     markus   1847:        } else if (have_agent) {
1.432     djm      1848:                if ((r = ssh_get_authentication_socket(&auth_sock)) != 0) {
                   1849:                        error("Unable to get agent socket: %s", ssh_err(r));
1.433     djm      1850:                        have_agent = 0;
1.432     djm      1851:                }
                   1852:        }
1.231     provos   1853:
1.77      markus   1854:        /* perform the key exchange */
                   1855:        /* authenticate user and start session */
1.472     markus   1856:        do_ssh2_kex();
                   1857:        do_authentication2(authctxt);
                   1858:
1.237     markus   1859:        /*
                   1860:         * If we use privilege separation, the unprivileged child transfers
                   1861:         * the current keystate and exits
                   1862:         */
                   1863:        if (use_privsep) {
1.242     mouring  1864:                mm_send_keystate(pmonitor);
1.231     provos   1865:                exit(0);
1.237     markus   1866:        }
1.231     provos   1867:
                   1868:  authenticated:
1.318     djm      1869:        /*
                   1870:         * Cancel the alarm we set to limit the time taken for
                   1871:         * authentication.
                   1872:         */
                   1873:        alarm(0);
                   1874:        signal(SIGALRM, SIG_DFL);
1.347     markus   1875:        authctxt->authenticated = 1;
1.318     djm      1876:        if (startup_pipe != -1) {
                   1877:                close(startup_pipe);
                   1878:                startup_pipe = -1;
                   1879:        }
                   1880:
1.234     markus   1881:        /*
1.231     provos   1882:         * In privilege separation, we fork another child and prepare
                   1883:         * file descriptor passing.
                   1884:         */
                   1885:        if (use_privsep) {
1.237     markus   1886:                privsep_postauth(authctxt);
                   1887:                /* the monitor process [priv] will not return */
1.231     provos   1888:        }
1.360     dtucker  1889:
                   1890:        packet_set_timeout(options.client_alive_interval,
                   1891:            options.client_alive_count_max);
1.439     djm      1892:
                   1893:        /* Try to send all our hostkeys to the client */
1.472     markus   1894:        notify_hostkeys(active_state);
1.230     provos   1895:
1.278     markus   1896:        /* Start session. */
1.230     provos   1897:        do_authenticated(authctxt);
                   1898:
1.64      markus   1899:        /* The connection has been terminated. */
1.434     markus   1900:        packet_get_bytes(&ibytes, &obytes);
1.381     djm      1901:        verbose("Transferred: sent %llu, received %llu bytes",
                   1902:            (unsigned long long)obytes, (unsigned long long)ibytes);
1.364     markus   1903:
                   1904:        verbose("Closing connection to %.500s port %d", remote_ip, remote_port);
1.64      markus   1905:        packet_close();
1.231     provos   1906:
                   1907:        if (use_privsep)
                   1908:                mm_terminate();
                   1909:
1.64      markus   1910:        exit(0);
1.1       deraadt  1911: }
1.98      markus   1912:
1.435     markus   1913: int
                   1914: sshd_hostkey_sign(Key *privkey, Key *pubkey, u_char **signature, size_t *slen,
1.461     markus   1915:     const u_char *data, size_t dlen, const char *alg, u_int flag)
1.404     markus   1916: {
1.432     djm      1917:        int r;
1.435     markus   1918:        u_int xxx_slen, xxx_dlen = dlen;
1.432     djm      1919:
1.404     markus   1920:        if (privkey) {
1.461     markus   1921:                if (PRIVSEP(key_sign(privkey, signature, &xxx_slen, data, xxx_dlen,
                   1922:                    alg) < 0))
1.404     markus   1923:                        fatal("%s: key_sign failed", __func__);
1.435     markus   1924:                if (slen)
                   1925:                        *slen = xxx_slen;
1.404     markus   1926:        } else if (use_privsep) {
1.461     markus   1927:                if (mm_key_sign(pubkey, signature, &xxx_slen, data, xxx_dlen,
                   1928:                    alg) < 0)
1.404     markus   1929:                        fatal("%s: pubkey_sign failed", __func__);
1.435     markus   1930:                if (slen)
                   1931:                        *slen = xxx_slen;
1.404     markus   1932:        } else {
1.435     markus   1933:                if ((r = ssh_agent_sign(auth_sock, pubkey, signature, slen,
1.461     markus   1934:                    data, dlen, alg, datafellows)) != 0)
1.432     djm      1935:                        fatal("%s: ssh_agent_sign failed: %s",
                   1936:                            __func__, ssh_err(r));
1.404     markus   1937:        }
1.435     markus   1938:        return 0;
1.404     markus   1939: }
                   1940:
1.452     djm      1941: /* SSH2 key exchange */
1.200     itojun   1942: static void
1.142     markus   1943: do_ssh2_kex(void)
1.98      markus   1944: {
1.422     markus   1945:        char *myproposal[PROPOSAL_MAX] = { KEX_SERVER };
1.435     markus   1946:        struct kex *kex;
1.437     markus   1947:        int r;
1.102     markus   1948:
1.457     djm      1949:        myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
                   1950:            options.kex_algorithms);
                   1951:        myproposal[PROPOSAL_ENC_ALGS_CTOS] = compat_cipher_proposal(
                   1952:            options.ciphers);
                   1953:        myproposal[PROPOSAL_ENC_ALGS_STOC] = compat_cipher_proposal(
                   1954:            options.ciphers);
                   1955:        myproposal[PROPOSAL_MAC_ALGS_CTOS] =
                   1956:            myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
                   1957:
1.312     markus   1958:        if (options.compression == COMP_NONE) {
1.246     markus   1959:                myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.470     dtucker  1960:                    myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
1.102     markus   1961:        }
1.400     dtucker  1962:
                   1963:        if (options.rekey_limit || options.rekey_interval)
1.464     dtucker  1964:                packet_set_rekey_limits(options.rekey_limit,
1.400     dtucker  1965:                    (time_t)options.rekey_interval);
1.327     deraadt  1966:
1.413     djm      1967:        myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
                   1968:            list_hostkey_types());
1.134     markus   1969:
1.189     markus   1970:        /* start key exchange */
1.437     markus   1971:        if ((r = kex_setup(active_state, myproposal)) != 0)
                   1972:                fatal("kex_setup: %s", ssh_err(r));
1.435     markus   1973:        kex = active_state->kex;
1.426     markus   1974: #ifdef WITH_OPENSSL
1.263     markus   1975:        kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1.292     djm      1976:        kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.468     djm      1977:        kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
                   1978:        kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
                   1979:        kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
1.263     markus   1980:        kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.324     djm      1981:        kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.378     djm      1982:        kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1.426     markus   1983: #endif
1.410     markus   1984:        kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1.186     markus   1985:        kex->server = 1;
                   1986:        kex->client_version_string=client_version_string;
                   1987:        kex->server_version_string=server_version_string;
1.373     djm      1988:        kex->load_host_public_key=&get_hostkey_public_by_type;
                   1989:        kex->load_host_private_key=&get_hostkey_private_by_type;
1.231     provos   1990:        kex->host_key_index=&get_hostkey_index;
1.404     markus   1991:        kex->sign = sshd_hostkey_sign;
1.189     markus   1992:
1.435     markus   1993:        dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
1.187     markus   1994:
                   1995:        session_id2 = kex->session_id;
                   1996:        session_id2_len = kex->session_id_len;
1.129     provos   1997:
                   1998: #ifdef DEBUG_KEXDH
                   1999:        /* send 1st encrypted/maced/compressed message */
                   2000:        packet_start(SSH2_MSG_IGNORE);
                   2001:        packet_put_cstring("markus");
                   2002:        packet_send();
                   2003:        packet_write_wait();
                   2004: #endif
1.186     markus   2005:        debug("KEX done");
1.278     markus   2006: }
                   2007:
                   2008: /* server specific fatal cleanup */
                   2009: void
                   2010: cleanup_exit(int i)
                   2011: {
1.386     djm      2012:        if (the_authctxt) {
1.278     markus   2013:                do_cleanup(the_authctxt);
1.423     djm      2014:                if (use_privsep && privsep_is_preauth &&
                   2015:                    pmonitor != NULL && pmonitor->m_pid > 1) {
1.386     djm      2016:                        debug("Killing privsep child %d", pmonitor->m_pid);
                   2017:                        if (kill(pmonitor->m_pid, SIGKILL) != 0 &&
1.388     djm      2018:                            errno != ESRCH)
1.386     djm      2019:                                error("%s: kill(%d): %s", __func__,
                   2020:                                    pmonitor->m_pid, strerror(errno));
                   2021:                }
                   2022:        }
1.278     markus   2023:        _exit(i);
1.1       deraadt  2024: }