]> the.earth.li Git - onak.git/blob - keydb_dynamic.c
Change delete_key to use a full fingerprint
[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 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         char *type;
218
219         if (dbcfg == NULL) {
220                 logthing(LOGTHING_CRITICAL,
221                         "No backend database configuration supplied.");
222                 return NULL;
223         }
224
225         dbctx = malloc(sizeof(struct onak_dbctx));
226
227         if (dbctx == NULL) {
228                 return NULL;
229         }
230
231         dbctx->config = dbcfg;
232         dbctx->priv = privctx = malloc(sizeof(struct onak_dynamic_dbctx));
233         if (dbctx->priv == NULL) {
234                 free(dbctx);
235                 return (NULL);
236         }
237
238         type = dbcfg->type;
239         if (config.use_keyd) {
240                 type = "keyd";
241         }
242
243         if (!config.db_backend) {
244                 logthing(LOGTHING_CRITICAL, "No database backend defined.");
245                 exit(EXIT_FAILURE);
246         }
247
248         if (config.backends_dir == NULL) {
249                 soname = malloc(strlen(type)
250                         + strlen("./libkeydb_")
251                         + strlen(".so")
252                         + 1);
253
254                 sprintf(soname, "./libkeydb_%s.so", type);
255         } else {
256                 soname = malloc(strlen(type)
257                         + strlen("/libkeydb_")
258                         + strlen(".so")
259                         + strlen(config.backends_dir)
260                         + 1);
261
262                 sprintf(soname, "%s/libkeydb_%s.so", config.backends_dir,
263                         type);
264         }
265
266         logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
267
268         privctx->backend_handle = dlopen(soname, RTLD_LAZY);
269         if (privctx->backend_handle == NULL) {
270                 logthing(LOGTHING_CRITICAL,
271                                 "Failed to open handle to library '%s': %s",
272                                 soname, dlerror());
273                 free(soname);
274                 soname = NULL;
275                 exit(EXIT_FAILURE);
276         }
277
278         initname = malloc(strlen(config.db_backend)
279                         + strlen("keydb_")
280                         + strlen("_init")
281                         + 1);
282         sprintf(initname, "keydb_%s_init", type);
283
284         *(void **) (&backend_init) = dlsym(privctx->backend_handle, initname);
285         free(initname);
286
287         if (backend_init == NULL) {
288                 logthing(LOGTHING_CRITICAL,
289                                 "Failed to find dbfuncs structure in library "
290                                 "'%s' : %s", soname, dlerror());
291                 free(soname);
292                 soname = NULL;
293                 exit(EXIT_FAILURE);
294         }
295
296         privctx->loadeddbctx = backend_init(dbcfg, readonly);
297
298         if (privctx->loadeddbctx == NULL) {
299                 logthing(LOGTHING_CRITICAL,
300                                 "Failed to initialise dynamic backend: %s",
301                                 soname);
302                 free(soname);
303                 soname = NULL;
304                 exit(EXIT_FAILURE);
305         }
306         free(soname);
307         soname = NULL;
308
309         if (privctx->loadeddbctx != NULL) {
310                 dbctx->cleanupdb = dynamic_cleanupdb;
311                 dbctx->starttrans = dynamic_starttrans;
312                 dbctx->endtrans = dynamic_endtrans;
313                 dbctx->fetch_key_id = dynamic_fetch_key_id;
314                 dbctx->fetch_key_fp = dynamic_fetch_key_fp;
315                 dbctx->fetch_key_text = dynamic_fetch_key_text;
316                 dbctx->fetch_key_skshash = dynamic_fetch_key_skshash;
317                 dbctx->store_key = dynamic_store_key;
318                 dbctx->update_keys = dynamic_update_keys;
319                 dbctx->delete_key = dynamic_delete_key;
320                 dbctx->getkeysigs = dynamic_getkeysigs;
321                 dbctx->cached_getkeysigs = dynamic_cached_getkeysigs;
322                 dbctx->keyid2uid = dynamic_keyid2uid;
323                 dbctx->getfullkeyid = dynamic_getfullkeyid;
324                 dbctx->iterate_keys = dynamic_iterate_keys;
325         }
326
327         return dbctx;
328 }