]> the.earth.li Git - onak.git/blob - keydb/keydb_stacked.c
6cfb1f76eed1b073a84b2926337ad7820b41a2b2
[onak.git] / keydb / 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, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "cleankey.h"
26 #include "keydb.h"
27 #include "keystructs.h"
28 #include "ll.h"
29 #include "log.h"
30 #include "onak-conf.h"
31
32 struct onak_stacked_dbctx {
33         struct ll *backends;
34         bool store_on_fallback;
35 };
36
37 /*
38  * The following functions only apply to the first backend.
39  */
40
41 static bool stacked_starttrans(struct onak_dbctx *dbctx)
42 {
43         struct onak_stacked_dbctx *privctx =
44                         (struct onak_stacked_dbctx *) dbctx->priv;
45         struct onak_dbctx *backend =
46                         (struct onak_dbctx *) privctx->backends->object;
47
48         return backend->starttrans(backend);
49 }
50
51 static void stacked_endtrans(struct onak_dbctx *dbctx)
52 {
53         struct onak_stacked_dbctx *privctx =
54                         (struct onak_stacked_dbctx *) dbctx->priv;
55         struct onak_dbctx *backend =
56                         (struct onak_dbctx *) privctx->backends->object;
57
58         backend->starttrans(backend);
59 }
60
61 static int stacked_store_key(struct onak_dbctx *dbctx,
62                 struct openpgp_publickey *publickey, bool intrans,
63                 bool update)
64 {
65         struct onak_stacked_dbctx *privctx =
66                         (struct onak_stacked_dbctx *) dbctx->priv;
67         struct onak_dbctx *backend =
68                         (struct onak_dbctx *) privctx->backends->object;
69
70         return backend->store_key(backend,
71                         publickey, intrans, update);
72 }
73
74 static int stacked_delete_key(struct onak_dbctx *dbctx,
75                 struct openpgp_fingerprint *fp,
76                 bool intrans)
77 {
78         struct onak_stacked_dbctx *privctx =
79                         (struct onak_stacked_dbctx *) dbctx->priv;
80         struct onak_dbctx *backend =
81                         (struct onak_dbctx *) privctx->backends->object;
82
83         return backend->delete_key(backend,
84                         fp, intrans);
85 }
86
87 static int stacked_update_keys(struct onak_dbctx *dbctx,
88                 struct openpgp_publickey **keys,
89                 struct keyarray *blacklist,
90                 bool updateonly,
91                 bool sendsync)
92 {
93         struct onak_stacked_dbctx *privctx =
94                         (struct onak_stacked_dbctx *) dbctx->priv;
95         struct onak_dbctx *backend =
96                         (struct onak_dbctx *) privctx->backends->object;
97
98         return backend->update_keys(backend, keys, blacklist, updateonly,
99                         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(backend, &publickey, config.clean_policies);
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 /*
231  * Include the basic keydb routines so we can use them for fall back.
232  * For all of the following we try the top keydb backend and if that doesn't
233  * have answer fall back to the generics, which will do a retrieve from a
234  * backend further down the stack, then a fallback store.
235  */
236 #define NEED_KEYID2UID 1
237 #define NEED_GETKEYSIGS 1
238 #define NEED_UPDATEKEYS 1
239 #include "keydb.c"
240
241 static struct ll *stacked_getkeysigs(struct onak_dbctx *dbctx,
242                 uint64_t keyid, bool *revoked)
243 {
244         struct onak_stacked_dbctx *privctx =
245                         (struct onak_stacked_dbctx *) dbctx->priv;
246         struct onak_dbctx *backend =
247                         (struct onak_dbctx *) privctx->backends->object;
248         struct ll *res;
249
250         res = backend->getkeysigs(backend, keyid, revoked);
251         if (res == NULL) {
252                 res = generic_getkeysigs(dbctx, keyid, revoked);
253         }
254
255         return res;
256 }
257
258 static struct ll *stacked_cached_getkeysigs(struct onak_dbctx *dbctx,
259                 uint64_t keyid)
260 {
261         struct onak_stacked_dbctx *privctx =
262                         (struct onak_stacked_dbctx *) dbctx->priv;
263         struct onak_dbctx *backend =
264                         (struct onak_dbctx *) privctx->backends->object;
265         struct ll *res;
266
267         res = backend->cached_getkeysigs(backend, keyid);
268         if (res == NULL) {
269                 res = generic_cached_getkeysigs(dbctx, keyid);
270         }
271
272         return res;
273 }
274
275 static char *stacked_keyid2uid(struct onak_dbctx *dbctx,
276                         uint64_t keyid)
277 {
278         struct onak_stacked_dbctx *privctx =
279                         (struct onak_stacked_dbctx *) dbctx->priv;
280         struct onak_dbctx *backend =
281                         (struct onak_dbctx *) privctx->backends->object;
282         char *res = NULL;
283
284         res = backend->keyid2uid(backend, keyid);
285         if (!res) {
286                 res = generic_keyid2uid(dbctx, keyid);
287         }
288
289         return res;
290 }
291
292 static void stacked_cleanupdb(struct onak_dbctx *dbctx)
293 {
294         struct onak_stacked_dbctx *privctx =
295                         (struct onak_stacked_dbctx *) dbctx->priv;
296         struct onak_dbctx *backend;
297         struct ll *cur;
298         int res = 0;
299
300         for (cur = privctx->backends; cur != NULL && res == 0;
301                         cur = cur->next) {
302                 backend = (struct onak_dbctx *) cur->object;
303                 backend->cleanupdb(backend);
304         }
305
306         if (dbctx->priv != NULL) {
307                 free(dbctx->priv);
308                 dbctx->priv = NULL;
309         }
310
311         if (dbctx != NULL) {
312                 free(dbctx);
313         }
314 }
315
316 struct onak_dbctx *keydb_stacked_init(struct onak_db_config *dbcfg,
317                 bool readonly)
318 {
319         struct onak_dbctx *dbctx;
320         struct onak_stacked_dbctx *privctx;
321         struct onak_dbctx *backend;
322         struct onak_db_config *backend_cfg;
323         char *backend_name, *saveptr = NULL;
324
325         if (dbcfg == NULL) {
326                 logthing(LOGTHING_CRITICAL,
327                         "No backend database configuration supplied.");
328                 return NULL;
329         }
330
331         dbctx = malloc(sizeof(struct onak_dbctx));
332
333         if (dbctx == NULL) {
334                 return NULL;
335         }
336
337         dbctx->config = dbcfg;
338         dbctx->priv = privctx = malloc(sizeof(struct onak_stacked_dbctx));
339         if (dbctx->priv == NULL) {
340                 free(dbctx);
341                 return (NULL);
342         }
343
344         /* TODO: Make configurable? */
345         privctx->store_on_fallback = true;
346         privctx->backends = NULL;
347
348         backend_name = strtok_r(dbcfg->location, ":", &saveptr);
349         while (backend_name != NULL) {
350                 backend_cfg = find_db_backend_config(config.backends,
351                                 backend_name);
352                 if (backend_cfg == NULL) {
353                         logthing(LOGTHING_CRITICAL,
354                                 "Couldn't find configuration for %s backend",
355                                 backend_name);
356                         stacked_cleanupdb(dbctx);
357                         return NULL;
358                 }
359                 logthing(LOGTHING_INFO, "Loading stacked backend: %s",
360                                 backend_cfg->name);
361
362                 backend = config.dbinit(backend_cfg, readonly);
363                 privctx->backends = lladdend(privctx->backends, backend);
364
365                 backend_name = strtok_r(NULL, ":", &saveptr);
366         }
367
368         if (privctx->backends != NULL) {
369                 dbctx->cleanupdb = stacked_cleanupdb;
370                 dbctx->starttrans = stacked_starttrans;
371                 dbctx->endtrans = stacked_endtrans;
372                 dbctx->fetch_key_id = stacked_fetch_key_id;
373                 dbctx->fetch_key_fp = stacked_fetch_key_fp;
374                 dbctx->fetch_key_text = stacked_fetch_key_text;
375                 dbctx->fetch_key_skshash = stacked_fetch_key_skshash;
376                 dbctx->store_key = stacked_store_key;
377                 dbctx->update_keys = stacked_update_keys;
378                 dbctx->delete_key = stacked_delete_key;
379                 dbctx->getkeysigs = stacked_getkeysigs;
380                 dbctx->cached_getkeysigs = stacked_cached_getkeysigs;
381                 dbctx->keyid2uid = stacked_keyid2uid;
382                 dbctx->iterate_keys = stacked_iterate_keys;
383         }
384
385         return dbctx;
386 }