]> andersk Git - openssh.git/blobdiff - ssh-keyscan.c
- jakob@cvs.openbsd.org 2003/05/15 04:08:44
[openssh.git] / ssh-keyscan.c
index 80eab26927f734240bbfe6d8034a271ac46242d5..6b19542691760ae22134d2847926e6201112582c 100644 (file)
@@ -7,14 +7,9 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: ssh-keyscan.c,v 1.33 2001/12/10 20:34:31 markus Exp $");
+RCSID("$OpenBSD: ssh-keyscan.c,v 1.43 2003/04/26 04:29:49 deraadt Exp $");
 
-#if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H)
-#include <sys/queue.h>
-#else
-#include "openbsd-compat/fake-queue.h"
-#endif
-#include <errno.h>
+#include "openbsd-compat/sys-queue.h"
 
 #include <openssl/bn.h>
 
@@ -121,7 +116,8 @@ Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
 
        if (!(lb = malloc(sizeof(*lb)))) {
                if (errfun)
-                       (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
+                       (*errfun) ("linebuf (%s): malloc failed\n",
+                           filename ? filename : "(stdin)");
                return (NULL);
        }
        if (filename) {
@@ -176,13 +172,14 @@ static char *
 Linebuf_getline(Linebuf * lb)
 {
        int n = 0;
+       void *p;
 
        lb->lineno++;
        for (;;) {
                /* Read a line */
                if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
                        if (ferror(lb->stream) && lb->errfun)
-                               (*lb->errfun) ("%s: %s\n", lb->filename,
+                               (*lb->errfun)("%s: %s\n", lb->filename,
                                    strerror(errno));
                        return (NULL);
                }
@@ -195,17 +192,20 @@ Linebuf_getline(Linebuf * lb)
                }
                if (n != lb->size - 1) {
                        if (lb->errfun)
-                               (*lb->errfun) ("%s: skipping incomplete last line\n",
+                               (*lb->errfun)("%s: skipping incomplete last line\n",
                                    lb->filename);
                        return (NULL);
                }
                /* Double the buffer if we need more space */
-               if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) {
+               lb->size *= 2;
+               if ((p = realloc(lb->buf, lb->size)) == NULL) {
+                       lb->size /= 2;
                        if (lb->errfun)
-                               (*lb->errfun) ("linebuf (%s): realloc failed\n",
+                               (*lb->errfun)("linebuf (%s): realloc failed\n",
                                    lb->filename);
                        return (NULL);
                }
+               lb->buf = p;
        }
 }
 
@@ -234,6 +234,7 @@ fdlim_set(int lim)
 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
        struct rlimit rlfd;
 #endif
+
        if (lim <= 0)
                return (-1);
 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
@@ -353,6 +354,8 @@ keygrab_ssh2(con *c)
        myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
            "ssh-dss": "ssh-rsa";
        c->c_kex = kex_setup(myproposal);
+       c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
+       c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
        c->c_kex->verify_host_key = hostjump;
 
        if (!(j = setjmp(kexjmp))) {
@@ -394,7 +397,7 @@ tcpconnect(char *host)
        if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
                fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
        for (ai = aitop; ai; ai = ai->ai_next) {
-               s = socket(ai->ai_family, SOCK_STREAM, 0);
+               s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
                if (s < 0) {
                        error("socket: %s", strerror(errno));
                        continue;
@@ -416,8 +419,8 @@ tcpconnect(char *host)
 static int
 conalloc(char *iname, char *oname, int keytype)
 {
-       int s;
        char *namebase, *name, *namelist;
+       int s;
 
        namebase = namelist = xstrdup(iname);
 
@@ -481,8 +484,8 @@ contouch(int s)
 static int
 conrecycle(int s)
 {
-       int ret;
        con *c = &fdcon[s];
+       int ret;
 
        ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
        confree(s);
@@ -492,10 +495,10 @@ conrecycle(int s)
 static void
 congreet(int s)
 {
+       int remote_major, remote_minor, n = 0;
        char buf[256], *cp;
        char remote_version[sizeof buf];
        size_t bufsiz;
-       int remote_major, remote_minor, n = 0;
        con *c = &fdcon[s];
 
        bufsiz = sizeof(buf);
@@ -511,6 +514,11 @@ congreet(int s)
                conrecycle(s);
                return;
        }
+       if (n == 0) {
+               error("%s: Connection closed by remote host", c->c_name);
+               conrecycle(s);
+               return;
+       }
        if (*cp != '\n' && *cp != '\r') {
                error("%s: bad greeting", c->c_name);
                confree(s);
@@ -554,8 +562,8 @@ congreet(int s)
 static void
 conread(int s)
 {
-       int n;
        con *c = &fdcon[s];
+       int n;
 
        if (c->c_status == CS_CON) {
                congreet(s);
@@ -594,13 +602,13 @@ conread(int s)
 static void
 conloop(void)
 {
-       fd_set *r, *e;
        struct timeval seltime, now;
-       int i;
+       fd_set *r, *e;
        con *c;
+       int i;
 
        gettimeofday(&now, NULL);
-       c = tq.tqh_first;
+       c = TAILQ_FIRST(&tq);
 
        if (c && (c->c_tv.tv_sec > now.tv_sec ||
            (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
@@ -633,12 +641,12 @@ conloop(void)
        xfree(r);
        xfree(e);
 
-       c = tq.tqh_first;
+       c = TAILQ_FIRST(&tq);
        while (c && (c->c_tv.tv_sec < now.tv_sec ||
            (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
                int s = c->c_fd;
 
-               c = c->c_link.tqe_next;
+               c = TAILQ_NEXT(c, c_link);
                conrecycle(s);
        }
 }
@@ -660,26 +668,26 @@ do_host(char *host)
        }
 }
 
-static void
-fatal_callback(void *arg)
+void
+fatal(const char *fmt,...)
 {
+       va_list args;
+
+       va_start(args, fmt);
+       do_log(SYSLOG_LEVEL_FATAL, fmt, args);
+       va_end(args);
        if (nonfatal_fatal)
                longjmp(kexjmp, -1);
+       else
+               fatal_cleanup();
 }
 
 static void
 usage(void)
 {
-       fprintf(stderr, "Usage: %s [options] host ...\n",
+       fprintf(stderr, "usage: %s [-v46] [-p port] [-T timeout] [-t type] [-f file]\n"
+           "\t\t   [host | addrlist namelist] [...]\n",
            __progname);
-       fprintf(stderr, "Options:\n");
-       fprintf(stderr, "  -f file     Read hosts or addresses from file.\n");
-       fprintf(stderr, "  -p port     Connect to the specified port.\n");
-       fprintf(stderr, "  -t keytype  Specify the host key type.\n");
-       fprintf(stderr, "  -T timeout  Set connection timeout.\n");
-        fprintf(stderr, "  -v          Verbose; display verbose debugging messages.\n");
-        fprintf(stderr, "  -4          Use IPv4 only.\n");
-        fprintf(stderr, "  -6          Use IPv6 only.\n");
        exit(1);
 }
 
@@ -711,9 +719,11 @@ main(int argc, char **argv)
                        }
                        break;
                case 'T':
-                       timeout = atoi(optarg);
-                       if (timeout <= 0)
+                       timeout = convtime(optarg);
+                       if (timeout == -1 || timeout == 0) {
+                               fprintf(stderr, "Bad timeout '%s'\n", optarg);
                                usage();
+                       }
                        break;
                case 'v':
                        if (!debug_flag) {
@@ -766,7 +776,6 @@ main(int argc, char **argv)
                usage();
 
        log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
-       fatal_add_cleanup(fatal_callback, NULL);
 
        maxfd = fdlim_get(1);
        if (maxfd < 0)
This page took 0.095936 seconds and 4 git commands to generate.