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