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