]> the.earth.li Git - onak.git/blob - keydb_file.c
Throw away invalid packet data when parsing packets
[onak.git] / keydb_file.c
1 /*
2  * keydb.c - Routines to store and fetch keys.
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 #include <sys/types.h>
21 #include <sys/uio.h>
22 #include <dirent.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "charfuncs.h"
31 #include "keydb.h"
32 #include "keyid.h"
33 #include "keystructs.h"
34 #include "ll.h"
35 #include "log.h"
36 #include "mem.h"
37 #include "onak-conf.h"
38 #include "parsekey.h"
39
40 /**
41  *      starttrans - Start a transaction.
42  *
43  *      This is just a no-op for flat file access.
44  */
45 static bool file_starttrans(struct onak_dbctx *dbctx)
46 {
47         return true;
48 }
49
50 /**
51  *      endtrans - End a transaction.
52  *
53  *      This is just a no-op for flat file access.
54  */
55 static void file_endtrans(struct onak_dbctx *dbctx)
56 {
57         return;
58 }
59
60 /**
61  *      fetch_key_id - Given a keyid fetch the key from storage.
62  *      @keyid: The keyid to fetch.
63  *      @publickey: A pointer to a structure to return the key in.
64  *      @intrans: If we're already in a transaction.
65  *
66  *      We use the hex representation of the keyid as the filename to fetch the
67  *      key from. The key is stored in the file as a binary OpenPGP stream of
68  *      packets, so we can just use read_openpgp_stream() to read the packets
69  *      in and then parse_keys() to parse the packets into a publickey
70  *      structure.
71  */
72 static int file_fetch_key_id(struct onak_dbctx *dbctx,
73                 uint64_t keyid,
74                 struct openpgp_publickey **publickey,
75                 bool intrans)
76 {
77         char *db_dir = (char *) dbctx->priv;
78         struct openpgp_packet_list *packets = NULL;
79         char keyfile[1024];
80         int fd = -1;
81
82         snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
83                         keyid & 0xFFFFFFFF);
84         fd = open(keyfile, O_RDONLY); // | O_SHLOCK);
85
86         if (fd > -1) {
87                 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
88                 parse_keys(packets, publickey);
89                 free_packet_list(packets);
90                 packets = NULL;
91                 close(fd);
92         }
93
94         return (fd > -1);
95 }
96
97 /**
98  *      store_key - Takes a key and stores it.
99  *      @publickey: A pointer to the public key to store.
100  *      @intrans: If we're already in a transaction.
101  *      @update: If true the key exists and should be updated.
102  *
103  *      Again we just use the hex representation of the keyid as the filename
104  *      to store the key to. We flatten the public key to a list of OpenPGP
105  *      packets and then use write_openpgp_stream() to write the stream out to
106  *      the file.
107  */
108 static int file_store_key(struct onak_dbctx *dbctx,
109                 struct openpgp_publickey *publickey, bool intrans,
110                 bool update)
111 {
112         char *db_dir = (char *) dbctx->priv;
113         struct openpgp_packet_list *packets = NULL;
114         struct openpgp_packet_list *list_end = NULL;
115         struct openpgp_publickey *next = NULL;
116         char keyfile[1024];
117         int fd = -1;
118         uint64_t keyid;
119
120         if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
121                 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
122                 return 0;
123         }
124         snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
125                         keyid & 0xFFFFFFFF);
126         fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
127
128         if (fd > -1) {
129                 next = publickey -> next;
130                 publickey -> next = NULL;
131                 flatten_publickey(publickey, &packets, &list_end);
132                 publickey -> next = next;
133                 
134                 write_openpgp_stream(file_putchar, &fd, packets);
135                 close(fd);
136                 free_packet_list(packets);
137                 packets = NULL;
138         }
139
140         return (fd > -1);
141 }
142
143 /**
144  *      delete_key - Given a keyid delete the key from storage.
145  *      @keyid: The keyid to delete.
146  *      @intrans: If we're already in a transaction.
147  *
148  *      This function deletes a public key from whatever storage mechanism we
149  *      are using. Returns 0 if the key existed.
150  */
151 static int file_delete_key(struct onak_dbctx *dbctx,
152                 uint64_t keyid, bool intrans)
153 {
154         char *db_dir = (char *) dbctx->priv;
155         char keyfile[1024];
156
157         snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
158                         keyid & 0xFFFFFFFF);
159
160         return unlink(keyfile);
161 }
162
163 /**
164  *      fetch_key_text - Trys to find the keys that contain the supplied text.
165  *      @search: The text to search for.
166  *      @publickey: A pointer to a structure to return the key in.
167  *
168  *      This function searches for the supplied text and returns the keys that
169  *      contain it.
170  *
171  *      TODO: Write for flat file access. Some sort of grep?
172  */
173 static int file_fetch_key_text(struct onak_dbctx *dbctx,
174                 const char *search,
175                 struct openpgp_publickey **publickey)
176 {
177         return 0;
178 }
179
180 /**
181  *      iterate_keys - call a function once for each key in the db.
182  *      @iterfunc: The function to call.
183  *      @ctx: A context pointer
184  *
185  *      Calls iterfunc once for each key in the database. ctx is passed
186  *      unaltered to iterfunc. This function is intended to aid database dumps
187  *      and statistic calculations.
188  *
189  *      Returns the number of keys we iterated over.
190  */
191 static int file_iterate_keys(struct onak_dbctx *dbctx,
192                 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
193                 void *ctx)
194 {
195         char *db_dir = (char *) dbctx->priv;
196         int                         numkeys = 0;
197         struct openpgp_packet_list *packets = NULL;
198         struct openpgp_publickey   *key = NULL;
199         DIR                        *dir;
200         char                        keyfile[1024];
201         int                         fd = -1;
202         struct dirent              *curfile = NULL;
203
204         dir = opendir(db_dir);
205
206         if (dir != NULL) {
207                 while ((curfile = readdir(dir)) != NULL) {
208                         if (curfile->d_name[0] == '0' &&
209                                         curfile->d_name[1] == 'x') {
210                                 snprintf(keyfile, 1023, "%s/%s",
211                                                 db_dir,
212                                                 curfile->d_name);
213                                 fd = open(keyfile, O_RDONLY);
214
215                                 if (fd > -1) {
216                                         read_openpgp_stream(file_fetchchar,
217                                                         &fd,
218                                                         &packets,
219                                                         0);
220                                         parse_keys(packets, &key);
221
222                                         iterfunc(ctx, key);
223
224                                         free_publickey(key);
225                                         key = NULL;
226                                         free_packet_list(packets);
227                                         packets = NULL;
228                                         close(fd);
229                                 }
230                                 numkeys++;
231                         }
232                 }
233                 
234                 closedir(dir);
235                 dir = NULL;
236         }
237
238         return numkeys;
239 }
240
241 /*
242  * Include the basic keydb routines.
243  */
244 #define NEED_KEYID2UID 1
245 #define NEED_GETKEYSIGS 1
246 #define NEED_GETFULLKEYID 1
247 #define NEED_UPDATEKEYS 1
248 #define NEED_GET_FP 1
249 #include "keydb.c"
250
251 /**
252  *      cleanupdb - De-initialize the key database.
253  *
254  *      This is just a no-op for flat file access.
255  */
256 static void file_cleanupdb(struct onak_dbctx *dbctx)
257 {
258         if (dbctx->priv != NULL) {
259                 free(dbctx->priv);
260                 dbctx->priv = NULL;
261         }
262
263         if (dbctx != NULL) {
264                 free(dbctx);
265         }
266 }
267
268 /**
269  *      initdb - Initialize the key database.
270  *
271  *      This is just a no-op for flat file access.
272  */
273 struct onak_dbctx *keydb_file_init(struct onak_db_config *dbcfg, bool readonly)
274 {
275         struct onak_dbctx *dbctx;
276
277         dbctx = malloc(sizeof(struct onak_dbctx));
278         if (dbctx == NULL) {
279                 return NULL;
280         }
281
282         dbctx->config = dbcfg;
283         dbctx->priv = strdup(dbcfg->location);
284
285         dbctx->cleanupdb                = file_cleanupdb;
286         dbctx->starttrans               = file_starttrans;
287         dbctx->endtrans                 = file_endtrans;
288         dbctx->fetch_key_id             = file_fetch_key_id;
289         dbctx->fetch_key_fp             = generic_fetch_key_fp;
290         dbctx->fetch_key_text           = file_fetch_key_text;
291         dbctx->store_key                = file_store_key;
292         dbctx->update_keys              = generic_update_keys;
293         dbctx->delete_key               = file_delete_key;
294         dbctx->getkeysigs               = generic_getkeysigs;
295         dbctx->cached_getkeysigs        = generic_cached_getkeysigs;
296         dbctx->keyid2uid                = generic_keyid2uid;
297         dbctx->getfullkeyid             = generic_getfullkeyid;
298         dbctx->iterate_keys             = file_iterate_keys;
299
300         return dbctx;
301 }