]> the.earth.li Git - onak.git/blob - keydb_dynamic.c
cf9d87fa030af270ae48bacfa154e139c6a7577a
[onak.git] / keydb_dynamic.c
1 /*
2  * keydb_dynamic.c - backend that can load the other backends
3  *
4  * Copyright 2005 Brett Parker <iDunno@sommitrealweird.co.uk>
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 <dlfcn.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "decodekey.h"
24 #include "hash.h"
25 #include "keydb.h"
26 #include "keyid.h"
27 #include "keystructs.h"
28 #include "log.h"
29 #include "mem.h"
30 #include "merge.h"
31 #include "onak-conf.h"
32 #include "openpgp.h"
33 #include "parsekey.h"
34 #include "sendsync.h"
35
36 struct onak_dynamic_dbctx {
37         struct onak_dbctx *loadeddbctx;
38         void              *backend_handle;
39 };
40
41 static bool dynamic_starttrans(struct onak_dbctx *dbctx)
42 {
43         struct onak_dynamic_dbctx *privctx =
44                         (struct onak_dynamic_dbctx *) dbctx->priv;
45
46         return privctx->loadeddbctx->starttrans(privctx->loadeddbctx);
47 }
48
49 static void dynamic_endtrans(struct onak_dbctx *dbctx)
50 {
51         struct onak_dynamic_dbctx *privctx =
52                         (struct onak_dynamic_dbctx *) dbctx->priv;
53
54         privctx->loadeddbctx->endtrans(privctx->loadeddbctx);
55 }
56
57 static int dynamic_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
58                 struct openpgp_publickey **publickey, bool intrans)
59 {
60         struct onak_dynamic_dbctx *privctx =
61                         (struct onak_dynamic_dbctx *) dbctx->priv;
62
63         return privctx->loadeddbctx->fetch_key_id(privctx->loadeddbctx, keyid,
64                         publickey, intrans);
65 }
66
67 static int dynamic_fetch_key_fp(struct onak_dbctx *dbctx,
68                 struct openpgp_fingerprint *fingerprint,
69                 struct openpgp_publickey **publickey, bool intrans)
70 {
71         struct onak_dynamic_dbctx *privctx =
72                         (struct onak_dynamic_dbctx *) dbctx->priv;
73
74         return privctx->loadeddbctx->fetch_key_fp(privctx->loadeddbctx,
75                         fingerprint, publickey, intrans);
76 }
77
78 static int dynamic_fetch_key_text(struct onak_dbctx *dbctx,
79                 const char *search,
80                 struct openpgp_publickey **publickey)
81 {
82         struct onak_dynamic_dbctx *privctx =
83                         (struct onak_dynamic_dbctx *) dbctx->priv;
84
85         return privctx->loadeddbctx->fetch_key_text(privctx->loadeddbctx,
86                         search, publickey);
87 }
88
89 static int dynamic_fetch_key_skshash(struct onak_dbctx *dbctx,
90                 const struct skshash *hash,
91                 struct openpgp_publickey **publickey)
92 {
93         struct onak_dynamic_dbctx *privctx =
94                         (struct onak_dynamic_dbctx *) dbctx->priv;
95
96         return privctx->loadeddbctx->fetch_key_skshash(privctx->loadeddbctx,
97                         hash, publickey);
98 }
99
100 static int dynamic_store_key(struct onak_dbctx *dbctx,
101                 struct openpgp_publickey *publickey, bool intrans,
102                 bool update)
103 {
104         struct onak_dynamic_dbctx *privctx =
105                         (struct onak_dynamic_dbctx *) dbctx->priv;
106
107         return privctx->loadeddbctx->store_key(privctx->loadeddbctx,
108                         publickey, intrans, update);
109 }
110
111 static int dynamic_delete_key(struct onak_dbctx *dbctx,
112                 struct openpgp_fingerprint *fp,
113                 bool intrans)
114 {
115         struct onak_dynamic_dbctx *privctx =
116                         (struct onak_dynamic_dbctx *) dbctx->priv;
117
118         return privctx->loadeddbctx->delete_key(privctx->loadeddbctx,
119                         fp, intrans);
120 }
121
122 static int dynamic_update_keys(struct onak_dbctx *dbctx,
123                 struct openpgp_publickey **keys,
124                 struct keyarray *blacklist,
125                 bool sendsync)
126 {
127         struct onak_dynamic_dbctx *privctx =
128                         (struct onak_dynamic_dbctx *) dbctx->priv;
129
130         return privctx->loadeddbctx->update_keys(privctx->loadeddbctx,
131                         keys, blacklist, sendsync);
132 }
133
134 static struct ll *dynamic_getkeysigs(struct onak_dbctx *dbctx,
135                 uint64_t keyid, bool *revoked)
136 {
137         struct onak_dynamic_dbctx *privctx =
138                         (struct onak_dynamic_dbctx *) dbctx->priv;
139
140         return privctx->loadeddbctx->getkeysigs(privctx->loadeddbctx,
141                         keyid, revoked);
142 }
143
144 static struct ll *dynamic_cached_getkeysigs(struct onak_dbctx *dbctx,
145                 uint64_t keyid)
146 {
147         struct onak_dynamic_dbctx *privctx =
148                         (struct onak_dynamic_dbctx *) dbctx->priv;
149
150         return privctx->loadeddbctx->cached_getkeysigs(privctx->loadeddbctx,
151                         keyid);
152 }
153
154 static char *dynamic_keyid2uid(struct onak_dbctx *dbctx,
155                         uint64_t keyid)
156 {
157         struct onak_dynamic_dbctx *privctx =
158                         (struct onak_dynamic_dbctx *) dbctx->priv;
159
160         return privctx->loadeddbctx->keyid2uid(privctx->loadeddbctx,
161                         keyid);
162 }
163
164 static int dynamic_iterate_keys(struct onak_dbctx *dbctx,
165                 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
166                 void *ctx)
167 {
168         struct onak_dynamic_dbctx *privctx =
169                         (struct onak_dynamic_dbctx *) dbctx->priv;
170
171         return privctx->loadeddbctx->iterate_keys(privctx->loadeddbctx,
172                         iterfunc, ctx);
173 }
174
175 static void dynamic_cleanupdb(struct onak_dbctx *dbctx)
176 {
177         struct onak_dynamic_dbctx *privctx =
178                         (struct onak_dynamic_dbctx *) dbctx->priv;
179
180         if (privctx->loadeddbctx != NULL) {
181                 if (privctx->loadeddbctx->cleanupdb != NULL) {
182                         privctx->loadeddbctx->cleanupdb(privctx->loadeddbctx);
183                         privctx->loadeddbctx = NULL;
184                 }
185         }
186
187         if (privctx->backend_handle != NULL) {
188                 dlclose(privctx->backend_handle);
189                 privctx->backend_handle = NULL;
190         }
191
192         if (dbctx->priv != NULL) {
193                 free(dbctx->priv);
194                 dbctx->priv = NULL;
195         }
196
197         if (dbctx != NULL) {
198                 free(dbctx);
199         }
200 }
201
202 struct onak_dbctx *keydb_dynamic_init(struct onak_db_config *dbcfg,
203                 bool readonly)
204 {
205         struct onak_dbctx *dbctx;
206         char *soname;
207         char *initname;
208         struct onak_dbctx *(*backend_init)(struct onak_db_config *, bool);
209         struct onak_dynamic_dbctx *privctx;
210         char *type;
211
212         if (dbcfg == NULL) {
213                 logthing(LOGTHING_CRITICAL,
214                         "No backend database configuration supplied.");
215                 return NULL;
216         }
217
218         dbctx = malloc(sizeof(struct onak_dbctx));
219
220         if (dbctx == NULL) {
221                 return NULL;
222         }
223
224         dbctx->config = dbcfg;
225         dbctx->priv = privctx = malloc(sizeof(struct onak_dynamic_dbctx));
226         if (dbctx->priv == NULL) {
227                 free(dbctx);
228                 return (NULL);
229         }
230
231         type = dbcfg->type;
232         if (config.use_keyd) {
233                 type = "keyd";
234         }
235
236         if (!config.db_backend) {
237                 logthing(LOGTHING_CRITICAL, "No database backend defined.");
238                 exit(EXIT_FAILURE);
239         }
240
241         if (config.backends_dir == NULL) {
242                 soname = malloc(strlen(type)
243                         + strlen("./libkeydb_")
244                         + strlen(".so")
245                         + 1);
246
247                 sprintf(soname, "./libkeydb_%s.so", type);
248         } else {
249                 soname = malloc(strlen(type)
250                         + strlen("/libkeydb_")
251                         + strlen(".so")
252                         + strlen(config.backends_dir)
253                         + 1);
254
255                 sprintf(soname, "%s/libkeydb_%s.so", config.backends_dir,
256                         type);
257         }
258
259         logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
260
261         privctx->backend_handle = dlopen(soname, RTLD_LAZY);
262         if (privctx->backend_handle == NULL) {
263                 logthing(LOGTHING_CRITICAL,
264                                 "Failed to open handle to library '%s': %s",
265                                 soname, dlerror());
266                 free(soname);
267                 soname = NULL;
268                 exit(EXIT_FAILURE);
269         }
270
271         initname = malloc(strlen(config.db_backend)
272                         + strlen("keydb_")
273                         + strlen("_init")
274                         + 1);
275         sprintf(initname, "keydb_%s_init", type);
276
277         *(void **) (&backend_init) = dlsym(privctx->backend_handle, initname);
278         free(initname);
279
280         if (backend_init == NULL) {
281                 logthing(LOGTHING_CRITICAL,
282                                 "Failed to find dbfuncs structure in library "
283                                 "'%s' : %s", soname, dlerror());
284                 free(soname);
285                 soname = NULL;
286                 exit(EXIT_FAILURE);
287         }
288
289         privctx->loadeddbctx = backend_init(dbcfg, readonly);
290
291         if (privctx->loadeddbctx == NULL) {
292                 logthing(LOGTHING_CRITICAL,
293                                 "Failed to initialise dynamic backend: %s",
294                                 soname);
295                 free(soname);
296                 soname = NULL;
297                 exit(EXIT_FAILURE);
298         }
299         free(soname);
300         soname = NULL;
301
302         if (privctx->loadeddbctx != NULL) {
303                 dbctx->cleanupdb = dynamic_cleanupdb;
304                 dbctx->starttrans = dynamic_starttrans;
305                 dbctx->endtrans = dynamic_endtrans;
306                 dbctx->fetch_key_id = dynamic_fetch_key_id;
307                 dbctx->fetch_key_fp = dynamic_fetch_key_fp;
308                 dbctx->fetch_key_text = dynamic_fetch_key_text;
309                 dbctx->fetch_key_skshash = dynamic_fetch_key_skshash;
310                 dbctx->store_key = dynamic_store_key;
311                 dbctx->update_keys = dynamic_update_keys;
312                 dbctx->delete_key = dynamic_delete_key;
313                 dbctx->getkeysigs = dynamic_getkeysigs;
314                 dbctx->cached_getkeysigs = dynamic_cached_getkeysigs;
315                 dbctx->keyid2uid = dynamic_keyid2uid;
316                 dbctx->iterate_keys = dynamic_iterate_keys;
317         }
318
319         return dbctx;
320 }