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,
92 struct onak_stacked_dbctx *privctx =
93 (struct onak_stacked_dbctx *) dbctx->priv;
94 struct onak_dbctx *backend =
95 (struct onak_dbctx *) privctx->backends->object;
97 return backend->update_keys(backend, keys, blacklist, sendsync);
100 static int stacked_iterate_keys(struct onak_dbctx *dbctx,
101 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
104 struct onak_stacked_dbctx *privctx =
105 (struct onak_stacked_dbctx *) dbctx->priv;
106 struct onak_dbctx *backend =
107 (struct onak_dbctx *) privctx->backends->object;
109 return backend->iterate_keys(backend, iterfunc, ctx);
112 static void store_on_fallback(struct onak_stacked_dbctx *privctx,
113 struct openpgp_publickey *publickey, bool intrans)
115 struct onak_dbctx *backend =
116 (struct onak_dbctx *) privctx->backends->object;
117 struct openpgp_publickey *curkey;
119 cleankeys(&publickey, config.clean_policies);
121 * If we walked the stack at all, store the key in the first
122 * backend if configured to do so. It's not an update as we
123 * know it's not there or we wouldn't have fallen back.
125 for (curkey = publickey; curkey != NULL; curkey = curkey->next) {
126 backend->store_key(backend, curkey, intrans, false);
131 * The functions below will walk along the backend stack until they
132 * reach the end or get a successful result.
135 static int stacked_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
136 struct openpgp_publickey **publickey, bool intrans)
138 struct onak_stacked_dbctx *privctx =
139 (struct onak_stacked_dbctx *) dbctx->priv;
140 struct onak_dbctx *backend;
144 for (cur = privctx->backends; cur != NULL && res == 0;
146 backend = (struct onak_dbctx *) cur->object;
147 res = backend->fetch_key_id(backend, keyid, publickey,
151 if (privctx->store_on_fallback && cur != privctx->backends) {
152 store_on_fallback(privctx, *publickey, intrans);
158 static int stacked_fetch_key_fp(struct onak_dbctx *dbctx,
159 struct openpgp_fingerprint *fingerprint,
160 struct openpgp_publickey **publickey, bool intrans)
162 struct onak_stacked_dbctx *privctx =
163 (struct onak_stacked_dbctx *) dbctx->priv;
164 struct onak_dbctx *backend;
168 for (cur = privctx->backends; cur != NULL && res == 0;
170 backend = (struct onak_dbctx *) cur->object;
171 res = backend->fetch_key_fp(backend, fingerprint, publickey,
175 if (privctx->store_on_fallback && cur != privctx->backends) {
176 store_on_fallback(privctx, *publickey, intrans);
182 static int stacked_fetch_key_text(struct onak_dbctx *dbctx,
184 struct openpgp_publickey **publickey)
186 struct onak_stacked_dbctx *privctx =
187 (struct onak_stacked_dbctx *) dbctx->priv;
188 struct onak_dbctx *backend;
192 for (cur = privctx->backends; cur != NULL && res == 0;
194 backend = (struct onak_dbctx *) cur->object;
195 res = backend->fetch_key_text(backend, search, publickey);
198 if (privctx->store_on_fallback && cur != privctx->backends) {
199 store_on_fallback(privctx, *publickey, false);
205 static int stacked_fetch_key_skshash(struct onak_dbctx *dbctx,
206 const struct skshash *hash,
207 struct openpgp_publickey **publickey)
209 struct onak_stacked_dbctx *privctx =
210 (struct onak_stacked_dbctx *) dbctx->priv;
211 struct onak_dbctx *backend;
215 for (cur = privctx->backends; cur != NULL && res == 0;
217 backend = (struct onak_dbctx *) cur->object;
218 res = backend->fetch_key_skshash(backend, hash, publickey);
221 if (privctx->store_on_fallback && cur != privctx->backends) {
222 store_on_fallback(privctx, *publickey, false);
229 * Include the basic keydb routines so we can use them for fall back.
230 * For all of the following we try the top keydb backend and if that doesn't
231 * have answer fall back to the generics, which will do a retrieve from a
232 * backend further down the stack, then a fallback store.
234 #define NEED_KEYID2UID 1
235 #define NEED_GETKEYSIGS 1
236 #define NEED_UPDATEKEYS 1
239 static struct ll *stacked_getkeysigs(struct onak_dbctx *dbctx,
240 uint64_t keyid, bool *revoked)
242 struct onak_stacked_dbctx *privctx =
243 (struct onak_stacked_dbctx *) dbctx->priv;
244 struct onak_dbctx *backend =
245 (struct onak_dbctx *) privctx->backends->object;
248 res = backend->getkeysigs(backend, keyid, revoked);
250 res = generic_getkeysigs(dbctx, keyid, revoked);
256 static struct ll *stacked_cached_getkeysigs(struct onak_dbctx *dbctx,
259 struct onak_stacked_dbctx *privctx =
260 (struct onak_stacked_dbctx *) dbctx->priv;
261 struct onak_dbctx *backend =
262 (struct onak_dbctx *) privctx->backends->object;
265 res = backend->cached_getkeysigs(backend, keyid);
267 res = generic_cached_getkeysigs(dbctx, keyid);
273 static char *stacked_keyid2uid(struct onak_dbctx *dbctx,
276 struct onak_stacked_dbctx *privctx =
277 (struct onak_stacked_dbctx *) dbctx->priv;
278 struct onak_dbctx *backend =
279 (struct onak_dbctx *) privctx->backends->object;
282 res = backend->keyid2uid(backend, keyid);
284 res = generic_keyid2uid(dbctx, keyid);
290 static void stacked_cleanupdb(struct onak_dbctx *dbctx)
292 struct onak_stacked_dbctx *privctx =
293 (struct onak_stacked_dbctx *) dbctx->priv;
294 struct onak_dbctx *backend;
298 for (cur = privctx->backends; cur != NULL && res == 0;
300 backend = (struct onak_dbctx *) cur->object;
301 backend->cleanupdb(backend);
304 if (dbctx->priv != NULL) {
314 struct onak_dbctx *keydb_stacked_init(struct onak_db_config *dbcfg,
317 struct onak_dbctx *dbctx;
318 struct onak_stacked_dbctx *privctx;
319 struct onak_dbctx *backend;
320 struct onak_db_config *backend_cfg;
321 char *backend_name, *saveptr = NULL;
324 logthing(LOGTHING_CRITICAL,
325 "No backend database configuration supplied.");
329 dbctx = malloc(sizeof(struct onak_dbctx));
335 dbctx->config = dbcfg;
336 dbctx->priv = privctx = malloc(sizeof(struct onak_stacked_dbctx));
337 if (dbctx->priv == NULL) {
342 /* TODO: Make configurable? */
343 privctx->store_on_fallback = true;
344 privctx->backends = NULL;
346 backend_name = strtok_r(dbcfg->location, ":", &saveptr);
347 while (backend_name != NULL) {
348 backend_cfg = find_db_backend_config(config.backends,
350 if (backend_cfg == NULL) {
351 logthing(LOGTHING_CRITICAL,
352 "Couldn't find configuration for %s backend",
354 stacked_cleanupdb(dbctx);
357 logthing(LOGTHING_INFO, "Loading stacked backend: %s",
360 backend = config.dbinit(backend_cfg, readonly);
361 privctx->backends = lladdend(privctx->backends, backend);
363 backend_name = strtok_r(NULL, ":", &saveptr);
366 if (privctx->backends != NULL) {
367 dbctx->cleanupdb = stacked_cleanupdb;
368 dbctx->starttrans = stacked_starttrans;
369 dbctx->endtrans = stacked_endtrans;
370 dbctx->fetch_key_id = stacked_fetch_key_id;
371 dbctx->fetch_key_fp = stacked_fetch_key_fp;
372 dbctx->fetch_key_text = stacked_fetch_key_text;
373 dbctx->fetch_key_skshash = stacked_fetch_key_skshash;
374 dbctx->store_key = stacked_store_key;
375 dbctx->update_keys = stacked_update_keys;
376 dbctx->delete_key = stacked_delete_key;
377 dbctx->getkeysigs = stacked_getkeysigs;
378 dbctx->cached_getkeysigs = stacked_cached_getkeysigs;
379 dbctx->keyid2uid = stacked_keyid2uid;
380 dbctx->iterate_keys = stacked_iterate_keys;