]> the.earth.li Git - onak.git/blob - onak.c
Set Rules-Requires-Root to no
[onak.git] / onak.c
1 /*
2  * onak.c - An OpenPGP keyserver.
3  *
4  * This is the main swiss army knife binary.
5  *
6  * Copyright 2002 Jonathan McDowell <noodles@earth.li>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29
30 #include "build-config.h"
31
32 #include "armor.h"
33 #include "charfuncs.h"
34 #include "cleankey.h"
35 #include "cleanup.h"
36 #include "keydb.h"
37 #include "keyid.h"
38 #include "keyindex.h"
39 #include "keystructs.h"
40 #include "log.h"
41 #include "mem.h"
42 #include "merge.h"
43 #include "onak-conf.h"
44 #include "parsekey.h"
45 #include "photoid.h"
46
47 void find_keys(struct onak_dbctx *dbctx,
48                 char *search, uint64_t keyid,
49                 struct openpgp_fingerprint *fingerprint,
50                 bool ishex, bool isfp, bool dispfp, bool skshash,
51                 bool exact, bool verbose)
52 {
53         struct openpgp_publickey *publickey = NULL;
54         int count = 0;
55
56         if (ishex) {
57                 count = dbctx->fetch_key_id(dbctx, keyid, &publickey,
58                                 false);
59         } else if (isfp) {
60                 count = dbctx->fetch_key_fp(dbctx, fingerprint,
61                                 &publickey, false);
62         } else {
63                 count = dbctx->fetch_key_text(dbctx, search, &publickey);
64         }
65         if (publickey != NULL) {
66                 key_index(dbctx, publickey, verbose, dispfp, skshash,
67                         false);
68                 free_publickey(publickey);
69         } else if (count == 0) {
70                 puts("Key not found.");
71         } else {
72                 printf("Found %d keys, but maximum number to return is %d.\n",
73                                 count,
74                                 config.maxkeys);
75                 puts("Try again with a more specific search.");
76         }
77 }
78
79 /**
80  * @brief Context for the keyserver dumping function
81  */
82 struct dump_ctx {
83         /** Keys we've dumped so far to this file */
84         int count;
85         /** Maximum keys to dump per file */
86         int maxcount;
87         /** File descriptor for the current dump file */
88         int fd;
89         /** Number of the current dump file */
90         int filenum;
91         /** Base filename to use for dump files */
92         char *filebase;
93 };
94
95 void dump_func(void *ctx, struct openpgp_publickey *key)
96 {
97         struct openpgp_packet_list *packets = NULL;
98         struct openpgp_packet_list *list_end = NULL;
99         struct dump_ctx *state;
100         char filename[1024];
101
102         state = (struct dump_ctx *) ctx;
103
104         if (state->fd == -1 || state->count++ > state->maxcount) {
105                 if (state->fd != -1) {
106                         close(state->fd);
107                         state->fd = -1;
108                 }
109                 snprintf(filename, 1023, state->filebase, state->filenum);
110                 state->fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
111                 state->filenum++;
112                 state->count = 0;
113         }
114         flatten_publickey(key, &packets, &list_end);
115         write_openpgp_stream(file_putchar, &state->fd, packets);
116         free_packet_list(packets);
117         packets = list_end = NULL;
118
119         return;
120 }
121
122 static uint8_t hex2bin(char c)
123 {
124         if (c >= '0' && c <= '9') {
125                 return (c - '0');
126         } else if (c >= 'a' && c <= 'f') {
127                 return (c - 'a' + 10);
128         } else if (c >= 'A' && c <= 'F') {
129                 return (c - 'A' + 10);
130         }
131
132         return 255;
133 }
134
135 void usage(void) {
136         puts("onak " ONAK_VERSION " - an OpenPGP keyserver.\n");
137         puts("Usage:\n");
138         puts("\tonak [options] <command> <parameters>\n");
139         puts("\tCommands:\n");
140         puts("\tadd      - read armored OpenPGP keys from stdin and add to the"
141                 " keyserver");
142         puts("\tclean    - read armored OpenPGP keys from stdin, run the"
143                 " cleaning\n\t             routines against them and dump to"
144                 " stdout");
145         puts("\tdelete   - delete a given key from the keyserver");
146         puts("\tdump     - dump all the keys from the keyserver to a file or"
147                 " files\n\t           starting keydump*");
148         puts("\tget      - retrieves the key requested from the keyserver");
149         puts("\tgetphoto - retrieves the first photoid on the given key and"
150                 " dumps to\n\t           stdout");
151         puts("\tindex    - search for a key and list it");
152         puts("\treindex  - retrieve and re-store a key in the backend db");
153         puts("\tvindex   - search for a key and list it and its signatures");
154 }
155
156 int main(int argc, char *argv[])
157 {
158         struct openpgp_packet_list      *packets = NULL;
159         struct openpgp_packet_list      *list_end = NULL;
160         struct openpgp_publickey        *keys = NULL;
161         char                            *configfile = NULL;
162         int                              rc = EXIT_SUCCESS;
163         int                              result = 0;
164         char                            *search = NULL;
165         char                            *end = NULL;
166         uint64_t                         keyid = 0;
167         int                              i;
168         bool                             ishex = false;
169         bool                             isfp = false;
170         bool                             update = false;
171         bool                             binary = false;
172         bool                             dispfp = false;
173         bool                             skshash = false;
174         int                              optchar;
175         struct dump_ctx                  dumpstate;
176         struct skshash                   hash;
177         struct onak_dbctx               *dbctx;
178         struct openpgp_fingerprint       fingerprint;
179
180         while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
181                 switch (optchar) {
182                 case 'b': 
183                         binary = true;
184                         break;
185                 case 'c':
186                         configfile = strdup(optarg);
187                         break;
188                 case 'f': 
189                         dispfp = true;
190                         break;
191                 case 's': 
192                         skshash = true;
193                         break;
194                 case 'u': 
195                         update = true;
196                         break;
197                 case 'v': 
198                         setlogthreshold(LOGTHING_INFO);
199                         break;
200                 }
201         }
202
203         readconfig(configfile);
204         initlogthing("onak", config.logfile);
205         catchsignals();
206
207         if ((argc - optind) < 1) {
208                 usage();
209         } else if (!strcmp("dump", argv[optind])) {
210                 dbctx = config.dbinit(config.backend, true);
211                 dumpstate.count = dumpstate.filenum = 0;
212                 dumpstate.maxcount = 100000;
213                 dumpstate.fd = -1;
214                 dumpstate.filebase = "keydump.%d.pgp";
215                 dbctx->iterate_keys(dbctx, dump_func, &dumpstate);
216                 if (dumpstate.fd != -1) {
217                         close(dumpstate.fd);
218                         dumpstate.fd = -1;
219                 }
220                 dbctx->cleanupdb(dbctx);
221         } else if (!strcmp("add", argv[optind])) {
222                 if (binary) {
223                         result = read_openpgp_stream(stdin_getchar, NULL,
224                                  &packets, 0);
225                         logthing(LOGTHING_INFO,
226                                         "read_openpgp_stream: %d", result);
227                 } else {
228                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
229                 }
230                 if (packets != NULL) {
231                         result = parse_keys(packets, &keys);
232                         free_packet_list(packets);
233                         packets = NULL;
234                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
235                                         result);
236
237                         result = cleankeys(&keys, config.clean_policies);
238                         logthing(LOGTHING_INFO, "%d keys cleaned.",
239                                         result);
240
241                         dbctx = config.dbinit(config.backend, false);
242                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
243                                         dbctx->update_keys(dbctx, &keys,
244                                                 &config.blacklist,
245                                                 (config.clean_policies &
246                                                  ONAK_CLEAN_UPDATE_ONLY),
247                                                 false));
248                         if (keys != NULL && update) {
249                                 flatten_publickey(keys,
250                                         &packets,
251                                         &list_end);
252                                 if (binary) {
253                                         write_openpgp_stream(stdout_putchar,
254                                                         NULL,
255                                                         packets);
256                                 } else {
257                                         armor_openpgp_stream(stdout_putchar,
258                                                 NULL,
259                                                 packets);
260                                 }
261                                 free_packet_list(packets);
262                                 packets = NULL;
263                         }
264                         dbctx->cleanupdb(dbctx);
265                 } else {
266                         rc = 1;
267                         logthing(LOGTHING_NOTICE, "No keys read.");
268                 }
269
270                 if (keys != NULL) {
271                         free_publickey(keys);
272                         keys = NULL;
273                 } else {
274                         rc = 1;
275                         logthing(LOGTHING_NOTICE, "No changes.");
276                 }
277         } else if (!strcmp("clean", argv[optind])) {
278                 if (binary) {
279                         result = read_openpgp_stream(stdin_getchar, NULL,
280                                  &packets, 0);
281                         logthing(LOGTHING_INFO,
282                                         "read_openpgp_stream: %d", result);
283                 } else {
284                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
285                 }
286
287                 if (packets != NULL) {
288                         result = parse_keys(packets, &keys);
289                         free_packet_list(packets);
290                         packets = NULL;
291                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
292                                         result);
293
294                         if (keys != NULL) {
295                                 result = cleankeys(&keys,
296                                                 config.clean_policies);
297                                 logthing(LOGTHING_INFO, "%d keys cleaned.",
298                                                 result);
299
300                                 flatten_publickey(keys,
301                                         &packets,
302                                         &list_end);
303
304                                 if (binary) {
305                                         write_openpgp_stream(stdout_putchar,
306                                                         NULL,
307                                                         packets);
308                                 } else {
309                                         armor_openpgp_stream(stdout_putchar,
310                                                 NULL,
311                                                 packets);
312                                 }
313                                 free_packet_list(packets);
314                                 packets = NULL;
315                         }
316                 } else {
317                         rc = 1;
318                         logthing(LOGTHING_NOTICE, "No keys read.");
319                 }
320                 
321                 if (keys != NULL) {
322                         free_publickey(keys);
323                         keys = NULL;
324                 }
325         } else if (!strcmp("dumpconfig", argv[optind])) {
326                 if ((argc - optind) == 2) {
327                         writeconfig(argv[optind + 1]);
328                 } else {
329                         /* Dump config to stdout */
330                         writeconfig(NULL);
331                 }
332         } else if ((argc - optind) == 2) {
333                 search = argv[optind+1];
334                 if (search != NULL && strlen(search) == 42 &&
335                                 search[0] == '0' && search[1] == 'x') {
336                         /* v4 fingerprint */
337                         fingerprint.length = 20;
338                         for (i = 0; i < 20; i++) {
339                                 fingerprint.fp[i] =
340                                         (hex2bin(search[2 + i * 2]) << 4) +
341                                                 hex2bin(search[3 + i * 2]);
342                         }
343                         isfp = true;
344                 } else if (search != NULL && strlen(search) == 66 &&
345                                 search[0] == '0' && search[1] == 'x') {
346                         /* v5 fingerprint */
347                         fingerprint.length = 32;
348                         for (i = 0; i < 32; i++) {
349                                 fingerprint.fp[i] =
350                                         (hex2bin(search[2 + i * 2]) << 4) +
351                                                 hex2bin(search[3 + i * 2]);
352                         }
353                         isfp = true;
354                 } else if (search != NULL) {
355                         keyid = strtouq(search, &end, 16);
356                         if (*search != 0 &&
357                                         end != NULL &&
358                                         *end == 0) {
359                                 ishex = true;
360                         }
361                 }
362                 dbctx = config.dbinit(config.backend, false);
363                 if (!strcmp("index", argv[optind])) {
364                         find_keys(dbctx, search, keyid, &fingerprint, ishex,
365                                         isfp, dispfp, skshash,
366                                         false, false);
367                 } else if (!strcmp("vindex", argv[optind])) {
368                         find_keys(dbctx, search, keyid, &fingerprint, ishex,
369                                         isfp, dispfp, skshash,
370                                         false, true);
371                 } else if (!strcmp("getphoto", argv[optind])) {
372                         if (!ishex) {
373                                 puts("Can't get a key on uid text."
374                                         " You must supply a keyid.");
375                         } else if (dbctx->fetch_key_id(dbctx, keyid, &keys,
376                                         false)) {
377                                 unsigned char *photo = NULL;
378                                 size_t         length = 0;
379
380                                 if (getphoto(keys, 0, &photo,
381                                                 &length) == ONAK_E_OK) {
382                                         fwrite(photo,
383                                                 1,
384                                                 length,
385                                                 stdout);
386                                 }
387                                 free_publickey(keys);
388                                 keys = NULL;
389                         } else {
390                                 puts("Key not found");
391                         }
392                 } else if (!strcmp("delete", argv[optind])) {
393                         if (!isfp) {
394                                 if (dbctx->fetch_key_id(dbctx, keyid, &keys,
395                                                         false)) {
396                                         get_fingerprint(keys->publickey,
397                                                         &fingerprint);
398                                         dbctx->delete_key(dbctx, &fingerprint,
399                                                         false);
400                                         free_publickey(keys);
401                                         keys = NULL;
402                                 }
403                         } else
404                                 dbctx->delete_key(dbctx, &fingerprint, false);
405                 } else if (!strcmp("get", argv[optind])) {
406                         if (!(ishex || isfp)) {
407                                 puts("Can't get a key on uid text."
408                                         " You must supply a keyid / "
409                                         "fingerprint.");
410                         } else if ((isfp &&
411                                         dbctx->fetch_key_fp(dbctx,
412                                                 &fingerprint,
413                                                 &keys, false)) ||
414                                         (ishex &&
415                                         dbctx->fetch_key_id(dbctx, keyid,
416                                                 &keys, false))) {
417                                 logthing(LOGTHING_INFO, "Got key.");
418                                 flatten_publickey(keys,
419                                                 &packets,
420                                                 &list_end);
421                                 free_publickey(keys);
422                                 if (binary) {
423                                         write_openpgp_stream(stdout_putchar,
424                                                 NULL,
425                                                 packets);
426                                 } else {
427                                         armor_openpgp_stream(stdout_putchar,
428                                                 NULL,
429                                                 packets);
430                                 }
431                                 free_packet_list(packets);
432                                 packets = NULL;
433                         } else {
434                                 puts("Key not found");
435                         }
436                 } else if (!strcmp("hget", argv[optind])) {
437                         if (!parse_skshash(search, &hash)) {
438                                 puts("Couldn't parse sks hash.");
439                         } else if (dbctx->fetch_key_skshash(dbctx, &hash,
440                                         &keys)) {
441                                 logthing(LOGTHING_INFO, "Got key.");
442                                 flatten_publickey(keys,
443                                                 &packets,
444                                                 &list_end);
445                                 free_publickey(keys);
446                                 if (binary) {
447                                         write_openpgp_stream(stdout_putchar,
448                                                 NULL,
449                                                 packets);
450                                 } else {
451                                         armor_openpgp_stream(stdout_putchar,
452                                                 NULL,
453                                                 packets);
454                                 }
455                                 free_packet_list(packets);
456                                 packets = NULL;
457                         } else {
458                                 puts("Key not found");
459                         }
460                 } else if (!strcmp("reindex", argv[optind])) {
461                         dbctx->starttrans(dbctx);
462                         if (dbctx->fetch_key_id(dbctx, keyid, &keys, true)) {
463                                 get_fingerprint(keys->publickey, &fingerprint);
464                                 dbctx->delete_key(dbctx, &fingerprint, true);
465                                 cleankeys(&keys, config.clean_policies);
466                                 dbctx->store_key(dbctx, keys, true, false);
467                         } else {
468                                 puts("Key not found");
469                         }
470                         dbctx->endtrans(dbctx);
471                 }
472                 dbctx->cleanupdb(dbctx);
473         } else {
474                 usage();
475         }
476
477         cleanuplogthing();
478         cleanupconfig();
479         free(configfile);
480
481         return rc;
482 }