]> andersk Git - openssh.git/blobdiff - sshd.c
- (djm) Stop shadow expiry checking from preventing logins with NIS. Based
[openssh.git] / sshd.c
diff --git a/sshd.c b/sshd.c
index c8508e931ac882f7df7b4cb27b2f2595ed289acd..a4749fbe41b62e93582bdbab677992c6b6c22b1e 100644 (file)
--- a/sshd.c
+++ b/sshd.c
@@ -14,7 +14,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: sshd.c,v 1.105 2000/04/14 10:30:33 markus Exp $");
+RCSID("$OpenBSD: sshd.c,v 1.119 2000/06/22 16:32:27 markus Exp $");
 
 #include "xmalloc.h"
 #include "rsa.h"
@@ -40,6 +40,7 @@ RCSID("$OpenBSD: sshd.c,v 1.105 2000/04/14 10:30:33 markus Exp $");
 
 #include "auth.h"
 #include "myproposal.h"
+#include "authfile.h"
 
 #ifdef LIBWRAP
 #include <tcpd.h>
@@ -87,6 +88,7 @@ char *av0;
 
 /* Saved arguments to main(). */
 char **saved_argv;
+int saved_argc;
 
 /*
  * The sockets that the server is listening; this is used in the SIGHUP
@@ -112,8 +114,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;
 
 /*
@@ -132,6 +135,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();
@@ -184,6 +191,7 @@ sigterm_handler(int sig)
 {
        log("Received signal %d; terminating.", sig);
        close_listen_socks();
+       unlink(options.pid_file);
        exit(255);
 }
 
@@ -224,6 +232,7 @@ grace_alarm_handler(int sig)
  * Thus there should be no concurrency control/asynchronous execution
  * problems.
  */
+/* XXX do we really want this work to be done in a signal handler ? -m */
 void
 key_regeneration_alarm(int sig)
 {
@@ -254,21 +263,6 @@ key_regeneration_alarm(int sig)
        errno = save_errno;
 }
 
-char *
-chop(char *s)
-{
-       char *t = s;
-       while (*t) {
-               if(*t == '\n' || *t == '\r') {
-                       *t = '\0';
-                       return s;
-               }
-               t++;
-       }
-       return s;
-
-}
-
 void
 sshd_exchange_identification(int sock_in, int sock_out)
 {
@@ -303,7 +297,7 @@ sshd_exchange_identification(int sock_in, int sock_out)
 
                /* Read other side\'s version identification. */
                for (i = 0; i < sizeof(buf) - 1; i++) {
-                       if (read(sock_in, &buf[i], 1) != 1) {
+                       if (atomicio(read, sock_in, &buf[i], 1) != 1) {
                                log("Did not receive ident string from %s.", get_remote_ipaddr());
                                fatal_cleanup();
                        }
@@ -344,6 +338,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;
@@ -355,12 +356,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) {
@@ -386,6 +381,23 @@ 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. */
+       if (public_key)
+               RSA_free(public_key);
+       if (sensitive_data.private_key)
+               RSA_free(sensitive_data.private_key);
+       if (sensitive_data.host_key)
+               RSA_free(sensitive_data.host_key);
+       if (sensitive_data.dsa_host_key != NULL)
+               key_free(sensitive_data.dsa_host_key);
 }
 
 /*
@@ -396,14 +408,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;
@@ -411,6 +423,7 @@ main(int ac, char **av)
        int listen_sock, maxfd;
 
        /* Save argv[0]. */
+       saved_argc = ac;
        saved_argv = av;
        if (strchr(av[0], '/'))
                av0 = strrchr(av[0], '/') + 1;
@@ -440,7 +453,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;
@@ -496,27 +509,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]);
@@ -525,26 +525,80 @@ 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.host_dsa_key_file, "", sensitive_data.dsa_host_key, NULL)) {
+
+                       error("Could not load DSA host key: %.200s", options.host_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;
@@ -564,18 +618,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);
 
@@ -593,20 +635,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)
@@ -677,25 +721,26 @@ main(int ac, char **av)
                         * fail if there already is a daemon, and this will
                         * overwrite any old pid in the file.
                         */
-                       f = fopen(SSH_DAEMON_PID_FILE, "w");
+                       f = fopen(options.pid_file, "w");
                        if (f) {
                                fprintf(f, "%u\n", (unsigned int) getpid());
                                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);
@@ -1068,9 +1113,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
@@ -1118,7 +1161,6 @@ do_ssh2_kex()
        int payload_len, dlen;
        int slen;
        unsigned int klen, kout;
-       char *ptr;
        unsigned char *signature = NULL;
        unsigned char *server_host_key_blob = NULL;
        unsigned int sbloblen;
@@ -1129,9 +1171,7 @@ do_ssh2_kex()
        unsigned char *kbuf;
        unsigned char *hash;
        Kex *kex;
-       Key *server_host_key;
        char *cprop[PROPOSAL_MAX];
-       char *sprop[PROPOSAL_MAX];
 
 /* KEXINIT */
 
@@ -1139,46 +1179,15 @@ do_ssh2_kex()
                myproposal[PROPOSAL_ENC_ALGS_CTOS] =
                myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
        }
-
-       debug("Sending KEX init.");
-
-       for (i = 0; i < PROPOSAL_MAX; i++)
-               sprop[i] = xstrdup(myproposal[i]);
-       server_kexinit = kex_init(sprop);
-       packet_start(SSH2_MSG_KEXINIT);
-       packet_put_raw(buffer_ptr(server_kexinit), buffer_len(server_kexinit)); 
-       packet_send();
-       packet_write_wait();
-
-       debug("done");
-
-       packet_read_expect(&payload_len, SSH2_MSG_KEXINIT);
-
-       /*
-        * save raw KEXINIT payload in buffer. this is used during
-        * computation of the session_id and the session keys.
-        */
+       server_kexinit = kex_init(myproposal);
        client_kexinit = xmalloc(sizeof(*client_kexinit));
        buffer_init(client_kexinit);
-       ptr = packet_get_raw(&payload_len);
-       buffer_append(client_kexinit, ptr, payload_len);
 
-       /* skip cookie */
-       for (i = 0; i < 16; i++)
-               (void) packet_get_char();
-       /* save kex init proposal strings */
-       for (i = 0; i < PROPOSAL_MAX; i++) {
-               cprop[i] = packet_get_string(NULL);
-               debug("got kexinit string: %s", cprop[i]);
-       }
-
-       i = (int) packet_get_char();
-       debug("first kex follow == %d", i);
-       i = packet_get_int();
-       debug("reserved == %d", i);
-
-       debug("done read kexinit");
-       kex = kex_choose_conf(cprop, sprop, 1);
+       /* algorithm negotiation */
+       kex_exchange_kexinit(server_kexinit, client_kexinit, cprop);
+       kex = kex_choose_conf(cprop, myproposal, 1);
+       for (i = 0; i < PROPOSAL_MAX; i++)
+               xfree(cprop[i]);
 
 /* KEXDH */
 
@@ -1230,8 +1239,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(
@@ -1254,17 +1263,26 @@ do_ssh2_kex()
                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);
        packet_put_string((char *)server_host_key_blob, sbloblen);
-       packet_put_bignum2(dh->pub_key);        // f
+       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.058086 seconds and 4 git commands to generate.