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