1 /* ks-engine-http.c - HTTP OpenPGP key access
2 * Copyright (C) 2011 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
29 #include "ks-engine.h"
31 /* How many redirections do we allow. */
32 #define MAX_REDIRECTS 2
34 /* Print a help output for the schemata supported by this module. */
36 ks_http_help (ctrl_t ctrl, parsed_uri_t uri)
39 "Handler for HTTP URLs:\n"
41 #if HTTP_USE_GNUTLS || HTTP_USE_NTBTLS
44 "Supported methods: fetch\n";
47 #if HTTP_USE_GNUTLS || HTTP_USE_NTBTLS
48 const char data2[] = " http\n https";
50 const char data2[] = " http";
54 err = ks_print_help (ctrl, data2);
55 else if (uri->is_http && strcmp (uri->scheme, "hkp"))
56 err = ks_print_help (ctrl, data);
64 /* Get the key from URL which is expected to specify a http style
65 scheme. On success R_FP has an open stream to read the data. */
67 ks_http_fetch (ctrl_t ctrl, const char *url, estream_t *r_fp)
70 http_session_t session = NULL;
72 int redirects_left = MAX_REDIRECTS;
74 char *request_buffer = NULL;
77 /* Note that we only use the system provided certificates with the
79 err = http_session_new (&session, NULL, NULL, HTTP_FLAG_TRUST_SYS);
82 http_session_set_log_cb (session, cert_log_cb);
85 err = http_open (&http,
89 /* fixme: AUTH */ NULL,
90 ((opt.honor_http_proxy? HTTP_FLAG_TRY_PROXY:0)
91 | (dirmngr_use_tor ()? HTTP_FLAG_FORCE_TOR:0)
92 | (opt.disable_ipv4? HTTP_FLAG_IGNORE_IPv4 : 0)
93 | (opt.disable_ipv6? HTTP_FLAG_IGNORE_IPv6 : 0)),
97 /*FIXME curl->srvtag*/NULL);
100 fp = http_get_write_ptr (http);
101 /* Avoid caches to get the most recent copy of the key. We set
102 both the Pragma and Cache-Control versions of the header, so
103 we're good with both HTTP 1.0 and 1.1. */
104 es_fputs ("Pragma: no-cache\r\n"
105 "Cache-Control: no-cache\r\n", fp);
106 http_start_data (http);
108 err = gpg_error_from_syserror ();
112 /* Fixme: After a redirection we show the old host name. */
113 log_error (_("error connecting to '%s': %s\n"),
114 url, gpg_strerror (err));
118 /* Wait for the response. */
120 err = http_wait_response (http);
123 log_error (_("error reading HTTP response for '%s': %s\n"),
124 url, gpg_strerror (err));
128 switch (http_get_status_code (http))
132 break; /* Success. */
138 const char *s = http_get_header (http, "Location");
140 log_info (_("URL '%s' redirected to '%s' (%u)\n"),
141 url, s?s:"[none]", http_get_status_code (http));
142 if (s && *s && redirects_left-- )
144 xfree (request_buffer);
145 request_buffer = xtrystrdup (s);
148 url = request_buffer;
149 http_close (http, 0);
151 http_session_release (session);
154 err = gpg_error_from_syserror ();
157 err = gpg_error (GPG_ERR_NO_DATA);
158 log_error (_("too many redirections\n"));
163 log_error (_("error accessing '%s': http status %u\n"),
164 url, http_get_status_code (http));
165 err = gpg_error (GPG_ERR_NO_DATA);
169 fp = http_get_read_ptr (http);
172 err = gpg_error (GPG_ERR_BUG);
176 /* Return the read stream and close the HTTP context. */
178 http_close (http, 1);
182 http_close (http, 0);
183 http_session_release (session);
184 xfree (request_buffer);