[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.361

1.361   ! dtucker     1: /* $OpenBSD: sshd.c,v 1.360 2008/06/12 20:38:28 dtucker 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.1       deraadt    64:
1.155     markus     65: #include <openssl/dh.h>
                     66: #include <openssl/bn.h>
1.226     markus     67: #include <openssl/md5.h>
1.231     provos     68: #include <openssl/rand.h>
1.155     markus     69:
1.343     deraadt    70: #include "xmalloc.h"
1.155     markus     71: #include "ssh.h"
                     72: #include "ssh1.h"
                     73: #include "ssh2.h"
1.1       deraadt    74: #include "rsa.h"
1.171     djm        75: #include "sshpty.h"
1.1       deraadt    76: #include "packet.h"
1.155     markus     77: #include "log.h"
1.343     deraadt    78: #include "buffer.h"
1.1       deraadt    79: #include "servconf.h"
                     80: #include "uidswap.h"
1.33      markus     81: #include "compat.h"
1.155     markus     82: #include "cipher.h"
1.343     deraadt    83: #include "key.h"
1.98      markus     84: #include "kex.h"
1.129     provos     85: #include "dh.h"
1.98      markus     86: #include "myproposal.h"
1.108     markus     87: #include "authfile.h"
1.154     markus     88: #include "pathnames.h"
1.155     markus     89: #include "atomicio.h"
                     90: #include "canohost.h"
1.343     deraadt    91: #include "hostfile.h"
1.155     markus     92: #include "auth.h"
                     93: #include "misc.h"
1.294     djm        94: #include "msg.h"
1.186     markus     95: #include "dispatch.h"
1.206     stevesk    96: #include "channels.h"
1.230     provos     97: #include "session.h"
1.231     provos     98: #include "monitor_mm.h"
                     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"
                    104: #include "monitor_fdpass.h"
1.332     stevesk   105: #include "version.h"
1.1       deraadt   106:
                    107: #ifdef LIBWRAP
                    108: #include <tcpd.h>
                    109: #include <syslog.h>
                    110: int allow_severity = LOG_INFO;
                    111: int deny_severity = LOG_WARNING;
                    112: #endif /* LIBWRAP */
                    113:
                    114: #ifndef O_NOCTTY
                    115: #define O_NOCTTY       0
                    116: #endif
                    117:
1.296     djm       118: /* Re-exec fds */
                    119: #define REEXEC_DEVCRYPTO_RESERVED_FD   (STDERR_FILENO + 1)
                    120: #define REEXEC_STARTUP_PIPE_FD         (STDERR_FILENO + 2)
                    121: #define REEXEC_CONFIG_PASS_FD          (STDERR_FILENO + 3)
                    122: #define REEXEC_MIN_FREE_FD             (STDERR_FILENO + 4)
                    123:
1.138     markus    124: extern char *__progname;
                    125:
1.1       deraadt   126: /* Server configuration options. */
                    127: ServerOptions options;
                    128:
                    129: /* Name of the server configuration file. */
1.154     markus    130: char *config_file_name = _PATH_SERVER_CONFIG_FILE;
1.1       deraadt   131:
1.105     markus    132: /*
1.65      deraadt   133:  * Debug mode flag.  This can be set on the command line.  If debug
                    134:  * mode is enabled, extra debugging output will be sent to the system
                    135:  * log, the daemon will not go to background, and will exit after processing
                    136:  * the first connection.
                    137:  */
1.1       deraadt   138: int debug_flag = 0;
                    139:
1.203     stevesk   140: /* Flag indicating that the daemon should only test the configuration and keys. */
                    141: int test_flag = 0;
                    142:
1.1       deraadt   143: /* Flag indicating that the daemon is being started from inetd. */
                    144: int inetd_flag = 0;
                    145:
1.135     markus    146: /* Flag indicating that sshd should not detach and become a daemon. */
                    147: int no_daemon_flag = 0;
                    148:
1.47      markus    149: /* debug goes to stderr unless inetd_flag is set */
                    150: int log_stderr = 0;
                    151:
1.1       deraadt   152: /* Saved arguments to main(). */
                    153: char **saved_argv;
                    154:
1.294     djm       155: /* re-exec */
                    156: int rexeced_flag = 0;
                    157: int rexec_flag = 1;
                    158: int rexec_argc = 0;
                    159: char **rexec_argv;
                    160:
1.66      markus    161: /*
1.75      markus    162:  * The sockets that the server is listening; this is used in the SIGHUP
                    163:  * signal handler.
1.66      markus    164:  */
1.75      markus    165: #define        MAX_LISTEN_SOCKS        16
                    166: int listen_socks[MAX_LISTEN_SOCKS];
                    167: int num_listen_socks = 0;
1.1       deraadt   168:
1.66      markus    169: /*
                    170:  * the client's version string, passed by sshd2 in compat mode. if != NULL,
                    171:  * sshd will skip the version-number exchange
                    172:  */
1.61      markus    173: char *client_version_string = NULL;
1.96      markus    174: char *server_version_string = NULL;
1.1       deraadt   175:
1.189     markus    176: /* for rekeying XXX fixme */
                    177: Kex *xxx_kex;
                    178:
1.66      markus    179: /*
                    180:  * Any really sensitive data in the application is contained in this
                    181:  * structure. The idea is that this structure could be locked into memory so
                    182:  * that the pages do not get written into swap.  However, there are some
                    183:  * problems. The private key contains BIGNUMs, and we do not (in principle)
                    184:  * have access to the internals of them, and locking just the structure is
                    185:  * not very useful.  Currently, memory locking is not implemented.
                    186:  */
1.64      markus    187: struct {
1.174     deraadt   188:        Key     *server_key;            /* ephemeral server key */
1.134     markus    189:        Key     *ssh1_host_key;         /* ssh1 host key */
                    190:        Key     **host_keys;            /* all private host keys */
                    191:        int     have_ssh1_key;
                    192:        int     have_ssh2_key;
1.169     markus    193:        u_char  ssh1_cookie[SSH_SESSION_KEY_LENGTH];
1.1       deraadt   194: } sensitive_data;
                    195:
1.66      markus    196: /*
1.151     markus    197:  * Flag indicating whether the RSA server key needs to be regenerated.
                    198:  * Is set in the SIGALRM handler and cleared when the key is regenerated.
1.66      markus    199:  */
1.212     markus    200: static volatile sig_atomic_t key_do_regen = 0;
1.1       deraadt   201:
1.199     markus    202: /* This is set to true when a signal is received. */
1.212     markus    203: static volatile sig_atomic_t received_sighup = 0;
                    204: static volatile sig_atomic_t received_sigterm = 0;
1.1       deraadt   205:
1.96      markus    206: /* session identifier, used by RSA-auth */
1.140     markus    207: u_char session_id[16];
1.96      markus    208:
1.108     markus    209: /* same for ssh2 */
1.140     markus    210: u_char *session_id2 = NULL;
1.269     markus    211: u_int session_id2_len = 0;
1.108     markus    212:
1.125     markus    213: /* record remote hostname or ip */
1.140     markus    214: u_int utmp_len = MAXHOSTNAMELEN;
1.125     markus    215:
1.211     markus    216: /* options.max_startup sized array of fd ints */
                    217: int *startup_pipes = NULL;
                    218: int startup_pipe;              /* in child */
                    219:
1.231     provos    220: /* variables used for privilege separation */
1.337     dtucker   221: int use_privsep = -1;
1.285     dtucker   222: struct monitor *pmonitor = NULL;
1.231     provos    223:
1.278     markus    224: /* global authentication context */
                    225: Authctxt *the_authctxt = NULL;
                    226:
1.337     dtucker   227: /* sshd_config buffer */
                    228: Buffer cfg;
                    229:
1.299     dtucker   230: /* message to be displayed after login */
                    231: Buffer loginmsg;
                    232:
1.1       deraadt   233: /* Prototypes for various functions defined later in this file. */
1.200     itojun    234: void destroy_sensitive_data(void);
1.231     provos    235: void demote_sensitive_data(void);
1.87      markus    236:
1.200     itojun    237: static void do_ssh1_kex(void);
                    238: static void do_ssh2_kex(void);
1.129     provos    239:
1.87      markus    240: /*
1.75      markus    241:  * Close all listening sockets
                    242:  */
1.200     itojun    243: static void
1.75      markus    244: close_listen_socks(void)
                    245: {
                    246:        int i;
1.250     deraadt   247:
1.75      markus    248:        for (i = 0; i < num_listen_socks; i++)
                    249:                close(listen_socks[i]);
                    250:        num_listen_socks = -1;
                    251: }
                    252:
1.211     markus    253: static void
                    254: close_startup_pipes(void)
                    255: {
                    256:        int i;
1.250     deraadt   257:
1.211     markus    258:        if (startup_pipes)
                    259:                for (i = 0; i < options.max_startups; i++)
                    260:                        if (startup_pipes[i] != -1)
                    261:                                close(startup_pipes[i]);
                    262: }
                    263:
1.75      markus    264: /*
1.65      deraadt   265:  * Signal handler for SIGHUP.  Sshd execs itself when it receives SIGHUP;
                    266:  * the effect is to reread the configuration file (and to regenerate
                    267:  * the server key).
                    268:  */
1.327     deraadt   269:
                    270: /*ARGSUSED*/
1.200     itojun    271: static void
1.64      markus    272: sighup_handler(int sig)
1.1       deraadt   273: {
1.210     deraadt   274:        int save_errno = errno;
                    275:
1.64      markus    276:        received_sighup = 1;
                    277:        signal(SIGHUP, sighup_handler);
1.210     deraadt   278:        errno = save_errno;
1.1       deraadt   279: }
                    280:
1.65      deraadt   281: /*
                    282:  * Called from the main program after receiving SIGHUP.
                    283:  * Restarts the server.
                    284:  */
1.200     itojun    285: static void
1.165     itojun    286: sighup_restart(void)
1.1       deraadt   287: {
1.264     itojun    288:        logit("Received SIGHUP; restarting.");
1.75      markus    289:        close_listen_socks();
1.211     markus    290:        close_startup_pipes();
1.349     dtucker   291:        alarm(0);  /* alarm timer persists across exec */
1.64      markus    292:        execv(saved_argv[0], saved_argv);
1.264     itojun    293:        logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
1.250     deraadt   294:            strerror(errno));
1.64      markus    295:        exit(1);
1.1       deraadt   296: }
                    297:
1.65      deraadt   298: /*
                    299:  * Generic signal handler for terminating signals in the master daemon.
                    300:  */
1.327     deraadt   301: /*ARGSUSED*/
1.200     itojun    302: static void
1.64      markus    303: sigterm_handler(int sig)
1.1       deraadt   304: {
1.199     markus    305:        received_sigterm = sig;
1.1       deraadt   306: }
                    307:
1.65      deraadt   308: /*
                    309:  * SIGCHLD handler.  This is called whenever a child dies.  This will then
1.199     markus    310:  * reap any zombies left by exited children.
1.65      deraadt   311:  */
1.327     deraadt   312: /*ARGSUSED*/
1.200     itojun    313: static void
1.64      markus    314: main_sigchld_handler(int sig)
1.1       deraadt   315: {
1.250     deraadt   316:        int save_errno = errno;
1.239     markus    317:        pid_t pid;
1.64      markus    318:        int status;
1.60      deraadt   319:
1.239     markus    320:        while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
                    321:            (pid < 0 && errno == EINTR))
