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