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