1.64      markus    322:                ;
1.60      deraadt   323:
1.64      markus    324:        signal(SIGCHLD, main_sigchld_handler);
                    325:        errno = save_errno;
1.1       deraadt   326: }
                    327:
1.65      deraadt   328: /*
                    329:  * Signal handler for the alarm after the login grace period has expired.
                    330:  */
1.327     deraadt   331: /*ARGSUSED*/
1.200     itojun    332: static void
1.64      markus    333: grace_alarm_handler(int sig)
1.1       deraadt   334: {
1.285     dtucker   335:        if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
                    336:                kill(pmonitor->m_pid, SIGALRM);
                    337:
1.64      markus    338:        /* Log error and exit. */
1.346     deraadt   339:        sigdie("Timeout before authentication for %s", get_remote_ipaddr());
1.62      markus    340: }
                    341:
1.65      deraadt   342: /*
                    343:  * Signal handler for the key regeneration alarm.  Note that this
                    344:  * alarm only occurs in the daemon waiting for connections, and it does not
                    345:  * do anything with the private key or random state before forking.
                    346:  * Thus there should be no concurrency control/asynchronous execution
                    347:  * problems.
                    348:  */
1.200     itojun    349: static void
1.174     deraadt   350: generate_ephemeral_server_key(void)
1.134     markus    351: {
1.191     markus    352:        verbose("Generating %s%d bit RSA key.",
1.185     djm       353:            sensitive_data.server_key ? "new " : "", options.server_key_bits);
1.134     markus    354:        if (sensitive_data.server_key != NULL)
                    355:                key_free(sensitive_data.server_key);
1.191     markus    356:        sensitive_data.server_key = key_generate(KEY_RSA1,
1.185     djm       357:            options.server_key_bits);
                    358:        verbose("RSA key generation complete.");
1.169     markus    359:
1.356     djm       360:        arc4random_buf(sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1.134     markus    361:        arc4random_stir();
                    362: }
1.147     deraadt   363:
1.327     deraadt   364: /*ARGSUSED*/
1.200     itojun    365: static void
1.64      markus    366: key_regeneration_alarm(int sig)
1.1       deraadt   367: {
1.64      markus    368:        int save_errno = errno;
1.250     deraadt   369:
1.151     markus    370:        signal(SIGALRM, SIG_DFL);
1.64      markus    371:        errno = save_errno;
1.151     markus    372:        key_do_regen = 1;
1.98      markus    373: }
                    374:
1.200     itojun    375: static void
1.96      markus    376: sshd_exchange_identification(int sock_in, int sock_out)
                    377: {
1.311     djm       378:        u_int i;
                    379:        int mismatch;
1.96      markus    380:        int remote_major, remote_minor;
1.102     markus    381:        int major, minor;
1.96      markus    382:        char *s;
                    383:        char buf[256];                  /* Must not be larger than remote_version. */
                    384:        char remote_version[256];       /* Must be at least as big as buf. */
                    385:
1.103     markus    386:        if ((options.protocol & SSH_PROTO_1) &&
                    387:            (options.protocol & SSH_PROTO_2)) {
1.102     markus    388:                major = PROTOCOL_MAJOR_1;
                    389:                minor = 99;
                    390:        } else if (options.protocol & SSH_PROTO_2) {
                    391:                major = PROTOCOL_MAJOR_2;
                    392:                minor = PROTOCOL_MINOR_2;
                    393:        } else {
                    394:                major = PROTOCOL_MAJOR_1;
                    395:                minor = PROTOCOL_MINOR_1;
                    396:        }
                    397:        snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
1.96      markus    398:        server_version_string = xstrdup(buf);
                    399:
1.272     markus    400:        /* Send our protocol version identification. */
                    401:        if (atomicio(vwrite, sock_out, server_version_string,
                    402:            strlen(server_version_string))
                    403:            != strlen(server_version_string)) {
                    404:                logit("Could not write ident string to %s", get_remote_ipaddr());
1.278     markus    405:                cleanup_exit(255);
1.272     markus    406:        }
                    407:
                    408:        /* Read other sides version identification. */
                    409:        memset(buf, 0, sizeof(buf));
                    410:        for (i = 0; i < sizeof(buf) - 1; i++) {
                    411:                if (atomicio(read, sock_in, &buf[i], 1) != 1) {
                    412:                        logit("Did not receive identification string from %s",
                    413:                            get_remote_ipaddr());
1.278     markus    414:                        cleanup_exit(255);
1.96      markus    415:                }
1.272     markus    416:                if (buf[i] == '\r') {
                    417:                        buf[i] = 0;
                    418:                        /* Kludge for F-Secure Macintosh < 1.0.2 */
                    419:                        if (i == 12 &&
                    420:                            strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
1.96      markus    421:                                break;
1.272     markus    422:                        continue;
                    423:                }
                    424:                if (buf[i] == '\n') {
                    425:                        buf[i] = 0;
                    426:                        break;
1.96      markus    427:                }
                    428:        }
1.272     markus    429:        buf[sizeof(buf) - 1] = 0;
                    430:        client_version_string = xstrdup(buf);
1.96      markus    431:
                    432:        /*
                    433:         * Check that the versions match.  In future this might accept
                    434:         * several versions and set appropriate flags to handle them.
                    435:         */
                    436:        if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
                    437:            &remote_major, &remote_minor, remote_version) != 3) {
1.105     markus    438:                s = "Protocol mismatch.\n";
1.271     deraadt   439:                (void) atomicio(vwrite, sock_out, s, strlen(s));
1.96      markus    440:                close(sock_in);
                    441:                close(sock_out);
1.264     itojun    442:                logit("Bad protocol version identification '%.100s' from %s",
1.96      markus    443:                    client_version_string, get_remote_ipaddr());
1.278     markus    444:                cleanup_exit(255);
1.96      markus    445:        }
                    446:        debug("Client protocol version %d.%d; client software version %.100s",
1.217     deraadt   447:            remote_major, remote_minor, remote_version);
1.96      markus    448:
1.98      markus    449:        compat_datafellows(remote_version);
1.260     mickey    450:
                    451:        if (datafellows & SSH_BUG_PROBE) {
1.264     itojun    452:                logit("probed from %s with %s.  Don't panic.",
1.260     mickey    453:                    get_remote_ipaddr(), client_version_string);
1.278     markus    454:                cleanup_exit(255);
1.260     mickey    455:        }
1.175     deraadt   456:
                    457:        if (datafellows & SSH_BUG_SCANNER) {
1.264     itojun    458:                logit("scanned from %s with %s.  Don't panic.",
1.175     deraadt   459:                    get_remote_ipaddr(), client_version_string);
1.278     markus    460:                cleanup_exit(255);
1.175     deraadt   461:        }
1.98      markus    462:
1.102     markus    463:        mismatch = 0;
1.214     deraadt   464:        switch (remote_major) {
1.96      markus    465:        case 1:
1.108     markus    466:                if (remote_minor == 99) {
                    467:                        if (options.protocol & SSH_PROTO_2)
                    468:                                enable_compat20();
                    469:                        else
                    470:                                mismatch = 1;
                    471:                        break;
                    472:                }
1.102     markus    473:                if (!(options.protocol & SSH_PROTO_1)) {
                    474:                        mismatch = 1;
                    475:                        break;
                    476:                }
1.96      markus    477:                if (remote_minor < 3) {
1.121     provos    478:                        packet_disconnect("Your ssh version is too old and "
1.96      markus    479:                            "is no longer supported.  Please install a newer version.");
                    480:                } else if (remote_minor == 3) {
                    481:                        /* note that this disables agent-forwarding */
                    482:                        enable_compat13();
                    483:                }
1.102     markus    484:                break;
1.98      markus    485:        case 2:
1.102     markus    486:                if (options.protocol & SSH_PROTO_2) {
1.98      markus    487:                        enable_compat20();
                    488:                        break;
                    489:                }
1.99      markus    490:                /* FALLTHROUGH */
1.105     markus    491:        default:
1.102     markus    492:                mismatch = 1;
                    493:                break;
                    494:        }
                    495:        chop(server_version_string);
                    496:        debug("Local version string %.200s", server_version_string);
                    497:
                    498:        if (mismatch) {
1.96      markus    499:                s = "Protocol major versions differ.\n";
1.271     deraadt   500:                (void) atomicio(vwrite, sock_out, s, strlen(s));
1.96      markus    501:                close(sock_in);
                    502:                close(sock_out);
1.264     itojun    503:                logit("Protocol major versions differ for %s: %.200s vs. %.200s",
1.102     markus    504:                    get_remote_ipaddr(),
                    505:                    server_version_string, client_version_string);
1.278     markus    506:                cleanup_exit(255);
1.96      markus    507:        }
1.108     markus    508: }
                    509:
1.134     markus    510: /* Destroy the host and server keys.  They will no longer be needed. */
1.108     markus    511: void
                    512: destroy_sensitive_data(void)
                    513: {
1.134     markus    514:        int i;
                    515:
                    516:        if (sensitive_data.server_key) {
                    517:                key_free(sensitive_data.server_key);
                    518:                sensitive_data.server_key = NULL;
                    519:        }
1.217     deraadt   520:        for (i = 0; i < options.num_host_key_files; i++) {
1.134     markus    521:                if (sensitive_data.host_keys[i]) {
                    522:                        key_free(sensitive_data.host_keys[i]);
                    523:                        sensitive_data.host_keys[i] = NULL;
                    524:                }
                    525:        }
                    526:        sensitive_data.ssh1_host_key = NULL;
1.169     markus    527:        memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
1.134     markus    528: }
                    529:
1.231     provos    530: /* Demote private to public keys for network child */
                    531: void
                    532: demote_sensitive_data(void)
                    533: {
                    534:        Key *tmp;
                    535:        int i;
                    536:
                    537:        if (sensitive_data.server_key) {
                    538:                tmp = key_demote(sensitive_data.server_key);
                    539:                key_free(sensitive_data.server_key);
                    540:                sensitive_data.server_key = tmp;
                    541:        }
                    542:
                    543:        for (i = 0; i < options.num_host_key_files; i++) {
                    544:                if (sensitive_data.host_keys[i]) {
                    545:                        tmp = key_demote(sensitive_data.host_keys[i]);
                    546:                        key_free(sensitive_data.host_keys[i]);
                    547:                        sensitive_data.host_keys[i] = tmp;
                    548:                        if (tmp->type == KEY_RSA1)
                    549:                                sensitive_data.ssh1_host_key = tmp;
                    550:                }
                    551:        }
                    552:
                    553:        /* We do not clear ssh1_host key and cookie.  XXX - Okay Niels? */
                    554: }
                    555:
1.233     markus    556: static void
1.231     provos    557: privsep_preauth_child(void)
                    558: {
1.254     deraadt   559:        u_int32_t rnd[256];
1.253     deraadt   560:        gid_t gidset[1];
1.250     deraadt   561:        struct passwd *pw;
1.231     provos    562:
                    563:        /* Enable challenge-response authentication for privilege separation */
                    564:        privsep_challenge_enable();
                    565:
1.354     djm       566:        arc4random_stir();
1.356     djm       567:        arc4random_buf(rnd, sizeof(rnd));
1.254     deraadt   568:        RAND_seed(rnd, sizeof(rnd));
1.231     provos    569:
                    570:        /* Demote the private keys to public keys. */
                    571:        demote_sensitive_data();
                    572:
1.235     stevesk   573:        if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
1.240     djm       574:                fatal("Privilege separation user %s does not exist",
                    575:                    SSH_PRIVSEP_USER);
1.235     stevesk   576:        memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
                    577:        endpwent();
                    578:
1.255     deraadt   579:        /* Change our root directory */
1.232     stevesk   580:        if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
                    581:                fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
                    582:                    strerror(errno));
