blob: 381fb0844da7423943f8012d4b5809217ed63a71 [file] [log] [blame]
jmc@openbsd.orgf493d2b2018-03-02 21:40:15 +00001/* $OpenBSD: ssh-keyscan.c,v 1.119 2018/03/02 21:40:15 jmc Exp $ */
Ben Lindstromb6434ae2000-12-05 01:15:09 +00002/*
3 * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
4 *
5 * Modification and redistribution in source and binary forms is
6 * permitted provided that due credit is given to the author and the
Ben Lindstroma238f6e2001-06-09 01:30:39 +00007 * OpenBSD project by leaving this copyright notice intact.
Ben Lindstromb6434ae2000-12-05 01:15:09 +00008 */
9
10#include "includes.h"
Damien Millercd4223c2006-03-15 11:22:47 +110011
djm@openbsd.org48b68ce2014-12-11 08:20:09 +000012#include <sys/types.h>
Damien Miller9b481512002-09-12 10:43:29 +100013#include "openbsd-compat/sys-queue.h"
Damien Millercd4223c2006-03-15 11:22:47 +110014#include <sys/resource.h>
Damien Miller9aec9192006-08-05 10:57:45 +100015#ifdef HAVE_SYS_TIME_H
16# include <sys/time.h>
17#endif
Damien Millere3476ed2006-07-24 14:13:33 +100018
Darren Tucker46aa3e02006-09-02 15:32:40 +100019#include <netinet/in.h>
20#include <arpa/inet.h>
21
Damien Millere3476ed2006-07-24 14:13:33 +100022#include <openssl/bn.h>
23
Damien Millerb8fe89c2006-07-24 14:51:00 +100024#include <netdb.h>
Darren Tuckerdeecec92006-07-12 22:44:34 +100025#include <errno.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100026#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100027#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100028#include <stdlib.h>
Damien Millerd7834352006-08-05 12:39:39 +100029#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100030#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100031#include <unistd.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000032
Ben Lindstromb6434ae2000-12-05 01:15:09 +000033#include "xmalloc.h"
34#include "ssh.h"
markus@openbsd.org3f797652015-01-19 20:32:39 +000035#include "sshbuf.h"
36#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100037#include "cipher.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000038#include "kex.h"
39#include "compat.h"
40#include "myproposal.h"
41#include "packet.h"
42#include "dispatch.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include "log.h"
Ben Lindstromd20b8552001-03-05 07:01:18 +000044#include "atomicio.h"
Ben Lindstrom325e70c2001-08-06 22:41:30 +000045#include "misc.h"
Damien Millerdb7b8172005-03-01 21:48:03 +110046#include "hostfile.h"
markus@openbsd.org3f797652015-01-19 20:32:39 +000047#include "ssherr.h"
48#include "ssh_api.h"
djm@openbsd.org1a348352018-02-23 05:14:05 +000049#include "dns.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000050
Ben Lindstrom325e70c2001-08-06 22:41:30 +000051/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
52 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Ben Lindstrom325e70c2001-08-06 22:41:30 +000053int IPv4or6 = AF_UNSPEC;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000054
Ben Lindstrom325e70c2001-08-06 22:41:30 +000055int ssh_port = SSH_DEFAULT_PORT;
Kevin Steves76e7d9b2001-09-20 20:30:09 +000056
djm@openbsd.org873d3e72017-04-30 23:18:44 +000057#define KT_DSA (1)
58#define KT_RSA (1<<1)
59#define KT_ECDSA (1<<2)
60#define KT_ED25519 (1<<3)
markus@openbsd.org1b11ea72018-02-23 15:58:37 +000061#define KT_XMSS (1<<4)
djm@openbsd.org873d3e72017-04-30 23:18:44 +000062
63#define KT_MIN KT_DSA
markus@openbsd.org1b11ea72018-02-23 15:58:37 +000064#define KT_MAX KT_XMSS
Kevin Steves76e7d9b2001-09-20 20:30:09 +000065
djm@openbsd.org3a424cd2015-11-08 22:30:20 +000066int get_cert = 0;
Damien Miller1c7ef4b2014-04-20 12:59:46 +100067int get_keytypes = KT_RSA|KT_ECDSA|KT_ED25519;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000068
Damien Millerdb7b8172005-03-01 21:48:03 +110069int hash_hosts = 0; /* Hash hostname on output */
70
djm@openbsd.org1a348352018-02-23 05:14:05 +000071int print_sshfp = 0; /* Print SSHFP records instead of known_hosts */
72
Ben Lindstromb6434ae2000-12-05 01:15:09 +000073#define MAXMAXFD 256
74
75/* The number of seconds after which to give up on a TCP connection */
76int timeout = 5;
77
78int maxfd;
Ben Lindstromd20b8552001-03-05 07:01:18 +000079#define MAXCON (maxfd - 10)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000080
Kevin Stevesec84dc12000-12-13 17:45:15 +000081extern char *__progname;
Ben Lindstromc1e04212001-03-05 07:04:38 +000082fd_set *read_wait;
Damien Miller07d86be2006-03-26 14:19:21 +110083size_t read_wait_nfdset;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000084int ncon;
85
Damien Millerade31d72015-01-27 23:06:23 +110086struct ssh *active_state = NULL; /* XXX needed for linking */
87
Ben Lindstromb6434ae2000-12-05 01:15:09 +000088/*
89 * Keep a connection structure for each file descriptor. The state
90 * associated with file descriptor n is held in fdcon[n].
91 */
92typedef struct Connection {
Ben Lindstrom46c16222000-12-22 01:43:59 +000093 u_char c_status; /* State of connection on this file desc. */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000094#define CS_UNUSED 0 /* File descriptor unused */
95#define CS_CON 1 /* Waiting to connect/read greeting */
96#define CS_SIZE 2 /* Waiting to read initial packet size */
97#define CS_KEYS 3 /* Waiting to read public key packet */
98 int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
99 int c_plen; /* Packet length field for ssh packet */
100 int c_len; /* Total bytes which must be read. */
101 int c_off; /* Length of data read so far. */
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000102 int c_keytype; /* Only one of KT_* */
miod@openbsd.orgc265e2e2015-04-05 15:43:43 +0000103 sig_atomic_t c_done; /* SSH2 done */
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000104 char *c_namebase; /* Address to free for c_name and c_namelist */
105 char *c_name; /* Hostname of connection for errors */
106 char *c_namelist; /* Pointer to other possible addresses */
107 char *c_output_name; /* Hostname of connection for output */
108 char *c_data; /* Data read from this fd */
markus@openbsd.org3f797652015-01-19 20:32:39 +0000109 struct ssh *c_ssh; /* SSH-connection */
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000110 struct timeval c_tv; /* Time at which connection gets aborted */
111 TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
112} con;
113
114TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
115con *fdcon;
116
markus@openbsd.org3f797652015-01-19 20:32:39 +0000117static void keyprint(con *c, struct sshkey *key);
118
Ben Lindstrombba81212001-06-25 05:01:22 +0000119static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000120fdlim_get(int hard)
121{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000122#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000123 struct rlimit rlfd;
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000124
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000125 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
126 return (-1);
127 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
Damien Millere00074a2003-11-24 13:07:45 +1100128 return SSH_SYSFDMAX;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000129 else
130 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000131#else
Damien Millere00074a2003-11-24 13:07:45 +1100132 return SSH_SYSFDMAX;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000133#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000134}
135
Ben Lindstrombba81212001-06-25 05:01:22 +0000136static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000137fdlim_set(int lim)
138{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000139#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000140 struct rlimit rlfd;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000141#endif
Ben Lindstrom5c98db52002-07-07 22:25:29 +0000142
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000143 if (lim <= 0)
144 return (-1);
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000145#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000146 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
147 return (-1);
148 rlfd.rlim_cur = lim;
149 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
150 return (-1);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000151#elif defined (HAVE_SETDTABLESIZE)
Kevin Steves28a7f262001-02-05 15:43:59 +0000152 setdtablesize(lim);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000153#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000154 return (0);
155}
156
157/*
158 * This is an strsep function that returns a null field for adjacent
159 * separators. This is the same as the 4.4BSD strsep, but different from the
160 * one in the GNU libc.
161 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000163xstrsep(char **str, const char *delim)
164{
165 char *s, *e;
166
167 if (!**str)
168 return (NULL);
169
170 s = *str;
171 e = s + strcspn(s, delim);
172
173 if (*e != '\0')
174 *e++ = '\0';
175 *str = e;
176
177 return (s);
178}
179
180/*
181 * Get the next non-null token (like GNU strsep). Strsep() will return a
182 * null token for two adjacent separators, so we may have to loop.
183 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000184static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000185strnnsep(char **stringp, char *delim)
186{
187 char *tok;
188
189 do {
190 tok = xstrsep(stringp, delim);
191 } while (tok && *tok == '\0');
192 return (tok);
193}
194
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000195
196static int
markus@openbsd.org3f797652015-01-19 20:32:39 +0000197key_print_wrapper(struct sshkey *hostkey, struct ssh *ssh)
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000198{
markus@openbsd.org3f797652015-01-19 20:32:39 +0000199 con *c;
200
201 if ((c = ssh_get_app_data(ssh)) != NULL)
202 keyprint(c, hostkey);
203 /* always abort key exchange */
204 return -1;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000205}
206
207static int
208ssh2_capable(int remote_major, int remote_minor)
209{
210 switch (remote_major) {
211 case 1:
212 if (remote_minor == 99)
213 return 1;
214 break;
215 case 2:
216 return 1;
217 default:
218 break;
219 }
220 return 0;
221}
222
markus@openbsd.org3f797652015-01-19 20:32:39 +0000223static void
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000224keygrab_ssh2(con *c)
225{
Damien Miller9235a032014-04-20 13:17:20 +1000226 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
markus@openbsd.org3f797652015-01-19 20:32:39 +0000227 int r;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000228
djm@openbsd.org3a424cd2015-11-08 22:30:20 +0000229 switch (c->c_keytype) {
230 case KT_DSA:
231 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
232 "ssh-dss-cert-v01@openssh.com" : "ssh-dss";
233 break;
234 case KT_RSA:
235 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
236 "ssh-rsa-cert-v01@openssh.com" : "ssh-rsa";
237 break;
238 case KT_ED25519:
239 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
240 "ssh-ed25519-cert-v01@openssh.com" : "ssh-ed25519";
241 break;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000242 case KT_XMSS:
243 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
244 "ssh-xmss-cert-v01@openssh.com" : "ssh-xmss@openssh.com";
245 break;
djm@openbsd.org3a424cd2015-11-08 22:30:20 +0000246 case KT_ECDSA:
247 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
248 "ecdsa-sha2-nistp256-cert-v01@openssh.com,"
249 "ecdsa-sha2-nistp384-cert-v01@openssh.com,"
250 "ecdsa-sha2-nistp521-cert-v01@openssh.com" :
251 "ecdsa-sha2-nistp256,"
252 "ecdsa-sha2-nistp384,"
253 "ecdsa-sha2-nistp521";
254 break;
255 default:
256 fatal("unknown key type %d", c->c_keytype);
257 break;
258 }
markus@openbsd.org3f797652015-01-19 20:32:39 +0000259 if ((r = kex_setup(c->c_ssh, myproposal)) != 0) {
260 free(c->c_ssh);
261 fprintf(stderr, "kex_setup: %s\n", ssh_err(r));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000262 exit(1);
263 }
markus@openbsd.org3f797652015-01-19 20:32:39 +0000264#ifdef WITH_OPENSSL
265 c->c_ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
266 c->c_ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +0000267 c->c_ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
268 c->c_ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
269 c->c_ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
markus@openbsd.org3f797652015-01-19 20:32:39 +0000270 c->c_ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
271 c->c_ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100272# ifdef OPENSSL_HAS_ECC
markus@openbsd.org3f797652015-01-19 20:32:39 +0000273 c->c_ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100274# endif
markus@openbsd.org3f797652015-01-19 20:32:39 +0000275#endif
276 c->c_ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_client;
277 ssh_set_verify_host_key_callback(c->c_ssh, key_print_wrapper);
278 /*
279 * do the key-exchange until an error occurs or until
280 * the key_print_wrapper() callback sets c_done.
281 */
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000282 ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000283}
284
285static void
djm@openbsd.org8a283442017-03-10 03:18:24 +0000286keyprint_one(const char *host, struct sshkey *key)
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000287{
djm@openbsd.org9ada37d2015-10-24 22:56:19 +0000288 char *hostport;
djm@openbsd.org8a283442017-03-10 03:18:24 +0000289 const char *known_host, *hashed;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000290
djm@openbsd.org1a348352018-02-23 05:14:05 +0000291 if (print_sshfp) {
292 export_dns_rr(host, key, stdout, 0);
293 return;
294 }
295
djm@openbsd.org2c2cfe12015-04-10 00:08:55 +0000296 hostport = put_host_port(host, ssh_port);
djm@openbsd.orgdb259722017-03-10 04:26:06 +0000297 lowercase(hostport);
djm@openbsd.org8a283442017-03-10 03:18:24 +0000298 if (hash_hosts && (hashed = host_hash(host, NULL, 0)) == NULL)
299 fatal("host_hash failed");
300 known_host = hash_hosts ? hashed : hostport;
djm@openbsd.org3a424cd2015-11-08 22:30:20 +0000301 if (!get_cert)
djm@openbsd.org8a283442017-03-10 03:18:24 +0000302 fprintf(stdout, "%s ", known_host);
markus@openbsd.org3f797652015-01-19 20:32:39 +0000303 sshkey_write(key, stdout);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000304 fputs("\n", stdout);
djm@openbsd.org2c2cfe12015-04-10 00:08:55 +0000305 free(hostport);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000306}
307
djm@openbsd.org9ada37d2015-10-24 22:56:19 +0000308static void
309keyprint(con *c, struct sshkey *key)
310{
311 char *hosts = c->c_output_name ? c->c_output_name : c->c_name;
312 char *host, *ohosts;
313
314 if (key == NULL)
315 return;
djm@openbsd.org3a424cd2015-11-08 22:30:20 +0000316 if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) {
djm@openbsd.org9ada37d2015-10-24 22:56:19 +0000317 keyprint_one(hosts, key);
318 return;
319 }
320 ohosts = hosts = xstrdup(hosts);
321 while ((host = strsep(&hosts, ",")) != NULL)
322 keyprint_one(host, key);
323 free(ohosts);
324}
325
Ben Lindstrombba81212001-06-25 05:01:22 +0000326static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000327tcpconnect(char *host)
328{
329 struct addrinfo hints, *ai, *aitop;
330 char strport[NI_MAXSERV];
331 int gaierr, s = -1;
332
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000333 snprintf(strport, sizeof strport, "%d", ssh_port);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000334 memset(&hints, 0, sizeof(hints));
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000335 hints.ai_family = IPv4or6;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000336 hints.ai_socktype = SOCK_STREAM;
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +0000337 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
338 error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
339 return -1;
340 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000341 for (ai = aitop; ai; ai = ai->ai_next) {
Darren Tucker7bd98e72010-01-10 10:31:12 +1100342 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000343 if (s < 0) {
344 error("socket: %s", strerror(errno));
345 continue;
346 }
Damien Miller232711f2004-06-15 10:35:30 +1000347 if (set_nonblock(s) == -1)
348 fatal("%s: set_nonblock(%d)", __func__, s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000349 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
350 errno != EINPROGRESS)
351 error("connect (`%s'): %s", host, strerror(errno));
352 else
353 break;
354 close(s);
355 s = -1;
356 }
357 freeaddrinfo(aitop);
358 return s;
359}
360
Ben Lindstrombba81212001-06-25 05:01:22 +0000361static int
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000362conalloc(char *iname, char *oname, int keytype)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000363{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000364 char *namebase, *name, *namelist;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000365 int s;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000366
367 namebase = namelist = xstrdup(iname);
368
369 do {
370 name = xstrsep(&namelist, ",");
371 if (!name) {
Darren Tuckera627d422013-06-02 07:31:17 +1000372 free(namebase);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000373 return (-1);
374 }
375 } while ((s = tcpconnect(name)) < 0);
376
377 if (s >= maxfd)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000378 fatal("conalloc: fdno %d too high", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000379 if (fdcon[s].c_status)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000380 fatal("conalloc: attempt to reuse fdno %d", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000381
djm@openbsd.org3a424cd2015-11-08 22:30:20 +0000382 debug3("%s: oname %s kt %d", __func__, oname, keytype);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000383 fdcon[s].c_fd = s;
384 fdcon[s].c_status = CS_CON;
385 fdcon[s].c_namebase = namebase;
386 fdcon[s].c_name = name;
387 fdcon[s].c_namelist = namelist;
388 fdcon[s].c_output_name = xstrdup(oname);
389 fdcon[s].c_data = (char *) &fdcon[s].c_plen;
390 fdcon[s].c_len = 4;
391 fdcon[s].c_off = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000392 fdcon[s].c_keytype = keytype;
dtucker@openbsd.org@openbsd.org5db6fbf2017-11-25 06:46:22 +0000393 monotime_tv(&fdcon[s].c_tv);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000394 fdcon[s].c_tv.tv_sec += timeout;
395 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000396 FD_SET(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000397 ncon++;
398 return (s);
399}
400
Ben Lindstrombba81212001-06-25 05:01:22 +0000401static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000402confree(int s)
403{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000404 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000405 fatal("confree: attempt to free bad fdno %d", s);
Darren Tuckera627d422013-06-02 07:31:17 +1000406 free(fdcon[s].c_namebase);
407 free(fdcon[s].c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000408 if (fdcon[s].c_status == CS_KEYS)
Darren Tuckera627d422013-06-02 07:31:17 +1000409 free(fdcon[s].c_data);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000410 fdcon[s].c_status = CS_UNUSED;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000411 fdcon[s].c_keytype = 0;
markus@openbsd.org3f797652015-01-19 20:32:39 +0000412 if (fdcon[s].c_ssh) {
413 ssh_packet_close(fdcon[s].c_ssh);
414 free(fdcon[s].c_ssh);
415 fdcon[s].c_ssh = NULL;
dtucker@openbsd.orgd79bceb2017-06-30 04:17:23 +0000416 } else
417 close(s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000418 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000419 FD_CLR(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000420 ncon--;
421}
422
Ben Lindstrombba81212001-06-25 05:01:22 +0000423static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000424contouch(int s)
425{
426 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
dtucker@openbsd.org@openbsd.org5db6fbf2017-11-25 06:46:22 +0000427 monotime_tv(&fdcon[s].c_tv);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000428 fdcon[s].c_tv.tv_sec += timeout;
429 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
430}
431
Ben Lindstrombba81212001-06-25 05:01:22 +0000432static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000433conrecycle(int s)
434{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000435 con *c = &fdcon[s];
Ben Lindstrom965710f2002-07-07 22:17:22 +0000436 int ret;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000437
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000438 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000439 confree(s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000440 return (ret);
441}
442
Ben Lindstrombba81212001-06-25 05:01:22 +0000443static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000444congreet(int s)
445{
Damien Millereccb9de2005-06-17 12:59:34 +1000446 int n = 0, remote_major = 0, remote_minor = 0;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000447 char buf[256], *cp;
Damien Miller83c02ef2001-12-21 12:45:43 +1100448 char remote_version[sizeof buf];
Damien Millereccb9de2005-06-17 12:59:34 +1000449 size_t bufsiz;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000450 con *c = &fdcon[s];
451
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000452 /* send client banner */
453 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
454 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
455 if (n < 0 || (size_t)n >= sizeof(buf)) {
456 error("snprintf: buffer too small");
457 confree(s);
458 return;
459 }
460 if (atomicio(vwrite, s, buf, n) != (size_t)n) {
461 error("write (%s): %s", c->c_name, strerror(errno));
462 confree(s);
463 return;
464 }
465
Damien Miller4bbacb72005-11-05 15:12:28 +1100466 for (;;) {
467 memset(buf, '\0', sizeof(buf));
468 bufsiz = sizeof(buf);
469 cp = buf;
470 while (bufsiz-- &&
471 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
472 if (*cp == '\r')
473 *cp = '\n';
474 cp++;
475 }
476 if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
477 break;
Ben Lindstromde8fc6f2001-08-06 22:43:50 +0000478 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000479 if (n == 0) {
Damien Millerb253cc42005-05-26 12:23:44 +1000480 switch (errno) {
481 case EPIPE:
482 error("%s: Connection closed by remote host", c->c_name);
483 break;
484 case ECONNREFUSED:
485 break;
486 default:
487 error("read (%s): %s", c->c_name, strerror(errno));
488 break;
489 }
Ben Lindstrom6b28c352002-03-05 01:54:52 +0000490 conrecycle(s);
491 return;
492 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000493 if (*cp != '\n' && *cp != '\r') {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000494 error("%s: bad greeting", c->c_name);
495 confree(s);
496 return;
497 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000498 *cp = '\0';
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000499 if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
500 fatal("ssh_packet_set_connection failed");
djm@openbsd.org802660c2015-01-30 10:44:49 +0000501 ssh_packet_set_timeout(c->c_ssh, timeout, 1);
markus@openbsd.org3f797652015-01-19 20:32:39 +0000502 ssh_set_app_data(c->c_ssh, c); /* back link */
Damien Miller83c02ef2001-12-21 12:45:43 +1100503 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
504 &remote_major, &remote_minor, remote_version) == 3)
markus@openbsd.org3f797652015-01-19 20:32:39 +0000505 c->c_ssh->compat = compat_datafellows(remote_version);
Damien Miller83c02ef2001-12-21 12:45:43 +1100506 else
markus@openbsd.org3f797652015-01-19 20:32:39 +0000507 c->c_ssh->compat = 0;
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000508 if (!ssh2_capable(remote_major, remote_minor)) {
509 debug("%s doesn't support ssh2", c->c_name);
Damien Miller83c02ef2001-12-21 12:45:43 +1100510 confree(s);
511 return;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000512 }
djm@openbsd.org1a348352018-02-23 05:14:05 +0000513 fprintf(stderr, "%c %s:%d %s\n", print_sshfp ? ';' : '#',
514 c->c_name, ssh_port, chop(buf));
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000515 keygrab_ssh2(c);
516 confree(s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000517}
518
Ben Lindstrombba81212001-06-25 05:01:22 +0000519static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000520conread(int s)
521{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000522 con *c = &fdcon[s];
Damien Millerb253cc42005-05-26 12:23:44 +1000523 size_t n;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000524
525 if (c->c_status == CS_CON) {
526 congreet(s);
527 return;
528 }
Darren Tuckerfe6649d2004-08-13 21:19:37 +1000529 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
Damien Millerb253cc42005-05-26 12:23:44 +1000530 if (n == 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000531 error("read (%s): %s", c->c_name, strerror(errno));
532 confree(s);
533 return;
534 }
535 c->c_off += n;
536
537 if (c->c_off == c->c_len)
538 switch (c->c_status) {
539 case CS_SIZE:
540 c->c_plen = htonl(c->c_plen);
541 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
542 c->c_off = 0;
543 c->c_data = xmalloc(c->c_len);
544 c->c_status = CS_KEYS;
545 break;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000546 default:
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000547 fatal("conread: invalid status %d", c->c_status);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000548 break;
549 }
550
551 contouch(s);
552}
553
Ben Lindstrombba81212001-06-25 05:01:22 +0000554static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000555conloop(void)
556{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000557 struct timeval seltime, now;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000558 fd_set *r, *e;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000559 con *c;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000560 int i;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000561
dtucker@openbsd.org@openbsd.org5db6fbf2017-11-25 06:46:22 +0000562 monotime_tv(&now);
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000563 c = TAILQ_FIRST(&tq);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000564
Ben Lindstromd20b8552001-03-05 07:01:18 +0000565 if (c && (c->c_tv.tv_sec > now.tv_sec ||
566 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000567 seltime = c->c_tv;
568 seltime.tv_sec -= now.tv_sec;
569 seltime.tv_usec -= now.tv_usec;
Ben Lindstromc791beb2001-02-10 23:18:11 +0000570 if (seltime.tv_usec < 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000571 seltime.tv_usec += 1000000;
572 seltime.tv_sec--;
573 }
574 } else
Damien Millerc5219e72011-05-05 14:05:12 +1000575 timerclear(&seltime);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000576
Damien Miller07d86be2006-03-26 14:19:21 +1100577 r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
578 e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
579 memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
580 memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
Ben Lindstromc1e04212001-03-05 07:04:38 +0000581
582 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
Damien Millerd8968ad2008-07-04 23:10:49 +1000583 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstromf9452512001-02-15 03:12:08 +0000584 ;
585
Ben Lindstromd20b8552001-03-05 07:01:18 +0000586 for (i = 0; i < maxfd; i++) {
Ben Lindstromc1e04212001-03-05 07:04:38 +0000587 if (FD_ISSET(i, e)) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000588 error("%s: exception!", fdcon[i].c_name);
589 confree(i);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000590 } else if (FD_ISSET(i, r))
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000591 conread(i);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000592 }
Darren Tuckera627d422013-06-02 07:31:17 +1000593 free(r);
594 free(e);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000595
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000596 c = TAILQ_FIRST(&tq);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000597 while (c && (c->c_tv.tv_sec < now.tv_sec ||
598 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000599 int s = c->c_fd;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000600
Ben Lindstrom61c183b2002-06-21 00:09:54 +0000601 c = TAILQ_NEXT(c, c_link);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000602 conrecycle(s);
603 }
604}
605
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000606static void
607do_host(char *host)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000608{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000609 char *name = strnnsep(&host, " \t\n");
610 int j;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000611
Ben Lindstromeaffb9d2001-12-06 16:28:19 +0000612 if (name == NULL)
613 return;
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000614 for (j = KT_MIN; j <= KT_MAX; j *= 2) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000615 if (get_keytypes & j) {
616 while (ncon >= MAXCON)
617 conloop();
618 conalloc(name, *host ? host : name, j);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000619 }
620 }
621}
622
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000623void
624fatal(const char *fmt,...)
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000625{
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000626 va_list args;
Ben Lindstrom965710f2002-07-07 22:17:22 +0000627
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000628 va_start(args, fmt);
629 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
630 va_end(args);
markus@openbsd.org3f797652015-01-19 20:32:39 +0000631 exit(255);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000632}
633
634static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000635usage(void)
636{
Damien Millerc1719f72008-11-03 19:27:07 +1100637 fprintf(stderr,
djm@openbsd.org1a348352018-02-23 05:14:05 +0000638 "usage: %s [-46cDHv] [-f file] [-p port] [-T timeout] [-t type]\n"
jmc@openbsd.orgf493d2b2018-03-02 21:40:15 +0000639 "\t\t [host | addrlist namelist]\n",
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000640 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000641 exit(1);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000642}
643
644int
645main(int argc, char **argv)
646{
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000647 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
Damien Miller0e76c5e2010-06-26 09:39:59 +1000648 int opt, fopt_count = 0, j;
649 char *tname, *cp, line[NI_MAXHOST];
650 FILE *fp;
651 u_long linenum;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000652
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000653 extern int optind;
654 extern char *optarg;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000655
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +0000656 ssh_malloc_init(); /* must be called before any mallocs */
Damien Miller59d3d5b2003-08-22 09:34:41 +1000657 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom4e088e42001-10-10 20:45:43 +0000658 seed_rng();
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000659 TAILQ_INIT(&tq);
660
Darren Tuckerce321d82005-10-03 18:11:24 +1000661 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
662 sanitise_stdfd();
663
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000664 if (argc <= 1)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000665 usage();
666
djm@openbsd.org1a348352018-02-23 05:14:05 +0000667 while ((opt = getopt(argc, argv, "cDHv46p:T:t:f:")) != -1) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000668 switch (opt) {
Damien Millerdb7b8172005-03-01 21:48:03 +1100669 case 'H':
670 hash_hosts = 1;
671 break;
djm@openbsd.org3a424cd2015-11-08 22:30:20 +0000672 case 'c':
673 get_cert = 1;
674 break;
djm@openbsd.org1a348352018-02-23 05:14:05 +0000675 case 'D':
676 print_sshfp = 1;
677 break;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000678 case 'p':
679 ssh_port = a2port(optarg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100680 if (ssh_port <= 0) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000681 fprintf(stderr, "Bad port '%s'\n", optarg);
682 exit(1);
683 }
684 break;
685 case 'T':
Ben Lindstromedd098b2002-07-04 00:07:13 +0000686 timeout = convtime(optarg);
687 if (timeout == -1 || timeout == 0) {
688 fprintf(stderr, "Bad timeout '%s'\n", optarg);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000689 usage();
Ben Lindstromedd098b2002-07-04 00:07:13 +0000690 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000691 break;
692 case 'v':
693 if (!debug_flag) {
694 debug_flag = 1;
695 log_level = SYSLOG_LEVEL_DEBUG1;
696 }
697 else if (log_level < SYSLOG_LEVEL_DEBUG3)
698 log_level++;
699 else
700 fatal("Too high debugging level.");
701 break;
Kevin Steves76e7d9b2001-09-20 20:30:09 +0000702 case 'f':
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000703 if (strcmp(optarg, "-") == 0)
704 optarg = NULL;
705 argv[fopt_count++] = optarg;
706 break;
707 case 't':
708 get_keytypes = 0;
709 tname = strtok(optarg, ",");
710 while (tname) {
markus@openbsd.org3f797652015-01-19 20:32:39 +0000711 int type = sshkey_type_from_name(tname);
djm@openbsd.orgf89b9282017-01-06 03:41:58 +0000712
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000713 switch (type) {
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000714 case KEY_DSA:
715 get_keytypes |= KT_DSA;
716 break;
Damien Millereb8b60e2010-08-31 22:41:14 +1000717 case KEY_ECDSA:
718 get_keytypes |= KT_ECDSA;
719 break;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000720 case KEY_RSA:
721 get_keytypes |= KT_RSA;
722 break;
Damien Miller5be9d9e2013-12-07 11:24:01 +1100723 case KEY_ED25519:
724 get_keytypes |= KT_ED25519;
725 break;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000726 case KEY_XMSS:
727 get_keytypes |= KT_XMSS;
728 break;
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000729 case KEY_UNSPEC:
djm@openbsd.orgf89b9282017-01-06 03:41:58 +0000730 default:
731 fatal("Unknown key type \"%s\"", tname);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000732 }
733 tname = strtok(NULL, ",");
734 }
735 break;
736 case '4':
737 IPv4or6 = AF_INET;
738 break;
739 case '6':
740 IPv4or6 = AF_INET6;
741 break;
742 case '?':
743 default:
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000744 usage();
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000745 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000746 }
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000747 if (optind == argc && !fopt_count)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000748 usage();
749
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000750 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000751
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000752 maxfd = fdlim_get(1);
753 if (maxfd < 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000754 fatal("%s: fdlim_get: bad value", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000755 if (maxfd > MAXMAXFD)
756 maxfd = MAXMAXFD;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000757 if (MAXCON <= 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000758 fatal("%s: not enough file descriptors", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000759 if (maxfd > fdlim_get(0))
760 fdlim_set(maxfd);
Damien Miller07d86be2006-03-26 14:19:21 +1100761 fdcon = xcalloc(maxfd, sizeof(con));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000762
Damien Miller07d86be2006-03-26 14:19:21 +1100763 read_wait_nfdset = howmany(maxfd, NFDBITS);
764 read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
Ben Lindstromc1e04212001-03-05 07:04:38 +0000765
Damien Miller0e76c5e2010-06-26 09:39:59 +1000766 for (j = 0; j < fopt_count; j++) {
767 if (argv[j] == NULL)
768 fp = stdin;
769 else if ((fp = fopen(argv[j], "r")) == NULL)
770 fatal("%s: %s: %s", __progname, argv[j],
771 strerror(errno));
772 linenum = 0;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000773
Damien Miller0e76c5e2010-06-26 09:39:59 +1000774 while (read_keyfile_line(fp,
775 argv[j] == NULL ? "(stdin)" : argv[j], line, sizeof(line),
776 &linenum) != -1) {
777 /* Chomp off trailing whitespace and comments */
778 if ((cp = strchr(line, '#')) == NULL)
779 cp = line + strlen(line) - 1;
780 while (cp >= line) {
781 if (*cp == ' ' || *cp == '\t' ||
782 *cp == '\n' || *cp == '#')
783 *cp-- = '\0';
784 else
785 break;
786 }
787
788 /* Skip empty lines */
789 if (*line == '\0')
Ben Lindstrom78bbd9e2001-09-12 17:10:40 +0000790 continue;
Damien Miller0e76c5e2010-06-26 09:39:59 +1000791
792 do_host(line);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000793 }
Damien Miller0e76c5e2010-06-26 09:39:59 +1000794
795 if (ferror(fp))
796 fatal("%s: %s: %s", __progname, argv[j],
797 strerror(errno));
798
799 fclose(fp);
Ben Lindstrom325e70c2001-08-06 22:41:30 +0000800 }
801
802 while (optind < argc)
803 do_host(argv[optind++]);
804
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000805 while (ncon > 0)
806 conloop();
807
808 return (0);
809}