]> andersk Git - openssh.git/blobdiff - sshd.c
- Merge big update to OpenSSH-2.0 from OpenBSD CVS
[openssh.git] / sshd.c
diff --git a/sshd.c b/sshd.c
index cd5760ecb1d718d343cc98f2d078d098ae51c0d2..fc2d1d20ea21d292f6221dcb24e4f350e7294618 100644 (file)
--- a/sshd.c
+++ b/sshd.c
@@ -14,7 +14,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: sshd.c,v 1.104 2000/04/12 09:39:10 markus Exp $");
+RCSID("$OpenBSD: sshd.c,v 1.111 2000/04/27 08:01:28 markus Exp $");
 
 #include "xmalloc.h"
 #include "rsa.h"
@@ -29,26 +29,18 @@ RCSID("$OpenBSD: sshd.c,v 1.104 2000/04/12 09:39:10 markus Exp $");
 #include "buffer.h"
 
 #include "ssh2.h"
-#ifdef HAVE_OPENSSL
-# include <openssl/dh.h>
-# include <openssl/bn.h>
-# include <openssl/hmac.h>
-# include <openssl/dsa.h>
-# include <openssl/rsa.h>
-#endif
-#ifdef HAVE_SSL
-# include <ssl/dh.h>
-# include <ssl/bn.h>
-# include <ssl/hmac.h>
-# include <ssl/dsa.h>
-# include <ssl/rsa.h>
-#endif
+#include <openssl/dh.h>
+#include <openssl/bn.h>
+#include <openssl/hmac.h>
 #include "kex.h"
+#include <openssl/dsa.h>
+#include <openssl/rsa.h>
 #include "key.h"
 #include "dsa.h"
 
 #include "auth.h"
 #include "myproposal.h"
+#include "authfile.h"
 
 #ifdef LIBWRAP
 #include <tcpd.h>
@@ -67,7 +59,7 @@ ServerOptions options;
 /* Name of the server configuration file. */
 char *config_file_name = SERVER_CONFIG_FILE;
 
-/* 
+/*
  * Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
  * Default value is AF_UNSPEC means both IPv4 and IPv6.
  */
@@ -121,8 +113,9 @@ char *server_version_string = NULL;
  * not very useful.  Currently, memory locking is not implemented.
  */
 struct {
-       RSA *private_key;        /* Private part of server key. */
+       RSA *private_key;        /* Private part of empheral server key. */
        RSA *host_key;           /* Private part of host key. */
+       Key *dsa_host_key;       /* Private DSA host key. */
 } sensitive_data;
 
 /*
@@ -141,6 +134,10 @@ RSA *public_key;
 /* session identifier, used by RSA-auth */
 unsigned char session_id[16];
 
+/* same for ssh2 */
+unsigned char *session_id2 = NULL;
+int session_id2_len = 0;
+
 /* Prototypes for various functions defined later in this file. */
 void do_ssh1_kex();
 void do_ssh2_kex();
@@ -162,7 +159,7 @@ close_listen_socks(void)
  * the effect is to reread the configuration file (and to regenerate
  * the server key).
  */
