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