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