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