|
Lines 99-220
typedef struct Connection {
Link Here
|
| 99 |
TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ |
99 |
TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ |
| 100 |
con *fdcon; |
100 |
con *fdcon; |
| 101 |
|
101 |
|
| 102 |
/* |
|
|
| 103 |
* This is just a wrapper around fgets() to make it usable. |
| 104 |
*/ |
| 105 |
|
| 106 |
/* Stress-test. Increase this later. */ |
| 107 |
#define LINEBUF_SIZE 16 |
| 108 |
|
| 109 |
typedef struct { |
| 110 |
char *buf; |
| 111 |
u_int size; |
| 112 |
int lineno; |
| 113 |
const char *filename; |
| 114 |
FILE *stream; |
| 115 |
void (*errfun) (const char *,...); |
| 116 |
} Linebuf; |
| 117 |
|
| 118 |
static Linebuf * |
| 119 |
Linebuf_alloc(const char *filename, void (*errfun) (const char *,...)) |
| 120 |
{ |
| 121 |
Linebuf *lb; |
| 122 |
|
| 123 |
if (!(lb = malloc(sizeof(*lb)))) { |
| 124 |
if (errfun) |
| 125 |
(*errfun) ("linebuf (%s): malloc failed\n", |
| 126 |
filename ? filename : "(stdin)"); |
| 127 |
return (NULL); |
| 128 |
} |
| 129 |
if (filename) { |
| 130 |
lb->filename = filename; |
| 131 |
if (!(lb->stream = fopen(filename, "r"))) { |
| 132 |
xfree(lb); |
| 133 |
if (errfun) |
| 134 |
(*errfun) ("%s: %s\n", filename, strerror(errno)); |
| 135 |
return (NULL); |
| 136 |
} |
| 137 |
} else { |
| 138 |
lb->filename = "(stdin)"; |
| 139 |
lb->stream = stdin; |
| 140 |
} |
| 141 |
|
| 142 |
if (!(lb->buf = malloc((lb->size = LINEBUF_SIZE)))) { |
| 143 |
if (errfun) |
| 144 |
(*errfun) ("linebuf (%s): malloc failed\n", lb->filename); |
| 145 |
xfree(lb); |
| 146 |
return (NULL); |
| 147 |
} |
| 148 |
lb->errfun = errfun; |
| 149 |
lb->lineno = 0; |
| 150 |
return (lb); |
| 151 |
} |
| 152 |
|
| 153 |
static void |
| 154 |
Linebuf_free(Linebuf * lb) |
| 155 |
{ |
| 156 |
fclose(lb->stream); |
| 157 |
xfree(lb->buf); |
| 158 |
xfree(lb); |
| 159 |
} |
| 160 |
|
| 161 |
#if 0 |
| 162 |
static void |
| 163 |
Linebuf_restart(Linebuf * lb) |
| 164 |
{ |
| 165 |
clearerr(lb->stream); |
| 166 |
rewind(lb->stream); |
| 167 |
lb->lineno = 0; |
| 168 |
} |
| 169 |
|
| 170 |
static int |
| 171 |
Linebuf_lineno(Linebuf * lb) |
| 172 |
{ |
| 173 |
return (lb->lineno); |
| 174 |
} |
| 175 |
#endif |
| 176 |
|
| 177 |
static char * |
| 178 |
Linebuf_getline(Linebuf * lb) |
| 179 |
{ |
| 180 |
size_t n = 0; |
| 181 |
void *p; |
| 182 |
|
| 183 |
lb->lineno++; |
| 184 |
for (;;) { |
| 185 |
/* Read a line */ |
| 186 |
if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) { |
| 187 |
if (ferror(lb->stream) && lb->errfun) |
| 188 |
(*lb->errfun)("%s: %s\n", lb->filename, |
| 189 |
strerror(errno)); |
| 190 |
return (NULL); |
| 191 |
} |
| 192 |
n = strlen(lb->buf); |
| 193 |
|
| 194 |
/* Return it or an error if it fits */ |
| 195 |
if (n > 0 && lb->buf[n - 1] == '\n') { |
| 196 |
lb->buf[n - 1] = '\0'; |
| 197 |
return (lb->buf); |
| 198 |
} |
| 199 |
if (n != lb->size - 1) { |
| 200 |
if (lb->errfun) |
| 201 |
(*lb->errfun)("%s: skipping incomplete last line\n", |
| 202 |
lb->filename); |
| 203 |
return (NULL); |
| 204 |
} |
| 205 |
/* Double the buffer if we need more space */ |
| 206 |
lb->size *= 2; |
| 207 |
if ((p = realloc(lb->buf, lb->size)) == NULL) { |
| 208 |
lb->size /= 2; |
| 209 |
if (lb->errfun) |
| 210 |
(*lb->errfun)("linebuf (%s): realloc failed\n", |
| 211 |
lb->filename); |
| 212 |
return (NULL); |
| 213 |
} |
| 214 |
lb->buf = p; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
static int |
102 |
static int |
| 219 |
fdlim_get(int hard) |
103 |
fdlim_get(int hard) |
| 220 |
{ |
104 |
{ |
|
Lines 709-716
int
Link Here
|
| 709 |
main(int argc, char **argv) |
593 |
main(int argc, char **argv) |
| 710 |
{ |
594 |
{ |
| 711 |
int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; |
595 |
int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; |
| 712 |
int opt, fopt_count = 0; |
596 |
int opt, fopt_count = 0, j; |
| 713 |
char *tname; |
597 |
char *tname, *cp, line[SSH_MAX_PUBKEY_BYTES]; |
|
|
598 |
FILE *fp; |
| 599 |
u_long linenum = 0; |
| 714 |
|
600 |
|
| 715 |
extern int optind; |
601 |
extern int optind; |
| 716 |
extern char *optarg; |
602 |
extern char *optarg; |
|
Lines 808-826
main(int argc, char **argv)
Link Here
|
| 808 |
read_wait_nfdset = howmany(maxfd, NFDBITS); |
694 |
read_wait_nfdset = howmany(maxfd, NFDBITS); |
| 809 |
read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask)); |
695 |
read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask)); |
| 810 |
|
696 |
|
| 811 |
if (fopt_count) { |
697 |
for (j = 0; j < fopt_count; j++) { |
| 812 |
Linebuf *lb; |
698 |
if (argv[j] == NULL) |
| 813 |
char *line; |
699 |
fp = stdin; |
| 814 |
int j; |
700 |
else if ((fp = fopen(argv[j], "r")) == NULL) |
| 815 |
|
701 |
fatal("%s: %s: %s", __progname, argv[j], |
| 816 |
for (j = 0; j < fopt_count; j++) { |
702 |
strerror(errno)); |
| 817 |
lb = Linebuf_alloc(argv[j], error); |
703 |
|
| 818 |
if (!lb) |
704 |
while (read_keyfile_line(fp, |
|
|
705 |
argv[j] == NULL ? "(stdin)" : argv[j], line, sizeof(line), |
| 706 |
&linenum) != -1) { |
| 707 |
/* Chomp off trailing whitespace and comments */ |
| 708 |
if ((cp = strchr(line, '#')) == NULL) |
| 709 |
cp = line + strlen(line) - 1; |
| 710 |
while (cp >= line) { |
| 711 |
if (*cp == ' ' || *cp == '\t' || |
| 712 |
*cp == '\n' || *cp == '#') |
| 713 |
*cp-- = '\0'; |
| 714 |
else |
| 715 |
break; |
| 716 |
} |
| 717 |
|
| 718 |
/* Skip empty lines */ |
| 719 |
if (*line == '\0') |
| 819 |
continue; |
720 |
continue; |
| 820 |
while ((line = Linebuf_getline(lb)) != NULL) |
721 |
|
| 821 |
do_host(line); |
722 |
do_host(line); |
| 822 |
Linebuf_free(lb); |
|
|
| 823 |
} |
723 |
} |
|
|
724 |
|
| 725 |
if (ferror(fp)) |
| 726 |
fatal("%s: %s: %s", __progname, argv[j], |
| 727 |
strerror(errno)); |
| 728 |
|
| 729 |
fclose(fp); |
| 824 |
} |
730 |
} |
| 825 |
|
731 |
|
| 826 |
while (optind < argc) |
732 |
while (optind < argc) |