]> the.earth.li Git - onak.git/blob - keyd.c
Fix compilation breakage introduced in last commit
[onak.git] / keyd.c
1 /*
2  * keyd.c - key retrieval daemon
3  *
4  * Copyright 2004,2011 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/select.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
30 #include <sys/un.h>
31 #include <time.h>
32 #include <unistd.h>
33
34 #include "config.h"
35
36 #ifdef HAVE_SYSTEMD
37 #include <systemd/sd-daemon.h>
38 #endif
39
40 #include "charfuncs.h"
41 #include "cleanup.h"
42 #include "keyd.h"
43 #include "keydb.h"
44 #include "keyid.h"
45 #include "keystructs.h"
46 #include "log.h"
47 #include "mem.h"
48 #include "onak-conf.h"
49 #include "parsekey.h"
50 #include "version.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                         if (ret == -1) {
236                                 close(fd);
237                                 fd = -1;
238                         }
239                 }
240 #ifdef HAVE_SYSTEMD
241         }
242 #endif
243
244         return fd;
245 }
246
247 static int sock_do(struct onak_dbctx *dbctx, int fd)
248 {
249         uint32_t cmd = KEYD_CMD_UNKNOWN;
250         ssize_t  bytes = 0;
251         ssize_t  count = 0;
252         int      ret = 0;
253         uint64_t keyid = 0;
254         char     *search = NULL;
255         struct openpgp_publickey *key = NULL;
256         struct openpgp_packet_list *packets = NULL;
257         struct buffer_ctx storebuf;
258         struct skshash hash;
259         struct openpgp_fingerprint fingerprint;
260
261         /*
262          * Get the command from the client.
263          */
264         bytes = read(fd, &cmd, sizeof(cmd));
265
266         logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
267
268         if (bytes != sizeof(cmd)) {
269                 ret = 1;
270         }
271         
272         if (ret == 0) {
273                 if (cmd < KEYD_CMD_LAST) {
274                         stats->command_stats[cmd]++;
275                 } else {
276                         stats->command_stats[KEYD_CMD_UNKNOWN]++;
277                 }
278                 switch (cmd) {
279                 case KEYD_CMD_VERSION:
280                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
281                                 ret = 1;
282                         }
283                         if (ret == 0) {
284                                 cmd = sizeof(keyd_version);
285                                 bytes = write(fd, &cmd, sizeof(cmd));
286                                 if (bytes != sizeof(cmd)) {
287                                         ret = 1;
288                                 }
289                         }
290                         if (ret == 0) {
291                                 bytes = write(fd, &keyd_version,
292                                         sizeof(keyd_version));
293                                 if (bytes != sizeof(keyd_version)) {
294                                         ret = 1;
295                                 }
296                         }
297                         break;
298                 case KEYD_CMD_GET_ID:
299                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
300                                 ret = 1;
301                         }
302                         if (ret == 0) {
303                                 bytes = read(fd, &keyid, sizeof(keyid));
304                                 if (bytes != sizeof(keyid)) {
305                                         ret = 1;
306                                 }
307                         }
308                         if (ret == 0) {
309                                 logthing(LOGTHING_INFO,
310                                                 "Fetching 0x%" PRIX64
311                                                 ", result: %d",
312                                                 keyid,
313                                                 dbctx->fetch_key_id(dbctx,
314                                                         keyid,
315                                                         &key, false));
316                                 if (key != NULL) {
317                                         keyd_write_key(fd, key);
318                                         free_publickey(key);
319                                         key = NULL;
320                                 } else {
321                                         if (!keyd_write_size(fd, 0)) {
322                                                 ret = 1;
323                                         }
324                                 }
325                         }
326                         break;
327                 case KEYD_CMD_GET_FP:
328                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
329                                 ret = 1;
330                         }
331                         if (ret == 0) {
332                                 if ((read(fd, &bytes, 1) != 1) ||
333                                                 (bytes > MAX_FINGERPRINT_LEN)) {
334                                         ret = 1;
335                                 } else {
336                                         fingerprint.length = bytes;
337                                         bytes = read(fd, fingerprint.fp,
338                                                 fingerprint.length);
339                                         if (bytes != fingerprint.length) {
340                                                 ret = 1;
341                                         }
342                                 }
343                         }
344                         if (ret == 0) {
345                                 logthing(LOGTHING_INFO,
346                                                 "Fetching by fingerprint"
347                                                 ", result: %d",
348                                                 dbctx->fetch_key_fp(dbctx,
349                                                         &fingerprint,
350                                                         &key, false));
351                                 if (key != NULL) {
352                                         keyd_write_key(fd, key);
353                                         free_publickey(key);
354                                         key = NULL;
355                                 } else {
356                                         if (!keyd_write_size(fd, 0)) {
357                                                 ret = 1;
358                                         }
359                                 }
360                         }
361                         break;
362
363                 case KEYD_CMD_GET_TEXT:
364                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
365                                 ret = 1;
366                         }
367                         if (ret == 0) {
368                                 bytes = read(fd, &count, sizeof(count));
369                                 if (bytes != sizeof(count)) {
370                                         ret = 1;
371                                 }
372                         }
373                         if (ret == 0) {
374                                 search = malloc(count+1);
375                                 bytes = read(fd, search, count);
376                                 if (bytes != count) {
377                                         ret = 1;
378                                         free(search);
379                                         break;
380                                 }
381                                 search[count] = 0;
382                                 logthing(LOGTHING_INFO,
383                                                 "Fetching %s, result: %d",
384                                                 search,
385                                                 dbctx->fetch_key_text(dbctx,
386                                                         search, &key));
387                                 if (key != NULL) {
388                                         keyd_write_key(fd, key);
389                                         free_publickey(key);
390                                         key = NULL;
391                                 } else {
392                                         if (!keyd_write_size(fd, 0)) {
393                                                 ret = 1;
394                                         }
395                                 }
396                                 free(search);
397                         }
398                         break;
399                 case KEYD_CMD_STORE:
400                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
401                                 ret = 1;
402                         }
403                         if (ret == 0) {
404                                 bytes = read(fd, &storebuf.size,
405                                         sizeof(storebuf.size));
406                                 logthing(LOGTHING_TRACE, "Reading %d bytes.",
407                                         storebuf.size);
408                                 if (bytes != sizeof(storebuf.size)) {
409                                         ret = 1;
410                                 }
411                         }
412                         if (ret == 0 && storebuf.size > 0) {
413                                 storebuf.buffer = malloc(storebuf.size);
414                                 storebuf.offset = 0;
415                                 bytes = count = 0;
416                                 while (bytes >= 0 && count < storebuf.size) {
417                                         bytes = read(fd,
418                                                 &storebuf.buffer[count],
419                                                 storebuf.size - count);
420                                         logthing(LOGTHING_TRACE,
421                                                         "Read %d bytes.",
422                                                         bytes);
423                                         count += bytes;
424                                 }
425                                 read_openpgp_stream(buffer_fetchchar,
426                                                 &storebuf,
427                                                 &packets,
428                                                 0);
429                                 parse_keys(packets, &key);
430                                 dbctx->store_key(dbctx, key, false, false);
431                                 free_packet_list(packets);
432                                 packets = NULL;
433                                 free_publickey(key);
434                                 key = NULL;
435                                 free(storebuf.buffer);
436                                 storebuf.buffer = NULL;
437                                 storebuf.size = storebuf.offset = 0;
438                         }
439                         break;
440                 case KEYD_CMD_DELETE:
441                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
442                                 ret = 1;
443                         }
444                         if (ret == 0) {
445                                 bytes = read(fd, &keyid, sizeof(keyid));
446                                 if (bytes != sizeof(keyid)) {
447                                         ret = 1;
448                                 }
449                         }
450                         if (ret == 0) {
451                                 logthing(LOGTHING_INFO,
452                                                 "Deleting 0x%" PRIX64
453                                                 ", result: %d",
454                                                 keyid,
455                                                 dbctx->delete_key(dbctx,
456                                                         keyid, false));
457                         }
458                         break;
459                 case KEYD_CMD_GETFULLKEYID:
460                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
461                                 ret = 1;
462                         }
463                         if (ret == 0) {
464                                 bytes = read(fd, &keyid, sizeof(keyid));
465                                 if (bytes != sizeof(keyid)) {
466                                         ret = 1;
467                                 }
468                         }
469                         if (ret == 0) {
470                                 keyid = dbctx->getfullkeyid(dbctx, keyid);
471                                 cmd = sizeof(keyid);
472                                 bytes = write(fd, &cmd, sizeof(cmd));
473                                 if (bytes != sizeof(cmd)) {
474                                         ret = 1;
475                                 }
476                         }
477                         if (ret == 0) {
478                                 bytes = write(fd, &keyid, sizeof(keyid));
479                                 if (bytes != sizeof(keyid)) {
480                                         ret = 1;
481                                 }
482                         }
483                         break;
484                 case KEYD_CMD_KEYITER:
485                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
486                                 ret = 1;
487                         }
488                         if (ret == 0) {
489                                 dbctx->iterate_keys(dbctx, iteratefunc,
490                                         &fd);
491                                 if (!keyd_write_size(fd, 0)) {
492                                         ret = 1;
493                                 }
494                         }
495                         break;
496                 case KEYD_CMD_CLOSE:
497                         /* We're going to close the FD even if this fails */
498                         (void) keyd_write_reply(fd, KEYD_REPLY_OK);
499                         ret = 1;
500                         break;
501                 case KEYD_CMD_QUIT:
502                         /* We're going to quit even if this fails */
503                         (void) keyd_write_reply(fd, KEYD_REPLY_OK);
504                         logthing(LOGTHING_NOTICE,
505                                 "Exiting due to quit request.");
506                         ret = 1;
507                         trytocleanup();
508                         break;
509                 case KEYD_CMD_STATS:
510                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
511                                 ret = 1;
512                         }
513                         if (ret == 0) {
514                                 cmd = sizeof(*stats);
515                                 bytes = write(fd, &cmd, sizeof(cmd));
516                                 if (bytes != sizeof(cmd)) {
517                                         ret = 1;
518                                 }
519                         }
520                         if (ret == 0) {
521                                 bytes = write(fd, stats, sizeof(*stats));
522                                 if (bytes != sizeof(*stats)) {
523                                         ret = 1;
524                                 }
525                         }
526                         break;
527                 case KEYD_CMD_GET_SKSHASH:
528                         if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
529                                 ret = 1;
530                         }
531                         if (ret == 0) {
532                                 bytes = read(fd, hash.hash, sizeof(hash.hash));
533                                 if (bytes != sizeof(hash.hash)) {
534                                         ret = 1;
535                                 }
536                         }
537                         if (ret == 0) {
538                                 logthing(LOGTHING_INFO,
539                                                 "Fetching by hash"
540                                                 ", result: %d",
541                                                 dbctx->fetch_key_skshash(dbctx,
542                                                         &hash, &key));
543                                 if (key != NULL) {
544                                         keyd_write_key(fd, key);
545                                         free_publickey(key);
546                                         key = NULL;
547                                 } else {
548                                         if (!keyd_write_size(fd, 0)) {
549                                                 ret = 1;
550                                         }
551                                 }
552                         }
553                         break;
554
555                 default:
556                         logthing(LOGTHING_ERROR, "Got unknown command: %d",
557                                         cmd);
558                         if (!keyd_write_reply(fd, KEYD_REPLY_UNKNOWN_CMD)) {
559                                 ret = 1;
560                         }
561                 }
562         }
563
564         return(ret);
565 }
566
567 static int sock_close(int fd)
568 {
569         shutdown(fd, SHUT_RDWR);
570         return close(fd);
571 }
572
573 static int sock_accept(int fd)
574 {
575         struct sockaddr_un sock;
576         socklen_t          socklen;
577         int    srv = -1;
578         int    ret = -1;
579
580         socklen = sizeof(sock);
581         srv = accept(fd, (struct sockaddr *) &sock, &socklen);
582         if (srv != -1) {
583                 ret = fcntl(srv, F_SETFD, FD_CLOEXEC);
584         }
585
586         if (ret != -1) {
587                 stats->connects++;
588         }
589
590         return (srv);
591 }
592
593 static void usage(void)
594 {
595         puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
596                 "onak PGP keyserver.\n");
597         puts("Usage:\n");
598         puts("\tkeyd [options]\n");
599         puts("\tOptions:\n:");
600         puts("-c <file> - use <file> as the config file");
601         puts("-f        - run in the foreground");
602         puts("-h        - show this help text");
603         exit(EXIT_FAILURE);
604 }
605
606 int main(int argc, char *argv[])
607 {
608         int fd = -1, maxfd, i, clients[MAX_CLIENTS];
609         fd_set rfds;
610         char sockname[1024];
611         char *configfile = NULL;
612         bool foreground = false;
613         int optchar;
614         struct onak_dbctx *dbctx;
615
616         while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
617                 switch (optchar) {
618                 case 'c':
619                         if (configfile != NULL) {
620                                 free(configfile);
621                         }
622                         configfile = strdup(optarg);
623                         break;
624                 case 'f':
625                         foreground = true;
626                         break;
627                 case 'h':
628                 default:
629                         usage();
630                         break;
631                 }
632         }
633
634         readconfig(configfile);
635         free(configfile);
636         configfile = NULL;
637         initlogthing("keyd", config.logfile);
638         config.use_keyd = false;
639
640         if (!foreground) {
641                 daemonize();
642         }
643
644         catchsignals();
645         signal(SIGPIPE, SIG_IGN);
646
647
648         stats = calloc(1, sizeof(*stats));
649         if (!stats) {
650                 logthing(LOGTHING_ERROR,
651                         "Couldn't allocate memory for stats structure.");
652                 exit(EXIT_FAILURE);
653         }
654         stats->started = time(NULL);
655
656         snprintf(sockname, 1023, "%s/%s", config.sock_dir, KEYD_SOCKET);
657         fd = sock_init(sockname);
658
659         if (fd != -1) {
660                 FD_ZERO(&rfds);
661                 FD_SET(fd, &rfds);
662                 maxfd = fd;
663                 memset(clients, -1, sizeof (clients));
664
665                 dbctx = config.dbinit(config.backend, false);
666
667                 logthing(LOGTHING_NOTICE, "Accepting connections.");
668                 while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
669                         /*
670                          * Deal with existing clients first; if we're at our
671                          * connection limit then processing them might free
672                          * things up and let us accept the next client below.
673                          */
674                         for (i = 0; i < MAX_CLIENTS; i++) {
675                                 if (clients[i] != -1 &&
676                                                 FD_ISSET(clients[i], &rfds)) {
677                                         logthing(LOGTHING_DEBUG,
678                                                 "Handling connection for client %d.", i);
679                                         if (sock_do(dbctx, clients[i])) {
680                                                 sock_close(clients[i]);
681                                                 clients[i] = -1;
682                                                 logthing(LOGTHING_DEBUG,
683                                                         "Closed connection for client %d.", i);
684                                         }
685                                 }
686                         }
687                         /*
688                          * Check if we have a new incoming connection to accept.
689                          */
690                         if (FD_ISSET(fd, &rfds)) {
691                                 for (i = 0; i < MAX_CLIENTS; i++) {
692                                         if (clients[i] == -1) {
693                                                 break;
694                                         }
695                                 }
696                                 if (i < MAX_CLIENTS) {
697                                         logthing(LOGTHING_INFO,
698                                                 "Accepted connection %d.", i);
699                                         clients[i] = sock_accept(fd);
700                                 }
701                         }
702                         FD_ZERO(&rfds);
703                         FD_SET(fd, &rfds);
704                         maxfd = fd;
705                         for (i = 0; i < MAX_CLIENTS; i++) {
706                                 if (clients[i] != -1) {
707                                         FD_SET(clients[i], &rfds);
708                                         maxfd = (maxfd > clients[i]) ?
709                                                         maxfd : clients[i];
710                                 }
711                         }
712                 }
713                 dbctx->cleanupdb(dbctx);
714 #ifdef HAVE_SYSTEMD
715                 if (!using_socket_activation) {
716 #endif
717                         sock_close(fd);
718                         unlink(sockname);
719 #ifdef HAVE_SYSTEMD
720                 }
721 #endif
722         }
723
724         free(stats);
725
726         cleanuplogthing();
727         cleanupconfig();
728
729         return(EXIT_SUCCESS);
730 }