*/
#include "includes.h"
-RCSID("$OpenBSD: ssh-keyscan.c,v 1.1 2000/12/04 19:24:02 markus Exp $");
+RCSID("$OpenBSD: ssh-keyscan.c,v 1.9 2001/01/13 18:12:47 markus Exp $");
-#ifdef HAVE_SYS_QUEUE_H
+#if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H)
#include <sys/queue.h>
#else
#include "bsd-queue.h"
#endif
-#include <err.h>
#include <errno.h>
#include <openssl/bn.h>
int family = AF_UNSPEC; /* IPv4, IPv6 or both */
-#define PORT 22
#define MAXMAXFD 256
/* The number of seconds after which to give up on a TCP connection */
int maxfd;
#define maxcon (maxfd - 10)
-char *prog;
+#ifdef HAVE___PROGNAME
+extern char *__progname;
+#else
+char *__progname;
+#endif
fd_set read_wait;
int ncon;
* associated with file descriptor n is held in fdcon[n].
*/
typedef struct Connection {
- unsigned char c_status; /* State of connection on this file desc. */
+ u_char c_status; /* State of connection on this file desc. */
#define CS_UNUSED 0 /* File descriptor unused */
#define CS_CON 1 /* Waiting to connect/read greeting */
#define CS_SIZE 2 /* Waiting to read initial packet size */
typedef struct {
char *buf;
- unsigned int size;
+ u_int size;
int lineno;
const char *filename;
FILE *stream;
if (filename) {
lb->filename = filename;
if (!(lb->stream = fopen(filename, "r"))) {
- free(lb);
+ xfree(lb);
if (errfun)
(*errfun) ("%s: %s\n", filename, strerror(errno));
return (NULL);
if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
if (errfun)
(*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
- free(lb);
+ xfree(lb);
return (NULL);
}
lb->errfun = errfun;
Linebuf_free(Linebuf * lb)
{
fclose(lb->stream);
- free(lb->buf);
- free(lb);
+ xfree(lb->buf);
+ xfree(lb);
}
static inline void
static int
fdlim_get(int hard)
{
+#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
struct rlimit rlfd;
if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
return (-1);
return 10000;
else
return hard ? rlfd.rlim_max : rlfd.rlim_cur;
+#elif defined (HAVE_SYSCONF)
+ return sysconf (_SC_OPEN_MAX);
+#else
+ return 10000;
+#endif
}
static int
fdlim_set(int lim)
{
+#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
struct rlimit rlfd;
+#endif
if (lim <= 0)
return (-1);
+#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
return (-1);
rlfd.rlim_cur = lim;
if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
return (-1);
+#elif defined (HAVE_SETDTABLESIZE)
+ setdtablesize (lim);
+#endif
return (0);
}
char strport[NI_MAXSERV];
int gaierr, s = -1;
- snprintf(strport, sizeof strport, "%d", PORT);
+ snprintf(strport, sizeof strport, "%d", SSH_DEFAULT_PORT);
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
error("socket: %s", strerror(errno));
continue;
}
- if (fcntl(s, F_SETFL, O_NDELAY) < 0)
+ if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
fatal("F_SETFL: %s", strerror(errno));
if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
errno != EINPROGRESS)
do {
name = xstrsep(&namelist, ",");
if (!name) {
- free(namebase);
+ xfree(namebase);
return (-1);
}
} while ((s = tcpconnect(name)) < 0);
if (s >= maxfd)
- fatal("conalloc: fdno %d too high\n", s);
+ fatal("conalloc: fdno %d too high", s);
if (fdcon[s].c_status)
- fatal("conalloc: attempt to reuse fdno %d\n", s);
+ fatal("conalloc: attempt to reuse fdno %d", s);
fdcon[s].c_fd = s;
fdcon[s].c_status = CS_CON;
{
close(s);
if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
- fatal("confree: attempt to free bad fdno %d\n", s);
- free(fdcon[s].c_namebase);
- free(fdcon[s].c_output_name);
+ fatal("confree: attempt to free bad fdno %d", s);
+ xfree(fdcon[s].c_namebase);
+ xfree(fdcon[s].c_output_name);
if (fdcon[s].c_status == CS_KEYS)
- free(fdcon[s].c_data);
+ xfree(fdcon[s].c_data);
fdcon[s].c_status = CS_UNUSED;
TAILQ_REMOVE(&tq, &fdcon[s], c_link);
FD_CLR(s, &read_wait);
char *iname, *oname;
iname = xstrdup(c->c_namelist);
- oname = c->c_output_name;
- c->c_output_name = NULL;/* prevent it from being freed */
+ oname = xstrdup(c->c_output_name);
confree(s);
ret = conalloc(iname, oname);
- free(iname);
+ xfree(iname);
+ xfree(oname);
return (ret);
}
return;
break;
default:
- fatal("conread: invalid status %d\n", c->c_status);
+ fatal("conread: invalid status %d", c->c_status);
break;
}
fname = argv[argno++];
if (!strcmp(fname, "-"))
fname = NULL;
- lb = Linebuf_alloc(fname, warn);
+ lb = Linebuf_alloc(fname, error);
} else
error("ignoring invalid/misplaced option `%s'", argv[argno++]);
} else {
static void
usage(void)
{
- fatal("usage: %s [-t timeout] { [--] host | -f file } ...\n", prog);
+ fatal("usage: %s [-t timeout] { [--] host | -f file } ...", __progname);
return;
}
{
char *host = NULL;
+ __progname = get_progname(argv[0]);
TAILQ_INIT(&tq);
- if ((prog = strrchr(argv[0], '/')))
- prog++;
- else
- prog = argv[0];
-
if (argc <= argno)
usage();
maxfd = fdlim_get(1);
if (maxfd < 0)
- fatal("%s: fdlim_get: bad value\n", prog);
+ fatal("%s: fdlim_get: bad value", __progname);
if (maxfd > MAXMAXFD)
maxfd = MAXMAXFD;
if (maxcon <= 0)
- fatal("%s: not enough file descriptors\n", prog);
+ fatal("%s: not enough file descriptors", __progname);
if (maxfd > fdlim_get(0))
fdlim_set(maxfd);
fdcon = xmalloc(maxfd * sizeof(con));