-void 
+void
 sighup_handler(int sig)
 {
        received_sighup = 1;
@@ -173,7 +170,7 @@ sighup_handler(int sig)
  * Called from the main program after receiving SIGHUP.
  * Restarts the server.
  */
-void 
+void
 sighup_restart()
 {
        log("Received SIGHUP; restarting.");
@@ -188,7 +185,7 @@ sighup_restart()
  * These close the listen socket; not closing it seems to cause "Address
  * already in use" problems on some machines, which is inconvenient.
  */
-void 
+void
 sigterm_handler(int sig)
 {
        log("Received signal %d; terminating.", sig);
@@ -200,7 +197,7 @@ sigterm_handler(int sig)
  * SIGCHLD handler.  This is called whenever a child dies.  This will then
  * reap any zombies left by exited c.
  */
-void 
+void
 main_sigchld_handler(int sig)
 {
        int save_errno = errno;
@@ -216,7 +213,7 @@ main_sigchld_handler(int sig)
 /*
  * Signal handler for the alarm after the login grace period has expired.
  */
-void 
+void
 grace_alarm_handler(int sig)
 {
        /* Close the connection. */
@@ -233,7 +230,8 @@ grace_alarm_handler(int sig)
  * Thus there should be no concurrency control/asynchronous execution
  * problems.
  */
-void 
+/* XXX do we really want this work to be done in a signal handler ? -m */
+void
 key_regeneration_alarm(int sig)
 {
        int save_errno = errno;
@@ -266,15 +264,15 @@ key_regeneration_alarm(int sig)
 char *
 chop(char *s)
 {
-        char *t = s;
-        while (*t) {
-                if(*t == '\n' || *t == '\r') {
-                        *t = '\0';
-                        return s;
-                }
-                t++;
-        }
-        return s;
+       char *t = s;
+       while (*t) {
+               if(*t == '\n' || *t == '\r') {
+                       *t = '\0';
+                       return s;
+               }
+               t++;
+       }
+       return s;
 
 }
 
@@ -337,7 +335,7 @@ sshd_exchange_identification(int sock_in, int sock_out)
         */
        if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
            &remote_major, &remote_minor, remote_version) != 3) {
-               s = "Protocol mismatch.\n";
+               s = "Protocol mismatch.\n";
                (void) atomicio(write, sock_out, s, strlen(s));
                close(sock_in);
                close(sock_out);
@@ -353,6 +351,13 @@ sshd_exchange_identification(int sock_in, int sock_out)
        mismatch = 0;
        switch(remote_major) {
        case 1:
+               if (remote_minor == 99) {
+                       if (options.protocol & SSH_PROTO_2)
+                               enable_compat20();
+                       else
+                               mismatch = 1;
+                       break;
+               }
                if (!(options.protocol & SSH_PROTO_1)) {
                        mismatch = 1;
                        break;
@@ -364,12 +369,6 @@ sshd_exchange_identification(int sock_in, int sock_out)
                        /* note that this disables agent-forwarding */
                        enable_compat13();
                }
-               if (remote_minor == 99) {
-                       if (options.protocol & SSH_PROTO_2)
-                               enable_compat20();
-                       else
-                               mismatch = 1;
-               }
                break;
        case 2:
                if (options.protocol & SSH_PROTO_2) {
@@ -377,7 +376,7 @@ sshd_exchange_identification(int sock_in, int sock_out)
                        break;
                }
                /* FALLTHROUGH */
-       default: 
+       default:
                mismatch = 1;
                break;
        }
@@ -395,6 +394,20 @@ sshd_exchange_identification(int sock_in, int sock_out)
                    server_version_string, client_version_string);
                fatal_cleanup();
        }
+       if (compat20)
+               packet_set_ssh2_format();
+}
+
+
+void
+destroy_sensitive_data(void)
+{
+       /* Destroy the private and public keys.  They will no longer be needed. */
+       RSA_free(public_key);
+       RSA_free(sensitive_data.private_key);
+       RSA_free(sensitive_data.host_key);
+       if (sensitive_data.dsa_host_key != NULL)
+               key_free(sensitive_data.dsa_host_key);
 }
 
 /*
@@ -405,14 +418,14 @@ main(int ac, char **av)
 {
        extern char *optarg;
        extern int optind;
-       int opt, sock_in = 0, sock_out = 0, newsock, i, fdsetsz, pid, on = 1;
+       int opt, sock_in = 0, sock_out = 0, newsock, i, fdsetsz, on = 1;
+       pid_t pid;
        socklen_t fromlen;
-       int silentrsa = 0;
+       int silent = 0;
        fd_set *fdset;
        struct sockaddr_storage from;
        const char *remote_ip;
        int remote_port;
-       char *comment;
        FILE *f;
        struct linger linger;
        struct addrinfo *ai;
@@ -449,7 +462,7 @@ main(int ac, char **av)
                        inetd_flag = 1;
                        break;
                case 'Q':
-                       silentrsa = 1;
+                       silent = 1;
                        break;
                case 'q':
                        options.log_level = SYSLOG_LEVEL_QUIET;
@@ -505,27 +518,14 @@ main(int ac, char **av)
        log_init(av0,
            options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
            options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility,
-           !inetd_flag);
+           !silent && !inetd_flag);
 
-       /* check if RSA support exists */
-       if (rsa_alive() == 0) {
-               if (silentrsa == 0)
-                       printf("sshd: no RSA support in libssl and libcrypto -- exiting.  See ssl(8)\n");
-               log("no RSA support in libssl and libcrypto -- exiting.  See ssl(8)");
-               exit(1);
-       }
        /* Read server configuration options from the configuration file. */
        read_server_config(&options, config_file_name);
 
        /* Fill in default values for those options not explicitly set. */
        fill_default_server_options(&options);
 
-       /* Check certain values for sanity. */
-       if (options.server_key_bits < 512 ||
-           options.server_key_bits > 32768) {
-               fprintf(stderr, "Bad server key size.\n");
-               exit(1);
-       }
        /* Check that there are no remaining arguments. */
        if (optind < ac) {
                fprintf(stderr, "Extra argument %s.\n", av[optind]);
@@ -534,26 +534,79 @@ main(int ac, char **av)
 
        debug("sshd version %.100s", SSH_VERSION);
 
-       sensitive_data.host_key = RSA_new();
-       errno = 0;
-       /* Load the host key.  It must have empty passphrase. */
-       if (!load_private_key(options.host_key_file, "",
-                             sensitive_data.host_key, &comment)) {
-               error("Could not load host key: %.200s: %.100s",
-                     options.host_key_file, strerror(errno));
+       sensitive_data.dsa_host_key = NULL;
+       sensitive_data.host_key = NULL;
+
+       /* check if RSA support exists */
+       if ((options.protocol & SSH_PROTO_1) &&
+           rsa_alive() == 0) {
+               log("no RSA support in libssl and libcrypto.  See ssl(8)");
+               log("Disabling protocol version 1");
+               options.protocol &= ~SSH_PROTO_1;
+       }
+       /* Load the RSA/DSA host key.  It must have empty passphrase. */
+       if (options.protocol & SSH_PROTO_1) {
+               Key k;
+               sensitive_data.host_key = RSA_new();
+               k.type = KEY_RSA;
+               k.rsa = sensitive_data.host_key;
+               errno = 0;
+               if (!load_private_key(options.host_key_file, "", &k, NULL)) {
+                       error("Could not load host key: %.200s: %.100s",
+                           options.host_key_file, strerror(errno));
+                       log("Disabling protocol version 1");
+                       options.protocol &= ~SSH_PROTO_1;
+               }
+               k.rsa = NULL;
+       }
+       if (options.protocol & SSH_PROTO_2) {
+               sensitive_data.dsa_host_key = key_new(KEY_DSA);
+               if (!load_private_key(options.dsa_key_file, "", sensitive_data.dsa_host_key, NULL)) {
+                       error("Could not load DSA host key: %.200s", options.dsa_key_file);
+                       log("Disabling protocol version 2");
+                       options.protocol &= ~SSH_PROTO_2;
+               }
+       }
+       if (! options.protocol & (SSH_PROTO_1|SSH_PROTO_2)) {
+               if (silent == 0)
+                       fprintf(stderr, "sshd: no hostkeys available -- exiting.\n");
+               log("sshd: no hostkeys available -- exiting.\n");
                exit(1);
        }
-       xfree(comment);
 
-       /* Initialize the log (it is reinitialized below in case we
-          forked). */
+       /* Check certain values for sanity. */
+       if (options.protocol & SSH_PROTO_1) {
+               if (options.server_key_bits < 512 ||
+                   options.server_key_bits > 32768) {
+                       fprintf(stderr, "Bad server key size.\n");
+                       exit(1);
+               }
+               /*
+                * Check that server and host key lengths differ sufficiently. This
+                * is necessary to make double encryption work with rsaref. Oh, I
+                * hate software patents. I dont know if this can go? Niels
+                */
+               if (options.server_key_bits >
+                   BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED &&
+                   options.server_key_bits <
+                   BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) {
+                       options.server_key_bits =
+                           BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED;
+                       debug("Forcing server key to %d bits to make it differ from host key.",
+                           options.server_key_bits);
+               }
+       }
+
+       /* Initialize the log (it is reinitialized below in case we forked). */
        if (debug_flag && !inetd_flag)
                log_stderr = 1;
        log_init(av0, options.log_level, options.log_facility, log_stderr);
 
-       /* If not in debugging mode, and not started from inetd,
-          disconnect from the controlling terminal, and fork.  The
-          original process exits. */
+       /*
+        * If not in debugging mode, and not started from inetd, disconnect
+        * from the controlling terminal, and fork.  The original process
+        * exits.
+        */
        if (!debug_flag && !inetd_flag) {
 #ifdef TIOCNOTTY
                int fd;
@@ -573,18 +626,6 @@ main(int ac, char **av)
        /* Reinitialize the log (because of the fork above). */
        log_init(av0, options.log_level, options.log_facility, log_stderr);
 
-       /* Check that server and host key lengths differ sufficiently.
-          This is necessary to make double encryption work with rsaref.
-          Oh, I hate software patents. I dont know if this can go? Niels */
-       if (options.server_key_bits >
-       BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED &&
-           options.server_key_bits <
-       BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) {
-               options.server_key_bits =
-                       BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED;
-               debug("Forcing server key to %d bits to make it differ from host key.",
-                     options.server_key_bits);
-       }
        /* Do not display messages to stdout in RSA code. */
        rsa_set_verbose(0);
 
@@ -602,20 +643,22 @@ main(int ac, char **av)
                s2 = dup(s1);
                sock_in = dup(0);
                sock_out = dup(1);
-               /* We intentionally do not close the descriptors 0, 1, and 2
-                  as our code for setting the descriptors won\'t work
-                  if ttyfd happens to be one of those. */
+               /*
+                * We intentionally do not close the descriptors 0, 1, and 2
+                * as our code for setting the descriptors won\'t work if
+                * ttyfd happens to be one of those.
+                */
                debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
 
-               public_key = RSA_new();
-               sensitive_data.private_key = RSA_new();
-
-               /* XXX check options.protocol */
-               log("Generating %d bit RSA key.", options.server_key_bits);
-               rsa_generate_key(sensitive_data.private_key, public_key,
-                                options.server_key_bits);
-               arc4random_stir();
-               log("RSA key generation complete.");
+               if (options.protocol & SSH_PROTO_1) {
+                       public_key = RSA_new();
+                       sensitive_data.private_key = RSA_new();
+                       log("Generating %d bit RSA key.", options.server_key_bits);
+                       rsa_generate_key(sensitive_data.private_key, public_key,
+                           options.server_key_bits);
+                       arc4random_stir();
+                       log("RSA key generation complete.");
+               }
        } else {
                for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
                        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
@@ -692,19 +735,20 @@ main(int ac, char **av)
                                fclose(f);
                        }
                }
+               if (options.protocol & SSH_PROTO_1) {
+                       public_key = RSA_new();
+                       sensitive_data.private_key = RSA_new();
 
-               public_key = RSA_new();
-               sensitive_data.private_key = RSA_new();
-
-               log("Generating %d bit RSA key.", options.server_key_bits);
-               rsa_generate_key(sensitive_data.private_key, public_key,
-                                options.server_key_bits);
-               arc4random_stir();
-               log("RSA key generation complete.");
+                       log("Generating %d bit RSA key.", options.server_key_bits);
+                       rsa_generate_key(sensitive_data.private_key, public_key,
+                           options.server_key_bits);
+                       arc4random_stir();
+                       log("RSA key generation complete.");
 
-               /* Schedule server key regeneration alarm. */
-               signal(SIGALRM, key_regeneration_alarm);
-               alarm(options.key_regeneration_time);
+                       /* Schedule server key regeneration alarm. */
+                       signal(SIGALRM, key_regeneration_alarm);
+                       alarm(options.key_regeneration_time);
+               }
 
                /* Arrange to restart on SIGHUP.  The handler needs listen_sock. */
                signal(SIGHUP, sighup_handler);
@@ -719,8 +763,8 @@ main(int ac, char **av)
                for (i = 0; i < num_listen_socks; i++)
                        if (listen_socks[i] > maxfd)
                                maxfd = listen_socks[i];
-               fdsetsz = howmany(maxfd, NFDBITS) * sizeof(fd_mask);         
-               fdset = (fd_set *)xmalloc(fdsetsz);                                  
+               fdsetsz = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
+               fdset = (fd_set *)xmalloc(fdsetsz);
 
                /*
                 * Stay listening for connections until the system crashes or
@@ -1018,7 +1062,7 @@ do_ssh1_kex()
        /* Get cipher type and check whether we accept this. */
        cipher_type = packet_get_char();
 
-        if (!(cipher_mask() & (1 << cipher_type)))
+       if (!(cipher_mask() & (1 << cipher_type)))
                packet_disconnect("Warning: client selects unsupported cipher.");
 
        /* Get check bytes from the packet.  These must match those we
@@ -1077,9 +1121,7 @@ do_ssh1_kex()
                           sensitive_data.private_key->n);
 
        /* Destroy the private and public keys.  They will no longer be needed. */
-       RSA_free(public_key);
-       RSA_free(sensitive_data.private_key);
-       RSA_free(sensitive_data.host_key);
+       destroy_sensitive_data();
 
        /*
         * Extract session key from the decrypted integer.  The key is in the
@@ -1138,14 +1180,13 @@ do_ssh2_kex()
        unsigned char *kbuf;
        unsigned char *hash;
        Kex *kex;
-       Key *server_host_key;
        char *cprop[PROPOSAL_MAX];
        char *sprop[PROPOSAL_MAX];
 
 /* KEXINIT */
 
        if (options.ciphers != NULL) {
-               myproposal[PROPOSAL_ENC_ALGS_CTOS] = 
+               myproposal[PROPOSAL_ENC_ALGS_CTOS] =
                myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
        }
 
@@ -1239,8 +1280,8 @@ do_ssh2_kex()
        memset(kbuf, 0, klen);
        xfree(kbuf);
 
-       server_host_key = dsa_get_serverkey(options.dsa_key_file);
-       dsa_make_serverkey_blob(server_host_key, &server_host_key_blob, &sbloblen);
+       /* XXX precompute? */
+       dsa_make_key_blob(sensitive_data.dsa_host_key, &server_host_key_blob, &sbloblen);
 
        /* calc H */                    /* XXX depends on 'kex' */
        hash = kex_hash(
@@ -1258,15 +1299,22 @@ do_ssh2_kex()
        xfree(client_kexinit);
        xfree(server_kexinit);
 #ifdef DEBUG_KEXDH
-        fprintf(stderr, "hash == ");
-        for (i = 0; i< 20; i++)
-                fprintf(stderr, "%02x", (hash[i])&0xff);
-        fprintf(stderr, "\n");
+       fprintf(stderr, "hash == ");
+       for (i = 0; i< 20; i++)
+               fprintf(stderr, "%02x", (hash[i])&0xff);
+       fprintf(stderr, "\n");
 #endif
+       /* save session id := H */
+       /* XXX hashlen depends on KEX */
+       session_id2_len = 20;
+       session_id2 = xmalloc(session_id2_len);
+       memcpy(session_id2, hash, session_id2_len);
+
        /* sign H */
-       dsa_sign(server_host_key, &signature, &slen, hash, 20);
-               /* hashlen depends on KEX */
-       key_free(server_host_key);
+       /* XXX hashlen depends on KEX */
+       dsa_sign(sensitive_data.dsa_host_key, &signature, &slen, hash, 20);
+
+       destroy_sensitive_data();
 
        /* send server hostkey, DH pubkey 'f' and singed H */
        packet_start(SSH2_MSG_KEXDH_REPLY);
@@ -1274,6 +1322,8 @@ do_ssh2_kex()
        packet_put_bignum2(dh->pub_key);        // f
        packet_put_string((char *)signature, slen);
        packet_send();
+       xfree(signature);
+       xfree(server_host_key_blob);
        packet_write_wait();
 
        kex_derive_keys(kex, hash, shared_secret);
This page took 0.07231 seconds and 4 git commands to generate.