]>
Commit | Line | Data |
---|---|---|
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" | |
11 | RCSID("$OpenBSD: ssh-keyscan.c,v 1.7 2001/01/08 22:03:23 markus Exp $"); | |
12 | ||
13 | #if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H) | |
14 | #include <sys/queue.h> | |
15 | #else | |
16 | #include "bsd-queue.h" | |
17 | #endif | |
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 | ||
43 | #ifdef HAVE___PROGNAME | |
44 | extern char *__progname; | |
45 | #else | |
46 | char *__progname; | |
47 | #endif | |
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 { | |
56 | u_char c_status; /* State of connection on this file desc. */ | |
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; | |
86 | u_int size; | |
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 | #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) | |
187 | struct rlimit rlfd; | |
188 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) | |
189 | return (-1); | |
190 | if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY) | |
191 | return 10000; | |
192 | else | |
193 | return hard ? rlfd.rlim_max : rlfd.rlim_cur; | |
194 | #elif defined (HAVE_SYSCONF) | |
195 | return sysconf (_SC_OPEN_MAX); | |
196 | #else | |
197 | return 10000; | |
198 | #endif | |
199 | } | |
200 | ||
201 | static int | |
202 | fdlim_set(int lim) | |
203 | { | |
204 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) | |
205 | struct rlimit rlfd; | |
206 | #endif | |
207 | if (lim <= 0) | |
208 | return (-1); | |
209 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) | |
210 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) | |
211 | return (-1); | |
212 | rlfd.rlim_cur = lim; | |
213 | if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0) | |
214 | return (-1); | |
215 | #elif defined (HAVE_SETDTABLESIZE) | |
216 | setdtablesize (lim); | |
217 | #endif | |
218 | return (0); | |
219 | } | |
220 | ||
221 | /* | |
222 | * This is an strsep function that returns a null field for adjacent | |
223 | * separators. This is the same as the 4.4BSD strsep, but different from the | |
224 | * one in the GNU libc. | |
225 | */ | |
226 | inline char * | |
227 | xstrsep(char **str, const char *delim) | |
228 | { | |
229 | char *s, *e; | |
230 | ||
231 | if (!**str) | |
232 | return (NULL); | |
233 | ||
234 | s = *str; | |
235 | e = s + strcspn(s, delim); | |
236 | ||
237 | if (*e != '\0') | |
238 | *e++ = '\0'; | |
239 | *str = e; | |
240 | ||
241 | return (s); | |
242 | } | |
243 | ||
244 | /* | |
245 | * Get the next non-null token (like GNU strsep). Strsep() will return a | |
246 | * null token for two adjacent separators, so we may have to loop. | |
247 | */ | |
248 | char * | |
249 | strnnsep(char **stringp, char *delim) | |
250 | { | |
251 | char *tok; | |
252 | ||
253 | do { | |
254 | tok = xstrsep(stringp, delim); | |
255 | } while (tok && *tok == '\0'); | |
256 | return (tok); | |
257 | } | |
258 | ||
259 | void | |
260 | keyprint(char *host, char *output_name, char *kd, int len) | |
261 | { | |
262 | static Key *rsa; | |
263 | static Buffer msg; | |
264 | ||
265 | if (rsa == NULL) { | |
266 | buffer_init(&msg); | |
267 | rsa = key_new(KEY_RSA1); | |
268 | } | |
269 | buffer_append(&msg, kd, len); | |
270 | buffer_consume(&msg, 8 - (len & 7)); /* padding */ | |
271 | if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) { | |
272 | error("%s: invalid packet type", host); | |
273 | buffer_clear(&msg); | |
274 | return; | |
275 | } | |
276 | buffer_consume(&msg, 8); /* cookie */ | |
277 | ||
278 | /* server key */ | |
279 | (void) buffer_get_int(&msg); | |
280 | buffer_get_bignum(&msg, rsa->rsa->e); | |
281 | buffer_get_bignum(&msg, rsa->rsa->n); | |
282 | ||
283 | /* host key */ | |
284 | (void) buffer_get_int(&msg); | |
285 | buffer_get_bignum(&msg, rsa->rsa->e); | |
286 | buffer_get_bignum(&msg, rsa->rsa->n); | |
287 | buffer_clear(&msg); | |
288 | ||
289 | fprintf(stdout, "%s ", output_name ? output_name : host); | |
290 | key_write(rsa, stdout); | |
291 | fputs("\n", stdout); | |
292 | } | |
293 | ||
294 | int | |
295 | tcpconnect(char *host) | |
296 | { | |
297 | struct addrinfo hints, *ai, *aitop; | |
298 | char strport[NI_MAXSERV]; | |
299 | int gaierr, s = -1; | |
300 | ||
301 | snprintf(strport, sizeof strport, "%d", PORT); | |
302 | memset(&hints, 0, sizeof(hints)); | |
303 | hints.ai_family = family; | |
304 | hints.ai_socktype = SOCK_STREAM; | |
305 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) | |
306 | fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr)); | |
307 | for (ai = aitop; ai; ai = ai->ai_next) { | |
308 | s = socket(ai->ai_family, SOCK_STREAM, 0); | |
309 | if (s < 0) { | |
310 | error("socket: %s", strerror(errno)); | |
311 | continue; | |
312 | } | |
313 | if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) | |
314 | fatal("F_SETFL: %s", strerror(errno)); | |
315 | if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 && | |
316 | errno != EINPROGRESS) | |
317 | error("connect (`%s'): %s", host, strerror(errno)); | |
318 | else | |
319 | break; | |
320 | close(s); | |
321 | s = -1; | |
322 | } | |
323 | freeaddrinfo(aitop); | |
324 | return s; | |
325 | } | |
326 | ||
327 | int | |
328 | conalloc(char *iname, char *oname) | |
329 | { | |
330 | int s; | |
331 | char *namebase, *name, *namelist; | |
332 | ||
333 | namebase = namelist = xstrdup(iname); | |
334 | ||
335 | do { | |
336 | name = xstrsep(&namelist, ","); | |
337 | if (!name) { | |
338 | free(namebase); | |
339 | return (-1); | |
340 | } | |
341 | } while ((s = tcpconnect(name)) < 0); | |
342 | ||
343 | if (s >= maxfd) | |
344 | fatal("conalloc: fdno %d too high", s); | |
345 | if (fdcon[s].c_status) | |
346 | fatal("conalloc: attempt to reuse fdno %d", s); | |
347 | ||
348 | fdcon[s].c_fd = s; | |
349 | fdcon[s].c_status = CS_CON; | |
350 | fdcon[s].c_namebase = namebase; | |
351 | fdcon[s].c_name = name; | |
352 | fdcon[s].c_namelist = namelist; | |
353 | fdcon[s].c_output_name = xstrdup(oname); | |
354 | fdcon[s].c_data = (char *) &fdcon[s].c_plen; | |
355 | fdcon[s].c_len = 4; | |
356 | fdcon[s].c_off = 0; | |
357 | gettimeofday(&fdcon[s].c_tv, NULL); | |
358 | fdcon[s].c_tv.tv_sec += timeout; | |
359 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); | |
360 | FD_SET(s, &read_wait); | |
361 | ncon++; | |
362 | return (s); | |
363 | } | |
364 | ||
365 | void | |
366 | confree(int s) | |
367 | { | |
368 | close(s); | |
369 | if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) | |
370 | fatal("confree: attempt to free bad fdno %d", s); | |
371 | free(fdcon[s].c_namebase); | |
372 | free(fdcon[s].c_output_name); | |
373 | if (fdcon[s].c_status == CS_KEYS) | |
374 | free(fdcon[s].c_data); | |
375 | fdcon[s].c_status = CS_UNUSED; | |
376 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); | |
377 | FD_CLR(s, &read_wait); | |
378 | ncon--; | |
379 | } | |
380 | ||
381 | void | |
382 | contouch(int s) | |
383 | { | |
384 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); | |
385 | gettimeofday(&fdcon[s].c_tv, NULL); | |
386 | fdcon[s].c_tv.tv_sec += timeout; | |
387 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); | |
388 | } | |
389 | ||
390 | int | |
391 | conrecycle(int s) | |
392 | { | |
393 | int ret; | |
394 | con *c = &fdcon[s]; | |
395 | char *iname, *oname; | |
396 | ||
397 | iname = xstrdup(c->c_namelist); | |
398 | oname = c->c_output_name; | |
399 | c->c_output_name = NULL;/* prevent it from being freed */ | |
400 | confree(s); | |
401 | ret = conalloc(iname, oname); | |
402 | free(iname); | |
403 | return (ret); | |
404 | } | |
405 | ||
406 | void | |
407 | congreet(int s) | |
408 | { | |
409 | char buf[80]; | |
410 | int n; | |
411 | con *c = &fdcon[s]; | |
412 | ||
413 | n = read(s, buf, sizeof(buf)); | |
414 | if (n < 0) { | |
415 | if (errno != ECONNREFUSED) | |
416 | error("read (%s): %s", c->c_name, strerror(errno)); | |
417 | conrecycle(s); | |
418 | return; | |
419 | } | |
420 | if (buf[n - 1] != '\n') { | |
421 | error("%s: bad greeting", c->c_name); | |
422 | confree(s); | |
423 | return; | |
424 | } | |
425 | buf[n - 1] = '\0'; | |
426 | fprintf(stderr, "# %s %s\n", c->c_name, buf); | |
427 | n = snprintf(buf, sizeof buf, "SSH-1.5-OpenSSH-keyscan\r\n"); | |
428 | if (write(s, buf, n) != n) { | |
429 | error("write (%s): %s", c->c_name, strerror(errno)); | |
430 | confree(s); | |
431 | return; | |
432 | } | |
433 | c->c_status = CS_SIZE; | |
434 | contouch(s); | |
435 | } | |
436 | ||
437 | void | |
438 | conread(int s) | |
439 | { | |
440 | int n; | |
441 | con *c = &fdcon[s]; | |
442 | ||
443 | if (c->c_status == CS_CON) { | |
444 | congreet(s); | |
445 | return; | |
446 | } | |
447 | n = read(s, c->c_data + c->c_off, c->c_len - c->c_off); | |
448 | if (n < 0) { | |
449 | error("read (%s): %s", c->c_name, strerror(errno)); | |
450 | confree(s); | |
451 | return; | |
452 | } | |
453 | c->c_off += n; | |
454 | ||
455 | if (c->c_off == c->c_len) | |
456 | switch (c->c_status) { | |
457 | case CS_SIZE: | |
458 | c->c_plen = htonl(c->c_plen); | |
459 | c->c_len = c->c_plen + 8 - (c->c_plen & 7); | |
460 | c->c_off = 0; | |
461 | c->c_data = xmalloc(c->c_len); | |
462 | c->c_status = CS_KEYS; | |
463 | break; | |
464 | case CS_KEYS: | |
465 | keyprint(c->c_name, c->c_output_name, c->c_data, c->c_plen); | |
466 | confree(s); | |
467 | return; | |
468 | break; | |
469 | default: | |
470 | fatal("conread: invalid status %d", c->c_status); | |
471 | break; | |
472 | } | |
473 | ||
474 | contouch(s); | |
475 | } | |
476 | ||
477 | void | |
478 | conloop(void) | |
479 | { | |
480 | fd_set r, e; | |
481 | struct timeval seltime, now; | |
482 | int i; | |
483 | con *c; | |
484 | ||
485 | gettimeofday(&now, NULL); | |
486 | c = tq.tqh_first; | |
487 | ||
488 | if (c && | |
489 | (c->c_tv.tv_sec > now.tv_sec || | |
490 | (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) { | |
491 | seltime = c->c_tv; | |
492 | seltime.tv_sec -= now.tv_sec; | |
493 | seltime.tv_usec -= now.tv_usec; | |
494 | if ((int) seltime.tv_usec < 0) { | |
495 | seltime.tv_usec += 1000000; | |
496 | seltime.tv_sec--; | |
497 | } | |
498 | } else | |
499 | seltime.tv_sec = seltime.tv_usec = 0; | |
500 | ||
501 | r = e = read_wait; | |
502 | select(maxfd, &r, NULL, &e, &seltime); | |
503 | for (i = 0; i < maxfd; i++) | |
504 | if (FD_ISSET(i, &e)) { | |
505 | error("%s: exception!", fdcon[i].c_name); | |
506 | confree(i); | |
507 | } else if (FD_ISSET(i, &r)) | |
508 | conread(i); | |
509 | ||
510 | c = tq.tqh_first; | |
511 | while (c && | |
512 | (c->c_tv.tv_sec < now.tv_sec || | |
513 | (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) { | |
514 | int s = c->c_fd; | |
515 | c = c->c_link.tqe_next; | |
516 | conrecycle(s); | |
517 | } | |
518 | } | |
519 | ||
520 | char * | |
521 | nexthost(int argc, char **argv) | |
522 | { | |
523 | static Linebuf *lb; | |
524 | ||
525 | for (;;) { | |
526 | if (!lb) { | |
527 | if (argno >= argc) | |
528 | return (NULL); | |
529 | if (argv[argno][0] != '-') | |
530 | return (argv[argno++]); | |
531 | if (!strcmp(argv[argno], "--")) { | |
532 | if (++argno >= argc) | |
533 | return (NULL); | |
534 | return (argv[argno++]); | |
535 | } else if (!strncmp(argv[argno], "-f", 2)) { | |
536 | char *fname; | |
537 | if (argv[argno][2]) | |
538 | fname = &argv[argno++][2]; | |
539 | else if (++argno >= argc) { | |
540 | error("missing filename for `-f'"); | |
541 | return (NULL); | |
542 | } else | |
543 | fname = argv[argno++]; | |
544 | if (!strcmp(fname, "-")) | |
545 | fname = NULL; | |
546 | lb = Linebuf_alloc(fname, error); | |
547 | } else | |
548 | error("ignoring invalid/misplaced option `%s'", argv[argno++]); | |
549 | } else { | |
550 | char *line; | |
551 | line = getline(lb); | |
552 | if (line) | |
553 | return (line); | |
554 | Linebuf_free(lb); | |
555 | lb = NULL; | |
556 | } | |
557 | } | |
558 | } | |
559 | ||
560 | static void | |
561 | usage(void) | |
562 | { | |
563 | fatal("usage: %s [-t timeout] { [--] host | -f file } ...", __progname); | |
564 | return; | |
565 | } | |
566 | ||
567 | int | |
568 | main(int argc, char **argv) | |
569 | { | |
570 | char *host = NULL; | |
571 | ||
572 | __progname = get_progname(argv[0]); | |
573 | TAILQ_INIT(&tq); | |
574 | ||
575 | if (argc <= argno) | |
576 | usage(); | |
577 | ||
578 | if (argv[1][0] == '-' && argv[1][1] == 't') { | |
579 | argno++; | |
580 | if (argv[1][2]) | |
581 | timeout = atoi(&argv[1][2]); | |
582 | else { | |
583 | if (argno >= argc) | |
584 | usage(); | |
585 | timeout = atoi(argv[argno++]); | |
586 | } | |
587 | if (timeout <= 0) | |
588 | usage(); | |
589 | } | |
590 | if (argc <= argno) | |
591 | usage(); | |
592 | ||
593 | maxfd = fdlim_get(1); | |
594 | if (maxfd < 0) | |
595 | fatal("%s: fdlim_get: bad value", __progname); | |
596 | if (maxfd > MAXMAXFD) | |
597 | maxfd = MAXMAXFD; | |
598 | if (maxcon <= 0) | |
599 | fatal("%s: not enough file descriptors", __progname); | |
600 | if (maxfd > fdlim_get(0)) | |
601 | fdlim_set(maxfd); | |
602 | fdcon = xmalloc(maxfd * sizeof(con)); | |
603 | ||
604 | do { | |
605 | while (ncon < maxcon) { | |
606 | char *name; | |
607 | ||
608 | host = nexthost(argc, argv); | |
609 | if (host == NULL) | |
610 | break; | |
611 | name = strnnsep(&host, " \t\n"); | |
612 | conalloc(name, *host ? host : name); | |
613 | } | |
614 | conloop(); | |
615 | } while (host); | |
616 | while (ncon > 0) | |
617 | conloop(); | |
618 | ||
619 | return (0); | |
620 | } |