2011-01-10 14:30:17 +01:00
|
|
|
/* ks-action.c - OpenPGP keyserver actions
|
|
|
|
* Copyright (C) 2011 Free Software Foundation, Inc.
|
2014-03-11 14:26:39 +01:00
|
|
|
* Copyright (C) 2011, 2014 Werner Koch
|
2015-03-28 17:23:56 +01:00
|
|
|
* Copyright (C) 2015 g10 Code GmbH
|
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
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "dirmngr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "ks-engine.h"
|
|
|
|
#include "ks-action.h"
|
2015-04-13 12:02:40 +02:00
|
|
|
#if USE_LDAP
|
|
|
|
# include "ldap-parse-uri.h"
|
|
|
|
#endif
|
2011-01-10 14:30:17 +01:00
|
|
|
|
2011-02-09 17:48:00 +01:00
|
|
|
/* Called by the engine's help functions to print the actual help. */
|
|
|
|
gpg_error_t
|
|
|
|
ks_print_help (ctrl_t ctrl, const char *text)
|
|
|
|
{
|
|
|
|
return dirmngr_status_help (ctrl, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-11 14:26:39 +01:00
|
|
|
/* Called by the engine's help functions to print the actual help. */
|
|
|
|
gpg_error_t
|
|
|
|
ks_printf_help (ctrl_t ctrl, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
|
|
|
gpg_error_t err;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
va_start (arg_ptr, format);
|
2014-08-26 17:47:22 +02:00
|
|
|
buf = es_vbsprintf (format, arg_ptr);
|
2014-03-11 14:26:39 +01:00
|
|
|
err = buf? 0 : gpg_error_from_syserror ();
|
|
|
|
va_end (arg_ptr);
|
|
|
|
if (!err)
|
|
|
|
err = dirmngr_status_help (ctrl, buf);
|
|
|
|
es_free (buf);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-09 17:48:00 +01:00
|
|
|
/* Run the help command for the engine responsible for URI. */
|
|
|
|
gpg_error_t
|
|
|
|
ks_action_help (ctrl_t ctrl, const char *url)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
parsed_uri_t parsed_uri; /* The broken down URI. */
|
|
|
|
|
|
|
|
if (!url || !*url)
|
|
|
|
{
|
|
|
|
ks_print_help (ctrl, "Known schemata:\n");
|
|
|
|
parsed_uri = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-13 12:02:40 +02:00
|
|
|
#if USE_LDAP
|
2015-03-19 11:02:46 +01:00
|
|
|
if (ldap_uri_p (url))
|
|
|
|
err = ldap_parse_uri (&parsed_uri, url);
|
|
|
|
else
|
2015-04-13 12:02:40 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
err = http_parse_uri (&parsed_uri, url, 1);
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
2011-02-09 17:48:00 +01:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-04-12 16:30:08 +02:00
|
|
|
/* Call all engines to give them a chance to print a help sting. */
|
2011-02-09 17:48:00 +01:00
|
|
|
err = ks_hkp_help (ctrl, parsed_uri);
|
|
|
|
if (!err)
|
|
|
|
err = ks_http_help (ctrl, parsed_uri);
|
|
|
|
if (!err)
|
|
|
|
err = ks_finger_help (ctrl, parsed_uri);
|
|
|
|
if (!err)
|
|
|
|
err = ks_kdns_help (ctrl, parsed_uri);
|
2015-04-13 12:02:40 +02:00
|
|
|
#if USE_LDAP
|
2015-03-19 11:02:46 +01:00
|
|
|
if (!err)
|
|
|
|
err = ks_ldap_help (ctrl, parsed_uri);
|
2015-04-13 12:02:40 +02:00
|
|
|
#endif
|
2011-02-09 17:48:00 +01:00
|
|
|
|
|
|
|
if (!parsed_uri)
|
|
|
|
ks_print_help (ctrl,
|
2011-11-24 15:48:24 +01:00
|
|
|
"(Use an URL for engine specific help.)");
|
2011-02-09 17:48:00 +01:00
|
|
|
else
|
|
|
|
http_release_parsed_uri (parsed_uri);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-01-10 14:30:17 +01:00
|
|
|
|
2014-03-11 14:26:39 +01:00
|
|
|
/* Resolve all host names. This is useful for looking at the status
|
|
|
|
of configured keyservers. */
|
|
|
|
gpg_error_t
|
2015-03-28 17:23:56 +01:00
|
|
|
ks_action_resolve (ctrl_t ctrl, uri_item_t keyservers)
|
2014-03-11 14:26:39 +01:00
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
2014-03-12 14:32:34 +01:00
|
|
|
int any_server = 0;
|
2014-03-11 14:26:39 +01:00
|
|
|
uri_item_t uri;
|
|
|
|
|
2015-03-28 17:23:56 +01:00
|
|
|
for (uri = keyservers; !err && uri; uri = uri->next)
|
2014-03-11 14:26:39 +01:00
|
|
|
{
|
|
|
|
if (uri->parsed_uri->is_http)
|
|
|
|
{
|
2014-03-12 14:32:34 +01:00
|
|
|
any_server = 1;
|
2014-03-11 14:26:39 +01:00
|
|
|
err = ks_hkp_resolve (ctrl, uri->parsed_uri);
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
if (!any_server)
|
2014-03-11 14:26:39 +01:00
|
|
|
err = gpg_error (GPG_ERR_NO_KEYSERVER);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-10 14:30:17 +01:00
|
|
|
/* Search all configured keyservers for keys matching PATTERNS and
|
|
|
|
write the result to the provided output stream. */
|
|
|
|
gpg_error_t
|
2015-03-28 17:23:56 +01:00
|
|
|
ks_action_search (ctrl_t ctrl, uri_item_t keyservers,
|
|
|
|
strlist_t patterns, estream_t outfp)
|
2011-01-10 14:30:17 +01:00
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
2014-03-12 14:32:34 +01:00
|
|
|
int any_server = 0;
|
2015-12-03 16:54:06 +01:00
|
|
|
int any_results = 0;
|
2011-01-10 14:30:17 +01:00
|
|
|
uri_item_t uri;
|
|
|
|
estream_t infp;
|
|
|
|
|
|
|
|
if (!patterns)
|
|
|
|
return gpg_error (GPG_ERR_NO_USER_ID);
|
|
|
|
|
|
|
|
/* FIXME: We only take care of the first pattern. To fully support
|
|
|
|
multiple patterns we might either want to run several queries in
|
|
|
|
parallel and merge them. We also need to decide what to do with
|
|
|
|
errors - it might not be the best idea to ignore an error from
|
|
|
|
one server and silently continue with another server. For now we
|
2015-12-04 12:32:20 +01:00
|
|
|
stop at the first error, unless the server responds with '404 Not
|
|
|
|
Found', in which case we try the next server. */
|
2015-03-28 17:23:56 +01:00
|
|
|
for (uri = keyservers; !err && uri; uri = uri->next)
|
2011-01-10 14:30:17 +01:00
|
|
|
{
|
2015-03-19 11:02:46 +01:00
|
|
|
int is_http = uri->parsed_uri->is_http;
|
2015-04-13 12:02:40 +02:00
|
|
|
int is_ldap = 0;
|
2015-12-07 11:21:47 +01:00
|
|
|
unsigned int http_status = 0;
|
2015-04-13 12:02:40 +02:00
|
|
|
#if USE_LDAP
|
|
|
|
is_ldap = (strcmp (uri->parsed_uri->scheme, "ldap") == 0
|
|
|
|
|| strcmp (uri->parsed_uri->scheme, "ldaps") == 0
|
|
|
|
|| strcmp (uri->parsed_uri->scheme, "ldapi") == 0);
|
|
|
|
#endif
|
2015-03-19 11:02:46 +01:00
|
|
|
if (is_http || is_ldap)
|
2011-01-10 14:30:17 +01:00
|
|
|
{
|
2014-03-12 14:32:34 +01:00
|
|
|
any_server = 1;
|
2015-04-13 12:02:40 +02:00
|
|
|
#if USE_LDAP
|
|
|
|
if (is_ldap)
|
2015-03-19 11:02:46 +01:00
|
|
|
err = ks_ldap_search (ctrl, uri->parsed_uri, patterns->d, &infp);
|
2015-04-13 12:02:40 +02:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2015-12-04 12:32:20 +01:00
|
|
|
err = ks_hkp_search (ctrl, uri->parsed_uri, patterns->d,
|
|
|
|
&infp, &http_status);
|
2015-04-13 12:02:40 +02:00
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
2015-12-04 12:32:20 +01:00
|
|
|
if (err == gpg_error (GPG_ERR_NO_DATA)
|
|
|
|
&& http_status == 404 /* not found */)
|
2015-12-03 16:54:06 +01:00
|
|
|
{
|
|
|
|
/* No record found. Clear error and try next server. */
|
|
|
|
err = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-10 14:30:17 +01:00
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
err = copy_stream (infp, outfp);
|
|
|
|
es_fclose (infp);
|
2015-12-03 16:54:06 +01:00
|
|
|
any_results = 1;
|
2011-01-18 12:51:16 +01:00
|
|
|
break;
|
2011-01-10 14:30:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
if (!any_server)
|
2011-01-10 14:30:17 +01:00
|
|
|
err = gpg_error (GPG_ERR_NO_KEYSERVER);
|
2015-12-03 16:54:06 +01:00
|
|
|
else if (err == 0 && !any_results)
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
2011-01-10 14:30:17 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-01-18 12:51:16 +01:00
|
|
|
|
2011-01-20 14:12:53 +01:00
|
|
|
/* Get the requested keys (matching PATTERNS) using all configured
|
2011-01-18 12:51:16 +01:00
|
|
|
keyservers and write the result to the provided output stream. */
|
|
|
|
gpg_error_t
|
2015-03-28 17:23:56 +01:00
|
|
|
ks_action_get (ctrl_t ctrl, uri_item_t keyservers,
|
|
|
|
strlist_t patterns, estream_t outfp)
|
2011-01-18 12:51:16 +01:00
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
gpg_error_t first_err = 0;
|
2014-03-12 14:32:34 +01:00
|
|
|
int any_server = 0;
|
|
|
|
int any_data = 0;
|
2011-01-18 12:51:16 +01:00
|
|
|
strlist_t sl;
|
|
|
|
uri_item_t uri;
|
|
|
|
estream_t infp;
|
|
|
|
|
|
|
|
if (!patterns)
|
|
|
|
return gpg_error (GPG_ERR_NO_USER_ID);
|
|
|
|
|
|
|
|
/* FIXME: We only take care of the first keyserver. To fully
|
|
|
|
support multiple keyservers we need to track the result for each
|
|
|
|
pattern and use the next keyserver if one key was not found. The
|
|
|
|
keyservers might not all be fully synced thus it is not clear
|
|
|
|
whether the first keyserver has the freshest copy of the key.
|
|
|
|
Need to think about a better strategy. */
|
2015-03-28 17:23:56 +01:00
|
|
|
for (uri = keyservers; !err && uri; uri = uri->next)
|
2011-01-18 12:51:16 +01:00
|
|
|
{
|
2015-03-19 11:02:46 +01:00
|
|
|
int is_http = uri->parsed_uri->is_http;
|
2015-04-13 12:02:40 +02:00
|
|
|
int is_ldap = 0;
|
|
|
|
|
|
|
|
#if USE_LDAP
|
|
|
|
is_ldap = (strcmp (uri->parsed_uri->scheme, "ldap") == 0
|
|
|
|
|| strcmp (uri->parsed_uri->scheme, "ldaps") == 0
|
|
|
|
|| strcmp (uri->parsed_uri->scheme, "ldapi") == 0);
|
|
|
|
#endif
|
|
|
|
|
2015-03-19 11:02:46 +01:00
|
|
|
if (is_http || is_ldap)
|
2011-01-18 12:51:16 +01:00
|
|
|
{
|
2014-03-12 14:32:34 +01:00
|
|
|
any_server = 1;
|
2011-01-18 12:51:16 +01:00
|
|
|
for (sl = patterns; !err && sl; sl = sl->next)
|
|
|
|
{
|
2015-04-13 12:02:40 +02:00
|
|
|
#if USE_LDAP
|
|
|
|
if (is_ldap)
|
2015-03-19 11:02:46 +01:00
|
|
|
err = ks_ldap_get (ctrl, uri->parsed_uri, sl->d, &infp);
|
2015-04-13 12:02:40 +02:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
err = ks_hkp_get (ctrl, uri->parsed_uri, sl->d, &infp);
|
|
|
|
}
|
2015-03-19 11:02:46 +01:00
|
|
|
|
2011-01-18 12:51:16 +01:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
/* It is possible that a server does not carry a
|
|
|
|
key, thus we only save the error and continue
|
|
|
|
with the next pattern. FIXME: It is an open
|
|
|
|
question how to return such an error condition to
|
|
|
|
the caller. */
|
|
|
|
first_err = err;
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = copy_stream (infp, outfp);
|
2011-02-08 21:11:19 +01:00
|
|
|
/* Reading from the keyserver should never fail, thus
|
2011-01-18 12:51:16 +01:00
|
|
|
return this error. */
|
2014-03-12 14:32:34 +01:00
|
|
|
if (!err)
|
|
|
|
any_data = 1;
|
2011-01-18 12:51:16 +01:00
|
|
|
es_fclose (infp);
|
|
|
|
infp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 14:32:34 +01:00
|
|
|
if (any_data)
|
|
|
|
break; /* Stop loop after a keyserver returned something. */
|
2011-01-18 12:51:16 +01:00
|
|
|
}
|
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
if (!any_server)
|
2011-01-18 12:51:16 +01:00
|
|
|
err = gpg_error (GPG_ERR_NO_KEYSERVER);
|
2014-03-12 14:32:34 +01:00
|
|
|
else if (!err && first_err && !any_data)
|
|
|
|
err = first_err;
|
2011-01-18 12:51:16 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-01-20 14:12:53 +01:00
|
|
|
|
2014-03-11 14:26:39 +01:00
|
|
|
/* Retrieve keys from URL and write the result to the provided output
|
2011-02-08 21:11:19 +01:00
|
|
|
stream OUTFP. */
|
|
|
|
gpg_error_t
|
|
|
|
ks_action_fetch (ctrl_t ctrl, const char *url, estream_t outfp)
|
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
estream_t infp;
|
|
|
|
parsed_uri_t parsed_uri; /* The broken down URI. */
|
|
|
|
|
|
|
|
if (!url)
|
|
|
|
return gpg_error (GPG_ERR_INV_URI);
|
|
|
|
|
|
|
|
err = http_parse_uri (&parsed_uri, url, 1);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (parsed_uri->is_http)
|
|
|
|
{
|
2011-02-09 15:42:29 +01:00
|
|
|
err = ks_http_fetch (ctrl, url, &infp);
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
err = copy_stream (infp, outfp);
|
|
|
|
es_fclose (infp);
|
|
|
|
}
|
2011-02-08 21:11:19 +01:00
|
|
|
}
|
|
|
|
else if (!parsed_uri->opaque)
|
|
|
|
{
|
|
|
|
err = gpg_error (GPG_ERR_INV_URI);
|
|
|
|
}
|
|
|
|
else if (!strcmp (parsed_uri->scheme, "finger"))
|
|
|
|
{
|
2011-02-09 15:42:29 +01:00
|
|
|
err = ks_finger_fetch (ctrl, parsed_uri, &infp);
|
2011-02-08 21:11:19 +01:00
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
err = copy_stream (infp, outfp);
|
|
|
|
es_fclose (infp);
|
|
|
|
}
|
|
|
|
}
|
2011-02-09 17:48:00 +01:00
|
|
|
else if (!strcmp (parsed_uri->scheme, "kdns"))
|
|
|
|
{
|
|
|
|
err = ks_kdns_fetch (ctrl, parsed_uri, &infp);
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
err = copy_stream (infp, outfp);
|
|
|
|
es_fclose (infp);
|
|
|
|
}
|
|
|
|
}
|
2011-02-08 21:11:19 +01:00
|
|
|
else
|
|
|
|
err = gpg_error (GPG_ERR_INV_URI);
|
|
|
|
|
|
|
|
http_release_parsed_uri (parsed_uri);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-20 14:12:53 +01:00
|
|
|
|
|
|
|
/* Send an OpenPGP key to all keyservers. The key in {DATA,DATALEN}
|
2015-03-19 11:02:46 +01:00
|
|
|
is expected to be in OpenPGP binary transport format. The metadata
|
|
|
|
in {INFO,INFOLEN} is in colon-separated format (concretely, it is
|
|
|
|
the output of 'for x in keys sigs; do gpg --list-$x --with-colons
|
|
|
|
KEYID; done'. This function may modify DATA and INFO. If this is
|
|
|
|
a problem, then the caller should create a copy. */
|
2011-01-20 14:12:53 +01:00
|
|
|
gpg_error_t
|
2015-03-28 17:23:56 +01:00
|
|
|
ks_action_put (ctrl_t ctrl, uri_item_t keyservers,
|
|
|
|
void *data, size_t datalen,
|
2015-03-19 11:02:46 +01:00
|
|
|
void *info, size_t infolen)
|
2011-01-20 14:12:53 +01:00
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
gpg_error_t first_err = 0;
|
2014-03-12 14:32:34 +01:00
|
|
|
int any_server = 0;
|
2011-01-20 14:12:53 +01:00
|
|
|
uri_item_t uri;
|
|
|
|
|
2015-04-13 12:02:40 +02:00
|
|
|
(void) info;
|
|
|
|
(void) infolen;
|
|
|
|
|
2015-03-28 17:23:56 +01:00
|
|
|
for (uri = keyservers; !err && uri; uri = uri->next)
|
2011-01-20 14:12:53 +01:00
|
|
|
{
|
2015-03-19 11:02:46 +01:00
|
|
|
int is_http = uri->parsed_uri->is_http;
|
2015-04-13 12:02:40 +02:00
|
|
|
int is_ldap = 0;
|
|
|
|
|
|
|
|
#if USE_LDAP
|
|
|
|
is_ldap = (strcmp (uri->parsed_uri->scheme, "ldap") == 0
|
|
|
|
|| strcmp (uri->parsed_uri->scheme, "ldaps") == 0
|
|
|
|
|| strcmp (uri->parsed_uri->scheme, "ldapi") == 0);
|
|
|
|
#endif
|
2015-03-19 11:02:46 +01:00
|
|
|
|
|
|
|
if (is_http || is_ldap)
|
2011-01-20 14:12:53 +01:00
|
|
|
{
|
2014-03-12 14:32:34 +01:00
|
|
|
any_server = 1;
|
2015-04-13 12:02:40 +02:00
|
|
|
#if USE_LDAP
|
|
|
|
if (is_ldap)
|
2015-03-19 11:02:46 +01:00
|
|
|
err = ks_ldap_put (ctrl, uri->parsed_uri, data, datalen,
|
|
|
|
info, infolen);
|
2015-04-13 12:02:40 +02:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
err = ks_hkp_put (ctrl, uri->parsed_uri, data, datalen);
|
|
|
|
}
|
2011-01-20 14:12:53 +01:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
first_err = err;
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2014-03-12 14:32:34 +01:00
|
|
|
if (!any_server)
|
2011-01-20 14:12:53 +01:00
|
|
|
err = gpg_error (GPG_ERR_NO_KEYSERVER);
|
|
|
|
else if (!err && first_err)
|
2014-03-12 14:32:34 +01:00
|
|
|
err = first_err;
|
2011-01-20 14:12:53 +01:00
|
|
|
return err;
|
|
|
|
}
|