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