]> the.earth.li Git - onak.git/blob - keydb_dynamic.c
Deprecate the .conf configuration file format
[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, 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 int dynamic_iterate_keys(struct onak_dbctx *dbctx,
163                 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
164                 void *ctx)
165 {
166         struct onak_dynamic_dbctx *privctx =
167                         (struct onak_dynamic_dbctx *) dbctx->priv;
168
169         return privctx->loadeddbctx->iterate_keys(privctx->loadeddbctx,
170                         iterfunc, ctx);
171 }
172
173 static void dynamic_cleanupdb(struct onak_dbctx *dbctx)
174 {
175         struct onak_dynamic_dbctx *privctx =
176                         (struct onak_dynamic_dbctx *) dbctx->priv;
177
178         if (privctx->loadeddbctx != NULL) {
179                 if (privctx->loadeddbctx->cleanupdb != NULL) {
180                         privctx->loadeddbctx->cleanupdb(privctx->loadeddbctx);
181                         privctx->loadeddbctx = NULL;
182                 }
183         }
184
185         if (privctx->backend_handle != NULL) {
186                 dlclose(privctx->backend_handle);
187                 privctx->backend_handle = NULL;
188         }
189
190         if (dbctx->priv != NULL) {
191                 free(dbctx->priv);
192                 dbctx->priv = NULL;
193         }
194
195         if (dbctx != NULL) {
196                 free(dbctx);
197         }
198 }
199
200 struct onak_dbctx *keydb_dynamic_init(struct onak_db_config *dbcfg,
201                 bool readonly)
202 {
203         struct onak_dbctx *dbctx;
204         char *soname;
205         char *initname;
206         struct onak_dbctx *(*backend_init)(struct onak_db_config *, bool);
207         struct onak_dynamic_dbctx *privctx;
208         char *type;
209
210         if (dbcfg == NULL) {
211                 logthing(LOGTHING_CRITICAL,
212                         "No backend database configuration supplied.");
213                 return NULL;
214         }
215
216         dbctx = malloc(sizeof(struct onak_dbctx));
217
218         if (dbctx == NULL) {
219                 return NULL;
220         }
221
222         dbctx->config = dbcfg;
223         dbctx->priv = privctx = malloc(sizeof(struct onak_dynamic_dbctx));
224         if (dbctx->priv == NULL) {
225                 free(dbctx);
226                 return (NULL);
227         }
228
229         type = dbcfg->type;
230         if (config.use_keyd) {
231                 type = "keyd";
232         }
233
234         if (!config.db_backend) {
235                 logthing(LOGTHING_CRITICAL, "No database backend defined.");
236                 exit(EXIT_FAILURE);
237         }
238
239         if (config.backends_dir == NULL) {
240                 soname = malloc(strlen(type)
241                         + strlen("./libkeydb_")
242                         + strlen(".so")
243                         + 1);
244
245                 sprintf(soname, "./libkeydb_%s.so", type);
246         } else {
247                 soname = malloc(strlen(type)
248                         + strlen("/libkeydb_")
249                         + strlen(".so")
250                         + strlen(config.backends_dir)
251                         + 1);
252
253                 sprintf(soname, "%s/libkeydb_%s.so", config.backends_dir,
254                         type);
255         }
256
257         logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
258
259         privctx->backend_handle = dlopen(soname, RTLD_LAZY);
260         if (privctx->backend_handle == NULL) {
261                 logthing(LOGTHING_CRITICAL,
262                                 "Failed to open handle to library '%s': %s",
263                                 soname, dlerror());
264                 free(soname);
265                 soname = NULL;
266                 exit(EXIT_FAILURE);
267         }
268
269         initname = malloc(strlen(config.db_backend)
270                         + strlen("keydb_")
271                         + strlen("_init")
272                         + 1);
273         sprintf(initname, "keydb_%s_init", type);
274
275         *(void **) (&backend_init) = dlsym(privctx->backend_handle, initname);
276         free(initname);
277
278         if (backend_init == NULL) {
279                 logthing(LOGTHING_CRITICAL,
280                                 "Failed to find dbfuncs structure in library "
281                                 "'%s' : %s", soname, dlerror());
282                 free(soname);
283                 soname = NULL;
284                 exit(EXIT_FAILURE);
285         }
286
287         privctx->loadeddbctx = backend_init(dbcfg, readonly);
288
289         if (privctx->loadeddbctx == NULL) {
290                 logthing(LOGTHING_CRITICAL,
291                                 "Failed to initialise dynamic backend: %s",
292                                 soname);
293                 free(soname);
294                 soname = NULL;
295                 exit(EXIT_FAILURE);
296         }
297         free(soname);
298         soname = NULL;
299
300         if (privctx->loadeddbctx != NULL) {
301                 dbctx->cleanupdb = dynamic_cleanupdb;
302                 dbctx->starttrans = dynamic_starttrans;
303                 dbctx->endtrans = dynamic_endtrans;
304                 dbctx->fetch_key_id = dynamic_fetch_key_id;
305                 dbctx->fetch_key_fp = dynamic_fetch_key_fp;
306                 dbctx->fetch_key_text = dynamic_fetch_key_text;
307                 dbctx->fetch_key_skshash = dynamic_fetch_key_skshash;
308                 dbctx->store_key = dynamic_store_key;
309                 dbctx->update_keys = dynamic_update_keys;
310                 dbctx->delete_key = dynamic_delete_key;
311                 dbctx->getkeysigs = dynamic_getkeysigs;
312                 dbctx->cached_getkeysigs = dynamic_cached_getkeysigs;
313                 dbctx->keyid2uid = dynamic_keyid2uid;
314                 dbctx->iterate_keys = dynamic_iterate_keys;
315         }
316
317         return dbctx;
318 }