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