]> git.sommitrealweird.co.uk Git - onak.git/blob - keyd.c
eab3b5495b951ecbe52b023829975b9b4e61a294
[onak.git] / keyd.c
1 /*
2  * keyd.c - key retrieval daemon
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 <getopt.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/select.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/un.h>
19 #include <unistd.h>
20
21 #include "charfuncs.h"
22 #include "cleanup.h"
23 #include "keyd.h"
24 #include "keydb.h"
25 #include "keyid.h"
26 #include "keystructs.h"
27 #include "log.h"
28 #include "mem.h"
29 #include "onak-conf.h"
30 #include "parsekey.h"
31
32 void daemonize(void)
33 {
34         pid_t pid;
35
36         pid = fork();
37
38         if (pid < 0) {
39                 logthing(LOGTHING_CRITICAL,
40                         "Failed to fork into background: %d (%s)",
41                         errno,
42                         strerror(errno));
43                 exit(EXIT_FAILURE);
44         } else if (pid > 0) {
45                 logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
46                 exit(EXIT_SUCCESS);
47         }
48
49         pid = setsid();
50
51         freopen("/dev/null", "r", stdin);
52         freopen("/dev/null", "w", stdout);
53         freopen("/dev/null", "w", stderr);
54
55         return;
56 }
57
58 void iteratefunc(void *ctx, struct openpgp_publickey *key)
59 {
60         struct openpgp_packet_list *packets = NULL;
61         struct openpgp_packet_list *list_end = NULL;
62         struct buffer_ctx           storebuf;
63         int                         ret = 0;
64         int                         *fd = (int *) ctx;
65
66         if (key != NULL) {
67                 storebuf.offset = 0;
68                 storebuf.size = 8192;
69                 storebuf.buffer = malloc(8192);
70
71                 logthing(LOGTHING_TRACE,
72                                 "Iterating over 0x%016" PRIX64 ".",
73                                 get_keyid(key));
74
75                 flatten_publickey(key,
76                                 &packets,
77                                 &list_end);
78                 write_openpgp_stream(buffer_putchar,
79                                 &storebuf,
80                                 packets);
81                 logthing(LOGTHING_TRACE,
82                                 "Sending %d bytes.",
83                                 storebuf.offset);
84                 ret = write(*fd, &storebuf.offset,
85                         sizeof(storebuf.offset));
86                 if (ret != 0) {
87                         write(*fd, storebuf.buffer,
88                                 storebuf.offset);
89                 }
90
91                 free(storebuf.buffer);
92                 storebuf.buffer = NULL;
93                 storebuf.size = storebuf.offset = 0;
94                 free_packet_list(packets);
95                 packets = list_end = NULL;
96         }
97
98         return;
99 }
100
101 int sock_init(const char *sockname)
102 {
103         struct sockaddr_un sock;
104         int                fd = -1;
105         int                ret = -1;
106
107         fd = socket(PF_UNIX, SOCK_STREAM, 0);
108         if (fd != -1) {
109                 ret = fcntl(fd, F_SETFD, 1);
110         }
111
112         if (ret != -1) {
113                 sock.sun_family = AF_UNIX;
114                 strncpy(sock.sun_path, sockname, sizeof(sock.sun_path) - 1);
115                 unlink(sockname);
116                 ret = bind(fd, (struct sockaddr *) &sock, sizeof(sock));
117         }
118
119         if (ret != -1) {
120                 ret = listen(fd, 5);
121         }
122         
123         return fd;
124 }
125
126 int sock_do(int fd)
127 {
128         int      cmd = KEYD_CMD_UNKNOWN;
129         ssize_t  bytes = 0;
130         ssize_t  count = 0;
131         int      ret = 0;
132         uint64_t keyid = 0;
133         char     *search = NULL;
134         struct openpgp_publickey *key = NULL;
135         struct openpgp_packet_list *packets = NULL;
136         struct openpgp_packet_list *list_end = NULL;
137         struct buffer_ctx storebuf;
138
139         /*
140          * Get the command from the client.
141          */
142         bytes = read(fd, &cmd, sizeof(cmd));
143
144         logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
145
146         if (bytes != sizeof(cmd)) {
147                 ret = 1;
148         }
149         
150         if (ret == 0) {
151                 switch (cmd) {
152                 case KEYD_CMD_VERSION:
153                         cmd = KEYD_REPLY_OK;
154                         write(fd, &cmd, sizeof(cmd));
155                         write(fd, &keyd_version, sizeof(keyd_version));
156                         break;
157                 case KEYD_CMD_GET:
158                         cmd = KEYD_REPLY_OK;
159                         write(fd, &cmd, sizeof(cmd));
160                         bytes = read(fd, &keyid, sizeof(keyid));
161                         if (bytes != sizeof(keyid)) {
162                                 ret = 1;
163                         }
164                         storebuf.offset = 0;
165                         if (ret == 0) {
166                                 logthing(LOGTHING_INFO,
167                                                 "Fetching 0x%" PRIX64
168                                                 ", result: %d",
169                                                 keyid,
170                                                 config.dbbackend->
171                                                 fetch_key(keyid, &key, false));
172                                 if (key != NULL) {
173                                         storebuf.size = 8192;
174                                         storebuf.buffer = malloc(8192);
175
176                                         flatten_publickey(key,
177                                                         &packets,
178                                                         &list_end);
179                                         write_openpgp_stream(buffer_putchar,
180                                                         &storebuf,
181                                                         packets);
182                                         logthing(LOGTHING_TRACE,
183                                                         "Sending %d bytes.",
184                                                         storebuf.offset);
185                                         write(fd, &storebuf.offset,
186                                                 sizeof(storebuf.offset));
187                                         write(fd, storebuf.buffer,
188                                                 storebuf.offset);
189
190                                         free(storebuf.buffer);
191                                         storebuf.buffer = NULL;
192                                         storebuf.size = storebuf.offset = 0;
193                                         free_packet_list(packets);
194                                         packets = list_end = NULL;
195                                         free_publickey(key);
196                                         key = NULL;
197                                 } else {
198                                         write(fd, &storebuf.offset,
199                                                 sizeof(storebuf.offset));
200                                 }
201                         }
202                         break;
203                 case KEYD_CMD_GETTEXT:
204                         cmd = KEYD_REPLY_OK;
205                         write(fd, &cmd, sizeof(cmd));
206                         bytes = read(fd, &count, sizeof(count));
207                         if (bytes != sizeof(count)) {
208                                 ret = 1;
209                         }
210                         storebuf.offset = 0;
211                         if (ret == 0) {
212                                 search = malloc(count+1);
213                                 read(fd, search, count);
214                                 search[count] = 0;
215                                 logthing(LOGTHING_INFO,
216                                                 "Fetching %s, result: %d",
217                                                 search,
218                                                 config.dbbackend->
219                                                 fetch_key_text(search, &key));
220                                 if (key != NULL) {
221                                         storebuf.size = 8192;
222                                         storebuf.buffer = malloc(8192);
223
224                                         flatten_publickey(key,
225                                                         &packets,
226                                                         &list_end);
227                                         write_openpgp_stream(buffer_putchar,
228                                                         &storebuf,
229                                                         packets);
230                                         logthing(LOGTHING_TRACE,
231                                                         "Sending %d bytes.",
232                                                         storebuf.offset);
233                                         write(fd, &storebuf.offset,
234                                                 sizeof(storebuf.offset));
235                                         write(fd, storebuf.buffer,
236                                                 storebuf.offset);
237
238                                         free(storebuf.buffer);
239                                         storebuf.buffer = NULL;
240                                         storebuf.size = storebuf.offset = 0;
241                                         free_packet_list(packets);
242                                         packets = list_end = NULL;
243                                         free_publickey(key);
244                                         key = NULL;
245                                 } else {
246                                         write(fd, &storebuf.offset,
247                                                 sizeof(storebuf.offset));
248                                 }
249                         }
250                         break;
251                 case KEYD_CMD_STORE:
252                         cmd = KEYD_REPLY_OK;
253                         write(fd, &cmd, sizeof(cmd));
254                         storebuf.offset = 0;
255                         bytes = read(fd, &storebuf.size,
256                                         sizeof(storebuf.size));
257                         logthing(LOGTHING_TRACE, "Reading %d bytes.",
258                                         storebuf.size);
259                         if (bytes != sizeof(storebuf.size)) {
260                                 ret = 1;
261                         }
262                         if (ret == 0 && storebuf.size > 0) {
263                                 storebuf.buffer = malloc(storebuf.size);
264                                 bytes = count = 0;
265                                 while (bytes >= 0 && count < storebuf.size) {
266                                         bytes = read(fd,
267                                                 &storebuf.buffer[count],
268                                                 storebuf.size - count);
269                                         logthing(LOGTHING_TRACE,
270                                                         "Read %d bytes.",
271                                                         bytes);
272                                         count += bytes;
273                                 }
274                                 read_openpgp_stream(buffer_fetchchar,
275                                                 &storebuf,
276                                                 &packets,
277                                                 0);
278                                 parse_keys(packets, &key);
279                                 config.dbbackend->store_key(key, false, false);
280                                 free_packet_list(packets);
281                                 packets = NULL;
282                                 free_publickey(key);
283                                 key = NULL;
284                                 free(storebuf.buffer);
285                                 storebuf.buffer = NULL;
286                                 storebuf.size = storebuf.offset = 0;
287                         }
288                         break;
289                 case KEYD_CMD_DELETE:
290                         cmd = KEYD_REPLY_OK;
291                         write(fd, &cmd, sizeof(cmd));
292                         bytes = read(fd, &keyid, sizeof(keyid));
293                         if (bytes != sizeof(keyid)) {
294                                 ret = 1;
295                         }
296                         if (ret == 0) {
297                                 logthing(LOGTHING_INFO,
298                                                 "Deleting 0x%" PRIX64
299                                                 ", result: %d",
300                                                 keyid,
301                                                 config.dbbackend->delete_key(
302                                                         keyid, false));
303                         }
304                         break;
305                 case KEYD_CMD_GETFULLKEYID:
306                         cmd = KEYD_REPLY_OK;
307                         write(fd, &cmd, sizeof(cmd));
308                         bytes = read(fd, &keyid, sizeof(keyid));
309                         if (bytes != sizeof(keyid)) {
310                                 ret = 1;
311                         }
312                         if (ret == 0) {
313                                 keyid = config.dbbackend->getfullkeyid(keyid);
314                                 write(fd, &keyid, sizeof(keyid));
315                         }
316                         break;
317                 case KEYD_CMD_KEYITER:
318                         cmd = KEYD_REPLY_OK;
319                         write(fd, &cmd, sizeof(cmd));
320                         config.dbbackend->iterate_keys(iteratefunc,
321                                         &fd);
322                         bytes = 0;
323                         write(fd, &bytes, sizeof(bytes));
324                         break;
325                 case KEYD_CMD_CLOSE:
326                         ret = 1;
327                         break;
328                 case KEYD_CMD_QUIT:
329                         trytocleanup();
330                         break;
331                 default:
332                         logthing(LOGTHING_ERROR, "Got unknown command: %d",
333                                         cmd);
334                         cmd = KEYD_REPLY_UNKNOWN_CMD;
335                         write(fd, &cmd, sizeof(cmd));
336                 }
337         }
338
339         return(ret);
340 }
341
342 int sock_close(int fd)
343 {
344         shutdown(fd, SHUT_RDWR);
345         return close(fd);
346 }
347
348 int sock_accept(int fd)
349 {
350         struct sockaddr_un sock;
351         socklen_t          socklen;
352         int    srv = -1;
353         int    ret = -1;
354
355         socklen = sizeof(sock);
356         srv = accept(fd, (struct sockaddr *) &sock, &socklen);
357         if (srv != -1) {
358                 ret = fcntl(srv, F_SETFD, 1);
359         }
360
361         if (ret != -1) {
362                 while (!sock_do(srv)) ;
363                 sock_close(srv);
364         }
365
366         return 1;
367 }
368
369 int main(int argc, char *argv[])
370 {
371         int fd = -1;
372         fd_set rfds;
373         char sockname[1024];
374         char *configfile = NULL;
375         bool foreground = false;
376         int optchar;
377
378         while ((optchar = getopt(argc, argv, "c:f")) != -1 ) {
379                 switch (optchar) {
380                 case 'c':
381                         configfile = strdup(optarg);
382                         break;
383                 case 'f':
384                         foreground = true;
385                         break;
386                 }
387         }
388
389         readconfig(configfile);
390         free(configfile);
391         configfile = NULL;
392         initlogthing("keyd", config.logfile);
393         config.use_keyd = false;
394
395         if (!foreground) {
396                 daemonize();
397         }
398
399         catchsignals();
400         
401         snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
402         fd = sock_init(sockname);
403
404         if (fd != -1) {
405                 FD_ZERO(&rfds);
406                 FD_SET(fd, &rfds);
407
408                 config.dbbackend->initdb(false);
409
410                 logthing(LOGTHING_NOTICE, "Accepting connections.");
411                 while (!cleanup() && select(fd + 1, &rfds, NULL, NULL, NULL) != -1) {
412                         logthing(LOGTHING_INFO, "Accepted connection.");
413                         sock_accept(fd);
414                         FD_SET(fd, &rfds);
415                 }
416                 config.dbbackend->cleanupdb();
417                 sock_close(fd);
418                 unlink(sockname);
419         }
420
421         cleanuplogthing();
422         cleanupconfig();
423
424         return(EXIT_SUCCESS);
425 }