]> andersk Git - openssh.git/blob - ssh-keyscan.c
- stevesk@cvs.openbsd.org 2006/07/22 19:08:54
[openssh.git] / ssh-keyscan.c
1 /* $OpenBSD: ssh-keyscan.c,v 1.68 2006/07/17 01:31:10 stevesk 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 "openbsd-compat/sys-queue.h"
13 #include <sys/resource.h>
14 #if defined(HAVE_NETDB_H)
15 # include <netdb.h>
16 #endif
17 #include <errno.h>
18 #include <stdarg.h>
19 #include <setjmp.h>
20 #include <unistd.h>
21
22 #include <openssl/bn.h>
23
24 #include "xmalloc.h"
25 #include "ssh.h"
26 #include "ssh1.h"
27 #include "key.h"
28 #include "kex.h"
29 #include "compat.h"
30 #include "myproposal.h"
31 #include "packet.h"
32 #include "dispatch.h"
33 #include "buffer.h"
34 #include "bufaux.h"
35 #include "log.h"
36 #include "atomicio.h"
37 #include "misc.h"
38 #include "hostfile.h"
39
40 /* Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
41    Default value is AF_UNSPEC means both IPv4 and IPv6. */
42 int IPv4or6 = AF_UNSPEC;
43
44 int ssh_port = SSH_DEFAULT_PORT;
45
46 #define KT_RSA1 1
47 #define KT_DSA  2
48 #define KT_RSA  4
49
50 int get_keytypes = KT_RSA1;     /* Get only RSA1 keys by default */
51
52 int hash_hosts = 0;             /* Hash hostname on output */
53
54 #define MAXMAXFD 256
55
56 /* The number of seconds after which to give up on a TCP connection */
57 int timeout = 5;
58
59 int maxfd;
60 #define MAXCON (maxfd - 10)
61
62 extern char *__progname;
63 fd_set *read_wait;
64 size_t read_wait_nfdset;
65 int ncon;
66 int nonfatal_fatal = 0;
67 jmp_buf kexjmp;
68 Key *kexjmp_key;
69
70 /*
71  * Keep a connection structure for each file descriptor.  The state
72  * associated with file descriptor n is held in fdcon[n].
73  */
74 typedef struct Connection {
75         u_char c_status;        /* State of connection on this file desc. */
76 #define CS_UNUSED 0             /* File descriptor unused */
77 #define CS_CON 1                /* Waiting to connect/read greeting */
78 #define CS_SIZE 2               /* Waiting to read initial packet size */
79 #define CS_KEYS 3               /* Waiting to read public key packet */
80         int c_fd;               /* Quick lookup: c->c_fd == c - fdcon */
81         int c_plen;             /* Packet length field for ssh packet */
82         int c_len;              /* Total bytes which must be read. */
83         int c_off;              /* Length of data read so far. */
84         int c_keytype;          /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
85         char *c_namebase;       /* Address to free for c_name and c_namelist */
86         char *c_name;           /* Hostname of connection for errors */
87         char *c_namelist;       /* Pointer to other possible addresses */
88         char *c_output_name;    /* Hostname of connection for output */
89         char *c_data;           /* Data read from this fd */
90         Kex *c_kex;             /* The key-exchange struct for ssh2 */
91         struct timeval c_tv;    /* Time at which connection gets aborted */
92         TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
93 } con;
94
95 TAILQ_HEAD(conlist, Connection) tq;     /* Timeout Queue */
96 con *fdcon;
97
98 /*
99  *  This is just a wrapper around fgets() to make it usable.
100  */
101
102 /* Stress-test.  Increase this later. */
103 #define LINEBUF_SIZE 16
104
105 typedef struct {
106         char *buf;
107         u_int size;
108         int lineno;
109         const char *filename;
110         FILE *stream;
111         void (*errfun) (const char *,...);
112 } Linebuf;
113
114 static Linebuf *
115 Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
116 {
117         Linebuf *lb;
118
119         if (!(lb = malloc(sizeof(*lb)))) {
120                 if (errfun)
121                         (*errfun) ("linebuf (%s): malloc failed\n",
122                             filename ? filename : "(stdin)");
123                 return (NULL);
124         }
125         if (filename) {
126                 lb->filename = filename;
127                 if (!(lb->stream = fopen(filename, "r"))) {
128                         xfree(lb);
129                         if (errfun)
130                                 (*errfun) ("%s: %s\n", filename, strerror(errno));
131                         return (NULL);
132                 }
133         } else {
134                 lb->filename = "(stdin)";
135                 lb->stream = stdin;
136         }
137
138         if (!(lb->buf = malloc((lb->size = LINEBUF_SIZE)))) {
139                 if (errfun)
140                         (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
141                 xfree(lb);
142                 return (NULL);
143         }
144         lb->errfun = errfun;
145         lb->lineno = 0;
146         return (lb);
147 }
148
149 static void
150 Linebuf_free(Linebuf * lb)
151 {
152         fclose(lb->stream);
153         xfree(lb->buf);
154         xfree(lb);
155 }
156
157 #if 0
158 static void
159 Linebuf_restart(Linebuf * lb)
160 {
161         clearerr(lb->stream);
162         rewind(lb->stream);
163         lb->lineno = 0;
164 }
165
166 static int
167 Linebuf_lineno(Linebuf * lb)
168 {
169         return (lb->lineno);
170 }
171 #endif
172
173 static char *
174 Linebuf_getline(Linebuf * lb)
175 {
176         size_t n = 0;
177         void *p;
178
179         lb->lineno++;
180         for (;;) {
181                 /* Read a line */
182                 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
183                         if (ferror(lb->stream) && lb->errfun)
184                                 (*lb->errfun)("%s: %s\n", lb->filename,
185                                     strerror(errno));
186                         return (NULL);
187                 }
188                 n = strlen(lb->buf);
189
190                 /* Return it or an error if it fits */
191                 if (n > 0 && lb->buf[n - 1] == '\n') {
192                         lb->buf[n - 1] = '\0';
193                         return (lb->buf);
194                 }
195                 if (n != lb->size - 1) {
196                         if (lb->errfun)
197                                 (*lb->errfun)("%s: skipping incomplete last line\n",
198                                     lb->filename);
199                         return (NULL);
200                 }
201                 /* Double the buffer if we need more space */
202                 lb->size *= 2;
203                 if ((p = realloc(lb->buf, lb->size)) == NULL) {
204                         lb->size /= 2;
205                         if (lb->errfun)
206                                 (*lb->errfun)("linebuf (%s): realloc failed\n",
207                                     lb->filename);
208                         return (NULL);
209                 }
210                 lb->buf = p;
211         }
212 }
213
214 static int
215 fdlim_get(int hard)
216 {
217 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
218         struct rlimit rlfd;
219
220         if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
221                 return (-1);
222         if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
223                 return SSH_SYSFDMAX;
224         else
225                 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
226 #else
227         return SSH_SYSFDMAX;
228 #endif
229 }
230
231 static int
232 fdlim_set(int lim)
233 {
234 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
235         struct rlimit rlfd;
236 #endif
237
238         if (lim <= 0)
239                 return (-1);
240 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
241         if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
242                 return (-1);
243         rlfd.rlim_cur = lim;
244         if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
245                 return (-1);
246 #elif defined (HAVE_SETDTABLESIZE)
247         setdtablesize(lim);
248 #endif
249         return (0);
250 }
251
252 /*
253  * This is an strsep function that returns a null field for adjacent
254  * separators.  This is the same as the 4.4BSD strsep, but different from the
255  * one in the GNU libc.
256  */
257 static char *
258 xstrsep(char **str, const char *delim)
259 {
260         char *s, *e;
261
262         if (!**str)
263                 return (NULL);
264
265         s = *str;
266         e = s + strcspn(s, delim);
267
268         if (*e != '\0')
269                 *e++ = '\0';
270         *str = e;
271
272         return (s);
273 }
274
275 /*
276  * Get the next non-null token (like GNU strsep).  Strsep() will return a
277  * null token for two adjacent separators, so we may have to loop.
278  */
279 static char *
280 strnnsep(char **stringp, char *delim)
281 {
282         char *tok;
283
284         do {
285                 tok = xstrsep(stringp, delim);
286         } while (tok && *tok == '\0');
287         return (tok);
288 }
289
290 static Key *
291 keygrab_ssh1(con *c)
292 {
293         static Key *rsa;
294         static Buffer msg;
295
296         if (rsa == NULL) {
297                 buffer_init(&msg);
298                 rsa = key_new(KEY_RSA1);
299         }
300         buffer_append(&msg, c->c_data, c->c_plen);
301         buffer_consume(&msg, 8 - (c->c_plen & 7));      /* padding */
302         if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
303                 error("%s: invalid packet type", c->c_name);
304                 buffer_clear(&msg);
305                 return NULL;
306         }
307         buffer_consume(&msg, 8);                /* cookie */
308
309         /* server key */
310         (void) buffer_get_int(&msg);
311         buffer_get_bignum(&msg, rsa->rsa->e);
312         buffer_get_bignum(&msg, rsa->rsa->n);
313
314         /* host key */
315         (void) buffer_get_int(&msg);
316         buffer_get_bignum(&msg, rsa->rsa->e);
317         buffer_get_bignum(&msg, rsa->rsa->n);
318
319         buffer_clear(&msg);
320
321         return (rsa);
322 }
323
324 static int
325 hostjump(Key *hostkey)
326 {
327         kexjmp_key = hostkey;
328         longjmp(kexjmp, 1);
329 }
330
331 static int
332 ssh2_capable(int remote_major, int remote_minor)
333 {
334         switch (remote_major) {
335         case 1:
336                 if (remote_minor == 99)
337                         return 1;
338                 break;
339         case 2:
340                 return 1;
341         default:
342                 break;
343         }
344         return 0;
345 }
346
347 static Key *
348 keygrab_ssh2(con *c)
349 {
350         int j;
351
352         packet_set_connection(c->c_fd, c->c_fd);
353         enable_compat20();
354         myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
355             "ssh-dss": "ssh-rsa";
356         c->c_kex = kex_setup(myproposal);
357         c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
358         c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
359         c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
360         c->c_kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
361         c->c_kex->verify_host_key = hostjump;
362
363         if (!(j = setjmp(kexjmp))) {
364                 nonfatal_fatal = 1;
365                 dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
366                 fprintf(stderr, "Impossible! dispatch_run() returned!\n");
367                 exit(1);
368         }
369         nonfatal_fatal = 0;
370         xfree(c->c_kex);
371         c->c_kex = NULL;
372         packet_close();
373
374         return j < 0? NULL : kexjmp_key;
375 }
376
377 static void
378 keyprint(con *c, Key *key)
379 {
380         char *host = c->c_output_name ? c->c_output_name : c->c_name;
381
382         if (!key)
383                 return;
384         if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL)
385                 fatal("host_hash failed");
386
387         fprintf(stdout, "%s ", host);
388         key_write(key, stdout);
389         fputs("\n", stdout);
390 }
391
392 static int
393 tcpconnect(char *host)
394 {
395         struct addrinfo hints, *ai, *aitop;
396         char strport[NI_MAXSERV];
397         int gaierr, s = -1;
398
399         snprintf(strport, sizeof strport, "%d", ssh_port);
400         memset(&hints, 0, sizeof(hints));
401         hints.ai_family = IPv4or6;
402         hints.ai_socktype = SOCK_STREAM;
403         if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
404                 fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
405         for (ai = aitop; ai; ai = ai->ai_next) {
406                 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
407                 if (s < 0) {
408                         error("socket: %s", strerror(errno));
409                         continue;
410                 }
411                 if (set_nonblock(s) == -1)
412                         fatal("%s: set_nonblock(%d)", __func__, s);
413                 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
414                     errno != EINPROGRESS)
415                         error("connect (`%s'): %s", host, strerror(errno));
416                 else
417                         break;
418                 close(s);
419                 s = -1;
420         }
421         freeaddrinfo(aitop);
422         return s;
423 }
424
425 static int
426 conalloc(char *iname, char *oname, int keytype)
427 {
428         char *namebase, *name, *namelist;
429         int s;
430
431         namebase = namelist = xstrdup(iname);
432
433         do {
434                 name = xstrsep(&namelist, ",");
435                 if (!name) {
436                         xfree(namebase);
437                         return (-1);
438                 }
439         } while ((s = tcpconnect(name)) < 0);
440
441         if (s >= maxfd)
442                 fatal("conalloc: fdno %d too high", s);
443         if (fdcon[s].c_status)
444                 fatal("conalloc: attempt to reuse fdno %d", s);
445
446         fdcon[s].c_fd = s;
447         fdcon[s].c_status = CS_CON;
448         fdcon[s].c_namebase = namebase;
449         fdcon[s].c_name = name;
450         fdcon[s].c_namelist = namelist;
451         fdcon[s].c_output_name = xstrdup(oname);
452         fdcon[s].c_data = (char *) &fdcon[s].c_plen;
453         fdcon[s].c_len = 4;
454         fdcon[s].c_off = 0;
455         fdcon[s].c_keytype = keytype;
456         gettimeofday(&fdcon[s].c_tv, NULL);
457         fdcon[s].c_tv.tv_sec += timeout;
458         TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
459         FD_SET(s, read_wait);
460         ncon++;
461         return (s);
462 }
463
464 static void
465 confree(int s)
466 {
467         if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
468                 fatal("confree: attempt to free bad fdno %d", s);
469         close(s);
470         xfree(fdcon[s].c_namebase);
471         xfree(fdcon[s].c_output_name);
472         if (fdcon[s].c_status == CS_KEYS)
473                 xfree(fdcon[s].c_data);
474         fdcon[s].c_status = CS_UNUSED;
475         fdcon[s].c_keytype = 0;
476         TAILQ_REMOVE(&tq, &fdcon[s], c_link);
477         FD_CLR(s, read_wait);
478         ncon--;
479 }
480
481 static void
482 contouch(int s)
483 {
484         TAILQ_REMOVE(&tq, &fdcon[s], c_link);
485         gettimeofday(&fdcon[s].c_tv, NULL);
486         fdcon[s].c_tv.tv_sec += timeout;
487         TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
488 }
489
490 static int
491 conrecycle(int s)
492 {
493         con *c = &fdcon[s];
494         int ret;
495
496         ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
497         confree(s);
498         return (ret);
499 }
500
501 static void
502 congreet(int s)
503 {
504         int n = 0, remote_major = 0, remote_minor = 0;
505         char buf[256], *cp;
506         char remote_version[sizeof buf];
507         size_t bufsiz;
508         con *c = &fdcon[s];
509
510         for (;;) {
511                 memset(buf, '\0', sizeof(buf));
512                 bufsiz = sizeof(buf);
513                 cp = buf;
514                 while (bufsiz-- &&
515                     (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
516                         if (*cp == '\r')
517                                 *cp = '\n';
518                         cp++;
519                 }
520                 if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
521                         break;
522         }
523         if (n == 0) {
524                 switch (errno) {
525                 case EPIPE:
526                         error("%s: Connection closed by remote host", c->c_name);
527                         break;
528                 case ECONNREFUSED:
529                         break;
530                 default:
531                         error("read (%s): %s", c->c_name, strerror(errno));
532                         break;
533                 }
534                 conrecycle(s);
535                 return;
536         }
537         if (*cp != '\n' && *cp != '\r') {
538                 error("%s: bad greeting", c->c_name);
539                 confree(s);
540                 return;
541         }
542         *cp = '\0';
543         if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
544             &remote_major, &remote_minor, remote_version) == 3)
545                 compat_datafellows(remote_version);
546         else
547                 datafellows = 0;
548         if (c->c_keytype != KT_RSA1) {
549                 if (!ssh2_capable(remote_major, remote_minor)) {
550                         debug("%s doesn't support ssh2", c->c_name);
551                         confree(s);
552                         return;
553                 }
554         } else if (remote_major != 1) {
555                 debug("%s doesn't support ssh1", c->c_name);
556                 confree(s);
557                 return;
558         }
559         fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
560         n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
561             c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
562             c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
563         if (n < 0 || (size_t)n >= sizeof(buf)) {
564                 error("snprintf: buffer too small");
565                 confree(s);
566                 return;
567         }
568         if (atomicio(vwrite, s, buf, n) != (size_t)n) {
569                 error("write (%s): %s", c->c_name, strerror(errno));
570                 confree(s);
571                 return;
572         }
573         if (c->c_keytype != KT_RSA1) {
574                 keyprint(c, keygrab_ssh2(c));
575                 confree(s);
576                 return;
577         }
578         c->c_status = CS_SIZE;
579         contouch(s);
580 }
581
582 static void
583 conread(int s)
584 {
585         con *c = &fdcon[s];
586         size_t n;
587
588         if (c->c_status == CS_CON) {
589                 congreet(s);
590                 return;
591         }
592         n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
593         if (n == 0) {
594                 error("read (%s): %s", c->c_name, strerror(errno));
595                 confree(s);
596                 return;
597         }
598         c->c_off += n;
599
600         if (c->c_off == c->c_len)
601                 switch (c->c_status) {
602                 case CS_SIZE:
603                         c->c_plen = htonl(c->c_plen);
604                         c->c_len = c->c_plen + 8 - (c->c_plen & 7);
605                         c->c_off = 0;
606                         c->c_data = xmalloc(c->c_len);
607                         c->c_status = CS_KEYS;
608                         break;
609                 case CS_KEYS:
610                         keyprint(c, keygrab_ssh1(c));
611                         confree(s);
612                         return;
613                 default:
614                         fatal("conread: invalid status %d", c->c_status);
615                         break;
616                 }
617
618         contouch(s);
619 }
620
621 static void
622 conloop(void)
623 {
624         struct timeval seltime, now;
625         fd_set *r, *e;
626         con *c;
627         int i;
628
629         gettimeofday(&now, NULL);
630         c = TAILQ_FIRST(&tq);
631
632         if (c && (c->c_tv.tv_sec > now.tv_sec ||
633             (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
634                 seltime = c->c_tv;
635                 seltime.tv_sec -= now.tv_sec;
636                 seltime.tv_usec -= now.tv_usec;
637                 if (seltime.tv_usec < 0) {
638                         seltime.tv_usec += 1000000;
639                         seltime.tv_sec--;
640                 }
641         } else
642                 seltime.tv_sec = seltime.tv_usec = 0;
643
644         r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
645         e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
646         memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
647         memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
648
649         while (select(maxfd, r, NULL, e, &seltime) == -1 &&
650             (errno == EAGAIN || errno == EINTR))
651                 ;
652
653         for (i = 0; i < maxfd; i++) {
654                 if (FD_ISSET(i, e)) {
655                         error("%s: exception!", fdcon[i].c_name);
656                         confree(i);
657                 } else if (FD_ISSET(i, r))
658                         conread(i);
659         }
660         xfree(r);
661         xfree(e);
662
663         c = TAILQ_FIRST(&tq);
664         while (c && (c->c_tv.tv_sec < now.tv_sec ||
665             (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
666                 int s = c->c_fd;
667
668                 c = TAILQ_NEXT(c, c_link);
669                 conrecycle(s);
670         }
671 }
672
673 static void
674 do_host(char *host)
675 {
676         char *name = strnnsep(&host, " \t\n");
677         int j;
678
679         if (name == NULL)
680                 return;
681         for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
682                 if (get_keytypes & j) {
683                         while (ncon >= MAXCON)
684                                 conloop();
685                         conalloc(name, *host ? host : name, j);
686                 }
687         }
688 }
689
690 void
691 fatal(const char *fmt,...)
692 {
693         va_list args;
694
695         va_start(args, fmt);
696         do_log(SYSLOG_LEVEL_FATAL, fmt, args);
697         va_end(args);
698         if (nonfatal_fatal)
699                 longjmp(kexjmp, -1);
700         else
701                 exit(255);
702 }
703
704 static void
705 usage(void)
706 {
707         fprintf(stderr, "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n"
708             "\t\t   [host | addrlist namelist] [...]\n",
709             __progname);
710         exit(1);
711 }
712
713 int
714 main(int argc, char **argv)
715 {
716         int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
717         int opt, fopt_count = 0;
718         char *tname;
719
720         extern int optind;
721         extern char *optarg;
722
723         __progname = ssh_get_progname(argv[0]);
724         init_rng();
725         seed_rng();
726         TAILQ_INIT(&tq);
727
728         /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
729         sanitise_stdfd();
730
731         if (argc <= 1)
732                 usage();
733
734         while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) {
735                 switch (opt) {
736                 case 'H':
737                         hash_hosts = 1;
738                         break;
739                 case 'p':
740                         ssh_port = a2port(optarg);
741                         if (ssh_port == 0) {
742                                 fprintf(stderr, "Bad port '%s'\n", optarg);
743                                 exit(1);
744                         }
745                         break;
746                 case 'T':
747                         timeout = convtime(optarg);
748                         if (timeout == -1 || timeout == 0) {
749                                 fprintf(stderr, "Bad timeout '%s'\n", optarg);
750                                 usage();
751                         }
752                         break;
753                 case 'v':
754                         if (!debug_flag) {
755                                 debug_flag = 1;
756                                 log_level = SYSLOG_LEVEL_DEBUG1;
757                         }
758                         else if (log_level < SYSLOG_LEVEL_DEBUG3)
759                                 log_level++;
760                         else
761                                 fatal("Too high debugging level.");
762                         break;
763                 case 'f':
764                         if (strcmp(optarg, "-") == 0)
765                                 optarg = NULL;
766                         argv[fopt_count++] = optarg;
767                         break;
768                 case 't':
769                         get_keytypes = 0;
770                         tname = strtok(optarg, ",");
771                         while (tname) {
772                                 int type = key_type_from_name(tname);
773                                 switch (type) {
774                                 case KEY_RSA1:
775                                         get_keytypes |= KT_RSA1;
776                                         break;
777                                 case KEY_DSA:
778                                         get_keytypes |= KT_DSA;
779                                         break;
780                                 case KEY_RSA:
781                                         get_keytypes |= KT_RSA;
782                                         break;
783                                 case KEY_UNSPEC:
784                                         fatal("unknown key type %s", tname);
785                                 }
786                                 tname = strtok(NULL, ",");
787                         }
788                         break;
789                 case '4':
790                         IPv4or6 = AF_INET;
791                         break;
792                 case '6':
793                         IPv4or6 = AF_INET6;
794                         break;
795                 case '?':
796                 default:
797                         usage();
798                 }
799         }
800         if (optind == argc && !fopt_count)
801                 usage();
802
803         log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
804
805         maxfd = fdlim_get(1);
806         if (maxfd < 0)
807                 fatal("%s: fdlim_get: bad value", __progname);
808         if (maxfd > MAXMAXFD)
809                 maxfd = MAXMAXFD;
810         if (MAXCON <= 0)
811                 fatal("%s: not enough file descriptors", __progname);
812         if (maxfd > fdlim_get(0))
813                 fdlim_set(maxfd);
814         fdcon = xcalloc(maxfd, sizeof(con));
815
816         read_wait_nfdset = howmany(maxfd, NFDBITS);
817         read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
818
819         if (fopt_count) {
820                 Linebuf *lb;
821                 char *line;
822                 int j;
823
824                 for (j = 0; j < fopt_count; j++) {
825                         lb = Linebuf_alloc(argv[j], error);
826                         if (!lb)
827                                 continue;
828                         while ((line = Linebuf_getline(lb)) != NULL)
829                                 do_host(line);
830                         Linebuf_free(lb);
831                 }
832         }
833
834         while (optind < argc)
835                 do_host(argv[optind++]);
836
837         while (ncon > 0)
838                 conloop();
839
840         return (0);
841 }
This page took 3.746081 seconds and 5 git commands to generate.