]> the.earth.li Git - onak.git/blob - keyd.c
Switch keyd to allow multiple clients to be served at once
[onak.git] / keyd.c
1 /*
2  * keyd.c - key retrieval daemon
3  *
4  * Copyright 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 <getopt.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/select.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
30 #include <sys/un.h>
31 #include <time.h>
32 #include <unistd.h>
33
34 #include "charfuncs.h"
35 #include "cleanup.h"
36 #include "keyd.h"
37 #include "keydb.h"
38 #include "keyid.h"
39 #include "keystructs.h"
40 #include "log.h"
41 #include "mem.h"
42 #include "onak-conf.h"
43 #include "parsekey.h"
44 #include "version.h"
45
46 /* Maximum number of clients we're prepared to accept at once */
47 #define MAX_CLIENTS 16
48
49 static struct keyd_stats *stats;
50
51 void daemonize(void)
52 {
53         pid_t pid;
54
55         pid = fork();
56
57         if (pid < 0) {
58                 logthing(LOGTHING_CRITICAL,
59                         "Failed to fork into background: %d (%s)",
60                         errno,
61                         strerror(errno));
62                 exit(EXIT_FAILURE);
63         } else if (pid > 0) {
64                 logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
65                 exit(EXIT_SUCCESS);
66         }
67
68         pid = setsid();
69
70         freopen("/dev/null", "r", stdin);
71         freopen("/dev/null", "w", stdout);
72         freopen("/dev/null", "w", stderr);
73
74         return;
75 }
76
77 void iteratefunc(void *ctx, struct openpgp_publickey *key)
78 {
79         struct openpgp_packet_list *packets = NULL;
80         struct openpgp_packet_list *list_end = NULL;
81         struct buffer_ctx           storebuf;
82         int                         ret = 0;
83         int                         *fd = (int *) ctx;
84         uint64_t                    keyid;
85
86         if (key != NULL) {
87                 storebuf.offset = 0;
88                 storebuf.size = 8192;
89                 storebuf.buffer = malloc(8192);
90
91                 get_keyid(key, &keyid);
92                 logthing(LOGTHING_TRACE,
93                                 "Iterating over 0x%016" PRIX64 ".",
94                                 keyid);
95
96                 flatten_publickey(key,
97                                 &packets,
98                                 &list_end);
99                 write_openpgp_stream(buffer_putchar,
100                                 &storebuf,
101                                 packets);
102                 logthing(LOGTHING_TRACE,
103                                 "Sending %d bytes.",
104                                 storebuf.offset);
105                 ret = write(*fd, &storebuf.offset,
106                         sizeof(storebuf.offset));
107                 if (ret != 0) {
108                         write(*fd, storebuf.buffer,
109                                 storebuf.offset);
110                 }
111
112                 free(storebuf.buffer);
113                 storebuf.buffer = NULL;
114                 storebuf.size = storebuf.offset = 0;
115                 free_packet_list(packets);
116                 packets = list_end = NULL;
117         }
118
119         return;
120 }
121
122 int sock_init(const char *sockname)
123 {
124         struct sockaddr_un sock;
125         int                fd = -1;
126         int                ret = -1;
127
128         fd = socket(PF_UNIX, SOCK_STREAM, 0);
129         if (fd != -1) {
130                 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
131         }
132
133         if (ret != -1) {
134                 sock.sun_family = AF_UNIX;
135                 strncpy(sock.sun_path, sockname, sizeof(sock.sun_path) - 1);
136                 unlink(sockname);
137                 ret = bind(fd, (struct sockaddr *) &sock, sizeof(sock));
138         }
139
140         if (ret != -1) {
141                 ret = listen(fd, 5);
142         }
143         
144         return fd;
145 }
146
147 int sock_do(int fd)
148 {
149         uint32_t cmd = KEYD_CMD_UNKNOWN;
150         ssize_t  bytes = 0;
151         ssize_t  count = 0;
152         int      ret = 0;
153         uint64_t keyid = 0;
154         char     *search = NULL;
155         struct openpgp_publickey *key = NULL;
156         struct openpgp_packet_list *packets = NULL;
157         struct openpgp_packet_list *list_end = NULL;
158         struct buffer_ctx storebuf;
159         struct skshash hash;
160
161         /*
162          * Get the command from the client.
163          */
164         bytes = read(fd, &cmd, sizeof(cmd));
165
166         logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
167
168         if (bytes != sizeof(cmd)) {
169                 ret = 1;
170         }
171         
172         if (ret == 0) {
173                 if (cmd < KEYD_CMD_LAST) {
174                         stats->command_stats[cmd]++;
175                 } else {
176                         stats->command_stats[KEYD_CMD_UNKNOWN]++;
177                 }
178                 switch (cmd) {
179                 case KEYD_CMD_VERSION:
180                         cmd = KEYD_REPLY_OK;
181                         write(fd, &cmd, sizeof(cmd));
182                         cmd = sizeof(keyd_version);
183                         write(fd, &cmd, sizeof(cmd));
184                         write(fd, &keyd_version, sizeof(keyd_version));
185                         break;
186                 case KEYD_CMD_GET:
187                         cmd = KEYD_REPLY_OK;
188                         write(fd, &cmd, sizeof(cmd));
189                         bytes = read(fd, &keyid, sizeof(keyid));
190                         if (bytes != sizeof(keyid)) {
191                                 ret = 1;
192                         }
193                         storebuf.offset = 0;
194                         if (ret == 0) {
195                                 logthing(LOGTHING_INFO,
196                                                 "Fetching 0x%" PRIX64
197                                                 ", result: %d",
198                                                 keyid,
199                                                 config.dbbackend->
200                                                 fetch_key(keyid, &key, false));
201                                 if (key != NULL) {
202                                         storebuf.size = 8192;
203                                         storebuf.buffer = malloc(8192);
204
205                                         flatten_publickey(key,
206                                                         &packets,
207                                                         &list_end);
208                                         write_openpgp_stream(buffer_putchar,
209                                                         &storebuf,
210                                                         packets);
211                                         logthing(LOGTHING_TRACE,
212                                                         "Sending %d bytes.",
213                                                         storebuf.offset);
214                                         write(fd, &storebuf.offset,
215                                                 sizeof(storebuf.offset));
216                                         write(fd, storebuf.buffer,
217                                                 storebuf.offset);
218
219                                         free(storebuf.buffer);
220                                         storebuf.buffer = NULL;
221                                         storebuf.size = storebuf.offset = 0;
222                                         free_packet_list(packets);
223                                         packets = list_end = NULL;
224                                         free_publickey(key);
225                                         key = NULL;
226                                 } else {
227                                         write(fd, &storebuf.offset,
228                                                 sizeof(storebuf.offset));
229                                 }
230                         }
231                         break;
232                 case KEYD_CMD_GETTEXT:
233                         cmd = KEYD_REPLY_OK;
234                         write(fd, &cmd, sizeof(cmd));
235                         bytes = read(fd, &count, sizeof(count));
236                         if (bytes != sizeof(count)) {
237                                 ret = 1;
238                         }
239                         storebuf.offset = 0;
240                         if (ret == 0) {
241                                 search = malloc(count+1);
242                                 read(fd, search, count);
243                                 search[count] = 0;
244                                 logthing(LOGTHING_INFO,
245                                                 "Fetching %s, result: %d",
246                                                 search,
247                                                 config.dbbackend->
248                                                 fetch_key_text(search, &key));
249                                 if (key != NULL) {
250                                         storebuf.size = 8192;
251                                         storebuf.buffer = malloc(8192);
252
253                                         flatten_publickey(key,
254                                                         &packets,
255                                                         &list_end);
256                                         write_openpgp_stream(buffer_putchar,
257                                                         &storebuf,
258                                                         packets);
259                                         logthing(LOGTHING_TRACE,
260                                                         "Sending %d bytes.",
261                                                         storebuf.offset);
262                                         write(fd, &storebuf.offset,
263                                                 sizeof(storebuf.offset));
264                                         write(fd, storebuf.buffer,
265                                                 storebuf.offset);
266
267                                         free(storebuf.buffer);
268                                         storebuf.buffer = NULL;
269                                         storebuf.size = storebuf.offset = 0;
270                                         free_packet_list(packets);
271                                         packets = list_end = NULL;
272                                         free_publickey(key);
273                                         key = NULL;
274                                 } else {
275                                         write(fd, &storebuf.offset,
276                                                 sizeof(storebuf.offset));
277                                 }
278                         }
279                         break;
280                 case KEYD_CMD_STORE:
281                         cmd = KEYD_REPLY_OK;
282                         write(fd, &cmd, sizeof(cmd));
283                         storebuf.offset = 0;
284                         bytes = read(fd, &storebuf.size,
285                                         sizeof(storebuf.size));
286                         logthing(LOGTHING_TRACE, "Reading %d bytes.",
287                                         storebuf.size);
288                         if (bytes != sizeof(storebuf.size)) {
289                                 ret = 1;
290                         }
291                         if (ret == 0 && storebuf.size > 0) {
292                                 storebuf.buffer = malloc(storebuf.size);
293                                 bytes = count = 0;
294                                 while (bytes >= 0 && count < storebuf.size) {
295                                         bytes = read(fd,
296                                                 &storebuf.buffer[count],
297                                                 storebuf.size - count);
298                                         logthing(LOGTHING_TRACE,
299                                                         "Read %d bytes.",
300                                                         bytes);
301                                         count += bytes;
302                                 }
303                                 read_openpgp_stream(buffer_fetchchar,
304                                                 &storebuf,
305                                                 &packets,
306                                                 0);
307                                 parse_keys(packets, &key);
308                                 config.dbbackend->store_key(key, false, false);
309                                 free_packet_list(packets);
310                                 packets = NULL;
311                                 free_publickey(key);
312                                 key = NULL;
313                                 free(storebuf.buffer);
314                                 storebuf.buffer = NULL;
315                                 storebuf.size = storebuf.offset = 0;
316                         }
317                         break;
318                 case KEYD_CMD_DELETE:
319                         cmd = KEYD_REPLY_OK;
320                         write(fd, &cmd, sizeof(cmd));
321                         bytes = read(fd, &keyid, sizeof(keyid));
322                         if (bytes != sizeof(keyid)) {
323                                 ret = 1;
324                         }
325                         if (ret == 0) {
326                                 logthing(LOGTHING_INFO,
327                                                 "Deleting 0x%" PRIX64
328                                                 ", result: %d",
329                                                 keyid,
330                                                 config.dbbackend->delete_key(
331                                                         keyid, false));
332                         }
333                         break;
334                 case KEYD_CMD_GETFULLKEYID:
335                         cmd = KEYD_REPLY_OK;
336                         write(fd, &cmd, sizeof(cmd));
337                         bytes = read(fd, &keyid, sizeof(keyid));
338                         if (bytes != sizeof(keyid)) {
339                                 ret = 1;
340                         }
341                         if (ret == 0) {
342                                 keyid = config.dbbackend->getfullkeyid(keyid);
343                                 cmd = sizeof(keyid);
344                                 write(fd, &cmd, sizeof(cmd));
345                                 write(fd, &keyid, sizeof(keyid));
346                         }
347                         break;
348                 case KEYD_CMD_KEYITER:
349                         cmd = KEYD_REPLY_OK;
350                         write(fd, &cmd, sizeof(cmd));
351                         config.dbbackend->iterate_keys(iteratefunc,
352                                         &fd);
353                         bytes = 0;
354                         write(fd, &bytes, sizeof(bytes));
355                         break;
356                 case KEYD_CMD_CLOSE:
357                         cmd = KEYD_REPLY_OK;
358                         write(fd, &cmd, sizeof(cmd));
359                         ret = 1;
360                         break;
361                 case KEYD_CMD_QUIT:
362                         cmd = KEYD_REPLY_OK;
363                         write(fd, &cmd, sizeof(cmd));
364                         logthing(LOGTHING_NOTICE,
365                                 "Exiting due to quit request.");
366                         ret = 1;
367                         trytocleanup();
368                         break;
369                 case KEYD_CMD_STATS:
370                         cmd = KEYD_REPLY_OK;
371                         write(fd, &cmd, sizeof(cmd));
372                         cmd = sizeof(*stats);
373                         write(fd, &cmd, sizeof(cmd));
374                         write(fd, stats,
375                                 sizeof(*stats));
376                         break;
377                 case KEYD_CMD_GETSKSHASH:
378                         cmd = KEYD_REPLY_OK;
379                         write(fd, &cmd, sizeof(cmd));
380                         bytes = read(fd, hash.hash, sizeof(hash.hash));
381                         if (bytes != sizeof(hash.hash)) {
382                                 ret = 1;
383                         }
384                         storebuf.offset = 0;
385                         if (ret == 0) {
386                                 logthing(LOGTHING_INFO,
387                                                 "Fetching by hash"
388                                                 ", result: %d",
389                                                 config.dbbackend->
390                                                 fetch_key_skshash(&hash,
391                                                         &key));
392                                 if (key != NULL) {
393                                         storebuf.size = 8192;
394                                         storebuf.buffer = malloc(8192);
395
396                                         flatten_publickey(key,
397                                                         &packets,
398                                                         &list_end);
399                                         write_openpgp_stream(buffer_putchar,
400                                                         &storebuf,
401                                                         packets);
402                                         logthing(LOGTHING_TRACE,
403                                                         "Sending %d bytes.",
404                                                         storebuf.offset);
405                                         write(fd, &storebuf.offset,
406                                                 sizeof(storebuf.offset));
407                                         write(fd, storebuf.buffer,
408                                                 storebuf.offset);
409
410                                         free(storebuf.buffer);
411                                         storebuf.buffer = NULL;
412                                         storebuf.size = storebuf.offset = 0;
413                                         free_packet_list(packets);
414                                         packets = list_end = NULL;
415                                         free_publickey(key);
416                                         key = NULL;
417                                 } else {
418                                         write(fd, &storebuf.offset,
419                                                 sizeof(storebuf.offset));
420                                 }
421                         }
422                         break;
423
424                 default:
425                         logthing(LOGTHING_ERROR, "Got unknown command: %d",
426                                         cmd);
427                         cmd = KEYD_REPLY_UNKNOWN_CMD;
428                         write(fd, &cmd, sizeof(cmd));
429                 }
430         }
431
432         return(ret);
433 }
434
435 int sock_close(int fd)
436 {
437         shutdown(fd, SHUT_RDWR);
438         return close(fd);
439 }
440
441 int sock_accept(int fd)
442 {
443         struct sockaddr_un sock;
444         socklen_t          socklen;
445         int    srv = -1;
446         int    ret = -1;
447
448         socklen = sizeof(sock);
449         srv = accept(fd, (struct sockaddr *) &sock, &socklen);
450         if (srv != -1) {
451                 ret = fcntl(srv, F_SETFD, FD_CLOEXEC);
452         }
453
454         if (ret != -1) {
455                 stats->connects++;
456         }
457
458         return (srv);
459 }
460
461 static void usage(void)
462 {
463         puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
464                 "onak PGP keyserver.\n");
465         puts("Usage:\n");
466         puts("\tkeyd [options]\n");
467         puts("\tOptions:\n:");
468         puts("-c <file> - use <file> as the config file");
469         puts("-f        - run in the foreground");
470         puts("-h        - show this help text");
471         exit(EXIT_FAILURE);
472 }
473
474 int main(int argc, char *argv[])
475 {
476         int fd = -1, maxfd, i, clients[MAX_CLIENTS];
477         fd_set rfds;
478         char sockname[1024];
479         char *configfile = NULL;
480         bool foreground = false;
481         int optchar;
482
483         while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
484                 switch (optchar) {
485                 case 'c':
486                         configfile = strdup(optarg);
487                         break;
488                 case 'f':
489                         foreground = true;
490                         break;
491                 case 'h':
492                 default:
493                         usage();
494                         break;
495                 }
496         }
497
498         readconfig(configfile);
499         free(configfile);
500         configfile = NULL;
501         initlogthing("keyd", config.logfile);
502         config.use_keyd = false;
503
504         if (!foreground) {
505                 daemonize();
506         }
507
508         catchsignals();
509         signal(SIGPIPE, SIG_IGN);
510
511
512         stats = calloc(1, sizeof(*stats));
513         if (!stats) {
514                 logthing(LOGTHING_ERROR,
515                         "Couldn't allocate memory for stats structure.");
516                 exit(EXIT_FAILURE);
517         }
518         stats->started = time(NULL);
519
520         snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
521         fd = sock_init(sockname);
522
523         if (fd != -1) {
524                 FD_ZERO(&rfds);
525                 FD_SET(fd, &rfds);
526                 maxfd = fd;
527                 memset(clients, -1, sizeof (clients));
528
529                 config.dbbackend->initdb(false);
530
531                 logthing(LOGTHING_NOTICE, "Accepting connections.");
532                 while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
533                         /*
534                          * Deal with existing clients first; if we're at our
535                          * connection limit then processing them might free
536                          * things up and let us accept the next client below.
537                          */
538                         for (i = 0; i < MAX_CLIENTS; i++) {
539                                 if (clients[i] != -1 &&
540                                                 FD_ISSET(clients[i], &rfds)) {
541                                         logthing(LOGTHING_DEBUG,
542                                                 "Handling connection for client %d.", i);
543                                         if (sock_do(clients[i])) {
544                                                 sock_close(clients[i]);
545                                                 clients[i] = -1;
546                                                 logthing(LOGTHING_DEBUG,
547                                                         "Closed connection for client %d.", i);
548                                         }
549                                 }
550                         }
551                         /*
552                          * Check if we have a new incoming connection to accept.
553                          */
554                         if (FD_ISSET(fd, &rfds)) {
555                                 for (i = 0; i < MAX_CLIENTS; i++) {
556                                         if (clients[i] == -1) {
557                                                 break;
558                                         }
559                                 }
560                                 if (i < MAX_CLIENTS) {
561                                         logthing(LOGTHING_INFO,
562                                                 "Accepted connection %d.", i);
563                                         clients[i] = sock_accept(fd);
564                                 }
565                         }
566                         FD_ZERO(&rfds);
567                         FD_SET(fd, &rfds);
568                         maxfd = fd;
569                         for (i = 0; i < MAX_CLIENTS; i++) {
570                                 if (clients[i] != -1) {
571                                         FD_SET(clients[i], &rfds);
572                                         maxfd = (maxfd > clients[i]) ?
573                                                         maxfd : clients[i];
574                                 }
575                         }
576                 }
577                 config.dbbackend->cleanupdb();
578                 sock_close(fd);
579                 unlink(sockname);
580         }
581
582         free(stats);
583
584         cleanuplogthing();
585         cleanupconfig();
586
587         return(EXIT_SUCCESS);
588 }