2 * keydb_stacked.c - backend that stacks other backends together
4 * Copyright 2016 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, see <https://www.gnu.org/licenses/>.
27 #include "keystructs.h"
30 #include "onak-conf.h"
32 struct onak_stacked_dbctx {
34 bool store_on_fallback;
38 * The following functions only apply to the first backend.
41 static bool stacked_starttrans(struct onak_dbctx *dbctx)
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;
48 return backend->starttrans(backend);
51 static void stacked_endtrans(struct onak_dbctx *dbctx)
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;
58 backend->starttrans(backend);
61 static int stacked_store_key(struct onak_dbctx *dbctx,
62 struct openpgp_publickey *publickey, bool intrans,
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;
70 return backend->store_key(backend,
71 publickey, intrans, update);
74 static int stacked_delete_key(struct onak_dbctx *dbctx,
75 struct openpgp_fingerprint *fp,
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;
83 return backend->delete_key(backend,
87 static int stacked_update_keys(struct onak_dbctx *dbctx,
88 struct openpgp_publickey **keys,
89 struct keyarray *blacklist,
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;
98 return backend->update_keys(backend, keys, blacklist, updateonly,
102 static int stacked_iterate_keys(struct onak_dbctx *dbctx,
103 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
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;
111 return backend->iterate_keys(backend, iterfunc, ctx);
114 static void store_on_fallback(struct onak_stacked_dbctx *privctx,
115 struct openpgp_publickey *publickey, bool intrans)
117 struct onak_dbctx *backend =
118 (struct onak_dbctx *) privctx->backends->object;
119 struct openpgp_publickey *curkey;
121 cleankeys(backend, &publickey, config.clean_policies);
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.
127 for (curkey = publickey; curkey != NULL; curkey = curkey->next) {
128 backend->store_key(backend, curkey, intrans, false);
133 * The functions below will walk along the backend stack until they
134 * reach the end or get a successful result.
137 static int stacked_fetch_key(struct onak_dbctx *dbctx,
138 struct openpgp_fingerprint *fingerprint,
139 struct openpgp_publickey **publickey, bool intrans)
141 struct onak_stacked_dbctx *privctx =
142 (struct onak_stacked_dbctx *) dbctx->priv;
143 struct onak_dbctx *backend;
147 for (cur = privctx->backends; cur != NULL && res == 0;
149 backend = (struct onak_dbctx *) cur->object;
150 res = backend->fetch_key(backend, fingerprint, publickey,
154 if (privctx->store_on_fallback && cur != privctx->backends) {
155 store_on_fallback(privctx, *publickey, intrans);
161 static int stacked_fetch_key_fp(struct onak_dbctx *dbctx,
162 struct openpgp_fingerprint *fingerprint,
163 struct openpgp_publickey **publickey, bool intrans)
165 struct onak_stacked_dbctx *privctx =
166 (struct onak_stacked_dbctx *) dbctx->priv;
167 struct onak_dbctx *backend;
171 for (cur = privctx->backends; cur != NULL && res == 0;
173 backend = (struct onak_dbctx *) cur->object;
174 res = backend->fetch_key_fp(backend, fingerprint, publickey,
178 if (privctx->store_on_fallback && cur != privctx->backends) {
179 store_on_fallback(privctx, *publickey, intrans);
185 static int stacked_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
186 struct openpgp_publickey **publickey, bool intrans)
188 struct onak_stacked_dbctx *privctx =
189 (struct onak_stacked_dbctx *) dbctx->priv;
190 struct onak_dbctx *backend;
194 for (cur = privctx->backends; cur != NULL && res == 0;
196 backend = (struct onak_dbctx *) cur->object;
197 res = backend->fetch_key_id(backend, keyid, publickey,
201 if (privctx->store_on_fallback && cur != privctx->backends) {
202 store_on_fallback(privctx, *publickey, intrans);
208 static int stacked_fetch_key_text(struct onak_dbctx *dbctx,
210 struct openpgp_publickey **publickey)
212 struct onak_stacked_dbctx *privctx =
213 (struct onak_stacked_dbctx *) dbctx->priv;
214 struct onak_dbctx *backend;
218 for (cur = privctx->backends; cur != NULL && res == 0;
220 backend = (struct onak_dbctx *) cur->object;
221 res = backend->fetch_key_text(backend, search, publickey);
224 if (privctx->store_on_fallback && cur != privctx->backends) {
225 store_on_fallback(privctx, *publickey, false);
231 static int stacked_fetch_key_skshash(struct onak_dbctx *dbctx,
232 const struct skshash *hash,
233 struct openpgp_publickey **publickey)
235 struct onak_stacked_dbctx *privctx =
236 (struct onak_stacked_dbctx *) dbctx->priv;
237 struct onak_dbctx *backend;
241 for (cur = privctx->backends; cur != NULL && res == 0;
243 backend = (struct onak_dbctx *) cur->object;
244 res = backend->fetch_key_skshash(backend, hash, publickey);
247 if (privctx->store_on_fallback && cur != privctx->backends) {
248 store_on_fallback(privctx, *publickey, false);
255 * Include the basic keydb routines so we can use them for fall back.
256 * For all of the following we try the top keydb backend and if that doesn't
257 * have answer fall back to the generics, which will do a retrieve from a
258 * backend further down the stack, then a fallback store.
260 #define NEED_KEYID2UID 1
261 #define NEED_GETKEYSIGS 1
262 #define NEED_UPDATEKEYS 1
265 static struct ll *stacked_getkeysigs(struct onak_dbctx *dbctx,
266 uint64_t keyid, bool *revoked)
268 struct onak_stacked_dbctx *privctx =
269 (struct onak_stacked_dbctx *) dbctx->priv;
270 struct onak_dbctx *backend =
271 (struct onak_dbctx *) privctx->backends->object;
274 res = backend->getkeysigs(backend, keyid, revoked);
276 res = generic_getkeysigs(dbctx, keyid, revoked);
282 static struct ll *stacked_cached_getkeysigs(struct onak_dbctx *dbctx,
285 struct onak_stacked_dbctx *privctx =
286 (struct onak_stacked_dbctx *) dbctx->priv;
287 struct onak_dbctx *backend =
288 (struct onak_dbctx *) privctx->backends->object;
291 res = backend->cached_getkeysigs(backend, keyid);
293 res = generic_cached_getkeysigs(dbctx, keyid);
299 static char *stacked_keyid2uid(struct onak_dbctx *dbctx,
302 struct onak_stacked_dbctx *privctx =
303 (struct onak_stacked_dbctx *) dbctx->priv;
304 struct onak_dbctx *backend =
305 (struct onak_dbctx *) privctx->backends->object;
308 res = backend->keyid2uid(backend, keyid);
310 res = generic_keyid2uid(dbctx, keyid);
316 static void stacked_cleanupdb(struct onak_dbctx *dbctx)
318 struct onak_stacked_dbctx *privctx =
319 (struct onak_stacked_dbctx *) dbctx->priv;
320 struct onak_dbctx *backend;
324 for (cur = privctx->backends; cur != NULL && res == 0;
326 backend = (struct onak_dbctx *) cur->object;
327 backend->cleanupdb(backend);
330 if (dbctx->priv != NULL) {
340 struct onak_dbctx *keydb_stacked_init(struct onak_db_config *dbcfg,
343 struct onak_dbctx *dbctx;
344 struct onak_stacked_dbctx *privctx;
345 struct onak_dbctx *backend;
346 struct onak_db_config *backend_cfg;
347 char *backend_name, *saveptr = NULL;
350 logthing(LOGTHING_CRITICAL,
351 "No backend database configuration supplied.");
355 dbctx = malloc(sizeof(struct onak_dbctx));
361 dbctx->config = dbcfg;
362 dbctx->priv = privctx = malloc(sizeof(struct onak_stacked_dbctx));
363 if (dbctx->priv == NULL) {
368 /* TODO: Make configurable? */
369 privctx->store_on_fallback = true;
370 privctx->backends = NULL;
372 backend_name = strtok_r(dbcfg->location, ":", &saveptr);
373 while (backend_name != NULL) {
374 backend_cfg = find_db_backend_config(config.backends,
376 if (backend_cfg == NULL) {
377 logthing(LOGTHING_CRITICAL,
378 "Couldn't find configuration for %s backend",
380 stacked_cleanupdb(dbctx);
383 logthing(LOGTHING_INFO, "Loading stacked backend: %s",
386 backend = config.dbinit(backend_cfg, readonly);
387 privctx->backends = lladdend(privctx->backends, backend);
389 backend_name = strtok_r(NULL, ":", &saveptr);
392 if (privctx->backends != NULL) {
393 dbctx->cleanupdb = stacked_cleanupdb;
394 dbctx->starttrans = stacked_starttrans;
395 dbctx->endtrans = stacked_endtrans;
396 dbctx->fetch_key = stacked_fetch_key;
397 dbctx->fetch_key_fp = stacked_fetch_key_fp;
398 dbctx->fetch_key_id = stacked_fetch_key_id;
399 dbctx->fetch_key_text = stacked_fetch_key_text;
400 dbctx->fetch_key_skshash = stacked_fetch_key_skshash;
401 dbctx->store_key = stacked_store_key;
402 dbctx->update_keys = stacked_update_keys;
403 dbctx->delete_key = stacked_delete_key;
404 dbctx->getkeysigs = stacked_getkeysigs;
405 dbctx->cached_getkeysigs = stacked_cached_getkeysigs;
406 dbctx->keyid2uid = stacked_keyid2uid;
407 dbctx->iterate_keys = stacked_iterate_keys;