1.231     provos    583:        if (chdir("/") == -1)
1.236     stevesk   584:                fatal("chdir(\"/\"): %s", strerror(errno));
1.234     markus    585:
1.231     provos    586:        /* Drop our privileges */
1.235     stevesk   587:        debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
                    588:            (u_int)pw->pw_gid);
1.251     markus    589: #if 0
1.287     djm       590:        /* XXX not ready, too heavy after chroot */
1.235     stevesk   591:        do_setusercontext(pw);
1.251     markus    592: #else
                    593:        gidset[0] = pw->pw_gid;
                    594:        if (setgroups(1, gidset) < 0)
                    595:                fatal("setgroups: %.100s", strerror(errno));
                    596:        permanently_set_uid(pw);
                    597: #endif
1.231     provos    598: }
                    599:
1.278     markus    600: static int
                    601: privsep_preauth(Authctxt *authctxt)
1.237     markus    602: {
                    603:        int status;
                    604:        pid_t pid;
                    605:
                    606:        /* Set up unprivileged child process to deal with network data */
1.242     mouring   607:        pmonitor = monitor_init();
1.237     markus    608:        /* Store a pointer to the kex for later rekeying */
1.242     mouring   609:        pmonitor->m_pkex = &xxx_kex;
1.237     markus    610:
                    611:        pid = fork();
                    612:        if (pid == -1) {
                    613:                fatal("fork of unprivileged child failed");
                    614:        } else if (pid != 0) {
1.245     mpech     615:                debug2("Network child is on pid %ld", (long)pid);
1.237     markus    616:
1.242     mouring   617:                close(pmonitor->m_recvfd);
1.285     dtucker   618:                pmonitor->m_pid = pid;
1.278     markus    619:                monitor_child_preauth(authctxt, pmonitor);
1.242     mouring   620:                close(pmonitor->m_sendfd);
1.237     markus    621:
                    622:                /* Sync memory */
1.242     mouring   623:                monitor_sync(pmonitor);
1.237     markus    624:
                    625:                /* Wait for the child's exit status */
1.239     markus    626:                while (waitpid(pid, &status, 0) < 0)
                    627:                        if (errno != EINTR)
                    628:                                break;
1.278     markus    629:                return (1);
1.237     markus    630:        } else {
                    631:                /* child */
                    632:
1.242     mouring   633:                close(pmonitor->m_sendfd);
1.237     markus    634:
                    635:                /* Demote the child */
                    636:                if (getuid() == 0 || geteuid() == 0)
                    637:                        privsep_preauth_child();
1.238     stevesk   638:                setproctitle("%s", "[net]");
1.237     markus    639:        }
1.278     markus    640:        return (0);
1.237     markus    641: }
                    642:
1.233     markus    643: static void
1.237     markus    644: privsep_postauth(Authctxt *authctxt)
1.231     provos    645: {
1.354     djm       646:        u_int32_t rnd[256];
                    647:
1.231     provos    648:        if (authctxt->pw->pw_uid == 0 || options.use_login) {
                    649:                /* File descriptor passing is broken or root login */
                    650:                use_privsep = 0;
1.315     djm       651:                goto skip;
1.231     provos    652:        }
1.234     markus    653:
1.231     provos    654:        /* New socket pair */
1.242     mouring   655:        monitor_reinit(pmonitor);
1.231     provos    656:
1.242     mouring   657:        pmonitor->m_pid = fork();
                    658:        if (pmonitor->m_pid == -1)
1.231     provos    659:                fatal("fork of unprivileged child failed");
1.242     mouring   660:        else if (pmonitor->m_pid != 0) {
1.245     mpech     661:                debug2("User child is on pid %ld", (long)pmonitor->m_pid);
1.242     mouring   662:                close(pmonitor->m_recvfd);
1.307     otto      663:                buffer_clear(&loginmsg);
1.242     mouring   664:                monitor_child_postauth(pmonitor);
1.231     provos    665:
                    666:                /* NEVERREACHED */
                    667:                exit(0);
                    668:        }
                    669:
1.242     mouring   670:        close(pmonitor->m_sendfd);
1.231     provos    671:
                    672:        /* Demote the private keys to public keys. */
                    673:        demote_sensitive_data();
1.354     djm       674:
                    675:        arc4random_stir();
1.356     djm       676:        arc4random_buf(rnd, sizeof(rnd));
1.354     djm       677:        RAND_seed(rnd, sizeof(rnd));
1.231     provos    678:
                    679:        /* Drop privileges */
                    680:        do_setusercontext(authctxt->pw);
                    681:
1.315     djm       682:  skip:
1.231     provos    683:        /* It is safe now to apply the key state */
1.242     mouring   684:        monitor_apply_keystate(pmonitor);
1.312     markus    685:
                    686:        /*
                    687:         * Tell the packet layer that authentication was successful, since
                    688:         * this information is not part of the key state.
                    689:         */
                    690:        packet_set_authenticated();
1.231     provos    691: }
                    692:
1.200     itojun    693: static char *
1.134     markus    694: list_hostkey_types(void)
                    695: {
1.223     markus    696:        Buffer b;
1.281     jakob     697:        const char *p;
                    698:        char *ret;
1.134     markus    699:        int i;
1.223     markus    700:
                    701:        buffer_init(&b);
1.217     deraadt   702:        for (i = 0; i < options.num_host_key_files; i++) {
1.134     markus    703:                Key *key = sensitive_data.host_keys[i];
                    704:                if (key == NULL)
                    705:                        continue;
1.214     deraadt   706:                switch (key->type) {
1.134     markus    707:                case KEY_RSA:
                    708:                case KEY_DSA:
1.223     markus    709:                        if (buffer_len(&b) > 0)
                    710:                                buffer_append(&b, ",", 1);
                    711:                        p = key_ssh_name(key);
                    712:                        buffer_append(&b, p, strlen(p));
1.134     markus    713:                        break;
                    714:                }
                    715:        }
1.223     markus    716:        buffer_append(&b, "\0", 1);
1.281     jakob     717:        ret = xstrdup(buffer_ptr(&b));
1.223     markus    718:        buffer_free(&b);
1.281     jakob     719:        debug("list_hostkey_types: %s", ret);
                    720:        return ret;
1.134     markus    721: }
                    722:
1.231     provos    723: Key *
1.134     markus    724: get_hostkey_by_type(int type)
                    725: {
                    726:        int i;
1.250     deraadt   727:
1.217     deraadt   728:        for (i = 0; i < options.num_host_key_files; i++) {
1.134     markus    729:                Key *key = sensitive_data.host_keys[i];
                    730:                if (key != NULL && key->type == type)
                    731:                        return key;
                    732:        }
                    733:        return NULL;
1.96      markus    734: }
                    735:
1.231     provos    736: Key *
                    737: get_hostkey_by_index(int ind)
                    738: {
                    739:        if (ind < 0 || ind >= options.num_host_key_files)
                    740:                return (NULL);
                    741:        return (sensitive_data.host_keys[ind]);
                    742: }
                    743:
                    744: int
                    745: get_hostkey_index(Key *key)
                    746: {
                    747:        int i;
1.250     deraadt   748:
1.231     provos    749:        for (i = 0; i < options.num_host_key_files; i++) {
                    750:                if (key == sensitive_data.host_keys[i])
                    751:                        return (i);
                    752:        }
                    753:        return (-1);
                    754: }
                    755:
1.124     markus    756: /*
                    757:  * returns 1 if connection should be dropped, 0 otherwise.
                    758:  * dropping starts at connection #max_startups_begin with a probability
                    759:  * of (max_startups_rate/100). the probability increases linearly until
                    760:  * all connections are dropped for startups > max_startups
                    761:  */
1.200     itojun    762: static int
1.124     markus    763: drop_connection(int startups)
                    764: {
1.303     mickey    765:        int p, r;
1.124     markus    766:
                    767:        if (startups < options.max_startups_begin)
                    768:                return 0;
                    769:        if (startups >= options.max_startups)
                    770:                return 1;
                    771:        if (options.max_startups_rate == 100)
                    772:                return 1;
                    773:
                    774:        p  = 100 - options.max_startups_rate;
                    775:        p *= startups - options.max_startups_begin;
1.303     mickey    776:        p /= options.max_startups - options.max_startups_begin;
1.124     markus    777:        p += options.max_startups_rate;
1.356     djm       778:        r = arc4random_uniform(100);
1.124     markus    779:
1.304     djm       780:        debug("drop_connection: p %d, r %d", p, r);
1.124     markus    781:        return (r < p) ? 1 : 0;
                    782: }
                    783:
