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