]> the.earth.li Git - onak.git/blob - keydb_dynamic.c
Add ability to drop overly large packets
[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, uint64_t keyid,
112                 bool intrans)
113 {
114         struct onak_dynamic_dbctx *privctx =
115                         (struct onak_dynamic_dbctx *) dbctx->priv;
116
117         return privctx->loadeddbctx->delete_key(privctx->loadeddbctx,
118                         keyid, intrans);
119 }
120
121 static int dynamic_update_keys(struct onak_dbctx *dbctx,
122                 struct openpgp_publickey **keys, bool sendsync)
123 {
124         struct onak_dynamic_dbctx *privctx =
125                         (struct onak_dynamic_dbctx *) dbctx->priv;
126
127         return privctx->loadeddbctx->update_keys(privctx->loadeddbctx,
128                         keys, sendsync);
129 }
130
131 static struct ll *dynamic_getkeysigs(struct onak_dbctx *dbctx,
132                 uint64_t keyid, bool *revoked)
133 {
134         struct onak_dynamic_dbctx *privctx =
135                         (struct onak_dynamic_dbctx *) dbctx->priv;
136
137         return privctx->loadeddbctx->getkeysigs(privctx->loadeddbctx,
138                         keyid, revoked);
139 }
140
141 static struct ll *dynamic_cached_getkeysigs(struct onak_dbctx *dbctx,
142                 uint64_t keyid)
143 {
144         struct onak_dynamic_dbctx *privctx =
145                         (struct onak_dynamic_dbctx *) dbctx->priv;
146
147         return privctx->loadeddbctx->cached_getkeysigs(privctx->loadeddbctx,
148                         keyid);
149 }
150
151 static char *dynamic_keyid2uid(struct onak_dbctx *dbctx,
152                         uint64_t keyid)
153 {
154         struct onak_dynamic_dbctx *privctx =
155                         (struct onak_dynamic_dbctx *) dbctx->priv;
156
157         return privctx->loadeddbctx->keyid2uid(privctx->loadeddbctx,
158                         keyid);
159 }
160
161 static uint64_t dynamic_getfullkeyid(struct onak_dbctx *dbctx,
162                 uint64_t keyid)
163 {
164         struct onak_dynamic_dbctx *privctx =
165                         (struct onak_dynamic_dbctx *) dbctx->priv;
166
167         return privctx->loadeddbctx->getfullkeyid(privctx->loadeddbctx, keyid);
168 }
169
170 static int dynamic_iterate_keys(struct onak_dbctx *dbctx,
171                 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
172                 void *ctx)
173 {
174         struct onak_dynamic_dbctx *privctx =
175                         (struct onak_dynamic_dbctx *) dbctx->priv;
176
177         return privctx->loadeddbctx->iterate_keys(privctx->loadeddbctx,
178                         iterfunc, ctx);
179 }
180
181 static void dynamic_cleanupdb(struct onak_dbctx *dbctx)
182 {
183         struct onak_dynamic_dbctx *privctx =
184                         (struct onak_dynamic_dbctx *) dbctx->priv;
185
186         if (privctx->loadeddbctx != NULL) {
187                 if (privctx->loadeddbctx->cleanupdb != NULL) {
188                         privctx->loadeddbctx->cleanupdb(privctx->loadeddbctx);
189                         privctx->loadeddbctx = NULL;
190                 }
191         }
192
193         if (privctx->backend_handle != NULL) {
194                 dlclose(privctx->backend_handle);
195                 privctx->backend_handle = NULL;
196         }
197
198         if (dbctx->priv != NULL) {
199                 free(dbctx->priv);
200                 dbctx->priv = NULL;
201         }
202
203         if (dbctx != NULL) {
204                 free(dbctx);
205         }
206 }
207
208 struct onak_dbctx *keydb_dynamic_init(struct onak_db_config *dbcfg,
209                 bool readonly)
210 {
211         struct onak_dbctx *dbctx;
212         char *soname;
213         char *initname;
214         struct onak_dbctx *(*backend_init)(struct onak_db_config *, bool);
215         struct onak_dynamic_dbctx *privctx;
216         char *type;
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         type = dbcfg->type;
238         if (config.use_keyd) {
239                 type = "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(type)
249                         + strlen("./libkeydb_")
250                         + strlen(".so")
251                         + 1);
252
253                 sprintf(soname, "./libkeydb_%s.so", type);
254         } else {
255                 soname = malloc(strlen(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                         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", 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 }