]> the.earth.li Git - onak.git/blob - keydb/keydb.c
Provide key_fetch routine that will not search subkey fingerprints
[onak.git] / keydb / keydb.c
1 /*
2  * keydb.c - Routines for DB access that just use store/fetch.
3  *
4  * Copyright 2002-2004 Jonathan McDowell <noodles@earth.li>
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 /**
20  *      The routines in this file are meant to be used as an initial step when
21  *      adding a new db access module. They provide various functions required
22  *      of the db access module using only the store and fetch functions. As
23  *      they need to parse the actual OpenPGP data to work they are a lot
24  *      slower than custom functions however.
25  */
26
27 #include <stdbool.h>
28 #include <stdio.h>
29
30 #include "decodekey.h"
31 #include "hash.h"
32 #include "keydb.h"
33 #include "keyid.h"
34 #include "keystructs.h"
35 #include "ll.h"
36 #include "mem.h"
37 #include "merge.h"
38 #include "openpgp.h"
39 #include "sendsync.h"
40 #include "stats.h"
41
42 #ifdef NEED_KEYID2UID
43 /**
44  *      keyid2uid - Takes a keyid and returns the primary UID for it.
45  *      @keyid: The keyid to lookup.
46  */
47 char *generic_keyid2uid(struct onak_dbctx *dbctx, uint64_t keyid)
48 {
49         struct openpgp_publickey *publickey = NULL;
50         struct openpgp_signedpacket_list *curuid = NULL;
51         char buf[1024];
52
53         buf[0]=0;
54         if (dbctx->fetch_key_id(dbctx, keyid, &publickey, false) &&
55                         publickey != NULL) {
56                 curuid = publickey->uids;
57                 while (curuid != NULL && buf[0] == 0) {
58                         if (curuid->packet->tag == OPENPGP_PACKET_UID) {
59                                 snprintf(buf, 1023, "%.*s",
60                                                 (int) curuid->packet->length,
61                                                 curuid->packet->data);
62                         }
63                         curuid = curuid -> next;
64                 }
65                 free_publickey(publickey);
66         }
67
68         if (buf[0] == 0) {
69                 return NULL;
70         } else {
71                 return strdup(buf);
72         }
73 }
74 #endif
75
76 #ifdef NEED_GETKEYSIGS
77 /**
78  *      getkeysigs - Gets a linked list of the signatures on a key.
79  *      @keyid: The keyid to get the sigs for.
80  *      @revoked: Is the key revoked?
81  *
82  *      This function gets the list of signatures on a key. Used for key 
83  *      indexing and doing stats bits. If revoked is non-NULL then if the key
84  *      is revoked it's set to true.
85  */
86 struct ll *generic_getkeysigs(struct onak_dbctx *dbctx,
87                 uint64_t keyid, bool *revoked)
88 {
89         struct ll *sigs = NULL;
90         struct openpgp_signedpacket_list *uids = NULL;
91         struct openpgp_packet_list *cursig;
92         struct openpgp_publickey *publickey = NULL;
93
94         dbctx->fetch_key_id(dbctx, keyid, &publickey, false);
95         
96         if (publickey != NULL) {
97                 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
98                         for (cursig = uids->sigs; cursig != NULL;
99                                         cursig = cursig->next) {
100                                 sigs = lladd(sigs,
101                                                 createandaddtohash(sig_keyid(
102                                                         cursig->packet)));
103                         }
104                 }
105                 if (revoked != NULL) {
106                         *revoked = publickey->revoked;
107                 }
108                 free_publickey(publickey);
109         }
110
111         return sigs;
112 }
113 #endif
114
115 /**
116  *      cached_getkeysigs - Gets the signatures on a key.
117  *      @keyid: The key we want the signatures for.
118  *      
119  *      This function gets the signatures on a key. It's the same as the
120  *      getkeysigs function above except we use the hash module to cache the
121  *      data so if we need it again it's already loaded.
122  */
123 struct ll *generic_cached_getkeysigs(struct onak_dbctx *dbctx, uint64_t keyid)
124 {
125         struct stats_key *key = NULL;
126         struct stats_key *signedkey = NULL;
127         struct ll        *cursig = NULL;
128         struct ll        *sigs = NULL;
129         bool              revoked = false;
130
131         if (keyid == 0)  {
132                 return NULL;
133         }
134
135         key = findinhash(keyid);
136
137         if (key == NULL || key->gotsigs == false) {
138                 sigs = dbctx->getkeysigs(dbctx, keyid, &revoked);
139                 if (sigs == NULL) {
140                         return NULL;
141                 }
142                 if (key == NULL) {
143                         key = createandaddtohash(keyid);
144                 }
145                 key->sigs = sigs;
146                 key->revoked = revoked;
147                 for (cursig = key->sigs; cursig != NULL;
148                                 cursig = cursig->next) {
149                         signedkey = (struct stats_key *) cursig->object;
150                         signedkey->signs = lladd(signedkey->signs, key);
151                 }
152                 key->gotsigs = true;
153         }
154
155         return key->sigs;
156 }
157
158 #ifdef NEED_UPDATEKEYS
159 /**
160  *      update_keys - Takes a list of public keys and updates them in the DB.
161  *      @keys: The keys to update in the DB.
162  *      @blacklist: A keyarray of key fingerprints not to accept.
163  *      @updateonly: Only update existing keys, don't add new ones.
164  *      @sendsync: Should we send a sync mail to our peers.
165  *
166  *      Takes a list of keys and adds them to the database, merging them with
167  *      the key in the database if it's already present there. The key list is
168  *      update to contain the minimum set of updates required to get from what
169  *      we had before to what we have now (ie the set of data that was added to
170  *      the DB). Returns the number of entirely new keys added.
171  */
172 int generic_update_keys(struct onak_dbctx *dbctx,
173                 struct openpgp_publickey **keys,
174                 struct keyarray *blacklist,
175                 bool updateonly,
176                 bool sendsync)
177 {
178         struct openpgp_publickey **curkey, *tmp = NULL;
179         struct openpgp_publickey *oldkey = NULL;
180         struct openpgp_fingerprint fp;
181         int newkeys = 0, ret;
182         bool intrans;
183
184         curkey = keys;
185         while (*curkey != NULL) {
186                 get_fingerprint((*curkey)->publickey, &fp);
187                 if (blacklist && array_find(blacklist, &fp)) {
188                         logthing(LOGTHING_INFO, "Ignoring blacklisted key.");
189                         tmp = *curkey;
190                         *curkey = (*curkey)->next;
191                         tmp->next = NULL;
192                         free_publickey(tmp);
193                         continue;
194                 }
195
196                 intrans = dbctx->starttrans(dbctx);
197
198                 ret = dbctx->fetch_key_fp(dbctx, &fp, &oldkey, intrans);
199                 if (ret == 0 && updateonly) {
200                         logthing(LOGTHING_INFO,
201                                 "Skipping new key as update only set.");
202                         curkey = &(*curkey)->next;
203                         goto next;
204                 }
205
206                 /*
207                  * If we already have the key stored in the DB then merge it
208                  * with the new one that's been supplied. Otherwise the key
209                  * we've just got is the one that goes in the DB and also the
210                  * one that we send out.
211                  */
212                 if (oldkey != NULL) {
213                         merge_keys(oldkey, *curkey);
214                         if ((*curkey)->sigs == NULL &&
215                                         (*curkey)->uids == NULL &&
216                                         (*curkey)->subkeys == NULL) {
217                                 tmp = *curkey;
218                                 *curkey = (*curkey)->next;
219                                 tmp->next = NULL;
220                                 free_publickey(tmp);
221                         } else {
222                                 logthing(LOGTHING_INFO,
223                                         "Merged key; storing updated key.");
224                                 dbctx->store_key(dbctx, oldkey, intrans,
225                                         true);
226                                 curkey = &(*curkey)->next;
227                         }
228                         free_publickey(oldkey);
229                         oldkey = NULL;
230                 } else {
231                         logthing(LOGTHING_INFO,
232                                 "Storing completely new key.");
233                         dbctx->store_key(dbctx, *curkey, intrans, false);
234                         newkeys++;
235                         curkey = &(*curkey)->next;
236                 }
237 next:
238                 dbctx->endtrans(dbctx);
239         }
240
241         if (sendsync && keys != NULL && *keys != NULL) {
242                 sendkeysync(*keys);
243         }
244
245         return newkeys;
246 }
247 #endif /* NEED_UPDATEKEYS */
248
249 #ifdef NEED_GET_FP
250 static int generic_fetch_key_fp(struct onak_dbctx *dbctx,
251                 struct openpgp_fingerprint *fingerprint,
252                 struct openpgp_publickey **publickey, bool intrans)
253 {
254         uint64_t keyid;
255         int i;
256
257         if (fingerprint->length > MAX_FINGERPRINT_LEN) {
258                 return 0;
259         }
260
261         /*
262          * We assume if the backend is using this function it's not storing
263          * anything bigger than the 64 bit key ID and just truncate the
264          * fingerprint to get that value. v4 keys want the lowest 64 bits, v5
265          * keys need the top 64 bits.  This doesn't work for v3 keys,
266          * but there's no way to map from v3 fingerprint to v3 key ID so
267          * if the backend can't do it we're going to fail anyway.
268          *
269          * We are also assuming they store a single key based on the ID, so
270          * we are implementing fetch_key rather than fetch_key_fp
271          */
272         keyid = 0;
273         if (fingerprint->length == 20) {
274                 /* v4 */
275                 for (i = (fingerprint->length - 8); i < fingerprint->length;
276                                 i++) {
277                         keyid = (keyid << 8) + fingerprint->fp[i];
278                 }
279         } else {
280                 /* v5 */
281                 for (i = 0; i < 8; i++) {
282                         keyid = (keyid << 8) + fingerprint->fp[i];
283                 }
284         }
285
286         return dbctx->fetch_key_id(dbctx, keyid, publickey, intrans);
287 }
288 #endif
289
290 #ifdef NEED_GET
291 /*
292  * This fetches a key by fingerprint from the back end, then filters
293  * out what we got back to ensure it's the primary key that matches the
294  * fingerprint, and that only one is returned.
295  */
296 static int generic_fetch_key(struct onak_dbctx *dbctx,
297                 struct openpgp_fingerprint *fingerprint,
298                 struct openpgp_publickey **publickey,
299                 bool intrans)
300 {
301         struct openpgp_publickey *curkey, **newkey;
302         struct openpgp_publickey *keys;
303         struct openpgp_fingerprint fp;
304         int count;
305
306         /* Find the last key in the provided set of keys */
307         for (newkey = publickey; *newkey != NULL; newkey = &(*newkey)->next)
308                 ;
309
310         keys = NULL;
311         dbctx->fetch_key_fp(dbctx, fingerprint, &keys, intrans);
312
313         count = 0;
314         for (curkey = keys; curkey != NULL; curkey = curkey->next) {
315                 if (get_fingerprint(curkey->publickey, &fp) == ONAK_E_OK) {
316                         if (fingerprint_cmp(fingerprint, &fp) == 0) {
317                                 *newkey = curkey;
318                                 curkey = curkey->next;
319                                 (*newkey)->next = NULL;
320                                 count = 1;
321                                 break;
322                         }
323                 }
324         }
325         free_publickey(keys);
326
327         return count;
328 }
329 #endif