LCOV - code coverage report
Current view: top level - dirmngr - t-dns-stuff.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 119 23.5 %
Date: 2015-11-05 17:10:59 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 <http://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_cert = 0;
      45           1 :   int opt_srv = 0;
      46           1 :   int opt_bracket = 0;
      47           1 :   int opt_cname = 0;
      48           1 :   char const *name = NULL;
      49             : 
      50           1 :   gpgrt_init ();
      51           1 :   log_set_prefix (PGM, GPGRT_LOG_WITH_PREFIX);
      52           1 :   if (argc)
      53           1 :     { argc--; argv++; }
      54           2 :   while (argc && last_argc != argc )
      55             :     {
      56           0 :       last_argc = argc;
      57           0 :       if (!strcmp (*argv, "--"))
      58             :         {
      59           0 :           argc--; argv++;
      60           0 :           break;
      61             :         }
      62           0 :       else if (!strcmp (*argv, "--help"))
      63             :         {
      64           0 :           fputs ("usage: " PGM " [HOST]\n"
      65             :                  "Options:\n"
      66             :                  "  --verbose         print timings etc.\n"
      67             :                  "  --debug           flyswatter\n"
      68             :                  "  --use-tor         use Tor\n"
      69             :                  "  --bracket         enclose v6 addresses in brackets\n"
      70             :                  "  --cert            lookup a CERT RR\n"
      71             :                  "  --srv             lookup a SRV RR\n"
      72             :                  "  --cname           lookup a CNAME RR\n"
      73             :                  , stdout);
      74           0 :           exit (0);
      75             :         }
      76           0 :       else if (!strcmp (*argv, "--verbose"))
      77             :         {
      78           0 :           verbose++;
      79           0 :           argc--; argv++;
      80             :         }
      81           0 :       else if (!strcmp (*argv, "--debug"))
      82             :         {
      83           0 :           verbose += 2;
      84           0 :           debug++;
      85           0 :           argc--; argv++;
      86             :         }
      87           0 :       else if (!strcmp (*argv, "--use-tor"))
      88             :         {
      89           0 :           opt_tor = 1;
      90           0 :           argc--; argv++;
      91             :         }
      92           0 :       else if (!strcmp (*argv, "--bracket"))
      93             :         {
      94           0 :           opt_bracket = 1;
      95           0 :           argc--; argv++;
      96             :         }
      97           0 :       else if (!strcmp (*argv, "--cert"))
      98             :         {
      99           0 :           any_options = opt_cert = 1;
     100           0 :           argc--; argv++;
     101             :         }
     102           0 :       else if (!strcmp (*argv, "--srv"))
     103             :         {
     104           0 :           any_options = opt_srv = 1;
     105           0 :           argc--; argv++;
     106             :         }
     107           0 :       else if (!strcmp (*argv, "--cname"))
     108             :         {
     109           0 :           any_options = opt_cname = 1;
     110           0 :           argc--; argv++;
     111             :         }
     112           0 :       else if (!strncmp (*argv, "--", 2))
     113             :         {
     114           0 :           fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
     115           0 :           exit (1);
     116             :         }
     117             :     }
     118             : 
     119           1 :   if (!argc && !any_options)
     120             :     {
     121           1 :       opt_cert = 1;
     122           1 :       name = "simon.josefsson.org";
     123             :     }
     124           0 :   else if (argc == 1)
     125           0 :     name = *argv;
     126             :   else
     127             :     {
     128           0 :       fprintf (stderr, PGM ": none or too many host names given\n");
     129           0 :       exit (1);
     130             :     }
     131             : 
     132           1 :   if (opt_tor)
     133             :     {
     134           0 :       err = enable_dns_tormode ();
     135           0 :       if (err)
     136             :         {
     137           0 :           fprintf (stderr, "error switching into Tor mode: %s\n",
     138             :                    gpg_strerror (err));
     139           0 :           exit (1);
     140             :         }
     141             :     }
     142             : 
     143           1 :   if (opt_cert)
     144             :     {
     145             :       unsigned char *fpr;
     146             :       size_t fpr_len;
     147             :       char *url;
     148             :       void *key;
     149             :       size_t keylen;
     150             : 
     151           1 :       printf ("CERT lookup on '%s'\n", name);
     152             : 
     153           1 :       err = get_dns_cert (name, DNS_CERTTYPE_ANY, &key, &keylen,
     154             :                           &fpr, &fpr_len, &url);
     155           1 :       if (err)
     156           0 :         printf ("get_dns_cert failed: %s <%s>\n",
     157             :                 gpg_strerror (err), gpg_strsource (err));
     158           1 :       else if (key)
     159             :         {
     160           1 :           printf ("Key found (%u bytes)\n", (unsigned int)keylen);
     161             :         }
     162             :       else
     163             :         {
     164           0 :           if (fpr)
     165             :             {
     166             :               int i;
     167             : 
     168           0 :               printf ("Fingerprint found (%d bytes): ", (int)fpr_len);
     169           0 :               for (i = 0; i < fpr_len; i++)
     170           0 :                 printf ("%02X", fpr[i]);
     171           0 :               putchar ('\n');
     172             :             }
     173             :           else
     174           0 :             printf ("No fingerprint found\n");
     175             : 
     176           0 :           if (url)
     177           0 :             printf ("URL found: %s\n", url);
     178             :           else
     179           0 :             printf ("No URL found\n");
     180             : 
     181             :         }
     182             : 
     183           1 :       xfree (key);
     184           1 :       xfree (fpr);
     185           1 :       xfree (url);
     186             :     }
     187           0 :   else if (opt_cname)
     188             :     {
     189             :       char *cname;
     190             : 
     191           0 :       printf ("CNAME lookup on '%s'\n", name);
     192           0 :       err = get_dns_cname (name, &cname);
     193           0 :       if (err)
     194           0 :         printf ("get_dns_cname failed: %s <%s>\n",
     195             :                 gpg_strerror (err), gpg_strsource (err));
     196             :       else
     197             :         {
     198           0 :           printf ("CNAME found: '%s'\n", cname);
     199             :         }
     200             : 
     201           0 :       xfree (cname);
     202             :     }
     203           0 :   else if (opt_srv)
     204             :     {
     205             :       struct srventry *srv;
     206             :       int rc,i;
     207             : 
     208           0 :       rc=getsrv (name? name : "_hkp._tcp.wwwkeys.pgp.net", &srv);
     209           0 :       printf("Count=%d\n",rc);
     210           0 :       for(i=0;i<rc;i++)
     211             :         {
     212           0 :           printf("priority=%-8hu  ",srv[i].priority);
     213           0 :           printf("weight=%-8hu  ",srv[i].weight);
     214           0 :           printf("port=%-5hu  ",srv[i].port);
     215           0 :           printf("target=%s\n",srv[i].target);
     216             :         }
     217             : 
     218           0 :       xfree(srv);
     219             :     }
     220             :   else /* Standard lookup.  */
     221             :     {
     222             :       char *cname;
     223             :       dns_addrinfo_t aibuf, ai;
     224             :       char *host;
     225             : 
     226           0 :       printf ("Lookup on '%s'\n", name);
     227             : 
     228           0 :       err = resolve_dns_name (name, 0, 0, SOCK_STREAM, &aibuf, &cname);
     229           0 :       if (err)
     230             :         {
     231           0 :           fprintf (stderr, PGM": resolving '%s' failed: %s\n",
     232             :                    name, gpg_strerror (err));
     233           0 :           exit (1);
     234             :         }
     235             : 
     236           0 :       if (cname)
     237           0 :         printf ("cname: %s\n", cname);
     238           0 :       for (ai = aibuf; ai; ai = ai->next)
     239             :         {
     240           0 :           printf ("%s %3d %3d   ",
     241           0 :                   ai->family == AF_INET6? "inet6" :
     242           0 :                   ai->family == AF_INET?  "inet4" : "?    ",
     243             :                   ai->socktype, ai->protocol);
     244             : 
     245           0 :           err = resolve_dns_addr (ai->addr, ai->addrlen,
     246             :                                   (DNS_NUMERICHOST
     247             :                                    | (opt_bracket? DNS_WITHBRACKET:0)),
     248             :                                   &host);
     249           0 :           if (err)
     250           0 :             printf ("[resolve_dns_addr failed: %s]", gpg_strerror (err));
     251             :           else
     252             :             {
     253           0 :               printf ("%s", host);
     254           0 :               xfree (host);
     255             :             }
     256             : 
     257           0 :           err = resolve_dns_addr (ai->addr, ai->addrlen,
     258             :                                   (opt_bracket? DNS_WITHBRACKET:0),
     259             :                                   &host);
     260           0 :           if (err)
     261           0 :             printf ("[resolve_dns_addr failed (2): %s]", gpg_strerror (err));
     262             :           else
     263             :             {
     264           0 :               if (!is_ip_address (host))
     265           0 :                 printf ("  (%s)", host);
     266           0 :               xfree (host);
     267             :             }
     268           0 :           putchar ('\n');
     269             :         }
     270           0 :       xfree (cname);
     271           0 :       free_dns_addrinfo (aibuf);
     272             :     }
     273             : 
     274             : 
     275           1 :   return 0;
     276             : }

Generated by: LCOV version 1.11