]> the.earth.li Git - onak.git/blob - keydb/keyd.c
0.6.3 release
[onak.git] / keydb / 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, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <getopt.h>
22 #include <inttypes.h>
23 #include <signal.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/select.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
31 #include <sys/un.h>
32 #include <time.h>
33 #include <unistd.h>
34
35 #include "build-config.h"
36
37 #ifdef HAVE_SYSTEMD
38 #include <systemd/sd-daemon.h>
39 #endif
40
41 #include "charfuncs.h"
42 #include "cleanup.h"
43 #include "keyd.h"
44 #include "keydb.h"
45 #include "keyid.h"
46 #include "keystructs.h"
47 #include "log.h"
48 #include "mem.h"
49 #include "onak-conf.h"
50 #include "parsekey.h"
51
52 /* Maximum number of clients we're prepared to accept at once */
53 #define MAX_CLIENTS 16
54
55 static bool using_socket_activation = false;
56
57 static struct keyd_stats *stats;
58
59 static void daemonize(void)
60 {
61         pid_t pid;
62
63         pid = fork();
64
65         if (pid < 0) {
66                 logthing(LOGTHING_CRITICAL,
67                         "Failed to fork into background: %d (%s)",
68                         errno,
69                         strerror(errno));
70                 exit(EXIT_FAILURE);
71         } else if (pid > 0) {
72                 logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
73                 exit(EXIT_SUCCESS);
74         }
75
76         if (setsid() == -1) {
77                 logthing(LOGTHING_CRITICAL,
78                         "Couldn't set process group leader: %d (%s)",
79                         errno,
80                         strerror(errno));
81                 exit(EXIT_FAILURE);
82         }
83
84         if (!freopen("/dev/null", "r", stdin)) {
85                 logthing(LOGTHING_CRITICAL,
86                         "Couldn't reopen stdin to NULL: %d (%s)",
87                         errno,
88                         strerror(errno));
89                 exit(EXIT_FAILURE);
90         }
91         if (!freopen("/dev/null", "w", stdout)) {
92                 logthing(LOGTHING_CRITICAL,
93                         "Couldn't reopen stdout to NULL: %d (%s)",
94                         errno,
95                         strerror(errno));
96                 exit(EXIT_FAILURE);
97         }
98         if (!freopen("/dev/null", "w", stderr)) {
99                 logthing(LOGTHING_CRITICAL,
100                         "Couldn't reopen stderr to NULL: %d (%s)",
101                         errno,
102                         strerror(errno));
103                 exit(EXIT_FAILURE);
104         }
105
106         return;
107 }
108
109 static bool keyd_write_key(int fd, struct openpgp_publickey *key)
110 {
111         struct openpgp_packet_list *packets = NULL;
112         struct openpgp_packet_list *list_end = NULL;
113         struct buffer_ctx           storebuf;
114         ssize_t written;
115         bool    ok = true;
116
117         storebuf.offset = 0;
118         storebuf.size = 8192;
119         storebuf.buffer = malloc(8192);
120
121         flatten_publickey(key,
122                                 &packets,
123                                 &list_end);
124         write_openpgp_stream(buffer_putchar,
125                                 &storebuf,
126                                 packets);
127         logthing(LOGTHING_TRACE,
128                                 "Sending %d bytes.",
129                                 storebuf.offset);
130         written = write(fd, &storebuf.offset,
131                         sizeof(storebuf.offset));
132         if (written == 0) {
133                 ok = false;
134         } else {
135                 written = write(fd, storebuf.buffer,
136                         storebuf.offset);
137                 if (written != storebuf.offset) {
138                         ok = false;
139                 }
140         }
141
142         free(storebuf.buffer);
143         storebuf.buffer = NULL;
144         storebuf.size = storebuf.offset = 0;
145         free_packet_list(packets);
146         packets = list_end = NULL;
147
148         return (ok);
149 }
150
151 static bool keyd_write_reply(int fd, enum keyd_reply _reply)
152 {
153         uint32_t reply = _reply;
154         ssize_t written;
155         bool ok = true;
156
157         written = write(fd, &reply, sizeof(reply));
158         if (written != sizeof(reply)) {
159                 ok = false;
160         }
161
162         return (ok);
163 }
164
165 static bool keyd_write_size(int fd, size_t size)
166 {
167         ssize_t written;
168         bool ok = true;
169
170         written = write(fd, &size, sizeof(size));
171         if (written != sizeof(size)) {
172                 ok = false;
173         }
174
175         return (ok);
176 }
177
178 static void iteratefunc(void *ctx, struct openpgp_publickey *key)
179 {
180         int      *fd = (int *) ctx;
181         uint64_t  keyid;
182
183         if (key != NULL) {
184                 get_keyid(key, &keyid);
185                 logthing(LOGTHING_TRACE,
186                                 "Iterating over 0x%016" PRIX64 ".",
187                                 keyid);
188
189                 keyd_write_key(*fd, key);
190         }
191
192         return;
193 }
194
195 static int sock_init(const char *sockname)
196 {
197         struct sockaddr_un sock;
198         int                fd = -1;
199         int                ret = -1;
200 #ifdef HAVE_SYSTEMD
201         int                n;
202
203         n = sd_listen_fds(0);
204         if (n > 1) {
205                 logthing(LOGTHING_ERROR,
206                         "Too many file descriptors received from systemd.");
207         } else if (n == 1) {
208                 fd = SD_LISTEN_FDS_START + 0;
209                 if (sd_is_socket_unix(fd, SOCK_STREAM, 1, NULL, 0) <= 0) {
210                         logthing(LOGTHING_ERROR,
211                                 "systemd passed an invalid socket.");
212                         fd = -1;
213                 }
214                 using_socket_activation = true;
215         } else {
216 #endif
217                 fd = socket(PF_UNIX, SOCK_STREAM, 0);
218                 if (fd != -1) {
219                         ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
220                 }
221
222                 if (ret != -1) {
223                         sock.sun_family = AF_UNIX;
224                         strncpy(sock.sun_path, sockname,
225                                         sizeof(sock.sun_path) - 1);
226                         unlink(sockname);
227                         ret = bind(fd, (struct sockaddr *) &sock,
228                                         sizeof(sock));
229                 }
230
231                 if (ret != -1) {
232                         ret = listen(fd, 5);
233                 }
234
235                 if (ret == -1) {
236                         logthing(LOGTHING_ERROR,
237                                 "Couldn't open socket to listen on: %s (%d)",
238                                 strerror(errno),
239                                 errno);
240                         close(fd);
241                         fd = -1;
242                 }
243 #ifdef HAVE_SYSTEMD
244         }
245 #endif
246
247         return fd;
248 }
249
250 static int sock_do(struct onak_dbctx *dbctx, int fd)
251 {
252         uint32_t cmd = KEYD_CMD_UNKNOWN;
253         ssize_t  bytes = 0;
254         ssize_t  count = 0;
255         int      ret = 0;
256         uint64_t keyid = 0;
257         char     *search = NULL;
258         struct openpgp_publickey *key = NULL;
259         struct openpgp_packet_list *packets = NULL;
260         struct buffer_ctx storebuf;
261         struct skshash hash;
262         struct openpgp_fingerprint fingerprint;
263
264         /*
265          * Get the command from the client.
266          */
267         bytes = read(fd, &cmd, sizeof(cmd));
268
269         logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
270
271         if (bytes != sizeof(cmd)) {
272                 ret = 1;
273         }
274         
275         if (ret == 0) {
276                 if (cmd < KEYD_CMD_LAST) {
277                         stats->command_stats[cmd]++;
278                 } else {
279                         stats->command_stats[KEYD_CMD_UNKNOWN]++;
280                 }
281                 switch (cmd) {
282                 case KEYD_CMD_VERSION:
283                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
284                                 ret = 1;
285                         }
286                         if (ret == 0) {
287                                 cmd = sizeof(keyd_version);
288                                 bytes = write(fd, &cmd, sizeof(cmd));
289                                 if (bytes != sizeof(cmd)) {
290                                         ret = 1;
291                                 }
292                         }
293                         if (ret == 0) {
294                                 bytes = write(fd, &keyd_version,
295                                         sizeof(keyd_version));
296                                 if (bytes != sizeof(keyd_version)) {
297                                         ret = 1;
298                                 }
299                         }
300                         break;
301                 case KEYD_CMD_GET:
302                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
303                                 ret = 1;
304                         }
305                         if (ret == 0) {
306                                 if ((read(fd, &bytes, 1) != 1) ||
307                                                 (bytes > MAX_FINGERPRINT_LEN)) {
308                                         ret = 1;
309                                 } else {
310                                         fingerprint.length = bytes;
311                                         bytes = read(fd, fingerprint.fp,
312                                                 fingerprint.length);
313                                         if (bytes != fingerprint.length) {
314                                                 ret = 1;
315                                         }
316                                 }
317                         }
318                         if (ret == 0) {
319                                 logthing(LOGTHING_INFO,
320                                                 "Fetching by fingerprint"
321                                                 ", result: %d",
322                                                 dbctx->fetch_key(dbctx,
323                                                         &fingerprint,
324                                                         &key, false));
325                                 if (key != NULL) {
326                                         keyd_write_key(fd, key);
327                                         free_publickey(key);
328                                         key = NULL;
329                                 } else {
330                                         if (!keyd_write_size(fd, 0)) {
331                                                 ret = 1;
332                                         }
333                                 }
334                         }
335                         break;
336                 case KEYD_CMD_GET_ID:
337                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
338                                 ret = 1;
339                         }
340                         if (ret == 0) {
341                                 bytes = read(fd, &keyid, sizeof(keyid));
342                                 if (bytes != sizeof(keyid)) {
343                                         ret = 1;
344                                 }
345                         }
346                         if (ret == 0) {
347                                 logthing(LOGTHING_INFO,
348                                                 "Fetching 0x%" PRIX64
349                                                 ", result: %d",
350                                                 keyid,
351                                                 dbctx->fetch_key_id(dbctx,
352                                                         keyid,
353                                                         &key, false));
354                                 if (key != NULL) {
355                                         keyd_write_key(fd, key);
356                                         free_publickey(key);
357                                         key = NULL;
358                                 } else {
359                                         if (!keyd_write_size(fd, 0)) {
360                                                 ret = 1;
361                                         }
362                                 }
363                         }
364                         break;
365                 case KEYD_CMD_GET_FP:
366                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
367                                 ret = 1;
368                         }
369                         if (ret == 0) {
370                                 if ((read(fd, &bytes, 1) != 1) ||
371                                                 (bytes > MAX_FINGERPRINT_LEN)) {
372                                         ret = 1;
373                                 } else {
374                                         fingerprint.length = bytes;
375                                         bytes = read(fd, fingerprint.fp,
376                                                 fingerprint.length);
377                                         if (bytes != fingerprint.length) {
378                                                 ret = 1;
379                                         }
380                                 }
381                         }
382                         if (ret == 0) {
383                                 logthing(LOGTHING_INFO,
384                                                 "Fetching by fingerprint"
385                                                 ", result: %d",
386                                                 dbctx->fetch_key_fp(dbctx,
387                                                         &fingerprint,
388                                                         &key, false));
389                                 if (key != NULL) {
390                                         keyd_write_key(fd, key);
391                                         free_publickey(key);
392                                         key = NULL;
393                                 } else {
394                                         if (!keyd_write_size(fd, 0)) {
395                                                 ret = 1;
396                                         }
397                                 }
398                         }
399                         break;
400                 case KEYD_CMD_GET_TEXT:
401                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
402                                 ret = 1;
403                         }
404                         if (ret == 0) {
405                                 bytes = read(fd, &count, sizeof(count));
406                                 if (bytes != sizeof(count)) {
407                                         ret = 1;
408                                 }
409                         }
410                         if (ret == 0) {
411                                 search = malloc(count+1);
412                                 bytes = read(fd, search, count);
413                                 if (bytes != count) {
414                                         ret = 1;
415                                         free(search);
416                                         break;
417                                 }
418                                 search[count] = 0;
419                                 logthing(LOGTHING_INFO,
420                                                 "Fetching %s, result: %d",
421                                                 search,
422                                                 dbctx->fetch_key_text(dbctx,
423                                                         search, &key));
424                                 if (key != NULL) {
425                                         keyd_write_key(fd, key);
426                                         free_publickey(key);
427                                         key = NULL;
428                                 } else {
429                                         if (!keyd_write_size(fd, 0)) {
430                                                 ret = 1;
431                                         }
432                                 }
433                                 free(search);
434                         }
435                         break;
436                 case KEYD_CMD_STORE:
437                 case KEYD_CMD_UPDATE:
438                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
439                                 ret = 1;
440                         }
441                         if (ret == 0) {
442                                 bytes = read(fd, &storebuf.size,
443                                         sizeof(storebuf.size));
444                                 logthing(LOGTHING_TRACE, "Reading %d bytes.",
445                                         storebuf.size);
446                                 if (bytes != sizeof(storebuf.size)) {
447                                         ret = 1;
448                                 }
449                         }
450                         if (ret == 0 && storebuf.size > 0) {
451                                 storebuf.buffer = malloc(storebuf.size);
452                                 storebuf.offset = 0;
453                                 bytes = count = 0;
454                                 while (bytes >= 0 && count < storebuf.size) {
455                                         bytes = read(fd,
456                                                 &storebuf.buffer[count],
457                                                 storebuf.size - count);
458                                         logthing(LOGTHING_TRACE,
459                                                         "Read %d bytes.",
460                                                         bytes);
461                                         count += bytes;
462                                 }
463                                 read_openpgp_stream(buffer_fetchchar,
464                                                 &storebuf,
465                                                 &packets,
466                                                 0);
467                                 parse_keys(packets, &key);
468                                 dbctx->store_key(dbctx, key, false,
469                                         (cmd == KEYD_CMD_UPDATE));
470                                 free_packet_list(packets);
471                                 packets = NULL;
472                                 free_publickey(key);
473                                 key = NULL;
474                                 free(storebuf.buffer);
475                                 storebuf.buffer = NULL;
476                                 storebuf.size = storebuf.offset = 0;
477                         }
478                         break;
479                 case KEYD_CMD_DELETE:
480                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
481                                 ret = 1;
482                         }
483                         if (ret == 0) {
484                                 bytes = read(fd, &fingerprint,
485                                                 sizeof(fingerprint));
486                                 if (bytes != sizeof(fingerprint)) {
487                                         ret = 1;
488                                 }
489                         }
490                         if (ret == 0) {
491                                 logthing(LOGTHING_INFO,
492                                                 "Deleting 0x%" PRIX64
493                                                 ", result: %d",
494                                                 keyid,
495                                                 dbctx->delete_key(dbctx,
496                                                         &fingerprint, false));
497                         }
498                         break;
499                 case KEYD_CMD_KEYITER:
500                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
501                                 ret = 1;
502                         }
503                         if (ret == 0) {
504                                 dbctx->iterate_keys(dbctx, iteratefunc,
505                                         &fd);
506                                 if (!keyd_write_size(fd, 0)) {
507                                         ret = 1;
508                                 }
509                         }
510                         break;
511                 case KEYD_CMD_CLOSE:
512                         /* We're going to close the FD even if this fails */
513                         (void) keyd_write_reply(fd, KEYD_REPLY_OK);
514                         ret = 1;
515                         break;
516                 case KEYD_CMD_QUIT:
517                         /* We're going to quit even if this fails */
518                         (void) keyd_write_reply(fd, KEYD_REPLY_OK);
519                         logthing(LOGTHING_NOTICE,
520                                 "Exiting due to quit request.");
521                         ret = 1;
522                         trytocleanup();
523                         break;
524                 case KEYD_CMD_STATS:
525                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
526                                 ret = 1;
527                         }
528                         if (ret == 0) {
529                                 cmd = sizeof(*stats);
530                                 bytes = write(fd, &cmd, sizeof(cmd));
531                                 if (bytes != sizeof(cmd)) {
532                                         ret = 1;
533                                 }
534                         }
535                         if (ret == 0) {
536                                 bytes = write(fd, stats, sizeof(*stats));
537                                 if (bytes != sizeof(*stats)) {
538                                         ret = 1;
539                                 }
540                         }
541                         break;
542                 case KEYD_CMD_GET_SKSHASH:
543                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
544                                 ret = 1;
545                         }
546                         if (ret == 0) {
547                                 bytes = read(fd, hash.hash, sizeof(hash.hash));
548                                 if (bytes != sizeof(hash.hash)) {
549                                         ret = 1;
550                                 }
551                         }
552                         if (ret == 0) {
553                                 logthing(LOGTHING_INFO,
554                                                 "Fetching by hash"
555                                                 ", result: %d",
556                                                 dbctx->fetch_key_skshash(dbctx,
557                                                         &hash, &key));
558                                 if (key != NULL) {
559                                         keyd_write_key(fd, key);
560                                         free_publickey(key);
561                                         key = NULL;
562                                 } else {
563                                         if (!keyd_write_size(fd, 0)) {
564                                                 ret = 1;
565                                         }
566                                 }
567                         }
568                         break;
569
570                 default:
571                         logthing(LOGTHING_ERROR, "Got unknown command: %d",
572                                         cmd);
573                         if (!keyd_write_reply(fd, KEYD_REPLY_UNKNOWN_CMD)) {
574                                 ret = 1;
575                         }
576                 }
577         }
578
579         return(ret);
580 }
581
582 static int sock_close(int fd)
583 {
584         shutdown(fd, SHUT_RDWR);
585         return close(fd);
586 }
587
588 static int sock_accept(int fd)
589 {
590         struct sockaddr_un sock;
591         socklen_t          socklen;
592         int    srv = -1;
593         int    ret = -1;
594
595         socklen = sizeof(sock);
596         srv = accept(fd, (struct sockaddr *) &sock, &socklen);
597         if (srv != -1) {
598                 ret = fcntl(srv, F_SETFD, FD_CLOEXEC);
599         }
600
601         if (ret != -1) {
602                 stats->connects++;
603         }
604
605         return (srv);
606 }
607
608 static void usage(void)
609 {
610         puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
611                 "onak PGP keyserver.\n");
612         puts("Usage:\n");
613         puts("\tkeyd [options]\n");
614         puts("\tOptions:\n:");
615         puts("-c <file> - use <file> as the config file");
616         puts("-f        - run in the foreground");
617         puts("-h        - show this help text");
618         exit(EXIT_FAILURE);
619 }
620
621 int main(int argc, char *argv[])
622 {
623         int fd = -1, maxfd, i, clients[MAX_CLIENTS];
624         fd_set rfds = { 0 }; /* Avoid scan-build false report for FD_SET */
625         char sockname[100];
626         char *configfile = NULL;
627         bool foreground = false;
628         int optchar;
629         struct onak_dbctx *dbctx;
630
631         while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
632                 switch (optchar) {
633                 case 'c':
634                         if (configfile != NULL) {
635                                 free(configfile);
636                         }
637                         configfile = strdup(optarg);
638                         break;
639                 case 'f':
640                         foreground = true;
641                         break;
642                 case 'h':
643                 default:
644                         usage();
645                         break;
646                 }
647         }
648
649         readconfig(configfile);
650         free(configfile);
651         configfile = NULL;
652         initlogthing("keyd", config.logfile);
653         config.use_keyd = false;
654
655         if (!foreground) {
656                 daemonize();
657         }
658
659         catchsignals();
660         signal(SIGPIPE, SIG_IGN);
661
662
663         stats = calloc(1, sizeof(*stats));
664         if (!stats) {
665                 logthing(LOGTHING_ERROR,
666                         "Couldn't allocate memory for stats structure.");
667                 exit(EXIT_FAILURE);
668         }
669         stats->started = time(NULL);
670
671         snprintf(sockname, sizeof(sockname) - 1, "%s/%s",
672                         config.sock_dir, KEYD_SOCKET);
673         fd = sock_init(sockname);
674
675         if (fd != -1) {
676                 FD_ZERO(&rfds);
677                 FD_SET(fd, &rfds);
678                 maxfd = fd;
679                 memset(clients, -1, sizeof (clients));
680
681                 dbctx = config.dbinit(config.backend, false);
682
683                 logthing(LOGTHING_NOTICE, "Accepting connections%s",
684                         using_socket_activation ? " (via systemd)" : "");
685                 while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
686                         /*
687                          * Deal with existing clients first; if we're at our
688                          * connection limit then processing them might free
689                          * things up and let us accept the next client below.
690                          */
691                         for (i = 0; i < MAX_CLIENTS; i++) {
692                                 if (clients[i] != -1 &&
693                                                 FD_ISSET(clients[i], &rfds)) {
694                                         logthing(LOGTHING_DEBUG,
695                                                 "Handling connection for client %d.", i);
696                                         if (sock_do(dbctx, clients[i])) {
697                                                 sock_close(clients[i]);
698                                                 clients[i] = -1;
699                                                 logthing(LOGTHING_DEBUG,
700                                                         "Closed connection for client %d.", i);
701                                         }
702                                 }
703                         }
704                         /*
705                          * Check if we have a new incoming connection to accept.
706                          */
707                         if (FD_ISSET(fd, &rfds)) {
708                                 for (i = 0; i < MAX_CLIENTS; i++) {
709                                         if (clients[i] == -1) {
710                                                 break;
711                                         }
712                                 }
713                                 if (i < MAX_CLIENTS) {
714                                         logthing(LOGTHING_INFO,
715                                                 "Accepted connection %d.", i);
716                                         clients[i] = sock_accept(fd);
717                                 }
718                         }
719                         FD_ZERO(&rfds);
720                         FD_SET(fd, &rfds);
721                         maxfd = fd;
722                         for (i = 0; i < MAX_CLIENTS; i++) {
723                                 if (clients[i] != -1) {
724                                         FD_SET(clients[i], &rfds);
725                                         maxfd = (maxfd > clients[i]) ?
726                                                         maxfd : clients[i];
727                                 }
728                         }
729                 }
730                 dbctx->cleanupdb(dbctx);
731 #ifdef HAVE_SYSTEMD
732                 if (!using_socket_activation) {
733 #endif
734                         sock_close(fd);
735                         unlink(sockname);
736 #ifdef HAVE_SYSTEMD
737                 }
738 #endif
739         }
740
741         free(stats);
742
743         logthing(LOGTHING_NOTICE, "Exiting");
744         cleanuplogthing();
745         cleanupconfig();
746
747         return(EXIT_SUCCESS);
748 }