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
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
const char const data[] =
|
|
|
|
"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
|
|
|
|
scheme. On success R_FP has an open stream to read the data. */
|
|
|
|
gpg_error_t
|
|
|
|
ks_http_fetch (ctrl_t ctrl, const char *url, estream_t *r_fp)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
2014-09-10 10:37:48 +02:00
|
|
|
http_session_t session = NULL;
|
2011-02-09 15:42:29 +01:00
|
|
|
http_t http = NULL;
|
|
|
|
int redirects_left = MAX_REDIRECTS;
|
|
|
|
estream_t fp = NULL;
|
|
|
|
char *request_buffer = NULL;
|
|
|
|
|
2016-05-29 16:55:42 +02:00
|
|
|
once_more:
|
2016-04-27 08:18:37 +02:00
|
|
|
/* Note that we only use the system provided certificates with the
|
|
|
|
* fetch command. */
|
|
|
|
err = http_session_new (&session, NULL, NULL, HTTP_FLAG_TRUST_SYS);
|
2014-09-10 10:37:48 +02:00
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
http_session_set_log_cb (session, cert_log_cb);
|
|
|
|
|
2011-02-09 15:42:29 +01:00
|
|
|
*r_fp = NULL;
|
|
|
|
err = http_open (&http,
|
|
|
|
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)
|
|
|
|
| (opt.use_tor? HTTP_FLAG_FORCE_TOR: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
|
|
|
|
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);
|
|
|
|
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
|
|
|
{
|
|
|
|
const char *s = http_get_header (http, "Location");
|
|
|
|
|
2012-06-05 19:29:22 +02:00
|
|
|
log_info (_("URL '%s' redirected to '%s' (%u)\n"),
|
2011-02-09 15:42:29 +01:00
|
|
|
url, s?s:"[none]", http_get_status_code (http));
|
|
|
|
if (s && *s && redirects_left-- )
|
|
|
|
{
|
|
|
|
xfree (request_buffer);
|
|
|
|
request_buffer = xtrystrdup (s);
|
|
|
|
if (request_buffer)
|
|
|
|
{
|
|
|
|
url = request_buffer;
|
|
|
|
http_close (http, 0);
|
|
|
|
http = NULL;
|
2016-05-29 16:55:42 +02:00
|
|
|
http_session_release (session);
|
2011-02-09 15:42:29 +01:00
|
|
|
goto once_more;
|
|
|
|
}
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
err = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
log_error (_("too many redirections\n"));
|
|
|
|
}
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
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);
|
|
|
|
return err;
|
|
|
|
}
|