diff options
Diffstat (limited to 'ssh-keyscan.c')
-rw-r--r-- | ssh-keyscan.c | 165 |
1 files changed, 36 insertions, 129 deletions
diff --git a/ssh-keyscan.c b/ssh-keyscan.c index 7afe446ae..b6cf427cd 100644 --- a/ssh-keyscan.c +++ b/ssh-keyscan.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: ssh-keyscan.c,v 1.81 2010/01/09 23:04:13 dtucker Exp $ */ | 1 | /* $OpenBSD: ssh-keyscan.c,v 1.82 2010/06/22 04:54:30 djm 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 | * |
@@ -104,122 +104,6 @@ typedef struct Connection { | |||
104 | TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ | 104 | TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ |
105 | con *fdcon; | 105 | con *fdcon; |
106 | 106 | ||
107 | /* | ||
108 | * This is just a wrapper around fgets() to make it usable. | ||
109 | */ | ||
110 | |||
111 | /* Stress-test. Increase this later. */ | ||
112 | #define LINEBUF_SIZE 16 | ||
113 | |||
114 | typedef struct { | ||
115 | char *buf; | ||
116 | u_int size; | ||
117 | int lineno; | ||
118 | const char *filename; | ||
119 | FILE *stream; | ||
120 | void (*errfun) (const char *,...); | ||
121 | } Linebuf; | ||
122 | |||
123 | static Linebuf * | ||
124 | Linebuf_alloc(const char *filename, void (*errfun) (const char *,...)) | ||
125 | { | ||
126 | Linebuf *lb; | ||
127 | |||
128 | if (!(lb = malloc(sizeof(*lb)))) { | ||
129 | if (errfun) | ||
130 | (*errfun) ("linebuf (%s): malloc failed\n", | ||
131 | filename ? filename : "(stdin)"); | ||
132 | return (NULL); | ||
133 | } | ||
134 | if (filename) { | ||
135 | lb->filename = filename; | ||
136 | if (!(lb->stream = fopen(filename, "r"))) { | ||
137 | xfree(lb); | ||
138 | if (errfun) | ||
139 | (*errfun) ("%s: %s\n", filename, strerror(errno)); | ||
140 | return (NULL); | ||
141 | } | ||
142 | } else { | ||
143 | lb->filename = "(stdin)"; | ||
144 | lb->stream = stdin; | ||
145 | } | ||
146 | |||
147 | if (!(lb->buf = malloc((lb->size = LINEBUF_SIZE)))) { | ||
148 | if (errfun) | ||
149 | (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); | ||
150 | xfree(lb); | ||
151 | return (NULL); | ||
152 | } | ||
153 | lb->errfun = errfun; | ||
154 | lb->lineno = 0; | ||
155 | return (lb); | ||
156 | } | ||
157 | |||
158 | static void | ||
159 | Linebuf_free(Linebuf * lb) | ||
160 | { | ||
161 | fclose(lb->stream); | ||
162 | xfree(lb->buf); | ||
163 | xfree(lb); | ||
164 | } | ||
165 | |||
166 | #if 0 | ||
167 | static void | ||
168 | Linebuf_restart(Linebuf * lb) | ||
169 | { | ||
170 | clearerr(lb->stream); | ||
171 | rewind(lb->stream); | ||
172 | lb->lineno = 0; | ||
173 | } | ||
174 | |||
175 | static int | ||
176 | Linebuf_lineno(Linebuf * lb) | ||
177 | { | ||
178 | return (lb->lineno); | ||
179 | } | ||
180 | #endif | ||
181 | |||
182 | static char * | ||
183 | Linebuf_getline(Linebuf * lb) | ||
184 | { | ||
185 | size_t n = 0; | ||
186 | void *p; | ||
187 | |||
188 | lb->lineno++; | ||
189 | for (;;) { | ||
190 | /* Read a line */ | ||
191 | if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) { | ||
192 | if (ferror(lb->stream) && lb->errfun) | ||
193 | (*lb->errfun)("%s: %s\n", lb->filename, | ||
194 | strerror(errno)); | ||
195 | return (NULL); | ||
196 | } | ||
197 | n = strlen(lb->buf); | ||
198 | |||
199 | /* Return it or an error if it fits */ | ||
200 | if (n > 0 && lb->buf[n - 1] == '\n') { | ||
201 | lb->buf[n - 1] = '\0'; | ||
202 | return (lb->buf); | ||
203 | } | ||
204 | if (n != lb->size - 1) { | ||
205 | if (lb->errfun) | ||
206 | (*lb->errfun)("%s: skipping incomplete last line\n", | ||
207 | lb->filename); | ||
208 | return (NULL); | ||
209 | } | ||
210 | /* Double the buffer if we need more space */ | ||
211 | lb->size *= 2; | ||
212 | if ((p = realloc(lb->buf, lb->size)) == NULL) { | ||
213 | lb->size /= 2; | ||
214 | if (lb->errfun) | ||
215 | (*lb->errfun)("linebuf (%s): realloc failed\n", | ||
216 | lb->filename); | ||
217 | return (NULL); | ||
218 | } | ||
219 | lb->buf = p; | ||
220 | } | ||
221 | } | ||
222 | |||
223 | static int | 107 | static int |
224 | fdlim_get(int hard) | 108 | fdlim_get(int hard) |
225 | { | 109 | { |
@@ -724,8 +608,10 @@ int | |||
724 | main(int argc, char **argv) | 608 | main(int argc, char **argv) |
725 | { | 609 | { |
726 | int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; | 610 | int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; |
727 | int opt, fopt_count = 0; | 611 | int opt, fopt_count = 0, j; |
728 | char *tname; | 612 | char *tname, *cp, line[NI_MAXHOST]; |
613 | FILE *fp; | ||
614 | u_long linenum; | ||
729 | 615 | ||
730 | extern int optind; | 616 | extern int optind; |
731 | extern char *optarg; | 617 | extern char *optarg; |
@@ -826,19 +712,40 @@ main(int argc, char **argv) | |||
826 | read_wait_nfdset = howmany(maxfd, NFDBITS); | 712 | read_wait_nfdset = howmany(maxfd, NFDBITS); |
827 | read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask)); | 713 | read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask)); |
828 | 714 | ||
829 | if (fopt_count) { | 715 | for (j = 0; j < fopt_count; j++) { |
830 | Linebuf *lb; | 716 | if (argv[j] == NULL) |
831 | char *line; | 717 | fp = stdin; |
832 | int j; | 718 | else if ((fp = fopen(argv[j], "r")) == NULL) |
719 | fatal("%s: %s: %s", __progname, argv[j], | ||
720 | strerror(errno)); | ||
721 | linenum = 0; | ||
722 | |||
723 | while (read_keyfile_line(fp, | ||
724 | argv[j] == NULL ? "(stdin)" : argv[j], line, sizeof(line), | ||
725 | &linenum) != -1) { | ||
726 | /* Chomp off trailing whitespace and comments */ | ||
727 | if ((cp = strchr(line, '#')) == NULL) | ||
728 | cp = line + strlen(line) - 1; | ||
729 | while (cp >= line) { | ||
730 | if (*cp == ' ' || *cp == '\t' || | ||
731 | *cp == '\n' || *cp == '#') | ||
732 | *cp-- = '\0'; | ||
733 | else | ||
734 | break; | ||
735 | } | ||
833 | 736 | ||
834 | for (j = 0; j < fopt_count; j++) { | 737 | /* Skip empty lines */ |
835 | lb = Linebuf_alloc(argv[j], error); | 738 | if (*line == '\0') |
836 | if (!lb) | ||
837 | continue; | 739 | continue; |
838 | while ((line = Linebuf_getline(lb)) != NULL) | 740 | |
839 | do_host(line); | 741 | do_host(line); |
840 | Linebuf_free(lb); | ||
841 | } | 742 | } |
743 | |||
744 | if (ferror(fp)) | ||
745 | fatal("%s: %s: %s", __progname, argv[j], | ||
746 | strerror(errno)); | ||
747 | |||
748 | fclose(fp); | ||
842 | } | 749 | } |
843 | 750 | ||
844 | while (optind < argc) | 751 | while (optind < argc) |