1.215     markus    784: static void
                    785: usage(void)
                    786: {
1.290     markus    787:        fprintf(stderr, "%s, %s\n",
1.280     markus    788:            SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.289     markus    789:        fprintf(stderr,
1.359     jmc       790: "usage: sshd [-46DdeiqTt] [-b bits] [-C connection_spec] [-f config_file]\n"
                    791: "            [-g login_grace_time] [-h host_key_file] [-k key_gen_time]\n"
                    792: "            [-o option] [-p port] [-u len]\n"
1.289     markus    793:        );
1.215     markus    794:        exit(1);
                    795: }
                    796:
1.294     djm       797: static void
                    798: send_rexec_state(int fd, Buffer *conf)
                    799: {
                    800:        Buffer m;
                    801:
                    802:        debug3("%s: entering fd = %d config len %d", __func__, fd,
                    803:            buffer_len(conf));
                    804:
                    805:        /*
                    806:         * Protocol from reexec master to child:
                    807:         *      string  configuration
                    808:         *      u_int   ephemeral_key_follows
                    809:         *      bignum  e               (only if ephemeral_key_follows == 1)
                    810:         *      bignum  n                       "
                    811:         *      bignum  d                       "
                    812:         *      bignum  iqmp                    "
                    813:         *      bignum  p                       "
                    814:         *      bignum  q                       "
                    815:         */
                    816:        buffer_init(&m);
                    817:        buffer_put_cstring(&m, buffer_ptr(conf));
                    818:
1.298     deraadt   819:        if (sensitive_data.server_key != NULL &&
1.294     djm       820:            sensitive_data.server_key->type == KEY_RSA1) {
                    821:                buffer_put_int(&m, 1);
                    822:                buffer_put_bignum(&m, sensitive_data.server_key->rsa->e);
                    823:                buffer_put_bignum(&m, sensitive_data.server_key->rsa->n);
                    824:                buffer_put_bignum(&m, sensitive_data.server_key->rsa->d);
                    825:                buffer_put_bignum(&m, sensitive_data.server_key->rsa->iqmp);
                    826:                buffer_put_bignum(&m, sensitive_data.server_key->rsa->p);
                    827:                buffer_put_bignum(&m, sensitive_data.server_key->rsa->q);
                    828:        } else
                    829:                buffer_put_int(&m, 0);
                    830:
                    831:        if (ssh_msg_send(fd, 0, &m) == -1)
                    832:                fatal("%s: ssh_msg_send failed", __func__);
                    833:
                    834:        buffer_free(&m);
                    835:
                    836:        debug3("%s: done", __func__);
                    837: }
                    838:
                    839: static void
                    840: recv_rexec_state(int fd, Buffer *conf)
                    841: {
                    842:        Buffer m;
                    843:        char *cp;
                    844:        u_int len;
                    845:
                    846:        debug3("%s: entering fd = %d", __func__, fd);
                    847:
                    848:        buffer_init(&m);
                    849:
                    850:        if (ssh_msg_recv(fd, &m) == -1)
                    851:                fatal("%s: ssh_msg_recv failed", __func__);
                    852:        if (buffer_get_char(&m) != 0)
                    853:                fatal("%s: rexec version mismatch", __func__);
                    854:
                    855:        cp = buffer_get_string(&m, &len);
                    856:        if (conf != NULL)
                    857:                buffer_append(conf, cp, len + 1);
                    858:        xfree(cp);
                    859:
                    860:        if (buffer_get_int(&m)) {
                    861:                if (sensitive_data.server_key != NULL)
                    862:                        key_free(sensitive_data.server_key);
                    863:                sensitive_data.server_key = key_new_private(KEY_RSA1);
                    864:                buffer_get_bignum(&m, sensitive_data.server_key->rsa->e);
                    865:                buffer_get_bignum(&m, sensitive_data.server_key->rsa->n);
                    866:                buffer_get_bignum(&m, sensitive_data.server_key->rsa->d);
                    867:                buffer_get_bignum(&m, sensitive_data.server_key->rsa->iqmp);
                    868:                buffer_get_bignum(&m, sensitive_data.server_key->rsa->p);
                    869:                buffer_get_bignum(&m, sensitive_data.server_key->rsa->q);
                    870:                rsa_generate_additional_parameters(
                    871:                    sensitive_data.server_key->rsa);
                    872:        }
                    873:        buffer_free(&m);
                    874:
                    875:        debug3("%s: done", __func__);
                    876: }
                    877:
1.345     djm       878: /* Accept a connection from inetd */
                    879: static void
                    880: server_accept_inetd(int *sock_in, int *sock_out)
                    881: {
                    882:        int fd;
                    883:
                    884:        startup_pipe = -1;
                    885:        if (rexeced_flag) {
                    886:                close(REEXEC_CONFIG_PASS_FD);
                    887:                *sock_in = *sock_out = dup(STDIN_FILENO);
                    888:                if (!debug_flag) {
                    889:                        startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
                    890:                        close(REEXEC_STARTUP_PIPE_FD);
                    891:                }
                    892:        } else {
                    893:                *sock_in = dup(STDIN_FILENO);
                    894:                *sock_out = dup(STDOUT_FILENO);
                    895:        }
                    896:        /*
                    897:         * We intentionally do not close the descriptors 0, 1, and 2
                    898:         * as our code for setting the descriptors won't work if
                    899:         * ttyfd happens to be one of those.
                    900:         */
                    901:        if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
                    902:                dup2(fd, STDIN_FILENO);
                    903:                dup2(fd, STDOUT_FILENO);
                    904:                if (fd > STDOUT_FILENO)
                    905:                        close(fd);
                    906:        }
                    907:        debug("inetd sockets after dupping: %d, %d", *sock_in, *sock_out);
                    908: }
                    909:
                    910: /*
                    911:  * Listen for TCP connections
                    912:  */
                    913: static void
                    914: server_listen(void)
                    915: {
                    916:        int ret, listen_sock, on = 1;
                    917:        struct addrinfo *ai;
                    918:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
                    919:
                    920:        for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
                    921:                if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                    922:                        continue;
                    923:                if (num_listen_socks >= MAX_LISTEN_SOCKS)
                    924:                        fatal("Too many listen sockets. "
                    925:                            "Enlarge MAX_LISTEN_SOCKS");
                    926:                if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
                    927:                    ntop, sizeof(ntop), strport, sizeof(strport),
                    928:                    NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
                    929:                        error("getnameinfo failed: %.100s",
1.352     dtucker   930:                            ssh_gai_strerror(ret));
1.345     djm       931:                        continue;
                    932:                }
                    933:                /* Create socket for listening. */
                    934:                listen_sock = socket(ai->ai_family, ai->ai_socktype,
                    935:                    ai->ai_protocol);
                    936:                if (listen_sock < 0) {
                    937:                        /* kernel may not support ipv6 */
                    938:                        verbose("socket: %.100s", strerror(errno));
                    939:                        continue;
                    940:                }
                    941:                if (set_nonblock(listen_sock) == -1) {
                    942:                        close(listen_sock);
                    943:                        continue;
                    944:                }
                    945:                /*
                    946:                 * Set socket options.
                    947:                 * Allow local port reuse in TIME_WAIT.
                    948:                 */
                    949:                if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
                    950:                    &on, sizeof(on)) == -1)
                    951:                        error("setsockopt SO_REUSEADDR: %s", strerror(errno));
                    952:
                    953:                debug("Bind to port %s on %s.", strport, ntop);
                    954:
                    955:                /* Bind the socket to the desired port. */
                    956:                if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
                    957:                        error("Bind to port %s on %s failed: %.200s.",
                    958:                            strport, ntop, strerror(errno));
                    959:                        close(listen_sock);
                    960:                        continue;
                    961:                }
                    962:                listen_socks[num_listen_socks] = listen_sock;
                    963:                num_listen_socks++;
                    964:
                    965:                /* Start listening on the port. */
                    966:                if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0)
                    967:                        fatal("listen on [%s]:%s: %.100s",
                    968:                            ntop, strport, strerror(errno));
                    969:                logit("Server listening on %s port %s.", ntop, strport);
                    970:        }
                    971:        freeaddrinfo(options.listen_addrs);
                    972:
                    973:        if (!num_listen_socks)
                    974:                fatal("Cannot bind any address.");
                    975: }
                    976:
                    977: /*
                    978:  * The main TCP accept loop. Note that, for the non-debug case, returns
                    979:  * from this function are in a forked subprocess.
                    980:  */
                    981: static void
                    982: server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
                    983: {
                    984:        fd_set *fdset;
                    985:        int i, j, ret, maxfd;
                    986:        int key_used = 0, startups = 0;
                    987:        int startup_p[2] = { -1 , -1 };
                    988:        struct sockaddr_storage from;
                    989:        socklen_t fromlen;
                    990:        pid_t pid;
                    991:
                    992:        /* setup fd set for accept */
                    993:        fdset = NULL;
                    994:        maxfd = 0;
                    995:        for (i = 0; i < num_listen_socks; i++)
                    996:                if (listen_socks[i] > maxfd)
                    997:                        maxfd = listen_socks[i];
                    998:        /* pipes connected to unauthenticated childs */
                    999:        startup_pipes = xcalloc(options.max_startups, sizeof(int));
                   1000:        for (i = 0; i < options.max_startups; i++)
                   1001:                startup_pipes[i] = -1;
                   1002:
                   1003:        /*
                   1004:         * Stay listening for connections until the system crashes or
                   1005:         * the daemon is killed with a signal.
                   1006:         */
                   1007:        for (;;) {
                   1008:                if (received_sighup)
                   1009:                        sighup_restart();
                   1010:                if (fdset != NULL)
                   1011:                        xfree(fdset);
                   1012:                fdset = (fd_set *)xcalloc(howmany(maxfd + 1, NFDBITS),
                   1013:                    sizeof(fd_mask));
                   1014:
                   1015:                for (i = 0; i < num_listen_socks; i++)
                   1016:                        FD_SET(listen_socks[i], fdset);
                   1017:                for (i = 0; i < options.max_startups; i++)
                   1018:                        if (startup_pipes[i] != -1)
                   1019:                                FD_SET(startup_pipes[i], fdset);
                   1020:
                   1021:                /* Wait in select until there is a connection. */
                   1022:                ret = select(maxfd+1, fdset, NULL, NULL, NULL);
                   1023:                if (ret < 0 && errno != EINTR)
                   1024:                        error("select: %.100s", strerror(errno));
                   1025:                if (received_sigterm) {
                   1026:                        logit("Received signal %d; terminating.",
                   1027:                            (int) received_sigterm);
                   1028:                        close_listen_socks();
                   1029:                        unlink(options.pid_file);
                   1030:                        exit(255);
                   1031:                }
                   1032:                if (key_used && key_do_regen) {
                   1033:                        generate_ephemeral_server_key();
                   1034:                        key_used = 0;
                   1035:                        key_do_regen = 0;
                   1036:                }
                   1037:                if (ret < 0)
                   1038:                        continue;
                   1039:
                   1040:                for (i = 0; i < options.max_startups; i++)
                   1041:                        if (startup_pipes[i] != -1 &&
                   1042:                            FD_ISSET(startup_pipes[i], fdset)) {
                   1043:                                /*
                   1044:                                 * the read end of the pipe is ready
                   1045:                                 * if the child has closed the pipe
                   1046:                                 * after successful authentication
                   1047:                                 * or if the child has died
                   1048:                                 */
                   1049:                                close(startup_pipes[i]);
                   1050:                                startup_pipes[i] = -1;
                   1051:                                startups--;
                   1052:                        }
                   1053:                for (i = 0; i < num_listen_socks; i++) {
                   1054:                        if (!FD_ISSET(listen_socks[i], fdset))
                   1055:                                continue;
                   1056:                        fromlen = sizeof(from);
                   1057:                        *newsock = accept(listen_socks[i],
                   1058:                            (struct sockaddr *)&from, &fromlen);
                   1059:                        if (*newsock < 0) {
                   1060:                                if (errno != EINTR && errno != EWOULDBLOCK)
                   1061:                                        error("accept: %.100s", strerror(errno));
                   1062:                                continue;
                   1063:                        }
                   1064:                        if (unset_nonblock(*newsock) == -1) {
                   1065:                                close(*newsock);
                   1066:                                continue;
                   1067:                        }
                   1068:                        if (drop_connection(startups) == 1) {
                   1069:                                debug("drop connection #%d", startups);
                   1070:                                close(*newsock);
                   1071:                                continue;
                   1072:                        }
                   1073:                        if (pipe(startup_p) == -1) {
                   1074:                                close(*newsock);
                   1075:                                continue;
                   1076:                        }
                   1077:
                   1078:                        if (rexec_flag && socketpair(AF_UNIX,
                   1079:                            SOCK_STREAM, 0, config_s) == -1) {
                   1080:                                error("reexec socketpair: %s",
                   1081:                                    strerror(errno));
                   1082:                                close(*newsock);
                   1083:                                close(startup_p[0]);
                   1084:                                close(startup_p[1]);
                   1085:                                continue;
                   1086:                        }
                   1087:
                   1088:                        for (j = 0; j < options.max_startups; j++)
                   1089:                                if (startup_pipes[j] == -1) {
                   1090:                                        startup_pipes[j] = startup_p[0];
                   1091:                                        if (maxfd < startup_p[0])
                   1092:                                                maxfd = startup_p[0];
                   1093:                                        startups++;
                   1094:                                        break;
                   1095:                                }
                   1096:
                   1097:                        /*
                   1098:                         * Got connection.  Fork a child to handle it, unless
                   1099:                         * we are in debugging mode.
                   1100:                         */
                   1101:                        if (debug_flag) {
                   1102:                                /*
                   1103:                                 * In debugging mode.  Close the listening
                   1104:                                 * socket, and start processing the
                   1105:                                 * connection without forking.
                   1106:                                 */
                   1107:                                debug("Server will not fork when running in debugging mode.");
                   1108:                                close_listen_socks();
                   1109:                                *sock_in = *newsock;
                   1110:                                *sock_out = *newsock;
                   1111:                                close(startup_p[0]);
                   1112:                                close(startup_p[1]);
                   1113:                                startup_pipe = -1;
                   1114:                                pid = getpid();
                   1115:                                if (rexec_flag) {
                   1116:                                        send_rexec_state(config_s[0],
                   1117:                                            &cfg);
                   1118:                                        close(config_s[0]);
                   1119:                                }
                   1120:                                break;
                   1121:                        }
                   1122:
                   1123:                        /*
                   1124:                         * Normal production daemon.  Fork, and have
                   1125:                         * the child process the connection. The
                   1126:                         * parent continues listening.
                   1127:                         */
                   1128:                        if ((pid = fork()) == 0) {
                   1129:                                /*
                   1130:                                 * Child.  Close the listening and
                   1131:                                 * max_startup sockets.  Start using
                   1132:                                 * the accepted socket. Reinitialize
                   1133:                                 * logging (since our pid has changed).
                   1134:                                 * We break out of the loop to handle
                   1135:                                 * the connection.
                   1136:                                 */
                   1137:                                startup_pipe = startup_p[1];
                   1138:                                close_startup_pipes();
                   1139:                                close_listen_socks();
                   1140:                                *sock_in = *newsock;
                   1141:                                *sock_out = *newsock;
                   1142:                                log_init(__progname,
                   1143:                                    options.log_level,
                   1144:                                    options.log_facility,
                   1145:                                    log_stderr);
                   1146:                                if (rexec_flag)
                   1147:                                        close(config_s[0]);
                   1148:                                break;
                   1149:                        }
                   1150:
                   1151:                        /* Parent.  Stay in the loop. */
                   1152:                        if (pid < 0)
                   1153:                                error("fork: %.100s", strerror(errno));
                   1154:                        else
                   1155:                                debug("Forked child %ld.", (long)pid);
                   1156:
                   1157:                        close(startup_p[1]);
                   1158:
                   1159:                        if (rexec_flag) {
                   1160:                                send_rexec_state(config_s[0], &cfg);
                   1161:                                close(config_s[0]);
                   1162:                                close(config_s[1]);
                   1163:                        }
                   1164:
                   1165:                        /*
                   1166:                         * Mark that the key has been used (it
                   1167:                         * was "given" to the child).
                   1168:                         */
                   1169:                        if ((options.protocol & SSH_PROTO_1) &&
                   1170:                            key_used == 0) {
                   1171:                                /* Schedule server key regeneration alarm. */
                   1172:                                signal(SIGALRM, key_regeneration_alarm);
                   1173:                                alarm(options.key_regeneration_time);
                   1174:                                key_used = 1;
                   1175:                        }
                   1176:
                   1177:                        close(*newsock);
                   1178:
                   1179:                        /*
                   1180:                         * Ensure that our random state differs
                   1181:                         * from that of the child
                   1182:                         */
                   1183:                        arc4random_stir();
                   1184:                }
                   1185:
                   1186:                /* child process check (or debug mode) */
                   1187:                if (num_listen_socks < 0)
                   1188:                        break;
                   1189:        }
                   1190: }
                   1191:
                   1192:
