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