]>
Commit | Line | Data |
---|---|---|
1 | /* $OpenBSD: ssh-keyscan.c,v 1.69 2006/07/22 20:48:23 stevesk Exp $ */ | |
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 | |
7 | * OpenBSD project by leaving this copyright notice intact. | |
8 | */ | |
9 | ||
10 | #include "includes.h" | |
11 | ||
12 | #include "openbsd-compat/sys-queue.h" | |
13 | #include <sys/resource.h> | |
14 | ||
15 | #include <openssl/bn.h> | |
16 | ||
17 | #if defined(HAVE_NETDB_H) | |
18 | # include <netdb.h> | |
19 | #endif | |
20 | #include <errno.h> | |
21 | #include <stdarg.h> | |
22 | #include <setjmp.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | ||
26 | #include "xmalloc.h" | |
27 | #include "ssh.h" | |
28 | #include "ssh1.h" | |
29 | #include "key.h" | |
30 | #include "kex.h" | |
31 | #include "compat.h" | |
32 | #include "myproposal.h" | |
33 | #include "packet.h" | |
34 | #include "dispatch.h" | |
35 | #include "buffer.h" | |
36 | #include "bufaux.h" | |
37 | #include "log.h" | |
38 | #include "atomicio.h" | |
39 | #include "misc.h" | |
40 | #include "hostfile.h" | |
41 | ||
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. */ | |
44 | int IPv4or6 = AF_UNSPEC; | |
45 | ||
46 | int ssh_port = SSH_DEFAULT_PORT; | |
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 */ | |
53 | ||
54 | int hash_hosts = 0; /* Hash hostname on output */ | |
55 | ||
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; | |
62 | #define MAXCON (maxfd - 10) | |
63 | ||
64 | extern char *__progname; | |
65 | fd_set *read_wait; | |
66 | size_t read_wait_nfdset; | |
67 | int ncon; | |
68 | int nonfatal_fatal = 0; | |
69 | jmp_buf kexjmp; | |
70 | Key *kexjmp_key; | |
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 { | |
77 | u_char c_status; /* State of connection on this file desc. */ | |
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. */ | |
86 | int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */ | |
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 */ | |
92 | Kex *c_kex; /* The key-exchange struct for ssh2 */ | |
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; | |
109 | u_int size; | |
110 | int lineno; | |
111 | const char *filename; | |
112 | FILE *stream; | |
113 | void (*errfun) (const char *,...); | |
114 | } Linebuf; | |
115 | ||
116 | static Linebuf * | |
117 | Linebuf_alloc(const char *filename, void (*errfun) (const char *,...)) | |
118 | { | |
119 | Linebuf *lb; | |
120 | ||
121 | if (!(lb = malloc(sizeof(*lb)))) { | |
122 | if (errfun) | |
123 | (*errfun) ("linebuf (%s): malloc failed\n", | |
124 | filename ? filename : "(stdin)"); | |
125 | return (NULL); | |
126 | } | |
127 | if (filename) { | |
128 | lb->filename = filename; | |
129 | if (!(lb->stream = fopen(filename, "r"))) { | |
130 | xfree(lb); | |
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 | ||
140 | if (!(lb->buf = malloc((lb->size = LINEBUF_SIZE)))) { | |
141 | if (errfun) | |
142 | (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); | |
143 | xfree(lb); | |
144 | return (NULL); | |
145 | } | |
146 | lb->errfun = errfun; | |
147 | lb->lineno = 0; | |
148 | return (lb); | |
149 | } | |
150 | ||
151 | static void | |
152 | Linebuf_free(Linebuf * lb) | |
153 | { | |
154 | fclose(lb->stream); | |
155 | xfree(lb->buf); | |
156 | xfree(lb); | |
157 | } | |
158 | ||
159 | #if 0 | |
160 | static void | |
161 | Linebuf_restart(Linebuf * lb) | |
162 | { | |
163 | clearerr(lb->stream); | |
164 | rewind(lb->stream); | |
165 | lb->lineno = 0; | |
166 | } | |
167 | ||
168 | static int | |
169 | Linebuf_lineno(Linebuf * lb) | |
170 | { | |
171 | return (lb->lineno); | |
172 | } | |
173 | #endif | |
174 | ||
175 | static char * | |
176 | Linebuf_getline(Linebuf * lb) | |
177 | { | |
178 | size_t n = 0; | |
179 | void *p; | |
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) | |
186 | (*lb->errfun)("%s: %s\n", lb->filename, | |
187 | strerror(errno)); | |
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) | |
199 | (*lb->errfun)("%s: skipping incomplete last line\n", | |
200 | lb->filename); | |
201 | return (NULL); | |
202 | } | |
203 | /* Double the buffer if we need more space */ | |
204 | lb->size *= 2; | |
205 | if ((p = realloc(lb->buf, lb->size)) == NULL) { | |
206 | lb->size /= 2; | |
207 | if (lb->errfun) | |
208 | (*lb->errfun)("linebuf (%s): realloc failed\n", | |
209 | lb->filename); | |
210 | return (NULL); | |
211 | } | |
212 | lb->buf = p; | |
213 | } | |
214 | } | |
215 | ||
216 | static int | |
217 | fdlim_get(int hard) | |
218 | { | |
219 | #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) | |
220 | struct rlimit rlfd; | |
221 | ||
222 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) | |
223 | return (-1); | |
224 | if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY) | |
225 | return SSH_SYSFDMAX; | |
226 | else | |
227 | return hard ? rlfd.rlim_max : rlfd.rlim_cur; | |
228 | #else | |
229 | return SSH_SYSFDMAX; | |
230 | #endif | |
231 | } | |
232 | ||
233 | static int | |
234 | fdlim_set(int lim) | |
235 | { | |
236 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) | |
237 | struct rlimit rlfd; | |
238 | #endif | |
239 | ||
240 | if (lim <= 0) | |
241 | return (-1); | |
242 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) | |
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); | |
248 | #elif defined (HAVE_SETDTABLESIZE) | |
249 | setdtablesize(lim); | |
250 | #endif | |
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 | */ | |
259 | static char * | |
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 | */ | |
281 | static char * | |
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 | ||
292 | static Key * | |
293 | keygrab_ssh1(con *c) | |
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 | } | |
302 | buffer_append(&msg, c->c_data, c->c_plen); | |
303 | buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */ | |
304 | if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) { | |
305 | error("%s: invalid packet type", c->c_name); | |
306 | buffer_clear(&msg); | |
307 | return NULL; | |
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); | |
320 | ||
321 | buffer_clear(&msg); | |
322 | ||
323 | return (rsa); | |
324 | } | |
325 | ||
326 | static int | |
327 | hostjump(Key *hostkey) | |
328 | { | |
329 | kexjmp_key = hostkey; | |
330 | longjmp(kexjmp, 1); | |
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); | |
359 | c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; | |
360 | c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; | |
361 | c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; | |
362 | c->c_kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; | |
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(); | |
375 | ||
376 | return j < 0? NULL : kexjmp_key; | |
377 | } | |
378 | ||
379 | static void | |
380 | keyprint(con *c, Key *key) | |
381 | { | |
382 | char *host = c->c_output_name ? c->c_output_name : c->c_name; | |
383 | ||
384 | if (!key) | |
385 | return; | |
386 | if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL) | |
387 | fatal("host_hash failed"); | |
388 | ||
389 | fprintf(stdout, "%s ", host); | |
390 | key_write(key, stdout); | |
391 | fputs("\n", stdout); | |
392 | } | |
393 | ||
394 | static int | |
395 | tcpconnect(char *host) | |
396 | { | |
397 | struct addrinfo hints, *ai, *aitop; | |
398 | char strport[NI_MAXSERV]; | |
399 | int gaierr, s = -1; | |
400 | ||
401 | snprintf(strport, sizeof strport, "%d", ssh_port); | |
402 | memset(&hints, 0, sizeof(hints)); | |
403 | hints.ai_family = IPv4or6; | |
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) { | |
408 | s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); | |
409 | if (s < 0) { | |
410 | error("socket: %s", strerror(errno)); | |
411 | continue; | |
412 | } | |
413 | if (set_nonblock(s) == -1) | |
414 | fatal("%s: set_nonblock(%d)", __func__, s); | |
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 | ||
427 | static int | |
428 | conalloc(char *iname, char *oname, int keytype) | |
429 | { | |
430 | char *namebase, *name, *namelist; | |
431 | int s; | |
432 | ||
433 | namebase = namelist = xstrdup(iname); | |
434 | ||
435 | do { | |
436 | name = xstrsep(&namelist, ","); | |
437 | if (!name) { | |
438 | xfree(namebase); | |
439 | return (-1); | |
440 | } | |
441 | } while ((s = tcpconnect(name)) < 0); | |
442 | ||
443 | if (s >= maxfd) | |
444 | fatal("conalloc: fdno %d too high", s); | |
445 | if (fdcon[s].c_status) | |
446 | fatal("conalloc: attempt to reuse fdno %d", s); | |
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; | |
457 | fdcon[s].c_keytype = keytype; | |
458 | gettimeofday(&fdcon[s].c_tv, NULL); | |
459 | fdcon[s].c_tv.tv_sec += timeout; | |
460 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); | |
461 | FD_SET(s, read_wait); | |
462 | ncon++; | |
463 | return (s); | |
464 | } | |
465 | ||
466 | static void | |
467 | confree(int s) | |
468 | { | |
469 | if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) | |
470 | fatal("confree: attempt to free bad fdno %d", s); | |
471 | close(s); | |
472 | xfree(fdcon[s].c_namebase); | |
473 | xfree(fdcon[s].c_output_name); | |
474 | if (fdcon[s].c_status == CS_KEYS) | |
475 | xfree(fdcon[s].c_data); | |
476 | fdcon[s].c_status = CS_UNUSED; | |
477 | fdcon[s].c_keytype = 0; | |
478 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); | |
479 | FD_CLR(s, read_wait); | |
480 | ncon--; | |
481 | } | |
482 | ||
483 | static void | |
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 | ||
492 | static int | |
493 | conrecycle(int s) | |
494 | { | |
495 | con *c = &fdcon[s]; | |
496 | int ret; | |
497 | ||
498 | ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype); | |
499 | confree(s); | |
500 | return (ret); | |
501 | } | |
502 | ||
503 | static void | |
504 | congreet(int s) | |
505 | { | |
506 | int n = 0, remote_major = 0, remote_minor = 0; | |
507 | char buf[256], *cp; | |
508 | char remote_version[sizeof buf]; | |
509 | size_t bufsiz; | |
510 | con *c = &fdcon[s]; | |
511 | ||
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; | |
524 | } | |
525 | if (n == 0) { | |
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 | } | |
536 | conrecycle(s); | |
537 | return; | |
538 | } | |
539 | if (*cp != '\n' && *cp != '\r') { | |
540 | error("%s: bad greeting", c->c_name); | |
541 | confree(s); | |
542 | return; | |
543 | } | |
544 | *cp = '\0'; | |
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; | |
550 | if (c->c_keytype != KT_RSA1) { | |
551 | if (!ssh2_capable(remote_major, remote_minor)) { | |
552 | debug("%s doesn't support ssh2", c->c_name); | |
553 | confree(s); | |
554 | return; | |
555 | } | |
556 | } else if (remote_major != 1) { | |
557 | debug("%s doesn't support ssh1", c->c_name); | |
558 | confree(s); | |
559 | return; | |
560 | } | |
561 | fprintf(stderr, "# %s %s\n", c->c_name, chop(buf)); | |
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); | |
565 | if (n < 0 || (size_t)n >= sizeof(buf)) { | |
566 | error("snprintf: buffer too small"); | |
567 | confree(s); | |
568 | return; | |
569 | } | |
570 | if (atomicio(vwrite, s, buf, n) != (size_t)n) { | |
571 | error("write (%s): %s", c->c_name, strerror(errno)); | |
572 | confree(s); | |
573 | return; | |
574 | } | |
575 | if (c->c_keytype != KT_RSA1) { | |
576 | keyprint(c, keygrab_ssh2(c)); | |
577 | confree(s); | |
578 | return; | |
579 | } | |
580 | c->c_status = CS_SIZE; | |
581 | contouch(s); | |
582 | } | |
583 | ||
584 | static void | |
585 | conread(int s) | |
586 | { | |
587 | con *c = &fdcon[s]; | |
588 | size_t n; | |
589 | ||
590 | if (c->c_status == CS_CON) { | |
591 | congreet(s); | |
592 | return; | |
593 | } | |
594 | n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off); | |
595 | if (n == 0) { | |
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: | |
612 | keyprint(c, keygrab_ssh1(c)); | |
613 | confree(s); | |
614 | return; | |
615 | default: | |
616 | fatal("conread: invalid status %d", c->c_status); | |
617 | break; | |
618 | } | |
619 | ||
620 | contouch(s); | |
621 | } | |
622 | ||
623 | static void | |
624 | conloop(void) | |
625 | { | |
626 | struct timeval seltime, now; | |
627 | fd_set *r, *e; | |
628 | con *c; | |
629 | int i; | |
630 | ||
631 | gettimeofday(&now, NULL); | |
632 | c = TAILQ_FIRST(&tq); | |
633 | ||
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))) { | |
636 | seltime = c->c_tv; | |
637 | seltime.tv_sec -= now.tv_sec; | |
638 | seltime.tv_usec -= now.tv_usec; | |
639 | if (seltime.tv_usec < 0) { | |
640 | seltime.tv_usec += 1000000; | |
641 | seltime.tv_sec--; | |
642 | } | |
643 | } else | |
644 | seltime.tv_sec = seltime.tv_usec = 0; | |
645 | ||
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)); | |
650 | ||
651 | while (select(maxfd, r, NULL, e, &seltime) == -1 && | |
652 | (errno == EAGAIN || errno == EINTR)) | |
653 | ; | |
654 | ||
655 | for (i = 0; i < maxfd; i++) { | |
656 | if (FD_ISSET(i, e)) { | |
657 | error("%s: exception!", fdcon[i].c_name); | |
658 | confree(i); | |
659 | } else if (FD_ISSET(i, r)) | |
660 | conread(i); | |
661 | } | |
662 | xfree(r); | |
663 | xfree(e); | |
664 | ||
665 | c = TAILQ_FIRST(&tq); | |
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))) { | |
668 | int s = c->c_fd; | |
669 | ||
670 | c = TAILQ_NEXT(c, c_link); | |
671 | conrecycle(s); | |
672 | } | |
673 | } | |
674 | ||
675 | static void | |
676 | do_host(char *host) | |
677 | { | |
678 | char *name = strnnsep(&host, " \t\n"); | |
679 | int j; | |
680 | ||
681 | if (name == NULL) | |
682 | return; | |
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); | |
688 | } | |
689 | } | |
690 | } | |
691 | ||
692 | void | |
693 | fatal(const char *fmt,...) | |
694 | { | |
695 | va_list args; | |
696 | ||
697 | va_start(args, fmt); | |
698 | do_log(SYSLOG_LEVEL_FATAL, fmt, args); | |
699 | va_end(args); | |
700 | if (nonfatal_fatal) | |
701 | longjmp(kexjmp, -1); | |
702 | else | |
703 | exit(255); | |
704 | } | |
705 | ||
706 | static void | |
707 | usage(void) | |
708 | { | |
709 | fprintf(stderr, "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n" | |
710 | "\t\t [host | addrlist namelist] [...]\n", | |
711 | __progname); | |
712 | exit(1); | |
713 | } | |
714 | ||
715 | int | |
716 | main(int argc, char **argv) | |
717 | { | |
718 | int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; | |
719 | int opt, fopt_count = 0; | |
720 | char *tname; | |
721 | ||
722 | extern int optind; | |
723 | extern char *optarg; | |
724 | ||
725 | __progname = ssh_get_progname(argv[0]); | |
726 | init_rng(); | |
727 | seed_rng(); | |
728 | TAILQ_INIT(&tq); | |
729 | ||
730 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ | |
731 | sanitise_stdfd(); | |
732 | ||
733 | if (argc <= 1) | |
734 | usage(); | |
735 | ||
736 | while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) { | |
737 | switch (opt) { | |
738 | case 'H': | |
739 | hash_hosts = 1; | |
740 | break; | |
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': | |
749 | timeout = convtime(optarg); | |
750 | if (timeout == -1 || timeout == 0) { | |
751 | fprintf(stderr, "Bad timeout '%s'\n", optarg); | |
752 | usage(); | |
753 | } | |
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; | |
765 | case 'f': | |
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: | |
786 | fatal("unknown key type %s", tname); | |
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: | |
799 | usage(); | |
800 | } | |
801 | } | |
802 | if (optind == argc && !fopt_count) | |
803 | usage(); | |
804 | ||
805 | log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1); | |
806 | ||
807 | maxfd = fdlim_get(1); | |
808 | if (maxfd < 0) | |
809 | fatal("%s: fdlim_get: bad value", __progname); | |
810 | if (maxfd > MAXMAXFD) | |
811 | maxfd = MAXMAXFD; | |
812 | if (MAXCON <= 0) | |
813 | fatal("%s: not enough file descriptors", __progname); | |
814 | if (maxfd > fdlim_get(0)) | |
815 | fdlim_set(maxfd); | |
816 | fdcon = xcalloc(maxfd, sizeof(con)); | |
817 | ||
818 | read_wait_nfdset = howmany(maxfd, NFDBITS); | |
819 | read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask)); | |
820 | ||
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); | |
828 | if (!lb) | |
829 | continue; | |
830 | while ((line = Linebuf_getline(lb)) != NULL) | |
831 | do_host(line); | |
832 | Linebuf_free(lb); | |
833 | } | |
834 | } | |
835 | ||
836 | while (optind < argc) | |
837 | do_host(argv[optind++]); | |
838 | ||
839 | while (ncon > 0) | |
840 | conloop(); | |
841 | ||
842 | return (0); | |
843 | } |