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