2 * onak.c - An OpenPGP keyserver.
4 * This is the main swiss army knife binary.
6 * Copyright 2002 Jonathan McDowell <noodles@earth.li>
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.
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
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/>.
26 #include <sys/types.h>
30 #include "build-config.h"
33 #include "charfuncs.h"
39 #include "keystructs.h"
43 #include "onak-conf.h"
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)
53 struct openpgp_publickey *publickey = NULL;
57 count = dbctx->fetch_key_id(dbctx, keyid, &publickey,
60 count = dbctx->fetch_key_fp(dbctx, fingerprint,
63 count = dbctx->fetch_key_text(dbctx, search, &publickey);
65 if (publickey != NULL) {
66 key_index(dbctx, publickey, verbose, dispfp, skshash,
68 free_publickey(publickey);
69 } else if (count == 0) {
70 puts("Key not found.");
72 printf("Found %d keys, but maximum number to return is %d.\n",
75 puts("Try again with a more specific search.");
80 * @brief Context for the keyserver dumping function
83 /** Keys we've dumped so far to this file */
85 /** Maximum keys to dump per file */
87 /** File descriptor for the current dump file */
89 /** Number of the current dump file */
91 /** Base filename to use for dump files */
95 void dump_func(void *ctx, struct openpgp_publickey *key)
97 struct openpgp_packet_list *packets = NULL;
98 struct openpgp_packet_list *list_end = NULL;
99 struct dump_ctx *state;
102 state = (struct dump_ctx *) ctx;
104 if (state->fd == -1 || state->count++ > state->maxcount) {
105 if (state->fd != -1) {
109 snprintf(filename, 1023, state->filebase, state->filenum);
110 state->fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
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;
122 static uint8_t hex2bin(char c)
124 if (c >= '0' && c <= '9') {
126 } else if (c >= 'a' && c <= 'f') {
127 return (c - 'a' + 10);
128 } else if (c >= 'A' && c <= 'F') {
129 return (c - 'A' + 10);
136 puts("onak " ONAK_VERSION " - an OpenPGP keyserver.\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"
142 puts("\tclean - read armored OpenPGP keys from stdin, run the"
143 " cleaning\n\t routines against them and dump to"
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");
156 int main(int argc, char *argv[])
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;
173 bool skshash = false;
175 struct dump_ctx dumpstate;
177 struct onak_dbctx *dbctx;
178 struct openpgp_fingerprint fingerprint;
180 while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
186 configfile = strdup(optarg);
198 setlogthreshold(LOGTHING_INFO);
203 readconfig(configfile);
204 initlogthing("onak", config.logfile);
207 if ((argc - optind) < 1) {
209 } else if (!strcmp("dump", argv[optind])) {
210 dbctx = config.dbinit(config.backend, true);
211 dumpstate.count = dumpstate.filenum = 0;
212 dumpstate.maxcount = 100000;
214 dumpstate.filebase = "keydump.%d.pgp";
215 dbctx->iterate_keys(dbctx, dump_func, &dumpstate);
216 if (dumpstate.fd != -1) {
220 dbctx->cleanupdb(dbctx);
221 } else if (!strcmp("add", argv[optind])) {
223 result = read_openpgp_stream(stdin_getchar, NULL,
225 logthing(LOGTHING_INFO,
226 "read_openpgp_stream: %d", result);
228 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
230 if (packets != NULL) {
231 result = parse_keys(packets, &keys);
232 free_packet_list(packets);
234 logthing(LOGTHING_INFO, "Finished reading %d keys.",
237 result = cleankeys(&keys, config.clean_policies);
238 logthing(LOGTHING_INFO, "%d keys cleaned.",
241 dbctx = config.dbinit(config.backend, false);
242 logthing(LOGTHING_NOTICE, "Got %d new keys.",
243 dbctx->update_keys(dbctx, &keys,
245 (config.clean_policies &
246 ONAK_CLEAN_UPDATE_ONLY),
248 if (keys != NULL && update) {
249 flatten_publickey(keys,
253 write_openpgp_stream(stdout_putchar,
257 armor_openpgp_stream(stdout_putchar,
261 free_packet_list(packets);
264 dbctx->cleanupdb(dbctx);
267 logthing(LOGTHING_NOTICE, "No keys read.");
271 free_publickey(keys);
275 logthing(LOGTHING_NOTICE, "No changes.");
277 } else if (!strcmp("clean", argv[optind])) {
279 result = read_openpgp_stream(stdin_getchar, NULL,
281 logthing(LOGTHING_INFO,
282 "read_openpgp_stream: %d", result);
284 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
287 if (packets != NULL) {
288 result = parse_keys(packets, &keys);
289 free_packet_list(packets);
291 logthing(LOGTHING_INFO, "Finished reading %d keys.",
295 result = cleankeys(&keys,
296 config.clean_policies);
297 logthing(LOGTHING_INFO, "%d keys cleaned.",
300 flatten_publickey(keys,
305 write_openpgp_stream(stdout_putchar,
309 armor_openpgp_stream(stdout_putchar,
313 free_packet_list(packets);
318 logthing(LOGTHING_NOTICE, "No keys read.");
322 free_publickey(keys);
325 } else if (!strcmp("dumpconfig", argv[optind])) {
326 if ((argc - optind) == 2) {
327 writeconfig(argv[optind + 1]);
329 /* Dump config to stdout */
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') {
337 fingerprint.length = 20;
338 for (i = 0; i < 20; i++) {
340 (hex2bin(search[2 + i * 2]) << 4) +
341 hex2bin(search[3 + i * 2]);
344 } else if (search != NULL && strlen(search) == 66 &&
345 search[0] == '0' && search[1] == 'x') {
347 fingerprint.length = 32;
348 for (i = 0; i < 32; i++) {
350 (hex2bin(search[2 + i * 2]) << 4) +
351 hex2bin(search[3 + i * 2]);
354 } else if (search != NULL) {
355 keyid = strtouq(search, &end, 16);
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,
367 } else if (!strcmp("vindex", argv[optind])) {
368 find_keys(dbctx, search, keyid, &fingerprint, ishex,
369 isfp, dispfp, skshash,
371 } else if (!strcmp("getphoto", argv[optind])) {
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,
377 unsigned char *photo = NULL;
380 if (getphoto(keys, 0, &photo,
381 &length) == ONAK_E_OK) {
387 free_publickey(keys);
390 puts("Key not found");
392 } else if (!strcmp("delete", argv[optind])) {
394 if (dbctx->fetch_key_id(dbctx, keyid, &keys,
396 get_fingerprint(keys->publickey,
398 dbctx->delete_key(dbctx, &fingerprint,
400 free_publickey(keys);
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 / "
411 dbctx->fetch_key_fp(dbctx,
415 dbctx->fetch_key_id(dbctx, keyid,
417 logthing(LOGTHING_INFO, "Got key.");
418 flatten_publickey(keys,
421 free_publickey(keys);
423 write_openpgp_stream(stdout_putchar,
427 armor_openpgp_stream(stdout_putchar,
431 free_packet_list(packets);
434 puts("Key not found");
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,
441 logthing(LOGTHING_INFO, "Got key.");
442 flatten_publickey(keys,
445 free_publickey(keys);
447 write_openpgp_stream(stdout_putchar,
451 armor_openpgp_stream(stdout_putchar,
455 free_packet_list(packets);
458 puts("Key not found");
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);
468 puts("Key not found");
470 dbctx->endtrans(dbctx);
472 dbctx->cleanupdb(dbctx);