X-Git-Url: http://andersk.mit.edu/gitweb/openssh.git/blobdiff_plain/2671b47fa138483f3e2b8ab9f8726b967fe26e4d..da89cf4dea90eaed324c80b9062f1eed13acd107:/ssh-keyscan.c diff --git a/ssh-keyscan.c b/ssh-keyscan.c index fe03c145..3f6c2313 100644 --- a/ssh-keyscan.c +++ b/ssh-keyscan.c @@ -8,12 +8,12 @@ */ #include "includes.h" -RCSID("$OpenBSD: ssh-keyscan.c,v 1.18 2001/03/03 06:53:12 deraadt Exp $"); +RCSID("$OpenBSD: ssh-keyscan.c,v 1.22 2001/03/06 06:11:18 deraadt Exp $"); #if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H) #include #else -#include "fake-queue.h" +#include "openbsd-compat/fake-queue.h" #endif #include @@ -45,7 +45,8 @@ extern char *__progname; #else char *__progname; #endif -fd_set read_wait; +fd_set *read_wait; +size_t read_wait_size; int ncon; /* @@ -90,7 +91,7 @@ typedef struct { void (*errfun) (const char *,...); } Linebuf; -static __inline__ Linebuf * +Linebuf * Linebuf_alloc(const char *filename, void (*errfun) (const char *,...)) { Linebuf *lb; @@ -124,7 +125,7 @@ Linebuf_alloc(const char *filename, void (*errfun) (const char *,...)) return (lb); } -static __inline__ void +void Linebuf_free(Linebuf * lb) { fclose(lb->stream); @@ -132,7 +133,7 @@ Linebuf_free(Linebuf * lb) xfree(lb); } -static __inline__ void +void Linebuf_restart(Linebuf * lb) { clearerr(lb->stream); @@ -140,13 +141,13 @@ Linebuf_restart(Linebuf * lb) lb->lineno = 0; } -static __inline__ int +int Linebuf_lineno(Linebuf * lb) { return (lb->lineno); } -static __inline__ char * +char * Linebuf_getline(Linebuf * lb) { int n = 0; @@ -183,7 +184,7 @@ Linebuf_getline(Linebuf * lb) } } -static int +int fdlim_get(int hard) { #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) @@ -202,7 +203,7 @@ fdlim_get(int hard) #endif } -static int +int fdlim_set(int lim) { #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) @@ -227,7 +228,7 @@ fdlim_set(int lim) * separators. This is the same as the 4.4BSD strsep, but different from the * one in the GNU libc. */ -static __inline__ char * +char * xstrsep(char **str, const char *delim) { char *s, *e; @@ -361,7 +362,7 @@ conalloc(char *iname, char *oname) gettimeofday(&fdcon[s].c_tv, NULL); fdcon[s].c_tv.tv_sec += timeout; TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); - FD_SET(s, &read_wait); + FD_SET(s, read_wait); ncon++; return (s); } @@ -378,7 +379,7 @@ confree(int s) xfree(fdcon[s].c_data); fdcon[s].c_status = CS_UNUSED; TAILQ_REMOVE(&tq, &fdcon[s], c_link); - FD_CLR(s, &read_wait); + FD_CLR(s, read_wait); ncon--; } @@ -410,23 +411,27 @@ conrecycle(int s) void congreet(int s) { - char buf[80]; - int n; + char buf[80], *cp; + size_t bufsiz; + int n = 0; con *c = &fdcon[s]; - n = read(s, buf, sizeof(buf)); + bufsiz = sizeof(buf); + cp = buf; + while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n' && *cp != '\r') + cp++; if (n < 0) { if (errno != ECONNREFUSED) error("read (%s): %s", c->c_name, strerror(errno)); conrecycle(s); return; } - if (buf[n - 1] != '\n') { + if (*cp != '\n' && *cp != '\r') { error("%s: bad greeting", c->c_name); confree(s); return; } - buf[n - 1] = '\0'; + *cp = '\0'; fprintf(stderr, "# %s %s\n", c->c_name, buf); n = snprintf(buf, sizeof buf, "SSH-1.5-OpenSSH-keyscan\r\n"); if (atomicio(write, s, buf, n) != n) { @@ -481,7 +486,7 @@ conread(int s) void conloop(void) { - fd_set r, e; + fd_set *r, *e; struct timeval seltime, now; int i; con *c; @@ -501,18 +506,24 @@ conloop(void) } else seltime.tv_sec = seltime.tv_usec = 0; - r = e = read_wait; - while (select(maxfd, &r, NULL, &e, &seltime) == -1 && + r = xmalloc(read_wait_size); + memcpy(r, read_wait, read_wait_size); + e = xmalloc(read_wait_size); + memcpy(e, read_wait, read_wait_size); + + while (select(maxfd, r, NULL, e, &seltime) == -1 && (errno == EAGAIN || errno == EINTR)) ; for (i = 0; i < maxfd; i++) { - if (FD_ISSET(i, &e)) { + if (FD_ISSET(i, e)) { error("%s: exception!", fdcon[i].c_name); confree(i); - } else if (FD_ISSET(i, &r)) + } else if (FD_ISSET(i, r)) conread(i); } + xfree(r); + xfree(e); c = tq.tqh_first; while (c && (c->c_tv.tv_sec < now.tv_sec || @@ -567,7 +578,7 @@ nexthost(int argc, char **argv) } } -static void +void usage(void) { fatal("usage: %s [-t timeout] { [--] host | -f file } ...", __progname); @@ -612,6 +623,10 @@ main(int argc, char **argv) fdcon = xmalloc(maxfd * sizeof(con)); memset(fdcon, 0, maxfd * sizeof(con)); + read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask); + read_wait = xmalloc(read_wait_size); + memset(read_wait, 0, read_wait_size); + do { while (ncon < MAXCON) { char *name;