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