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