2 * keydb_hkp.c - Routines to store and fetch keys from another keyserver.
4 * Copyright 2013 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <curl/curl.h>
27 #include "charfuncs.h"
29 #include "keystructs.h"
32 #include "onak-conf.h"
35 static CURL *curl = NULL;
38 * initdb - Initialize the key database.
40 * We initialize CURL here.
42 static void hkp_initdb(bool readonly)
44 curl_global_init(CURL_GLOBAL_DEFAULT);
45 curl = curl_easy_init();
49 * cleanupdb - De-initialize the key database.
51 * We cleanup CURL here.
53 static void hkp_cleanupdb(void)
56 curl_easy_cleanup(curl);
59 curl_global_cleanup();
63 * Receive data from a CURL request and process it into a buffer context.
65 static size_t hkp_curl_recv_data(void *buffer, size_t size, size_t nmemb,
68 buffer_putchar(ctx, nmemb * size, buffer);
70 return (nmemb * size);
74 * fetch_key - Given a keyid fetch the key from storage.
75 * @keyid: The keyid to fetch.
76 * @publickey: A pointer to a structure to return the key in.
77 * @intrans: If we're already in a transaction.
79 * We use the hex representation of the keyid as the filename to fetch the
80 * key from. The key is stored in the file as a binary OpenPGP stream of
81 * packets, so we can just use read_openpgp_stream() to read the packets
82 * in and then parse_keys() to parse the packets into a publickey
85 static int hkp_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
88 struct openpgp_packet_list *packets = NULL;
91 struct buffer_ctx buf;
95 buf.buffer = malloc(8192);
97 snprintf(keyurl, sizeof(keyurl),
98 "http://%s:11371/pks/lookup?op=get&search=0x%08" PRIX64,
99 config.db_dir, keyid);
101 curl_easy_setopt(curl, CURLOPT_URL, keyurl);
102 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
104 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
105 res = curl_easy_perform(curl);
109 dearmor_openpgp_stream(buffer_fetchchar, &buf, &packets);
110 parse_keys(packets, publickey);
111 free_packet_list(packets);
114 logthing(LOGTHING_ERROR, "Couldn't find key: %s (%d)",
115 curl_easy_strerror(res), res);
119 buf.offset = buf.size = 0;
122 return (res == 0) ? 1 : 0;
126 * fetch_key_text - Tries to find the keys that contain the supplied text.
127 * @search: The text to search for.
128 * @publickey: A pointer to a structure to return the key in.
130 * This function searches for the supplied text and returns the keys that
133 * TODO: Write for flat file access. Some sort of grep?
135 static int hkp_fetch_key_text(const char *search,
136 struct openpgp_publickey **publickey)
138 struct openpgp_packet_list *packets = NULL;
141 struct buffer_ctx buf;
146 buf.buffer = malloc(8192);
148 snprintf(keyurl, sizeof(keyurl),
149 "http://%s:11371/pks/lookup?op=get&search=%s",
150 config.db_dir, search);
152 curl_easy_setopt(curl, CURLOPT_URL, keyurl);
153 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
155 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
156 res = curl_easy_perform(curl);
160 dearmor_openpgp_stream(buffer_fetchchar, &buf, &packets);
161 count = parse_keys(packets, publickey);
162 free_packet_list(packets);
165 logthing(LOGTHING_ERROR, "Couldn't find key: %s (%d)",
166 curl_easy_strerror(res), res);
170 buf.offset = buf.size = 0;
177 * store_key - Takes a key and stores it.
178 * @publickey: A pointer to the public key to store.
179 * @intrans: If we're already in a transaction.
180 * @update: If true the key exists and should be updated.
183 static int hkp_store_key(struct openpgp_publickey *publickey, bool intrans,
186 struct openpgp_packet_list *packets = NULL;
187 struct openpgp_packet_list *list_end = NULL;
190 struct buffer_ctx buf;
195 buf.buffer = malloc(8192);
196 buf.offset = snprintf(buf.buffer, buf.size, "keytextz");
198 flatten_publickey(publickey, &packets, &list_end);
199 armor_openpgp_stream(buffer_putchar, &buf, packets);
200 addform = curl_easy_escape(curl, buf.buffer, buf.offset);
203 snprintf(keyurl, sizeof(keyurl),
204 "http://%s:11371/pks/add",
207 curl_easy_setopt(curl, CURLOPT_URL, keyurl);
208 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, addform);
209 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
212 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
213 res = curl_easy_perform(curl);
216 logthing(LOGTHING_ERROR, "Couldn't send key: %s (%d)",
217 curl_easy_strerror(res), res);
222 /* TODO: buf has any response text we might want to parse. */
224 buf.offset = buf.size = 0;
227 return (res == 0) ? 1 : 0;
231 * delete_key - Given a keyid delete the key from storage.
232 * @keyid: The keyid to delete.
233 * @intrans: If we're already in a transaction.
237 static int hkp_delete_key(uint64_t keyid, bool intrans)
243 * iterate_keys - call a function once for each key in the db.
244 * @iterfunc: The function to call.
245 * @ctx: A context pointer
247 * Not applicable for HKP backend.
249 static int hkp_iterate_keys(void (*iterfunc)(void *ctx,
250 struct openpgp_publickey *key), void *ctx)
256 * starttrans - Start a transaction.
258 * This is just a no-op for HKP access.
260 static bool hkp_starttrans(void)
266 * endtrans - End a transaction.
268 * This is just a no-op for HKP access.
270 static void hkp_endtrans(void)
276 * Include the basic keydb routines.
278 #define NEED_KEYID2UID 1
279 #define NEED_GETKEYSIGS 1
280 #define NEED_GETFULLKEYID 1
281 #define NEED_UPDATEKEYS 1
284 struct dbfuncs keydb_hkp_funcs = {
285 .initdb = hkp_initdb,
286 .cleanupdb = hkp_cleanupdb,
287 .starttrans = hkp_starttrans,
288 .endtrans = hkp_endtrans,
289 .fetch_key = hkp_fetch_key,
290 .fetch_key_text = hkp_fetch_key_text,
291 .store_key = hkp_store_key,
292 .update_keys = generic_update_keys,
293 .delete_key = hkp_delete_key,
294 .getkeysigs = generic_getkeysigs,
295 .cached_getkeysigs = generic_cached_getkeysigs,
296 .keyid2uid = generic_keyid2uid,
297 .getfullkeyid = generic_getfullkeyid,
298 .iterate_keys = hkp_iterate_keys,