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