LCOV - code coverage report
Current view: top level - dirmngr - t-dns-stuff.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 125 22.4 %
Date: 2016-11-29 15:00:56 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* t-dns-cert.c - Module test for dns-stuff.c
       2             :  * Copyright (C) 2011 Free Software Foundation, Inc.
       3             :  * Copyright (C) 2011, 2015 Werner Koch
       4             :  *
       5             :  * This file is part of GnuPG.
       6             :  *
       7             :  * GnuPG is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the GNU General Public License as published by
       9             :  * the Free Software Foundation; either version 3 of the License, or
      10             :  * (at your option) any later version.
      11             :  *
      12             :  * GnuPG is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  * GNU General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License
      18             :  * along with this program; if not, see <https://www.gnu.org/licenses/>.
      19             :  */
      20             : 
      21             : #include <config.h>
      22             : #include <stdio.h>
      23             : #include <stdlib.h>
      24             : #include <assert.h>
      25             : 
      26             : 
      27             : #include "util.h"
      28             : #include "dns-stuff.h"
      29             : 
      30             : #define PGM "t-dns-stuff"
      31             : 
      32             : static int verbose;
      33             : static int debug;
      34             : 
      35             : 
      36             : 
      37             : int
      38           1 : main (int argc, char **argv)
      39             : {
      40           1 :   int last_argc = -1;
      41             :   gpg_error_t err;
      42           1 :   int any_options = 0;
      43           1 :   int opt_tor = 0;
      44           1 :   int opt_new_circuit = 0;
      45           1 :   int opt_cert = 0;
      46           1 :   int opt_srv = 0;
      47           1 :   int opt_bracket = 0;
      48           1 :   int opt_cname = 0;
      49           1 :   char const *name = NULL;
      50             : 
      51           1 :   gpgrt_init ();
      52           1 :   log_set_prefix (PGM, GPGRT_LOG_WITH_PREFIX);
      53           1 :   if (argc)
      54           1 :     { argc--; argv++; }
      55           2 :   while (argc && last_argc != argc )
      56             :     {
      57           0 :       last_argc = argc;
      58           0 :       if (!strcmp (*argv, "--"))
      59             :         {
      60           0 :           argc--; argv++;
      61           0 :           break;
      62             :         }
      63           0 :       else if (!strcmp (*argv, "--help"))
      64             :         {
      65           0 :           fputs ("usage: " PGM " [HOST]\n"
      66             :                  "Options:\n"
      67             :                  "  --verbose         print timings etc.\n"
      68             :                  "  --debug           flyswatter\n"
      69             :                  "  --use-tor         use Tor\n"
      70             :                  "  --new-circuit     use a new Tor circuit\n"
      71             :                  "  --bracket         enclose v6 addresses in brackets\n"
      72             :                  "  --cert            lookup a CERT RR\n"
      73             :                  "  --srv             lookup a SRV RR\n"
      74             :                  "  --cname           lookup a CNAME RR\n"
      75             :                  , stdout);
      76           0 :           exit (0);
      77             :         }
      78           0 :       else if (!strcmp (*argv, "--verbose"))
      79             :         {
      80           0 :           verbose++;
      81           0 :           argc--; argv++;
      82             :         }
      83           0 :       else if (!strcmp (*argv, "--debug"))
      84             :         {
      85           0 :           verbose += 2;
      86           0 :           debug++;
      87           0 :           argc--; argv++;
      88             :         }
      89           0 :       else if (!strcmp (*argv, "--use-tor"))
      90             :         {
      91           0 :           opt_tor = 1;
      92           0 :           argc--; argv++;
      93             :         }
      94           0 :       else if (!strcmp (*argv, "--new-circuit"))
      95             :         {
      96           0 :           opt_new_circuit = 1;
      97           0 :           argc--; argv++;
      98             :         }
      99           0 :       else if (!strcmp (*argv, "--bracket"))
     100             :         {
     101           0 :           opt_bracket = 1;
     102           0 :           argc--; argv++;
     103             :         }
     104           0 :       else if (!strcmp (*argv, "--cert"))
     105             :         {
     106           0 :           any_options = opt_cert = 1;
     107           0 :           argc--; argv++;
     108             :         }
     109           0 :       else if (!strcmp (*argv, "--srv"))
     110             :         {
     111           0 :           any_options = opt_srv = 1;
     112           0 :           argc--; argv++;
     113             :         }
     114           0 :       else if (!strcmp (*argv, "--cname"))
     115             :         {
     116           0 :           any_options = opt_cname = 1;
     117           0 :           argc--; argv++;
     118             :         }
     119           0 :       else if (!strncmp (*argv, "--", 2))
     120             :         {
     121           0 :           fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
     122           0 :           exit (1);
     123             :         }
     124             :     }
     125             : 
     126           1 :   if (!argc && !any_options)
     127             :     {
     128           1 :       opt_cert = 1;
     129           1 :       name = "simon.josefsson.org";
     130             :     }
     131           0 :   else if (argc == 1)
     132           0 :     name = *argv;
     133             :   else
     134             :     {
     135           0 :       fprintf (stderr, PGM ": none or too many host names given\n");
     136           0 :       exit (1);
     137             :     }
     138             : 
     139           1 :   if (opt_tor)
     140             :     {
     141           0 :       err = enable_dns_tormode (opt_new_circuit);
     142           0 :       if (err)
     143             :         {
     144           0 :           fprintf (stderr, "error switching into Tor mode: %s\n",
     145             :                    gpg_strerror (err));
     146           0 :           exit (1);
     147             :         }
     148             :     }
     149             : 
     150           1 :   if (opt_cert)
     151             :     {
     152             :       unsigned char *fpr;
     153             :       size_t fpr_len;
     154             :       char *url;
     155             :       void *key;
     156             :       size_t keylen;
     157             : 
     158           1 :       if (verbose || any_options)
     159           0 :         printf ("CERT lookup on '%s'\n", name);
     160             : 
     161           1 :       err = get_dns_cert (name, DNS_CERTTYPE_ANY, &key, &keylen,
     162             :                           &fpr, &fpr_len, &url);
     163           1 :       if (err)
     164           1 :         printf ("get_dns_cert failed: %s <%s>\n",
     165             :                 gpg_strerror (err), gpg_strsource (err));
     166           0 :       else if (key)
     167             :         {
     168           0 :           if (verbose || any_options)
     169           0 :             printf ("Key found (%u bytes)\n", (unsigned int)keylen);
     170             :         }
     171             :       else
     172             :         {
     173           0 :           if (fpr)
     174             :             {
     175             :               int i;
     176             : 
     177           0 :               printf ("Fingerprint found (%d bytes): ", (int)fpr_len);
     178           0 :               for (i = 0; i < fpr_len; i++)
     179           0 :                 printf ("%02X", fpr[i]);
     180           0 :               putchar ('\n');
     181             :             }
     182             :           else
     183           0 :             printf ("No fingerprint found\n");
     184             : 
     185           0 :           if (url)
     186           0 :             printf ("URL found: %s\n", url);
     187             :           else
     188           0 :             printf ("No URL found\n");
     189             : 
     190             :         }
     191             : 
     192           1 :       xfree (key);
     193           1 :       xfree (fpr);
     194           1 :       xfree (url);
     195             :     }
     196           0 :   else if (opt_cname)
     197             :     {
     198             :       char *cname;
     199             : 
     200           0 :       printf ("CNAME lookup on '%s'\n", name);
     201           0 :       err = get_dns_cname (name, &cname);
     202           0 :       if (err)
     203           0 :         printf ("get_dns_cname failed: %s <%s>\n",
     204             :                 gpg_strerror (err), gpg_strsource (err));
     205             :       else
     206             :         {
     207           0 :           printf ("CNAME found: '%s'\n", cname);
     208             :         }
     209             : 
     210           0 :       xfree (cname);
     211             :     }
     212           0 :   else if (opt_srv)
     213             :     {
     214             :       struct srventry *srv;
     215             :       int rc,i;
     216             : 
     217           0 :       rc=getsrv (name? name : "_hkp._tcp.wwwkeys.pgp.net", &srv);
     218           0 :       printf("Count=%d\n",rc);
     219           0 :       for(i=0;i<rc;i++)
     220             :         {
     221           0 :           printf("priority=%-8hu  ",srv[i].priority);
     222           0 :           printf("weight=%-8hu  ",srv[i].weight);
     223           0 :           printf("port=%-5hu  ",srv[i].port);
     224           0 :           printf("target=%s\n",srv[i].target);
     225             :         }
     226             : 
     227           0 :       xfree(srv);
     228             :     }
     229             :   else /* Standard lookup.  */
     230             :     {
     231             :       char *cname;
     232             :       dns_addrinfo_t aibuf, ai;
     233             :       char *host;
     234             : 
     235           0 :       printf ("Lookup on '%s'\n", name);
     236             : 
     237           0 :       err = resolve_dns_name (name, 0, 0, SOCK_STREAM, &aibuf, &cname);
     238           0 :       if (err)
     239             :         {
     240           0 :           fprintf (stderr, PGM": resolving '%s' failed: %s\n",
     241             :                    name, gpg_strerror (err));
     242           0 :           exit (1);
     243             :         }
     244             : 
     245           0 :       if (cname)
     246           0 :         printf ("cname: %s\n", cname);
     247           0 :       for (ai = aibuf; ai; ai = ai->next)
     248             :         {
     249           0 :           printf ("%s %3d %3d   ",
     250           0 :                   ai->family == AF_INET6? "inet6" :
     251           0 :                   ai->family == AF_INET?  "inet4" : "?    ",
     252             :                   ai->socktype, ai->protocol);
     253             : 
     254           0 :           err = resolve_dns_addr (ai->addr, ai->addrlen,
     255             :                                   (DNS_NUMERICHOST
     256             :                                    | (opt_bracket? DNS_WITHBRACKET:0)),
     257             :                                   &host);
     258           0 :           if (err)
     259           0 :             printf ("[resolve_dns_addr failed: %s]", gpg_strerror (err));
     260             :           else
     261             :             {
     262           0 :               printf ("%s", host);
     263           0 :               xfree (host);
     264             :             }
     265             : 
     266           0 :           err = resolve_dns_addr (ai->addr, ai->addrlen,
     267             :                                   (opt_bracket? DNS_WITHBRACKET:0),
     268             :                                   &host);
     269           0 :           if (err)
     270           0 :             printf ("[resolve_dns_addr failed (2): %s]", gpg_strerror (err));
     271             :           else
     272             :             {
     273           0 :               if (!is_ip_address (host))
     274           0 :                 printf ("  (%s)", host);
     275           0 :               xfree (host);
     276             :             }
     277           0 :           putchar ('\n');
     278             :         }
     279           0 :       xfree (cname);
     280           0 :       free_dns_addrinfo (aibuf);
     281             :     }
     282             : 
     283             : 
     284           1 :   return 0;
     285             : }

Generated by: LCOV version 1.11