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

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