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