]> the.earth.li Git - onak.git/blob - keydb_stacked.c
Remove unused worddb_cmp() from DB4 backend
[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, 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, bool sendsync)
89 {
90         struct onak_stacked_dbctx *privctx =
91                         (struct onak_stacked_dbctx *) dbctx->priv;
92         struct onak_dbctx *backend =
93                         (struct onak_dbctx *) privctx->backends->object;
94
95         return backend->update_keys(backend, keys, sendsync);
96 }
97
98 static int stacked_iterate_keys(struct onak_dbctx *dbctx,
99                 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
100                 void *ctx)
101 {
102         struct onak_stacked_dbctx *privctx =
103                         (struct onak_stacked_dbctx *) dbctx->priv;
104         struct onak_dbctx *backend =
105                         (struct onak_dbctx *) privctx->backends->object;
106
107         return backend->iterate_keys(backend, iterfunc, ctx);
108 }
109
110 static void store_on_fallback(struct onak_stacked_dbctx *privctx,
111                 struct openpgp_publickey *publickey, bool intrans)
112 {
113         struct onak_dbctx *backend =
114                         (struct onak_dbctx *) privctx->backends->object;
115         struct openpgp_publickey *curkey;
116
117         cleankeys(&publickey, config.clean_policies);
118         /*
119          * If we walked the stack at all, store the key in the first
120          * backend if configured to do so. It's not an update as we
121          * know it's not there or we wouldn't have fallen back.
122          */
123         for (curkey = publickey; curkey != NULL; curkey = curkey->next) {
124                 backend->store_key(backend, curkey, intrans, false);
125         }
126 }
127
128 /*
129  * The functions below will walk along the backend stack until they
130  * reach the end or get a successful result.
131  */
132
133 static int stacked_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
134                 struct openpgp_publickey **publickey, bool intrans)
135 {
136         struct onak_stacked_dbctx *privctx =
137                         (struct onak_stacked_dbctx *) dbctx->priv;
138         struct onak_dbctx *backend;
139         struct ll *cur;
140         int res = 0;
141
142         for (cur = privctx->backends; cur != NULL && res == 0;
143                         cur = cur->next) {
144                 backend = (struct onak_dbctx *) cur->object;
145                 res = backend->fetch_key_id(backend, keyid, publickey,
146                                 intrans);
147         }
148
149         if (privctx->store_on_fallback && cur != privctx->backends) {
150                 store_on_fallback(privctx, *publickey, intrans);
151         }
152
153         return res;
154 }
155
156 static int stacked_fetch_key_fp(struct onak_dbctx *dbctx,
157                 struct openpgp_fingerprint *fingerprint,
158                 struct openpgp_publickey **publickey, bool intrans)
159 {
160         struct onak_stacked_dbctx *privctx =
161                         (struct onak_stacked_dbctx *) dbctx->priv;
162         struct onak_dbctx *backend;
163         struct ll *cur;
164         int res = 0;
165
166         for (cur = privctx->backends; cur != NULL && res == 0;
167                         cur = cur->next) {
168                 backend = (struct onak_dbctx *) cur->object;
169                 res = backend->fetch_key_fp(backend, fingerprint, publickey,
170                                 intrans);
171         }
172
173         if (privctx->store_on_fallback && cur != privctx->backends) {
174                 store_on_fallback(privctx, *publickey, intrans);
175         }
176
177         return res;
178 }
179
180 static int stacked_fetch_key_text(struct onak_dbctx *dbctx,
181                 const char *search,
182                 struct openpgp_publickey **publickey)
183 {
184         struct onak_stacked_dbctx *privctx =
185                         (struct onak_stacked_dbctx *) dbctx->priv;
186         struct onak_dbctx *backend;
187         struct ll *cur;
188         int res = 0;
189
190         for (cur = privctx->backends; cur != NULL && res == 0;
191                         cur = cur->next) {
192                 backend = (struct onak_dbctx *) cur->object;
193                 res = backend->fetch_key_text(backend, search, publickey);
194         }
195
196         if (privctx->store_on_fallback && cur != privctx->backends) {
197                 store_on_fallback(privctx, *publickey, false);
198         }
199
200         return res;
201 }
202
203 static int stacked_fetch_key_skshash(struct onak_dbctx *dbctx,
204                 const struct skshash *hash,
205                 struct openpgp_publickey **publickey)
206 {
207         struct onak_stacked_dbctx *privctx =
208                         (struct onak_stacked_dbctx *) dbctx->priv;
209         struct onak_dbctx *backend;
210         struct ll *cur;
211         int res = 0;
212
213         for (cur = privctx->backends; cur != NULL && res == 0;
214                         cur = cur->next) {
215                 backend = (struct onak_dbctx *) cur->object;
216                 res = backend->fetch_key_skshash(backend, hash, publickey);
217         }
218
219         if (privctx->store_on_fallback && cur != privctx->backends) {
220                 store_on_fallback(privctx, *publickey, false);
221         }
222
223         return res;
224 }
225
226 /*
227  * Include the basic keydb routines so we can use them for fall back.
228  * For all of the following we try the top keydb backend and if that doesn't
229  * have answer fall back to the generics, which will do a retrieve from a
230  * backend further down the stack, then a fallback store.
231  */
232 #define NEED_KEYID2UID 1
233 #define NEED_GETKEYSIGS 1
234 #define NEED_UPDATEKEYS 1
235 #include "keydb.c"
236
237 static struct ll *stacked_getkeysigs(struct onak_dbctx *dbctx,
238                 uint64_t keyid, bool *revoked)
239 {
240         struct onak_stacked_dbctx *privctx =
241                         (struct onak_stacked_dbctx *) dbctx->priv;
242         struct onak_dbctx *backend =
243                         (struct onak_dbctx *) privctx->backends->object;
244         struct ll *res;
245
246         res = backend->getkeysigs(backend, keyid, revoked);
247         if (res == NULL) {
248                 res = generic_getkeysigs(dbctx, keyid, revoked);
249         }
250
251         return res;
252 }
253
254 static struct ll *stacked_cached_getkeysigs(struct onak_dbctx *dbctx,
255                 uint64_t keyid)
256 {
257         struct onak_stacked_dbctx *privctx =
258                         (struct onak_stacked_dbctx *) dbctx->priv;
259         struct onak_dbctx *backend =
260                         (struct onak_dbctx *) privctx->backends->object;
261         struct ll *res;
262
263         res = backend->cached_getkeysigs(backend, keyid);
264         if (res == NULL) {
265                 res = generic_cached_getkeysigs(dbctx, keyid);
266         }
267
268         return res;
269 }
270
271 static char *stacked_keyid2uid(struct onak_dbctx *dbctx,
272                         uint64_t keyid)
273 {
274         struct onak_stacked_dbctx *privctx =
275                         (struct onak_stacked_dbctx *) dbctx->priv;
276         struct onak_dbctx *backend =
277                         (struct onak_dbctx *) privctx->backends->object;
278         char *res = NULL;
279
280         res = backend->keyid2uid(backend, keyid);
281         if (!res) {
282                 res = generic_keyid2uid(dbctx, keyid);
283         }
284
285         return res;
286 }
287
288 static void stacked_cleanupdb(struct onak_dbctx *dbctx)
289 {
290         struct onak_stacked_dbctx *privctx =
291                         (struct onak_stacked_dbctx *) dbctx->priv;
292         struct onak_dbctx *backend;
293         struct ll *cur;
294         int res = 0;
295
296         for (cur = privctx->backends; cur != NULL && res == 0;
297                         cur = cur->next) {
298                 backend = (struct onak_dbctx *) cur->object;
299                 backend->cleanupdb(backend);
300         }
301
302         if (dbctx->priv != NULL) {
303                 free(dbctx->priv);
304                 dbctx->priv = NULL;
305         }
306
307         if (dbctx != NULL) {
308                 free(dbctx);
309         }
310 }
311
312 struct onak_dbctx *keydb_stacked_init(struct onak_db_config *dbcfg,
313                 bool readonly)
314 {
315         struct onak_dbctx *dbctx;
316         struct onak_stacked_dbctx *privctx;
317         struct onak_dbctx *backend;
318         struct onak_db_config *backend_cfg;
319         char *backend_name, *saveptr = NULL;
320
321         if (dbcfg == NULL) {
322                 logthing(LOGTHING_CRITICAL,
323                         "No backend database configuration supplied.");
324                 return NULL;
325         }
326
327         dbctx = malloc(sizeof(struct onak_dbctx));
328
329         if (dbctx == NULL) {
330                 return NULL;
331         }
332
333         dbctx->config = dbcfg;
334         dbctx->priv = privctx = malloc(sizeof(struct onak_stacked_dbctx));
335         if (dbctx->priv == NULL) {
336                 free(dbctx);
337                 return (NULL);
338         }
339
340         /* TODO: Make configurable? */
341         privctx->store_on_fallback = true;
342         privctx->backends = NULL;
343
344         backend_name = strtok_r(dbcfg->location, ":", &saveptr);
345         while (backend_name != NULL) {
346                 backend_cfg = find_db_backend_config(config.backends,
347                                 backend_name);
348                 if (backend_cfg == NULL) {
349                         logthing(LOGTHING_CRITICAL,
350                                 "Couldn't find configuration for %s backend",
351                                 backend_name);
352                         stacked_cleanupdb(dbctx);
353                         return NULL;
354                 }
355                 logthing(LOGTHING_INFO, "Loading stacked backend: %s",
356                                 backend_cfg->name);
357
358                 backend = config.dbinit(backend_cfg, readonly);
359                 privctx->backends = lladdend(privctx->backends, backend);
360
361                 backend_name = strtok_r(NULL, ":", &saveptr);
362         }
363
364         if (privctx->backends != NULL) {
365                 dbctx->cleanupdb = stacked_cleanupdb;
366                 dbctx->starttrans = stacked_starttrans;
367                 dbctx->endtrans = stacked_endtrans;
368                 dbctx->fetch_key_id = stacked_fetch_key_id;
369                 dbctx->fetch_key_fp = stacked_fetch_key_fp;
370                 dbctx->fetch_key_text = stacked_fetch_key_text;
371                 dbctx->fetch_key_skshash = stacked_fetch_key_skshash;
372                 dbctx->store_key = stacked_store_key;
373                 dbctx->update_keys = stacked_update_keys;
374                 dbctx->delete_key = stacked_delete_key;
375                 dbctx->getkeysigs = stacked_getkeysigs;
376                 dbctx->cached_getkeysigs = stacked_cached_getkeysigs;
377                 dbctx->keyid2uid = stacked_keyid2uid;
378                 dbctx->iterate_keys = stacked_iterate_keys;
379         }
380
381         return dbctx;
382 }