]>
Commit | Line | Data |
---|---|---|
f6fdbddf | 1 | /* |
2 | * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>. | |
3 | * | |
4 | * Modification and redistribution in source and binary forms is | |
5 | * permitted provided that due credit is given to the author and the | |
6 | * OpenBSD project (for instance by leaving this copyright notice | |
7 | * intact). | |
8 | */ | |
9 | ||
10 | #include "includes.h" | |
1e3b8b07 | 11 | RCSID("$OpenBSD: ssh-keyscan.c,v 1.6 2000/12/19 23:17:58 markus Exp $"); |
f6fdbddf | 12 | |
bf5f69f7 | 13 | #ifdef HAVE_SYS_QUEUE_H |
f6fdbddf | 14 | #include <sys/queue.h> |
bf5f69f7 | 15 | #else |
16 | #include "bsd-queue.h" | |
17 | #endif | |
f6fdbddf | 18 | #include <errno.h> |
19 | ||
20 | #include <openssl/bn.h> | |
21 | #include <openssl/rsa.h> | |
22 | #include <openssl/dsa.h> | |
23 | ||
24 | #include "xmalloc.h" | |
25 | #include "ssh.h" | |
26 | #include "key.h" | |
27 | #include "buffer.h" | |
28 | #include "bufaux.h" | |
29 | ||
30 | static int argno = 1; /* Number of argument currently being parsed */ | |
31 | ||
32 | int family = AF_UNSPEC; /* IPv4, IPv6 or both */ | |
33 | ||
34 | #define PORT 22 | |
35 | #define MAXMAXFD 256 | |
36 | ||
37 | /* The number of seconds after which to give up on a TCP connection */ | |
38 | int timeout = 5; | |
39 | ||
40 | int maxfd; | |
41 | #define maxcon (maxfd - 10) | |
42 | ||
1fe6a48f | 43 | #ifdef HAVE___PROGNAME |
44 | extern char *__progname; | |
45 | #else | |
46 | char *__progname; | |
47 | #endif | |
f6fdbddf | 48 | fd_set read_wait; |
49 | int ncon; | |
50 | ||
51 | /* | |
52 | * Keep a connection structure for each file descriptor. The state | |
53 | * associated with file descriptor n is held in fdcon[n]. | |
54 | */ | |
55 | typedef struct Connection { | |
1e3b8b07 | 56 | u_char c_status; /* State of connection on this file desc. */ |
f6fdbddf | 57 | #define CS_UNUSED 0 /* File descriptor unused */ |
58 | #define CS_CON 1 /* Waiting to connect/read greeting */ | |
59 | #define CS_SIZE 2 /* Waiting to read initial packet size */ | |
60 | #define CS_KEYS 3 /* Waiting to read public key packet */ | |
61 | int c_fd; /* Quick lookup: c->c_fd == c - fdcon */ | |
62 | int c_plen; /* Packet length field for ssh packet */ | |
63 | int c_len; /* Total bytes which must be read. */ | |
64 | int c_off; /* Length of data read so far. */ | |
65 | char *c_namebase; /* Address to free for c_name and c_namelist */ | |
66 | char *c_name; /* Hostname of connection for errors */ | |
67 | char *c_namelist; /* Pointer to other possible addresses */ | |
68 | char *c_output_name; /* Hostname of connection for output */ | |
69 | char *c_data; /* Data read from this fd */ | |
70 | struct timeval c_tv; /* Time at which connection gets aborted */ | |
71 | TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */ | |
72 | } con; | |
73 | ||
74 | TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ | |
75 | con *fdcon; | |
76 | ||
77 | /* | |
78 | * This is just a wrapper around fgets() to make it usable. | |
79 | */ | |
80 | ||
81 | /* Stress-test. Increase this later. */ | |
82 | #define LINEBUF_SIZE 16 | |
83 | ||
84 | typedef struct { | |
85 | char *buf; | |
1e3b8b07 | 86 | u_int size; |
f6fdbddf | 87 | int lineno; |
88 | const char *filename; | |
89 | FILE *stream; | |
90 | void (*errfun) (const char *,...); | |
91 | } Linebuf; | |
92 | ||
93 | static inline Linebuf * | |
94 | Linebuf_alloc(const char *filename, void (*errfun) (const char *,...)) | |
95 | { | |
96 | Linebuf *lb; | |
97 | ||
98 | if (!(lb = malloc(sizeof(*lb)))) { | |
99 | if (errfun) | |
100 | (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); | |
101 | return (NULL); | |
102 | } | |
103 | if (filename) { | |
104 | lb->filename = filename; | |
105 | if (!(lb->stream = fopen(filename, "r"))) { | |
106 | free(lb); | |
107 | if (errfun) | |
108 | (*errfun) ("%s: %s\n", filename, strerror(errno)); | |
109 | return (NULL); | |
110 | } | |
111 | } else { | |
112 | lb->filename = "(stdin)"; | |
113 | lb->stream = stdin; | |
114 | } | |
115 | ||
116 | if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) { | |
117 | if (errfun) | |
118 | (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); | |
119 | free(lb); | |
120 | return (NULL); | |
121 | } | |
122 | lb->errfun = errfun; | |
123 | lb->lineno = 0; | |
124 | return (lb); | |
125 | } | |
126 | ||
127 | static inline void | |
128 | Linebuf_free(Linebuf * lb) | |
129 | { | |
130 | fclose(lb->stream); | |
131 | free(lb->buf); | |
132 | free(lb); | |
133 | } | |
134 | ||
135 | static inline void | |
136 | Linebuf_restart(Linebuf * lb) | |
137 | { | |
138 | clearerr(lb->stream); | |
139 | rewind(lb->stream); | |
140 | lb->lineno = 0; | |
141 | } | |
142 | ||
143 | static inline int | |
144 | Linebuf_lineno(Linebuf * lb) | |
145 | { | |
146 | return (lb->lineno); | |
147 | } | |
148 | ||
149 | static inline char * | |
150 | getline(Linebuf * lb) | |
151 | { | |
152 | int n = 0; | |
153 | ||
154 | lb->lineno++; | |
155 | for (;;) { | |
156 | /* Read a line */ | |
157 | if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) { | |
158 | if (ferror(lb->stream) && lb->errfun) | |
159 | (*lb->errfun) ("%s: %s\n", lb->filename, strerror(errno)); | |
160 | return (NULL); | |
161 | } | |
162 | n = strlen(lb->buf); | |
163 | ||
164 | /* Return it or an error if it fits */ | |
165 | if (n > 0 && lb->buf[n - 1] == '\n') { | |
166 | lb->buf[n - 1] = '\0'; | |
167 | return (lb->buf); | |
168 | } | |
169 | if (n != lb->size - 1) { | |
170 | if (lb->errfun) | |
171 | (*lb->errfun) ("%s: skipping incomplete last line\n", lb->filename); | |
172 | return (NULL); | |
173 | } | |
174 | /* Double the buffer if we need more space */ | |
175 | if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) { | |
176 | if (lb->errfun) | |
177 | (*lb->errfun) ("linebuf (%s): realloc failed\n", lb->filename); | |
178 | return (NULL); | |
179 | } | |
180 | } | |
181 | } | |
182 | ||
183 | static int | |
184 | fdlim_get(int hard) | |
185 | { | |
186 | struct rlimit rlfd; | |
187 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) | |
188 | return (-1); | |
189 | if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY) | |
190 | return 10000; | |
191 | else | |
192 | return hard ? rlfd.rlim_max : rlfd.rlim_cur; | |
193 | } | |
194 | ||
195 | static int | |
196 | fdlim_set(int lim) | |
197 | { | |
198 | struct rlimit rlfd; | |
199 | if (lim <= 0) | |
200 | return (-1); | |
201 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) | |
202 | return (-1); | |
203 | rlfd.rlim_cur = lim; | |
204 | if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0) | |
205 | return (-1); | |
206 | return (0); | |
207 | } | |
208 | ||
209 | /* | |
210 | * This is an strsep function that returns a null field for adjacent | |
211 | * separators. This is the same as the 4.4BSD strsep, but different from the | |
212 | * one in the GNU libc. | |
213 | */ | |
214 | inline char * | |
215 | xstrsep(char **str, const char *delim) | |
216 | { | |
217 | char *s, *e; | |
218 | ||
219 | if (!**str) | |
220 | return (NULL); | |
221 | ||
222 | s = *str; | |
223 | e = s + strcspn(s, delim); | |
224 | ||
225 | if (*e != '\0') | |
226 | *e++ = '\0'; | |
227 | *str = e; | |
228 | ||
229 | return (s); | |
230 | } | |
231 | ||
232 | /* | |
233 | * Get the next non-null token (like GNU strsep). Strsep() will return a | |
234 | * null token for two adjacent separators, so we may have to loop. | |
235 | */ | |
236 | char * | |
237 | strnnsep(char **stringp, char *delim) | |
238 | { | |
239 | char *tok; | |
240 | ||
241 | do { | |
242 | tok = xstrsep(stringp, delim); | |
243 | } while (tok && *tok == '\0'); | |
244 | return (tok); | |
245 | } | |
246 | ||
247 | void | |
248 | keyprint(char *host, char *output_name, char *kd, int len) | |
249 | { | |
250 | static Key *rsa; | |
251 | static Buffer msg; | |
252 | ||
253 | if (rsa == NULL) { | |
254 | buffer_init(&msg); | |
255 | rsa = key_new(KEY_RSA1); | |
256 | } | |
257 | buffer_append(&msg, kd, len); | |
258 | buffer_consume(&msg, 8 - (len & 7)); /* padding */ | |
259 | if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) { | |
260 | error("%s: invalid packet type", host); | |
261 | buffer_clear(&msg); | |
262 | return; | |
263 | } | |
264 | buffer_consume(&msg, 8); /* cookie */ | |
265 | ||
266 | /* server key */ | |
267 | (void) buffer_get_int(&msg); | |
268 | buffer_get_bignum(&msg, rsa->rsa->e); | |
269 | buffer_get_bignum(&msg, rsa->rsa->n); | |
270 | ||
271 | /* host key */ | |
272 | (void) buffer_get_int(&msg); | |
273 | buffer_get_bignum(&msg, rsa->rsa->e); | |
274 | buffer_get_bignum(&msg, rsa->rsa->n); | |
275 | buffer_clear(&msg); | |
276 | ||
277 | fprintf(stdout, "%s ", output_name ? output_name : host); | |
278 | key_write(rsa, stdout); | |
279 | fputs("\n", stdout); | |
280 | } | |
281 | ||
282 | int | |
283 | tcpconnect(char *host) | |
284 | { | |
285 | struct addrinfo hints, *ai, *aitop; | |
286 | char strport[NI_MAXSERV]; | |
287 | int gaierr, s = -1; | |
288 | ||
289 | snprintf(strport, sizeof strport, "%d", PORT); | |
290 | memset(&hints, 0, sizeof(hints)); | |
291 | hints.ai_family = family; | |
292 | hints.ai_socktype = SOCK_STREAM; | |
293 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) | |
294 | fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr)); | |
295 | for (ai = aitop; ai; ai = ai->ai_next) { | |
296 | s = socket(ai->ai_family, SOCK_STREAM, 0); | |
297 | if (s < 0) { | |
298 | error("socket: %s", strerror(errno)); | |
299 | continue; | |
300 | } | |
301 | if (fcntl(s, F_SETFL, O_NDELAY) < 0) | |
302 | fatal("F_SETFL: %s", strerror(errno)); | |
303 | if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 && | |
304 | errno != EINPROGRESS) | |
305 | error("connect (`%s'): %s", host, strerror(errno)); | |
306 | else | |
307 | break; | |
308 | close(s); | |
309 | s = -1; | |
310 | } | |
311 | freeaddrinfo(aitop); | |
312 | return s; | |
313 | } | |
314 | ||
315 | int | |
316 | conalloc(char *iname, char *oname) | |
317 | { | |
318 | int s; | |
319 | char *namebase, *name, *namelist; | |
320 | ||
321 | namebase = namelist = xstrdup(iname); | |
322 | ||
323 | do { | |
324 | name = xstrsep(&namelist, ","); | |
325 | if (!name) { | |
326 | free(namebase); | |
327 | return (-1); | |
328 | } | |
329 | } while ((s = tcpconnect(name)) < 0); | |
330 | ||
331 | if (s >= maxfd) | |
227e8e86 | 332 | fatal("conalloc: fdno %d too high", s); |
f6fdbddf | 333 | if (fdcon[s].c_status) |
227e8e86 | 334 | fatal("conalloc: attempt to reuse fdno %d", s); |
f6fdbddf | 335 | |
336 | fdcon[s].c_fd = s; | |
337 | fdcon[s].c_status = CS_CON; | |
338 | fdcon[s].c_namebase = namebase; | |
339 | fdcon[s].c_name = name; | |
340 | fdcon[s].c_namelist = namelist; | |
341 | fdcon[s].c_output_name = xstrdup(oname); | |
342 | fdcon[s].c_data = (char *) &fdcon[s].c_plen; | |
343 | fdcon[s].c_len = 4; | |
344 | fdcon[s].c_off = 0; | |
345 | gettimeofday(&fdcon[s].c_tv, NULL); | |
346 | fdcon[s].c_tv.tv_sec += timeout; | |
347 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); | |
348 | FD_SET(s, &read_wait); | |
349 | ncon++; | |
350 | return (s); | |
351 | } | |
352 | ||
353 | void | |
354 | confree(int s) | |
355 | { | |
356 | close(s); | |
357 | if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) | |
227e8e86 | 358 | fatal("confree: attempt to free bad fdno %d", s); |
f6fdbddf | 359 | free(fdcon[s].c_namebase); |
360 | free(fdcon[s].c_output_name); | |
361 | if (fdcon[s].c_status == CS_KEYS) | |
362 | free(fdcon[s].c_data); | |
363 | fdcon[s].c_status = CS_UNUSED; | |
364 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); | |
365 | FD_CLR(s, &read_wait); | |
366 | ncon--; | |
367 | } | |
368 | ||
369 | void | |
370 | contouch(int s) | |
371 | { | |
372 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); | |
373 | gettimeofday(&fdcon[s].c_tv, NULL); | |
374 | fdcon[s].c_tv.tv_sec += timeout; | |
375 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); | |
376 | } | |
377 | ||
378 | int | |
379 | conrecycle(int s) | |
380 | { | |
381 | int ret; | |
382 | con *c = &fdcon[s]; | |
383 | char *iname, *oname; | |
384 | ||
385 | iname = xstrdup(c->c_namelist); | |
386 | oname = c->c_output_name; | |
387 | c->c_output_name = NULL;/* prevent it from being freed */ | |
388 | confree(s); | |
389 | ret = conalloc(iname, oname); | |
390 | free(iname); | |
391 | return (ret); | |
392 | } | |
393 | ||
394 | void | |
395 | congreet(int s) | |
396 | { | |
397 | char buf[80]; | |
398 | int n; | |
399 | con *c = &fdcon[s]; | |
400 | ||
401 | n = read(s, buf, sizeof(buf)); | |
402 | if (n < 0) { | |
403 | if (errno != ECONNREFUSED) | |
404 | error("read (%s): %s", c->c_name, strerror(errno)); | |
405 | conrecycle(s); | |
406 | return; | |
407 | } | |
408 | if (buf[n - 1] != '\n') { | |
409 | error("%s: bad greeting", c->c_name); | |
410 | confree(s); | |
411 | return; | |
412 | } | |
413 | buf[n - 1] = '\0'; | |
414 | fprintf(stderr, "# %s %s\n", c->c_name, buf); | |
415 | n = snprintf(buf, sizeof buf, "SSH-1.5-OpenSSH-keyscan\r\n"); | |
416 | if (write(s, buf, n) != n) { | |
417 | error("write (%s): %s", c->c_name, strerror(errno)); | |
418 | confree(s); | |
419 | return; | |
420 | } | |
421 | c->c_status = CS_SIZE; | |
422 | contouch(s); | |
423 | } | |
424 | ||
425 | void | |
426 | conread(int s) | |
427 | { | |
428 | int n; | |
429 | con *c = &fdcon[s]; | |
430 | ||
431 | if (c->c_status == CS_CON) { | |
432 | congreet(s); | |
433 | return; | |
434 | } | |
435 | n = read(s, c->c_data + c->c_off, c->c_len - c->c_off); | |
436 | if (n < 0) { | |
437 | error("read (%s): %s", c->c_name, strerror(errno)); | |
438 | confree(s); | |
439 | return; | |
440 | } | |
441 | c->c_off += n; | |
442 | ||
443 | if (c->c_off == c->c_len) | |
444 | switch (c->c_status) { | |
445 | case CS_SIZE: | |
446 | c->c_plen = htonl(c->c_plen); | |
447 | c->c_len = c->c_plen + 8 - (c->c_plen & 7); | |
448 | c->c_off = 0; | |
449 | c->c_data = xmalloc(c->c_len); | |
450 | c->c_status = CS_KEYS; | |
451 | break; | |
452 | case CS_KEYS: | |
453 | keyprint(c->c_name, c->c_output_name, c->c_data, c->c_plen); | |
454 | confree(s); | |
455 | return; | |
456 | break; | |
457 | default: | |
227e8e86 | 458 | fatal("conread: invalid status %d", c->c_status); |
f6fdbddf | 459 | break; |
460 | } | |
461 | ||
462 | contouch(s); | |
463 | } | |
464 | ||
465 | void | |
466 | conloop(void) | |
467 | { | |
468 | fd_set r, e; | |
469 | struct timeval seltime, now; | |
470 | int i; | |
471 | con *c; | |
472 | ||
473 | gettimeofday(&now, NULL); | |
474 | c = tq.tqh_first; | |
475 | ||
476 | if (c && | |
477 | (c->c_tv.tv_sec > now.tv_sec || | |
478 | (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) { | |
479 | seltime = c->c_tv; | |
480 | seltime.tv_sec -= now.tv_sec; | |
481 | seltime.tv_usec -= now.tv_usec; | |
482 | if ((int) seltime.tv_usec < 0) { | |
483 | seltime.tv_usec += 1000000; | |
484 | seltime.tv_sec--; | |
485 | } | |
486 | } else | |
487 | seltime.tv_sec = seltime.tv_usec = 0; | |
488 | ||
489 | r = e = read_wait; | |
490 | select(maxfd, &r, NULL, &e, &seltime); | |
491 | for (i = 0; i < maxfd; i++) | |
492 | if (FD_ISSET(i, &e)) { | |
493 | error("%s: exception!", fdcon[i].c_name); | |
494 | confree(i); | |
495 | } else if (FD_ISSET(i, &r)) | |
496 | conread(i); | |
497 | ||
498 | c = tq.tqh_first; | |
499 | while (c && | |
500 | (c->c_tv.tv_sec < now.tv_sec || | |
501 | (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) { | |
502 | int s = c->c_fd; | |
503 | c = c->c_link.tqe_next; | |
504 | conrecycle(s); | |
505 | } | |
506 | } | |
507 | ||
508 | char * | |
509 | nexthost(int argc, char **argv) | |
510 | { | |
511 | static Linebuf *lb; | |
512 | ||
513 | for (;;) { | |
514 | if (!lb) { | |
515 | if (argno >= argc) | |
516 | return (NULL); | |
517 | if (argv[argno][0] != '-') | |
518 | return (argv[argno++]); | |
519 | if (!strcmp(argv[argno], "--")) { | |
520 | if (++argno >= argc) | |
521 | return (NULL); | |
522 | return (argv[argno++]); | |
523 | } else if (!strncmp(argv[argno], "-f", 2)) { | |
524 | char *fname; | |
525 | if (argv[argno][2]) | |
526 | fname = &argv[argno++][2]; | |
527 | else if (++argno >= argc) { | |
528 | error("missing filename for `-f'"); | |
529 | return (NULL); | |
530 | } else | |
531 | fname = argv[argno++]; | |
532 | if (!strcmp(fname, "-")) | |
533 | fname = NULL; | |
f20255cb | 534 | lb = Linebuf_alloc(fname, error); |
f6fdbddf | 535 | } else |
536 | error("ignoring invalid/misplaced option `%s'", argv[argno++]); | |
537 | } else { | |
538 | char *line; | |
539 | line = getline(lb); | |
540 | if (line) | |
541 | return (line); | |
542 | Linebuf_free(lb); | |
543 | lb = NULL; | |
544 | } | |
545 | } | |
546 | } | |
547 | ||
548 | static void | |
549 | usage(void) | |
550 | { | |
227e8e86 | 551 | fatal("usage: %s [-t timeout] { [--] host | -f file } ...", __progname); |
f6fdbddf | 552 | return; |
553 | } | |
554 | ||
555 | int | |
556 | main(int argc, char **argv) | |
557 | { | |
558 | char *host = NULL; | |
559 | ||
1fe6a48f | 560 | __progname = get_progname(argv[0]); |
f6fdbddf | 561 | TAILQ_INIT(&tq); |
562 | ||
f6fdbddf | 563 | if (argc <= argno) |
564 | usage(); | |
565 | ||
566 | if (argv[1][0] == '-' && argv[1][1] == 't') { | |
567 | argno++; | |
568 | if (argv[1][2]) | |
569 | timeout = atoi(&argv[1][2]); | |
570 | else { | |
571 | if (argno >= argc) | |
572 | usage(); | |
573 | timeout = atoi(argv[argno++]); | |
574 | } | |
575 | if (timeout <= 0) | |
576 | usage(); | |
577 | } | |
578 | if (argc <= argno) | |
579 | usage(); | |
580 | ||
581 | maxfd = fdlim_get(1); | |
582 | if (maxfd < 0) | |
227e8e86 | 583 | fatal("%s: fdlim_get: bad value", __progname); |
f6fdbddf | 584 | if (maxfd > MAXMAXFD) |
585 | maxfd = MAXMAXFD; | |
586 | if (maxcon <= 0) | |
227e8e86 | 587 | fatal("%s: not enough file descriptors", __progname); |
f6fdbddf | 588 | if (maxfd > fdlim_get(0)) |
589 | fdlim_set(maxfd); | |
590 | fdcon = xmalloc(maxfd * sizeof(con)); | |
591 | ||
592 | do { | |
593 | while (ncon < maxcon) { | |
594 | char *name; | |
595 | ||
596 | host = nexthost(argc, argv); | |
597 | if (host == NULL) | |
598 | break; | |
599 | name = strnnsep(&host, " \t\n"); | |
600 | conalloc(name, *host ? host : name); | |
601 | } | |
602 | conloop(); | |
603 | } while (host); | |
604 | while (ncon > 0) | |
605 | conloop(); | |
606 | ||
607 | return (0); | |
608 | } |