1.65      deraadt  1193: /*
                   1194:  * Main program for the daemon.
                   1195:  */
1.2       provos   1196: int
                   1197: main(int ac, char **av)
1.1       deraadt  1198: {
1.64      markus   1199:        extern char *optarg;
                   1200:        extern int optind;
1.345     djm      1201:        int opt, i, on = 1;
1.297     avsm     1202:        int sock_in = -1, sock_out = -1, newsock = -1;
1.64      markus   1203:        const char *remote_ip;
1.358     dtucker  1204:        char *test_user = NULL, *test_host = NULL, *test_addr = NULL;
1.64      markus   1205:        int remote_port;
1.358     dtucker  1206:        char *line, *p, *cp;
1.345     djm      1207:        int config_s[2] = { -1 , -1 };
1.278     markus   1208:        Key *key;
1.230     provos   1209:        Authctxt *authctxt;
1.64      markus   1210:
1.138     markus   1211:        /* Save argv. */
1.64      markus   1212:        saved_argv = av;
1.294     djm      1213:        rexec_argc = ac;
1.313     djm      1214:
                   1215:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                   1216:        sanitise_stdfd();
1.64      markus   1217:
                   1218:        /* Initialize configuration options to their default values. */
                   1219:        initialize_server_options(&options);
                   1220:
                   1221:        /* Parse command-line arguments. */
1.358     dtucker  1222:        while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:C:dDeiqrtQRT46")) != -1) {
1.64      markus   1223:                switch (opt) {
1.75      markus   1224:                case '4':
1.305     djm      1225:                        options.address_family = AF_INET;
1.75      markus   1226:                        break;
                   1227:                case '6':
1.305     djm      1228:                        options.address_family = AF_INET6;
1.75      markus   1229:                        break;
1.64      markus   1230:                case 'f':
                   1231:                        config_file_name = optarg;
                   1232:                        break;
                   1233:                case 'd':
1.273     markus   1234:                        if (debug_flag == 0) {
1.127     markus   1235:                                debug_flag = 1;
                   1236:                                options.log_level = SYSLOG_LEVEL_DEBUG1;
1.273     markus   1237:                        } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
1.127     markus   1238:                                options.log_level++;
1.64      markus   1239:                        break;
1.135     markus   1240:                case 'D':
                   1241:                        no_daemon_flag = 1;
1.192     lebel    1242:                        break;
                   1243:                case 'e':
                   1244:                        log_stderr = 1;
1.135     markus   1245:                        break;
1.64      markus   1246:                case 'i':
                   1247:                        inetd_flag = 1;
                   1248:                        break;
1.294     djm      1249:                case 'r':
                   1250:                        rexec_flag = 0;
                   1251:                        break;
                   1252:                case 'R':
                   1253:                        rexeced_flag = 1;
                   1254:                        inetd_flag = 1;
                   1255:                        break;
1.64      markus   1256:                case 'Q':
1.158     markus   1257:                        /* ignored */
1.64      markus   1258:                        break;
                   1259:                case 'q':
                   1260:                        options.log_level = SYSLOG_LEVEL_QUIET;
                   1261:                        break;
                   1262:                case 'b':
1.327     deraadt  1263:                        options.server_key_bits = (int)strtonum(optarg, 256,
                   1264:                            32768, NULL);
1.64      markus   1265:                        break;
                   1266:                case 'p':
1.75      markus   1267:                        options.ports_from_cmdline = 1;
1.127     markus   1268:                        if (options.num_ports >= MAX_PORTS) {
                   1269:                                fprintf(stderr, "too many ports.\n");
                   1270:                                exit(1);
                   1271:                        }
1.193     stevesk  1272:                        options.ports[options.num_ports++] = a2port(optarg);
                   1273:                        if (options.ports[options.num_ports-1] == 0) {
                   1274:                                fprintf(stderr, "Bad port number.\n");
                   1275:                                exit(1);
                   1276:                        }
1.64      markus   1277:                        break;
                   1278:                case 'g':
1.197     stevesk  1279:                        if ((options.login_grace_time = convtime(optarg)) == -1) {
                   1280:                                fprintf(stderr, "Invalid login grace time.\n");
                   1281:                                exit(1);
                   1282:                        }
1.64      markus   1283:                        break;
                   1284:                case 'k':
1.197     stevesk  1285:                        if ((options.key_regeneration_time = convtime(optarg)) == -1) {
                   1286:                                fprintf(stderr, "Invalid key regeneration interval.\n");
                   1287:                                exit(1);
                   1288:                        }
1.64      markus   1289:                        break;
                   1290:                case 'h':
1.134     markus   1291:                        if (options.num_host_key_files >= MAX_HOSTKEYS) {
                   1292:                                fprintf(stderr, "too many host keys.\n");
                   1293:                                exit(1);
                   1294:                        }
                   1295:                        options.host_key_files[options.num_host_key_files++] = optarg;
1.64      markus   1296:                        break;
1.203     stevesk  1297:                case 't':
                   1298:                        test_flag = 1;
                   1299:                        break;
1.358     dtucker  1300:                case 'T':
                   1301:                        test_flag = 2;
                   1302:                        break;
                   1303:                case 'C':
                   1304:                        cp = optarg;
                   1305:                        while ((p = strsep(&cp, ",")) && *p != '\0') {
                   1306:                                if (strncmp(p, "addr=", 5) == 0)
                   1307:                                        test_addr = xstrdup(p + 5);
                   1308:                                else if (strncmp(p, "host=", 5) == 0)
                   1309:                                        test_host = xstrdup(p + 5);
                   1310:                                else if (strncmp(p, "user=", 5) == 0)
                   1311:                                        test_user = xstrdup(p + 5);
                   1312:                                else {
                   1313:                                        fprintf(stderr, "Invalid test "
                   1314:                                            "mode specification %s\n", p);
                   1315:                                        exit(1);
                   1316:                                }
                   1317:                        }
                   1318:                        break;
1.125     markus   1319:                case 'u':
1.327     deraadt  1320:                        utmp_len = (u_int)strtonum(optarg, 0, MAXHOSTNAMELEN+1, NULL);
1.257     stevesk  1321:                        if (utmp_len > MAXHOSTNAMELEN) {
                   1322:                                fprintf(stderr, "Invalid utmp length.\n");
                   1323:                                exit(1);
                   1324:                        }
1.125     markus   1325:                        break;
1.215     markus   1326:                case 'o':
1.283     markus   1327:                        line = xstrdup(optarg);
                   1328:                        if (process_server_config_line(&options, line,
1.337     dtucker  1329:                            "command-line", 0, NULL, NULL, NULL, NULL) != 0)
1.217     deraadt  1330:                                exit(1);
1.283     markus   1331:                        xfree(line);
1.215     markus   1332:                        break;
1.64      markus   1333:                case '?':
                   1334:                default:
1.215     markus   1335:                        usage();
                   1336:                        break;
1.64      markus   1337:                }
                   1338:        }
1.294     djm      1339:        if (rexeced_flag || inetd_flag)
                   1340:                rexec_flag = 0;
1.355     mbalmer  1341:        if (!test_flag && (rexec_flag && (av[0] == NULL || *av[0] != '/')))
1.294     djm      1342:                fatal("sshd re-exec requires execution with an absolute path");
                   1343:        if (rexeced_flag)
1.296     djm      1344:                closefrom(REEXEC_MIN_FREE_FD);
                   1345:        else
                   1346:                closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
1.294     djm      1347:
1.180     markus   1348:        SSLeay_add_all_algorithms();
1.64      markus   1349:
1.75      markus   1350:        /*
                   1351:         * Force logging to stderr until we have loaded the private host
                   1352:         * key (unless started from inetd)
                   1353:         */
1.138     markus   1354:        log_init(__progname,
1.224     markus   1355:            options.log_level == SYSLOG_LEVEL_NOT_SET ?
                   1356:            SYSLOG_LEVEL_INFO : options.log_level,
                   1357:            options.log_facility == SYSLOG_FACILITY_NOT_SET ?
                   1358:            SYSLOG_FACILITY_AUTH : options.log_facility,
1.261     markus   1359:            log_stderr || !inetd_flag);
1.75      markus   1360:
1.294     djm      1361:        sensitive_data.server_key = NULL;
                   1362:        sensitive_data.ssh1_host_key = NULL;
                   1363:        sensitive_data.have_ssh1_key = 0;
                   1364:        sensitive_data.have_ssh2_key = 0;
                   1365:
