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