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, bool sendsync)
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;
95 return backend->update_keys(backend, keys, sendsync);
98 static int stacked_iterate_keys(struct onak_dbctx *dbctx,
99 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
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;
107 return backend->iterate_keys(backend, iterfunc, ctx);
110 static void store_on_fallback(struct onak_stacked_dbctx *privctx,
111 struct openpgp_publickey *publickey, bool intrans)
113 struct onak_dbctx *backend =
114 (struct onak_dbctx *) privctx->backends->object;
115 struct openpgp_publickey *curkey;
117 cleankeys(&publickey, config.clean_policies);
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.
123 for (curkey = publickey; curkey != NULL; curkey = curkey->next) {
124 backend->store_key(backend, curkey, intrans, false);
129 * The functions below will walk along the backend stack until they
130 * reach the end or get a successful result.
133 static int stacked_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
134 struct openpgp_publickey **publickey, bool intrans)
136 struct onak_stacked_dbctx *privctx =
137 (struct onak_stacked_dbctx *) dbctx->priv;
138 struct onak_dbctx *backend;
142 for (cur = privctx->backends; cur != NULL && res == 0;
144 backend = (struct onak_dbctx *) cur->object;
145 res = backend->fetch_key_id(backend, keyid, publickey,
149 if (privctx->store_on_fallback && cur != privctx->backends) {
150 store_on_fallback(privctx, *publickey, intrans);
156 static int stacked_fetch_key_fp(struct onak_dbctx *dbctx,
157 struct openpgp_fingerprint *fingerprint,
158 struct openpgp_publickey **publickey, bool intrans)
160 struct onak_stacked_dbctx *privctx =
161 (struct onak_stacked_dbctx *) dbctx->priv;
162 struct onak_dbctx *backend;
166 for (cur = privctx->backends; cur != NULL && res == 0;
168 backend = (struct onak_dbctx *) cur->object;
169 res = backend->fetch_key_fp(backend, fingerprint, publickey,
173 if (privctx->store_on_fallback && cur != privctx->backends) {
174 store_on_fallback(privctx, *publickey, intrans);
180 static int stacked_fetch_key_text(struct onak_dbctx *dbctx,
182 struct openpgp_publickey **publickey)
184 struct onak_stacked_dbctx *privctx =
185 (struct onak_stacked_dbctx *) dbctx->priv;
186 struct onak_dbctx *backend;
190 for (cur = privctx->backends; cur != NULL && res == 0;
192 backend = (struct onak_dbctx *) cur->object;
193 res = backend->fetch_key_text(backend, search, publickey);
196 if (privctx->store_on_fallback && cur != privctx->backends) {
197 store_on_fallback(privctx, *publickey, false);
203 static int stacked_fetch_key_skshash(struct onak_dbctx *dbctx,
204 const struct skshash *hash,
205 struct openpgp_publickey **publickey)
207 struct onak_stacked_dbctx *privctx =
208 (struct onak_stacked_dbctx *) dbctx->priv;
209 struct onak_dbctx *backend;
213 for (cur = privctx->backends; cur != NULL && res == 0;
215 backend = (struct onak_dbctx *) cur->object;
216 res = backend->fetch_key_skshash(backend, hash, publickey);
219 if (privctx->store_on_fallback && cur != privctx->backends) {
220 store_on_fallback(privctx, *publickey, false);
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.
232 #define NEED_KEYID2UID 1
233 #define NEED_GETKEYSIGS 1
234 #define NEED_UPDATEKEYS 1
237 static struct ll *stacked_getkeysigs(struct onak_dbctx *dbctx,
238 uint64_t keyid, bool *revoked)
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;
246 res = backend->getkeysigs(backend, keyid, revoked);
248 res = generic_getkeysigs(dbctx, keyid, revoked);
254 static struct ll *stacked_cached_getkeysigs(struct onak_dbctx *dbctx,
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;
263 res = backend->cached_getkeysigs(backend, keyid);
265 res = generic_cached_getkeysigs(dbctx, keyid);
271 static char *stacked_keyid2uid(struct onak_dbctx *dbctx,
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;
280 res = backend->keyid2uid(backend, keyid);
282 res = generic_keyid2uid(dbctx, keyid);
288 static void stacked_cleanupdb(struct onak_dbctx *dbctx)
290 struct onak_stacked_dbctx *privctx =
291 (struct onak_stacked_dbctx *) dbctx->priv;
292 struct onak_dbctx *backend;
296 for (cur = privctx->backends; cur != NULL && res == 0;
298 backend = (struct onak_dbctx *) cur->object;
299 backend->cleanupdb(backend);
302 if (dbctx->priv != NULL) {
312 struct onak_dbctx *keydb_stacked_init(struct onak_db_config *dbcfg,
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;
322 logthing(LOGTHING_CRITICAL,
323 "No backend database configuration supplied.");
327 dbctx = malloc(sizeof(struct onak_dbctx));
333 dbctx->config = dbcfg;
334 dbctx->priv = privctx = malloc(sizeof(struct onak_stacked_dbctx));
335 if (dbctx->priv == NULL) {
340 /* TODO: Make configurable? */
341 privctx->store_on_fallback = true;
342 privctx->backends = NULL;
344 backend_name = strtok_r(dbcfg->location, ":", &saveptr);
345 while (backend_name != NULL) {
346 backend_cfg = find_db_backend_config(config.backends,
348 if (backend_cfg == NULL) {
349 logthing(LOGTHING_CRITICAL,
350 "Couldn't find configuration for %s backend",
352 stacked_cleanupdb(dbctx);
355 logthing(LOGTHING_INFO, "Loading stacked backend: %s",
358 backend = config.dbinit(backend_cfg, readonly);
359 privctx->backends = lladdend(privctx->backends, backend);
361 backend_name = strtok_r(NULL, ":", &saveptr);
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;