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(&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_id(struct onak_dbctx *dbctx, uint64_t keyid,
138 struct openpgp_publickey **publickey, bool intrans)
140 struct onak_stacked_dbctx *privctx =
141 (struct onak_stacked_dbctx *) dbctx->priv;
142 struct onak_dbctx *backend;
146 for (cur = privctx->backends; cur != NULL && res == 0;
148 backend = (struct onak_dbctx *) cur->object;
149 res = backend->fetch_key_id(backend, keyid, publickey,
153 if (privctx->store_on_fallback && cur != privctx->backends) {
154 store_on_fallback(privctx, *publickey, intrans);
160 static int stacked_fetch_key_fp(struct onak_dbctx *dbctx,
161 struct openpgp_fingerprint *fingerprint,
162 struct openpgp_publickey **publickey, bool intrans)
164 struct onak_stacked_dbctx *privctx =
165 (struct onak_stacked_dbctx *) dbctx->priv;
166 struct onak_dbctx *backend;
170 for (cur = privctx->backends; cur != NULL && res == 0;
172 backend = (struct onak_dbctx *) cur->object;
173 res = backend->fetch_key_fp(backend, fingerprint, publickey,
177 if (privctx->store_on_fallback && cur != privctx->backends) {
178 store_on_fallback(privctx, *publickey, intrans);
184 static int stacked_fetch_key_text(struct onak_dbctx *dbctx,
186 struct openpgp_publickey **publickey)
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_text(backend, search, publickey);
200 if (privctx->store_on_fallback && cur != privctx->backends) {
201 store_on_fallback(privctx, *publickey, false);
207 static int stacked_fetch_key_skshash(struct onak_dbctx *dbctx,
208 const struct skshash *hash,
209 struct openpgp_publickey **publickey)
211 struct onak_stacked_dbctx *privctx =
212 (struct onak_stacked_dbctx *) dbctx->priv;
213 struct onak_dbctx *backend;
217 for (cur = privctx->backends; cur != NULL && res == 0;
219 backend = (struct onak_dbctx *) cur->object;
220 res = backend->fetch_key_skshash(backend, hash, publickey);
223 if (privctx->store_on_fallback && cur != privctx->backends) {
224 store_on_fallback(privctx, *publickey, false);
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.
236 #define NEED_KEYID2UID 1
237 #define NEED_GETKEYSIGS 1
238 #define NEED_UPDATEKEYS 1
241 static struct ll *stacked_getkeysigs(struct onak_dbctx *dbctx,
242 uint64_t keyid, bool *revoked)
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;
250 res = backend->getkeysigs(backend, keyid, revoked);
252 res = generic_getkeysigs(dbctx, keyid, revoked);
258 static struct ll *stacked_cached_getkeysigs(struct onak_dbctx *dbctx,
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;
267 res = backend->cached_getkeysigs(backend, keyid);
269 res = generic_cached_getkeysigs(dbctx, keyid);
275 static char *stacked_keyid2uid(struct onak_dbctx *dbctx,
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;
284 res = backend->keyid2uid(backend, keyid);
286 res = generic_keyid2uid(dbctx, keyid);
292 static void stacked_cleanupdb(struct onak_dbctx *dbctx)
294 struct onak_stacked_dbctx *privctx =
295 (struct onak_stacked_dbctx *) dbctx->priv;
296 struct onak_dbctx *backend;
300 for (cur = privctx->backends; cur != NULL && res == 0;
302 backend = (struct onak_dbctx *) cur->object;
303 backend->cleanupdb(backend);
306 if (dbctx->priv != NULL) {
316 struct onak_dbctx *keydb_stacked_init(struct onak_db_config *dbcfg,
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;
326 logthing(LOGTHING_CRITICAL,
327 "No backend database configuration supplied.");
331 dbctx = malloc(sizeof(struct onak_dbctx));
337 dbctx->config = dbcfg;
338 dbctx->priv = privctx = malloc(sizeof(struct onak_stacked_dbctx));
339 if (dbctx->priv == NULL) {
344 /* TODO: Make configurable? */
345 privctx->store_on_fallback = true;
346 privctx->backends = NULL;
348 backend_name = strtok_r(dbcfg->location, ":", &saveptr);
349 while (backend_name != NULL) {
350 backend_cfg = find_db_backend_config(config.backends,
352 if (backend_cfg == NULL) {
353 logthing(LOGTHING_CRITICAL,
354 "Couldn't find configuration for %s backend",
356 stacked_cleanupdb(dbctx);
359 logthing(LOGTHING_INFO, "Loading stacked backend: %s",
362 backend = config.dbinit(backend_cfg, readonly);
363 privctx->backends = lladdend(privctx->backends, backend);
365 backend_name = strtok_r(NULL, ":", &saveptr);
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;