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