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