1.358     dtucker  1366:        /*
                   1367:         * If we're doing an extended config test, make sure we have all of
                   1368:         * the parameters we need.  If we're not doing an extended test,
                   1369:         * do not silently ignore connection test params.
                   1370:         */
1.361   ! dtucker  1371:        if (test_flag >= 2 &&
        !          1372:           (test_user != NULL || test_host != NULL || test_addr != NULL)
1.358     dtucker  1373:            && (test_user == NULL || test_host == NULL || test_addr == NULL))
                   1374:                fatal("user, host and addr are all required when testing "
                   1375:                   "Match configs");
                   1376:        if (test_flag < 2 && (test_user != NULL || test_host != NULL ||
                   1377:            test_addr != NULL))
                   1378:                fatal("Config test connection parameter (-C) provided without "
                   1379:                   "test mode (-T)");
                   1380:
1.294     djm      1381:        /* Fetch our configuration */
                   1382:        buffer_init(&cfg);
                   1383:        if (rexeced_flag)
1.296     djm      1384:                recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg);
1.294     djm      1385:        else
                   1386:                load_server_config(config_file_name, &cfg);
                   1387:
1.337     dtucker  1388:        parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name,
                   1389:            &cfg, NULL, NULL, NULL);
1.64      markus   1390:
                   1391:        /* Fill in default values for those options not explicitly set. */
                   1392:        fill_default_server_options(&options);
1.350     dtucker  1393:
                   1394:        /* challenge-response is implemented via keyboard interactive */
                   1395:        if (options.challenge_response_authentication)
                   1396:                options.kbd_interactive_authentication = 1;
1.305     djm      1397:
                   1398:        /* set default channel AF */
                   1399:        channel_set_af(options.address_family);
1.64      markus   1400:
                   1401:        /* Check that there are no remaining arguments. */
                   1402:        if (optind < ac) {
                   1403:                fprintf(stderr, "Extra argument %s.\n", av[optind]);
                   1404:                exit(1);
                   1405:        }
                   1406:
                   1407:        debug("sshd version %.100s", SSH_VERSION);
                   1408:
1.134     markus   1409:        /* load private host keys */
1.329     djm      1410:        sensitive_data.host_keys = xcalloc(options.num_host_key_files,
1.255     deraadt  1411:            sizeof(Key *));
1.217     deraadt  1412:        for (i = 0; i < options.num_host_key_files; i++)
1.141     markus   1413:                sensitive_data.host_keys[i] = NULL;
1.134     markus   1414:
1.217     deraadt  1415:        for (i = 0; i < options.num_host_key_files; i++) {
1.179     markus   1416:                key = key_load_private(options.host_key_files[i], "", NULL);
                   1417:                sensitive_data.host_keys[i] = key;
1.134     markus   1418:                if (key == NULL) {
1.195     markus   1419:                        error("Could not load host key: %s",
                   1420:                            options.host_key_files[i]);
1.179     markus   1421:                        sensitive_data.host_keys[i] = NULL;
1.134     markus   1422:                        continue;
                   1423:                }
1.214     deraadt  1424:                switch (key->type) {
1.134     markus   1425:                case KEY_RSA1:
                   1426:                        sensitive_data.ssh1_host_key = key;
                   1427:                        sensitive_data.have_ssh1_key = 1;
                   1428:                        break;
                   1429:                case KEY_RSA:
                   1430:                case KEY_DSA:
                   1431:                        sensitive_data.have_ssh2_key = 1;
                   1432:                        break;
                   1433:                }
1.179     markus   1434:                debug("private host key: #%d type %d %s", i, key->type,
                   1435:                    key_type(key));
1.134     markus   1436:        }
                   1437:        if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
1.264     itojun   1438:                logit("Disabling protocol version 1. Could not load host key");
1.108     markus   1439:                options.protocol &= ~SSH_PROTO_1;
                   1440:        }
1.134     markus   1441:        if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
1.264     itojun   1442:                logit("Disabling protocol version 2. Could not load host key");
1.134     markus   1443:                options.protocol &= ~SSH_PROTO_2;
1.108     markus   1444:        }
1.162     stevesk  1445:        if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
1.264     itojun   1446:                logit("sshd: no hostkeys available -- exiting.");
1.64      markus   1447:                exit(1);
                   1448:        }
                   1449:
1.108     markus   1450:        /* Check certain values for sanity. */
                   1451:        if (options.protocol & SSH_PROTO_1) {
                   1452:                if (options.server_key_bits < 512 ||
                   1453:                    options.server_key_bits > 32768) {
                   1454:                        fprintf(stderr, "Bad server key size.\n");
                   1455:                        exit(1);
                   1456:                }
                   1457:                /*
                   1458:                 * Check that server and host key lengths differ sufficiently. This
                   1459:                 * is necessary to make double encryption work with rsaref. Oh, I
                   1460:                 * hate software patents. I dont know if this can go? Niels
                   1461:                 */
                   1462:                if (options.server_key_bits >
1.250     deraadt  1463:                    BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
                   1464:                    SSH_KEY_BITS_RESERVED && options.server_key_bits <
                   1465:                    BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
                   1466:                    SSH_KEY_BITS_RESERVED) {
1.108     markus   1467:                        options.server_key_bits =
1.250     deraadt  1468:                            BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
                   1469:                            SSH_KEY_BITS_RESERVED;
1.108     markus   1470:                        debug("Forcing server key to %d bits to make it differ from host key.",
                   1471:                            options.server_key_bits);
                   1472:                }
1.244     markus   1473:        }
                   1474:
                   1475:        if (use_privsep) {
                   1476:                struct stat st;
                   1477:
1.327     deraadt  1478:                if (getpwnam(SSH_PRIVSEP_USER) == NULL)
1.244     markus   1479:                        fatal("Privilege separation user %s does not exist",
                   1480:                            SSH_PRIVSEP_USER);
                   1481:                if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
                   1482:                    (S_ISDIR(st.st_mode) == 0))
                   1483:                        fatal("Missing privilege separation directory: %s",
1.247     stevesk  1484:                            _PATH_PRIVSEP_CHROOT_DIR);
                   1485:                if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
1.262     markus   1486:                        fatal("%s must be owned by root and not group or "
                   1487:                            "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
1.358     dtucker  1488:        }
                   1489:
                   1490:        if (test_flag > 1) {
                   1491:                if (test_user != NULL && test_addr != NULL && test_host != NULL)
                   1492:                        parse_server_match_config(&options, test_user,
                   1493:                            test_host, test_addr);
                   1494:                dump_config(&options);
1.108     markus   1495:        }
1.203     stevesk  1496:
                   1497:        /* Configuration looks good, so exit if in test mode. */
                   1498:        if (test_flag)
                   1499:                exit(0);
1.108     markus   1500:
1.294     djm      1501:        if (rexec_flag) {
1.329     djm      1502:                rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
1.294     djm      1503:                for (i = 0; i < rexec_argc; i++) {
                   1504:                        debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
                   1505:                        rexec_argv[i] = saved_argv[i];
                   1506:                }
                   1507:                rexec_argv[rexec_argc] = "-R";
                   1508:                rexec_argv[rexec_argc + 1] = NULL;
                   1509:        }
                   1510:
1.108     markus   1511:        /* Initialize the log (it is reinitialized below in case we forked). */
1.306     dtucker  1512:        if (debug_flag && (!inetd_flag || rexeced_flag))
1.64      markus   1513:                log_stderr = 1;
1.138     markus   1514:        log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64      markus   1515:
1.108     markus   1516:        /*
                   1517:         * If not in debugging mode, and not started from inetd, disconnect
                   1518:         * from the controlling terminal, and fork.  The original process
                   1519:         * exits.
                   1520:         */
1.135     markus   1521:        if (!(debug_flag || inetd_flag || no_daemon_flag)) {
1.64      markus   1522:                int fd;
1.345     djm      1523:
1.64      markus   1524:                if (daemon(0, 0) < 0)
                   1525:                        fatal("daemon() failed: %.200s", strerror(errno));
                   1526:
                   1527:                /* Disconnect from the controlling tty. */
1.165     itojun   1528:                fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
1.64      markus   1529:                if (fd >= 0) {
                   1530:                        (void) ioctl(fd, TIOCNOTTY, NULL);
                   1531:                        close(fd);
                   1532:                }
                   1533:        }
                   1534:        /* Reinitialize the log (because of the fork above). */
1.138     markus   1535:        log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64      markus   1536:
                   1537:        /* Initialize the random number generator. */
                   1538:        arc4random_stir();
                   1539:
                   1540:        /* Chdir to the root directory so that the current disk can be
                   1541:           unmounted if desired. */
                   1542:        chdir("/");
1.217     deraadt  1543:
1.178     markus   1544:        /* ignore SIGPIPE */
                   1545:        signal(SIGPIPE, SIG_IGN);
1.64      markus   1546:
1.345     djm      1547:        /* Get a connection, either from inetd or a listening TCP socket */
1.64      markus   1548:        if (inetd_flag) {
1.345     djm      1549:                server_accept_inetd(&sock_in, &sock_out);
1.64      markus   1550:        } else {
1.345     djm      1551:                server_listen();
1.75      markus   1552:
1.201     markus   1553:                if (options.protocol & SSH_PROTO_1)
                   1554:                        generate_ephemeral_server_key();
                   1555:
                   1556:                signal(SIGHUP, sighup_handler);
1.345     djm      1557:                signal(SIGCHLD, main_sigchld_handler);
1.201     markus   1558:                signal(SIGTERM, sigterm_handler);
                   1559:                signal(SIGQUIT, sigterm_handler);
                   1560:
1.345     djm      1561:                /*
                   1562:                 * Write out the pid file after the sigterm handler
                   1563:                 * is setup and the listen sockets are bound
                   1564:                 */
                   1565:                if (!debug_flag) {
                   1566:                        FILE *f = fopen(options.pid_file, "w");
1.201     markus   1567:
1.270     djm      1568:                        if (f == NULL) {
                   1569:                                error("Couldn't create pid file \"%s\": %s",
                   1570:                                    options.pid_file, strerror(errno));
                   1571:                        } else {
1.245     mpech    1572:                                fprintf(f, "%ld\n", (long) getpid());
1.64      markus   1573:                                fclose(f);
                   1574:                        }
                   1575:                }
                   1576:
1.345     djm      1577:                /* Accept a connection and return in a forked child */
                   1578:                server_accept_loop(&sock_in, &sock_out,
                   1579:                    &newsock, config_s);
1.1       deraadt  1580:        }
                   1581:
1.64      markus   1582:        /* This is the child processing a new connection. */
1.288     markus   1583:        setproctitle("%s", "[accepted]");
1.294     djm      1584:
1.300     markus   1585:        /*
                   1586:         * Create a new session and process group since the 4.4BSD
                   1587:         * setlogin() affects the entire process group.  We don't
                   1588:         * want the child to be able to affect the parent.
                   1589:         */
                   1590:        if (!debug_flag && !inetd_flag && setsid() < 0)
                   1591:                error("setsid: %.100s", strerror(errno));
                   1592:
