]> the.earth.li Git - onak.git/blob - keydb/keydb_hkp.c
Provide key_fetch routine that will not search subkey fingerprints
[onak.git] / keydb / keydb_hkp.c
1 /*
2  * keydb_hkp.c - Routines to store and fetch keys from another keyserver.
3  *
4  * Copyright 2013 Jonathan McDowell <noodles@earth.li>
5  *
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.
9  *
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
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <inttypes.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <curl/curl.h>
25
26 #include "build-config.h"
27
28 #include "armor.h"
29 #include "charfuncs.h"
30 #include "keydb.h"
31 #include "keystructs.h"
32 #include "log.h"
33 #include "mem.h"
34 #include "onak-conf.h"
35 #include "parsekey.h"
36
37 struct onak_hkp_dbctx {
38         struct onak_db_config *config; /* Our DB config info */
39         CURL *curl;
40         char hkpbase[512];
41 };
42
43 static int hkp_parse_url(struct onak_hkp_dbctx *privctx, const char *url)
44 {
45         char proto[6], host[256];
46         unsigned int port;
47         int matched;
48         int ret = 1;
49
50         proto[0] = host[0] = 0;
51         port = 0;
52
53         matched = sscanf(url, "%5[a-z]://%256[a-zA-Z0-9.-]:%u", proto, host,
54                         &port);
55         if (matched < 2) {
56                 proto[0] = 0;
57                 sscanf(url, "%256[a-zA-Z0-9.-]:%u", host, &port);
58         }
59
60         if (host[0] == 0) {
61                 logthing(LOGTHING_CRITICAL, "Couldn't parse HKP host: %s",
62                         url);
63                 ret = 0;
64                 goto out;
65         }
66
67         if (proto[0] == 0 || !strcmp(proto, "hkp")) {
68                 if (port == 0) {
69                         port = 11371;
70                 }
71                 snprintf(privctx->hkpbase, sizeof(privctx->hkpbase),
72                         "http://%s:%u/pks", host, port);
73         } else if (!strcmp(proto, "hkps")) {
74                 if (port == 0) {
75                         port = 11372;
76                 }
77                 snprintf(privctx->hkpbase, sizeof(privctx->hkpbase),
78                         "https://%s:%u/pks", host, port);
79         } else if (strcmp(proto, "http") && strcmp(proto, "https")) {
80                 logthing(LOGTHING_CRITICAL, "Unknown HKP protocol: %s",
81                         proto);
82                 ret = 0;
83                 goto out;
84         } else if (port == 0) {
85                 snprintf(privctx->hkpbase, sizeof(privctx->hkpbase),
86                         "%s://%s/pks", proto, host);
87         } else {
88                 snprintf(privctx->hkpbase, sizeof(privctx->hkpbase),
89                         "%s://%s:%u/pks", proto, host, port);
90         }
91
92 out:
93         return ret;
94 }
95
96 /**
97  *      Receive data from a CURL request and process it into a buffer context.
98  */
99 static size_t hkp_curl_recv_data(void *buffer, size_t size, size_t nmemb,
100                 void *ctx)
101 {
102         buffer_putchar(ctx, nmemb * size, buffer);
103
104         return (nmemb * size);
105 }
106
107 static int hkp_fetch_key_url(struct onak_dbctx *dbctx,
108                 char *url,
109                 struct openpgp_publickey **publickey,
110                 bool intrans)
111 {
112         struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
113         struct openpgp_packet_list *packets = NULL;
114         CURLcode res;
115         struct buffer_ctx buf;
116         int count = 0;
117
118         buf.offset = 0;
119         buf.size = 8192;
120         buf.buffer = malloc(8192);
121
122         curl_easy_setopt(privctx->curl, CURLOPT_URL, url);
123         curl_easy_setopt(privctx->curl, CURLOPT_WRITEFUNCTION,
124                         hkp_curl_recv_data);
125         curl_easy_setopt(privctx->curl, CURLOPT_WRITEDATA, &buf);
126         res = curl_easy_perform(privctx->curl);
127
128         if (res == 0) {
129                 buf.offset = 0;
130                 dearmor_openpgp_stream(buffer_fetchchar, &buf, &packets);
131                 count = parse_keys(packets, publickey);
132                 free_packet_list(packets);
133                 packets = NULL;
134         } else {
135                 logthing(LOGTHING_ERROR, "Couldn't find key: %s (%d)",
136                         curl_easy_strerror(res), res);
137         }
138
139         free(buf.buffer);
140         buf.offset = buf.size = 0;
141         buf.buffer = NULL;
142
143         return count;
144 }
145
146 /**
147  *      hkp_fetch_key_fp - Given a fingerprint fetch the key from HKP server.
148  */
149 static int hkp_fetch_key_fp(struct onak_dbctx *dbctx,
150                 struct openpgp_fingerprint *fingerprint,
151                 struct openpgp_publickey **publickey,
152                 bool intrans)
153 {
154         struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
155         char keyurl[1024];
156         int i, ofs;
157
158         if (fingerprint->length > MAX_FINGERPRINT_LEN) {
159                 return 0;
160         }
161
162         ofs = snprintf(keyurl, sizeof(keyurl),
163                         "%s/lookup?op=get&search=0x", privctx->hkpbase);
164
165         if ((ofs + fingerprint->length * 2 + 1)> sizeof(keyurl)) {
166                 return 0;
167         }
168
169         for (i = 0; i < fingerprint->length; i++) {
170                 ofs += sprintf(&keyurl[ofs], "%02X", fingerprint->fp[i]);
171         }
172
173         return (hkp_fetch_key_url(dbctx, keyurl, publickey, intrans));
174 }
175
176 /**
177  *      hkp_fetch_key_id - Given a keyid fetch the key from HKP server.
178  */
179 static int hkp_fetch_key_id(struct onak_dbctx *dbctx,
180                 uint64_t keyid,
181                 struct openpgp_publickey **publickey,
182                 bool intrans)
183 {
184         struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
185         char keyurl[1024];
186
187         snprintf(keyurl, sizeof(keyurl),
188                         "%s/lookup?op=get&search=0x%08" PRIX64,
189                         privctx->hkpbase, keyid);
190
191         return (hkp_fetch_key_url(dbctx, keyurl, publickey, intrans));
192 }
193
194 /**
195  *      fetch_key_text - Tries to find the keys that contain the supplied text.
196  *      @search: The text to search for.
197  *      @publickey: A pointer to a structure to return the key in.
198  *
199  *      This function searches for the supplied text and returns the keys that
200  *      contain it.
201  *
202  *      TODO: Write for flat file access. Some sort of grep?
203  */
204 static int hkp_fetch_key_text(struct onak_dbctx *dbctx,
205                 const char *search,
206                 struct openpgp_publickey **publickey)
207 {
208         struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
209         char keyurl[1024];
210
211         snprintf(keyurl, sizeof(keyurl),
212                         "%s/lookup?op=get&search=%s",
213                         privctx->hkpbase, search);
214
215         return (hkp_fetch_key_url(dbctx, keyurl, publickey, false));
216 }
217
218 /**
219  *      store_key - Takes a key and stores it.
220  *      @publickey: A pointer to the public key to store.
221  *      @intrans: If we're already in a transaction.
222  *      @update: If true the key exists and should be updated.
223  *
224  */
225 static int hkp_store_key(struct onak_dbctx *dbctx,
226                 struct openpgp_publickey *publickey, bool intrans,
227                 bool update)
228 {
229         struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
230         struct openpgp_packet_list *packets = NULL;
231         struct openpgp_packet_list *list_end = NULL;
232         char keyurl[1024];
233         CURLcode res;
234         struct buffer_ctx buf;
235         char *addform;
236
237         buf.offset = 0;
238         buf.size = 8192;
239         buf.buffer = malloc(8192);
240         buf.offset = snprintf(buf.buffer, buf.size, "keytextz");
241
242         flatten_publickey(publickey, &packets, &list_end);
243         armor_openpgp_stream(buffer_putchar, &buf, packets);
244         addform = curl_easy_escape(privctx->curl, buf.buffer, buf.offset);
245         addform[7] = '=';
246
247         snprintf(keyurl, sizeof(keyurl), "%s/add", privctx->hkpbase);
248
249         curl_easy_setopt(privctx->curl, CURLOPT_URL, keyurl);
250         curl_easy_setopt(privctx->curl, CURLOPT_POSTFIELDS, addform);
251         curl_easy_setopt(privctx->curl, CURLOPT_WRITEFUNCTION,
252                         hkp_curl_recv_data);
253         buf.offset = 0;
254         curl_easy_setopt(privctx->curl, CURLOPT_WRITEDATA, &buf);
255         res = curl_easy_perform(privctx->curl);
256
257         if (res != 0) {
258                 logthing(LOGTHING_ERROR, "Couldn't send key: %s (%d)",
259                         curl_easy_strerror(res), res);
260         }
261
262         curl_free(addform);
263
264         /* TODO: buf has any response text we might want to parse. */
265         free(buf.buffer);
266         buf.offset = buf.size = 0;
267         buf.buffer = NULL;
268
269         return (res == 0) ? 1 : 0;
270 }
271
272 /**
273  *      delete_key - Given a keyid delete the key from storage.
274  *      @fp: The fingerprint of the key to delete.
275  *      @intrans: If we're already in a transaction.
276  *
277  *      No op for HKP.
278  */
279 static int hkp_delete_key(struct onak_dbctx *dbctx,
280                 struct openpgp_fingerprint *fp, bool intrans)
281 {
282         return -1;
283 }
284
285 /**
286  *      iterate_keys - call a function once for each key in the db.
287  *      @iterfunc: The function to call.
288  *      @ctx: A context pointer
289  *
290  *      Not applicable for HKP backend.
291  */
292 static int hkp_iterate_keys(struct onak_dbctx *dbctx,
293                 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
294                 void *ctx)
295 {
296         return 0;
297 }
298
299 /**
300  *      starttrans - Start a transaction.
301  *
302  *      This is just a no-op for HKP access.
303  */
304 static bool hkp_starttrans(struct onak_dbctx *dbctx)
305 {
306         return true;
307 }
308
309 /**
310  *      endtrans - End a transaction.
311  *
312  *      This is just a no-op for HKP access.
313  */
314 static void hkp_endtrans(struct onak_dbctx *dbctx)
315 {
316         return;
317 }
318
319 /*
320  * Include the basic keydb routines.
321  */
322 #define NEED_KEYID2UID 1
323 #define NEED_GETKEYSIGS 1
324 #define NEED_UPDATEKEYS 1
325 #define NEED_GET 1
326 #include "keydb.c"
327
328 /**
329  *      cleanupdb - De-initialize the key database.
330  *
331  *      We cleanup CURL here.
332  */
333 static void hkp_cleanupdb(struct onak_dbctx *dbctx)
334 {
335         struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
336
337         if (privctx->curl) {
338                 curl_easy_cleanup(privctx->curl);
339                 privctx->curl = NULL;
340         }
341         curl_global_cleanup();
342         free(privctx);
343         free(dbctx);
344 }
345
346 /**
347  *      initdb - Initialize the key database.
348  *
349  *      We initialize CURL here.
350  */
351 struct onak_dbctx *keydb_hkp_init(struct onak_db_config *dbcfg, bool readonly)
352 {
353         struct onak_dbctx *dbctx;
354         struct onak_hkp_dbctx *privctx;
355         curl_version_info_data *curl_info;
356
357         dbctx = malloc(sizeof(struct onak_dbctx));
358         if (dbctx == NULL) {
359                 return NULL;
360         }
361
362         dbctx->config = dbcfg;
363         dbctx->priv = privctx = malloc(sizeof(*privctx));
364         dbctx->cleanupdb                = hkp_cleanupdb;
365         dbctx->starttrans               = hkp_starttrans;
366         dbctx->endtrans                 = hkp_endtrans;
367         dbctx->fetch_key                = generic_fetch_key;
368         dbctx->fetch_key_fp             = hkp_fetch_key_fp;
369         dbctx->fetch_key_id             = hkp_fetch_key_id;
370         dbctx->fetch_key_text           = hkp_fetch_key_text;
371         dbctx->store_key                = hkp_store_key;
372         dbctx->update_keys              = generic_update_keys;
373         dbctx->delete_key               = hkp_delete_key;
374         dbctx->getkeysigs               = generic_getkeysigs;
375         dbctx->cached_getkeysigs        = generic_cached_getkeysigs;
376         dbctx->keyid2uid                = generic_keyid2uid;
377         dbctx->iterate_keys             = hkp_iterate_keys;
378
379         if (!hkp_parse_url(privctx, dbcfg->location)) {
380                 exit(EXIT_FAILURE);
381         }
382         logthing(LOGTHING_INFO, "Using %s as HKP forwarding URL.",
383                 privctx->hkpbase);
384         curl_global_init(CURL_GLOBAL_DEFAULT);
385         privctx->curl = curl_easy_init();
386         if (privctx->curl == NULL) {
387                 logthing(LOGTHING_CRITICAL, "Could not initialize CURL.");
388                 hkp_cleanupdb(dbctx);
389                 dbctx = NULL;
390                 exit(EXIT_FAILURE);
391         }
392         curl_easy_setopt(privctx->curl, CURLOPT_USERAGENT,
393                 "onak/" ONAK_VERSION);
394
395         if (strncmp(privctx->hkpbase, "https://", 8) == 0) {
396                 curl_info = curl_version_info(CURLVERSION_NOW);
397                 if (! (curl_info->features & CURL_VERSION_SSL)) {
398                         logthing(LOGTHING_CRITICAL,
399                                 "CURL lacks SSL support; cannot use HKP url: %s",
400                                 privctx->hkpbase);
401                         hkp_cleanupdb(dbctx);
402                         dbctx = NULL;
403                         exit(EXIT_FAILURE);
404                 }
405         }
406
407         return dbctx;
408 }