]> the.earth.li Git - onak.git/blob - keydb_stacked.c
Throw away invalid packet data when parsing packets
[onak.git] / keydb_stacked.c
1 /*
2  * keydb_stacked.c - backend that stacks other backends together
3  *
4  * Copyright 2016 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, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "cleankey.h"
24 #include "decodekey.h"
25 #include "hash.h"
26 #include "keydb.h"
27 #include "keyid.h"
28 #include "keystructs.h"
29 #include "log.h"
30 #include "mem.h"
31 #include "merge.h"
32 #include "onak-conf.h"
33 #include "openpgp.h"
34 #include "parsekey.h"
35 #include "sendsync.h"
36
37 struct onak_stacked_dbctx {
38         struct ll *backends;
39         bool store_on_fallback;
40 };
41
42 /*
43  * The following functions only apply to the first backend.
44  */
45
46 static bool stacked_starttrans(struct onak_dbctx *dbctx)
47 {
48         struct onak_stacked_dbctx *privctx =
49                         (struct onak_stacked_dbctx *) dbctx->priv;
50         struct onak_dbctx *backend =
51                         (struct onak_dbctx *) privctx->backends->object;
52
53         return backend->starttrans(backend);
54 }
55
56 static void stacked_endtrans(struct onak_dbctx *dbctx)
57 {
58         struct onak_stacked_dbctx *privctx =
59                         (struct onak_stacked_dbctx *) dbctx->priv;
60         struct onak_dbctx *backend =
61                         (struct onak_dbctx *) privctx->backends->object;
62
63         backend->starttrans(backend);
64 }
65
66 static int stacked_store_key(struct onak_dbctx *dbctx,
67                 struct openpgp_publickey *publickey, bool intrans,
68                 bool update)
69 {
70         struct onak_stacked_dbctx *privctx =
71                         (struct onak_stacked_dbctx *) dbctx->priv;
72         struct onak_dbctx *backend =
73                         (struct onak_dbctx *) privctx->backends->object;
74
75         return backend->store_key(backend,
76                         publickey, intrans, update);
77 }
78
79 static int stacked_delete_key(struct onak_dbctx *dbctx, uint64_t keyid,
80                 bool intrans)
81 {
82         struct onak_stacked_dbctx *privctx =
83                         (struct onak_stacked_dbctx *) dbctx->priv;
84         struct onak_dbctx *backend =
85                         (struct onak_dbctx *) privctx->backends->object;
86
87         return backend->delete_key(backend,
88                         keyid, intrans);
89 }
90
91 static int stacked_update_keys(struct onak_dbctx *dbctx,
92                 struct openpgp_publickey **keys, bool sendsync)
93 {
94         struct onak_stacked_dbctx *privctx =
95                         (struct onak_stacked_dbctx *) dbctx->priv;
96         struct onak_dbctx *backend =
97                         (struct onak_dbctx *) privctx->backends->object;
98
99         return backend->update_keys(backend, keys, sendsync);
100 }
101
102 static int stacked_iterate_keys(struct onak_dbctx *dbctx,
103                 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
104                 void *ctx)
105 {
106         struct onak_stacked_dbctx *privctx =
107                         (struct onak_stacked_dbctx *) dbctx->priv;
108         struct onak_dbctx *backend =
109                         (struct onak_dbctx *) privctx->backends->object;
110
111         return backend->iterate_keys(backend, iterfunc, ctx);
112 }
113
114 static void store_on_fallback(struct onak_stacked_dbctx *privctx,
115                 struct openpgp_publickey *publickey, bool intrans)
116 {
117         struct onak_dbctx *backend =
118                         (struct onak_dbctx *) privctx->backends->object;
119         struct openpgp_publickey *curkey;
120
121         cleankeys(publickey);
122         /*
123          * If we walked the stack at all, store the key in the first
124          * backend if configured to do so. It's not an update as we
125          * know it's not there or we wouldn't have fallen back.
126          */
127         for (curkey = publickey; curkey != NULL; curkey = curkey->next) {
128                 backend->store_key(backend, curkey, intrans, false);
129         }
130 }
131
132 /*
133  * The functions below will walk along the backend stack until they
134  * reach the end or get a successful result.
135  */
136
137 static int stacked_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
138                 struct openpgp_publickey **publickey, bool intrans)
139 {
140         struct onak_stacked_dbctx *privctx =
141                         (struct onak_stacked_dbctx *) dbctx->priv;
142         struct onak_dbctx *backend;
143         struct ll *cur;
144         int res = 0;
145
146         for (cur = privctx->backends; cur != NULL && res == 0;
147                         cur = cur->next) {
148                 backend = (struct onak_dbctx *) cur->object;
149                 res = backend->fetch_key_id(backend, keyid, publickey,
150                                 intrans);
151         }
152
153         if (privctx->store_on_fallback && cur != privctx->backends) {
154                 store_on_fallback(privctx, *publickey, intrans);
155         }
156
157         return res;
158 }
159
160 static int stacked_fetch_key_fp(struct onak_dbctx *dbctx,
161                 struct openpgp_fingerprint *fingerprint,
162                 struct openpgp_publickey **publickey, bool intrans)
163 {
164         struct onak_stacked_dbctx *privctx =
165                         (struct onak_stacked_dbctx *) dbctx->priv;
166         struct onak_dbctx *backend;
167         struct ll *cur;
168         int res = 0;
169
170         for (cur = privctx->backends; cur != NULL && res == 0;
171                         cur = cur->next) {
172                 backend = (struct onak_dbctx *) cur->object;
173                 res = backend->fetch_key_fp(backend, fingerprint, publickey,
174                                 intrans);
175         }
176
177         if (privctx->store_on_fallback && cur != privctx->backends) {
178                 store_on_fallback(privctx, *publickey, intrans);
179         }
180
181         return res;
182 }
183
184 static int stacked_fetch_key_text(struct onak_dbctx *dbctx,
185                 const char *search,
186                 struct openpgp_publickey **publickey)
187 {
188         struct onak_stacked_dbctx *privctx =
189                         (struct onak_stacked_dbctx *) dbctx->priv;
190         struct onak_dbctx *backend;
191         struct ll *cur;
192         int res = 0;
193
194         for (cur = privctx->backends; cur != NULL && res == 0;
195                         cur = cur->next) {
196                 backend = (struct onak_dbctx *) cur->object;
197                 res = backend->fetch_key_text(backend, search, publickey);
198         }
199
200         if (privctx->store_on_fallback && cur != privctx->backends) {
201                 store_on_fallback(privctx, *publickey, false);
202         }
203
204         return res;
205 }
206
207 static int stacked_fetch_key_skshash(struct onak_dbctx *dbctx,
208                 const struct skshash *hash,
209                 struct openpgp_publickey **publickey)
210 {
211         struct onak_stacked_dbctx *privctx =
212                         (struct onak_stacked_dbctx *) dbctx->priv;
213         struct onak_dbctx *backend;
214         struct ll *cur;
215         int res = 0;
216
217         for (cur = privctx->backends; cur != NULL && res == 0;
218                         cur = cur->next) {
219                 backend = (struct onak_dbctx *) cur->object;
220                 res = backend->fetch_key_skshash(backend, hash, publickey);
221         }
222
223         if (privctx->store_on_fallback && cur != privctx->backends) {
224                 store_on_fallback(privctx, *publickey, false);
225         }
226
227         return res;
228 }
229
230 static struct ll *stacked_getkeysigs(struct onak_dbctx *dbctx,
231                 uint64_t keyid, bool *revoked)
232 {
233         struct onak_stacked_dbctx *privctx =
234                         (struct onak_stacked_dbctx *) dbctx->priv;
235         struct onak_dbctx *backend;
236         struct ll *cur;
237         struct ll *res = NULL;
238
239         for (cur = privctx->backends; cur != NULL && res == NULL;
240                         cur = cur->next) {
241                 backend = (struct onak_dbctx *) cur->object;
242                 res = backend->getkeysigs(backend, keyid, revoked);
243         }
244
245         return res;
246 }
247
248 static struct ll *stacked_cached_getkeysigs(struct onak_dbctx *dbctx,
249                 uint64_t keyid)
250 {
251         struct onak_stacked_dbctx *privctx =
252                         (struct onak_stacked_dbctx *) dbctx->priv;
253         struct onak_dbctx *backend;
254         struct ll *cur;
255         struct ll *res = NULL;
256
257         for (cur = privctx->backends; cur != NULL && res == NULL;
258                         cur = cur->next) {
259                 backend = (struct onak_dbctx *) cur->object;
260                 res = backend->cached_getkeysigs(backend, keyid);
261         }
262
263         return res;
264 }
265
266 static char *stacked_keyid2uid(struct onak_dbctx *dbctx,
267                         uint64_t keyid)
268 {
269         struct onak_stacked_dbctx *privctx =
270                         (struct onak_stacked_dbctx *) dbctx->priv;
271         struct onak_dbctx *backend;
272         struct ll *cur;
273         char *res = NULL;
274
275         for (cur = privctx->backends; cur != NULL && res == NULL;
276                         cur = cur->next) {
277                 backend = (struct onak_dbctx *) cur->object;
278                 res = backend->keyid2uid(backend, keyid);
279         }
280
281         return res;
282 }
283
284 static uint64_t stacked_getfullkeyid(struct onak_dbctx *dbctx,
285                 uint64_t keyid)
286 {
287         struct onak_stacked_dbctx *privctx =
288                         (struct onak_stacked_dbctx *) dbctx->priv;
289         struct onak_dbctx *backend;
290         struct ll *cur;
291         uint64_t res = 0;
292
293         for (cur = privctx->backends; cur != NULL && res == 0;
294                         cur = cur->next) {
295                 backend = (struct onak_dbctx *) cur->object;
296                 res = backend->getfullkeyid(backend, keyid);
297         }
298
299         return res;
300 }
301
302 static void stacked_cleanupdb(struct onak_dbctx *dbctx)
303 {
304         struct onak_stacked_dbctx *privctx =
305                         (struct onak_stacked_dbctx *) dbctx->priv;
306         struct onak_dbctx *backend;
307         struct ll *cur;
308         int res = 0;
309
310         for (cur = privctx->backends; cur != NULL && res == 0;
311                         cur = cur->next) {
312                 backend = (struct onak_dbctx *) cur->object;
313                 backend->cleanupdb(backend);
314         }
315
316         if (dbctx->priv != NULL) {
317                 free(dbctx->priv);
318                 dbctx->priv = NULL;
319         }
320
321         if (dbctx != NULL) {
322                 free(dbctx);
323         }
324 }
325
326 struct onak_dbctx *keydb_stacked_init(struct onak_db_config *dbcfg,
327                 bool readonly)
328 {
329         struct onak_dbctx *dbctx;
330         struct onak_stacked_dbctx *privctx;
331         struct onak_dbctx *backend;
332         struct onak_db_config *backend_cfg;
333         char *backend_name, *saveptr = NULL;
334
335         if (dbcfg == NULL) {
336                 logthing(LOGTHING_CRITICAL,
337                         "No backend database configuration supplied.");
338                 return NULL;
339         }
340
341         dbctx = malloc(sizeof(struct onak_dbctx));
342
343         if (dbctx == NULL) {
344                 return NULL;
345         }
346
347         dbctx->config = dbcfg;
348         dbctx->priv = privctx = malloc(sizeof(struct onak_stacked_dbctx));
349         if (dbctx->priv == NULL) {
350                 free(dbctx);
351                 return (NULL);
352         }
353
354         /* TODO: Make configurable? */
355         privctx->store_on_fallback = true;
356         privctx->backends = NULL;
357
358         backend_name = strtok_r(dbcfg->location, ":", &saveptr);
359         while (backend_name != NULL) {
360                 backend_cfg = find_db_backend_config(config.backends,
361                                 backend_name);
362                 if (backend_cfg == NULL) {
363                         logthing(LOGTHING_CRITICAL,
364                                 "Couldn't find configuration for %s backend",
365                                 backend_name);
366                         stacked_cleanupdb(dbctx);
367                         return NULL;
368                 }
369                 logthing(LOGTHING_INFO, "Loading stacked backend: %s",
370                                 backend_cfg->name);
371
372                 backend = config.dbinit(backend_cfg, readonly);
373                 privctx->backends = lladdend(privctx->backends, backend);
374
375                 backend_name = strtok_r(NULL, ":", &saveptr);
376         }
377
378         if (privctx->backends != NULL) {
379                 dbctx->cleanupdb = stacked_cleanupdb;
380                 dbctx->starttrans = stacked_starttrans;
381                 dbctx->endtrans = stacked_endtrans;
382                 dbctx->fetch_key_id = stacked_fetch_key_id;
383                 dbctx->fetch_key_fp = stacked_fetch_key_fp;
384                 dbctx->fetch_key_text = stacked_fetch_key_text;
385                 dbctx->fetch_key_skshash = stacked_fetch_key_skshash;
386                 dbctx->store_key = stacked_store_key;
387                 dbctx->update_keys = stacked_update_keys;
388                 dbctx->delete_key = stacked_delete_key;
389                 dbctx->getkeysigs = stacked_getkeysigs;
390                 dbctx->cached_getkeysigs = stacked_cached_getkeysigs;
391                 dbctx->keyid2uid = stacked_keyid2uid;
392                 dbctx->getfullkeyid = stacked_getfullkeyid;
393                 dbctx->iterate_keys = stacked_iterate_keys;
394         }
395
396         return dbctx;
397 }