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