2011-02-09 15:42:29 +01:00
|
|
|
/* ks-engine-http.c - HTTP OpenPGP key access
|
|
|
|
* Copyright (C) 2011 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* 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-02-09 15:42:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
/* How many redirections do we allow. */
|
|
|
|
#define MAX_REDIRECTS 2
|
|
|
|
|
2011-02-09 17:48:00 +01:00
|
|
|
/* Print a help output for the schemata supported by this module. */
|
|
|
|
gpg_error_t
|
|
|
|
ks_http_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 HTTP URLs:\n"
|
|
|
|
" http://\n"
|
2016-04-15 17:19:40 +02:00
|
|
|
#if HTTP_USE_GNUTLS || HTTP_USE_NTBTLS
|
2014-09-10 10:37:48 +02:00
|
|
|
" https://\n"
|
2016-04-15 17:19:40 +02:00
|
|
|
#endif
|
2011-02-09 17:48:00 +01:00
|
|
|
"Supported methods: fetch\n";
|
|
|
|
gpg_error_t err;
|
|
|
|
|
2016-04-15 17:50:07 +02:00
|
|
|
#if HTTP_USE_GNUTLS || HTTP_USE_NTBTLS
|
|
|
|
const char data2[] = " http\n https";
|
|
|
|
#else
|
|
|
|
const char data2[] = " http";
|
|
|
|
#endif
|
|
|
|
|
2011-02-09 17:48:00 +01:00
|
|
|
if (!uri)
|
2016-04-15 17:50:07 +02:00
|
|
|
err = ks_print_help (ctrl, data2);
|
2011-11-24 15:48:24 +01:00
|
|
|
else if (uri->is_http && strcmp (uri->scheme, "hkp"))
|
2011-02-09 17:48:00 +01:00
|
|
|
err = ks_print_help (ctrl, data);
|
|
|
|
else
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-02-09 15:42:29 +01:00
|
|
|
|
|
|
|
/* Get the key from URL which is expected to specify a http style
|
2018-04-25 09:43:18 +02:00
|
|
|
* scheme. On success R_FP has an open stream to read the data.
|
|
|
|
* Despite its name this function is also used to retrieve arbitrary
|
|
|
|
* data via https or http.
|
|
|
|
*/
|
2011-02-09 15:42:29 +01:00
|
|
|
gpg_error_t
|
2018-04-25 12:37:34 +02:00
|
|
|
ks_http_fetch (ctrl_t ctrl, const char *url, unsigned int flags,
|
|
|
|
estream_t *r_fp)
|
2011-02-09 15:42:29 +01:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
2014-09-10 10:37:48 +02:00
|
|
|
http_session_t session = NULL;
|
2018-04-25 12:37:34 +02:00
|
|
|
unsigned int session_flags;
|
2011-02-09 15:42:29 +01:00
|
|
|
http_t http = NULL;
|
2018-11-22 22:27:56 +01:00
|
|
|
http_redir_info_t redirinfo = { MAX_REDIRECTS };
|
2011-02-09 15:42:29 +01:00
|
|
|
estream_t fp = NULL;
|
|
|
|
char *request_buffer = NULL;
|
2017-07-19 16:02:05 +02:00
|
|
|
parsed_uri_t uri = NULL;
|
|
|
|
|
|
|
|
err = http_parse_uri (&uri, url, 0);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
2018-11-22 22:27:56 +01:00
|
|
|
redirinfo.orig_url = url;
|
|
|
|
redirinfo.orig_onion = uri->onion;
|
|
|
|
redirinfo.orig_https = uri->use_tls;
|
|
|
|
redirinfo.allow_downgrade = !!(flags & KS_HTTP_FETCH_ALLOW_DOWNGRADE);
|
2011-02-09 15:42:29 +01:00
|
|
|
|
2018-04-25 09:43:18 +02:00
|
|
|
/* By default we only use the system provided certificates with this
|
2018-04-25 12:37:34 +02:00
|
|
|
* fetch command. */
|
|
|
|
session_flags = HTTP_FLAG_TRUST_SYS;
|
|
|
|
if ((flags & KS_HTTP_FETCH_NO_CRL) || ctrl->http_no_crl)
|
|
|
|
session_flags |= HTTP_FLAG_NO_CRL;
|
|
|
|
if ((flags & KS_HTTP_FETCH_TRUST_CFG))
|
|
|
|
session_flags |= HTTP_FLAG_TRUST_CFG;
|
|
|
|
|
|
|
|
once_more:
|
|
|
|
err = http_session_new (&session, NULL, session_flags,
|
2017-02-19 10:36:43 +01:00
|
|
|
gnupg_http_tls_verify_cb, ctrl);
|
2014-09-10 10:37:48 +02:00
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
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-09-10 10:37:48 +02:00
|
|
|
|
2011-02-09 15:42:29 +01:00
|
|
|
*r_fp = NULL;
|
2018-07-25 14:35:04 +02:00
|
|
|
err = http_open (ctrl, &http,
|
2011-02-09 15:42:29 +01:00
|
|
|
HTTP_REQ_GET,
|
|
|
|
url,
|
2014-05-16 20:58:58 +02:00
|
|
|
/* httphost */ NULL,
|
2011-02-09 15:42:29 +01:00
|
|
|
/* fixme: AUTH */ NULL,
|
2015-09-18 16:17:11 +02:00
|
|
|
((opt.honor_http_proxy? HTTP_FLAG_TRY_PROXY:0)
|
2018-04-25 09:43:18 +02:00
|
|
|
| (DBG_LOOKUP? HTTP_FLAG_LOG_RESP: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-09-10 10:37:48 +02:00
|
|
|
session,
|
|
|
|
NULL,
|
2011-02-09 15:42:29 +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
|
2018-04-25 09:43:18 +02:00
|
|
|
* both the Pragma and Cache-Control versions of the header, so
|
|
|
|
* we're good with both HTTP 1.0 and 1.1. */
|
2018-04-25 12:37:34 +02:00
|
|
|
if ((flags & KS_HTTP_FETCH_NOCACHE))
|
2018-04-25 09:43:18 +02:00
|
|
|
es_fputs ("Pragma: no-cache\r\n"
|
|
|
|
"Cache-Control: no-cache\r\n", fp);
|
2011-02-09 15:42:29 +01:00
|
|
|
http_start_data (http);
|
|
|
|
if (es_ferror (fp))
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
}
|
|
|
|
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-02-09 15:42:29 +01:00
|
|
|
url, 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-02-09 15:42:29 +01:00
|
|
|
url, gpg_strerror (err));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (http_get_status_code (http))
|
|
|
|
{
|
|
|
|
case 200:
|
|
|
|
err = 0;
|
|
|
|
break; /* Success. */
|
|
|
|
|
|
|
|
case 301:
|
|
|
|
case 302:
|
2014-09-10 10:37:48 +02:00
|
|
|
case 307:
|
2011-02-09 15:42:29 +01:00
|
|
|
{
|
2018-11-22 22:27:56 +01:00
|
|
|
xfree (request_buffer);
|
|
|
|
err = http_prepare_redirect (&redirinfo, http_get_status_code (http),
|
|
|
|
http_get_header (http, "Location"),
|
|
|
|
&request_buffer);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
url = request_buffer;
|
|
|
|
http_close (http, 0);
|
|
|
|
http = NULL;
|
|
|
|
http_session_release (session);
|
|
|
|
session = NULL;
|
2011-02-09 15:42:29 +01:00
|
|
|
}
|
2018-11-22 22:27:56 +01:00
|
|
|
goto once_more;
|
2011-02-09 15:42:29 +01:00
|
|
|
|
|
|
|
default:
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("error accessing '%s': http status %u\n"),
|
2011-02-09 15:42:29 +01:00
|
|
|
url, http_get_status_code (http));
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-10 10:37:48 +02:00
|
|
|
http_session_release (session);
|
2011-02-09 15:42:29 +01:00
|
|
|
xfree (request_buffer);
|
2017-07-19 16:02:05 +02:00
|
|
|
http_release_parsed_uri (uri);
|
2011-02-09 15:42:29 +01:00
|
|
|
return err;
|
|
|
|
}
|