]>
Commit | Line | Data |
---|---|---|
f6fdbddf | 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 | |
aa144206 | 6 | * OpenBSD project by leaving this copyright notice intact. |
f6fdbddf | 7 | */ |
8 | ||
9 | #include "includes.h" | |
48ba8dc8 | 10 | RCSID("$OpenBSD: ssh-keyscan.c,v 1.36 2002/06/16 21:30:58 itojun Exp $"); |
f6fdbddf | 11 | |
51e2fc8f | 12 | #include "openbsd-compat/fake-queue.h" |
f6fdbddf | 13 | |
14 | #include <openssl/bn.h> | |
f6fdbddf | 15 | |
5061072f | 16 | #include <setjmp.h> |
f6fdbddf | 17 | #include "xmalloc.h" |
18 | #include "ssh.h" | |
42f11eb2 | 19 | #include "ssh1.h" |
f6fdbddf | 20 | #include "key.h" |
5061072f | 21 | #include "kex.h" |
22 | #include "compat.h" | |
23 | #include "myproposal.h" | |
24 | #include "packet.h" | |
25 | #include "dispatch.h" | |
f6fdbddf | 26 | #include "buffer.h" |
27 | #include "bufaux.h" | |
42f11eb2 | 28 | #include "log.h" |
2671b47f | 29 | #include "atomicio.h" |
5061072f | 30 | #include "misc.h" |
f6fdbddf | 31 | |
5061072f | 32 | /* Flag indicating whether IPv4 or IPv6. This can be set on the command line. |
33 | Default value is AF_UNSPEC means both IPv4 and IPv6. */ | |
20716479 | 34 | #ifdef IPV4_DEFAULT |
35 | int IPv4or6 = AF_INET; | |
36 | #else | |
5061072f | 37 | int IPv4or6 = AF_UNSPEC; |
20716479 | 38 | #endif |
f6fdbddf | 39 | |
5061072f | 40 | int ssh_port = SSH_DEFAULT_PORT; |
a204806b | 41 | |
42 | #define KT_RSA1 1 | |
43 | #define KT_DSA 2 | |
44 | #define KT_RSA 4 | |
45 | ||
46 | int get_keytypes = KT_RSA1; /* Get only RSA1 keys by default */ | |
f6fdbddf | 47 | |
f6fdbddf | 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; | |
2671b47f | 54 | #define MAXCON (maxfd - 10) |
f6fdbddf | 55 | |
1fe6a48f | 56 | #ifdef HAVE___PROGNAME |
57 | extern char *__progname; | |
58 | #else | |
59 | char *__progname; | |
60 | #endif | |
ff7fee59 | 61 | fd_set *read_wait; |
62 | size_t read_wait_size; | |
f6fdbddf | 63 | int ncon; |
5061072f | 64 | int nonfatal_fatal = 0; |
65 | jmp_buf kexjmp; | |
4278ff63 | 66 | Key *kexjmp_key; |
f6fdbddf | 67 | |
68 | /* | |
69 | * Keep a connection structure for each file descriptor. The state | |
70 | * associated with file descriptor n is held in fdcon[n]. | |
71 | */ | |
72 | typedef struct Connection { | |
1e3b8b07 | 73 | u_char c_status; /* State of connection on this file desc. */ |
f6fdbddf | 74 | #define CS_UNUSED 0 /* File descriptor unused */ |
75 | #define CS_CON 1 /* Waiting to connect/read greeting */ | |
76 | #define CS_SIZE 2 /* Waiting to read initial packet size */ | |
77 | #define CS_KEYS 3 /* Waiting to read public key packet */ | |
78 | int c_fd; /* Quick lookup: c->c_fd == c - fdcon */ | |
79 | int c_plen; /* Packet length field for ssh packet */ | |
80 | int c_len; /* Total bytes which must be read. */ | |
81 | int c_off; /* Length of data read so far. */ | |
5061072f | 82 | int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */ |
f6fdbddf | 83 | char *c_namebase; /* Address to free for c_name and c_namelist */ |
84 | char *c_name; /* Hostname of connection for errors */ | |
85 | char *c_namelist; /* Pointer to other possible addresses */ | |
86 | char *c_output_name; /* Hostname of connection for output */ | |
87 | char *c_data; /* Data read from this fd */ | |
5061072f | 88 | Kex *c_kex; /* The key-exchange struct for ssh2 */ |
f6fdbddf | 89 | struct timeval c_tv; /* Time at which connection gets aborted */ |
90 | TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */ | |
91 | } con; | |
92 | ||
93 | TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ | |
94 | con *fdcon; | |
95 | ||
96 | /* | |
97 | * This is just a wrapper around fgets() to make it usable. | |
98 | */ | |
99 | ||
100 | /* Stress-test. Increase this later. */ | |
101 | #define LINEBUF_SIZE 16 | |
102 | ||
103 | typedef struct { | |
104 | char *buf; | |
1e3b8b07 | 105 | u_int size; |
f6fdbddf | 106 | int lineno; |
107 | const char *filename; | |
108 | FILE *stream; | |
109 | void (*errfun) (const char *,...); | |
110 | } Linebuf; | |
111 | ||
396c147e | 112 | static Linebuf * |
f6fdbddf | 113 | Linebuf_alloc(const char *filename, void (*errfun) (const char *,...)) |
114 | { | |
115 | Linebuf *lb; | |
116 | ||
117 | if (!(lb = malloc(sizeof(*lb)))) { | |
118 | if (errfun) | |
119 | (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); | |
120 | return (NULL); | |
121 | } | |
122 | if (filename) { | |
123 | lb->filename = filename; | |
124 | if (!(lb->stream = fopen(filename, "r"))) { | |
b5c334cc | 125 | xfree(lb); |
f6fdbddf | 126 | if (errfun) |
127 | (*errfun) ("%s: %s\n", filename, strerror(errno)); | |
128 | return (NULL); | |
129 | } | |
130 | } else { | |
131 | lb->filename = "(stdin)"; | |
132 | lb->stream = stdin; | |
133 | } | |
134 | ||
135 | if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) { | |
136 | if (errfun) | |
137 | (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); | |
b5c334cc | 138 | xfree(lb); |
f6fdbddf | 139 | return (NULL); |
140 | } | |
141 | lb->errfun = errfun; | |
142 | lb->lineno = 0; | |
143 | return (lb); | |
144 | } | |
145 | ||
396c147e | 146 | static void |
f6fdbddf | 147 | Linebuf_free(Linebuf * lb) |
148 | { | |
149 | fclose(lb->stream); | |
b5c334cc | 150 | xfree(lb->buf); |
151 | xfree(lb); | |
f6fdbddf | 152 | } |
153 | ||
396c147e | 154 | #if 0 |
155 | static void | |
f6fdbddf | 156 | Linebuf_restart(Linebuf * lb) |
157 | { | |
158 | clearerr(lb->stream); | |
159 | rewind(lb->stream); | |
160 | lb->lineno = 0; | |
161 | } | |
162 | ||
396c147e | 163 | static int |
f6fdbddf | 164 | Linebuf_lineno(Linebuf * lb) |
165 | { | |
166 | return (lb->lineno); | |
167 | } | |
396c147e | 168 | #endif |
f6fdbddf | 169 | |
396c147e | 170 | static char * |
5be2ec5e | 171 | Linebuf_getline(Linebuf * lb) |
f6fdbddf | 172 | { |
173 | int n = 0; | |
174 | ||
175 | lb->lineno++; | |
176 | for (;;) { | |
177 | /* Read a line */ | |
178 | if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) { | |
179 | if (ferror(lb->stream) && lb->errfun) | |
778f6940 | 180 | (*lb->errfun) ("%s: %s\n", lb->filename, |
181 | strerror(errno)); | |
f6fdbddf | 182 | return (NULL); |
183 | } | |
184 | n = strlen(lb->buf); | |
185 | ||
186 | /* Return it or an error if it fits */ | |
187 | if (n > 0 && lb->buf[n - 1] == '\n') { | |
188 | lb->buf[n - 1] = '\0'; | |
189 | return (lb->buf); | |
190 | } | |
191 | if (n != lb->size - 1) { | |
192 | if (lb->errfun) | |
778f6940 | 193 | (*lb->errfun) ("%s: skipping incomplete last line\n", |
194 | lb->filename); | |
f6fdbddf | 195 | return (NULL); |
196 | } | |
197 | /* Double the buffer if we need more space */ | |
198 | if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) { | |
199 | if (lb->errfun) | |
778f6940 | 200 | (*lb->errfun) ("linebuf (%s): realloc failed\n", |
201 | lb->filename); | |
f6fdbddf | 202 | return (NULL); |
203 | } | |
204 | } | |
205 | } | |
206 | ||
396c147e | 207 | static int |
f6fdbddf | 208 | fdlim_get(int hard) |
209 | { | |
f318b98b | 210 | #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) |
f6fdbddf | 211 | struct rlimit rlfd; |
778f6940 | 212 | |
f6fdbddf | 213 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) |
214 | return (-1); | |
215 | if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY) | |
216 | return 10000; | |
217 | else | |
218 | return hard ? rlfd.rlim_max : rlfd.rlim_cur; | |
b03bd394 | 219 | #elif defined (HAVE_SYSCONF) |
220 | return sysconf (_SC_OPEN_MAX); | |
221 | #else | |
222 | return 10000; | |
223 | #endif | |
f6fdbddf | 224 | } |
225 | ||
396c147e | 226 | static int |
f6fdbddf | 227 | fdlim_set(int lim) |
228 | { | |
f318b98b | 229 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) |
f6fdbddf | 230 | struct rlimit rlfd; |
b03bd394 | 231 | #endif |
f6fdbddf | 232 | if (lim <= 0) |
233 | return (-1); | |
f318b98b | 234 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) |
f6fdbddf | 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); | |
b03bd394 | 240 | #elif defined (HAVE_SETDTABLESIZE) |
b705bea0 | 241 | setdtablesize(lim); |
b03bd394 | 242 | #endif |
f6fdbddf | 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 | */ | |
396c147e | 251 | static char * |
f6fdbddf | 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 | */ | |
396c147e | 273 | static char * |
f6fdbddf | 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 | ||
5061072f | 284 | static Key * |
285 | keygrab_ssh1(con *c) | |
f6fdbddf | 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 | } | |
5061072f | 294 | buffer_append(&msg, c->c_data, c->c_plen); |
295 | buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */ | |
f6fdbddf | 296 | if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) { |
5061072f | 297 | error("%s: invalid packet type", c->c_name); |
f6fdbddf | 298 | buffer_clear(&msg); |
5061072f | 299 | return NULL; |
f6fdbddf | 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); | |
5061072f | 312 | |
f6fdbddf | 313 | buffer_clear(&msg); |
314 | ||
5061072f | 315 | return (rsa); |
316 | } | |
317 | ||
318 | static int | |
319 | hostjump(Key *hostkey) | |
320 | { | |
4278ff63 | 321 | kexjmp_key = hostkey; |
322 | longjmp(kexjmp, 1); | |
5061072f | 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); | |
351 | c->c_kex->verify_host_key = hostjump; | |
352 | ||
353 | if (!(j = setjmp(kexjmp))) { | |
354 | nonfatal_fatal = 1; | |
355 | dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex); | |
356 | fprintf(stderr, "Impossible! dispatch_run() returned!\n"); | |
357 | exit(1); | |
358 | } | |
359 | nonfatal_fatal = 0; | |
360 | xfree(c->c_kex); | |
361 | c->c_kex = NULL; | |
362 | packet_close(); | |
5061072f | 363 | |
4278ff63 | 364 | return j < 0? NULL : kexjmp_key; |
5061072f | 365 | } |
366 | ||
367 | static void | |
368 | keyprint(con *c, Key *key) | |
369 | { | |
370 | if (!key) | |
371 | return; | |
372 | ||
373 | fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name); | |
374 | key_write(key, stdout); | |
f6fdbddf | 375 | fputs("\n", stdout); |
376 | } | |
377 | ||
396c147e | 378 | static int |
f6fdbddf | 379 | tcpconnect(char *host) |
380 | { | |
381 | struct addrinfo hints, *ai, *aitop; | |
382 | char strport[NI_MAXSERV]; | |
383 | int gaierr, s = -1; | |
384 | ||
5061072f | 385 | snprintf(strport, sizeof strport, "%d", ssh_port); |
f6fdbddf | 386 | memset(&hints, 0, sizeof(hints)); |
5061072f | 387 | hints.ai_family = IPv4or6; |
f6fdbddf | 388 | hints.ai_socktype = SOCK_STREAM; |
389 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) | |
390 | fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr)); | |
391 | for (ai = aitop; ai; ai = ai->ai_next) { | |
392 | s = socket(ai->ai_family, SOCK_STREAM, 0); | |
393 | if (s < 0) { | |
394 | error("socket: %s", strerror(errno)); | |
395 | continue; | |
396 | } | |
eea39c02 | 397 | if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) |
f6fdbddf | 398 | fatal("F_SETFL: %s", strerror(errno)); |
399 | if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 && | |
400 | errno != EINPROGRESS) | |
401 | error("connect (`%s'): %s", host, strerror(errno)); | |
402 | else | |
403 | break; | |
404 | close(s); | |
405 | s = -1; | |
406 | } | |
407 | freeaddrinfo(aitop); | |
408 | return s; | |
409 | } | |
410 | ||
396c147e | 411 | static int |
5061072f | 412 | conalloc(char *iname, char *oname, int keytype) |
f6fdbddf | 413 | { |
414 | int s; | |
415 | char *namebase, *name, *namelist; | |
416 | ||
417 | namebase = namelist = xstrdup(iname); | |
418 | ||
419 | do { | |
420 | name = xstrsep(&namelist, ","); | |
421 | if (!name) { | |
b5c334cc | 422 | xfree(namebase); |
f6fdbddf | 423 | return (-1); |
424 | } | |
425 | } while ((s = tcpconnect(name)) < 0); | |
426 | ||
427 | if (s >= maxfd) | |
227e8e86 | 428 | fatal("conalloc: fdno %d too high", s); |
f6fdbddf | 429 | if (fdcon[s].c_status) |
227e8e86 | 430 | fatal("conalloc: attempt to reuse fdno %d", s); |
f6fdbddf | 431 | |
432 | fdcon[s].c_fd = s; | |
433 | fdcon[s].c_status = CS_CON; | |
434 | fdcon[s].c_namebase = namebase; | |
435 | fdcon[s].c_name = name; | |
436 | fdcon[s].c_namelist = namelist; | |
437 | fdcon[s].c_output_name = xstrdup(oname); | |
438 | fdcon[s].c_data = (char *) &fdcon[s].c_plen; | |
439 | fdcon[s].c_len = 4; | |
440 | fdcon[s].c_off = 0; | |
5061072f | 441 | fdcon[s].c_keytype = keytype; |
f6fdbddf | 442 | gettimeofday(&fdcon[s].c_tv, NULL); |
443 | fdcon[s].c_tv.tv_sec += timeout; | |
444 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); | |
ff7fee59 | 445 | FD_SET(s, read_wait); |
f6fdbddf | 446 | ncon++; |
447 | return (s); | |
448 | } | |
449 | ||
396c147e | 450 | static void |
f6fdbddf | 451 | confree(int s) |
452 | { | |
f6fdbddf | 453 | if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) |
227e8e86 | 454 | fatal("confree: attempt to free bad fdno %d", s); |
2671b47f | 455 | close(s); |
b5c334cc | 456 | xfree(fdcon[s].c_namebase); |
457 | xfree(fdcon[s].c_output_name); | |
f6fdbddf | 458 | if (fdcon[s].c_status == CS_KEYS) |
b5c334cc | 459 | xfree(fdcon[s].c_data); |
f6fdbddf | 460 | fdcon[s].c_status = CS_UNUSED; |
5061072f | 461 | fdcon[s].c_keytype = 0; |
f6fdbddf | 462 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); |
ff7fee59 | 463 | FD_CLR(s, read_wait); |
f6fdbddf | 464 | ncon--; |
465 | } | |
466 | ||
396c147e | 467 | static void |
f6fdbddf | 468 | contouch(int s) |
469 | { | |
470 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); | |
471 | gettimeofday(&fdcon[s].c_tv, NULL); | |
472 | fdcon[s].c_tv.tv_sec += timeout; | |
473 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); | |
474 | } | |
475 | ||
396c147e | 476 | static int |
f6fdbddf | 477 | conrecycle(int s) |
478 | { | |
479 | int ret; | |
480 | con *c = &fdcon[s]; | |
f6fdbddf | 481 | |
5061072f | 482 | ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype); |
f6fdbddf | 483 | confree(s); |
f6fdbddf | 484 | return (ret); |
485 | } | |
486 | ||
396c147e | 487 | static void |
f6fdbddf | 488 | congreet(int s) |
489 | { | |
5061072f | 490 | char buf[256], *cp; |
cdc95d6e | 491 | char remote_version[sizeof buf]; |
c16c7f20 | 492 | size_t bufsiz; |
cdc95d6e | 493 | int remote_major, remote_minor, n = 0; |
f6fdbddf | 494 | con *c = &fdcon[s]; |
495 | ||
c16c7f20 | 496 | bufsiz = sizeof(buf); |
497 | cp = buf; | |
578954b1 | 498 | while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n') { |
499 | if (*cp == '\r') | |
500 | *cp = '\n'; | |
c16c7f20 | 501 | cp++; |
578954b1 | 502 | } |
f6fdbddf | 503 | if (n < 0) { |
504 | if (errno != ECONNREFUSED) | |
505 | error("read (%s): %s", c->c_name, strerror(errno)); | |
506 | conrecycle(s); | |
507 | return; | |
508 | } | |
27452401 | 509 | if (n == 0) { |
510 | error("%s: Connection closed by remote host", c->c_name); | |
511 | conrecycle(s); | |
512 | return; | |
513 | } | |
c16c7f20 | 514 | if (*cp != '\n' && *cp != '\r') { |
f6fdbddf | 515 | error("%s: bad greeting", c->c_name); |
516 | confree(s); | |
517 | return; | |
518 | } | |
c16c7f20 | 519 | *cp = '\0'; |
cdc95d6e | 520 | if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", |
521 | &remote_major, &remote_minor, remote_version) == 3) | |
522 | compat_datafellows(remote_version); | |
523 | else | |
524 | datafellows = 0; | |
5061072f | 525 | if (c->c_keytype != KT_RSA1) { |
5061072f | 526 | if (!ssh2_capable(remote_major, remote_minor)) { |
527 | debug("%s doesn't support ssh2", c->c_name); | |
528 | confree(s); | |
529 | return; | |
530 | } | |
cdc95d6e | 531 | } else if (remote_major != 1) { |
532 | debug("%s doesn't support ssh1", c->c_name); | |
533 | confree(s); | |
534 | return; | |
5061072f | 535 | } |
578954b1 | 536 | fprintf(stderr, "# %s %s\n", c->c_name, chop(buf)); |
5061072f | 537 | n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n", |
538 | c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2, | |
539 | c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2); | |
2671b47f | 540 | if (atomicio(write, s, buf, n) != n) { |
f6fdbddf | 541 | error("write (%s): %s", c->c_name, strerror(errno)); |
542 | confree(s); | |
543 | return; | |
544 | } | |
5061072f | 545 | if (c->c_keytype != KT_RSA1) { |
546 | keyprint(c, keygrab_ssh2(c)); | |
547 | confree(s); | |
548 | return; | |
549 | } | |
f6fdbddf | 550 | c->c_status = CS_SIZE; |
551 | contouch(s); | |
552 | } | |
553 | ||
396c147e | 554 | static void |
f6fdbddf | 555 | conread(int s) |
556 | { | |
557 | int n; | |
558 | con *c = &fdcon[s]; | |
559 | ||
560 | if (c->c_status == CS_CON) { | |
561 | congreet(s); | |
562 | return; | |
563 | } | |
564 | n = read(s, c->c_data + c->c_off, c->c_len - c->c_off); | |
565 | if (n < 0) { | |
566 | error("read (%s): %s", c->c_name, strerror(errno)); | |
567 | confree(s); | |
568 | return; | |
569 | } | |
570 | c->c_off += n; | |
571 | ||
572 | if (c->c_off == c->c_len) | |
573 | switch (c->c_status) { | |
574 | case CS_SIZE: | |
575 | c->c_plen = htonl(c->c_plen); | |
576 | c->c_len = c->c_plen + 8 - (c->c_plen & 7); | |
577 | c->c_off = 0; | |
578 | c->c_data = xmalloc(c->c_len); | |
579 | c->c_status = CS_KEYS; | |
580 | break; | |
581 | case CS_KEYS: | |
5061072f | 582 | keyprint(c, keygrab_ssh1(c)); |
f6fdbddf | 583 | confree(s); |
584 | return; | |
585 | break; | |
586 | default: | |
227e8e86 | 587 | fatal("conread: invalid status %d", c->c_status); |
f6fdbddf | 588 | break; |
589 | } | |
590 | ||
591 | contouch(s); | |
592 | } | |
593 | ||
396c147e | 594 | static void |
f6fdbddf | 595 | conloop(void) |
596 | { | |
ff7fee59 | 597 | fd_set *r, *e; |
f6fdbddf | 598 | struct timeval seltime, now; |
599 | int i; | |
600 | con *c; | |
601 | ||
602 | gettimeofday(&now, NULL); | |
48ba8dc8 | 603 | c = TAILQ_FIRST(&tq); |
f6fdbddf | 604 | |
2671b47f | 605 | if (c && (c->c_tv.tv_sec > now.tv_sec || |
606 | (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) { | |
f6fdbddf | 607 | seltime = c->c_tv; |
608 | seltime.tv_sec -= now.tv_sec; | |
609 | seltime.tv_usec -= now.tv_usec; | |
5be2ec5e | 610 | if (seltime.tv_usec < 0) { |
f6fdbddf | 611 | seltime.tv_usec += 1000000; |
612 | seltime.tv_sec--; | |
613 | } | |
614 | } else | |
615 | seltime.tv_sec = seltime.tv_usec = 0; | |
616 | ||
ff7fee59 | 617 | r = xmalloc(read_wait_size); |
618 | memcpy(r, read_wait, read_wait_size); | |
619 | e = xmalloc(read_wait_size); | |
620 | memcpy(e, read_wait, read_wait_size); | |
621 | ||
622 | while (select(maxfd, r, NULL, e, &seltime) == -1 && | |
fd193ca4 | 623 | (errno == EAGAIN || errno == EINTR)) |
624 | ; | |
625 | ||
2671b47f | 626 | for (i = 0; i < maxfd; i++) { |
ff7fee59 | 627 | if (FD_ISSET(i, e)) { |
f6fdbddf | 628 | error("%s: exception!", fdcon[i].c_name); |
629 | confree(i); | |
ff7fee59 | 630 | } else if (FD_ISSET(i, r)) |
f6fdbddf | 631 | conread(i); |
2671b47f | 632 | } |
ff7fee59 | 633 | xfree(r); |
634 | xfree(e); | |
f6fdbddf | 635 | |
48ba8dc8 | 636 | c = TAILQ_FIRST(&tq); |
2671b47f | 637 | while (c && (c->c_tv.tv_sec < now.tv_sec || |
638 | (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) { | |
f6fdbddf | 639 | int s = c->c_fd; |
2671b47f | 640 | |
48ba8dc8 | 641 | c = TAILQ_NEXT(c, c_link); |
f6fdbddf | 642 | conrecycle(s); |
643 | } | |
644 | } | |
645 | ||
5061072f | 646 | static void |
647 | do_host(char *host) | |
f6fdbddf | 648 | { |
5061072f | 649 | char *name = strnnsep(&host, " \t\n"); |
650 | int j; | |
651 | ||
0408c978 | 652 | if (name == NULL) |
653 | return; | |
5061072f | 654 | for (j = KT_RSA1; j <= KT_RSA; j *= 2) { |
655 | if (get_keytypes & j) { | |
656 | while (ncon >= MAXCON) | |
657 | conloop(); | |
658 | conalloc(name, *host ? host : name, j); | |
f6fdbddf | 659 | } |
660 | } | |
661 | } | |
662 | ||
2bae80e9 | 663 | void |
664 | fatal(const char *fmt,...) | |
5061072f | 665 | { |
2bae80e9 | 666 | va_list args; |
667 | va_start(args, fmt); | |
668 | do_log(SYSLOG_LEVEL_FATAL, fmt, args); | |
669 | va_end(args); | |
5061072f | 670 | if (nonfatal_fatal) |
671 | longjmp(kexjmp, -1); | |
2bae80e9 | 672 | else |
673 | fatal_cleanup(); | |
5061072f | 674 | } |
675 | ||
396c147e | 676 | static void |
f6fdbddf | 677 | usage(void) |
678 | { | |
5061072f | 679 | fprintf(stderr, "Usage: %s [options] host ...\n", |
33e766d2 | 680 | __progname); |
681 | fprintf(stderr, "Options:\n"); | |
33e766d2 | 682 | fprintf(stderr, " -f file Read hosts or addresses from file.\n"); |
5061072f | 683 | fprintf(stderr, " -p port Connect to the specified port.\n"); |
684 | fprintf(stderr, " -t keytype Specify the host key type.\n"); | |
685 | fprintf(stderr, " -T timeout Set connection timeout.\n"); | |
2bae80e9 | 686 | fprintf(stderr, " -v Verbose; display verbose debugging messages.\n"); |
687 | fprintf(stderr, " -4 Use IPv4 only.\n"); | |
688 | fprintf(stderr, " -6 Use IPv6 only.\n"); | |
33e766d2 | 689 | exit(1); |
f6fdbddf | 690 | } |
691 | ||
692 | int | |
693 | main(int argc, char **argv) | |
694 | { | |
5061072f | 695 | int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; |
696 | int opt, fopt_count = 0; | |
697 | char *tname; | |
a204806b | 698 | |
5061072f | 699 | extern int optind; |
700 | extern char *optarg; | |
f6fdbddf | 701 | |
1fe6a48f | 702 | __progname = get_progname(argv[0]); |
8782141f | 703 | init_rng(); |
704 | seed_rng(); | |
f6fdbddf | 705 | TAILQ_INIT(&tq); |
706 | ||
5061072f | 707 | if (argc <= 1) |
f6fdbddf | 708 | usage(); |
709 | ||
5061072f | 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': | |
720 | timeout = atoi(optarg); | |
721 | if (timeout <= 0) | |
f6fdbddf | 722 | usage(); |
5061072f | 723 | break; |
724 | case 'v': | |
725 | if (!debug_flag) { | |
726 | debug_flag = 1; | |
727 | log_level = SYSLOG_LEVEL_DEBUG1; | |
728 | } | |
729 | else if (log_level < SYSLOG_LEVEL_DEBUG3) | |
730 | log_level++; | |
731 | else | |
732 | fatal("Too high debugging level."); | |
733 | break; | |
a204806b | 734 | case 'f': |
5061072f | 735 | if (strcmp(optarg, "-") == 0) |
736 | optarg = NULL; | |
737 | argv[fopt_count++] = optarg; | |
738 | break; | |
739 | case 't': | |
740 | get_keytypes = 0; | |
741 | tname = strtok(optarg, ","); | |
742 | while (tname) { | |
743 | int type = key_type_from_name(tname); | |
744 | switch (type) { | |
745 | case KEY_RSA1: | |
746 | get_keytypes |= KT_RSA1; | |
747 | break; | |
748 | case KEY_DSA: | |
749 | get_keytypes |= KT_DSA; | |
750 | break; | |
751 | case KEY_RSA: | |
752 | get_keytypes |= KT_RSA; | |
753 | break; | |
754 | case KEY_UNSPEC: | |
f48e63c8 | 755 | fatal("unknown key type %s", tname); |
5061072f | 756 | } |
757 | tname = strtok(NULL, ","); | |
758 | } | |
759 | break; | |
760 | case '4': | |
761 | IPv4or6 = AF_INET; | |
762 | break; | |
763 | case '6': | |
764 | IPv4or6 = AF_INET6; | |
765 | break; | |
766 | case '?': | |
767 | default: | |
f6fdbddf | 768 | usage(); |
5061072f | 769 | } |
f6fdbddf | 770 | } |
5061072f | 771 | if (optind == argc && !fopt_count) |
f6fdbddf | 772 | usage(); |
773 | ||
5061072f | 774 | log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1); |
5061072f | 775 | |
f6fdbddf | 776 | maxfd = fdlim_get(1); |
777 | if (maxfd < 0) | |
227e8e86 | 778 | fatal("%s: fdlim_get: bad value", __progname); |
f6fdbddf | 779 | if (maxfd > MAXMAXFD) |
780 | maxfd = MAXMAXFD; | |
2671b47f | 781 | if (MAXCON <= 0) |
227e8e86 | 782 | fatal("%s: not enough file descriptors", __progname); |
f6fdbddf | 783 | if (maxfd > fdlim_get(0)) |
784 | fdlim_set(maxfd); | |
785 | fdcon = xmalloc(maxfd * sizeof(con)); | |
5be2ec5e | 786 | memset(fdcon, 0, maxfd * sizeof(con)); |
f6fdbddf | 787 | |
ff7fee59 | 788 | read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask); |
789 | read_wait = xmalloc(read_wait_size); | |
790 | memset(read_wait, 0, read_wait_size); | |
791 | ||
5061072f | 792 | if (fopt_count) { |
793 | Linebuf *lb; | |
794 | char *line; | |
795 | int j; | |
796 | ||
797 | for (j = 0; j < fopt_count; j++) { | |
798 | lb = Linebuf_alloc(argv[j], error); | |
b7f79e7a | 799 | if (!lb) |
800 | continue; | |
5061072f | 801 | while ((line = Linebuf_getline(lb)) != NULL) |
802 | do_host(line); | |
803 | Linebuf_free(lb); | |
f6fdbddf | 804 | } |
5061072f | 805 | } |
806 | ||
807 | while (optind < argc) | |
808 | do_host(argv[optind++]); | |
809 | ||
f6fdbddf | 810 | while (ncon > 0) |
811 | conloop(); | |
812 | ||
813 | return (0); | |
814 | } |