1.294     djm      1593:        if (rexec_flag) {
                   1594:                int fd;
                   1595:
1.296     djm      1596:                debug("rexec start in %d out %d newsock %d pipe %d sock %d",
                   1597:                    sock_in, sock_out, newsock, startup_pipe, config_s[0]);
1.294     djm      1598:                dup2(newsock, STDIN_FILENO);
                   1599:                dup2(STDIN_FILENO, STDOUT_FILENO);
                   1600:                if (startup_pipe == -1)
1.296     djm      1601:                        close(REEXEC_STARTUP_PIPE_FD);
1.294     djm      1602:                else
1.296     djm      1603:                        dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
1.294     djm      1604:
1.296     djm      1605:                dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
1.294     djm      1606:                close(config_s[1]);
1.301     dtucker  1607:                if (startup_pipe != -1)
                   1608:                        close(startup_pipe);
1.296     djm      1609:
1.294     djm      1610:                execv(rexec_argv[0], rexec_argv);
                   1611:
                   1612:                /* Reexec has failed, fall back and continue */
                   1613:                error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
1.296     djm      1614:                recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
1.294     djm      1615:                log_init(__progname, options.log_level,
                   1616:                    options.log_facility, log_stderr);
                   1617:
                   1618:                /* Clean up fds */
1.296     djm      1619:                startup_pipe = REEXEC_STARTUP_PIPE_FD;
1.294     djm      1620:                close(config_s[1]);
1.296     djm      1621:                close(REEXEC_CONFIG_PASS_FD);
                   1622:                newsock = sock_out = sock_in = dup(STDIN_FILENO);
1.294     djm      1623:                if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
                   1624:                        dup2(fd, STDIN_FILENO);
                   1625:                        dup2(fd, STDOUT_FILENO);
                   1626:                        if (fd > STDERR_FILENO)
                   1627:                                close(fd);
                   1628:                }
1.296     djm      1629:                debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
                   1630:                    sock_in, sock_out, newsock, startup_pipe, config_s[0]);
1.294     djm      1631:        }
1.64      markus   1632:
1.66      markus   1633:        /*
                   1634:         * Disable the key regeneration alarm.  We will not regenerate the
                   1635:         * key since we are no longer in a position to give it to anyone. We
                   1636:         * will not restart on SIGHUP since it no longer makes sense.
                   1637:         */
1.64      markus   1638:        alarm(0);
                   1639:        signal(SIGALRM, SIG_DFL);
                   1640:        signal(SIGHUP, SIG_DFL);
                   1641:        signal(SIGTERM, SIG_DFL);
                   1642:        signal(SIGQUIT, SIG_DFL);
                   1643:        signal(SIGCHLD, SIG_DFL);
1.150     markus   1644:
1.66      markus   1645:        /*
                   1646:         * Register our connection.  This turns encryption off because we do
                   1647:         * not have a key.
                   1648:         */
1.64      markus   1649:        packet_set_connection(sock_in, sock_out);
1.312     markus   1650:        packet_set_server();
1.309     djm      1651:
                   1652:        /* Set SO_KEEPALIVE if requested. */
                   1653:        if (options.tcp_keep_alive && packet_connection_is_on_socket() &&
                   1654:            setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
                   1655:                error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.1       deraadt  1656:
1.310     markus   1657:        if ((remote_port = get_remote_port()) < 0) {
                   1658:                debug("get_remote_port failed");
                   1659:                cleanup_exit(255);
                   1660:        }
1.316     dtucker  1661:
                   1662:        /*
                   1663:         * We use get_canonical_hostname with usedns = 0 instead of
                   1664:         * get_remote_ipaddr here so IP options will be checked.
                   1665:         */
1.331     markus   1666:        (void) get_canonical_hostname(0);
                   1667:        /*
                   1668:         * The rest of the code depends on the fact that
                   1669:         * get_remote_ipaddr() caches the remote ip, even if
                   1670:         * the socket goes away.
                   1671:         */
                   1672:        remote_ip = get_remote_ipaddr();
1.52      markus   1673:
1.209     markus   1674: #ifdef LIBWRAP
1.64      markus   1675:        /* Check whether logins are denied from this host. */
1.295     djm      1676:        if (packet_connection_is_on_socket()) {
1.64      markus   1677:                struct request_info req;
1.37      dugsong  1678:
1.204     camield  1679:                request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
1.64      markus   1680:                fromhost(&req);
1.37      dugsong  1681:
1.64      markus   1682:                if (!hosts_access(&req)) {
1.209     markus   1683:                        debug("Connection refused by tcp wrapper");
1.182     markus   1684:                        refuse(&req);
1.209     markus   1685:                        /* NOTREACHED */
                   1686:                        fatal("libwrap refuse returns");
1.64      markus   1687:                }
                   1688:        }
1.75      markus   1689: #endif /* LIBWRAP */
1.209     markus   1690:
1.64      markus   1691:        /* Log the connection. */
                   1692:        verbose("Connection from %.500s port %d", remote_ip, remote_port);
1.1       deraadt  1693:
1.66      markus   1694:        /*
1.317     djm      1695:         * We don't want to listen forever unless the other side
1.66      markus   1696:         * successfully authenticates itself.  So we set up an alarm which is
                   1697:         * cleared after successful authentication.  A limit of zero
1.317     djm      1698:         * indicates no limit. Note that we don't set the alarm in debugging
1.66      markus   1699:         * mode; it is just annoying to have the server exit just when you
                   1700:         * are about to discover the bug.
                   1701:         */
1.64      markus   1702:        signal(SIGALRM, grace_alarm_handler);
                   1703:        if (!debug_flag)
                   1704:                alarm(options.login_grace_time);
                   1705:
1.96      markus   1706:        sshd_exchange_identification(sock_in, sock_out);
1.353     dtucker  1707:
                   1708:        /* In inetd mode, generate ephemeral key only for proto 1 connections */
                   1709:        if (!compat20 && inetd_flag && sensitive_data.server_key == NULL)
                   1710:                generate_ephemeral_server_key();
1.275     markus   1711:
1.64      markus   1712:        packet_set_nonblocking();
1.1       deraadt  1713:
1.278     markus   1714:        /* allocate authentication context */
1.329     djm      1715:        authctxt = xcalloc(1, sizeof(*authctxt));
1.278     markus   1716:
                   1717:        /* XXX global for cleanup, access from other modules */
                   1718:        the_authctxt = authctxt;
                   1719:
1.307     otto     1720:        /* prepare buffer to collect messages to display to user after login */
                   1721:        buffer_init(&loginmsg);
                   1722:
1.237     markus   1723:        if (use_privsep)
1.278     markus   1724:                if (privsep_preauth(authctxt) == 1)
1.237     markus   1725:                        goto authenticated;
1.231     provos   1726:
1.77      markus   1727:        /* perform the key exchange */
                   1728:        /* authenticate user and start session */
1.98      markus   1729:        if (compat20) {
                   1730:                do_ssh2_kex();
1.278     markus   1731:                do_authentication2(authctxt);
1.98      markus   1732:        } else {
                   1733:                do_ssh1_kex();
1.278     markus   1734:                do_authentication(authctxt);
1.98      markus   1735:        }
1.237     markus   1736:        /*
                   1737:         * If we use privilege separation, the unprivileged child transfers
                   1738:         * the current keystate and exits
                   1739:         */
                   1740:        if (use_privsep) {
1.242     mouring  1741:                mm_send_keystate(pmonitor);
1.231     provos   1742:                exit(0);
1.237     markus   1743:        }
1.231     provos   1744:
                   1745:  authenticated:
1.318     djm      1746:        /*
                   1747:         * Cancel the alarm we set to limit the time taken for
                   1748:         * authentication.
                   1749:         */
                   1750:        alarm(0);
                   1751:        signal(SIGALRM, SIG_DFL);
1.347     markus   1752:        authctxt->authenticated = 1;
1.318     djm      1753:        if (startup_pipe != -1) {
                   1754:                close(startup_pipe);
                   1755:                startup_pipe = -1;
                   1756:        }
                   1757:
1.234     markus   1758:        /*
1.231     provos   1759:         * In privilege separation, we fork another child and prepare
                   1760:         * file descriptor passing.
                   1761:         */
                   1762:        if (use_privsep) {
1.237     markus   1763:                privsep_postauth(authctxt);
                   1764:                /* the monitor process [priv] will not return */
1.231     provos   1765:                if (!compat20)
                   1766:                        destroy_sensitive_data();
                   1767:        }
1.360     dtucker  1768:
                   1769:        packet_set_timeout(options.client_alive_interval,
                   1770:            options.client_alive_count_max);
1.230     provos   1771:
1.278     markus   1772:        /* Start session. */
1.230     provos   1773:        do_authenticated(authctxt);
                   1774:
1.64      markus   1775:        /* The connection has been terminated. */
                   1776:        verbose("Closing connection to %.100s", remote_ip);
                   1777:        packet_close();
1.231     provos   1778:
                   1779:        if (use_privsep)
                   1780:                mm_terminate();
                   1781:
1.64      markus   1782:        exit(0);
1.1       deraadt  1783: }
                   1784:
1.65      deraadt  1785: /*
1.229     markus   1786:  * Decrypt session_key_int using our private server key and private host key
                   1787:  * (key with larger modulus first).
                   1788:  */
