2015-10-21 10:29:02 +02:00
|
|
|
/* t-dns-cert.c - Module test for dns-stuff.c
|
2011-11-28 18:11:59 +01:00
|
|
|
* Copyright (C) 2011 Free Software Foundation, Inc.
|
2015-10-21 10:29:02 +02:00
|
|
|
* Copyright (C) 2011, 2015 Werner Koch
|
2011-11-28 18:11:59 +01:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2011-11-28 18:11:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
2015-10-24 16:27:47 +02:00
|
|
|
|
2011-11-28 18:11:59 +01:00
|
|
|
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/util.h"
|
2015-10-21 10:29:02 +02:00
|
|
|
#include "dns-stuff.h"
|
2011-11-28 18:11:59 +01:00
|
|
|
|
2015-10-21 17:55:56 +02:00
|
|
|
#define PGM "t-dns-stuff"
|
|
|
|
|
|
|
|
static int verbose;
|
|
|
|
static int debug;
|
|
|
|
|
|
|
|
|
2016-12-17 21:54:45 +01:00
|
|
|
static void
|
|
|
|
init_sockets (void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
WSADATA wsadat;
|
|
|
|
|
|
|
|
WSAStartup (0x202, &wsadat);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-11-28 18:11:59 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2015-10-21 17:55:56 +02:00
|
|
|
int last_argc = -1;
|
2011-11-30 17:14:08 +01:00
|
|
|
gpg_error_t err;
|
2015-10-22 09:22:41 +02:00
|
|
|
int any_options = 0;
|
2015-10-22 10:14:10 +02:00
|
|
|
int opt_tor = 0;
|
2015-10-21 17:55:56 +02:00
|
|
|
int opt_cert = 0;
|
2015-10-22 09:22:41 +02:00
|
|
|
int opt_srv = 0;
|
2015-10-24 16:27:47 +02:00
|
|
|
int opt_bracket = 0;
|
2015-10-25 16:38:07 +01:00
|
|
|
int opt_cname = 0;
|
2015-10-22 09:22:41 +02:00
|
|
|
char const *name = NULL;
|
2011-11-28 18:11:59 +01:00
|
|
|
|
2015-10-21 17:55:56 +02:00
|
|
|
gpgrt_init ();
|
2015-10-22 09:22:41 +02:00
|
|
|
log_set_prefix (PGM, GPGRT_LOG_WITH_PREFIX);
|
2011-11-28 18:11:59 +01:00
|
|
|
if (argc)
|
2015-10-21 17:55:56 +02:00
|
|
|
{ argc--; argv++; }
|
|
|
|
while (argc && last_argc != argc )
|
2011-11-28 18:11:59 +01:00
|
|
|
{
|
2015-10-21 17:55:56 +02:00
|
|
|
last_argc = argc;
|
|
|
|
if (!strcmp (*argv, "--"))
|
|
|
|
{
|
|
|
|
argc--; argv++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!strcmp (*argv, "--help"))
|
|
|
|
{
|
|
|
|
fputs ("usage: " PGM " [HOST]\n"
|
|
|
|
"Options:\n"
|
2016-12-01 15:45:51 +01:00
|
|
|
" --verbose print timings etc.\n"
|
|
|
|
" --debug flyswatter\n"
|
|
|
|
" --standard-resolver use the system's resolver\n"
|
|
|
|
" --use-tor use Tor\n"
|
|
|
|
" --new-circuit use a new Tor circuit\n"
|
|
|
|
" --bracket enclose v6 addresses in brackets\n"
|
|
|
|
" --cert lookup a CERT RR\n"
|
|
|
|
" --srv lookup a SRV RR\n"
|
|
|
|
" --cname lookup a CNAME RR\n"
|
2016-12-20 09:53:58 +01:00
|
|
|
" --timeout SECONDS timeout after SECONDS\n"
|
2015-10-21 17:55:56 +02:00
|
|
|
, stdout);
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
else if (!strcmp (*argv, "--verbose"))
|
|
|
|
{
|
|
|
|
verbose++;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
else if (!strcmp (*argv, "--debug"))
|
|
|
|
{
|
|
|
|
verbose += 2;
|
|
|
|
debug++;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
2015-10-22 10:14:10 +02:00
|
|
|
else if (!strcmp (*argv, "--use-tor"))
|
|
|
|
{
|
|
|
|
opt_tor = 1;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
2016-12-01 15:45:51 +01:00
|
|
|
else if (!strcmp (*argv, "--standard-resolver"))
|
|
|
|
{
|
|
|
|
enable_standard_resolver (1);
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
2016-12-14 15:36:25 +01:00
|
|
|
else if (!strcmp (*argv, "--recursive-resolver"))
|
|
|
|
{
|
|
|
|
enable_recursive_resolver (1);
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
2015-10-24 16:27:47 +02:00
|
|
|
else if (!strcmp (*argv, "--bracket"))
|
|
|
|
{
|
|
|
|
opt_bracket = 1;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
2015-10-21 17:55:56 +02:00
|
|
|
else if (!strcmp (*argv, "--cert"))
|
|
|
|
{
|
2015-10-22 09:22:41 +02:00
|
|
|
any_options = opt_cert = 1;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
else if (!strcmp (*argv, "--srv"))
|
|
|
|
{
|
|
|
|
any_options = opt_srv = 1;
|
2015-10-21 17:55:56 +02:00
|
|
|
argc--; argv++;
|
|
|
|
}
|
2015-10-25 16:38:07 +01:00
|
|
|
else if (!strcmp (*argv, "--cname"))
|
|
|
|
{
|
|
|
|
any_options = opt_cname = 1;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
2016-12-20 09:53:58 +01:00
|
|
|
else if (!strcmp (*argv, "--timeout"))
|
|
|
|
{
|
|
|
|
argc--; argv++;
|
|
|
|
if (argc)
|
|
|
|
{
|
|
|
|
set_dns_timeout (atoi (*argv));
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 17:55:56 +02:00
|
|
|
else if (!strncmp (*argv, "--", 2))
|
|
|
|
{
|
|
|
|
fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
|
|
|
|
exit (1);
|
|
|
|
}
|
2011-11-28 18:11:59 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 09:22:41 +02:00
|
|
|
if (!argc && !any_options)
|
2015-10-21 17:55:56 +02:00
|
|
|
{
|
|
|
|
opt_cert = 1;
|
|
|
|
name = "simon.josefsson.org";
|
|
|
|
}
|
2011-11-28 18:11:59 +01:00
|
|
|
else if (argc == 1)
|
|
|
|
name = *argv;
|
|
|
|
else
|
|
|
|
{
|
2015-10-22 09:22:41 +02:00
|
|
|
fprintf (stderr, PGM ": none or too many host names given\n");
|
2015-10-21 17:55:56 +02:00
|
|
|
exit (1);
|
2011-11-28 18:11:59 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 23:27:02 +01:00
|
|
|
set_dns_verbose (verbose, debug);
|
2016-12-17 21:54:45 +01:00
|
|
|
init_sockets ();
|
|
|
|
|
2015-10-22 10:14:10 +02:00
|
|
|
if (opt_tor)
|
2017-01-03 12:03:28 +01:00
|
|
|
enable_dns_tormode (0);
|
2015-10-22 10:14:10 +02:00
|
|
|
|
2015-10-21 17:55:56 +02:00
|
|
|
if (opt_cert)
|
2011-11-28 18:11:59 +01:00
|
|
|
{
|
2015-10-21 17:55:56 +02:00
|
|
|
unsigned char *fpr;
|
|
|
|
size_t fpr_len;
|
|
|
|
char *url;
|
|
|
|
void *key;
|
|
|
|
size_t keylen;
|
|
|
|
|
2016-01-06 08:37:03 +01:00
|
|
|
if (verbose || any_options)
|
|
|
|
printf ("CERT lookup on '%s'\n", name);
|
2015-10-21 17:55:56 +02:00
|
|
|
|
2018-07-25 14:35:04 +02:00
|
|
|
err = get_dns_cert (NULL, name, DNS_CERTTYPE_ANY, &key, &keylen,
|
2015-10-21 17:55:56 +02:00
|
|
|
&fpr, &fpr_len, &url);
|
|
|
|
if (err)
|
|
|
|
printf ("get_dns_cert failed: %s <%s>\n",
|
|
|
|
gpg_strerror (err), gpg_strsource (err));
|
|
|
|
else if (key)
|
|
|
|
{
|
2016-01-06 08:37:03 +01:00
|
|
|
if (verbose || any_options)
|
|
|
|
printf ("Key found (%u bytes)\n", (unsigned int)keylen);
|
2015-10-21 17:55:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (fpr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf ("Fingerprint found (%d bytes): ", (int)fpr_len);
|
|
|
|
for (i = 0; i < fpr_len; i++)
|
|
|
|
printf ("%02X", fpr[i]);
|
|
|
|
putchar ('\n');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf ("No fingerprint found\n");
|
|
|
|
|
|
|
|
if (url)
|
|
|
|
printf ("URL found: %s\n", url);
|
|
|
|
else
|
|
|
|
printf ("No URL found\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
xfree (key);
|
|
|
|
xfree (fpr);
|
|
|
|
xfree (url);
|
2011-11-28 18:11:59 +01:00
|
|
|
}
|
2015-10-25 16:38:07 +01:00
|
|
|
else if (opt_cname)
|
|
|
|
{
|
|
|
|
char *cname;
|
|
|
|
|
|
|
|
printf ("CNAME lookup on '%s'\n", name);
|
2018-07-25 14:35:04 +02:00
|
|
|
err = get_dns_cname (NULL, name, &cname);
|
2015-10-25 16:38:07 +01:00
|
|
|
if (err)
|
|
|
|
printf ("get_dns_cname failed: %s <%s>\n",
|
|
|
|
gpg_strerror (err), gpg_strsource (err));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf ("CNAME found: '%s'\n", cname);
|
|
|
|
}
|
|
|
|
xfree (cname);
|
|
|
|
}
|
2015-10-22 09:22:41 +02:00
|
|
|
else if (opt_srv)
|
|
|
|
{
|
|
|
|
struct srventry *srv;
|
2016-12-19 23:27:02 +01:00
|
|
|
unsigned int count;
|
|
|
|
int i;
|
2015-10-22 09:22:41 +02:00
|
|
|
|
2018-07-25 14:35:04 +02:00
|
|
|
err = get_dns_srv (NULL, name? name : "_hkp._tcp.wwwkeys.pgp.net",
|
2017-01-08 18:04:59 +01:00
|
|
|
NULL, NULL, &srv, &count);
|
2016-12-19 23:27:02 +01:00
|
|
|
if (err)
|
|
|
|
printf ("get_dns_srv failed: %s <%s>\n",
|
|
|
|
gpg_strerror (err), gpg_strsource (err));
|
|
|
|
else
|
2015-10-22 09:22:41 +02:00
|
|
|
{
|
2016-12-19 23:27:02 +01:00
|
|
|
printf ("count=%u\n",count);
|
|
|
|
for (i=0; i < count; i++)
|
|
|
|
{
|
|
|
|
printf("priority=%-8hu ",srv[i].priority);
|
|
|
|
printf("weight=%-8hu ",srv[i].weight);
|
|
|
|
printf("port=%-5hu ",srv[i].port);
|
|
|
|
printf("target=%s\n",srv[i].target);
|
|
|
|
}
|
2015-10-22 09:22:41 +02:00
|
|
|
|
2016-12-19 23:27:02 +01:00
|
|
|
xfree(srv);
|
|
|
|
}
|
2015-10-22 09:22:41 +02:00
|
|
|
}
|
2015-10-21 17:55:56 +02:00
|
|
|
else /* Standard lookup. */
|
2011-11-28 18:11:59 +01:00
|
|
|
{
|
2015-10-21 17:55:56 +02:00
|
|
|
char *cname;
|
|
|
|
dns_addrinfo_t aibuf, ai;
|
2015-10-24 16:27:47 +02:00
|
|
|
char *host;
|
2011-11-28 18:11:59 +01:00
|
|
|
|
2015-10-21 17:55:56 +02:00
|
|
|
printf ("Lookup on '%s'\n", name);
|
|
|
|
|
2018-07-25 14:35:04 +02:00
|
|
|
err = resolve_dns_name (NULL, name, 0, 0, SOCK_STREAM, &aibuf, &cname);
|
2015-10-21 17:55:56 +02:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
fprintf (stderr, PGM": resolving '%s' failed: %s\n",
|
|
|
|
name, gpg_strerror (err));
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cname)
|
|
|
|
printf ("cname: %s\n", cname);
|
|
|
|
for (ai = aibuf; ai; ai = ai->next)
|
|
|
|
{
|
|
|
|
printf ("%s %3d %3d ",
|
|
|
|
ai->family == AF_INET6? "inet6" :
|
|
|
|
ai->family == AF_INET? "inet4" : "? ",
|
|
|
|
ai->socktype, ai->protocol);
|
2011-11-28 18:11:59 +01:00
|
|
|
|
2018-07-25 14:35:04 +02:00
|
|
|
err = resolve_dns_addr (NULL, ai->addr, ai->addrlen,
|
2015-10-24 16:27:47 +02:00
|
|
|
(DNS_NUMERICHOST
|
|
|
|
| (opt_bracket? DNS_WITHBRACKET:0)),
|
|
|
|
&host);
|
|
|
|
if (err)
|
2015-10-24 12:25:17 +02:00
|
|
|
printf ("[resolve_dns_addr failed: %s]", gpg_strerror (err));
|
2015-10-24 16:27:47 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
printf ("%s", host);
|
|
|
|
xfree (host);
|
|
|
|
}
|
|
|
|
|
2018-07-25 14:35:04 +02:00
|
|
|
err = resolve_dns_addr (NULL, ai->addr, ai->addrlen,
|
2015-10-24 16:27:47 +02:00
|
|
|
(opt_bracket? DNS_WITHBRACKET:0),
|
|
|
|
&host);
|
|
|
|
if (err)
|
2016-12-19 23:27:02 +01:00
|
|
|
printf (" [resolve_dns_addr failed (2): %s]", gpg_strerror (err));
|
2015-10-21 17:55:56 +02:00
|
|
|
else
|
2015-10-24 16:27:47 +02:00
|
|
|
{
|
|
|
|
if (!is_ip_address (host))
|
|
|
|
printf (" (%s)", host);
|
|
|
|
xfree (host);
|
|
|
|
}
|
|
|
|
putchar ('\n');
|
2015-10-21 17:55:56 +02:00
|
|
|
}
|
|
|
|
xfree (cname);
|
|
|
|
free_dns_addrinfo (aibuf);
|
2011-11-28 18:11:59 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 21:00:14 +01:00
|
|
|
reload_dns_stuff (1); /* Release objects. */
|
2011-11-30 17:14:08 +01:00
|
|
|
|
2011-11-28 18:11:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|