]> andersk Git - openssh.git/blobdiff - ssh-keysign.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / ssh-keysign.c
index 7f1d25d8c3f3ad944fa8531c0e4564ce19d15bc3..0fdcebbd22c79ea809d9d691e3e5cb05b8390eb9 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenBSD: ssh-keysign.c,v 1.30 2010/01/13 01:20:20 dtucker Exp $ */
 /*
  * Copyright (c) 2002 Markus Friedl.  All rights reserved.
  *
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+
 #include "includes.h"
-RCSID("$OpenBSD: ssh-keysign.c,v 1.4 2002/06/19 00:27:55 deraadt Exp $");
+
+#include <fcntl.h>
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
 #include <openssl/evp.h>
+#include <openssl/rand.h>
+#include <openssl/rsa.h>
 
+#include "xmalloc.h"
 #include "log.h"
 #include "key.h"
+#include "ssh.h"
 #include "ssh2.h"
 #include "misc.h"
-#include "xmalloc.h"
 #include "buffer.h"
-#include "bufaux.h"
 #include "authfile.h"
 #include "msg.h"
 #include "canohost.h"
 #include "pathnames.h"
+#include "readconf.h"
+#include "uidswap.h"
+
+/* XXX readconf.c needs these */
+uid_t original_real_uid;
 
-#ifdef HAVE___PROGNAME
 extern char *__progname;
-#else
-char *__progname;
-#endif
 
 static int
 valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
     u_int datalen)
 {
        Buffer b;
-       Key *key;
+       Key *key = NULL;
        u_char *pkblob;
        u_int blen, len;
        char *pkalg, *p;
@@ -60,9 +74,9 @@ valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
        buffer_init(&b);
        buffer_append(&b, data, datalen);
 
-       /* session id, currently limited to SHA1 (20 bytes) */
+       /* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */
        p = buffer_get_string(&b, &len);
-       if (len != 20)
+       if (len != 20 && len != 32)
                fail++;
        xfree(p);
 
@@ -119,6 +133,7 @@ valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
        /* end of message */
        if (buffer_len(&b) != 0)
                fail++;
+       buffer_free(&b);
 
        debug3("valid_request: fail %d", fail);
 
@@ -134,18 +149,31 @@ int
 main(int argc, char **argv)
 {
        Buffer b;
-       Key *keys[2], *key;
+       Options options;
+       Key *keys[2], *key = NULL;
        struct passwd *pw;
        int key_fd[2], i, found, version = 2, fd;
        u_char *signature, *data;
        char *host;
        u_int slen, dlen;
+       u_int32_t rnd[256];
+
+       /* Ensure that stdin and stdout are connected */
+       if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
+               exit(1);
+       /* Leave /dev/null fd iff it is attached to stderr */
+       if (fd > 2)
+               close(fd);
 
        key_fd[0] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
        key_fd[1] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
 
-       seteuid(getuid());
-       setuid(getuid());
+       original_real_uid = getuid();   /* XXX readconf.c needs this */
+       if ((pw = getpwuid(original_real_uid)) == NULL)
+               fatal("getpwuid failed");
+       pw = pwcopy(pw);
+
+       permanently_set_uid(pw);
 
        init_rng();
        seed_rng();
@@ -155,14 +183,21 @@ main(int argc, char **argv)
        log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
 #endif
 
+       /* verify that ssh-keysign is enabled by the admin */
+       initialize_options(&options);
+       (void)read_config_file(_PATH_HOST_CONFIG_FILE, "", &options, 0);
+       fill_default_options(&options);
+       if (options.enable_ssh_keysign != 1)
+               fatal("ssh-keysign not enabled in %s",
+                   _PATH_HOST_CONFIG_FILE);
+
        if (key_fd[0] == -1 && key_fd[1] == -1)
                fatal("could not open any host key");
 
-       if ((pw = getpwuid(getuid())) == NULL)
-               fatal("getpwuid failed");
-       pw = pwcopy(pw);
-
        SSLeay_add_all_algorithms();
+       for (i = 0; i < 256; i++)
+               rnd[i] = arc4random();
+       RAND_seed(rnd, sizeof(rnd));
 
        found = 0;
        for (i = 0; i < 2; i++) {
@@ -179,20 +214,19 @@ main(int argc, char **argv)
                fatal("no hostkey found");
 
        buffer_init(&b);
-       if (msg_recv(STDIN_FILENO, &b) < 0)
-               fatal("msg_recv failed");
+       if (ssh_msg_recv(STDIN_FILENO, &b) < 0)
+               fatal("ssh_msg_recv failed");
        if (buffer_get_char(&b) != version)
                fatal("bad version");
        fd = buffer_get_int(&b);
        if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO))
                fatal("bad fd");
        if ((host = get_local_name(fd)) == NULL)
-               fatal("cannot get sockname for fd");
+               fatal("cannot get local name for fd");
 
        data = buffer_get_string(&b, &dlen);
        if (valid_request(pw, host, &key, data, dlen) < 0)
                fatal("not a valid request");
-       xfree(data);
        xfree(host);
 
        found = 0;
@@ -208,11 +242,13 @@ main(int argc, char **argv)
 
        if (key_sign(keys[i], &signature, &slen, data, dlen) != 0)
                fatal("key_sign failed");
+       xfree(data);
 
        /* send reply */
        buffer_clear(&b);
        buffer_put_string(&b, signature, slen);
-       msg_send(STDOUT_FILENO, version, &b);
+       if (ssh_msg_send(STDOUT_FILENO, version, &b) == -1)
+               fatal("ssh_msg_send failed");
 
        return (0);
 }
This page took 3.554648 seconds and 4 git commands to generate.