]> the.earth.li Git - onak.git/blob - keydb/keydb_file.c
Add a helper for reading OpenPGP packets from a file
[onak.git] / keydb / 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, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <sys/types.h>
20 #include <sys/uio.h>
21 #include <dirent.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "charfuncs.h"
32 #include "key-store.h"
33 #include "keydb.h"
34 #include "keyid.h"
35 #include "keystructs.h"
36 #include "log.h"
37 #include "mem.h"
38 #include "onak.h"
39 #include "onak-conf.h"
40 #include "parsekey.h"
41
42 /**
43  *      starttrans - Start a transaction.
44  *
45  *      This is just a no-op for flat file access.
46  */
47 static bool file_starttrans(struct onak_dbctx *dbctx)
48 {
49         return true;
50 }
51
52 /**
53  *      endtrans - End a transaction.
54  *
55  *      This is just a no-op for flat file access.
56  */
57 static void file_endtrans(struct onak_dbctx *dbctx)
58 {
59         return;
60 }
61
62 /**
63  *      fetch_key_id - Given a keyid fetch the key from storage.
64  *      @keyid: The keyid to fetch.
65  *      @publickey: A pointer to a structure to return the key in.
66  *      @intrans: If we're already in a transaction.
67  *
68  *      We use the hex representation of the keyid as the filename to fetch the
69  *      key from. The key is stored in the file as a binary OpenPGP stream of
70  *      packets, so we can just use read_openpgp_stream() to read the packets
71  *      in and then parse_keys() to parse the packets into a publickey
72  *      structure.
73  */
74 static int file_fetch_key_id(struct onak_dbctx *dbctx,
75                 uint64_t keyid,
76                 struct openpgp_publickey **publickey,
77                 bool intrans)
78 {
79         char *db_dir = (char *) dbctx->priv;
80         struct openpgp_packet_list *packets = NULL;
81         char keyfile[1024];
82         onak_status_t res;
83
84         snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
85                         keyid & 0xFFFFFFFF);
86         res = onak_read_openpgp_file(keyfile, &packets);
87
88         if (res == ONAK_E_OK) {
89                 parse_keys(packets, publickey);
90                 free_packet_list(packets);
91                 packets = NULL;
92         }
93
94         return (res == ONAK_E_OK);
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  *      @fp: The fingerprint of the key 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                 struct openpgp_fingerprint *fp, 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                         fingerprint2keyid(fp) & 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         struct dirent              *curfile = NULL;
202         onak_status_t               res;
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                                 res = onak_read_openpgp_file(keyfile,
214                                                 &packets);
215
216                                 if (res == ONAK_E_OK) {
217                                         parse_keys(packets, &key);
218
219                                         iterfunc(ctx, key);
220
221                                         free_publickey(key);
222                                         key = NULL;
223                                         free_packet_list(packets);
224                                         packets = NULL;
225                                 }
226                                 numkeys++;
227                         }
228                 }
229                 
230                 closedir(dir);
231                 dir = NULL;
232         }
233
234         return numkeys;
235 }
236
237 /*
238  * Include the basic keydb routines.
239  */
240 #define NEED_KEYID2UID 1
241 #define NEED_GETKEYSIGS 1
242 #define NEED_UPDATEKEYS 1
243 #define NEED_GET_FP 1
244 #include "keydb.c"
245
246 /**
247  *      cleanupdb - De-initialize the key database.
248  *
249  *      This is just a no-op for flat file access.
250  */
251 static void file_cleanupdb(struct onak_dbctx *dbctx)
252 {
253         if (dbctx->priv != NULL) {
254                 free(dbctx->priv);
255                 dbctx->priv = NULL;
256         }
257
258         if (dbctx != NULL) {
259                 free(dbctx);
260         }
261 }
262
263 /**
264  *      initdb - Initialize the key database.
265  *
266  *      This is just a no-op for flat file access.
267  */
268 struct onak_dbctx *keydb_file_init(struct onak_db_config *dbcfg, bool readonly)
269 {
270         struct onak_dbctx *dbctx;
271
272         dbctx = malloc(sizeof(struct onak_dbctx));
273         if (dbctx == NULL) {
274                 return NULL;
275         }
276
277         dbctx->config = dbcfg;
278         dbctx->priv = strdup(dbcfg->location);
279
280         dbctx->cleanupdb                = file_cleanupdb;
281         dbctx->starttrans               = file_starttrans;
282         dbctx->endtrans                 = file_endtrans;
283         /* Our fetch fp doesn't look at subkeys */
284         dbctx->fetch_key                = generic_fetch_key_fp;
285         dbctx->fetch_key_fp             = generic_fetch_key_fp;
286         dbctx->fetch_key_id             = file_fetch_key_id;
287         dbctx->fetch_key_text           = file_fetch_key_text;
288         dbctx->store_key                = file_store_key;
289         dbctx->update_keys              = generic_update_keys;
290         dbctx->delete_key               = file_delete_key;
291         dbctx->getkeysigs               = generic_getkeysigs;
292         dbctx->cached_getkeysigs        = generic_cached_getkeysigs;
293         dbctx->keyid2uid                = generic_keyid2uid;
294         dbctx->iterate_keys             = file_iterate_keys;
295
296         return dbctx;
297 }