]> the.earth.li Git - onak.git/blob - keydb_keyd.c
Add support for displaying EC/ECDSA key types
[onak.git] / keydb_keyd.c
1 /*
2  * keydb_keyd.c - Routines to talk to keyd backend.
3  *
4  * Copyright 2002-2004,2011 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 <errno.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 #include <sys/un.h>
28 #include <unistd.h>
29
30 #include "charfuncs.h"
31 #include "keyd.h"
32 #include "keydb.h"
33 #include "keyid.h"
34 #include "keystructs.h"
35 #include "log.h"
36 #include "mem.h"
37 #include "onak-conf.h"
38 #include "parsekey.h"
39
40 /**
41  *      keyd_fd - our file descriptor for the socket connection to keyd.
42  */
43 static int keyd_fd = -1;
44
45 /**
46  *      initdb - Initialize the key database.
47  *      @readonly: If we'll only be reading the DB, not writing to it.
48  *
49  *      This function should be called before any of the other functions in
50  *      this file are called in order to allow the DB to be initialized ready
51  *      for access.
52  */
53 static void keyd_initdb(bool readonly)
54 {
55         struct sockaddr_un sock;
56         uint32_t           cmd = KEYD_CMD_UNKNOWN;
57         uint32_t           reply = KEYD_REPLY_UNKNOWN_CMD;
58         ssize_t            count;
59
60         keyd_fd = socket(PF_UNIX, SOCK_STREAM, 0);
61         if (keyd_fd < 0) {
62                 logthing(LOGTHING_CRITICAL,
63                                 "Couldn't open socket: %s (%d)",
64                                 strerror(errno),
65                                 errno);
66                 exit(EXIT_FAILURE);
67         }
68
69         sock.sun_family = AF_UNIX;
70         snprintf(sock.sun_path, sizeof(sock.sun_path) - 1, "%s/%s",
71                         config.db_dir,
72                         KEYD_SOCKET);
73         if (connect(keyd_fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
74                 logthing(LOGTHING_CRITICAL,
75                                 "Couldn't connect to socket %s: %s (%d)",
76                                 sock.sun_path,
77                                 strerror(errno),
78                                 errno);
79                 exit(EXIT_FAILURE);
80         }
81
82         cmd = KEYD_CMD_VERSION;
83         if (write(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {
84                 logthing(LOGTHING_CRITICAL,
85                                 "Couldn't write version cmd: %s (%d)",
86                                 strerror(errno),
87                                 errno);
88         } else {
89                 count = read(keyd_fd, &reply, sizeof(reply));
90                 if (count == sizeof(reply) && reply == KEYD_REPLY_OK) {
91                         count = read(keyd_fd, &reply, sizeof(reply));
92                         if (count != sizeof(reply) || reply != sizeof(reply)) {
93                                 logthing(LOGTHING_CRITICAL,
94                                         "Error! Unexpected keyd version "
95                                         "length: %d != %d",
96                                         reply, sizeof(reply));
97                                 exit(EXIT_FAILURE);
98                         }
99
100                         count = read(keyd_fd, &reply, sizeof(reply));
101                         logthing(LOGTHING_DEBUG,
102                                         "keyd protocol version %d",
103                                         reply);
104                         if (reply != keyd_version) {
105                                 logthing(LOGTHING_CRITICAL,
106                                         "Error! keyd protocol version "
107                                         "mismatch. (us = %d, it = %d)",
108                                                 keyd_version, reply);
109                         }
110                 }
111         }
112
113         return;
114 }
115
116 /**
117  *      cleanupdb - De-initialize the key database.
118  *
119  *      This function should be called upon program exit to allow the DB to
120  *      cleanup after itself.
121  */
122 static void keyd_cleanupdb(void)
123 {
124         uint32_t cmd = KEYD_CMD_CLOSE;
125
126         if (write(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {
127                 logthing(LOGTHING_CRITICAL,
128                                 "Couldn't send close cmd: %s (%d)",
129                                 strerror(errno),
130                                 errno);
131         }
132         
133         if (read(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {
134                 logthing(LOGTHING_CRITICAL,
135                         "Couldn't read close cmd reply: %s (%d)",
136                         strerror(errno),
137                         errno);
138         } else if (cmd != KEYD_REPLY_OK) {
139                 logthing(LOGTHING_CRITICAL,
140                         "Got bad reply to KEYD_CMD_CLOSE: %d", cmd);
141         }
142
143         if (shutdown(keyd_fd, SHUT_RDWR) < 0) {
144                 logthing(LOGTHING_NOTICE, "Error shutting down socket: %d",
145                                 errno);
146         }
147         if (close(keyd_fd) < 0) {
148                 logthing(LOGTHING_NOTICE, "Error closing down socket: %d",
149                                 errno);
150         }
151         keyd_fd = -1;
152
153         return;
154 }
155
156
157 /**
158  *      starttrans - Start a transaction.
159  *
160  *      Start a transaction. Intended to be used if we're about to perform many
161  *      operations on the database to help speed it all up, or if we want
162  *      something to only succeed if all relevant operations are successful.
163  */
164 static bool keyd_starttrans(void)
165 {
166         return true;
167 }
168
169 /**
170  *      endtrans - End a transaction.
171  *
172  *      Ends a transaction.
173  */
174 static void keyd_endtrans(void)
175 {
176         return;
177 }
178
179 /**
180  *      fetch_key - Given a keyid fetch the key from storage.
181  *      @keyid: The keyid to fetch.
182  *      @publickey: A pointer to a structure to return the key in.
183  *      @intrans: If we're already in a transaction.
184  *
185  *      This function returns a public key from whatever storage mechanism we
186  *      are using.
187  *
188  *      TODO: What about keyid collisions? Should we use fingerprint instead?
189  */
190 static int keyd_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
191                 bool intrans)
192 {
193         struct buffer_ctx           keybuf;
194         struct openpgp_packet_list *packets = NULL;
195         uint32_t                    cmd = KEYD_CMD_GET;
196         ssize_t                     bytes = 0;
197         ssize_t                     count = 0;
198
199         write(keyd_fd, &cmd, sizeof(cmd));
200         read(keyd_fd, &cmd, sizeof(cmd));
201         if (cmd == KEYD_REPLY_OK) {
202                 write(keyd_fd, &keyid, sizeof(keyid));
203                 keybuf.offset = 0;
204                 read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
205                 if (keybuf.size > 0) {
206                         keybuf.buffer = malloc(keybuf.size);
207                         bytes = count = 0;
208                         logthing(LOGTHING_TRACE,
209                                         "Getting %d bytes of key data.",
210                                         keybuf.size);
211                         while (bytes >= 0 && count < keybuf.size) {
212                                 bytes = read(keyd_fd, &keybuf.buffer[count],
213                                                 keybuf.size - count);
214                                 logthing(LOGTHING_TRACE,
215                                                 "Read %d bytes.", bytes);
216                                 count += bytes;
217                         }
218                         read_openpgp_stream(buffer_fetchchar, &keybuf,
219                                         &packets, 0);
220                         parse_keys(packets, publickey);
221                         free_packet_list(packets);
222                         packets = NULL;
223                         free(keybuf.buffer);
224                         keybuf.buffer = NULL;
225                         keybuf.size = 0;
226                 }
227         }
228         
229         return (count > 0) ? 1 : 0;
230 }
231
232 /**
233 *       delete_key - Given a keyid delete the key from storage.
234 *       @keyid: The keyid to delete.
235 *       @intrans: If we're already in a transaction.
236 *
237 *       This function deletes a public key from whatever storage mechanism we
238 *       are using. Returns 0 if the key existed.
239 */
240 static int keyd_delete_key(uint64_t keyid, bool intrans)
241 {
242         uint32_t cmd = KEYD_CMD_DELETE;
243
244         write(keyd_fd, &cmd, sizeof(cmd));
245         read(keyd_fd, &cmd, sizeof(cmd));
246         if (cmd == KEYD_REPLY_OK) {
247                 write(keyd_fd, &keyid, sizeof(keyid));
248         }
249
250         return 0;
251 }
252
253 /**
254  *      store_key - Takes a key and stores it.
255  *      @publickey: A pointer to the public key to store.
256  *      @intrans: If we're already in a transaction.
257  *      @update: If true the key exists and should be updated.
258  *
259  *      This function stores a public key in whatever storage mechanism we are
260  *      using. intrans indicates if we're already in a transaction so don't
261  *      need to start one. update indicates if the key already exists and is
262  *      just being updated.
263  *
264  *      TODO: Do we store multiple keys of the same id? Or only one and replace
265  *      it?
266  */
267 static int keyd_store_key(struct openpgp_publickey *publickey, bool intrans,
268                 bool update)
269 {
270         struct buffer_ctx           keybuf;
271         struct openpgp_packet_list *packets = NULL;
272         struct openpgp_packet_list *list_end = NULL;
273         struct openpgp_publickey   *next = NULL;
274         uint32_t                    cmd = KEYD_CMD_STORE;
275         uint64_t                    keyid;
276
277         if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
278                 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
279                 return 0;
280         }
281         
282         if (update) {
283                 keyd_delete_key(keyid, false);
284         }
285
286         write(keyd_fd, &cmd, sizeof(cmd));
287         read(keyd_fd, &cmd, sizeof(cmd));
288         if (cmd == KEYD_REPLY_OK) {
289                 keybuf.offset = 0;
290                 keybuf.size = 8192;
291                 keybuf.buffer = malloc(keybuf.size);
292
293                 next = publickey->next;
294                 publickey->next = NULL;
295                 flatten_publickey(publickey,
296                                 &packets,
297                                 &list_end);
298                 publickey->next = next;
299
300                 write_openpgp_stream(buffer_putchar, &keybuf, packets);
301                 logthing(LOGTHING_TRACE, "Sending %d bytes.", keybuf.offset);
302                 write(keyd_fd, &keybuf.offset, sizeof(keybuf.offset));
303                 write(keyd_fd, keybuf.buffer, keybuf.offset);
304
305                 free_packet_list(packets);
306                 packets = list_end = NULL;
307                 free(keybuf.buffer);
308                 keybuf.buffer = NULL;
309                 keybuf.size = keybuf.offset = 0;
310         }
311         
312         return 0;
313 }
314
315 /**
316  *      fetch_key_text - Trys to find the keys that contain the supplied text.
317  *      @search: The text to search for.
318  *      @publickey: A pointer to a structure to return the key in.
319  *
320  *      This function searches for the supplied text and returns the keys that
321  *      contain it.
322  */
323 static int keyd_fetch_key_text(const char *search,
324                 struct openpgp_publickey **publickey)
325 {
326         struct buffer_ctx           keybuf;
327         struct openpgp_packet_list *packets = NULL;
328         uint32_t                    cmd = KEYD_CMD_GETTEXT;
329         ssize_t                     bytes = 0;
330         ssize_t                     count = 0;
331
332         write(keyd_fd, &cmd, sizeof(cmd));
333         read(keyd_fd, &cmd, sizeof(cmd));
334         if (cmd == KEYD_REPLY_OK) {
335                 bytes = strlen(search);
336                 write(keyd_fd, &bytes, sizeof(bytes));
337                 write(keyd_fd, search, bytes);
338                 keybuf.offset = 0;
339                 read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
340                 if (keybuf.size > 0) {
341                         keybuf.buffer = malloc(keybuf.size);
342                         bytes = count = 0;
343                         logthing(LOGTHING_TRACE,
344                                         "Getting %d bytes of key data.",
345                                         keybuf.size);
346                         while (bytes >= 0 && count < keybuf.size) {
347                                 bytes = read(keyd_fd, &keybuf.buffer[count],
348                                                 keybuf.size - count);
349                                 logthing(LOGTHING_TRACE,
350                                                 "Read %d bytes.", bytes);
351                                 count += bytes;
352                         }
353                         read_openpgp_stream(buffer_fetchchar, &keybuf,
354                                         &packets, 0);
355                         parse_keys(packets, publickey);
356                         free_packet_list(packets);
357                         packets = NULL;
358                         free(keybuf.buffer);
359                         keybuf.buffer = NULL;
360                         keybuf.size = 0;
361                 }
362         }
363         
364         return (count > 0) ? 1 : 0;
365
366         return 0;
367 }
368
369 static int keyd_fetch_key_skshash(const struct skshash *hash,
370                 struct openpgp_publickey **publickey)
371 {
372         struct buffer_ctx           keybuf;
373         struct openpgp_packet_list *packets = NULL;
374         uint32_t                    cmd = KEYD_CMD_GETSKSHASH;
375         ssize_t                     bytes = 0;
376         ssize_t                     count = 0;
377
378         write(keyd_fd, &cmd, sizeof(cmd));
379         read(keyd_fd, &cmd, sizeof(cmd));
380         if (cmd == KEYD_REPLY_OK) {
381                 write(keyd_fd, hash->hash, sizeof(hash->hash));
382                 keybuf.offset = 0;
383                 read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
384                 if (keybuf.size > 0) {
385                         keybuf.buffer = malloc(keybuf.size);
386                         bytes = count = 0;
387                         logthing(LOGTHING_TRACE,
388                                         "Getting %d bytes of key data.",
389                                         keybuf.size);
390                         while (bytes >= 0 && count < keybuf.size) {
391                                 bytes = read(keyd_fd, &keybuf.buffer[count],
392                                                 keybuf.size - count);
393                                 logthing(LOGTHING_TRACE,
394                                                 "Read %d bytes.", bytes);
395                                 count += bytes;
396                         }
397                         read_openpgp_stream(buffer_fetchchar, &keybuf,
398                                         &packets, 0);
399                         parse_keys(packets, publickey);
400                         free_packet_list(packets);
401                         packets = NULL;
402                         free(keybuf.buffer);
403                         keybuf.buffer = NULL;
404                         keybuf.size = 0;
405                 }
406         }
407         
408         return (count > 0) ? 1 : 0;
409 }
410
411
412 /**
413  *      getfullkeyid - Maps a 32bit key id to a 64bit one.
414  *      @keyid: The 32bit keyid.
415  *
416  *      This function maps a 32bit key id to the full 64bit one. It returns the
417  *      full keyid. If the key isn't found a keyid of 0 is returned.
418  */
419 static uint64_t keyd_getfullkeyid(uint64_t keyid)
420 {
421         uint32_t cmd = KEYD_CMD_GETFULLKEYID;
422
423         write(keyd_fd, &cmd, sizeof(cmd));
424         read(keyd_fd, &cmd, sizeof(cmd));
425         if (cmd == KEYD_REPLY_OK) {
426                 write(keyd_fd, &keyid, sizeof(keyid));
427                 read(keyd_fd, &cmd, sizeof(cmd));
428                 if (cmd != sizeof(keyid)) {
429                         return 0;
430                 }
431                 read(keyd_fd, &keyid, sizeof(keyid));
432         }
433
434         return keyid;
435 }
436
437 /**
438  *      iterate_keys - call a function once for each key in the db.
439  *      @iterfunc: The function to call.
440  *      @ctx: A context pointer
441  *
442  *      Calls iterfunc once for each key in the database. ctx is passed
443  *      unaltered to iterfunc. This function is intended to aid database dumps
444  *      and statistic calculations.
445  *
446  *      Returns the number of keys we iterated over.
447  */
448 static int keyd_iterate_keys(void (*iterfunc)(void *ctx,
449                 struct openpgp_publickey *key), void *ctx)
450 {
451         struct buffer_ctx           keybuf;
452         struct openpgp_packet_list *packets = NULL;
453         struct openpgp_publickey   *key = NULL;
454         uint32_t                    cmd = KEYD_CMD_KEYITER;
455         ssize_t                     bytes = 0;
456         ssize_t                     count = 0;
457         int                         numkeys = 0;
458
459         write(keyd_fd, &cmd, sizeof(cmd));
460         read(keyd_fd, &cmd, sizeof(cmd));
461         if (cmd == KEYD_REPLY_OK) {
462                 keybuf.offset = 0;
463                 read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
464                 while (keybuf.size > 0) {
465                         keybuf.buffer = malloc(keybuf.size);
466                         bytes = count = 0;
467                         logthing(LOGTHING_TRACE,
468                                         "Getting %d bytes of key data.",
469                                         keybuf.size);
470                         while (bytes >= 0 && count < keybuf.size) {
471                                 bytes = read(keyd_fd, &keybuf.buffer[count],
472                                                 keybuf.size - count);
473                                 logthing(LOGTHING_TRACE,
474                                                 "Read %d bytes.", bytes);
475                                 count += bytes;
476                         }
477                         read_openpgp_stream(buffer_fetchchar, &keybuf,
478                                         &packets, 0);
479                         parse_keys(packets, &key);
480
481                         if (iterfunc != NULL && key != NULL) {
482                                 iterfunc(ctx, key);
483                         }
484
485                         free_publickey(key);
486                         key = NULL;
487                         free_packet_list(packets);
488                         packets = NULL;
489                         free(keybuf.buffer);
490                         keybuf.buffer = NULL;
491                         keybuf.size = keybuf.offset = 0;
492
493                         numkeys++;
494
495                         read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
496                 }
497         }
498         
499         return numkeys;
500 }
501
502 #define NEED_KEYID2UID 1
503 #define NEED_GETKEYSIGS 1
504 #define NEED_UPDATEKEYS 1
505 #include "keydb.c"
506
507 struct dbfuncs keydb_keyd_funcs = {
508         .initdb                 = keyd_initdb,
509         .cleanupdb              = keyd_cleanupdb,
510         .starttrans             = keyd_starttrans,
511         .endtrans               = keyd_endtrans,
512         .fetch_key              = keyd_fetch_key,
513         .fetch_key_text         = keyd_fetch_key_text,
514         .fetch_key_skshash      = keyd_fetch_key_skshash,
515         .store_key              = keyd_store_key,
516         .update_keys            = generic_update_keys,
517         .delete_key             = keyd_delete_key,
518         .getkeysigs             = generic_getkeysigs,
519         .cached_getkeysigs      = generic_cached_getkeysigs,
520         .keyid2uid              = generic_keyid2uid,
521         .getfullkeyid           = keyd_getfullkeyid,
522         .iterate_keys           = keyd_iterate_keys,
523 };