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