]> the.earth.li Git - onak.git/blob - keydb.c
Throw away invalid packet data when parsing packets
[onak.git] / 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, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  *      The routines in this file are meant to be used as an initial step when
22  *      adding a new db access module. They provide various functions required
23  *      of the db access module using only the store and fetch functions. As
24  *      they need to parse the actual OpenPGP data to work they are a lot
25  *      slower than custom functions however.
26  */
27
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 "mem.h"
36 #include "merge.h"
37 #include "openpgp.h"
38 #include "parsekey.h"
39 #include "sendsync.h"
40
41 #ifdef NEED_KEYID2UID
42 /**
43  *      keyid2uid - Takes a keyid and returns the primary UID for it.
44  *      @keyid: The keyid to lookup.
45  */
46 char *generic_keyid2uid(struct onak_dbctx *dbctx, uint64_t keyid)
47 {
48         struct openpgp_publickey *publickey = NULL;
49         struct openpgp_signedpacket_list *curuid = NULL;
50         char buf[1024];
51
52         buf[0]=0;
53         if (dbctx->fetch_key_id(dbctx, keyid, &publickey, false) &&
54                         publickey != NULL) {
55                 curuid = publickey->uids;
56                 while (curuid != NULL && buf[0] == 0) {
57                         if (curuid->packet->tag == OPENPGP_PACKET_UID) {
58                                 snprintf(buf, 1023, "%.*s",
59                                                 (int) curuid->packet->length,
60                                                 curuid->packet->data);
61                         }
62                         curuid = curuid -> next;
63                 }
64                 free_publickey(publickey);
65         }
66
67         if (buf[0] == 0) {
68                 return NULL;
69         } else {
70                 return strdup(buf);
71         }
72 }
73 #endif
74
75 #ifdef NEED_GETKEYSIGS
76 /**
77  *      getkeysigs - Gets a linked list of the signatures on a key.
78  *      @keyid: The keyid to get the sigs for.
79  *      @revoked: Is the key revoked?
80  *
81  *      This function gets the list of signatures on a key. Used for key 
82  *      indexing and doing stats bits. If revoked is non-NULL then if the key
83  *      is revoked it's set to true.
84  */
85 struct ll *generic_getkeysigs(struct onak_dbctx *dbctx,
86                 uint64_t keyid, bool *revoked)
87 {
88         struct ll *sigs = NULL;
89         struct openpgp_signedpacket_list *uids = NULL;
90         struct openpgp_publickey *publickey = NULL;
91
92         dbctx->fetch_key_id(dbctx, keyid, &publickey, false);
93         
94         if (publickey != NULL) {
95                 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
96                         sigs = keysigs(sigs, uids->sigs);
97                 }
98                 if (revoked != NULL) {
99                         *revoked = publickey->revoked;
100                 }
101                 free_publickey(publickey);
102         }
103
104         return sigs;
105 }
106 #endif
107
108 /**
109  *      cached_getkeysigs - Gets the signatures on a key.
110  *      @keyid: The key we want the signatures for.
111  *      
112  *      This function gets the signatures on a key. It's the same as the
113  *      getkeysigs function above except we use the hash module to cache the
114  *      data so if we need it again it's already loaded.
115  */
116 struct ll *generic_cached_getkeysigs(struct onak_dbctx *dbctx, uint64_t keyid)
117 {
118         struct stats_key *key = NULL;
119         struct stats_key *signedkey = NULL;
120         struct ll        *cursig = NULL;
121         struct ll        *sigs = NULL;
122         bool              revoked = false;
123
124         if (keyid == 0)  {
125                 return NULL;
126         }
127
128         key = findinhash(keyid);
129
130         if (key == NULL || key->gotsigs == false) {
131                 sigs = dbctx->getkeysigs(dbctx, keyid, &revoked);
132                 if (sigs == NULL) {
133                         return NULL;
134                 }
135                 if (key == NULL) {
136                         key = createandaddtohash(keyid);
137                 }
138                 key->sigs = sigs;
139                 key->revoked = revoked;
140                 for (cursig = key->sigs; cursig != NULL;
141                                 cursig = cursig->next) {
142                         signedkey = (struct stats_key *) cursig->object;
143                         signedkey->signs = lladd(signedkey->signs, key);
144                 }
145                 key->gotsigs = true;
146         }
147
148         return key->sigs;
149 }
150
151 #ifdef NEED_GETFULLKEYID
152 /**
153  *      getfullkeyid - Maps a 32bit key id to a 64bit one.
154  *      @keyid: The 32bit keyid.
155  *
156  *      This function maps a 32bit key id to the full 64bit one. It returns the
157  *      full keyid. If the key isn't found a keyid of 0 is returned.
158  */
159 uint64_t generic_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
160 {
161         struct openpgp_publickey *publickey = NULL;
162
163         if (keyid < 0x100000000LL) {
164                 dbctx->fetch_key_id(dbctx, keyid, &publickey, false);
165                 if (publickey != NULL) {
166                         get_keyid(publickey, &keyid);
167                         free_publickey(publickey);
168                         publickey = NULL;
169                 } else {
170                         keyid = 0;
171                 }
172         }
173         
174         return keyid;
175 }
176 #endif
177
178 #ifdef NEED_UPDATEKEYS
179 /**
180  *      update_keys - Takes a list of public keys and updates them in the DB.
181  *      @keys: The keys to update in the DB.
182  *      @sendsync: Should we send a sync mail to our peers.
183  *
184  *      Takes a list of keys and adds them to the database, merging them with
185  *      the key in the database if it's already present there. The key list is
186  *      update to contain the minimum set of updates required to get from what
187  *      we had before to what we have now (ie the set of data that was added to
188  *      the DB). Returns the number of entirely new keys added.
189  */
190 int generic_update_keys(struct onak_dbctx *dbctx,
191                 struct openpgp_publickey **keys, bool sendsync)
192 {
193         struct openpgp_publickey *curkey = NULL;
194         struct openpgp_publickey *oldkey = NULL;
195         struct openpgp_publickey *prev = NULL;
196         int newkeys = 0;
197         bool intrans;
198         uint64_t keyid;
199
200         for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
201                 intrans = dbctx->starttrans(dbctx);
202                 get_keyid(curkey, &keyid);
203                 logthing(LOGTHING_INFO,
204                         "Fetching key 0x%" PRIX64 ", result: %d",
205                         keyid,
206                         dbctx->fetch_key_id(dbctx, keyid, &oldkey,
207                                         intrans));
208
209                 /*
210                  * If we already have the key stored in the DB then merge it
211                  * with the new one that's been supplied. Otherwise the key
212                  * we've just got is the one that goes in the DB and also the
213                  * one that we send out.
214                  */
215                 if (oldkey != NULL) {
216                         merge_keys(oldkey, curkey);
217                         if (curkey->sigs == NULL &&
218                                         curkey->uids == NULL &&
219                                         curkey->subkeys == NULL) {
220                                 if (prev == NULL) {
221                                         *keys = curkey->next;
222                                 } else {
223                                         prev->next = curkey->next;
224                                         curkey->next = NULL;
225                                         free_publickey(curkey);
226                                         curkey = prev;
227                                 }
228                         } else {
229                                 prev = curkey;
230                                 logthing(LOGTHING_INFO,
231                                         "Merged key; storing updated key.");
232                                 dbctx->store_key(dbctx, oldkey, intrans,
233                                         true);
234                         }
235                         free_publickey(oldkey);
236                         oldkey = NULL;
237                 } else {
238                         logthing(LOGTHING_INFO,
239                                 "Storing completely new key.");
240                         dbctx->store_key(dbctx, curkey, intrans, false);
241                         newkeys++;
242                 }
243                 dbctx->endtrans(dbctx);
244         }
245
246         if (sendsync && keys != NULL) {
247                 sendkeysync(*keys);
248         }
249
250         return newkeys;
251 }
252 #endif /* NEED_UPDATEKEYS */
253
254 #ifdef NEED_GET_FP
255 static int generic_fetch_key_fp(struct onak_dbctx *dbctx,
256                 struct openpgp_fingerprint *fingerprint,
257                 struct openpgp_publickey **publickey, bool intrans)
258 {
259         uint64_t keyid;
260         int i;
261
262         if (fingerprint->length > MAX_FINGERPRINT_LEN) {
263                 return 0;
264         }
265
266         /*
267          * We assume if the backend is using this function it's not storing
268          * anything bigger than the 64 bit key ID and just truncate the
269          * fingerprint to get that value. This doesn't work for v3 keys,
270          * but there's no way to map from v3 fingerprint to v3 key ID so
271          * if the backend can't do it we're going to fail anyway.
272          */
273         keyid = 0;
274         for (i = (fingerprint->length - 8); i < fingerprint->length; i++) {
275                 keyid = (keyid << 8) + fingerprint->fp[i];
276         }
277
278         return dbctx->fetch_key_id(dbctx, keyid, publickey, intrans);
279 }
280 #endif