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