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