OpenCoverage

ssh-keyscan.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/ssh-keyscan.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* $OpenBSD: ssh-keyscan.c,v 1.120 2018/06/06 18:29:18 markus Exp $ */-
2/*-
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-
7 * OpenBSD project by leaving this copyright notice intact.-
8 */-
9-
10#include "includes.h"-
11 -
12#include <sys/types.h>-
13#include "openbsd-compat/sys-queue.h"-
14#include <sys/resource.h>-
15#ifdef HAVE_SYS_TIME_H-
16# include <sys/time.h>-
17#endif-
18-
19#include <netinet/in.h>-
20#include <arpa/inet.h>-
21-
22#include <openssl/bn.h>-
23-
24#include <netdb.h>-
25#include <errno.h>-
26#include <stdarg.h>-
27#include <stdio.h>-
28#include <stdlib.h>-
29#include <signal.h>-
30#include <string.h>-
31#include <unistd.h>-
32-
33#include "xmalloc.h"-
34#include "ssh.h"-
35#include "sshbuf.h"-
36#include "sshkey.h"-
37#include "cipher.h"-
38#include "kex.h"-
39#include "compat.h"-
40#include "myproposal.h"-
41#include "packet.h"-
42#include "dispatch.h"-
43#include "log.h"-
44#include "atomicio.h"-
45#include "misc.h"-
46#include "hostfile.h"-
47#include "ssherr.h"-
48#include "ssh_api.h"-
49#include "dns.h"-
50-
51/* 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. */-
53int IPv4or6 = AF_UNSPEC;-
54-
55int ssh_port = SSH_DEFAULT_PORT;-
56-
57#define KT_DSA (1)-
58#define KT_RSA (1<<1)-
59#define KT_ECDSA (1<<2)-
60#define KT_ED25519 (1<<3)-
61#define KT_XMSS (1<<4)-
62-
63#define KT_MIN KT_DSA-
64#define KT_MAX KT_XMSS-
65-
66int get_cert = 0;-
67int get_keytypes = KT_RSA|KT_ECDSA|KT_ED25519;-
68-
69int hash_hosts = 0; /* Hash hostname on output */-
70-
71int print_sshfp = 0; /* Print SSHFP records instead of known_hosts */-
72-
73#define MAXMAXFD 256-
74-
75/* The number of seconds after which to give up on a TCP connection */-
76int timeout = 5;-
77-
78int maxfd;-
79#define MAXCON (maxfd - 10)-
80-
81extern char *__progname;-
82fd_set *read_wait;-
83size_t read_wait_nfdset;-
84int ncon;-
85-
86struct ssh *active_state = NULL; /* XXX needed for linking */-
87-
88/*-
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 {-
93 u_char c_status; /* State of connection on this file desc. */-
94#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. */-
102 int c_keytype; /* Only one of KT_* */-
103 sig_atomic_t c_done; /* SSH2 done */-
104 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 */-
109 struct ssh *c_ssh; /* SSH-connection */-
110 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-
117static void keyprint(con *c, struct sshkey *key);-
118-
119static int-
120fdlim_get(int hard)-
121{-
122#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)-
123 struct rlimit rlfd;-
124-
125 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
getrlimit( RLI...E , &rlfd) < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
126 return (-1);
never executed: return (-1);
0
127 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
(hard ? rlfd.r...(__rlim_t) -1)Description
TRUEnever evaluated
FALSEnever evaluated
hardDescription
TRUEnever evaluated
FALSEnever evaluated
0
128 return SSH_SYSFDMAX;
never executed: return sysconf( _SC_OPEN_MAX );
0
129 else-
130 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
never executed: return hard ? rlfd.rlim_max : rlfd.rlim_cur;
hardDescription
TRUEnever evaluated
FALSEnever evaluated
0
131#else-
132 return SSH_SYSFDMAX;-
133#endif-
134}-
135-
136static int-
137fdlim_set(int lim)-
138{-
139#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)-
140 struct rlimit rlfd;-
141#endif-
142-
143 if (lim <= 0)
lim <= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
144 return (-1);
never executed: return (-1);
0
145#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)-
146 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
getrlimit( RLI...E , &rlfd) < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
147 return (-1);
never executed: return (-1);
0
148 rlfd.rlim_cur = lim;-
149 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
setrlimit( RLI...E , &rlfd) < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
150 return (-1);
never executed: return (-1);
0
151#elif defined (HAVE_SETDTABLESIZE)-
152 setdtablesize(lim);-
153#endif-
154 return (0);
never executed: return (0);
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 */-
162static char *-
163xstrsep(char **str, const char *delim)-
164{-
165 char *s, *e;-
166-
167 if (!**str)
!**strDescription
TRUEnever evaluated
FALSEnever evaluated
0
168 return (NULL);
never executed: return ( ((void *)0) );
0
169-
170 s = *str;-
171 e = s + strcspn(s, delim);-
172-
173 if (*e != '\0')
*e != '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0
174 *e++ = '\0';
never executed: *e++ = '\0';
0
175 *str = e;-
176-
177 return (s);
never executed: return (s);
0
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 */-
184static char *-
185strnnsep(char **stringp, char *delim)-
186{-
187 char *tok;-
188-
189 do {-
190 tok = xstrsep(stringp, delim);-
191 } while (tok && *tok == '\0');
never executed: end of block
tokDescription
TRUEnever evaluated
FALSEnever evaluated
*tok == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0
192 return (tok);
never executed: return (tok);
0
193}-
194-
195-
196static int-
197key_print_wrapper(struct sshkey *hostkey, struct ssh *ssh)-
198{-
199 con *c;-
200-
201 if ((c = ssh_get_app_data(ssh)) != NULL)
(c = ssh_get_a...!= ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
202 keyprint(c, hostkey);
never executed: keyprint(c, hostkey);
0
203 /* always abort key exchange */-
204 return -1;
never executed: return -1;
0
205}-
206-
207static int-
208ssh2_capable(int remote_major, int remote_minor)-
209{-
210 switch (remote_major) {-
211 case 1:
never executed: case 1:
0
212 if (remote_minor == 99)
remote_minor == 99Description
TRUEnever evaluated
FALSEnever evaluated
0
213 return 1;
never executed: return 1;
0
214 break;
never executed: break;
0
215 case 2:
never executed: case 2:
0
216 return 1;
never executed: return 1;
0
217 default:
never executed: default:
0
218 break;
never executed: break;
0
219 }-
220 return 0;
never executed: return 0;
0
221}-
222-
223static void-
224keygrab_ssh2(con *c)-
225{-
226 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };-
227 int r;-
228-
229 switch (c->c_keytype) {-
230 case KT_DSA:
never executed: case (1):
0
231 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
get_certDescription
TRUEnever evaluated
FALSEnever evaluated
0
232 "ssh-dss-cert-v01@openssh.com" : "ssh-dss";-
233 break;
never executed: break;
0
234 case KT_RSA:
never executed: case (1<<1):
0
235 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
get_certDescription
TRUEnever evaluated
FALSEnever evaluated
0
236 "ssh-rsa-cert-v01@openssh.com" : "ssh-rsa";-
237 break;
never executed: break;
0
238 case KT_ED25519:
never executed: case (1<<3):
0
239 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
get_certDescription
TRUEnever evaluated
FALSEnever evaluated
0
240 "ssh-ed25519-cert-v01@openssh.com" : "ssh-ed25519";-
241 break;
never executed: break;
0
242 case KT_XMSS:
never executed: case (1<<4):
0
243 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
get_certDescription
TRUEnever evaluated
FALSEnever evaluated
0
244 "ssh-xmss-cert-v01@openssh.com" : "ssh-xmss@openssh.com";-
245 break;
never executed: break;
0
246 case KT_ECDSA:
never executed: case (1<<2):
0
247 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
get_certDescription
TRUEnever evaluated
FALSEnever evaluated
0
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;
never executed: break;
0
255 default:
never executed: default:
0
256 fatal("unknown key type %d", c->c_keytype);-
257 break;
never executed: break;
0
258 }-
259 if ((r = kex_setup(c->c_ssh, myproposal)) != 0) {
(r = kex_setup...roposal)) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
260 free(c->c_ssh);-
261 fprintf(stderr, "kex_setup: %s\n", ssh_err(r));-
262 exit(1);
never executed: exit(1);
0
263 }-
264#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;-
267 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;-
270 c->c_ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;-
271 c->c_ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;-
272# ifdef OPENSSL_HAS_ECC-
273 c->c_ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;-
274# endif-
275#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 */-
282 ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done);-
283}
never executed: end of block
0
284-
285static void-
286keyprint_one(const char *host, struct sshkey *key)-
287{-
288 char *hostport;-
289 const char *known_host, *hashed;-
290-
291 if (print_sshfp) {
print_sshfpDescription
TRUEnever evaluated
FALSEnever evaluated
0
292 export_dns_rr(host, key, stdout, 0);-
293 return;
never executed: return;
0
294 }-
295-
296 hostport = put_host_port(host, ssh_port);-
297 lowercase(hostport);-
298 if (hash_hosts && (hashed = host_hash(host, NULL, 0)) == NULL)
hash_hostsDescription
TRUEnever evaluated
FALSEnever evaluated
(hashed = host...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
299 fatal("host_hash failed");
never executed: fatal("host_hash failed");
0
300 known_host = hash_hosts ? hashed : hostport;
hash_hostsDescription
TRUEnever evaluated
FALSEnever evaluated
0
301 if (!get_cert)
!get_certDescription
TRUEnever evaluated
FALSEnever evaluated
0
302 fprintf(stdout, "%s ", known_host);
never executed: fprintf( stdout , "%s ", known_host);
0
303 sshkey_write(key, stdout);-
304 fputs("\n", stdout);-
305 free(hostport);-
306}
never executed: end of block
0
307-
308static void-
309keyprint(con *c, struct sshkey *key)-
310{-
311 char *hosts = c->c_output_name ? c->c_output_name : c->c_name;
c->c_output_nameDescription
TRUEnever evaluated
FALSEnever evaluated
0
312 char *host, *ohosts;-
313-
314 if (key == NULL)
key == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
315 return;
never executed: return;
0
316 if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) {
get_certDescription
TRUEnever evaluated
FALSEnever evaluated
!hash_hostsDescription
TRUEnever evaluated
FALSEnever evaluated
ssh_port == 22Description
TRUEnever evaluated
FALSEnever evaluated
0
317 keyprint_one(hosts, key);-
318 return;
never executed: return;
0
319 }-
320 ohosts = hosts = xstrdup(hosts);-
321 while ((host = strsep(&hosts, ",")) != NULL)
(host = __exte...!= ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
322 keyprint_one(host, key);
never executed: keyprint_one(host, key);
0
323 free(ohosts);-
324}
never executed: end of block
0
325-
326static int-
327tcpconnect(char *host)-
328{-
329 struct addrinfo hints, *ai, *aitop;-
330 char strport[NI_MAXSERV];-
331 int gaierr, s = -1;-
332-
333 snprintf(strport, sizeof strport, "%d", ssh_port);-
334 memset(&hints, 0, sizeof(hints));-
335 hints.ai_family = IPv4or6;-
336 hints.ai_socktype = SOCK_STREAM;-
337 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
(gaierr = geta... &aitop)) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
338 error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));-
339 return -1;
never executed: return -1;
0
340 }-
341 for (ai = aitop; ai; ai = ai->ai_next) {
aiDescription
TRUEnever evaluated
FALSEnever evaluated
0
342 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);-
343 if (s < 0) {
s < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
344 error("socket: %s", strerror(errno));-
345 continue;
never executed: continue;
0
346 }-
347 if (set_nonblock(s) == -1)
set_nonblock(s) == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
348 fatal("%s: set_nonblock(%d)", __func__, s);
never executed: fatal("%s: set_nonblock(%d)", __func__, s);
0
349 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
connect(s, ai-...i_addrlen) < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
350 errno != EINPROGRESS)
(*__errno_location ()) != 115Description
TRUEnever evaluated
FALSEnever evaluated
0
351 error("connect (`%s'): %s", host, strerror(errno));
never executed: error("connect (`%s'): %s", host, strerror( (*__errno_location ()) ));
0
352 else-
353 break;
never executed: break;
0
354 close(s);-
355 s = -1;-
356 }
never executed: end of block
0
357 freeaddrinfo(aitop);-
358 return s;
never executed: return s;
0
359}-
360-
361static int-
362conalloc(char *iname, char *oname, int keytype)-
363{-
364 char *namebase, *name, *namelist;-
365 int s;-
366-
367 namebase = namelist = xstrdup(iname);-
368-
369 do {-
370 name = xstrsep(&namelist, ",");-
371 if (!name) {
!nameDescription
TRUEnever evaluated
FALSEnever evaluated
0
372 free(namebase);-
373 return (-1);
never executed: return (-1);
0
374 }-
375 } while ((s = tcpconnect(name)) < 0);
never executed: end of block
(s = tcpconnect(name)) < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
376-
377 if (s >= maxfd)
s >= maxfdDescription
TRUEnever evaluated
FALSEnever evaluated
0
378 fatal("conalloc: fdno %d too high", s);
never executed: fatal("conalloc: fdno %d too high", s);
0
379 if (fdcon[s].c_status)
fdcon[s].c_statusDescription
TRUEnever evaluated
FALSEnever evaluated
0
380 fatal("conalloc: attempt to reuse fdno %d", s);
never executed: fatal("conalloc: attempt to reuse fdno %d", s);
0
381-
382 debug3("%s: oname %s kt %d", __func__, oname, keytype);-
383 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;-
392 fdcon[s].c_keytype = keytype;-
393 monotime_tv(&fdcon[s].c_tv);-
394 fdcon[s].c_tv.tv_sec += timeout;-
395 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);-
396 FD_SET(s, read_wait);-
397 ncon++;-
398 return (s);
never executed: return (s);
0
399}-
400-
401static void-
402confree(int s)-
403{-
404 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
s >= maxfdDescription
TRUEnever evaluated
FALSEnever evaluated
fdcon[s].c_status == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
405 fatal("confree: attempt to free bad fdno %d", s);
never executed: fatal("confree: attempt to free bad fdno %d", s);
0
406 free(fdcon[s].c_namebase);-
407 free(fdcon[s].c_output_name);-
408 if (fdcon[s].c_status == CS_KEYS)
fdcon[s].c_status == 3Description
TRUEnever evaluated
FALSEnever evaluated
0
409 free(fdcon[s].c_data);
never executed: free(fdcon[s].c_data);
0
410 fdcon[s].c_status = CS_UNUSED;-
411 fdcon[s].c_keytype = 0;-
412 if (fdcon[s].c_ssh) {
fdcon[s].c_sshDescription
TRUEnever evaluated
FALSEnever evaluated
0
413 ssh_packet_close(fdcon[s].c_ssh);-
414 free(fdcon[s].c_ssh);-
415 fdcon[s].c_ssh = NULL;-
416 } else
never executed: end of block
0
417 close(s);
never executed: close(s);
0
418 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
never executed: (&fdcon[s])->c_link.tqe_next->c_link.tqe_prev = (&fdcon[s])->c_link.tqe_prev;
never executed: (&tq)->tqh_last = (&fdcon[s])->c_link.tqe_prev;
((&fdcon[s])->...!= ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
419 FD_CLR(s, read_wait);-
420 ncon--;-
421}
never executed: end of block
0
422-
423static void-
424contouch(int s)-
425{-
426 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
never executed: (&fdcon[s])->c_link.tqe_next->c_link.tqe_prev = (&fdcon[s])->c_link.tqe_prev;
never executed: (&tq)->tqh_last = (&fdcon[s])->c_link.tqe_prev;
((&fdcon[s])->...!= ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
427 monotime_tv(&fdcon[s].c_tv);-
428 fdcon[s].c_tv.tv_sec += timeout;-
429 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);-
430}
never executed: end of block
0
431-
432static int-
433conrecycle(int s)-
434{-
435 con *c = &fdcon[s];-
436 int ret;-
437-
438 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);-
439 confree(s);-
440 return (ret);
never executed: return (ret);
0
441}-
442-
443static void-
444congreet(int s)-
445{-
446 int n = 0, remote_major = 0, remote_minor = 0;-
447 char buf[256], *cp;-
448 char remote_version[sizeof buf];-
449 size_t bufsiz;-
450 con *c = &fdcon[s];-
451-
452 /* 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)) {
n < 0Description
TRUEnever evaluated
FALSEnever evaluated
(size_t)n >= sizeof(buf)Description
TRUEnever evaluated
FALSEnever evaluated
0
456 error("snprintf: buffer too small");-
457 confree(s);-
458 return;
never executed: return;
0
459 }-
460 if (atomicio(vwrite, s, buf, n) != (size_t)n) {
atomicio((ssiz...) != (size_t)nDescription
TRUEnever evaluated
FALSEnever evaluated
0
461 error("write (%s): %s", c->c_name, strerror(errno));-
462 confree(s);-
463 return;
never executed: return;
0
464 }-
465-
466 for (;;) {-
467 memset(buf, '\0', sizeof(buf));-
468 bufsiz = sizeof(buf);-
469 cp = buf;-
470 while (bufsiz-- &&
bufsiz--Description
TRUEnever evaluated
FALSEnever evaluated
0
471 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
(n = atomicio(..., cp, 1)) == 1Description
TRUEnever evaluated
FALSEnever evaluated
*cp != '\n'Description
TRUEnever evaluated
FALSEnever evaluated
0
472 if (*cp == '\r')
*cp == '\r'Description
TRUEnever evaluated
FALSEnever evaluated
0
473 *cp = '\n';
never executed: *cp = '\n';
0
474 cp++;-
475 }
never executed: end of block
0
476 if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
never executed: __result = (((const unsigned char *) (const char *) ( buf ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( "SSH-" ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
n != 1Description
TRUEnever evaluated
FALSEnever evaluated
(__extension__..." , 4 ))) == 0Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( 4 )Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( buf )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( buf )...size_t) ( 4 ))Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...t_p ( "SSH-" )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( "SSH-...size_t) ( 4 ))Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
477 break;
never executed: break;
0
478 }
never executed: end of block
0
479 if (n == 0) {
n == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
480 switch (errno) {-
481 case EPIPE:
never executed: case 32 :
0
482 error("%s: Connection closed by remote host", c->c_name);-
483 break;
never executed: break;
0
484 case ECONNREFUSED:
never executed: case 111 :
0
485 break;
never executed: break;
0
486 default:
never executed: default:
0
487 error("read (%s): %s", c->c_name, strerror(errno));-
488 break;
never executed: break;
0
489 }-
490 conrecycle(s);-
491 return;
never executed: return;
0
492 }-
493 if (*cp != '\n' && *cp != '\r') {
*cp != '\n'Description
TRUEnever evaluated
FALSEnever evaluated
*cp != '\r'Description
TRUEnever evaluated
FALSEnever evaluated
0
494 error("%s: bad greeting", c->c_name);-
495 confree(s);-
496 return;
never executed: return;
0
497 }-
498 *cp = '\0';-
499 if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
(c->c_ssh = ss...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
500 fatal("ssh_packet_set_connection failed");
never executed: fatal("ssh_packet_set_connection failed");
0
501 ssh_packet_set_timeout(c->c_ssh, timeout, 1);-
502 ssh_set_app_data(c->c_ssh, c); /* back link */-
503 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
sscanf(buf, "S..._version) == 3Description
TRUEnever evaluated
FALSEnever evaluated
0
504 &remote_major, &remote_minor, remote_version) == 3)
sscanf(buf, "S..._version) == 3Description
TRUEnever evaluated
FALSEnever evaluated
0
505 c->c_ssh->compat = compat_datafellows(remote_version);
never executed: c->c_ssh->compat = compat_datafellows(remote_version);
0
506 else-
507 c->c_ssh->compat = 0;
never executed: c->c_ssh->compat = 0;
0
508 if (!ssh2_capable(remote_major, remote_minor)) {
!ssh2_capable(... remote_minor)Description
TRUEnever evaluated
FALSEnever evaluated
0
509 debug("%s doesn't support ssh2", c->c_name);-
510 confree(s);-
511 return;
never executed: return;
0
512 }-
513 fprintf(stderr, "%c %s:%d %s\n", print_sshfp ? ';' : '#',-
514 c->c_name, ssh_port, chop(buf));-
515 keygrab_ssh2(c);-
516 confree(s);-
517}
never executed: end of block
0
518-
519static void-
520conread(int s)-
521{-
522 con *c = &fdcon[s];-
523 size_t n;-
524-
525 if (c->c_status == CS_CON) {
c->c_status == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
526 congreet(s);-
527 return;
never executed: return;
0
528 }-
529 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);-
530 if (n == 0) {
n == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
531 error("read (%s): %s", c->c_name, strerror(errno));-
532 confree(s);-
533 return;
never executed: return;
0
534 }-
535 c->c_off += n;-
536-
537 if (c->c_off == c->c_len)
c->c_off == c->c_lenDescription
TRUEnever evaluated
FALSEnever evaluated
0
538 switch (c->c_status) {-
539 case CS_SIZE:
never executed: case 2:
0
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;
never executed: break;
0
546 default:
never executed: default:
0
547 fatal("conread: invalid status %d", c->c_status);-
548 break;
never executed: break;
0
549 }-
550-
551 contouch(s);-
552}
never executed: end of block
0
553-
554static void-
555conloop(void)-
556{-
557 struct timeval seltime, now;-
558 fd_set *r, *e;-
559 con *c;-
560 int i;-
561-
562 monotime_tv(&now);-
563 c = TAILQ_FIRST(&tq);-
564-
565 if (c && (c->c_tv.tv_sec > now.tv_sec ||
cDescription
TRUEnever evaluated
FALSEnever evaluated
c->c_tv.tv_sec > now.tv_secDescription
TRUEnever evaluated
FALSEnever evaluated
0
566 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
c->c_tv.tv_sec == now.tv_secDescription
TRUEnever evaluated
FALSEnever evaluated
c->c_tv.tv_usec > now.tv_usecDescription
TRUEnever evaluated
FALSEnever evaluated
0
567 seltime = c->c_tv;-
568 seltime.tv_sec -= now.tv_sec;-
569 seltime.tv_usec -= now.tv_usec;-
570 if (seltime.tv_usec < 0) {
seltime.tv_usec < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
571 seltime.tv_usec += 1000000;-
572 seltime.tv_sec--;-
573 }
never executed: end of block
0
574 } else
never executed: end of block
0
575 timerclear(&seltime);
never executed: (( &seltime )->tv_sec = ( &seltime )->tv_usec = 0) ;
0
576-
577 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));-
581-
582 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
select(maxfd, ...seltime) == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
583 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
(*__errno_location ()) == 11Description
TRUEnever evaluated
FALSEnever evaluated
(*__errno_location ()) == 4Description
TRUEnever evaluated
FALSEnever evaluated
(*__errno_location ()) == 11Description
TRUEnever evaluated
FALSEnever evaluated
0
584 ;
never executed: ;
0
585-
586 for (i = 0; i < maxfd; i++) {
i < maxfdDescription
TRUEnever evaluated
FALSEnever evaluated
0
587 if (FD_ISSET(i, e)) {
kludge_FD_ISSET(i, e)Description
TRUEnever evaluated
FALSEnever evaluated
0
588 error("%s: exception!", fdcon[i].c_name);-
589 confree(i);-
590 } else if (FD_ISSET(i, r))
never executed: end of block
kludge_FD_ISSET(i, r)Description
TRUEnever evaluated
FALSEnever evaluated
0
591 conread(i);
never executed: conread(i);
0
592 }
never executed: end of block
0
593 free(r);-
594 free(e);-
595-
596 c = TAILQ_FIRST(&tq);-
597 while (c && (c->c_tv.tv_sec < now.tv_sec ||
cDescription
TRUEnever evaluated
FALSEnever evaluated
c->c_tv.tv_sec < now.tv_secDescription
TRUEnever evaluated
FALSEnever evaluated
0
598 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
c->c_tv.tv_sec == now.tv_secDescription
TRUEnever evaluated
FALSEnever evaluated
c->c_tv.tv_usec < now.tv_usecDescription
TRUEnever evaluated
FALSEnever evaluated
0
599 int s = c->c_fd;-
600-
601 c = TAILQ_NEXT(c, c_link);-
602 conrecycle(s);-
603 }
never executed: end of block
0
604}
never executed: end of block
0
605-
606static void-
607do_host(char *host)-
608{-
609 char *name = strnnsep(&host, " \t\n");-
610 int j;-
611-
612 if (name == NULL)
name == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
613 return;
never executed: return;
0
614 for (j = KT_MIN; j <= KT_MAX; j *= 2) {
j <= (1<<4)Description
TRUEnever evaluated
FALSEnever evaluated
0
615 if (get_keytypes & j) {
get_keytypes & jDescription
TRUEnever evaluated
FALSEnever evaluated
0
616 while (ncon >= MAXCON)
ncon >= (maxfd - 10)Description
TRUEnever evaluated
FALSEnever evaluated
0
617 conloop();
never executed: conloop();
0
618 conalloc(name, *host ? host : name, j);-
619 }
never executed: end of block
0
620 }
never executed: end of block
0
621}
never executed: end of block
0
622-
623void-
624fatal(const char *fmt,...)-
625{-
626 va_list args;-
627-
628 va_start(args, fmt);-
629 do_log(SYSLOG_LEVEL_FATAL, fmt, args);-
630 va_end(args);-
631 exit(255);
never executed: exit(255);
0
632}-
633-
634static void-
635usage(void)-
636{-
637 fprintf(stderr,-
638 "usage: %s [-46cDHv] [-f file] [-p port] [-T timeout] [-t type]\n"-
639 "\t\t [host | addrlist namelist]\n",-
640 __progname);-
641 exit(1);
never executed: exit(1);
0
642}-
643-
644int-
645main(int argc, char **argv)-
646{-
647 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;-
648 int opt, fopt_count = 0, j;-
649 char *tname, *cp, *line = NULL;-
650 size_t linesize = 0;-
651 FILE *fp;-
652-
653 extern int optind;-
654 extern char *optarg;-
655-
656 ssh_malloc_init(); /* must be called before any mallocs */-
657 __progname = ssh_get_progname(argv[0]);-
658 seed_rng();-
659 TAILQ_INIT(&tq);-
660-
661 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */-
662 sanitise_stdfd();-
663-
664 if (argc <= 1)
argc <= 1Description
TRUEnever evaluated
FALSEnever evaluated
0
665 usage();
never executed: usage();
0
666-
667 while ((opt = getopt(argc, argv, "cDHv46p:T:t:f:")) != -1) {
(opt = BSDgeto...:t:f:")) != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
668 switch (opt) {-
669 case 'H':
never executed: case 'H':
0
670 hash_hosts = 1;-
671 break;
never executed: break;
0
672 case 'c':
never executed: case 'c':
0
673 get_cert = 1;-
674 break;
never executed: break;
0
675 case 'D':
never executed: case 'D':
0
676 print_sshfp = 1;-
677 break;
never executed: break;
0
678 case 'p':
never executed: case 'p':
0
679 ssh_port = a2port(optarg);-
680 if (ssh_port <= 0) {
ssh_port <= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
681 fprintf(stderr, "Bad port '%s'\n", optarg);-
682 exit(1);
never executed: exit(1);
0
683 }-
684 break;
never executed: break;
0
685 case 'T':
never executed: case 'T':
0
686 timeout = convtime(optarg);-
687 if (timeout == -1 || timeout == 0) {
timeout == -1Description
TRUEnever evaluated
FALSEnever evaluated
timeout == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
688 fprintf(stderr, "Bad timeout '%s'\n", optarg);-
689 usage();-
690 }
never executed: end of block
0
691 break;
never executed: break;
0
692 case 'v':
never executed: case 'v':
0
693 if (!debug_flag) {
!debug_flagDescription
TRUEnever evaluated
FALSEnever evaluated
0
694 debug_flag = 1;-
695 log_level = SYSLOG_LEVEL_DEBUG1;-
696 }
never executed: end of block
0
697 else if (log_level < SYSLOG_LEVEL_DEBUG3)
log_level < SY...G_LEVEL_DEBUG3Description
TRUEnever evaluated
FALSEnever evaluated
0
698 log_level++;
never executed: log_level++;
0
699 else-
700 fatal("Too high debugging level.");
never executed: fatal("Too high debugging level.");
0
701 break;
never executed: break;
0
702 case 'f':
never executed: case 'f':
0
703 if (strcmp(optarg, "-") == 0)
never executed: __result = (((const unsigned char *) (const char *) ( BSDoptarg ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( "-" ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
__extension__ ... )))); }) == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
704 optarg = NULL;
never executed: BSDoptarg = ((void *)0) ;
0
705 argv[fopt_count++] = optarg;-
706 break;
never executed: break;
0
707 case 't':
never executed: case 't':
0
708 get_keytypes = 0;-
709 tname = strtok(optarg, ",");-
710 while (tname) {
tnameDescription
TRUEnever evaluated
FALSEnever evaluated
0
711 int type = sshkey_type_from_name(tname);-
712-
713 switch (type) {-
714 case KEY_DSA:
never executed: case KEY_DSA:
0
715 get_keytypes |= KT_DSA;-
716 break;
never executed: break;
0
717 case KEY_ECDSA:
never executed: case KEY_ECDSA:
0
718 get_keytypes |= KT_ECDSA;-
719 break;
never executed: break;
0
720 case KEY_RSA:
never executed: case KEY_RSA:
0
721 get_keytypes |= KT_RSA;-
722 break;
never executed: break;
0
723 case KEY_ED25519:
never executed: case KEY_ED25519:
0
724 get_keytypes |= KT_ED25519;-
725 break;
never executed: break;
0
726 case KEY_XMSS:
never executed: case KEY_XMSS:
0
727 get_keytypes |= KT_XMSS;-
728 break;
never executed: break;
0
729 case KEY_UNSPEC:
never executed: case KEY_UNSPEC:
0
730 default:
never executed: default:
0
731 fatal("Unknown key type \"%s\"", tname);-
732 }
never executed: end of block
0
733 tname = strtok(NULL, ",");-
734 }
never executed: end of block
0
735 break;
never executed: break;
0
736 case '4':
never executed: case '4':
0
737 IPv4or6 = AF_INET;-
738 break;
never executed: break;
0
739 case '6':
never executed: case '6':
0
740 IPv4or6 = AF_INET6;-
741 break;
never executed: break;
0
742 case '?':
never executed: case '?':
0
743 default:
never executed: default:
0
744 usage();-
745 }
never executed: end of block
0
746 }-
747 if (optind == argc && !fopt_count)
BSDoptind == argcDescription
TRUEnever evaluated
FALSEnever evaluated
!fopt_countDescription
TRUEnever evaluated
FALSEnever evaluated
0
748 usage();
never executed: usage();
0
749-
750 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);-
751-
752 maxfd = fdlim_get(1);-
753 if (maxfd < 0)
maxfd < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
754 fatal("%s: fdlim_get: bad value", __progname);
never executed: fatal("%s: fdlim_get: bad value", __progname);
0
755 if (maxfd > MAXMAXFD)
maxfd > 256Description
TRUEnever evaluated
FALSEnever evaluated
0
756 maxfd = MAXMAXFD;
never executed: maxfd = 256;
0
757 if (MAXCON <= 0)
(maxfd - 10) <= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
758 fatal("%s: not enough file descriptors", __progname);
never executed: fatal("%s: not enough file descriptors", __progname);
0
759 if (maxfd > fdlim_get(0))
maxfd > fdlim_get(0)Description
TRUEnever evaluated
FALSEnever evaluated
0
760 fdlim_set(maxfd);
never executed: fdlim_set(maxfd);
0
761 fdcon = xcalloc(maxfd, sizeof(con));-
762-
763 read_wait_nfdset = howmany(maxfd, NFDBITS);-
764 read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));-
765-
766 for (j = 0; j < fopt_count; j++) {
j < fopt_countDescription
TRUEnever evaluated
FALSEnever evaluated
0
767 if (argv[j] == NULL)
argv[j] == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
768 fp = stdin;
never executed: fp = stdin ;
0
769 else if ((fp = fopen(argv[j], "r")) == NULL)
(fp = fopen(ar...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
770 fatal("%s: %s: %s", __progname, argv[j],
never executed: fatal("%s: %s: %s", __progname, argv[j], strerror( (*__errno_location ()) ));
0
771 strerror(errno));
never executed: fatal("%s: %s: %s", __progname, argv[j], strerror( (*__errno_location ()) ));
0
772-
773 while (getline(&line, &linesize, fp) != -1) {
getline(&line,...ize, fp) != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
774 /* Chomp off trailing whitespace and comments */-
775 if ((cp = strchr(line, '#')) == NULL)
(cp = (__exten...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( '#' )Description
TRUEnever evaluated
FALSEnever evaluated
!__builtin_constant_p ( line )Description
TRUEnever evaluated
FALSEnever evaluated
( '#' ) == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0
776 cp = line + strlen(line) - 1;
never executed: cp = line + strlen(line) - 1;
0
777 while (cp >= line) {
cp >= lineDescription
TRUEnever evaluated
FALSEnever evaluated
0
778 if (*cp == ' ' || *cp == '\t' ||
*cp == ' 'Description
TRUEnever evaluated
FALSEnever evaluated
*cp == '\t'Description
TRUEnever evaluated
FALSEnever evaluated
0
779 *cp == '\n' || *cp == '#')
*cp == '\n'Description
TRUEnever evaluated
FALSEnever evaluated
*cp == '#'Description
TRUEnever evaluated
FALSEnever evaluated
0
780 *cp-- = '\0';
never executed: *cp-- = '\0';
0
781 else-
782 break;
never executed: break;
0
783 }-
784-
785 /* Skip empty lines */-
786 if (*line == '\0')
*line == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0
787 continue;
never executed: continue;
0
788-
789 do_host(line);-
790 }
never executed: end of block
0
791-
792 if (ferror(fp))
ferror(fp)Description
TRUEnever evaluated
FALSEnever evaluated
0
793 fatal("%s: %s: %s", __progname, argv[j],
never executed: fatal("%s: %s: %s", __progname, argv[j], strerror( (*__errno_location ()) ));
0
794 strerror(errno));
never executed: fatal("%s: %s: %s", __progname, argv[j], strerror( (*__errno_location ()) ));
0
795-
796 fclose(fp);-
797 }
never executed: end of block
0
798 free(line);-
799-
800 while (optind < argc)
BSDoptind < argcDescription
TRUEnever evaluated
FALSEnever evaluated
0
801 do_host(argv[optind++]);
never executed: do_host(argv[BSDoptind++]);
0
802-
803 while (ncon > 0)
ncon > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
804 conloop();
never executed: conloop();
0
805-
806 return (0);
never executed: return (0);
0
807}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2