1.231     provos   1789: int
1.229     markus   1790: ssh1_session_key(BIGNUM *session_key_int)
                   1791: {
                   1792:        int rsafail = 0;
                   1793:
1.327     deraadt  1794:        if (BN_cmp(sensitive_data.server_key->rsa->n,
                   1795:            sensitive_data.ssh1_host_key->rsa->n) > 0) {
1.229     markus   1796:                /* Server key has bigger modulus. */
                   1797:                if (BN_num_bits(sensitive_data.server_key->rsa->n) <
1.327     deraadt  1798:                    BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
                   1799:                    SSH_KEY_BITS_RESERVED) {
                   1800:                        fatal("do_connection: %s: "
                   1801:                            "server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1.229     markus   1802:                            get_remote_ipaddr(),
                   1803:                            BN_num_bits(sensitive_data.server_key->rsa->n),
                   1804:                            BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
                   1805:                            SSH_KEY_BITS_RESERVED);
                   1806:                }
                   1807:                if (rsa_private_decrypt(session_key_int, session_key_int,
                   1808:                    sensitive_data.server_key->rsa) <= 0)
                   1809:                        rsafail++;
                   1810:                if (rsa_private_decrypt(session_key_int, session_key_int,
                   1811:                    sensitive_data.ssh1_host_key->rsa) <= 0)
                   1812:                        rsafail++;
                   1813:        } else {
                   1814:                /* Host key has bigger modulus (or they are equal). */
                   1815:                if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
1.327     deraadt  1816:                    BN_num_bits(sensitive_data.server_key->rsa->n) +
                   1817:                    SSH_KEY_BITS_RESERVED) {
                   1818:                        fatal("do_connection: %s: "
                   1819:                            "host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
1.229     markus   1820:                            get_remote_ipaddr(),
                   1821:                            BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
                   1822:                            BN_num_bits(sensitive_data.server_key->rsa->n),
                   1823:                            SSH_KEY_BITS_RESERVED);
                   1824:                }
                   1825:                if (rsa_private_decrypt(session_key_int, session_key_int,
                   1826:                    sensitive_data.ssh1_host_key->rsa) < 0)
                   1827:                        rsafail++;
                   1828:                if (rsa_private_decrypt(session_key_int, session_key_int,
                   1829:                    sensitive_data.server_key->rsa) < 0)
                   1830:                        rsafail++;
                   1831:        }
                   1832:        return (rsafail);
                   1833: }
                   1834: /*
1.77      markus   1835:  * SSH1 key exchange
1.65      deraadt  1836:  */
1.200     itojun   1837: static void
1.142     markus   1838: do_ssh1_kex(void)
1.1       deraadt  1839: {
1.64      markus   1840:        int i, len;
1.159     markus   1841:        int rsafail = 0;
1.64      markus   1842:        BIGNUM *session_key_int;
1.140     markus   1843:        u_char session_key[SSH_SESSION_KEY_LENGTH];
                   1844:        u_char cookie[8];
                   1845:        u_int cipher_type, auth_mask, protocol_flags;
1.64      markus   1846:
1.66      markus   1847:        /*
                   1848:         * Generate check bytes that the client must send back in the user
                   1849:         * packet in order for it to be accepted; this is used to defy ip
                   1850:         * spoofing attacks.  Note that this only works against somebody
                   1851:         * doing IP spoofing from a remote machine; any machine on the local
                   1852:         * network can still see outgoing packets and catch the random
                   1853:         * cookie.  This only affects rhosts authentication, and this is one
                   1854:         * of the reasons why it is inherently insecure.
                   1855:         */
1.356     djm      1856:        arc4random_buf(cookie, sizeof(cookie));
1.64      markus   1857:
1.66      markus   1858:        /*
                   1859:         * Send our public key.  We include in the packet 64 bits of random
                   1860:         * data that must be matched in the reply in order to prevent IP
                   1861:         * spoofing.
                   1862:         */
1.64      markus   1863:        packet_start(SSH_SMSG_PUBLIC_KEY);
                   1864:        for (i = 0; i < 8; i++)
1.77      markus   1865:                packet_put_char(cookie[i]);
1.64      markus   1866:
                   1867:        /* Store our public server RSA key. */
1.134     markus   1868:        packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
                   1869:        packet_put_bignum(sensitive_data.server_key->rsa->e);
                   1870:        packet_put_bignum(sensitive_data.server_key->rsa->n);
1.64      markus   1871:
                   1872:        /* Store our public host RSA key. */
1.134     markus   1873:        packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
                   1874:        packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
                   1875:        packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
1.64      markus   1876:
                   1877:        /* Put protocol flags. */
                   1878:        packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
                   1879:
                   1880:        /* Declare which ciphers we support. */
1.131     markus   1881:        packet_put_int(cipher_mask_ssh1(0));
1.64      markus   1882:
                   1883:        /* Declare supported authentication types. */
                   1884:        auth_mask = 0;
                   1885:        if (options.rhosts_rsa_authentication)
                   1886:                auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
                   1887:        if (options.rsa_authentication)
                   1888:                auth_mask |= 1 << SSH_AUTH_RSA;
1.196     markus   1889:        if (options.challenge_response_authentication == 1)
1.64      markus   1890:                auth_mask |= 1 << SSH_AUTH_TIS;
                   1891:        if (options.password_authentication)
                   1892:                auth_mask |= 1 << SSH_AUTH_PASSWORD;
                   1893:        packet_put_int(auth_mask);
                   1894:
                   1895:        /* Send the packet and wait for it to be sent. */
                   1896:        packet_send();
                   1897:        packet_write_wait();
                   1898:
1.134     markus   1899:        debug("Sent %d bit server key and %d bit host key.",
                   1900:            BN_num_bits(sensitive_data.server_key->rsa->n),
                   1901:            BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1.64      markus   1902:
                   1903:        /* Read clients reply (cipher type and session key). */
1.222     markus   1904:        packet_read_expect(SSH_CMSG_SESSION_KEY);
1.64      markus   1905:
1.69      markus   1906:        /* Get cipher type and check whether we accept this. */
1.64      markus   1907:        cipher_type = packet_get_char();
1.69      markus   1908:
1.131     markus   1909:        if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
1.69      markus   1910:                packet_disconnect("Warning: client selects unsupported cipher.");
1.64      markus   1911:
                   1912:        /* Get check bytes from the packet.  These must match those we
                   1913:           sent earlier with the public key packet. */
                   1914:        for (i = 0; i < 8; i++)
1.77      markus   1915:                if (cookie[i] != packet_get_char())
1.64      markus   1916:                        packet_disconnect("IP Spoofing check bytes do not match.");
                   1917:
                   1918:        debug("Encryption type: %.200s", cipher_name(cipher_type));
                   1919:
                   1920:        /* Get the encrypted integer. */
1.218     markus   1921:        if ((session_key_int = BN_new()) == NULL)
                   1922:                fatal("do_ssh1_kex: BN_new failed");
1.221     markus   1923:        packet_get_bignum(session_key_int);
1.64      markus   1924:
                   1925:        protocol_flags = packet_get_int();
                   1926:        packet_set_protocol_flags(protocol_flags);
1.220     markus   1927:        packet_check_eom();
1.64      markus   1928:
1.229     markus   1929:        /* Decrypt session_key_int using host/server keys */
1.231     provos   1930:        rsafail = PRIVSEP(ssh1_session_key(session_key_int));
                   1931:
1.66      markus   1932:        /*
                   1933:         * Extract session key from the decrypted integer.  The key is in the
                   1934:         * least significant 256 bits of the integer; the first byte of the
                   1935:         * key is in the highest bits.
                   1936:         */
1.159     markus   1937:        if (!rsafail) {
1.348     markus   1938:                (void) BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1.159     markus   1939:                len = BN_num_bytes(session_key_int);
1.311     djm      1940:                if (len < 0 || (u_int)len > sizeof(session_key)) {
1.348     markus   1941:                        error("do_ssh1_kex: bad session key len from %s: "
1.165     itojun   1942:                            "session_key_int %d > sizeof(session_key) %lu",
                   1943:                            get_remote_ipaddr(), len, (u_long)sizeof(session_key));
1.159     markus   1944:                        rsafail++;
                   1945:                } else {
                   1946:                        memset(session_key, 0, sizeof(session_key));
                   1947:                        BN_bn2bin(session_key_int,
                   1948:                            session_key + sizeof(session_key) - len);
1.169     markus   1949:
1.291     djm      1950:                        derive_ssh1_session_id(
1.298     deraadt  1951:                            sensitive_data.ssh1_host_key->rsa->n,
1.291     djm      1952:                            sensitive_data.server_key->rsa->n,
                   1953:                            cookie, session_id);
1.169     markus   1954:                        /*
                   1955:                         * Xor the first 16 bytes of the session key with the
                   1956:                         * session id.
                   1957:                         */
                   1958:                        for (i = 0; i < 16; i++)
                   1959:                                session_key[i] ^= session_id[i];
1.159     markus   1960:                }
                   1961:        }
                   1962:        if (rsafail) {
1.169     markus   1963:                int bytes = BN_num_bytes(session_key_int);
1.227     stevesk  1964:                u_char *buf = xmalloc(bytes);
1.169     markus   1965:                MD5_CTX md;
                   1966:
1.264     itojun   1967:                logit("do_connection: generating a fake encryption key");
1.169     markus   1968:                BN_bn2bin(session_key_int, buf);
                   1969:                MD5_Init(&md);
                   1970:                MD5_Update(&md, buf, bytes);
                   1971:                MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
                   1972:                MD5_Final(session_key, &md);
                   1973:                MD5_Init(&md);
                   1974:                MD5_Update(&md, session_key, 16);
                   1975:                MD5_Update(&md, buf, bytes);
                   1976:                MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
                   1977:                MD5_Final(session_key + 16, &md);
                   1978:                memset(buf, 0, bytes);
                   1979:                xfree(buf);
1.170     markus   1980:                for (i = 0; i < 16; i++)
                   1981:                        session_id[i] = session_key[i] ^ session_key[i + 16];
1.159     markus   1982:        }
1.231     provos   1983:        /* Destroy the private and public keys. No longer. */
1.169     markus   1984:        destroy_sensitive_data();
                   1985:
1.231     provos   1986:        if (use_privsep)
                   1987:                mm_ssh1_session_id(session_id);
                   1988:
1.77      markus   1989:        /* Destroy the decrypted integer.  It is no longer needed. */
                   1990:        BN_clear_free(session_key_int);
1.64      markus   1991:
                   1992:        /* Set the session key.  From this on all communications will be encrypted. */
                   1993:        packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
                   1994:
                   1995:        /* Destroy our copy of the session key.  It is no longer needed. */
                   1996:        memset(session_key, 0, sizeof(session_key));
                   1997:
                   1998:        debug("Received session key; encryption turned on.");
                   1999:
1.243     deraadt  2000:        /* Send an acknowledgment packet.  Note that this packet is sent encrypted. */
1.64      markus   2001:        packet_start(SSH_SMSG_SUCCESS);
                   2002:        packet_send();
                   2003:        packet_write_wait();
1.98      markus   2004: }
                   2005:
                   2006: /*
                   2007:  * SSH2 key exchange: diffie-hellman-group1-sha1
                   2008:  */
1.200     itojun   2009: static void
1.142     markus   2010: do_ssh2_kex(void)
1.98      markus   2011: {
                   2012:        Kex *kex;
1.102     markus   2013:
                   2014:        if (options.ciphers != NULL) {
1.105     markus   2015:                myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1.102     markus   2016:                myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1.166     markus   2017:        }
1.184     stevesk  2018:        myproposal[PROPOSAL_ENC_ALGS_CTOS] =
                   2019:            compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
                   2020:        myproposal[PROPOSAL_ENC_ALGS_STOC] =
                   2021:            compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
                   2022:
1.166     markus   2023:        if (options.macs != NULL) {
                   2024:                myproposal[PROPOSAL_MAC_ALGS_CTOS] =
                   2025:                myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.246     markus   2026:        }
1.312     markus   2027:        if (options.compression == COMP_NONE) {
1.246     markus   2028:                myproposal[PROPOSAL_COMP_ALGS_CTOS] =
                   2029:                myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
1.312     markus   2030:        } else if (options.compression == COMP_DELAYED) {
                   2031:                myproposal[PROPOSAL_COMP_ALGS_CTOS] =
                   2032:                myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com";
1.102     markus   2033:        }
1.327     deraadt  2034:
1.134     markus   2035:        myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
                   2036:
1.189     markus   2037:        /* start key exchange */
1.188     markus   2038:        kex = kex_setup(myproposal);
1.263     markus   2039:        kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1.292     djm      2040:        kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.263     markus   2041:        kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.324     djm      2042:        kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.186     markus   2043:        kex->server = 1;
                   2044:        kex->client_version_string=client_version_string;
                   2045:        kex->server_version_string=server_version_string;
                   2046:        kex->load_host_key=&get_hostkey_by_type;
1.231     provos   2047:        kex->host_key_index=&get_hostkey_index;
1.129     provos   2048:
1.189     markus   2049:        xxx_kex = kex;
                   2050:
1.190     markus   2051:        dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
1.187     markus   2052:
                   2053:        session_id2 = kex->session_id;
                   2054:        session_id2_len = kex->session_id_len;
1.129     provos   2055:
                   2056: #ifdef DEBUG_KEXDH
                   2057:        /* send 1st encrypted/maced/compressed message */
                   2058:        packet_start(SSH2_MSG_IGNORE);
                   2059:        packet_put_cstring("markus");
                   2060:        packet_send();
                   2061:        packet_write_wait();
                   2062: #endif
1.186     markus   2063:        debug("KEX done");
1.278     markus   2064: }
                   2065:
                   2066: /* server specific fatal cleanup */
                   2067: void
                   2068: cleanup_exit(int i)
                   2069: {
                   2070:        if (the_authctxt)
                   2071:                do_cleanup(the_authctxt);
                   2072:        _exit(i);
1.1       deraadt  2073: }