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