2011-01-10 14:30:17 +01:00
|
|
|
|
/* ks-engine-hkp.c - HKP keyserver engine
|
2012-01-10 10:23:54 -05:00
|
|
|
|
* Copyright (C) 2011, 2012 Free Software Foundation, Inc.
|
2014-03-11 14:26:39 +01:00
|
|
|
|
* Copyright (C) 2011, 2012, 2014 Werner Koch
|
2011-01-10 14:30:17 +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-01-10 14:30:17 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
2011-04-12 16:30:08 +02:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2014-03-07 09:46:44 +01:00
|
|
|
|
# ifdef HAVE_WINSOCK2_H
|
|
|
|
|
# include <winsock2.h>
|
|
|
|
|
# endif
|
2011-04-12 16:30:08 +02:00
|
|
|
|
# include <windows.h>
|
|
|
|
|
#else /*!HAVE_W32_SYSTEM*/
|
|
|
|
|
# include <sys/types.h>
|
|
|
|
|
# include <sys/socket.h>
|
|
|
|
|
# include <netdb.h>
|
|
|
|
|
#endif /*!HAVE_W32_SYSTEM*/
|
2011-01-10 14:30:17 +01:00
|
|
|
|
|
|
|
|
|
#include "dirmngr.h"
|
|
|
|
|
#include "misc.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
|
#include "../common/userids.h"
|
2015-10-21 17:46:21 +02:00
|
|
|
|
#include "dns-stuff.h"
|
2011-01-10 14:30:17 +01:00
|
|
|
|
#include "ks-engine.h"
|
|
|
|
|
|
2014-06-10 15:11:32 +02:00
|
|
|
|
/* Substitutes for missing Mingw macro. The EAI_SYSTEM mechanism
|
|
|
|
|
seems not to be available (probably because there is only one set
|
|
|
|
|
of error codes anyway). For now we use WSAEINVAL. */
|
2014-03-11 18:02:29 +01:00
|
|
|
|
#ifndef EAI_OVERFLOW
|
|
|
|
|
# define EAI_OVERFLOW EAI_FAIL
|
|
|
|
|
#endif
|
2014-06-10 15:11:32 +02:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
# ifndef EAI_SYSTEM
|
|
|
|
|
# define EAI_SYSTEM WSAEINVAL
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
2014-03-11 18:02:29 +01:00
|
|
|
|
|
|
|
|
|
|
2014-03-18 11:07:05 +01:00
|
|
|
|
/* Number of seconds after a host is marked as resurrected. */
|
|
|
|
|
#define RESURRECT_INTERVAL (3600*3) /* 3 hours */
|
|
|
|
|
|
2011-01-10 14:30:17 +01:00
|
|
|
|
/* To match the behaviour of our old gpgkeys helper code we escape
|
|
|
|
|
more characters than actually needed. */
|
|
|
|
|
#define EXTRA_ESCAPE_CHARS "@!\"#$%&'()*+,-./:;<=>?[\\]^_{|}~"
|
|
|
|
|
|
|
|
|
|
/* How many redirections do we allow. */
|
|
|
|
|
#define MAX_REDIRECTS 2
|
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
|
/* Number of retries done for a dead host etc. */
|
|
|
|
|
#define SEND_REQUEST_RETRIES 3
|
|
|
|
|
|
2017-06-20 16:27:59 +02:00
|
|
|
|
enum ks_protocol { KS_PROTOCOL_HKP, KS_PROTOCOL_HKPS, KS_PROTOCOL_MAX };
|
|
|
|
|
|
2011-04-12 16:30:08 +02:00
|
|
|
|
/* Objects used to maintain information about hosts. */
|
|
|
|
|
struct hostinfo_s;
|
|
|
|
|
typedef struct hostinfo_s *hostinfo_t;
|
|
|
|
|
struct hostinfo_s
|
|
|
|
|
{
|
|
|
|
|
time_t lastfail; /* Time we tried to connect and failed. */
|
|
|
|
|
time_t lastused; /* Time of last use. */
|
2017-06-20 14:54:17 +02:00
|
|
|
|
int *pool; /* An array with indices into HOSTTABLE or NULL
|
|
|
|
|
if NAME is not a pool name. */
|
|
|
|
|
size_t pool_len; /* Length of POOL. */
|
|
|
|
|
size_t pool_size; /* Allocated size of POOL. */
|
|
|
|
|
#define MAX_POOL_SIZE 128
|
2014-05-16 21:14:03 +02:00
|
|
|
|
int poolidx; /* Index into POOL with the used host. -1 if not set. */
|
2011-04-12 16:30:08 +02:00
|
|
|
|
unsigned int v4:1; /* Host supports AF_INET. */
|
|
|
|
|
unsigned int v6:1; /* Host supports AF_INET6. */
|
2015-10-26 16:32:03 +01:00
|
|
|
|
unsigned int onion:1;/* NAME is an onion (Tor HS) address. */
|
2011-04-12 16:30:08 +02:00
|
|
|
|
unsigned int dead:1; /* Host is currently unresponsive. */
|
2017-02-15 19:14:20 +01:00
|
|
|
|
unsigned int iporname_valid:1; /* The field IPORNAME below is valid */
|
|
|
|
|
/* (but may be NULL) */
|
2017-06-20 16:27:59 +02:00
|
|
|
|
unsigned int did_a_lookup:1; /* Have we done an A lookup yet? */
|
|
|
|
|
unsigned int did_srv_lookup:2; /* One bit per protocol indicating
|
|
|
|
|
whether we already did a SRV
|
|
|
|
|
lookup. */
|
2014-05-16 21:14:03 +02:00
|
|
|
|
time_t died_at; /* The time the host was marked dead. If this is
|
2014-03-18 11:07:05 +01:00
|
|
|
|
0 the host has been manually marked dead. */
|
2014-05-16 21:14:03 +02:00
|
|
|
|
char *cname; /* Canonical name of the host. Only set if this
|
2017-01-16 19:04:58 +01:00
|
|
|
|
is a pool or NAME has a numerical IP address. */
|
2017-02-15 19:14:20 +01:00
|
|
|
|
char *iporname; /* Numeric IP address or name for printing. */
|
2017-06-20 16:27:59 +02:00
|
|
|
|
unsigned short port[KS_PROTOCOL_MAX];
|
|
|
|
|
/* The port used by the host for all protocols, 0
|
|
|
|
|
if unknown. */
|
2011-04-12 16:30:08 +02:00
|
|
|
|
char name[1]; /* The hostname. */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* An array of hostinfo_t for all hosts requested by the caller or
|
|
|
|
|
resolved from a pool name and its allocated size.*/
|
|
|
|
|
static hostinfo_t *hosttable;
|
|
|
|
|
static int hosttable_size;
|
|
|
|
|
|
2015-11-16 12:41:46 +01:00
|
|
|
|
/* The number of host slots we initially allocate for HOSTTABLE. */
|
2011-04-12 16:30:08 +02:00
|
|
|
|
#define INITIAL_HOSTTABLE_SIZE 10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Create a new hostinfo object, fill in NAME and put it into
|
|
|
|
|
HOSTTABLE. Return the index into hosttable on success or -1 on
|
|
|
|
|
error. */
|
|
|
|
|
static int
|
|
|
|
|
create_new_hostinfo (const char *name)
|
|
|
|
|
{
|
|
|
|
|
hostinfo_t hi, *newtable;
|
|
|
|
|
int newsize;
|
|
|
|
|
int idx, rc;
|
|
|
|
|
|
|
|
|
|
hi = xtrymalloc (sizeof *hi + strlen (name));
|
|
|
|
|
if (!hi)
|
|
|
|
|
return -1;
|
|
|
|
|
strcpy (hi->name, name);
|
|
|
|
|
hi->pool = NULL;
|
2017-06-20 14:54:17 +02:00
|
|
|
|
hi->pool_len = 0;
|
|
|
|
|
hi->pool_size = 0;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
hi->poolidx = -1;
|
|
|
|
|
hi->lastused = (time_t)(-1);
|
|
|
|
|
hi->lastfail = (time_t)(-1);
|
|
|
|
|
hi->v4 = 0;
|
|
|
|
|
hi->v6 = 0;
|
2015-10-26 16:32:03 +01:00
|
|
|
|
hi->onion = 0;
|
2014-03-11 14:26:39 +01:00
|
|
|
|
hi->dead = 0;
|
2017-06-20 16:27:59 +02:00
|
|
|
|
hi->did_a_lookup = 0;
|
|
|
|
|
hi->did_srv_lookup = 0;
|
2017-02-15 19:14:20 +01:00
|
|
|
|
hi->iporname_valid = 0;
|
2014-03-18 11:07:05 +01:00
|
|
|
|
hi->died_at = 0;
|
2014-05-16 21:14:03 +02:00
|
|
|
|
hi->cname = NULL;
|
2017-02-15 19:14:20 +01:00
|
|
|
|
hi->iporname = NULL;
|
2017-06-20 16:27:59 +02:00
|
|
|
|
hi->port[KS_PROTOCOL_HKP] = 0;
|
|
|
|
|
hi->port[KS_PROTOCOL_HKPS] = 0;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
|
|
|
|
/* Add it to the hosttable. */
|
|
|
|
|
for (idx=0; idx < hosttable_size; idx++)
|
|
|
|
|
if (!hosttable[idx])
|
|
|
|
|
{
|
|
|
|
|
hosttable[idx] = hi;
|
|
|
|
|
return idx;
|
|
|
|
|
}
|
|
|
|
|
/* Need to extend the hosttable. */
|
|
|
|
|
newsize = hosttable_size + INITIAL_HOSTTABLE_SIZE;
|
|
|
|
|
newtable = xtryrealloc (hosttable, newsize * sizeof *hosttable);
|
|
|
|
|
if (!newtable)
|
|
|
|
|
{
|
|
|
|
|
xfree (hi);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
hosttable = newtable;
|
|
|
|
|
idx = hosttable_size;
|
|
|
|
|
hosttable_size = newsize;
|
|
|
|
|
rc = idx;
|
|
|
|
|
hosttable[idx++] = hi;
|
|
|
|
|
while (idx < hosttable_size)
|
|
|
|
|
hosttable[idx++] = NULL;
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Find the host NAME in our table. Return the index into the
|
|
|
|
|
hosttable or -1 if not found. */
|
|
|
|
|
static int
|
|
|
|
|
find_hostinfo (const char *name)
|
|
|
|
|
{
|
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
|
|
for (idx=0; idx < hosttable_size; idx++)
|
|
|
|
|
if (hosttable[idx] && !ascii_strcasecmp (hosttable[idx]->name, name))
|
|
|
|
|
return idx;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
sort_hostpool (const void *xa, const void *xb)
|
|
|
|
|
{
|
|
|
|
|
int a = *(int *)xa;
|
|
|
|
|
int b = *(int *)xb;
|
|
|
|
|
|
|
|
|
|
assert (a >= 0 && a < hosttable_size);
|
|
|
|
|
assert (b >= 0 && b < hosttable_size);
|
|
|
|
|
assert (hosttable[a]);
|
|
|
|
|
assert (hosttable[b]);
|
|
|
|
|
|
|
|
|
|
return ascii_strcasecmp (hosttable[a]->name, hosttable[b]->name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-06-20 14:54:17 +02:00
|
|
|
|
/* Return true if the host with the hosttable index TBLIDX is in HI->pool. */
|
2014-03-11 16:19:41 +01:00
|
|
|
|
static int
|
2017-06-20 14:54:17 +02:00
|
|
|
|
host_in_pool_p (hostinfo_t hi, int tblidx)
|
2014-03-11 16:19:41 +01:00
|
|
|
|
{
|
|
|
|
|
int i, pidx;
|
|
|
|
|
|
2017-06-20 14:54:17 +02:00
|
|
|
|
for (i = 0; i < hi->pool_len && (pidx = hi->pool[i]) != -1; i++)
|
2014-03-11 16:19:41 +01:00
|
|
|
|
if (pidx == tblidx && hosttable[pidx])
|
|
|
|
|
return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-06-20 14:54:17 +02:00
|
|
|
|
/* Select a random host. Consult HI->pool which indices into the global
|
|
|
|
|
hosttable. Returns index into HI->pool or -1 if no host could be
|
2011-04-12 16:30:08 +02:00
|
|
|
|
selected. */
|
|
|
|
|
static int
|
2017-06-20 14:54:17 +02:00
|
|
|
|
select_random_host (hostinfo_t hi)
|
2011-04-12 16:30:08 +02:00
|
|
|
|
{
|
|
|
|
|
int *tbl;
|
|
|
|
|
size_t tblsize;
|
|
|
|
|
int pidx, idx;
|
|
|
|
|
|
2014-03-11 14:26:39 +01:00
|
|
|
|
/* We create a new table so that we randomly select only from
|
|
|
|
|
currently alive hosts. */
|
2017-06-20 14:54:17 +02:00
|
|
|
|
for (idx = 0, tblsize = 0;
|
|
|
|
|
idx < hi->pool_len && (pidx = hi->pool[idx]) != -1;
|
|
|
|
|
idx++)
|
2011-04-12 16:30:08 +02:00
|
|
|
|
if (hosttable[pidx] && !hosttable[pidx]->dead)
|
|
|
|
|
tblsize++;
|
|
|
|
|
if (!tblsize)
|
|
|
|
|
return -1; /* No hosts. */
|
|
|
|
|
|
|
|
|
|
tbl = xtrymalloc (tblsize * sizeof *tbl);
|
|
|
|
|
if (!tbl)
|
|
|
|
|
return -1;
|
2017-06-20 14:54:17 +02:00
|
|
|
|
for (idx = 0, tblsize = 0;
|
|
|
|
|
idx < hi->pool_len && (pidx = hi->pool[idx]) != -1;
|
|
|
|
|
idx++)
|
2011-04-12 16:30:08 +02:00
|
|
|
|
if (hosttable[pidx] && !hosttable[pidx]->dead)
|
|
|
|
|
tbl[tblsize++] = pidx;
|
|
|
|
|
|
|
|
|
|
if (tblsize == 1) /* Save a get_uint_nonce. */
|
|
|
|
|
pidx = tbl[0];
|
|
|
|
|
else
|
2014-03-11 14:26:39 +01:00
|
|
|
|
pidx = tbl[get_uint_nonce () % tblsize];
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
|
|
|
|
xfree (tbl);
|
|
|
|
|
return pidx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-11-19 12:43:23 +01:00
|
|
|
|
/* Figure out if a set of DNS records looks like a pool. */
|
|
|
|
|
static int
|
|
|
|
|
arecords_is_pool (dns_addrinfo_t aibuf)
|
|
|
|
|
{
|
|
|
|
|
dns_addrinfo_t ai;
|
|
|
|
|
int n_v6, n_v4;
|
|
|
|
|
|
|
|
|
|
n_v6 = n_v4 = 0;
|
|
|
|
|
for (ai = aibuf; ai; ai = ai->next)
|
|
|
|
|
{
|
2015-11-23 13:59:01 +01:00
|
|
|
|
if (ai->family == AF_INET6)
|
2015-11-19 12:43:23 +01:00
|
|
|
|
n_v6++;
|
2015-11-23 13:59:01 +01:00
|
|
|
|
else if (ai->family == AF_INET)
|
2015-11-19 12:43:23 +01:00
|
|
|
|
n_v4++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return n_v6 > 1 || n_v4 > 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-06-20 13:34:12 +02:00
|
|
|
|
/* Print a warning iff Tor is not running but Tor has been requested.
|
2017-01-24 20:45:31 +01:00
|
|
|
|
* Also return true if it is not running. */
|
|
|
|
|
static int
|
|
|
|
|
tor_not_running_p (ctrl_t ctrl)
|
|
|
|
|
{
|
|
|
|
|
assuan_fd_t sock;
|
|
|
|
|
|
2017-02-01 17:54:14 +01:00
|
|
|
|
if (!dirmngr_use_tor ())
|
2017-01-24 20:45:31 +01:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
sock = assuan_sock_connect_byname (NULL, 0, 0, NULL, ASSUAN_SOCK_TOR);
|
|
|
|
|
if (sock != ASSUAN_INVALID_FD)
|
|
|
|
|
{
|
|
|
|
|
assuan_sock_close (sock);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_info ("(it seems Tor is not running)\n");
|
|
|
|
|
dirmngr_status (ctrl, "WARNING", "tor_not_running 0",
|
|
|
|
|
"Tor is enabled but the local Tor daemon"
|
|
|
|
|
" seems to be down", NULL);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-11-19 15:03:30 +01:00
|
|
|
|
/* Add the host AI under the NAME into the HOSTTABLE. If PORT is not
|
2017-06-20 16:27:59 +02:00
|
|
|
|
zero, it specifies which port to use to talk to the host for
|
|
|
|
|
PROTOCOL. If NAME specifies a pool (as indicated by IS_POOL),
|
|
|
|
|
update the given reference table accordingly. */
|
2015-11-19 13:00:59 +01:00
|
|
|
|
static void
|
2015-11-19 15:03:30 +01:00
|
|
|
|
add_host (const char *name, int is_pool,
|
2017-06-20 16:27:59 +02:00
|
|
|
|
const dns_addrinfo_t ai,
|
|
|
|
|
enum ks_protocol protocol, unsigned short port)
|
2015-11-19 13:00:59 +01:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t tmperr;
|
|
|
|
|
char *tmphost;
|
|
|
|
|
int idx, tmpidx;
|
2017-06-20 14:54:17 +02:00
|
|
|
|
hostinfo_t host;
|
2015-11-19 13:00:59 +01:00
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
idx = find_hostinfo (name);
|
2017-06-20 14:54:17 +02:00
|
|
|
|
host = hosttable[idx];
|
2015-11-19 13:00:59 +01:00
|
|
|
|
|
2017-02-15 17:03:57 +01:00
|
|
|
|
if (is_pool)
|
2015-11-19 13:00:59 +01:00
|
|
|
|
{
|
2017-02-15 17:03:57 +01:00
|
|
|
|
/* For a pool immediately convert the address to a string. */
|
|
|
|
|
tmperr = resolve_dns_addr (ai->addr, ai->addrlen,
|
|
|
|
|
(DNS_NUMERICHOST | DNS_WITHBRACKET), &tmphost);
|
|
|
|
|
}
|
|
|
|
|
else if (!is_ip_address (name))
|
|
|
|
|
{
|
|
|
|
|
/* This is a hostname. Use the name as given without going
|
|
|
|
|
* through resolve_dns_addr. */
|
2015-11-19 13:00:59 +01:00
|
|
|
|
tmphost = xtrystrdup (name);
|
|
|
|
|
if (!tmphost)
|
|
|
|
|
tmperr = gpg_error_from_syserror ();
|
|
|
|
|
else
|
|
|
|
|
tmperr = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-02-15 17:03:57 +01:00
|
|
|
|
/* Do a PTR lookup on AI. If a name was not found the function
|
2017-02-15 19:14:20 +01:00
|
|
|
|
* returns the numeric address (with brackets). */
|
2015-11-19 13:00:59 +01:00
|
|
|
|
tmperr = resolve_dns_addr (ai->addr, ai->addrlen,
|
|
|
|
|
DNS_WITHBRACKET, &tmphost);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tmperr)
|
|
|
|
|
{
|
|
|
|
|
log_info ("resolve_dns_addr failed while checking '%s': %s\n",
|
|
|
|
|
name, gpg_strerror (tmperr));
|
|
|
|
|
}
|
2017-06-20 14:54:17 +02:00
|
|
|
|
else if (host->pool_len + 1 >= MAX_POOL_SIZE)
|
2015-11-19 13:00:59 +01:00
|
|
|
|
{
|
|
|
|
|
log_error ("resolve_dns_addr for '%s': '%s'"
|
|
|
|
|
" [index table full - ignored]\n", name, tmphost);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!is_pool && is_ip_address (name))
|
|
|
|
|
/* Update the original entry. */
|
|
|
|
|
tmpidx = idx;
|
|
|
|
|
else
|
|
|
|
|
tmpidx = find_hostinfo (tmphost);
|
|
|
|
|
log_info ("resolve_dns_addr for '%s': '%s'%s\n",
|
|
|
|
|
name, tmphost,
|
|
|
|
|
tmpidx == -1? "" : " [already known]");
|
|
|
|
|
|
|
|
|
|
if (tmpidx == -1) /* Create a new entry. */
|
|
|
|
|
tmpidx = create_new_hostinfo (tmphost);
|
|
|
|
|
|
|
|
|
|
if (tmpidx == -1)
|
|
|
|
|
{
|
2017-02-15 17:03:57 +01:00
|
|
|
|
log_error ("map_host for '%s' problem: %s - '%s' [ignored]\n",
|
2015-11-19 13:00:59 +01:00
|
|
|
|
name, strerror (errno), tmphost);
|
|
|
|
|
}
|
|
|
|
|
else /* Set or update the entry. */
|
|
|
|
|
{
|
2015-11-19 15:03:30 +01:00
|
|
|
|
if (port)
|
2017-06-20 16:27:59 +02:00
|
|
|
|
hosttable[tmpidx]->port[protocol] = port;
|
2015-11-19 15:03:30 +01:00
|
|
|
|
|
2015-11-19 13:00:59 +01:00
|
|
|
|
if (ai->family == AF_INET6)
|
|
|
|
|
{
|
|
|
|
|
hosttable[tmpidx]->v6 = 1;
|
|
|
|
|
}
|
|
|
|
|
else if (ai->family == AF_INET)
|
|
|
|
|
{
|
|
|
|
|
hosttable[tmpidx]->v4 = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
BUG ();
|
|
|
|
|
|
2017-06-20 14:54:17 +02:00
|
|
|
|
/* If we updated the main entry, we're done. */
|
|
|
|
|
if (idx == tmpidx)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
/* If we updated an existing entry, we're done. */
|
|
|
|
|
for (i = 0; i < host->pool_len; i++)
|
|
|
|
|
if (host->pool[i] == tmpidx)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
/* Otherwise, we need to add it to the pool. Check if there
|
|
|
|
|
is space. */
|
|
|
|
|
if (host->pool_len + 1 > host->pool_size)
|
|
|
|
|
{
|
|
|
|
|
int *new_pool;
|
|
|
|
|
size_t new_size;
|
|
|
|
|
|
|
|
|
|
if (host->pool_size == 0)
|
|
|
|
|
new_size = 4;
|
|
|
|
|
else
|
|
|
|
|
new_size = host->pool_size * 2;
|
|
|
|
|
|
|
|
|
|
new_pool = xtryrealloc (host->pool,
|
|
|
|
|
new_size * sizeof *new_pool);
|
|
|
|
|
|
|
|
|
|
if (new_pool == NULL)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
host->pool = new_pool;
|
|
|
|
|
host->pool_size = new_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Finally, add it. */
|
|
|
|
|
log_assert (host->pool_len < host->pool_size);
|
|
|
|
|
host->pool[host->pool_len++] = tmpidx;
|
2015-11-19 13:00:59 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-20 14:54:17 +02:00
|
|
|
|
leave:
|
2015-11-19 13:00:59 +01:00
|
|
|
|
xfree (tmphost);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-06-20 14:54:17 +02:00
|
|
|
|
/* Sort the pool of the given hostinfo HI. */
|
|
|
|
|
static void
|
|
|
|
|
hostinfo_sort_pool (hostinfo_t hi)
|
|
|
|
|
{
|
|
|
|
|
qsort (hi->pool, hi->pool_len, sizeof *hi->pool, sort_hostpool);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-12 16:30:08 +02:00
|
|
|
|
/* Map the host name NAME to the actual to be used host name. This
|
2017-01-09 10:11:20 +01:00
|
|
|
|
* allows us to manage round robin DNS names. We use our own strategy
|
|
|
|
|
* to choose one of the hosts. For example we skip those hosts which
|
|
|
|
|
* failed for some time and we stick to one host for a time
|
|
|
|
|
* independent of DNS retry times. If FORCE_RESELECT is true a new
|
2017-01-09 10:42:30 +01:00
|
|
|
|
* host is always selected. If SRVTAG is NULL no service record
|
|
|
|
|
* lookup will be done, if it is set that service name is used. The
|
|
|
|
|
* selected host is stored as a malloced string at R_HOST; on error
|
|
|
|
|
* NULL is stored. If we know the port used by the selected host from
|
|
|
|
|
* a service record, a string representation is written to R_PORTSTR,
|
|
|
|
|
* otherwise it is left untouched. If R_HTTPFLAGS is not NULL it will
|
2017-01-12 21:09:42 +01:00
|
|
|
|
* receive flags which are to be passed to http_open. If R_HTTPHOST
|
|
|
|
|
* is not NULL a malloced name of the host is stored there; this might
|
|
|
|
|
* be different from R_HOST in case it has been selected from a
|
|
|
|
|
* pool. */
|
2014-12-08 11:13:17 +01:00
|
|
|
|
static gpg_error_t
|
2017-01-09 10:42:30 +01:00
|
|
|
|
map_host (ctrl_t ctrl, const char *name, const char *srvtag, int force_reselect,
|
2017-06-20 16:27:59 +02:00
|
|
|
|
enum ks_protocol protocol, char **r_host, char *r_portstr,
|
2017-01-12 21:09:42 +01:00
|
|
|
|
unsigned int *r_httpflags, char **r_httphost)
|
2011-04-12 16:30:08 +02:00
|
|
|
|
{
|
2015-01-08 12:14:13 +09:00
|
|
|
|
gpg_error_t err = 0;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
hostinfo_t hi;
|
|
|
|
|
int idx;
|
2017-06-20 16:27:59 +02:00
|
|
|
|
dns_addrinfo_t aibuf, ai;
|
|
|
|
|
int is_pool;
|
|
|
|
|
int new_hosts = 0;
|
|
|
|
|
char *cname;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
2014-12-08 11:13:17 +01:00
|
|
|
|
*r_host = NULL;
|
2014-03-14 17:00:10 +01:00
|
|
|
|
if (r_httpflags)
|
|
|
|
|
*r_httpflags = 0;
|
2017-01-12 21:09:42 +01:00
|
|
|
|
if (r_httphost)
|
|
|
|
|
*r_httphost = NULL;
|
2014-03-14 17:00:10 +01:00
|
|
|
|
|
2011-04-12 16:30:08 +02:00
|
|
|
|
/* No hostname means localhost. */
|
|
|
|
|
if (!name || !*name)
|
2014-12-08 11:13:17 +01:00
|
|
|
|
{
|
|
|
|
|
*r_host = xtrystrdup ("localhost");
|
|
|
|
|
return *r_host? 0 : gpg_error_from_syserror ();
|
|
|
|
|
}
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
|
|
|
|
/* See whether the host is in our table. */
|
|
|
|
|
idx = find_hostinfo (name);
|
2017-06-20 16:27:59 +02:00
|
|
|
|
if (idx == -1)
|
2015-10-26 16:32:03 +01:00
|
|
|
|
{
|
|
|
|
|
idx = create_new_hostinfo (name);
|
|
|
|
|
if (idx == -1)
|
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
hi = hosttable[idx];
|
2017-06-20 16:27:59 +02:00
|
|
|
|
hi->onion = is_onion_address (name);
|
2015-10-26 16:32:03 +01:00
|
|
|
|
}
|
2017-06-20 16:27:59 +02:00
|
|
|
|
else
|
|
|
|
|
hi = hosttable[idx];
|
|
|
|
|
|
|
|
|
|
is_pool = hi->pool != NULL;
|
|
|
|
|
|
|
|
|
|
if (srvtag && !is_ip_address (name)
|
|
|
|
|
&& ! hi->onion
|
|
|
|
|
&& ! (hi->did_srv_lookup & 1 << protocol))
|
2011-04-12 16:30:08 +02:00
|
|
|
|
{
|
2015-11-19 14:29:36 +01:00
|
|
|
|
struct srventry *srvs;
|
2016-12-19 23:27:02 +01:00
|
|
|
|
unsigned int srvscount;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
2017-06-20 16:27:59 +02:00
|
|
|
|
/* Check for SRV records. */
|
|
|
|
|
err = get_dns_srv (name, srvtag, NULL, &srvs, &srvscount);
|
|
|
|
|
if (err)
|
2011-04-12 16:30:08 +02:00
|
|
|
|
{
|
2017-06-20 16:27:59 +02:00
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_ECONNREFUSED)
|
|
|
|
|
tor_not_running_p (ctrl);
|
2015-01-08 12:14:13 +09:00
|
|
|
|
return err;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 16:27:59 +02:00
|
|
|
|
if (srvscount > 0)
|
2015-11-19 14:29:36 +01:00
|
|
|
|
{
|
2017-06-20 16:27:59 +02:00
|
|
|
|
int i;
|
|
|
|
|
if (! is_pool)
|
|
|
|
|
is_pool = srvscount > 1;
|
2015-11-19 14:29:36 +01:00
|
|
|
|
|
2017-06-20 16:27:59 +02:00
|
|
|
|
for (i = 0; i < srvscount; i++)
|
2016-11-10 11:38:42 +01:00
|
|
|
|
{
|
2017-06-20 16:27:59 +02:00
|
|
|
|
err = resolve_dns_name (srvs[i].target, 0,
|
|
|
|
|
AF_UNSPEC, SOCK_STREAM,
|
|
|
|
|
&ai, &cname);
|
|
|
|
|
if (err)
|
|
|
|
|
continue;
|
|
|
|
|
dirmngr_tick (ctrl);
|
|
|
|
|
add_host (name, is_pool, ai, protocol, srvs[i].port);
|
|
|
|
|
new_hosts = 1;
|
2016-11-10 11:38:42 +01:00
|
|
|
|
}
|
2017-06-20 16:27:59 +02:00
|
|
|
|
|
|
|
|
|
xfree (srvs);
|
2015-11-19 14:29:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 16:27:59 +02:00
|
|
|
|
hi->did_srv_lookup |= 1 << protocol;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! hi->did_a_lookup
|
|
|
|
|
&& ! hi->onion)
|
|
|
|
|
{
|
2011-04-12 16:30:08 +02:00
|
|
|
|
/* Find all A records for this entry and put them into the pool
|
|
|
|
|
list - if any. */
|
2015-10-21 17:46:21 +02:00
|
|
|
|
err = resolve_dns_name (name, 0, 0, SOCK_STREAM, &aibuf, &cname);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("resolving '%s' failed: %s\n", name, gpg_strerror (err));
|
|
|
|
|
err = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
2011-04-12 16:30:08 +02:00
|
|
|
|
{
|
2014-05-16 21:14:03 +02:00
|
|
|
|
/* First figure out whether this is a pool. For a pool we
|
2015-10-02 11:31:45 +02:00
|
|
|
|
use a different strategy than for a plain server: We use
|
2014-05-16 21:14:03 +02:00
|
|
|
|
the canonical name of the pool as the virtual host along
|
|
|
|
|
with the IP addresses. If it is not a pool, we use the
|
|
|
|
|
specified name. */
|
2015-11-19 14:29:36 +01:00
|
|
|
|
if (! is_pool)
|
|
|
|
|
is_pool = arecords_is_pool (aibuf);
|
2015-10-21 17:46:21 +02:00
|
|
|
|
if (is_pool && cname)
|
|
|
|
|
{
|
|
|
|
|
hi->cname = cname;
|
|
|
|
|
cname = NULL;
|
|
|
|
|
}
|
2014-05-16 21:14:03 +02:00
|
|
|
|
|
2015-10-21 17:46:21 +02:00
|
|
|
|
for (ai = aibuf; ai; ai = ai->next)
|
2014-05-16 21:14:03 +02:00
|
|
|
|
{
|
2015-10-21 17:46:21 +02:00
|
|
|
|
if (ai->family != AF_INET && ai->family != AF_INET6)
|
2011-04-12 16:30:08 +02:00
|
|
|
|
continue;
|
2017-01-24 16:36:28 +01:00
|
|
|
|
if (opt.disable_ipv4 && ai->family == AF_INET)
|
|
|
|
|
continue;
|
2017-04-03 20:56:12 +02:00
|
|
|
|
if (opt.disable_ipv6 && ai->family == AF_INET6)
|
|
|
|
|
continue;
|
2014-03-12 14:32:34 +01:00
|
|
|
|
dirmngr_tick (ctrl);
|
2014-05-16 21:14:03 +02:00
|
|
|
|
|
2017-06-20 16:27:59 +02:00
|
|
|
|
add_host (name, is_pool, ai, 0, 0);
|
|
|
|
|
new_hosts = 1;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
2017-06-20 16:27:59 +02:00
|
|
|
|
|
|
|
|
|
hi->did_a_lookup = 1;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
2015-10-21 17:46:21 +02:00
|
|
|
|
xfree (cname);
|
|
|
|
|
free_dns_addrinfo (aibuf);
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
2017-06-20 16:27:59 +02:00
|
|
|
|
if (new_hosts)
|
|
|
|
|
hostinfo_sort_pool (hi);
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
|
|
|
|
if (hi->pool)
|
|
|
|
|
{
|
2015-03-19 15:37:05 +01:00
|
|
|
|
/* Deal with the pool name before selecting a host. */
|
2017-01-12 21:09:42 +01:00
|
|
|
|
if (r_httphost)
|
2015-03-19 15:37:05 +01:00
|
|
|
|
{
|
2017-01-12 21:09:42 +01:00
|
|
|
|
*r_httphost = xtrystrdup (hi->cname? hi->cname : hi->name);
|
|
|
|
|
if (!*r_httphost)
|
2015-03-19 15:37:05 +01:00
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-12 16:30:08 +02:00
|
|
|
|
/* If the currently selected host is now marked dead, force a
|
|
|
|
|
re-selection . */
|
2014-03-11 14:26:39 +01:00
|
|
|
|
if (force_reselect)
|
|
|
|
|
hi->poolidx = -1;
|
|
|
|
|
else if (hi->poolidx >= 0 && hi->poolidx < hosttable_size
|
|
|
|
|
&& hosttable[hi->poolidx] && hosttable[hi->poolidx]->dead)
|
2011-04-12 16:30:08 +02:00
|
|
|
|
hi->poolidx = -1;
|
|
|
|
|
|
|
|
|
|
/* Select a host if needed. */
|
|
|
|
|
if (hi->poolidx == -1)
|
|
|
|
|
{
|
2017-06-20 14:54:17 +02:00
|
|
|
|
hi->poolidx = select_random_host (hi);
|
2011-04-12 16:30:08 +02:00
|
|
|
|
if (hi->poolidx == -1)
|
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
|
log_error ("no alive host found in pool '%s'\n", name);
|
2017-01-12 21:09:42 +01:00
|
|
|
|
if (r_httphost)
|
2015-03-19 15:37:05 +01:00
|
|
|
|
{
|
2017-01-12 21:09:42 +01:00
|
|
|
|
xfree (*r_httphost);
|
|
|
|
|
*r_httphost = NULL;
|
2015-03-19 15:37:05 +01:00
|
|
|
|
}
|
2014-12-08 11:13:17 +01:00
|
|
|
|
return gpg_error (GPG_ERR_NO_KEYSERVER);
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert (hi->poolidx >= 0 && hi->poolidx < hosttable_size);
|
|
|
|
|
hi = hosttable[hi->poolidx];
|
|
|
|
|
assert (hi);
|
|
|
|
|
}
|
2017-01-16 19:04:58 +01:00
|
|
|
|
else if (r_httphost && is_ip_address (hi->name))
|
|
|
|
|
{
|
|
|
|
|
/* This is a numerical IP address and not a pool. We want to
|
|
|
|
|
* find the canonical name so that it can be used in the HTTP
|
|
|
|
|
* Host header. Fixme: We should store that name in the
|
|
|
|
|
* hosttable. */
|
|
|
|
|
char *host;
|
|
|
|
|
|
|
|
|
|
err = resolve_dns_name (hi->name, 0, 0, SOCK_STREAM, &aibuf, NULL);
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
|
|
|
|
for (ai = aibuf; ai; ai = ai->next)
|
|
|
|
|
{
|
2017-04-03 20:56:12 +02:00
|
|
|
|
if ((!opt.disable_ipv6 && ai->family == AF_INET6)
|
2017-01-24 16:36:28 +01:00
|
|
|
|
|| (!opt.disable_ipv4 && ai->family == AF_INET))
|
2017-01-16 19:04:58 +01:00
|
|
|
|
{
|
|
|
|
|
err = resolve_dns_addr (ai->addr, ai->addrlen, 0, &host);
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
|
|
|
|
/* Okay, we return the first found name. */
|
|
|
|
|
*r_httphost = host;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free_dns_addrinfo (aibuf);
|
|
|
|
|
}
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
|
|
|
|
if (hi->dead)
|
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
|
log_error ("host '%s' marked as dead\n", hi->name);
|
2017-01-12 21:09:42 +01:00
|
|
|
|
if (r_httphost)
|
2015-03-19 15:37:05 +01:00
|
|
|
|
{
|
2017-01-12 21:09:42 +01:00
|
|
|
|
xfree (*r_httphost);
|
|
|
|
|
*r_httphost = NULL;
|
2015-03-19 15:37:05 +01:00
|
|
|
|
}
|
2014-12-08 11:13:17 +01:00
|
|
|
|
return gpg_error (GPG_ERR_NO_KEYSERVER);
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-14 17:00:10 +01:00
|
|
|
|
if (r_httpflags)
|
|
|
|
|
{
|
|
|
|
|
/* If the hosttable does not indicate that a certain host
|
|
|
|
|
supports IPv<N>, we explicit set the corresponding http
|
|
|
|
|
flags. The reason for this is that a host might be listed in
|
|
|
|
|
a pool as not v6 only but actually support v6 when later
|
2014-05-16 21:14:03 +02:00
|
|
|
|
the name is resolved by our http layer. */
|
2014-03-14 17:00:10 +01:00
|
|
|
|
if (!hi->v4)
|
|
|
|
|
*r_httpflags |= HTTP_FLAG_IGNORE_IPv4;
|
|
|
|
|
if (!hi->v6)
|
|
|
|
|
*r_httpflags |= HTTP_FLAG_IGNORE_IPv6;
|
2015-10-26 16:32:03 +01:00
|
|
|
|
|
|
|
|
|
/* Note that we do not set the HTTP_FLAG_FORCE_TOR for onion
|
|
|
|
|
addresses because the http module detects this itself. This
|
|
|
|
|
also allows us to use an onion address without Tor mode being
|
|
|
|
|
enabled. */
|
2014-03-14 17:00:10 +01:00
|
|
|
|
}
|
2014-05-16 21:14:03 +02:00
|
|
|
|
|
2014-12-08 11:13:17 +01:00
|
|
|
|
*r_host = xtrystrdup (hi->name);
|
|
|
|
|
if (!*r_host)
|
|
|
|
|
{
|
2015-01-08 12:14:13 +09:00
|
|
|
|
err = gpg_error_from_syserror ();
|
2017-01-12 21:09:42 +01:00
|
|
|
|
if (r_httphost)
|
2014-12-08 11:13:17 +01:00
|
|
|
|
{
|
2017-01-12 21:09:42 +01:00
|
|
|
|
xfree (*r_httphost);
|
|
|
|
|
*r_httphost = NULL;
|
2014-12-08 11:13:17 +01:00
|
|
|
|
}
|
2015-01-08 12:14:13 +09:00
|
|
|
|
return err;
|
2014-12-08 11:13:17 +01:00
|
|
|
|
}
|
2017-06-20 16:27:59 +02:00
|
|
|
|
if (hi->port[protocol])
|
2015-11-19 15:03:30 +01:00
|
|
|
|
snprintf (r_portstr, 6 /* five digits and the sentinel */,
|
2017-06-20 16:27:59 +02:00
|
|
|
|
"%hu", hi->port[protocol]);
|
2014-12-08 11:13:17 +01:00
|
|
|
|
return 0;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
|
/* Mark the host NAME as dead. NAME may be given as an URL. Returns
|
|
|
|
|
true if a host was really marked as dead or was already marked dead
|
|
|
|
|
(e.g. by a concurrent session). */
|
|
|
|
|
static int
|
2011-04-12 16:30:08 +02:00
|
|
|
|
mark_host_dead (const char *name)
|
|
|
|
|
{
|
2014-03-12 14:32:34 +01:00
|
|
|
|
const char *host;
|
|
|
|
|
char *host_buffer = NULL;
|
|
|
|
|
parsed_uri_t parsed_uri = NULL;
|
|
|
|
|
int done = 0;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
|
if (name && *name && !http_parse_uri (&parsed_uri, name, 1))
|
|
|
|
|
{
|
|
|
|
|
if (parsed_uri->v6lit)
|
|
|
|
|
{
|
|
|
|
|
host_buffer = strconcat ("[", parsed_uri->host, "]", NULL);
|
|
|
|
|
if (!host_buffer)
|
|
|
|
|
log_error ("out of core in mark_host_dead");
|
|
|
|
|
host = host_buffer;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
host = parsed_uri->host;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
host = name;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
|
if (host && *host && strcmp (host, "localhost"))
|
|
|
|
|
{
|
|
|
|
|
hostinfo_t hi;
|
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
|
|
idx = find_hostinfo (host);
|
|
|
|
|
if (idx != -1)
|
|
|
|
|
{
|
|
|
|
|
hi = hosttable[idx];
|
|
|
|
|
log_info ("marking host '%s' as dead%s\n",
|
|
|
|
|
hi->name, hi->dead? " (again)":"");
|
|
|
|
|
hi->dead = 1;
|
2014-03-18 11:07:05 +01:00
|
|
|
|
hi->died_at = gnupg_get_time ();
|
|
|
|
|
if (!hi->died_at)
|
|
|
|
|
hi->died_at = 1;
|
2014-03-12 14:32:34 +01:00
|
|
|
|
done = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http_release_parsed_uri (parsed_uri);
|
|
|
|
|
xfree (host_buffer);
|
|
|
|
|
return done;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-11 16:19:41 +01:00
|
|
|
|
/* Mark a host in the hosttable as dead or - if ALIVE is true - as
|
2014-03-14 16:12:54 +01:00
|
|
|
|
alive. */
|
2014-03-11 16:19:41 +01:00
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
hostinfo_t hi, hi2;
|
|
|
|
|
int idx, idx2, idx3, n;
|
|
|
|
|
|
|
|
|
|
if (!name || !*name || !strcmp (name, "localhost"))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
idx = find_hostinfo (name);
|
|
|
|
|
if (idx == -1)
|
|
|
|
|
return gpg_error (GPG_ERR_NOT_FOUND);
|
|
|
|
|
|
|
|
|
|
hi = hosttable[idx];
|
|
|
|
|
if (alive && hi->dead)
|
|
|
|
|
{
|
|
|
|
|
hi->dead = 0;
|
|
|
|
|
err = ks_printf_help (ctrl, "marking '%s' as alive", name);
|
|
|
|
|
}
|
|
|
|
|
else if (!alive && !hi->dead)
|
|
|
|
|
{
|
|
|
|
|
hi->dead = 1;
|
2014-03-18 11:07:05 +01:00
|
|
|
|
hi->died_at = 0; /* Manually set dead. */
|
2014-03-11 16:19:41 +01:00
|
|
|
|
err = ks_printf_help (ctrl, "marking '%s' as dead", name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If the host is a pool mark all member hosts. */
|
|
|
|
|
if (!err && hi->pool)
|
|
|
|
|
{
|
2017-06-20 14:54:17 +02:00
|
|
|
|
for (idx2 = 0;
|
|
|
|
|
!err && idx2 < hi->pool_len && (n = hi->pool[idx2]) != -1;
|
|
|
|
|
idx2++)
|
2014-03-11 16:19:41 +01:00
|
|
|
|
{
|
|
|
|
|
assert (n >= 0 && n < hosttable_size);
|
|
|
|
|
|
|
|
|
|
if (!alive)
|
|
|
|
|
{
|
|
|
|
|
/* Do not mark a host from a pool dead if it is also a
|
|
|
|
|
member in another pool. */
|
|
|
|
|
for (idx3=0; idx3 < hosttable_size; idx3++)
|
|
|
|
|
{
|
2014-12-22 12:34:57 +01:00
|
|
|
|
if (hosttable[idx3]
|
2014-03-11 16:19:41 +01:00
|
|
|
|
&& hosttable[idx3]->pool
|
|
|
|
|
&& idx3 != idx
|
2017-06-20 14:54:17 +02:00
|
|
|
|
&& host_in_pool_p (hosttable[idx3], n))
|
2014-03-11 16:19:41 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (idx3 < hosttable_size)
|
|
|
|
|
continue; /* Host is also a member of another pool. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hi2 = hosttable[n];
|
|
|
|
|
if (!hi2)
|
|
|
|
|
;
|
|
|
|
|
else if (alive && hi2->dead)
|
|
|
|
|
{
|
|
|
|
|
hi2->dead = 0;
|
|
|
|
|
err = ks_printf_help (ctrl, "marking '%s' as alive",
|
|
|
|
|
hi2->name);
|
|
|
|
|
}
|
|
|
|
|
else if (!alive && !hi2->dead)
|
|
|
|
|
{
|
|
|
|
|
hi2->dead = 1;
|
2014-03-18 11:07:05 +01:00
|
|
|
|
hi2->died_at = 0; /* Manually set dead. */
|
2014-03-11 16:19:41 +01:00
|
|
|
|
err = ks_printf_help (ctrl, "marking '%s' as dead",
|
|
|
|
|
hi2->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-04-12 16:30:08 +02:00
|
|
|
|
/* Debug function to print the entire hosttable. */
|
2014-03-11 14:26:39 +01:00
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_hkp_print_hosttable (ctrl_t ctrl)
|
2011-04-12 16:30:08 +02:00
|
|
|
|
{
|
2014-03-11 14:26:39 +01:00
|
|
|
|
gpg_error_t err;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
int idx, idx2;
|
|
|
|
|
hostinfo_t hi;
|
2014-03-11 14:26:39 +01:00
|
|
|
|
membuf_t mb;
|
2014-03-18 11:07:05 +01:00
|
|
|
|
time_t curtime;
|
|
|
|
|
char *p, *died;
|
|
|
|
|
const char *diedstr;
|
2014-03-11 14:26:39 +01:00
|
|
|
|
|
2014-05-16 21:14:03 +02:00
|
|
|
|
err = ks_print_help (ctrl, "hosttable (idx, ipv6, ipv4, dead, name, time):");
|
2014-03-11 14:26:39 +01:00
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
2017-02-15 19:14:20 +01:00
|
|
|
|
/* FIXME: We need a lock for the hosttable. */
|
2014-03-18 11:07:05 +01:00
|
|
|
|
curtime = gnupg_get_time ();
|
2011-04-12 16:30:08 +02:00
|
|
|
|
for (idx=0; idx < hosttable_size; idx++)
|
|
|
|
|
if ((hi=hosttable[idx]))
|
|
|
|
|
{
|
2014-03-18 11:07:05 +01:00
|
|
|
|
if (hi->dead && hi->died_at)
|
|
|
|
|
{
|
|
|
|
|
died = elapsed_time_string (hi->died_at, curtime);
|
|
|
|
|
diedstr = died? died : "error";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
diedstr = died = NULL;
|
2017-02-15 19:14:20 +01:00
|
|
|
|
|
|
|
|
|
if (!hi->iporname_valid)
|
|
|
|
|
{
|
|
|
|
|
char *canon = NULL;
|
|
|
|
|
|
|
|
|
|
xfree (hi->iporname);
|
|
|
|
|
hi->iporname = NULL;
|
|
|
|
|
|
|
|
|
|
/* Do a lookup just for the display purpose. */
|
|
|
|
|
if (hi->onion || hi->pool)
|
|
|
|
|
;
|
|
|
|
|
else if (is_ip_address (hi->name))
|
|
|
|
|
{
|
|
|
|
|
dns_addrinfo_t aibuf, ai;
|
|
|
|
|
|
|
|
|
|
/* Turn the numerical IP address string into an AI and
|
|
|
|
|
* then do a DNS PTR lookup. */
|
|
|
|
|
if (!resolve_dns_name (hi->name, 0, 0,
|
|
|
|
|
SOCK_STREAM,
|
|
|
|
|
&aibuf, &canon))
|
|
|
|
|
{
|
|
|
|
|
if (canon && is_ip_address (canon))
|
|
|
|
|
{
|
|
|
|
|
xfree (canon);
|
|
|
|
|
canon = NULL;
|
|
|
|
|
}
|
|
|
|
|
for (ai = aibuf; !canon && ai; ai = ai->next)
|
|
|
|
|
{
|
|
|
|
|
resolve_dns_addr (ai->addr, ai->addrlen,
|
|
|
|
|
DNS_WITHBRACKET, &canon);
|
|
|
|
|
if (canon && is_ip_address (canon))
|
|
|
|
|
{
|
|
|
|
|
/* We already have the numeric IP - no need to
|
|
|
|
|
* display it a second time. */
|
|
|
|
|
xfree (canon);
|
|
|
|
|
canon = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free_dns_addrinfo (aibuf);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dns_addrinfo_t aibuf, ai;
|
|
|
|
|
|
|
|
|
|
/* Get the IP address as a string from a name. Note
|
|
|
|
|
* that resolve_dns_addr allocates CANON on success
|
|
|
|
|
* and thus terminates the loop. */
|
|
|
|
|
if (!resolve_dns_name (hi->name, 0,
|
|
|
|
|
hi->v6? AF_INET6 : AF_INET,
|
|
|
|
|
SOCK_STREAM,
|
|
|
|
|
&aibuf, NULL))
|
|
|
|
|
{
|
|
|
|
|
for (ai = aibuf; !canon && ai; ai = ai->next)
|
|
|
|
|
{
|
|
|
|
|
resolve_dns_addr (ai->addr, ai->addrlen,
|
|
|
|
|
DNS_NUMERICHOST|DNS_WITHBRACKET,
|
|
|
|
|
&canon);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free_dns_addrinfo (aibuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hi->iporname = canon;
|
|
|
|
|
hi->iporname_valid = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = ks_printf_help (ctrl, "%3d %s %s %s %s%s%s%s%s%s%s\n",
|
2015-10-26 16:32:03 +01:00
|
|
|
|
idx,
|
|
|
|
|
hi->onion? "O" : hi->v6? "6":" ",
|
|
|
|
|
hi->v4? "4":" ",
|
2014-05-16 21:14:03 +02:00
|
|
|
|
hi->dead? "d":" ",
|
|
|
|
|
hi->name,
|
2017-02-15 19:14:20 +01:00
|
|
|
|
hi->iporname? " (":"",
|
|
|
|
|
hi->iporname? hi->iporname : "",
|
|
|
|
|
hi->iporname? ")":"",
|
2014-03-18 11:07:05 +01:00
|
|
|
|
diedstr? " (":"",
|
|
|
|
|
diedstr? diedstr:"",
|
|
|
|
|
diedstr? ")":"" );
|
|
|
|
|
xfree (died);
|
2014-05-16 21:14:03 +02:00
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
2014-03-18 11:07:05 +01:00
|
|
|
|
|
2014-05-16 21:14:03 +02:00
|
|
|
|
if (hi->cname)
|
|
|
|
|
err = ks_printf_help (ctrl, " . %s", hi->cname);
|
2014-03-11 14:26:39 +01:00
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
2014-05-16 21:14:03 +02:00
|
|
|
|
|
2011-04-12 16:30:08 +02:00
|
|
|
|
if (hi->pool)
|
|
|
|
|
{
|
2014-03-11 14:26:39 +01:00
|
|
|
|
init_membuf (&mb, 256);
|
|
|
|
|
put_membuf_printf (&mb, " . -->");
|
2017-06-20 14:54:17 +02:00
|
|
|
|
for (idx2 = 0; idx2 < hi->pool_len && hi->pool[idx2] != -1; idx2++)
|
2011-04-12 16:30:08 +02:00
|
|
|
|
{
|
2014-03-11 14:26:39 +01:00
|
|
|
|
put_membuf_printf (&mb, " %d", hi->pool[idx2]);
|
|
|
|
|
if (hi->poolidx == hi->pool[idx2])
|
|
|
|
|
put_membuf_printf (&mb, "*");
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
2014-03-11 14:26:39 +01:00
|
|
|
|
put_membuf( &mb, "", 1);
|
|
|
|
|
p = get_membuf (&mb, NULL);
|
|
|
|
|
if (!p)
|
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
err = ks_print_help (ctrl, p);
|
|
|
|
|
xfree (p);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-11 14:26:39 +01:00
|
|
|
|
return 0;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-09 17:48:00 +01:00
|
|
|
|
/* Print a help output for the schemata supported by this module. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_hkp_help (ctrl_t ctrl, parsed_uri_t uri)
|
|
|
|
|
{
|
2017-01-23 16:32:44 +01:00
|
|
|
|
const char data[] =
|
2011-02-09 17:48:00 +01:00
|
|
|
|
"Handler for HKP URLs:\n"
|
|
|
|
|
" hkp://\n"
|
2014-11-25 19:24:18 +01:00
|
|
|
|
#if HTTP_USE_GNUTLS || HTTP_USE_NTBTLS
|
2014-05-05 16:09:45 +02:00
|
|
|
|
" hkps://\n"
|
2014-11-25 19:24:18 +01:00
|
|
|
|
#endif
|
2011-02-09 17:48:00 +01:00
|
|
|
|
"Supported methods: search, get, put\n";
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
|
2014-11-25 19:24:18 +01:00
|
|
|
|
#if HTTP_USE_GNUTLS || HTTP_USE_NTBTLS
|
|
|
|
|
const char data2[] = " hkp\n hkps";
|
|
|
|
|
#else
|
|
|
|
|
const char data2[] = " hkp";
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-02-09 17:48:00 +01:00
|
|
|
|
if (!uri)
|
2014-11-25 19:24:18 +01:00
|
|
|
|
err = ks_print_help (ctrl, data2);
|
2014-05-05 16:09:45 +02:00
|
|
|
|
else if (uri->is_http && (!strcmp (uri->scheme, "hkp")
|
|
|
|
|
|| !strcmp (uri->scheme, "hkps")))
|
2011-02-09 17:48:00 +01:00
|
|
|
|
err = ks_print_help (ctrl, data);
|
|
|
|
|
else
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-10 14:30:17 +01:00
|
|
|
|
|
2014-05-16 21:14:03 +02:00
|
|
|
|
/* Build the remote part of the URL from SCHEME, HOST and an optional
|
2017-01-09 10:11:20 +01:00
|
|
|
|
* PORT. If NO_SRV is set no SRV record lookup will be done. Returns
|
2017-01-12 21:09:42 +01:00
|
|
|
|
* an allocated string at R_HOSTPORT or NULL on failure. If
|
|
|
|
|
* R_HTTPHOST is not NULL it receives a malloced string with the
|
|
|
|
|
* hostname; this may be different from HOST if HOST is selected from
|
|
|
|
|
* a pool. */
|
2014-12-08 11:13:17 +01:00
|
|
|
|
static gpg_error_t
|
2014-03-12 14:32:34 +01:00
|
|
|
|
make_host_part (ctrl_t ctrl,
|
|
|
|
|
const char *scheme, const char *host, unsigned short port,
|
2017-01-09 10:11:20 +01:00
|
|
|
|
int force_reselect, int no_srv,
|
2017-01-12 21:09:42 +01:00
|
|
|
|
char **r_hostport, unsigned int *r_httpflags, char **r_httphost)
|
2011-04-12 16:30:08 +02:00
|
|
|
|
{
|
2014-12-08 11:13:17 +01:00
|
|
|
|
gpg_error_t err;
|
2017-01-09 10:42:30 +01:00
|
|
|
|
const char *srvtag;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
char portstr[10];
|
|
|
|
|
char *hostname;
|
2017-06-20 16:27:59 +02:00
|
|
|
|
enum ks_protocol protocol;
|
2014-12-08 11:13:17 +01:00
|
|
|
|
|
|
|
|
|
*r_hostport = NULL;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
|
|
|
|
if (!strcmp (scheme, "hkps") || !strcmp (scheme,"https"))
|
|
|
|
|
{
|
|
|
|
|
scheme = "https";
|
2017-01-09 10:42:30 +01:00
|
|
|
|
srvtag = no_srv? NULL : "pgpkey-https";
|
2017-06-20 16:27:59 +02:00
|
|
|
|
protocol = KS_PROTOCOL_HKPS;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
|
|
|
|
else /* HKP or HTTP. */
|
|
|
|
|
{
|
|
|
|
|
scheme = "http";
|
2017-01-09 10:42:30 +01:00
|
|
|
|
srvtag = no_srv? NULL : "pgpkey-http";
|
2017-06-20 16:27:59 +02:00
|
|
|
|
protocol = KS_PROTOCOL_HKP;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-09 10:42:30 +01:00
|
|
|
|
portstr[0] = 0;
|
2017-06-20 16:27:59 +02:00
|
|
|
|
err = map_host (ctrl, host, srvtag, force_reselect, protocol,
|
2017-01-12 21:09:42 +01:00
|
|
|
|
&hostname, portstr, r_httpflags, r_httphost);
|
2017-01-09 10:42:30 +01:00
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
/* If map_host did not return a port (from a SRV record) but a port
|
|
|
|
|
* has been specified (implicitly or explicitly) then use that port.
|
|
|
|
|
* In the case that a port was not specified (which is probably a
|
|
|
|
|
* bug in https.c) we will set up defaults. */
|
|
|
|
|
if (*portstr)
|
|
|
|
|
;
|
|
|
|
|
else if (!*portstr && port)
|
|
|
|
|
snprintf (portstr, sizeof portstr, "%hu", port);
|
|
|
|
|
else if (!strcmp (scheme,"https"))
|
|
|
|
|
strcpy (portstr, "443");
|
|
|
|
|
else
|
|
|
|
|
strcpy (portstr, "11371");
|
|
|
|
|
|
2017-01-12 21:09:42 +01:00
|
|
|
|
if (*hostname != '[' && is_ip_address (hostname) == 6)
|
|
|
|
|
*r_hostport = strconcat (scheme, "://[", hostname, "]:", portstr, NULL);
|
|
|
|
|
else
|
|
|
|
|
*r_hostport = strconcat (scheme, "://", hostname, ":", portstr, NULL);
|
2011-04-12 16:30:08 +02:00
|
|
|
|
xfree (hostname);
|
2014-12-08 11:13:17 +01:00
|
|
|
|
if (!*r_hostport)
|
|
|
|
|
{
|
2017-01-12 21:09:42 +01:00
|
|
|
|
if (r_httphost)
|
2014-12-08 11:13:17 +01:00
|
|
|
|
{
|
2017-01-12 21:09:42 +01:00
|
|
|
|
xfree (*r_httphost);
|
|
|
|
|
*r_httphost = NULL;
|
2014-12-08 11:13:17 +01:00
|
|
|
|
}
|
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-11 14:26:39 +01:00
|
|
|
|
/* Resolve all known keyserver names and update the hosttable. This
|
|
|
|
|
is mainly useful for debugging because the resolving is anyway done
|
|
|
|
|
on demand. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_hkp_resolve (ctrl_t ctrl, parsed_uri_t uri)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
char *hostport = NULL;
|
|
|
|
|
|
2017-01-09 10:11:20 +01:00
|
|
|
|
/* NB: With an explicitly given port we do not want to consult a
|
|
|
|
|
* service record because that might be in conflict with the port
|
|
|
|
|
* from such a service record. */
|
|
|
|
|
err = make_host_part (ctrl, uri->scheme, uri->host, uri->port,
|
|
|
|
|
1, uri->explicit_port,
|
2014-12-08 11:13:17 +01:00
|
|
|
|
&hostport, NULL, NULL);
|
|
|
|
|
if (err)
|
2014-03-11 14:26:39 +01:00
|
|
|
|
{
|
|
|
|
|
err = ks_printf_help (ctrl, "%s://%s:%hu: resolve failed: %s",
|
|
|
|
|
uri->scheme, uri->host, uri->port,
|
|
|
|
|
gpg_strerror (err));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
err = ks_printf_help (ctrl, "%s", hostport);
|
|
|
|
|
xfree (hostport);
|
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-18 11:07:05 +01:00
|
|
|
|
/* Housekeeping function called from the housekeeping thread. It is
|
|
|
|
|
used to mark dead hosts alive so that they may be tried again after
|
|
|
|
|
some time. */
|
|
|
|
|
void
|
|
|
|
|
ks_hkp_housekeeping (time_t curtime)
|
|
|
|
|
{
|
|
|
|
|
int idx;
|
|
|
|
|
hostinfo_t hi;
|
|
|
|
|
|
|
|
|
|
for (idx=0; idx < hosttable_size; idx++)
|
|
|
|
|
{
|
|
|
|
|
hi = hosttable[idx];
|
|
|
|
|
if (!hi)
|
|
|
|
|
continue;
|
|
|
|
|
if (!hi->dead)
|
|
|
|
|
continue;
|
|
|
|
|
if (!hi->died_at)
|
|
|
|
|
continue; /* Do not resurrect manually shot hosts. */
|
|
|
|
|
if (hi->died_at + RESURRECT_INTERVAL <= curtime
|
|
|
|
|
|| hi->died_at > curtime)
|
|
|
|
|
{
|
|
|
|
|
hi->dead = 0;
|
|
|
|
|
log_info ("resurrected host '%s'", hi->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-23 13:07:54 +01:00
|
|
|
|
/* Reload (SIGHUP) action for this module. We mark all host alive
|
|
|
|
|
* even those which have been manually shot. */
|
|
|
|
|
void
|
|
|
|
|
ks_hkp_reload (void)
|
|
|
|
|
{
|
|
|
|
|
int idx, count;
|
|
|
|
|
hostinfo_t hi;
|
|
|
|
|
|
|
|
|
|
for (idx=count=0; idx < hosttable_size; idx++)
|
|
|
|
|
{
|
|
|
|
|
hi = hosttable[idx];
|
|
|
|
|
if (!hi)
|
|
|
|
|
continue;
|
2017-02-15 19:14:20 +01:00
|
|
|
|
hi->iporname_valid = 0;
|
2017-01-23 13:07:54 +01:00
|
|
|
|
if (!hi->dead)
|
|
|
|
|
continue;
|
|
|
|
|
hi->dead = 0;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if (count)
|
|
|
|
|
log_info ("number of resurrected hosts: %d", count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-01-18 12:51:16 +01:00
|
|
|
|
/* Send an HTTP request. On success returns an estream object at
|
2014-05-16 21:14:03 +02:00
|
|
|
|
R_FP. HOSTPORTSTR is only used for diagnostics. If HTTPHOST is
|
|
|
|
|
not NULL it will be used as HTTP "Host" header. If POST_CB is not
|
2011-01-20 14:12:53 +01:00
|
|
|
|
NULL a post request is used and that callback is called to allow
|
2015-12-04 12:32:20 +01:00
|
|
|
|
writing the post data. If R_HTTP_STATUS is not NULL, the http
|
|
|
|
|
status code will be stored there. */
|
2011-01-18 12:51:16 +01:00
|
|
|
|
static gpg_error_t
|
|
|
|
|
send_request (ctrl_t ctrl, const char *request, const char *hostportstr,
|
2014-05-16 21:14:03 +02:00
|
|
|
|
const char *httphost, unsigned int httpflags,
|
2011-01-20 14:12:53 +01:00
|
|
|
|
gpg_error_t (*post_cb)(void *, http_t), void *post_cb_value,
|
2015-12-04 12:32:20 +01:00
|
|
|
|
estream_t *r_fp, unsigned int *r_http_status)
|
2011-01-18 12:51:16 +01:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
2014-05-05 16:09:45 +02:00
|
|
|
|
http_session_t session = NULL;
|
2011-01-18 12:51:16 +01:00
|
|
|
|
http_t http = NULL;
|
|
|
|
|
int redirects_left = MAX_REDIRECTS;
|
|
|
|
|
estream_t fp = NULL;
|
|
|
|
|
char *request_buffer = NULL;
|
2017-07-19 16:02:05 +02:00
|
|
|
|
parsed_uri_t uri = NULL;
|
|
|
|
|
int is_onion;
|
2011-01-18 12:51:16 +01:00
|
|
|
|
|
|
|
|
|
*r_fp = NULL;
|
2011-11-24 15:48:24 +01:00
|
|
|
|
|
2017-07-19 16:02:05 +02:00
|
|
|
|
err = http_parse_uri (&uri, request, 0);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
is_onion = uri->onion;
|
|
|
|
|
|
2017-02-21 09:37:07 +01:00
|
|
|
|
err = http_session_new (&session, httphost,
|
|
|
|
|
((ctrl->http_no_crl? HTTP_FLAG_NO_CRL : 0)
|
|
|
|
|
| HTTP_FLAG_TRUST_DEF),
|
2017-02-19 10:36:43 +01:00
|
|
|
|
gnupg_http_tls_verify_cb, ctrl);
|
2014-05-05 16:09:45 +02:00
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
2014-05-19 09:48:42 +02:00
|
|
|
|
http_session_set_log_cb (session, cert_log_cb);
|
2017-06-08 09:30:48 +02:00
|
|
|
|
http_session_set_timeout (session, ctrl->timeout);
|
2014-05-05 16:09:45 +02:00
|
|
|
|
|
2011-01-18 12:51:16 +01:00
|
|
|
|
once_more:
|
2011-01-20 14:12:53 +01:00
|
|
|
|
err = http_open (&http,
|
|
|
|
|
post_cb? HTTP_REQ_POST : HTTP_REQ_GET,
|
|
|
|
|
request,
|
2014-05-16 21:14:03 +02:00
|
|
|
|
httphost,
|
2011-01-18 12:51:16 +01:00
|
|
|
|
/* fixme: AUTH */ NULL,
|
2015-09-18 16:17:11 +02:00
|
|
|
|
(httpflags
|
|
|
|
|
|(opt.honor_http_proxy? HTTP_FLAG_TRY_PROXY:0)
|
2017-02-01 17:54:14 +01:00
|
|
|
|
|(dirmngr_use_tor ()? HTTP_FLAG_FORCE_TOR:0)
|
2017-04-03 20:56:12 +02:00
|
|
|
|
|(opt.disable_ipv4? HTTP_FLAG_IGNORE_IPv4 : 0)
|
|
|
|
|
|(opt.disable_ipv6? HTTP_FLAG_IGNORE_IPv6 : 0)),
|
2015-04-21 17:14:53 +02:00
|
|
|
|
ctrl->http_proxy,
|
2014-05-05 16:09:45 +02:00
|
|
|
|
session,
|
|
|
|
|
NULL,
|
2011-01-18 12:51:16 +01:00
|
|
|
|
/*FIXME curl->srvtag*/NULL);
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
|
|
|
|
fp = http_get_write_ptr (http);
|
|
|
|
|
/* Avoid caches to get the most recent copy of the key. We set
|
|
|
|
|
both the Pragma and Cache-Control versions of the header, so
|
|
|
|
|
we're good with both HTTP 1.0 and 1.1. */
|
|
|
|
|
es_fputs ("Pragma: no-cache\r\n"
|
|
|
|
|
"Cache-Control: no-cache\r\n", fp);
|
2011-01-20 14:12:53 +01:00
|
|
|
|
if (post_cb)
|
|
|
|
|
err = post_cb (post_cb_value, http);
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
|
|
|
|
http_start_data (http);
|
|
|
|
|
if (es_ferror (fp))
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
}
|
2011-01-18 12:51:16 +01:00
|
|
|
|
}
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
/* Fixme: After a redirection we show the old host name. */
|
2012-06-05 19:29:22 +02:00
|
|
|
|
log_error (_("error connecting to '%s': %s\n"),
|
2011-01-18 12:51:16 +01:00
|
|
|
|
hostportstr, gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Wait for the response. */
|
|
|
|
|
dirmngr_tick (ctrl);
|
|
|
|
|
err = http_wait_response (http);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
|
log_error (_("error reading HTTP response for '%s': %s\n"),
|
2011-01-18 12:51:16 +01:00
|
|
|
|
hostportstr, gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-05 16:09:45 +02:00
|
|
|
|
if (http_get_tls_info (http, NULL))
|
|
|
|
|
{
|
|
|
|
|
/* Update the httpflags so that a redirect won't fallback to an
|
|
|
|
|
unencrypted connection. */
|
|
|
|
|
httpflags |= HTTP_FLAG_FORCE_TLS;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 12:32:20 +01:00
|
|
|
|
if (r_http_status)
|
|
|
|
|
*r_http_status = http_get_status_code (http);
|
|
|
|
|
|
2011-01-18 12:51:16 +01:00
|
|
|
|
switch (http_get_status_code (http))
|
|
|
|
|
{
|
|
|
|
|
case 200:
|
|
|
|
|
err = 0;
|
|
|
|
|
break; /* Success. */
|
|
|
|
|
|
|
|
|
|
case 301:
|
|
|
|
|
case 302:
|
2014-05-05 16:09:45 +02:00
|
|
|
|
case 307:
|
2011-01-18 12:51:16 +01:00
|
|
|
|
{
|
|
|
|
|
const char *s = http_get_header (http, "Location");
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2012-06-05 19:29:22 +02:00
|
|
|
|
log_info (_("URL '%s' redirected to '%s' (%u)\n"),
|
2011-01-18 12:51:16 +01:00
|
|
|
|
request, s?s:"[none]", http_get_status_code (http));
|
|
|
|
|
if (s && *s && redirects_left-- )
|
|
|
|
|
{
|
2017-07-19 16:02:05 +02:00
|
|
|
|
if (is_onion)
|
|
|
|
|
{
|
|
|
|
|
/* Make sure that an onion address only redirects to
|
|
|
|
|
* another onion address. */
|
|
|
|
|
http_release_parsed_uri (uri);
|
|
|
|
|
uri = NULL;
|
|
|
|
|
err = http_parse_uri (&uri, s, 0);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
if (! uri->onion)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_FORBIDDEN);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-18 12:51:16 +01:00
|
|
|
|
xfree (request_buffer);
|
|
|
|
|
request_buffer = xtrystrdup (s);
|
|
|
|
|
if (request_buffer)
|
|
|
|
|
{
|
|
|
|
|
request = request_buffer;
|
|
|
|
|
http_close (http, 0);
|
|
|
|
|
http = NULL;
|
|
|
|
|
goto once_more;
|
|
|
|
|
}
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
log_error (_("too many redirections\n"));
|
|
|
|
|
}
|
|
|
|
|
goto leave;
|
|
|
|
|
|
2015-12-04 12:08:37 +01:00
|
|
|
|
case 501:
|
|
|
|
|
err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
|
|
|
|
|
goto leave;
|
|
|
|
|
|
2011-01-18 12:51:16 +01:00
|
|
|
|
default:
|
2012-06-05 19:29:22 +02:00
|
|
|
|
log_error (_("error accessing '%s': http status %u\n"),
|
2011-01-18 12:51:16 +01:00
|
|
|
|
request, http_get_status_code (http));
|
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-05 16:09:45 +02:00
|
|
|
|
/* FIXME: We should register a permanent redirection and whether a
|
|
|
|
|
host has ever used TLS so that future calls will always use
|
|
|
|
|
TLS. */
|
|
|
|
|
|
2011-01-18 12:51:16 +01:00
|
|
|
|
fp = http_get_read_ptr (http);
|
|
|
|
|
if (!fp)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_BUG);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return the read stream and close the HTTP context. */
|
|
|
|
|
*r_fp = fp;
|
|
|
|
|
http_close (http, 1);
|
|
|
|
|
http = NULL;
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
http_close (http, 0);
|
2014-05-05 16:09:45 +02:00
|
|
|
|
http_session_release (session);
|
2011-01-18 12:51:16 +01:00
|
|
|
|
xfree (request_buffer);
|
2017-07-19 16:02:05 +02:00
|
|
|
|
http_release_parsed_uri (uri);
|
2011-01-18 12:51:16 +01:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-24 20:45:31 +01:00
|
|
|
|
/* Helper to evaluate the error code ERR from a send_request() call
|
2014-03-12 14:32:34 +01:00
|
|
|
|
with REQUEST. The function returns true if the caller shall try
|
|
|
|
|
again. TRIES_LEFT points to a variable to track the number of
|
|
|
|
|
retries; this function decrements it and won't return true if it is
|
|
|
|
|
down to zero. */
|
|
|
|
|
static int
|
2017-01-24 20:45:31 +01:00
|
|
|
|
handle_send_request_error (ctrl_t ctrl, gpg_error_t err, const char *request,
|
2014-03-12 14:32:34 +01:00
|
|
|
|
unsigned int *tries_left)
|
|
|
|
|
{
|
|
|
|
|
int retry = 0;
|
|
|
|
|
|
2017-01-11 16:43:30 +01:00
|
|
|
|
/* Fixme: Should we disable all hosts of a protocol family if a
|
|
|
|
|
* request for an address of that familiy returned ENETDOWN? */
|
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
|
switch (gpg_err_code (err))
|
|
|
|
|
{
|
|
|
|
|
case GPG_ERR_ECONNREFUSED:
|
2017-01-24 20:45:31 +01:00
|
|
|
|
if (tor_not_running_p (ctrl))
|
|
|
|
|
break; /* A retry does not make sense. */
|
|
|
|
|
/* Okay: Tor is up or --use-tor is not used. */
|
2017-01-11 17:09:16 +01:00
|
|
|
|
/*FALLTHRU*/
|
2014-03-12 14:32:34 +01:00
|
|
|
|
case GPG_ERR_ENETUNREACH:
|
2017-01-11 16:43:30 +01:00
|
|
|
|
case GPG_ERR_ENETDOWN:
|
2014-12-08 17:13:11 +01:00
|
|
|
|
case GPG_ERR_UNKNOWN_HOST:
|
|
|
|
|
case GPG_ERR_NETWORK:
|
2017-04-03 20:23:18 +02:00
|
|
|
|
case GPG_ERR_EIO: /* Sometimes used by estream cookie functions. */
|
2014-03-12 14:32:34 +01:00
|
|
|
|
if (mark_host_dead (request) && *tries_left)
|
|
|
|
|
retry = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GPG_ERR_ETIMEDOUT:
|
|
|
|
|
if (*tries_left)
|
|
|
|
|
{
|
|
|
|
|
log_info ("selecting a different host due to a timeout\n");
|
|
|
|
|
retry = 1;
|
|
|
|
|
}
|
2017-01-11 16:43:30 +01:00
|
|
|
|
break;
|
2014-03-12 14:32:34 +01:00
|
|
|
|
|
2017-01-24 20:45:31 +01:00
|
|
|
|
case GPG_ERR_EACCES:
|
2017-02-01 17:54:14 +01:00
|
|
|
|
if (dirmngr_use_tor ())
|
2017-01-24 20:45:31 +01:00
|
|
|
|
{
|
|
|
|
|
log_info ("(Tor configuration problem)\n");
|
|
|
|
|
dirmngr_status (ctrl, "WARNING", "tor_config_problem 0",
|
|
|
|
|
"Please check that the \"SocksPort\" flag "
|
|
|
|
|
"\"IPv6Traffic\" is set in torrc", NULL);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*tries_left)
|
|
|
|
|
--*tries_left;
|
|
|
|
|
|
|
|
|
|
return retry;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-20 14:12:53 +01:00
|
|
|
|
|
2011-01-10 14:30:17 +01:00
|
|
|
|
/* Search the keyserver identified by URI for keys matching PATTERN.
|
2015-12-04 12:32:20 +01:00
|
|
|
|
On success R_FP has an open stream to read the data. If
|
|
|
|
|
R_HTTP_STATUS is not NULL, the http status code will be stored
|
|
|
|
|
there. */
|
2011-01-10 14:30:17 +01:00
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_hkp_search (ctrl_t ctrl, parsed_uri_t uri, const char *pattern,
|
2015-12-04 12:32:20 +01:00
|
|
|
|
estream_t *r_fp, unsigned int *r_http_status)
|
2011-01-10 14:30:17 +01:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
KEYDB_SEARCH_DESC desc;
|
|
|
|
|
char fprbuf[2+40+1];
|
|
|
|
|
char *hostport = NULL;
|
|
|
|
|
char *request = NULL;
|
|
|
|
|
estream_t fp = NULL;
|
2014-03-12 14:32:34 +01:00
|
|
|
|
int reselect;
|
2014-03-14 17:00:10 +01:00
|
|
|
|
unsigned int httpflags;
|
2014-05-16 21:14:03 +02:00
|
|
|
|
char *httphost = NULL;
|
2014-03-12 14:32:34 +01:00
|
|
|
|
unsigned int tries = SEND_REQUEST_RETRIES;
|
2011-01-10 14:30:17 +01:00
|
|
|
|
|
|
|
|
|
*r_fp = NULL;
|
|
|
|
|
|
|
|
|
|
/* Remove search type indicator and adjust PATTERN accordingly.
|
|
|
|
|
Note that HKP keyservers like the 0x to be present when searching
|
|
|
|
|
by keyid. We need to re-format the fingerprint and keyids so to
|
|
|
|
|
remove the gpg specific force-use-of-this-key flag ("!"). */
|
2011-04-25 23:56:47 +02:00
|
|
|
|
err = classify_user_id (pattern, &desc, 1);
|
2011-01-10 14:30:17 +01:00
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
switch (desc.mode)
|
|
|
|
|
{
|
|
|
|
|
case KEYDB_SEARCH_MODE_EXACT:
|
|
|
|
|
case KEYDB_SEARCH_MODE_SUBSTR:
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAIL:
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAILSUB:
|
|
|
|
|
pattern = desc.u.name;
|
|
|
|
|
break;
|
|
|
|
|
case KEYDB_SEARCH_MODE_SHORT_KID:
|
|
|
|
|
snprintf (fprbuf, sizeof fprbuf, "0x%08lX", (ulong)desc.u.kid[1]);
|
|
|
|
|
pattern = fprbuf;
|
|
|
|
|
break;
|
|
|
|
|
case KEYDB_SEARCH_MODE_LONG_KID:
|
2011-02-04 12:57:53 +01:00
|
|
|
|
snprintf (fprbuf, sizeof fprbuf, "0x%08lX%08lX",
|
2011-01-10 14:30:17 +01:00
|
|
|
|
(ulong)desc.u.kid[0], (ulong)desc.u.kid[1]);
|
|
|
|
|
pattern = fprbuf;
|
|
|
|
|
break;
|
|
|
|
|
case KEYDB_SEARCH_MODE_FPR16:
|
2016-07-14 14:00:37 +02:00
|
|
|
|
fprbuf[0] = '0';
|
|
|
|
|
fprbuf[1] = 'x';
|
|
|
|
|
bin2hex (desc.u.fpr, 16, fprbuf+2);
|
2011-01-10 14:30:17 +01:00
|
|
|
|
pattern = fprbuf;
|
|
|
|
|
break;
|
|
|
|
|
case KEYDB_SEARCH_MODE_FPR20:
|
|
|
|
|
case KEYDB_SEARCH_MODE_FPR:
|
2016-07-14 14:00:37 +02:00
|
|
|
|
fprbuf[0] = '0';
|
|
|
|
|
fprbuf[1] = 'x';
|
|
|
|
|
bin2hex (desc.u.fpr, 20, fprbuf+2);
|
2011-01-10 14:30:17 +01:00
|
|
|
|
pattern = fprbuf;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return gpg_error (GPG_ERR_INV_USER_ID);
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2011-01-10 14:30:17 +01:00
|
|
|
|
/* Build the request string. */
|
2014-03-12 14:32:34 +01:00
|
|
|
|
reselect = 0;
|
|
|
|
|
again:
|
2011-01-10 14:30:17 +01:00
|
|
|
|
{
|
|
|
|
|
char *searchkey;
|
|
|
|
|
|
2014-12-08 11:13:17 +01:00
|
|
|
|
xfree (hostport); hostport = NULL;
|
2014-05-16 21:14:03 +02:00
|
|
|
|
xfree (httphost); httphost = NULL;
|
2017-01-09 10:11:20 +01:00
|
|
|
|
err = make_host_part (ctrl, uri->scheme, uri->host, uri->port,
|
|
|
|
|
reselect, uri->explicit_port,
|
2014-12-08 11:13:17 +01:00
|
|
|
|
&hostport, &httpflags, &httphost);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
2011-01-10 14:30:17 +01:00
|
|
|
|
|
|
|
|
|
searchkey = http_escape_string (pattern, EXTRA_ESCAPE_CHARS);
|
|
|
|
|
if (!searchkey)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
|
xfree (request);
|
2011-01-10 14:30:17 +01:00
|
|
|
|
request = strconcat (hostport,
|
|
|
|
|
"/pks/lookup?op=index&options=mr&search=",
|
|
|
|
|
searchkey,
|
|
|
|
|
NULL);
|
|
|
|
|
xfree (searchkey);
|
|
|
|
|
if (!request)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2011-01-10 14:30:17 +01:00
|
|
|
|
/* Send the request. */
|
2014-05-16 21:14:03 +02:00
|
|
|
|
err = send_request (ctrl, request, hostport, httphost, httpflags,
|
2015-12-04 12:32:20 +01:00
|
|
|
|
NULL, NULL, &fp, r_http_status);
|
2017-01-24 20:45:31 +01:00
|
|
|
|
if (handle_send_request_error (ctrl, err, request, &tries))
|
2014-03-12 14:32:34 +01:00
|
|
|
|
{
|
|
|
|
|
reselect = 1;
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
2011-01-10 14:30:17 +01:00
|
|
|
|
if (err)
|
2011-01-18 12:51:16 +01:00
|
|
|
|
goto leave;
|
2011-01-10 14:30:17 +01:00
|
|
|
|
|
2014-03-14 16:12:54 +01:00
|
|
|
|
err = dirmngr_status (ctrl, "SOURCE", hostport, NULL);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
2014-03-11 14:26:39 +01:00
|
|
|
|
/* Peek at the response. */
|
2011-01-10 14:30:17 +01:00
|
|
|
|
{
|
|
|
|
|
int c = es_getc (fp);
|
|
|
|
|
if (c == -1)
|
|
|
|
|
{
|
|
|
|
|
err = es_ferror (fp)?gpg_error_from_syserror ():gpg_error (GPG_ERR_EOF);
|
|
|
|
|
log_error ("error reading response: %s\n", gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (c == '<')
|
|
|
|
|
{
|
2014-03-11 14:26:39 +01:00
|
|
|
|
/* The document begins with a '<': Assume a HTML response,
|
|
|
|
|
which we don't support. */
|
2011-01-10 14:30:17 +01:00
|
|
|
|
err = gpg_error (GPG_ERR_UNSUPPORTED_ENCODING);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
es_ungetc (c, fp);
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-18 12:51:16 +01:00
|
|
|
|
/* Return the read stream. */
|
|
|
|
|
*r_fp = fp;
|
|
|
|
|
fp = NULL;
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
es_fclose (fp);
|
|
|
|
|
xfree (request);
|
|
|
|
|
xfree (hostport);
|
2014-05-16 21:14:03 +02:00
|
|
|
|
xfree (httphost);
|
2011-01-18 12:51:16 +01:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Get the key described key the KEYSPEC string from the keyserver
|
|
|
|
|
identified by URI. On success R_FP has an open stream to read the
|
2015-03-19 11:15:53 +01:00
|
|
|
|
data. The data will be provided in a format GnuPG can import
|
|
|
|
|
(either a binary OpenPGP message or an armored one). */
|
2011-01-18 12:51:16 +01:00
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_hkp_get (ctrl_t ctrl, parsed_uri_t uri, const char *keyspec, estream_t *r_fp)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
KEYDB_SEARCH_DESC desc;
|
2014-03-17 15:39:33 +01:00
|
|
|
|
char kidbuf[2+40+1];
|
|
|
|
|
const char *exactname = NULL;
|
|
|
|
|
char *searchkey = NULL;
|
2011-01-18 12:51:16 +01:00
|
|
|
|
char *hostport = NULL;
|
|
|
|
|
char *request = NULL;
|
|
|
|
|
estream_t fp = NULL;
|
2014-03-12 14:32:34 +01:00
|
|
|
|
int reselect;
|
2014-05-16 21:14:03 +02:00
|
|
|
|
char *httphost = NULL;
|
2014-03-14 17:00:10 +01:00
|
|
|
|
unsigned int httpflags;
|
2014-03-12 14:32:34 +01:00
|
|
|
|
unsigned int tries = SEND_REQUEST_RETRIES;
|
2011-01-18 12:51:16 +01:00
|
|
|
|
|
|
|
|
|
*r_fp = NULL;
|
|
|
|
|
|
|
|
|
|
/* Remove search type indicator and adjust PATTERN accordingly.
|
|
|
|
|
Note that HKP keyservers like the 0x to be present when searching
|
|
|
|
|
by keyid. We need to re-format the fingerprint and keyids so to
|
|
|
|
|
remove the gpg specific force-use-of-this-key flag ("!"). */
|
2011-04-25 23:56:47 +02:00
|
|
|
|
err = classify_user_id (keyspec, &desc, 1);
|
2011-01-18 12:51:16 +01:00
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
switch (desc.mode)
|
|
|
|
|
{
|
|
|
|
|
case KEYDB_SEARCH_MODE_SHORT_KID:
|
2014-03-17 15:39:33 +01:00
|
|
|
|
snprintf (kidbuf, sizeof kidbuf, "0x%08lX", (ulong)desc.u.kid[1]);
|
2011-01-18 12:51:16 +01:00
|
|
|
|
break;
|
2012-01-10 10:23:54 -05:00
|
|
|
|
case KEYDB_SEARCH_MODE_LONG_KID:
|
2014-03-17 15:39:33 +01:00
|
|
|
|
snprintf (kidbuf, sizeof kidbuf, "0x%08lX%08lX",
|
2012-01-10 10:23:54 -05:00
|
|
|
|
(ulong)desc.u.kid[0], (ulong)desc.u.kid[1]);
|
|
|
|
|
break;
|
2011-01-18 12:51:16 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_FPR20:
|
|
|
|
|
case KEYDB_SEARCH_MODE_FPR:
|
2012-01-10 10:23:54 -05:00
|
|
|
|
/* This is a v4 fingerprint. */
|
2014-03-17 15:39:33 +01:00
|
|
|
|
kidbuf[0] = '0';
|
|
|
|
|
kidbuf[1] = 'x';
|
|
|
|
|
bin2hex (desc.u.fpr, 20, kidbuf+2);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_EXACT:
|
|
|
|
|
exactname = desc.u.name;
|
2011-01-18 12:51:16 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_FPR16:
|
2012-01-10 10:23:54 -05:00
|
|
|
|
log_error ("HKP keyservers do not support v3 fingerprints\n");
|
2017-05-10 11:01:15 +09:00
|
|
|
|
/* fall through */
|
2011-01-18 12:51:16 +01:00
|
|
|
|
default:
|
|
|
|
|
return gpg_error (GPG_ERR_INV_USER_ID);
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2014-03-17 15:39:33 +01:00
|
|
|
|
searchkey = http_escape_string (exactname? exactname : kidbuf,
|
|
|
|
|
EXTRA_ESCAPE_CHARS);
|
|
|
|
|
if (!searchkey)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
|
reselect = 0;
|
|
|
|
|
again:
|
2011-04-12 16:30:08 +02:00
|
|
|
|
/* Build the request string. */
|
2014-12-08 11:13:17 +01:00
|
|
|
|
xfree (hostport); hostport = NULL;
|
2014-05-16 21:14:03 +02:00
|
|
|
|
xfree (httphost); httphost = NULL;
|
2017-01-09 10:11:20 +01:00
|
|
|
|
err = make_host_part (ctrl, uri->scheme, uri->host, uri->port,
|
|
|
|
|
reselect, uri->explicit_port,
|
2014-12-08 11:13:17 +01:00
|
|
|
|
&hostport, &httpflags, &httphost);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
2011-04-12 16:30:08 +02:00
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
|
xfree (request);
|
2011-04-12 16:30:08 +02:00
|
|
|
|
request = strconcat (hostport,
|
2014-03-17 15:39:33 +01:00
|
|
|
|
"/pks/lookup?op=get&options=mr&search=",
|
|
|
|
|
searchkey,
|
|
|
|
|
exactname? "&exact=on":"",
|
2011-04-12 16:30:08 +02:00
|
|
|
|
NULL);
|
|
|
|
|
if (!request)
|
2011-01-18 12:51:16 +01:00
|
|
|
|
{
|
2011-04-12 16:30:08 +02:00
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
2011-01-18 12:51:16 +01:00
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2011-01-18 12:51:16 +01:00
|
|
|
|
/* Send the request. */
|
2014-05-16 21:14:03 +02:00
|
|
|
|
err = send_request (ctrl, request, hostport, httphost, httpflags,
|
2015-12-04 12:32:20 +01:00
|
|
|
|
NULL, NULL, &fp, NULL);
|
2017-01-24 20:45:31 +01:00
|
|
|
|
if (handle_send_request_error (ctrl, err, request, &tries))
|
2014-03-12 14:32:34 +01:00
|
|
|
|
{
|
|
|
|
|
reselect = 1;
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
2011-01-18 12:51:16 +01:00
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
2014-03-14 16:12:54 +01:00
|
|
|
|
err = dirmngr_status (ctrl, "SOURCE", hostport, NULL);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
2011-01-10 14:30:17 +01:00
|
|
|
|
/* Return the read stream and close the HTTP context. */
|
|
|
|
|
*r_fp = fp;
|
|
|
|
|
fp = NULL;
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
es_fclose (fp);
|
|
|
|
|
xfree (request);
|
|
|
|
|
xfree (hostport);
|
2014-05-16 21:14:03 +02:00
|
|
|
|
xfree (httphost);
|
2014-03-17 15:39:33 +01:00
|
|
|
|
xfree (searchkey);
|
2011-01-10 14:30:17 +01:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-01-20 14:12:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Callback parameters for put_post_cb. */
|
|
|
|
|
struct put_post_parm_s
|
|
|
|
|
{
|
|
|
|
|
char *datastring;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Helper for ks_hkp_put. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
put_post_cb (void *opaque, http_t http)
|
|
|
|
|
{
|
|
|
|
|
struct put_post_parm_s *parm = opaque;
|
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
estream_t fp;
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
fp = http_get_write_ptr (http);
|
|
|
|
|
len = strlen (parm->datastring);
|
|
|
|
|
|
|
|
|
|
es_fprintf (fp,
|
|
|
|
|
"Content-Type: application/x-www-form-urlencoded\r\n"
|
|
|
|
|
"Content-Length: %zu\r\n", len+8 /* 8 is for "keytext" */);
|
|
|
|
|
http_start_data (http);
|
|
|
|
|
if (es_fputs ("keytext=", fp) || es_write (fp, parm->datastring, len, NULL))
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-03-19 11:14:52 +01:00
|
|
|
|
/* Send the key in {DATA,DATALEN} to the keyserver identified by URI. */
|
2011-01-20 14:12:53 +01:00
|
|
|
|
gpg_error_t
|
|
|
|
|
ks_hkp_put (ctrl_t ctrl, parsed_uri_t uri, const void *data, size_t datalen)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
char *hostport = NULL;
|
|
|
|
|
char *request = NULL;
|
|
|
|
|
estream_t fp = NULL;
|
|
|
|
|
struct put_post_parm_s parm;
|
|
|
|
|
char *armored = NULL;
|
2014-03-12 14:32:34 +01:00
|
|
|
|
int reselect;
|
2014-05-16 21:14:03 +02:00
|
|
|
|
char *httphost = NULL;
|
2014-03-14 17:00:10 +01:00
|
|
|
|
unsigned int httpflags;
|
2014-03-12 14:32:34 +01:00
|
|
|
|
unsigned int tries = SEND_REQUEST_RETRIES;
|
2011-01-20 14:12:53 +01:00
|
|
|
|
|
|
|
|
|
parm.datastring = NULL;
|
|
|
|
|
|
|
|
|
|
err = armor_data (&armored, data, datalen);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
parm.datastring = http_escape_string (armored, EXTRA_ESCAPE_CHARS);
|
|
|
|
|
if (!parm.datastring)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
xfree (armored);
|
|
|
|
|
armored = NULL;
|
|
|
|
|
|
|
|
|
|
/* Build the request string. */
|
2014-03-12 14:32:34 +01:00
|
|
|
|
reselect = 0;
|
|
|
|
|
again:
|
2014-12-08 11:13:17 +01:00
|
|
|
|
xfree (hostport); hostport = NULL;
|
2014-05-16 21:14:03 +02:00
|
|
|
|
xfree (httphost); httphost = NULL;
|
2017-01-09 10:11:20 +01:00
|
|
|
|
err = make_host_part (ctrl, uri->scheme, uri->host, uri->port,
|
|
|
|
|
reselect, uri->explicit_port,
|
2014-12-08 11:13:17 +01:00
|
|
|
|
&hostport, &httpflags, &httphost);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
2011-01-20 14:12:53 +01:00
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
|
xfree (request);
|
2011-01-20 14:12:53 +01:00
|
|
|
|
request = strconcat (hostport, "/pks/add", NULL);
|
|
|
|
|
if (!request)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2011-01-20 14:12:53 +01:00
|
|
|
|
/* Send the request. */
|
2014-05-16 21:14:03 +02:00
|
|
|
|
err = send_request (ctrl, request, hostport, httphost, 0,
|
2015-12-04 12:32:20 +01:00
|
|
|
|
put_post_cb, &parm, &fp, NULL);
|
2017-01-24 20:45:31 +01:00
|
|
|
|
if (handle_send_request_error (ctrl, err, request, &tries))
|
2014-03-12 14:32:34 +01:00
|
|
|
|
{
|
|
|
|
|
reselect = 1;
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
2011-01-20 14:12:53 +01:00
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
es_fclose (fp);
|
|
|
|
|
xfree (parm.datastring);
|
|
|
|
|
xfree (armored);
|
|
|
|
|
xfree (request);
|
|
|
|
|
xfree (hostport);
|
2014-05-16 21:14:03 +02:00
|
|
|
|
xfree (httphost);
|
2011-01-20 14:12:53 +01:00
|
|
|
|
return err;
|
|
|
|
|
}
|