View | Details | Raw Unified | Return to bug 1565 | Differences between
and this patch

Collapse All | Expand All

(-)ssh-keyscan.c (-130 / +48 lines)
Lines 1-4 Link Here
1
/* $OpenBSD: ssh-keyscan.c,v 1.78 2009/01/22 10:02:34 djm Exp $ */
1
/* $OpenBSD: ssh-keyscan.c,v 1.81 2010/01/09 23:04:13 dtucker Exp $ */
2
/*
2
/*
3
 * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
3
 * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
4
 *
4
 *
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, *line;
598
	size_t i, line_len;
599
	FILE *fp;
714
600
715
	extern int optind;
601
	extern int optind;
716
	extern char *optarg;
602
	extern char *optarg;
Lines 808-827 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
	line = NULL;
812
		Linebuf *lb;
698
813
		char *line;
699
	for (j = 0; j < fopt_count; j++) {
814
		int j;
700
		if (line == NULL)
815
701
			line = xmalloc(line_len = BUFSIZ);
816
		for (j = 0; j < fopt_count; j++) {
702
817
			lb = Linebuf_alloc(argv[j], error);
703
		if ((fp = fopen(argv[j], "r")) == NULL)
818
			if (!lb)
704
			fatal("%s: %s: %s", __progname, argv[j],
705
			    strerror(errno));
706
707
		i = 0;
708
		while (fgets(&line[i], line_len - i, fp)) {
709
			/* Read a line */
710
			i += strcspn(&line[i], "\n");
711
			if (line[i] == '\0' && i == line_len - 1) {
712
				if (line_len > SIZE_MAX / 2)
713
					fatal("%s: %s: line %.20s too long",
714
					    __progname, argv[j], line);
715
				line = xrealloc(line, line_len *= 2, 1);
716
				continue;
717
			}
718
719
			/* Strip off comments and whitespace at end */
720
			for (i = strcspn(line, "#\n");
721
			     i > 0 && strchr(" \t", line[i - 1]);
722
			     i--);
723
			line[i] = '\0';
724
725
			/* Skip empty lines, comments */
726
			if (i == 0)
819
				continue;
727
				continue;
820
			while ((line = Linebuf_getline(lb)) != NULL)
728
821
				do_host(line);
729
			do_host(line);
822
			Linebuf_free(lb);
730
731
			i = 0;
823
		}
732
		}
733
734
		if (ferror(fp))
735
			fatal("%s: %s: %s", __progname, argv[j],
736
			    strerror(errno));
737
738
		fclose(fp);
824
	}
739
	}
740
741
	if (line)
742
		xfree(line);
825
743
826
	while (optind < argc)
744
	while (optind < argc)
827
		do_host(argv[optind++]);
745
		do_host(argv[optind++]);

Return to bug 1565