Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>. |
| 3 | * |
| 4 | * Modification and redistribution in source and binary forms is |
| 5 | * permitted provided that due credit is given to the author and the |
Ben Lindstrom | a238f6e | 2001-06-09 01:30:39 +0000 | [diff] [blame] | 6 | * OpenBSD project by leaving this copyright notice intact. |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
Ben Lindstrom | 61c183b | 2002-06-21 00:09:54 +0000 | [diff] [blame^] | 10 | RCSID("$OpenBSD: ssh-keyscan.c,v 1.36 2002/06/16 21:30:58 itojun Exp $"); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 11 | |
Kevin Steves | 54f15b6 | 2001-03-14 18:37:13 +0000 | [diff] [blame] | 12 | #include "openbsd-compat/fake-queue.h" |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 13 | |
| 14 | #include <openssl/bn.h> |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 15 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 16 | #include <setjmp.h> |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 17 | #include "xmalloc.h" |
| 18 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 19 | #include "ssh1.h" |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 20 | #include "key.h" |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 21 | #include "kex.h" |
| 22 | #include "compat.h" |
| 23 | #include "myproposal.h" |
| 24 | #include "packet.h" |
| 25 | #include "dispatch.h" |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 26 | #include "buffer.h" |
| 27 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 28 | #include "log.h" |
Ben Lindstrom | d20b855 | 2001-03-05 07:01:18 +0000 | [diff] [blame] | 29 | #include "atomicio.h" |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 30 | #include "misc.h" |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 31 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 32 | /* Flag indicating whether IPv4 or IPv6. This can be set on the command line. |
| 33 | Default value is AF_UNSPEC means both IPv4 and IPv6. */ |
Damien Miller | efdc1f1 | 2001-11-15 08:40:45 +1100 | [diff] [blame] | 34 | #ifdef IPV4_DEFAULT |
| 35 | int IPv4or6 = AF_INET; |
| 36 | #else |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 37 | int IPv4or6 = AF_UNSPEC; |
Damien Miller | efdc1f1 | 2001-11-15 08:40:45 +1100 | [diff] [blame] | 38 | #endif |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 39 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 40 | int ssh_port = SSH_DEFAULT_PORT; |
Kevin Steves | 76e7d9b | 2001-09-20 20:30:09 +0000 | [diff] [blame] | 41 | |
| 42 | #define KT_RSA1 1 |
| 43 | #define KT_DSA 2 |
| 44 | #define KT_RSA 4 |
| 45 | |
| 46 | int get_keytypes = KT_RSA1; /* Get only RSA1 keys by default */ |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 47 | |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 48 | #define MAXMAXFD 256 |
| 49 | |
| 50 | /* The number of seconds after which to give up on a TCP connection */ |
| 51 | int timeout = 5; |
| 52 | |
| 53 | int maxfd; |
Ben Lindstrom | d20b855 | 2001-03-05 07:01:18 +0000 | [diff] [blame] | 54 | #define MAXCON (maxfd - 10) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 55 | |
Kevin Steves | ec84dc1 | 2000-12-13 17:45:15 +0000 | [diff] [blame] | 56 | #ifdef HAVE___PROGNAME |
| 57 | extern char *__progname; |
| 58 | #else |
| 59 | char *__progname; |
| 60 | #endif |
Ben Lindstrom | c1e0421 | 2001-03-05 07:04:38 +0000 | [diff] [blame] | 61 | fd_set *read_wait; |
| 62 | size_t read_wait_size; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 63 | int ncon; |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 64 | int nonfatal_fatal = 0; |
| 65 | jmp_buf kexjmp; |
Ben Lindstrom | 520b55c | 2001-09-12 18:05:05 +0000 | [diff] [blame] | 66 | Key *kexjmp_key; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * Keep a connection structure for each file descriptor. The state |
| 70 | * associated with file descriptor n is held in fdcon[n]. |
| 71 | */ |
| 72 | typedef struct Connection { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 73 | u_char c_status; /* State of connection on this file desc. */ |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 74 | #define CS_UNUSED 0 /* File descriptor unused */ |
| 75 | #define CS_CON 1 /* Waiting to connect/read greeting */ |
| 76 | #define CS_SIZE 2 /* Waiting to read initial packet size */ |
| 77 | #define CS_KEYS 3 /* Waiting to read public key packet */ |
| 78 | int c_fd; /* Quick lookup: c->c_fd == c - fdcon */ |
| 79 | int c_plen; /* Packet length field for ssh packet */ |
| 80 | int c_len; /* Total bytes which must be read. */ |
| 81 | int c_off; /* Length of data read so far. */ |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 82 | int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */ |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 83 | char *c_namebase; /* Address to free for c_name and c_namelist */ |
| 84 | char *c_name; /* Hostname of connection for errors */ |
| 85 | char *c_namelist; /* Pointer to other possible addresses */ |
| 86 | char *c_output_name; /* Hostname of connection for output */ |
| 87 | char *c_data; /* Data read from this fd */ |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 88 | Kex *c_kex; /* The key-exchange struct for ssh2 */ |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 89 | struct timeval c_tv; /* Time at which connection gets aborted */ |
| 90 | TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */ |
| 91 | } con; |
| 92 | |
| 93 | TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ |
| 94 | con *fdcon; |
| 95 | |
| 96 | /* |
| 97 | * This is just a wrapper around fgets() to make it usable. |
| 98 | */ |
| 99 | |
| 100 | /* Stress-test. Increase this later. */ |
| 101 | #define LINEBUF_SIZE 16 |
| 102 | |
| 103 | typedef struct { |
| 104 | char *buf; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 105 | u_int size; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 106 | int lineno; |
| 107 | const char *filename; |
| 108 | FILE *stream; |
| 109 | void (*errfun) (const char *,...); |
| 110 | } Linebuf; |
| 111 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 112 | static Linebuf * |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 113 | Linebuf_alloc(const char *filename, void (*errfun) (const char *,...)) |
| 114 | { |
| 115 | Linebuf *lb; |
| 116 | |
| 117 | if (!(lb = malloc(sizeof(*lb)))) { |
| 118 | if (errfun) |
| 119 | (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); |
| 120 | return (NULL); |
| 121 | } |
| 122 | if (filename) { |
| 123 | lb->filename = filename; |
| 124 | if (!(lb->stream = fopen(filename, "r"))) { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 125 | xfree(lb); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 126 | if (errfun) |
| 127 | (*errfun) ("%s: %s\n", filename, strerror(errno)); |
| 128 | return (NULL); |
| 129 | } |
| 130 | } else { |
| 131 | lb->filename = "(stdin)"; |
| 132 | lb->stream = stdin; |
| 133 | } |
| 134 | |
| 135 | if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) { |
| 136 | if (errfun) |
| 137 | (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 138 | xfree(lb); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 139 | return (NULL); |
| 140 | } |
| 141 | lb->errfun = errfun; |
| 142 | lb->lineno = 0; |
| 143 | return (lb); |
| 144 | } |
| 145 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 146 | static void |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 147 | Linebuf_free(Linebuf * lb) |
| 148 | { |
| 149 | fclose(lb->stream); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 150 | xfree(lb->buf); |
| 151 | xfree(lb); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 154 | #if 0 |
| 155 | static void |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 156 | Linebuf_restart(Linebuf * lb) |
| 157 | { |
| 158 | clearerr(lb->stream); |
| 159 | rewind(lb->stream); |
| 160 | lb->lineno = 0; |
| 161 | } |
| 162 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 163 | static int |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 164 | Linebuf_lineno(Linebuf * lb) |
| 165 | { |
| 166 | return (lb->lineno); |
| 167 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 168 | #endif |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 169 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 170 | static char * |
Ben Lindstrom | c791beb | 2001-02-10 23:18:11 +0000 | [diff] [blame] | 171 | Linebuf_getline(Linebuf * lb) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 172 | { |
| 173 | int n = 0; |
| 174 | |
| 175 | lb->lineno++; |
| 176 | for (;;) { |
| 177 | /* Read a line */ |
| 178 | if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) { |
| 179 | if (ferror(lb->stream) && lb->errfun) |
Ben Lindstrom | b0a4cd8 | 2001-03-05 04:54:49 +0000 | [diff] [blame] | 180 | (*lb->errfun) ("%s: %s\n", lb->filename, |
| 181 | strerror(errno)); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 182 | return (NULL); |
| 183 | } |
| 184 | n = strlen(lb->buf); |
| 185 | |
| 186 | /* Return it or an error if it fits */ |
| 187 | if (n > 0 && lb->buf[n - 1] == '\n') { |
| 188 | lb->buf[n - 1] = '\0'; |
| 189 | return (lb->buf); |
| 190 | } |
| 191 | if (n != lb->size - 1) { |
| 192 | if (lb->errfun) |
Ben Lindstrom | b0a4cd8 | 2001-03-05 04:54:49 +0000 | [diff] [blame] | 193 | (*lb->errfun) ("%s: skipping incomplete last line\n", |
| 194 | lb->filename); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 195 | return (NULL); |
| 196 | } |
| 197 | /* Double the buffer if we need more space */ |
| 198 | if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) { |
| 199 | if (lb->errfun) |
Ben Lindstrom | b0a4cd8 | 2001-03-05 04:54:49 +0000 | [diff] [blame] | 200 | (*lb->errfun) ("linebuf (%s): realloc failed\n", |
| 201 | lb->filename); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 202 | return (NULL); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 207 | static int |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 208 | fdlim_get(int hard) |
| 209 | { |
Ben Lindstrom | 5adbad2 | 2000-12-27 07:06:21 +0000 | [diff] [blame] | 210 | #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 211 | struct rlimit rlfd; |
Ben Lindstrom | b0a4cd8 | 2001-03-05 04:54:49 +0000 | [diff] [blame] | 212 | |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 213 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) |
| 214 | return (-1); |
| 215 | if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY) |
| 216 | return 10000; |
| 217 | else |
| 218 | return hard ? rlfd.rlim_max : rlfd.rlim_cur; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 219 | #elif defined (HAVE_SYSCONF) |
| 220 | return sysconf (_SC_OPEN_MAX); |
| 221 | #else |
| 222 | return 10000; |
| 223 | #endif |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 226 | static int |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 227 | fdlim_set(int lim) |
| 228 | { |
Ben Lindstrom | 5adbad2 | 2000-12-27 07:06:21 +0000 | [diff] [blame] | 229 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 230 | struct rlimit rlfd; |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 231 | #endif |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 232 | if (lim <= 0) |
| 233 | return (-1); |
Ben Lindstrom | 5adbad2 | 2000-12-27 07:06:21 +0000 | [diff] [blame] | 234 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 235 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) |
| 236 | return (-1); |
| 237 | rlfd.rlim_cur = lim; |
| 238 | if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0) |
| 239 | return (-1); |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 240 | #elif defined (HAVE_SETDTABLESIZE) |
Kevin Steves | 28a7f26 | 2001-02-05 15:43:59 +0000 | [diff] [blame] | 241 | setdtablesize(lim); |
Ben Lindstrom | 2c467a2 | 2000-12-27 04:57:41 +0000 | [diff] [blame] | 242 | #endif |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 243 | return (0); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * This is an strsep function that returns a null field for adjacent |
| 248 | * separators. This is the same as the 4.4BSD strsep, but different from the |
| 249 | * one in the GNU libc. |
| 250 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 251 | static char * |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 252 | xstrsep(char **str, const char *delim) |
| 253 | { |
| 254 | char *s, *e; |
| 255 | |
| 256 | if (!**str) |
| 257 | return (NULL); |
| 258 | |
| 259 | s = *str; |
| 260 | e = s + strcspn(s, delim); |
| 261 | |
| 262 | if (*e != '\0') |
| 263 | *e++ = '\0'; |
| 264 | *str = e; |
| 265 | |
| 266 | return (s); |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Get the next non-null token (like GNU strsep). Strsep() will return a |
| 271 | * null token for two adjacent separators, so we may have to loop. |
| 272 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 273 | static char * |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 274 | strnnsep(char **stringp, char *delim) |
| 275 | { |
| 276 | char *tok; |
| 277 | |
| 278 | do { |
| 279 | tok = xstrsep(stringp, delim); |
| 280 | } while (tok && *tok == '\0'); |
| 281 | return (tok); |
| 282 | } |
| 283 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 284 | static Key * |
| 285 | keygrab_ssh1(con *c) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 286 | { |
| 287 | static Key *rsa; |
| 288 | static Buffer msg; |
| 289 | |
| 290 | if (rsa == NULL) { |
| 291 | buffer_init(&msg); |
| 292 | rsa = key_new(KEY_RSA1); |
| 293 | } |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 294 | buffer_append(&msg, c->c_data, c->c_plen); |
| 295 | buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */ |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 296 | if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) { |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 297 | error("%s: invalid packet type", c->c_name); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 298 | buffer_clear(&msg); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 299 | return NULL; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 300 | } |
| 301 | buffer_consume(&msg, 8); /* cookie */ |
| 302 | |
| 303 | /* server key */ |
| 304 | (void) buffer_get_int(&msg); |
| 305 | buffer_get_bignum(&msg, rsa->rsa->e); |
| 306 | buffer_get_bignum(&msg, rsa->rsa->n); |
| 307 | |
| 308 | /* host key */ |
| 309 | (void) buffer_get_int(&msg); |
| 310 | buffer_get_bignum(&msg, rsa->rsa->e); |
| 311 | buffer_get_bignum(&msg, rsa->rsa->n); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 312 | |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 313 | buffer_clear(&msg); |
| 314 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 315 | return (rsa); |
| 316 | } |
| 317 | |
| 318 | static int |
| 319 | hostjump(Key *hostkey) |
| 320 | { |
Ben Lindstrom | 520b55c | 2001-09-12 18:05:05 +0000 | [diff] [blame] | 321 | kexjmp_key = hostkey; |
| 322 | longjmp(kexjmp, 1); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static int |
| 326 | ssh2_capable(int remote_major, int remote_minor) |
| 327 | { |
| 328 | switch (remote_major) { |
| 329 | case 1: |
| 330 | if (remote_minor == 99) |
| 331 | return 1; |
| 332 | break; |
| 333 | case 2: |
| 334 | return 1; |
| 335 | default: |
| 336 | break; |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static Key * |
| 342 | keygrab_ssh2(con *c) |
| 343 | { |
| 344 | int j; |
| 345 | |
| 346 | packet_set_connection(c->c_fd, c->c_fd); |
| 347 | enable_compat20(); |
| 348 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA? |
| 349 | "ssh-dss": "ssh-rsa"; |
| 350 | c->c_kex = kex_setup(myproposal); |
| 351 | c->c_kex->verify_host_key = hostjump; |
| 352 | |
| 353 | if (!(j = setjmp(kexjmp))) { |
| 354 | nonfatal_fatal = 1; |
| 355 | dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex); |
| 356 | fprintf(stderr, "Impossible! dispatch_run() returned!\n"); |
| 357 | exit(1); |
| 358 | } |
| 359 | nonfatal_fatal = 0; |
| 360 | xfree(c->c_kex); |
| 361 | c->c_kex = NULL; |
| 362 | packet_close(); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 363 | |
Ben Lindstrom | 520b55c | 2001-09-12 18:05:05 +0000 | [diff] [blame] | 364 | return j < 0? NULL : kexjmp_key; |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static void |
| 368 | keyprint(con *c, Key *key) |
| 369 | { |
| 370 | if (!key) |
| 371 | return; |
| 372 | |
| 373 | fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name); |
| 374 | key_write(key, stdout); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 375 | fputs("\n", stdout); |
| 376 | } |
| 377 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 378 | static int |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 379 | tcpconnect(char *host) |
| 380 | { |
| 381 | struct addrinfo hints, *ai, *aitop; |
| 382 | char strport[NI_MAXSERV]; |
| 383 | int gaierr, s = -1; |
| 384 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 385 | snprintf(strport, sizeof strport, "%d", ssh_port); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 386 | memset(&hints, 0, sizeof(hints)); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 387 | hints.ai_family = IPv4or6; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 388 | hints.ai_socktype = SOCK_STREAM; |
| 389 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) |
| 390 | fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr)); |
| 391 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 392 | s = socket(ai->ai_family, SOCK_STREAM, 0); |
| 393 | if (s < 0) { |
| 394 | error("socket: %s", strerror(errno)); |
| 395 | continue; |
| 396 | } |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 397 | if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 398 | fatal("F_SETFL: %s", strerror(errno)); |
| 399 | if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 && |
| 400 | errno != EINPROGRESS) |
| 401 | error("connect (`%s'): %s", host, strerror(errno)); |
| 402 | else |
| 403 | break; |
| 404 | close(s); |
| 405 | s = -1; |
| 406 | } |
| 407 | freeaddrinfo(aitop); |
| 408 | return s; |
| 409 | } |
| 410 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 411 | static int |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 412 | conalloc(char *iname, char *oname, int keytype) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 413 | { |
| 414 | int s; |
| 415 | char *namebase, *name, *namelist; |
| 416 | |
| 417 | namebase = namelist = xstrdup(iname); |
| 418 | |
| 419 | do { |
| 420 | name = xstrsep(&namelist, ","); |
| 421 | if (!name) { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 422 | xfree(namebase); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 423 | return (-1); |
| 424 | } |
| 425 | } while ((s = tcpconnect(name)) < 0); |
| 426 | |
| 427 | if (s >= maxfd) |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 428 | fatal("conalloc: fdno %d too high", s); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 429 | if (fdcon[s].c_status) |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 430 | fatal("conalloc: attempt to reuse fdno %d", s); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 431 | |
| 432 | fdcon[s].c_fd = s; |
| 433 | fdcon[s].c_status = CS_CON; |
| 434 | fdcon[s].c_namebase = namebase; |
| 435 | fdcon[s].c_name = name; |
| 436 | fdcon[s].c_namelist = namelist; |
| 437 | fdcon[s].c_output_name = xstrdup(oname); |
| 438 | fdcon[s].c_data = (char *) &fdcon[s].c_plen; |
| 439 | fdcon[s].c_len = 4; |
| 440 | fdcon[s].c_off = 0; |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 441 | fdcon[s].c_keytype = keytype; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 442 | gettimeofday(&fdcon[s].c_tv, NULL); |
| 443 | fdcon[s].c_tv.tv_sec += timeout; |
| 444 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); |
Ben Lindstrom | c1e0421 | 2001-03-05 07:04:38 +0000 | [diff] [blame] | 445 | FD_SET(s, read_wait); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 446 | ncon++; |
| 447 | return (s); |
| 448 | } |
| 449 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 450 | static void |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 451 | confree(int s) |
| 452 | { |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 453 | if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 454 | fatal("confree: attempt to free bad fdno %d", s); |
Ben Lindstrom | d20b855 | 2001-03-05 07:01:18 +0000 | [diff] [blame] | 455 | close(s); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 456 | xfree(fdcon[s].c_namebase); |
| 457 | xfree(fdcon[s].c_output_name); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 458 | if (fdcon[s].c_status == CS_KEYS) |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 459 | xfree(fdcon[s].c_data); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 460 | fdcon[s].c_status = CS_UNUSED; |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 461 | fdcon[s].c_keytype = 0; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 462 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); |
Ben Lindstrom | c1e0421 | 2001-03-05 07:04:38 +0000 | [diff] [blame] | 463 | FD_CLR(s, read_wait); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 464 | ncon--; |
| 465 | } |
| 466 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 467 | static void |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 468 | contouch(int s) |
| 469 | { |
| 470 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); |
| 471 | gettimeofday(&fdcon[s].c_tv, NULL); |
| 472 | fdcon[s].c_tv.tv_sec += timeout; |
| 473 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); |
| 474 | } |
| 475 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 476 | static int |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 477 | conrecycle(int s) |
| 478 | { |
| 479 | int ret; |
| 480 | con *c = &fdcon[s]; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 481 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 482 | ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 483 | confree(s); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 484 | return (ret); |
| 485 | } |
| 486 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 487 | static void |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 488 | congreet(int s) |
| 489 | { |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 490 | char buf[256], *cp; |
Damien Miller | 83c02ef | 2001-12-21 12:45:43 +1100 | [diff] [blame] | 491 | char remote_version[sizeof buf]; |
Ben Lindstrom | 884a4ac | 2001-03-06 03:33:04 +0000 | [diff] [blame] | 492 | size_t bufsiz; |
Damien Miller | 83c02ef | 2001-12-21 12:45:43 +1100 | [diff] [blame] | 493 | int remote_major, remote_minor, n = 0; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 494 | con *c = &fdcon[s]; |
| 495 | |
Ben Lindstrom | 884a4ac | 2001-03-06 03:33:04 +0000 | [diff] [blame] | 496 | bufsiz = sizeof(buf); |
| 497 | cp = buf; |
Ben Lindstrom | de8fc6f | 2001-08-06 22:43:50 +0000 | [diff] [blame] | 498 | while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n') { |
| 499 | if (*cp == '\r') |
| 500 | *cp = '\n'; |
Ben Lindstrom | 884a4ac | 2001-03-06 03:33:04 +0000 | [diff] [blame] | 501 | cp++; |
Ben Lindstrom | de8fc6f | 2001-08-06 22:43:50 +0000 | [diff] [blame] | 502 | } |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 503 | if (n < 0) { |
| 504 | if (errno != ECONNREFUSED) |
| 505 | error("read (%s): %s", c->c_name, strerror(errno)); |
| 506 | conrecycle(s); |
| 507 | return; |
| 508 | } |
Ben Lindstrom | 6b28c35 | 2002-03-05 01:54:52 +0000 | [diff] [blame] | 509 | if (n == 0) { |
| 510 | error("%s: Connection closed by remote host", c->c_name); |
| 511 | conrecycle(s); |
| 512 | return; |
| 513 | } |
Ben Lindstrom | 884a4ac | 2001-03-06 03:33:04 +0000 | [diff] [blame] | 514 | if (*cp != '\n' && *cp != '\r') { |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 515 | error("%s: bad greeting", c->c_name); |
| 516 | confree(s); |
| 517 | return; |
| 518 | } |
Ben Lindstrom | 884a4ac | 2001-03-06 03:33:04 +0000 | [diff] [blame] | 519 | *cp = '\0'; |
Damien Miller | 83c02ef | 2001-12-21 12:45:43 +1100 | [diff] [blame] | 520 | if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", |
| 521 | &remote_major, &remote_minor, remote_version) == 3) |
| 522 | compat_datafellows(remote_version); |
| 523 | else |
| 524 | datafellows = 0; |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 525 | if (c->c_keytype != KT_RSA1) { |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 526 | if (!ssh2_capable(remote_major, remote_minor)) { |
| 527 | debug("%s doesn't support ssh2", c->c_name); |
| 528 | confree(s); |
| 529 | return; |
| 530 | } |
Damien Miller | 83c02ef | 2001-12-21 12:45:43 +1100 | [diff] [blame] | 531 | } else if (remote_major != 1) { |
| 532 | debug("%s doesn't support ssh1", c->c_name); |
| 533 | confree(s); |
| 534 | return; |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 535 | } |
Ben Lindstrom | de8fc6f | 2001-08-06 22:43:50 +0000 | [diff] [blame] | 536 | fprintf(stderr, "# %s %s\n", c->c_name, chop(buf)); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 537 | n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n", |
| 538 | c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2, |
| 539 | c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2); |
Ben Lindstrom | d20b855 | 2001-03-05 07:01:18 +0000 | [diff] [blame] | 540 | if (atomicio(write, s, buf, n) != n) { |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 541 | error("write (%s): %s", c->c_name, strerror(errno)); |
| 542 | confree(s); |
| 543 | return; |
| 544 | } |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 545 | if (c->c_keytype != KT_RSA1) { |
| 546 | keyprint(c, keygrab_ssh2(c)); |
| 547 | confree(s); |
| 548 | return; |
| 549 | } |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 550 | c->c_status = CS_SIZE; |
| 551 | contouch(s); |
| 552 | } |
| 553 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 554 | static void |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 555 | conread(int s) |
| 556 | { |
| 557 | int n; |
| 558 | con *c = &fdcon[s]; |
| 559 | |
| 560 | if (c->c_status == CS_CON) { |
| 561 | congreet(s); |
| 562 | return; |
| 563 | } |
| 564 | n = read(s, c->c_data + c->c_off, c->c_len - c->c_off); |
| 565 | if (n < 0) { |
| 566 | error("read (%s): %s", c->c_name, strerror(errno)); |
| 567 | confree(s); |
| 568 | return; |
| 569 | } |
| 570 | c->c_off += n; |
| 571 | |
| 572 | if (c->c_off == c->c_len) |
| 573 | switch (c->c_status) { |
| 574 | case CS_SIZE: |
| 575 | c->c_plen = htonl(c->c_plen); |
| 576 | c->c_len = c->c_plen + 8 - (c->c_plen & 7); |
| 577 | c->c_off = 0; |
| 578 | c->c_data = xmalloc(c->c_len); |
| 579 | c->c_status = CS_KEYS; |
| 580 | break; |
| 581 | case CS_KEYS: |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 582 | keyprint(c, keygrab_ssh1(c)); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 583 | confree(s); |
| 584 | return; |
| 585 | break; |
| 586 | default: |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 587 | fatal("conread: invalid status %d", c->c_status); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | |
| 591 | contouch(s); |
| 592 | } |
| 593 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 594 | static void |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 595 | conloop(void) |
| 596 | { |
Ben Lindstrom | c1e0421 | 2001-03-05 07:04:38 +0000 | [diff] [blame] | 597 | fd_set *r, *e; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 598 | struct timeval seltime, now; |
| 599 | int i; |
| 600 | con *c; |
| 601 | |
| 602 | gettimeofday(&now, NULL); |
Ben Lindstrom | 61c183b | 2002-06-21 00:09:54 +0000 | [diff] [blame^] | 603 | c = TAILQ_FIRST(&tq); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 604 | |
Ben Lindstrom | d20b855 | 2001-03-05 07:01:18 +0000 | [diff] [blame] | 605 | if (c && (c->c_tv.tv_sec > now.tv_sec || |
| 606 | (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) { |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 607 | seltime = c->c_tv; |
| 608 | seltime.tv_sec -= now.tv_sec; |
| 609 | seltime.tv_usec -= now.tv_usec; |
Ben Lindstrom | c791beb | 2001-02-10 23:18:11 +0000 | [diff] [blame] | 610 | if (seltime.tv_usec < 0) { |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 611 | seltime.tv_usec += 1000000; |
| 612 | seltime.tv_sec--; |
| 613 | } |
| 614 | } else |
| 615 | seltime.tv_sec = seltime.tv_usec = 0; |
| 616 | |
Ben Lindstrom | c1e0421 | 2001-03-05 07:04:38 +0000 | [diff] [blame] | 617 | r = xmalloc(read_wait_size); |
| 618 | memcpy(r, read_wait, read_wait_size); |
| 619 | e = xmalloc(read_wait_size); |
| 620 | memcpy(e, read_wait, read_wait_size); |
| 621 | |
| 622 | while (select(maxfd, r, NULL, e, &seltime) == -1 && |
Ben Lindstrom | f945251 | 2001-02-15 03:12:08 +0000 | [diff] [blame] | 623 | (errno == EAGAIN || errno == EINTR)) |
| 624 | ; |
| 625 | |
Ben Lindstrom | d20b855 | 2001-03-05 07:01:18 +0000 | [diff] [blame] | 626 | for (i = 0; i < maxfd; i++) { |
Ben Lindstrom | c1e0421 | 2001-03-05 07:04:38 +0000 | [diff] [blame] | 627 | if (FD_ISSET(i, e)) { |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 628 | error("%s: exception!", fdcon[i].c_name); |
| 629 | confree(i); |
Ben Lindstrom | c1e0421 | 2001-03-05 07:04:38 +0000 | [diff] [blame] | 630 | } else if (FD_ISSET(i, r)) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 631 | conread(i); |
Ben Lindstrom | d20b855 | 2001-03-05 07:01:18 +0000 | [diff] [blame] | 632 | } |
Ben Lindstrom | c1e0421 | 2001-03-05 07:04:38 +0000 | [diff] [blame] | 633 | xfree(r); |
| 634 | xfree(e); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 635 | |
Ben Lindstrom | 61c183b | 2002-06-21 00:09:54 +0000 | [diff] [blame^] | 636 | c = TAILQ_FIRST(&tq); |
Ben Lindstrom | d20b855 | 2001-03-05 07:01:18 +0000 | [diff] [blame] | 637 | while (c && (c->c_tv.tv_sec < now.tv_sec || |
| 638 | (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) { |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 639 | int s = c->c_fd; |
Ben Lindstrom | d20b855 | 2001-03-05 07:01:18 +0000 | [diff] [blame] | 640 | |
Ben Lindstrom | 61c183b | 2002-06-21 00:09:54 +0000 | [diff] [blame^] | 641 | c = TAILQ_NEXT(c, c_link); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 642 | conrecycle(s); |
| 643 | } |
| 644 | } |
| 645 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 646 | static void |
| 647 | do_host(char *host) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 648 | { |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 649 | char *name = strnnsep(&host, " \t\n"); |
| 650 | int j; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 651 | |
Ben Lindstrom | eaffb9d | 2001-12-06 16:28:19 +0000 | [diff] [blame] | 652 | if (name == NULL) |
| 653 | return; |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 654 | for (j = KT_RSA1; j <= KT_RSA; j *= 2) { |
| 655 | if (get_keytypes & j) { |
| 656 | while (ncon >= MAXCON) |
| 657 | conloop(); |
| 658 | conalloc(name, *host ? host : name, j); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
Ben Lindstrom | 9c8edc9 | 2002-02-26 17:52:14 +0000 | [diff] [blame] | 663 | void |
| 664 | fatal(const char *fmt,...) |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 665 | { |
Ben Lindstrom | 9c8edc9 | 2002-02-26 17:52:14 +0000 | [diff] [blame] | 666 | va_list args; |
| 667 | va_start(args, fmt); |
| 668 | do_log(SYSLOG_LEVEL_FATAL, fmt, args); |
| 669 | va_end(args); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 670 | if (nonfatal_fatal) |
| 671 | longjmp(kexjmp, -1); |
Ben Lindstrom | 9c8edc9 | 2002-02-26 17:52:14 +0000 | [diff] [blame] | 672 | else |
| 673 | fatal_cleanup(); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | static void |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 677 | usage(void) |
| 678 | { |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 679 | fprintf(stderr, "Usage: %s [options] host ...\n", |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 680 | __progname); |
| 681 | fprintf(stderr, "Options:\n"); |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 682 | fprintf(stderr, " -f file Read hosts or addresses from file.\n"); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 683 | fprintf(stderr, " -p port Connect to the specified port.\n"); |
| 684 | fprintf(stderr, " -t keytype Specify the host key type.\n"); |
| 685 | fprintf(stderr, " -T timeout Set connection timeout.\n"); |
Ben Lindstrom | 9c8edc9 | 2002-02-26 17:52:14 +0000 | [diff] [blame] | 686 | fprintf(stderr, " -v Verbose; display verbose debugging messages.\n"); |
| 687 | fprintf(stderr, " -4 Use IPv4 only.\n"); |
| 688 | fprintf(stderr, " -6 Use IPv6 only.\n"); |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 689 | exit(1); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | int |
| 693 | main(int argc, char **argv) |
| 694 | { |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 695 | int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; |
| 696 | int opt, fopt_count = 0; |
| 697 | char *tname; |
Kevin Steves | 76e7d9b | 2001-09-20 20:30:09 +0000 | [diff] [blame] | 698 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 699 | extern int optind; |
| 700 | extern char *optarg; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 701 | |
Kevin Steves | ec84dc1 | 2000-12-13 17:45:15 +0000 | [diff] [blame] | 702 | __progname = get_progname(argv[0]); |
Ben Lindstrom | 4e088e4 | 2001-10-10 20:45:43 +0000 | [diff] [blame] | 703 | init_rng(); |
| 704 | seed_rng(); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 705 | TAILQ_INIT(&tq); |
| 706 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 707 | if (argc <= 1) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 708 | usage(); |
| 709 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 710 | while ((opt = getopt(argc, argv, "v46p:T:t:f:")) != -1) { |
| 711 | switch (opt) { |
| 712 | case 'p': |
| 713 | ssh_port = a2port(optarg); |
| 714 | if (ssh_port == 0) { |
| 715 | fprintf(stderr, "Bad port '%s'\n", optarg); |
| 716 | exit(1); |
| 717 | } |
| 718 | break; |
| 719 | case 'T': |
| 720 | timeout = atoi(optarg); |
| 721 | if (timeout <= 0) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 722 | usage(); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 723 | break; |
| 724 | case 'v': |
| 725 | if (!debug_flag) { |
| 726 | debug_flag = 1; |
| 727 | log_level = SYSLOG_LEVEL_DEBUG1; |
| 728 | } |
| 729 | else if (log_level < SYSLOG_LEVEL_DEBUG3) |
| 730 | log_level++; |
| 731 | else |
| 732 | fatal("Too high debugging level."); |
| 733 | break; |
Kevin Steves | 76e7d9b | 2001-09-20 20:30:09 +0000 | [diff] [blame] | 734 | case 'f': |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 735 | if (strcmp(optarg, "-") == 0) |
| 736 | optarg = NULL; |
| 737 | argv[fopt_count++] = optarg; |
| 738 | break; |
| 739 | case 't': |
| 740 | get_keytypes = 0; |
| 741 | tname = strtok(optarg, ","); |
| 742 | while (tname) { |
| 743 | int type = key_type_from_name(tname); |
| 744 | switch (type) { |
| 745 | case KEY_RSA1: |
| 746 | get_keytypes |= KT_RSA1; |
| 747 | break; |
| 748 | case KEY_DSA: |
| 749 | get_keytypes |= KT_DSA; |
| 750 | break; |
| 751 | case KEY_RSA: |
| 752 | get_keytypes |= KT_RSA; |
| 753 | break; |
| 754 | case KEY_UNSPEC: |
Ben Lindstrom | 28c603b | 2001-12-06 16:45:10 +0000 | [diff] [blame] | 755 | fatal("unknown key type %s", tname); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 756 | } |
| 757 | tname = strtok(NULL, ","); |
| 758 | } |
| 759 | break; |
| 760 | case '4': |
| 761 | IPv4or6 = AF_INET; |
| 762 | break; |
| 763 | case '6': |
| 764 | IPv4or6 = AF_INET6; |
| 765 | break; |
| 766 | case '?': |
| 767 | default: |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 768 | usage(); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 769 | } |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 770 | } |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 771 | if (optind == argc && !fopt_count) |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 772 | usage(); |
| 773 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 774 | log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1); |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 775 | |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 776 | maxfd = fdlim_get(1); |
| 777 | if (maxfd < 0) |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 778 | fatal("%s: fdlim_get: bad value", __progname); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 779 | if (maxfd > MAXMAXFD) |
| 780 | maxfd = MAXMAXFD; |
Ben Lindstrom | d20b855 | 2001-03-05 07:01:18 +0000 | [diff] [blame] | 781 | if (MAXCON <= 0) |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 782 | fatal("%s: not enough file descriptors", __progname); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 783 | if (maxfd > fdlim_get(0)) |
| 784 | fdlim_set(maxfd); |
| 785 | fdcon = xmalloc(maxfd * sizeof(con)); |
Ben Lindstrom | c791beb | 2001-02-10 23:18:11 +0000 | [diff] [blame] | 786 | memset(fdcon, 0, maxfd * sizeof(con)); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 787 | |
Ben Lindstrom | c1e0421 | 2001-03-05 07:04:38 +0000 | [diff] [blame] | 788 | read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask); |
| 789 | read_wait = xmalloc(read_wait_size); |
| 790 | memset(read_wait, 0, read_wait_size); |
| 791 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 792 | if (fopt_count) { |
| 793 | Linebuf *lb; |
| 794 | char *line; |
| 795 | int j; |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 796 | |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 797 | for (j = 0; j < fopt_count; j++) { |
| 798 | lb = Linebuf_alloc(argv[j], error); |
Ben Lindstrom | 78bbd9e | 2001-09-12 17:10:40 +0000 | [diff] [blame] | 799 | if (!lb) |
| 800 | continue; |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 801 | while ((line = Linebuf_getline(lb)) != NULL) |
| 802 | do_host(line); |
| 803 | Linebuf_free(lb); |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 804 | } |
Ben Lindstrom | 325e70c | 2001-08-06 22:41:30 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | while (optind < argc) |
| 808 | do_host(argv[optind++]); |
| 809 | |
Ben Lindstrom | b6434ae | 2000-12-05 01:15:09 +0000 | [diff] [blame] | 810 | while (ncon > 0) |
| 811 | conloop(); |
| 812 | |
| 813 | return (0); |
| 814 | } |