#include "userids.h"
#include "ks-engine.h"
-/* Substitute a missing Mingw macro. */
+/* 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. */
#ifndef EAI_OVERFLOW
# define EAI_OVERFLOW EAI_FAIL
#endif
+#ifdef HAVE_W32_SYSTEM
+# ifndef EAI_SYSTEM
+# define EAI_SYSTEM WSAEINVAL
+# endif
+#endif
+/* Number of seconds after a host is marked as resurrected. */
+#define RESURRECT_INTERVAL (3600*3) /* 3 hours */
+
/* To match the behaviour of our old gpgkeys helper code we escape
more characters than actually needed. */
#define EXTRA_ESCAPE_CHARS "@!\"#$%&'()*+,-./:;<=>?[\\]^_{|}~"
int *pool; /* A -1 terminated array with indices into
HOSTTABLE or NULL if NAME is not a pool
name. */
- int poolidx; /* Index into POOL with the used host. */
+ int poolidx; /* Index into POOL with the used host. -1 if not set. */
unsigned int v4:1; /* Host supports AF_INET. */
unsigned int v6:1; /* Host supports AF_INET6. */
unsigned int dead:1; /* Host is currently unresponsive. */
+ time_t died_at; /* The time the host was marked dead. If this is
+ 0 the host has been manually marked dead. */
+ char *cname; /* Canonical name of the host. Only set if this
+ is a pool. */
+ char *v4addr; /* A string with the v4 IP address of the host.
+ NULL if NAME has a numeric IP address or no v4
+ address is available. */
+ char *v6addr; /* A string with the v6 IP address of the host.
+ NULL if NAME has a numeric IP address or no v4
+ address is available. */
char name[1]; /* The hostname. */
};
hi->v4 = 0;
hi->v6 = 0;
hi->dead = 0;
+ hi->died_at = 0;
+ hi->cname = NULL;
+ hi->v4addr = NULL;
+ hi->v6addr = NULL;
/* Add it to the hosttable. */
for (idx=0; idx < hosttable_size; idx++)
/* Simplified version of getnameinfo which also returns a numeric
hostname inside of brackets. The caller should provide a buffer
- for TMPHOST which is 2 bytes larger than the the largest hostname.
- returns 0 on success or an EAI error code. */
+ for HOST which is 2 bytes larger than the largest hostname. If
+ NUMERIC is true the returned value is numeric IP address. Returns
+ 0 on success or an EAI error code. True is stored at R_ISNUMERIC
+ if HOST has a numeric IP address. */
static int
-my_getnameinfo (const struct sockaddr *sa, socklen_t salen,
- char *host, size_t hostlen)
+my_getnameinfo (struct addrinfo *ai, char *host, size_t hostlen,
+ int numeric, int *r_isnumeric)
{
int ec;
+ char *p;
+
+ *r_isnumeric = 0;
if (hostlen < 5)
return EAI_OVERFLOW;
- ec = getnameinfo (sa, salen, host, hostlen, NULL, 0, NI_NAMEREQD);
+ if (numeric)
+ ec = EAI_NONAME;
+ else
+ ec = getnameinfo (ai->ai_addr, ai->ai_addrlen,
+ host, hostlen, NULL, 0, NI_NAMEREQD);
+
if (!ec && *host == '[')
ec = EAI_FAIL; /* A name may never start with a bracket. */
else if (ec == EAI_NONAME)
{
- *host = '[';
- ec = getnameinfo (sa, salen, host + 1, hostlen - 2,
- NULL, 0, NI_NUMERICHOST);
- if (!ec)
+ p = host;
+ if (ai->ai_family == AF_INET6)
+ {
+ *p++ = '[';
+ hostlen -= 2;
+ }
+ ec = getnameinfo (ai->ai_addr, ai->ai_addrlen,
+ p, hostlen, NULL, 0, NI_NUMERICHOST);
+ if (!ec && ai->ai_family == AF_INET6)
strcat (host, "]");
+
+ *r_isnumeric = 1;
}
return ec;
}
+/* Check whether NAME is an IP address. */
+static int
+is_ip_address (const char *name)
+{
+ int ndots, n;
+
+ if (*name == '[')
+ return 1;
+ /* Check whether it is legacy IP address. */
+ if (*name == '.')
+ return 0; /* No. */
+ ndots = n = 0;
+ for (; *name; name++)
+ {
+ if (*name == '.')
+ {
+ if (name[1] == '.')
+ return 0; /* No. */
+ if (atoi (name+1) > 255)
+ return 0; /* Value too large. */
+ ndots++;
+ n = 0;
+ }
+ else if (!strchr ("012345678", *name))
+ return 0; /* Not a digit. */
+ else if (++n > 3)
+ return 0; /* More than 3 digits. */
+ }
+ return !!(ndots == 3);
+}
+
+
/* Map the host name NAME to the actual to be used host name. This
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
- host is always selected. */
-static char *
-map_host (ctrl_t ctrl, const char *name, int force_reselect)
+ host is always selected. The selected host is stored as a malloced
+ string at R_HOST; on error NULL is stored. If R_HTTPFLAGS is not
+ NULL it will receive flags which are to be passed to http_open. If
+ R_POOLNAME is not NULL a malloced name of the pool is stored or
+ NULL if it is not a pool. */
+static gpg_error_t
+map_host (ctrl_t ctrl, const char *name, int force_reselect,
+ char **r_host, unsigned int *r_httpflags, char **r_poolname)
{
+ gpg_error_t err = 0;
hostinfo_t hi;
int idx;
+ *r_host = NULL;
+ if (r_httpflags)
+ *r_httpflags = 0;
+ if (r_poolname)
+ *r_poolname = NULL;
+
/* No hostname means localhost. */
if (!name || !*name)
- return xtrystrdup ("localhost");
+ {
+ *r_host = xtrystrdup ("localhost");
+ return *r_host? 0 : gpg_error_from_syserror ();
+ }
/* See whether the host is in our table. */
idx = find_hostinfo (name);
int *reftbl;
size_t reftblsize;
int refidx;
+ int is_pool = 0;
reftblsize = 100;
reftbl = xtrymalloc (reftblsize * sizeof *reftbl);
if (!reftbl)
- return NULL;
+ return gpg_error_from_syserror ();
refidx = 0;
idx = create_new_hostinfo (name);
if (idx == -1)
{
+ err = gpg_error_from_syserror ();
xfree (reftbl);
- return NULL;
+ return err;
}
hi = hosttable[idx];
/* Find all A records for this entry and put them into the pool
list - if any. */
memset (&hints, 0, sizeof (hints));
+ hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_CANONNAME;
+ /* We can't use the the AI_IDN flag because that does the
+ conversion using the current locale. However, GnuPG always
+ used UTF-8. To support IDN we would need to make use of the
+ libidn API. */
if (!getaddrinfo (name, NULL, &hints, &aibuf))
{
+ int n_v6, n_v4;
+
+ /* First figure out whether this is a pool. For a pool we
+ use a different strategy than for a plains erver: We use
+ 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. */
+ n_v6 = n_v4 = 0;
for (ai = aibuf; ai; ai = ai->ai_next)
{
- char tmphost[NI_MAXHOST];
+ if (ai->ai_family != AF_INET6)
+ n_v6++;
+ else if (ai->ai_family != AF_INET)
+ n_v4++;
+ }
+ if (n_v6 > 1 || n_v4 > 1)
+ is_pool = 1;
+ if (is_pool && aibuf->ai_canonname)
+ hi->cname = xtrystrdup (aibuf->ai_canonname);
+
+ for (ai = aibuf; ai; ai = ai->ai_next)
+ {
+ char tmphost[NI_MAXHOST + 2];
int tmpidx;
+ int is_numeric;
int ec;
int i;
continue;
dirmngr_tick (ctrl);
- if ((ec = my_getnameinfo (ai->ai_addr, ai->ai_addrlen,
- tmphost, sizeof tmphost)))
+
+ if (!is_pool && !is_ip_address (name))
+ {
+ /* This is a hostname but not a pool. Use the name
+ as given without going through getnameinfo. */
+ if (strlen (name)+1 > sizeof tmphost)
+ {
+ ec = EAI_SYSTEM;
+ gpg_err_set_errno (EINVAL);
+ }
+ else
+ {
+ ec = 0;
+ strcpy (tmphost, name);
+ }
+ is_numeric = 0;
+ }
+ else
+ ec = my_getnameinfo (ai, tmphost, sizeof tmphost,
+ 0, &is_numeric);
+
+ if (ec)
{
log_info ("getnameinfo failed while checking '%s': %s\n",
name, gai_strerror (ec));
}
else
{
+ tmpidx = find_hostinfo (tmphost);
+ log_info ("getnameinfo returned 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 = find_hostinfo (tmphost)) != -1)
+ if (tmpidx == -1)
{
- log_info ("getnameinfo returned for '%s': '%s'"
- " [already known]\n", name, tmphost);
- if (ai->ai_family == AF_INET)
- hosttable[tmpidx]->v4 = 1;
+ log_error ("map_host for '%s' problem: %s - '%s'"
+ " [ignored]\n",
+ name, strerror (errno), tmphost);
+ }
+ else /* Set or update the entry. */
+ {
+ char *ipaddr = NULL;
+
+ if (!is_numeric)
+ {
+ ec = my_getnameinfo (ai, tmphost, sizeof tmphost,
+ 1, &is_numeric);
+ if (!ec && !(ipaddr = xtrystrdup (tmphost)))
+ ec = EAI_SYSTEM;
+ if (ec)
+ log_info ("getnameinfo failed: %s\n",
+ gai_strerror (ec));
+ }
+
if (ai->ai_family == AF_INET6)
- hosttable[tmpidx]->v6 = 1;
+ {
+ hosttable[tmpidx]->v6 = 1;
+ xfree (hosttable[tmpidx]->v6addr);
+ hosttable[tmpidx]->v6addr = ipaddr;
+ }
+ else if (ai->ai_family == AF_INET)
+ {
+ hosttable[tmpidx]->v4 = 1;
+ xfree (hosttable[tmpidx]->v4addr);
+ hosttable[tmpidx]->v4addr = ipaddr;
+ }
+ else
+ BUG ();
for (i=0; i < refidx; i++)
if (reftbl[i] == tmpidx)
if (!(i < refidx) && tmpidx != idx)
reftbl[refidx++] = tmpidx;
}
- else
- {
- log_info ("getnameinfo returned for '%s': '%s'\n",
- name, tmphost);
- /* Create a new entry. */
- tmpidx = create_new_hostinfo (tmphost);
- if (tmpidx == -1)
- log_error ("map_host for '%s' problem: %s - '%s'"
- " [ignored]\n",
- name, strerror (errno), tmphost);
- else
- {
- if (ai->ai_family == AF_INET)
- hosttable[tmpidx]->v4 = 1;
- if (ai->ai_family == AF_INET6)
- hosttable[tmpidx]->v6 = 1;
-
- for (i=0; i < refidx; i++)
- if (reftbl[i] == tmpidx)
- break;
- if (!(i < refidx) && tmpidx != idx)
- reftbl[refidx++] = tmpidx;
- }
- }
}
}
+ freeaddrinfo (aibuf);
}
reftbl[refidx] = -1;
- if (refidx)
+ if (refidx && is_pool)
{
assert (!hi->pool);
hi->pool = xtryrealloc (reftbl, (refidx+1) * sizeof *reftbl);
if (!hi->pool)
{
+ err = gpg_error_from_syserror ();
log_error ("shrinking index table in map_host failed: %s\n",
- strerror (errno));
+ gpg_strerror (err));
xfree (reftbl);
+ return err;
}
qsort (reftbl, refidx, sizeof *reftbl, sort_hostpool);
}
hi = hosttable[idx];
if (hi->pool)
{
+ /* Deal with the pool name before selecting a host. */
+ if (r_poolname && hi->cname)
+ {
+ *r_poolname = xtrystrdup (hi->cname);
+ if (!*r_poolname)
+ return gpg_error_from_syserror ();
+ }
+
/* If the currently selected host is now marked dead, force a
re-selection . */
if (force_reselect)
if (hi->poolidx == -1)
{
log_error ("no alive host found in pool '%s'\n", name);
- return NULL;
+ if (r_poolname)
+ {
+ xfree (*r_poolname);
+ *r_poolname = NULL;
+ }
+ return gpg_error (GPG_ERR_NO_KEYSERVER);
}
}
if (hi->dead)
{
log_error ("host '%s' marked as dead\n", hi->name);
- return NULL;
+ if (r_poolname)
+ {
+ xfree (*r_poolname);
+ *r_poolname = NULL;
+ }
+ return gpg_error (GPG_ERR_NO_KEYSERVER);
}
- return xtrystrdup (hi->name);
+ 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
+ the name is resolved by our http layer. */
+ if (!hi->v4)
+ *r_httpflags |= HTTP_FLAG_IGNORE_IPv4;
+ if (!hi->v6)
+ *r_httpflags |= HTTP_FLAG_IGNORE_IPv6;
+ }
+
+ *r_host = xtrystrdup (hi->name);
+ if (!*r_host)
+ {
+ err = gpg_error_from_syserror ();
+ if (r_poolname)
+ {
+ xfree (*r_poolname);
+ *r_poolname = NULL;
+ }
+ return err;
+ }
+ return 0;
}
log_info ("marking host '%s' as dead%s\n",
hi->name, hi->dead? " (again)":"");
hi->dead = 1;
+ hi->died_at = gnupg_get_time ();
+ if (!hi->died_at)
+ hi->died_at = 1;
done = 1;
}
}
else if (!alive && !hi->dead)
{
hi->dead = 1;
+ hi->died_at = 0; /* Manually set dead. */
err = ks_printf_help (ctrl, "marking '%s' as dead", name);
}
member in another pool. */
for (idx3=0; idx3 < hosttable_size; idx3++)
{
- if (hosttable[idx3] && hosttable[idx3]
+ if (hosttable[idx3]
&& hosttable[idx3]->pool
&& idx3 != idx
&& host_in_pool_p (hosttable[idx3]->pool, n))
else if (!alive && !hi2->dead)
{
hi2->dead = 1;
+ hi2->died_at = 0; /* Manually set dead. */
err = ks_printf_help (ctrl, "marking '%s' as dead",
hi2->name);
}
int idx, idx2;
hostinfo_t hi;
membuf_t mb;
- char *p;
+ time_t curtime;
+ char *p, *died;
+ const char *diedstr;
- err = ks_print_help (ctrl, "hosttable (idx, ipv4, ipv6, dead, name):");
+ err = ks_print_help (ctrl, "hosttable (idx, ipv6, ipv4, dead, name, time):");
if (err)
return err;
+ curtime = gnupg_get_time ();
for (idx=0; idx < hosttable_size; idx++)
if ((hi=hosttable[idx]))
{
- err = ks_printf_help (ctrl, "%3d %s %s %s %s\n",
- idx, hi->v4? "4":" ", hi->v6? "6":" ",
- hi->dead? "d":" ", hi->name);
+ if (hi->dead && hi->died_at)
+ {
+ died = elapsed_time_string (hi->died_at, curtime);
+ diedstr = died? died : "error";
+ }
+ else
+ diedstr = died = NULL;
+ err = ks_printf_help (ctrl, "%3d %s %s %s %s%s%s%s%s%s%s%s\n",
+ idx, hi->v6? "6":" ", hi->v4? "4":" ",
+ hi->dead? "d":" ",
+ hi->name,
+ hi->v6addr? " v6=":"",
+ hi->v6addr? hi->v6addr:"",
+ hi->v4addr? " v4=":"",
+ hi->v4addr? hi->v4addr:"",
+ diedstr? " (":"",
+ diedstr? diedstr:"",
+ diedstr? ")":"" );
+ xfree (died);
+ if (err)
+ return err;
+
+ if (hi->cname)
+ err = ks_printf_help (ctrl, " . %s", hi->cname);
if (err)
return err;
+
if (hi->pool)
{
init_membuf (&mb, 256);
const char const data[] =
"Handler for HKP URLs:\n"
" hkp://\n"
+#if HTTP_USE_GNUTLS || HTTP_USE_NTBTLS
+ " hkps://\n"
+#endif
"Supported methods: search, get, put\n";
gpg_error_t err;
+#if HTTP_USE_GNUTLS || HTTP_USE_NTBTLS
+ const char data2[] = " hkp\n hkps";
+#else
+ const char data2[] = " hkp";
+#endif
+
if (!uri)
- err = ks_print_help (ctrl, " hkp");
- else if (uri->is_http && !strcmp (uri->scheme, "hkp"))
+ err = ks_print_help (ctrl, data2);
+ else if (uri->is_http && (!strcmp (uri->scheme, "hkp")
+ || !strcmp (uri->scheme, "hkps")))
err = ks_print_help (ctrl, data);
else
err = 0;
}
-/* Build the remote part or the URL from SCHEME, HOST and an optional
- PORT. Returns an allocated string or NULL on failure and sets
- ERRNO. */
-static char *
+/* Build the remote part of the URL from SCHEME, HOST and an optional
+ PORT. Returns an allocated string at R_HOSTPORT or NULL on failure
+ If R_POOLNAME is not NULL it receives a malloced string with the
+ poolname. */
+static gpg_error_t
make_host_part (ctrl_t ctrl,
const char *scheme, const char *host, unsigned short port,
- int force_reselect)
+ int force_reselect,
+ char **r_hostport, unsigned int *r_httpflags, char **r_poolname)
{
+ gpg_error_t err;
char portstr[10];
char *hostname;
- char *hostport;
+
+ *r_hostport = NULL;
/* Map scheme and port. */
if (!strcmp (scheme, "hkps") || !strcmp (scheme,"https"))
/*fixme_do_srv_lookup ()*/
}
- hostname = map_host (ctrl, host, force_reselect);
- if (!hostname)
- return NULL;
+ err = map_host (ctrl, host, force_reselect,
+ &hostname, r_httpflags, r_poolname);
+ if (err)
+ return err;
- hostport = strconcat (scheme, "://", hostname, ":", portstr, NULL);
+ *r_hostport = strconcat (scheme, "://", hostname, ":", portstr, NULL);
xfree (hostname);
- return hostport;
+ if (!*r_hostport)
+ {
+ if (r_poolname)
+ {
+ xfree (*r_poolname);
+ *r_poolname = NULL;
+ }
+ return gpg_error_from_syserror ();
+ }
+ return 0;
}
gpg_error_t err;
char *hostport = NULL;
- hostport = make_host_part (ctrl, uri->scheme, uri->host, uri->port, 1);
- if (!hostport)
+ err = make_host_part (ctrl, uri->scheme, uri->host, uri->port, 1,
+ &hostport, NULL, NULL);
+ if (err)
{
- err = gpg_error_from_syserror ();
err = ks_printf_help (ctrl, "%s://%s:%hu: resolve failed: %s",
uri->scheme, uri->host, uri->port,
gpg_strerror (err));
}
+/* 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);
+ }
+ }
+}
+
+
/* Send an HTTP request. On success returns an estream object at
- R_FP. HOSTPORTSTR is only used for diagnostics. If POST_CB is not
+ 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
NULL a post request is used and that callback is called to allow
writing the post data. */
static gpg_error_t
send_request (ctrl_t ctrl, const char *request, const char *hostportstr,
+ const char *httphost, unsigned int httpflags,
gpg_error_t (*post_cb)(void *, http_t), void *post_cb_value,
estream_t *r_fp)
{
gpg_error_t err;
+ http_session_t session = NULL;
http_t http = NULL;
int redirects_left = MAX_REDIRECTS;
estream_t fp = NULL;
*r_fp = NULL;
+ err = http_session_new (&session, NULL);
+ if (err)
+ goto leave;
+ http_session_set_log_cb (session, cert_log_cb);
+
once_more:
err = http_open (&http,
post_cb? HTTP_REQ_POST : HTTP_REQ_GET,
request,
+ httphost,
/* fixme: AUTH */ NULL,
- 0,
- /* fixme: proxy*/ NULL,
- NULL, NULL,
+ (httpflags | (opt.honor_http_proxy? HTTP_FLAG_TRY_PROXY:0)),
+ ctrl->http_proxy,
+ session,
+ NULL,
/*FIXME curl->srvtag*/NULL);
if (!err)
{
goto leave;
}
+ 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;
+ }
+
switch (http_get_status_code (http))
{
case 200:
case 301:
case 302:
+ case 307:
{
const char *s = http_get_header (http, "Location");
goto leave;
}
+ /* FIXME: We should register a permanent redirection and whether a
+ host has ever used TLS so that future calls will always use
+ TLS. */
+
fp = http_get_read_ptr (http);
if (!fp)
{
leave:
http_close (http, 0);
+ http_session_release (session);
xfree (request_buffer);
return err;
}
{
case GPG_ERR_ECONNREFUSED:
case GPG_ERR_ENETUNREACH:
+ case GPG_ERR_UNKNOWN_HOST:
+ case GPG_ERR_NETWORK:
if (mark_host_dead (request) && *tries_left)
retry = 1;
break;
return retry;
}
-static gpg_error_t
-armor_data (char **r_string, const void *data, size_t datalen)
-{
- gpg_error_t err;
- struct b64state b64state;
- estream_t fp;
- long length;
- char *buffer;
- size_t nread;
-
- *r_string = NULL;
-
- fp = es_fopenmem (0, "rw");
- if (!fp)
- return gpg_error_from_syserror ();
-
- if ((err=b64enc_start_es (&b64state, fp, "PGP PUBLIC KEY BLOCK"))
- || (err=b64enc_write (&b64state, data, datalen))
- || (err = b64enc_finish (&b64state)))
- {
- es_fclose (fp);
- return err;
- }
-
- /* FIXME: To avoid the extra buffer allocation estream should
- provide a function to snatch the internal allocated memory from
- such a memory stream. */
- length = es_ftell (fp);
- if (length < 0)
- {
- err = gpg_error_from_syserror ();
- es_fclose (fp);
- return err;
- }
-
- buffer = xtrymalloc (length+1);
- if (!buffer)
- {
- err = gpg_error_from_syserror ();
- es_fclose (fp);
- return err;
- }
-
- es_rewind (fp);
- if (es_read (fp, buffer, length, &nread))
- {
- err = gpg_error_from_syserror ();
- es_fclose (fp);
- return err;
- }
- buffer[nread] = 0;
- es_fclose (fp);
-
- *r_string = buffer;
- return 0;
-}
-
-
\f
/* Search the keyserver identified by URI for keys matching PATTERN.
On success R_FP has an open stream to read the data. */
char *request = NULL;
estream_t fp = NULL;
int reselect;
+ unsigned int httpflags;
+ char *httphost = NULL;
unsigned int tries = SEND_REQUEST_RETRIES;
*r_fp = NULL;
{
char *searchkey;
- xfree (hostport);
- hostport = make_host_part (ctrl, uri->scheme, uri->host, uri->port,
- reselect);
- if (!hostport)
- {
- err = gpg_error_from_syserror ();
- goto leave;
- }
+ xfree (hostport); hostport = NULL;
+ xfree (httphost); httphost = NULL;
+ err = make_host_part (ctrl, uri->scheme, uri->host, uri->port, reselect,
+ &hostport, &httpflags, &httphost);
+ if (err)
+ goto leave;
searchkey = http_escape_string (pattern, EXTRA_ESCAPE_CHARS);
if (!searchkey)
}
/* Send the request. */
- err = send_request (ctrl, request, hostport, NULL, NULL, &fp);
+ err = send_request (ctrl, request, hostport, httphost, httpflags,
+ NULL, NULL, &fp);
if (handle_send_request_error (err, request, &tries))
{
reselect = 1;
es_fclose (fp);
xfree (request);
xfree (hostport);
+ xfree (httphost);
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
- data. */
+ data. The data will be provided in a format GnuPG can import
+ (either a binary OpenPGP message or an armored one). */
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;
- char kidbuf[40+1];
+ char kidbuf[2+40+1];
+ const char *exactname = NULL;
+ char *searchkey = NULL;
char *hostport = NULL;
char *request = NULL;
estream_t fp = NULL;
int reselect;
+ char *httphost = NULL;
+ unsigned int httpflags;
unsigned int tries = SEND_REQUEST_RETRIES;
*r_fp = NULL;
switch (desc.mode)
{
case KEYDB_SEARCH_MODE_SHORT_KID:
- snprintf (kidbuf, sizeof kidbuf, "%08lX", (ulong)desc.u.kid[1]);
+ snprintf (kidbuf, sizeof kidbuf, "0x%08lX", (ulong)desc.u.kid[1]);
break;
case KEYDB_SEARCH_MODE_LONG_KID:
- snprintf (kidbuf, sizeof kidbuf, "%08lX%08lX",
+ snprintf (kidbuf, sizeof kidbuf, "0x%08lX%08lX",
(ulong)desc.u.kid[0], (ulong)desc.u.kid[1]);
break;
case KEYDB_SEARCH_MODE_FPR20:
case KEYDB_SEARCH_MODE_FPR:
/* This is a v4 fingerprint. */
- bin2hex (desc.u.fpr, 20, kidbuf);
+ kidbuf[0] = '0';
+ kidbuf[1] = 'x';
+ bin2hex (desc.u.fpr, 20, kidbuf+2);
+ break;
+
+ case KEYDB_SEARCH_MODE_EXACT:
+ exactname = desc.u.name;
break;
case KEYDB_SEARCH_MODE_FPR16:
return gpg_error (GPG_ERR_INV_USER_ID);
}
- reselect = 0;
- again:
- /* Build the request string. */
- xfree (hostport);
- hostport = make_host_part (ctrl, uri->scheme, uri->host, uri->port, reselect);
- if (!hostport)
+ searchkey = http_escape_string (exactname? exactname : kidbuf,
+ EXTRA_ESCAPE_CHARS);
+ if (!searchkey)
{
err = gpg_error_from_syserror ();
goto leave;
}
+ reselect = 0;
+ again:
+ /* Build the request string. */
+ xfree (hostport); hostport = NULL;
+ xfree (httphost); httphost = NULL;
+ err = make_host_part (ctrl, uri->scheme, uri->host, uri->port, reselect,
+ &hostport, &httpflags, &httphost);
+ if (err)
+ goto leave;
+
xfree (request);
request = strconcat (hostport,
- "/pks/lookup?op=get&options=mr&search=0x",
- kidbuf,
+ "/pks/lookup?op=get&options=mr&search=",
+ searchkey,
+ exactname? "&exact=on":"",
NULL);
if (!request)
{
}
/* Send the request. */
- err = send_request (ctrl, request, hostport, NULL, NULL, &fp);
+ err = send_request (ctrl, request, hostport, httphost, httpflags,
+ NULL, NULL, &fp);
if (handle_send_request_error (err, request, &tries))
{
reselect = 1;
es_fclose (fp);
xfree (request);
xfree (hostport);
+ xfree (httphost);
+ xfree (searchkey);
return err;
}
}
-/* Send the key in {DATA,DATALEN} to the keyserver identified by URI. */
+/* Send the key in {DATA,DATALEN} to the keyserver identified by URI. */
gpg_error_t
ks_hkp_put (ctrl_t ctrl, parsed_uri_t uri, const void *data, size_t datalen)
{
struct put_post_parm_s parm;
char *armored = NULL;
int reselect;
+ char *httphost = NULL;
+ unsigned int httpflags;
unsigned int tries = SEND_REQUEST_RETRIES;
parm.datastring = NULL;
/* Build the request string. */
reselect = 0;
again:
- xfree (hostport);
- hostport = make_host_part (ctrl, uri->scheme, uri->host, uri->port, reselect);
- if (!hostport)
- {
- err = gpg_error_from_syserror ();
- goto leave;
- }
+ xfree (hostport); hostport = NULL;
+ xfree (httphost); httphost = NULL;
+ err = make_host_part (ctrl, uri->scheme, uri->host, uri->port, reselect,
+ &hostport, &httpflags, &httphost);
+ if (err)
+ goto leave;
xfree (request);
request = strconcat (hostport, "/pks/add", NULL);
}
/* Send the request. */
- err = send_request (ctrl, request, hostport, put_post_cb, &parm, &fp);
+ err = send_request (ctrl, request, hostport, httphost, 0,
+ put_post_cb, &parm, &fp);
if (handle_send_request_error (err, request, &tries))
{
reselect = 1;
xfree (armored);
xfree (request);
xfree (hostport);
+ xfree (httphost);
return err;
}