]> git.sommitrealweird.co.uk Git - onak.git/blob - keyd.c
18d09bf515fd5533c74a52e9f59e62042d45a094
[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%016llX.",
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%llX, result: %d",
168                                                 keyid,
169                                                 config.dbbackend->
170                                                 fetch_key(keyid, &key, false));
171                                 if (key != NULL) {
172                                         storebuf.size = 8192;
173                                         storebuf.buffer = malloc(8192);
174
175                                         flatten_publickey(key,
176                                                         &packets,
177                                                         &list_end);
178                                         write_openpgp_stream(buffer_putchar,
179                                                         &storebuf,
180                                                         packets);
181                                         logthing(LOGTHING_TRACE,
182                                                         "Sending %d bytes.",
183                                                         storebuf.offset);
184                                         write(fd, &storebuf.offset,
185                                                 sizeof(storebuf.offset));
186                                         write(fd, storebuf.buffer,
187                                                 storebuf.offset);
188
189                                         free(storebuf.buffer);
190                                         storebuf.buffer = NULL;
191                                         storebuf.size = storebuf.offset = 0;
192                                         free_packet_list(packets);
193                                         packets = list_end = NULL;
194                                         free_publickey(key);
195                                         key = NULL;
196                                 } else {
197                                         write(fd, &storebuf.offset,
198                                                 sizeof(storebuf.offset));
199                                 }
200                         }
201                         break;
202                 case KEYD_CMD_GETTEXT:
203                         cmd = KEYD_REPLY_OK;
204                         write(fd, &cmd, sizeof(cmd));
205                         bytes = read(fd, &count, sizeof(count));
206                         if (bytes != sizeof(count)) {
207                                 ret = 1;
208                         }
209                         storebuf.offset = 0;
210                         if (ret == 0) {
211                                 search = malloc(count+1);
212                                 read(fd, search, count);
213                                 search[count] = 0;
214                                 logthing(LOGTHING_INFO,
215                                                 "Fetching %s, result: %d",
216                                                 search,
217                                                 config.dbbackend->
218                                                 fetch_key_text(search, &key));
219                                 if (key != NULL) {
220                                         storebuf.size = 8192;
221                                         storebuf.buffer = malloc(8192);
222
223                                         flatten_publickey(key,
224                                                         &packets,
225                                                         &list_end);
226                                         write_openpgp_stream(buffer_putchar,
227                                                         &storebuf,
228                                                         packets);
229                                         logthing(LOGTHING_TRACE,
230                                                         "Sending %d bytes.",
231                                                         storebuf.offset);
232                                         write(fd, &storebuf.offset,
233                                                 sizeof(storebuf.offset));
234                                         write(fd, storebuf.buffer,
235                                                 storebuf.offset);
236
237                                         free(storebuf.buffer);
238                                         storebuf.buffer = NULL;
239                                         storebuf.size = storebuf.offset = 0;
240                                         free_packet_list(packets);
241                                         packets = list_end = NULL;
242                                         free_publickey(key);
243                                         key = NULL;
244                                 } else {
245                                         write(fd, &storebuf.offset,
246                                                 sizeof(storebuf.offset));
247                                 }
248                         }
249                         break;
250                 case KEYD_CMD_STORE:
251                         cmd = KEYD_REPLY_OK;
252                         write(fd, &cmd, sizeof(cmd));
253                         storebuf.offset = 0;
254                         bytes = read(fd, &storebuf.size,
255                                         sizeof(storebuf.size));
256                         logthing(LOGTHING_TRACE, "Reading %d bytes.",
257                                         storebuf.size);
258                         if (bytes != sizeof(storebuf.size)) {
259                                 ret = 1;
260                         }
261                         if (ret == 0 && storebuf.size > 0) {
262                                 storebuf.buffer = malloc(storebuf.size);
263                                 bytes = count = 0;
264                                 while (bytes >= 0 && count < storebuf.size) {
265                                         bytes = read(fd,
266                                                 &storebuf.buffer[count],
267                                                 storebuf.size - count);
268                                         logthing(LOGTHING_TRACE,
269                                                         "Read %d bytes.",
270                                                         bytes);
271                                         count += bytes;
272                                 }
273                                 read_openpgp_stream(buffer_fetchchar,
274                                                 &storebuf,
275                                                 &packets,
276                                                 0);
277                                 parse_keys(packets, &key);
278                                 config.dbbackend->store_key(key, false, false);
279                                 free_packet_list(packets);
280                                 packets = NULL;
281                                 free_publickey(key);
282                                 key = NULL;
283                                 free(storebuf.buffer);
284                                 storebuf.buffer = NULL;
285                                 storebuf.size = storebuf.offset = 0;
286                         }
287                         break;
288                 case KEYD_CMD_DELETE:
289                         cmd = KEYD_REPLY_OK;
290                         write(fd, &cmd, sizeof(cmd));
291                         bytes = read(fd, &keyid, sizeof(keyid));
292                         if (bytes != sizeof(keyid)) {
293                                 ret = 1;
294                         }
295                         if (ret == 0) {
296                                 logthing(LOGTHING_INFO,
297                                                 "Deleting 0x%llX, result: %d",
298                                                 keyid,
299                                                 config.dbbackend->delete_key(
300                                                         keyid, false));
301                         }
302                         break;
303                 case KEYD_CMD_GETFULLKEYID:
304                         cmd = KEYD_REPLY_OK;
305                         write(fd, &cmd, sizeof(cmd));
306                         bytes = read(fd, &keyid, sizeof(keyid));
307                         if (bytes != sizeof(keyid)) {
308                                 ret = 1;
309                         }
310                         if (ret == 0) {
311                                 keyid = config.dbbackend->getfullkeyid(keyid);
312                                 write(fd, &keyid, sizeof(keyid));
313                         }
314                         break;
315                 case KEYD_CMD_KEYITER:
316                         cmd = KEYD_REPLY_OK;
317                         write(fd, &cmd, sizeof(cmd));
318                         config.dbbackend->iterate_keys(iteratefunc,
319                                         &fd);
320                         bytes = 0;
321                         write(fd, &bytes, sizeof(bytes));
322                         break;
323                 case KEYD_CMD_CLOSE:
324                         ret = 1;
325                         break;
326                 case KEYD_CMD_QUIT:
327                         trytocleanup();
328                         break;
329                 default:
330                         logthing(LOGTHING_ERROR, "Got unknown command: %d",
331                                         cmd);
332                         cmd = KEYD_REPLY_UNKNOWN_CMD;
333                         write(fd, &cmd, sizeof(cmd));
334                 }
335         }
336
337         return(ret);
338 }
339
340 int sock_close(int fd)
341 {
342         shutdown(fd, SHUT_RDWR);
343         return close(fd);
344 }
345
346 int sock_accept(int fd)
347 {
348         struct sockaddr_un sock;
349         socklen_t          socklen;
350         int    srv = -1;
351         int    ret = -1;
352
353         socklen = sizeof(sock);
354         srv = accept(fd, (struct sockaddr *) &sock, &socklen);
355         if (srv != -1) {
356                 ret = fcntl(srv, F_SETFD, 1);
357         }
358
359         if (ret != -1) {
360                 while (!sock_do(srv)) ;
361                 sock_close(srv);
362         }
363
364         return 1;
365 }
366
367 int main(int argc, char *argv[])
368 {
369         int fd = -1;
370         fd_set rfds;
371         char sockname[1024];
372         char *configfile = NULL;
373         bool foreground = false;
374         int optchar;
375
376         while ((optchar = getopt(argc, argv, "c:f")) != -1 ) {
377                 switch (optchar) {
378                 case 'c':
379                         configfile = strdup(optarg);
380                         break;
381                 case 'f':
382                         foreground = true;
383                         break;
384                 }
385         }
386
387         readconfig(configfile);
388         free(configfile);
389         configfile = NULL;
390         initlogthing("keyd", config.logfile);
391
392         if (!foreground) {
393                 daemonize();
394         }
395
396         catchsignals();
397         
398         snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
399         fd = sock_init(sockname);
400
401         if (fd != -1) {
402                 FD_ZERO(&rfds);
403                 FD_SET(fd, &rfds);
404
405                 config.dbbackend->initdb(false);
406
407                 logthing(LOGTHING_NOTICE, "Accepting connections.");
408                 while (!cleanup() && select(fd + 1, &rfds, NULL, NULL, NULL) != -1) {
409                         logthing(LOGTHING_INFO, "Accepted connection.");
410                         sock_accept(fd);
411                         FD_SET(fd, &rfds);
412                 }
413                 config.dbbackend->cleanupdb();
414                 sock_close(fd);
415                 unlink(sockname);
416         }
417
418         cleanuplogthing();
419         cleanupconfig();
420
421         return(EXIT_SUCCESS);
422 }