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>
31 #include "charfuncs.h"
37 #include "keystructs.h"
41 #include "onak-conf.h"
46 void find_keys(struct onak_dbctx *dbctx,
47 char *search, uint64_t keyid,
48 struct openpgp_fingerprint *fingerprint,
49 bool ishex, bool isfp, bool dispfp, bool skshash,
50 bool exact, bool verbose)
52 struct openpgp_publickey *publickey = NULL;
56 count = dbctx->fetch_key_id(dbctx, keyid, &publickey,
59 count = dbctx->fetch_key_fp(dbctx, fingerprint,
62 count = dbctx->fetch_key_text(dbctx, search, &publickey);
64 if (publickey != NULL) {
65 key_index(dbctx, publickey, verbose, dispfp, skshash,
67 free_publickey(publickey);
68 } else if (count == 0) {
69 puts("Key not found.");
71 printf("Found %d keys, but maximum number to return is %d.\n",
74 puts("Try again with a more specific search.");
79 * @brief Context for the keyserver dumping function
82 /** Keys we've dumped so far to this file */
84 /** Maximum keys to dump per file */
86 /** File descriptor for the current dump file */
88 /** Number of the current dump file */
90 /** Base filename to use for dump files */
94 void dump_func(void *ctx, struct openpgp_publickey *key)
96 struct openpgp_packet_list *packets = NULL;
97 struct openpgp_packet_list *list_end = NULL;
98 struct dump_ctx *state;
101 state = (struct dump_ctx *) ctx;
103 if (state->fd == -1 || state->count++ > state->maxcount) {
104 if (state->fd != -1) {
108 snprintf(filename, 1023, state->filebase, state->filenum);
109 state->fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
113 flatten_publickey(key, &packets, &list_end);
114 write_openpgp_stream(file_putchar, &state->fd, packets);
115 free_packet_list(packets);
116 packets = list_end = NULL;
121 static uint8_t hex2bin(char c)
123 if (c >= '0' && c <= '9') {
125 } else if (c >= 'a' && c <= 'f') {
126 return (c - 'a' + 10);
127 } else if (c >= 'A' && c <= 'F') {
128 return (c - 'A' + 10);
135 puts("onak " ONAK_VERSION " - an OpenPGP keyserver.\n");
137 puts("\tonak [options] <command> <parameters>\n");
138 puts("\tCommands:\n");
139 puts("\tadd - read armored OpenPGP keys from stdin and add to the"
141 puts("\tclean - read armored OpenPGP keys from stdin, run the"
142 " cleaning\n\t routines against them and dump to"
144 puts("\tdelete - delete a given key from the keyserver");
145 puts("\tdump - dump all the keys from the keyserver to a file or"
146 " files\n\t starting keydump*");
147 puts("\tget - retrieves the key requested from the keyserver");
148 puts("\tgetphoto - retrieves the first photoid on the given key and"
149 " dumps to\n\t stdout");
150 puts("\tindex - search for a key and list it");
151 puts("\treindex - retrieve and re-store a key in the backend db");
152 puts("\tvindex - search for a key and list it and its signatures");
155 int main(int argc, char *argv[])
157 struct openpgp_packet_list *packets = NULL;
158 struct openpgp_packet_list *list_end = NULL;
159 struct openpgp_publickey *keys = NULL;
160 char *configfile = NULL;
161 int rc = EXIT_SUCCESS;
172 bool skshash = false;
174 struct dump_ctx dumpstate;
176 struct onak_dbctx *dbctx;
177 struct openpgp_fingerprint fingerprint;
179 while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
185 configfile = strdup(optarg);
197 setlogthreshold(LOGTHING_INFO);
202 readconfig(configfile);
203 initlogthing("onak", config.logfile);
206 if ((argc - optind) < 1) {
208 } else if (!strcmp("dump", argv[optind])) {
209 dbctx = config.dbinit(config.backend, true);
210 dumpstate.count = dumpstate.filenum = 0;
211 dumpstate.maxcount = 100000;
213 dumpstate.filebase = "keydump.%d.pgp";
214 dbctx->iterate_keys(dbctx, dump_func, &dumpstate);
215 if (dumpstate.fd != -1) {
219 dbctx->cleanupdb(dbctx);
220 } else if (!strcmp("add", argv[optind])) {
222 result = read_openpgp_stream(stdin_getchar, NULL,
224 logthing(LOGTHING_INFO,
225 "read_openpgp_stream: %d", result);
227 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
229 if (packets != NULL) {
230 result = parse_keys(packets, &keys);
231 free_packet_list(packets);
233 logthing(LOGTHING_INFO, "Finished reading %d keys.",
236 result = cleankeys(keys);
237 logthing(LOGTHING_INFO, "%d keys cleaned.",
240 dbctx = config.dbinit(config.backend, false);
241 logthing(LOGTHING_NOTICE, "Got %d new keys.",
242 dbctx->update_keys(dbctx, &keys,
244 if (keys != NULL && update) {
245 flatten_publickey(keys,
249 write_openpgp_stream(stdout_putchar,
253 armor_openpgp_stream(stdout_putchar,
257 free_packet_list(packets);
260 dbctx->cleanupdb(dbctx);
263 logthing(LOGTHING_NOTICE, "No keys read.");
267 free_publickey(keys);
271 logthing(LOGTHING_NOTICE, "No changes.");
273 } else if (!strcmp("clean", argv[optind])) {
275 result = read_openpgp_stream(stdin_getchar, NULL,
277 logthing(LOGTHING_INFO,
278 "read_openpgp_stream: %d", result);
280 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
283 if (packets != NULL) {
284 result = parse_keys(packets, &keys);
285 free_packet_list(packets);
287 logthing(LOGTHING_INFO, "Finished reading %d keys.",
291 result = cleankeys(keys);
292 logthing(LOGTHING_INFO, "%d keys cleaned.",
295 flatten_publickey(keys,
300 write_openpgp_stream(stdout_putchar,
304 armor_openpgp_stream(stdout_putchar,
308 free_packet_list(packets);
313 logthing(LOGTHING_NOTICE, "No keys read.");
317 free_publickey(keys);
320 } else if (!strcmp("dumpconfig", argv[optind])) {
321 if ((argc - optind) == 2) {
322 writeconfig(argv[optind + 1]);
324 /* Dump config to stdout */
327 } else if ((argc - optind) == 2) {
328 search = argv[optind+1];
329 if (search != NULL && strlen(search) == 42 &&
330 search[0] == '0' && search[1] == 'x') {
331 fingerprint.length = MAX_FINGERPRINT_LEN;
332 for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
334 (hex2bin(search[2 + i * 2]) << 4) +
335 hex2bin(search[3 + i * 2]);
338 } else if (search != NULL) {
339 keyid = strtouq(search, &end, 16);
346 dbctx = config.dbinit(config.backend, false);
347 if (!strcmp("index", argv[optind])) {
348 find_keys(dbctx, search, keyid, &fingerprint, ishex,
349 isfp, dispfp, skshash,
351 } else if (!strcmp("vindex", argv[optind])) {
352 find_keys(dbctx, search, keyid, &fingerprint, ishex,
353 isfp, dispfp, skshash,
355 } else if (!strcmp("getphoto", argv[optind])) {
357 puts("Can't get a key on uid text."
358 " You must supply a keyid.");
359 } else if (dbctx->fetch_key_id(dbctx, keyid, &keys,
361 unsigned char *photo = NULL;
364 if (getphoto(keys, 0, &photo,
365 &length) == ONAK_E_OK) {
371 free_publickey(keys);
374 puts("Key not found");
376 } else if (!strcmp("delete", argv[optind])) {
377 dbctx->delete_key(dbctx,
378 dbctx->getfullkeyid(dbctx, keyid),
380 } else if (!strcmp("get", argv[optind])) {
381 if (!(ishex || isfp)) {
382 puts("Can't get a key on uid text."
383 " You must supply a keyid / "
386 dbctx->fetch_key_fp(dbctx,
390 dbctx->fetch_key_id(dbctx, keyid,
392 logthing(LOGTHING_INFO, "Got key.");
393 flatten_publickey(keys,
396 free_publickey(keys);
398 write_openpgp_stream(stdout_putchar,
402 armor_openpgp_stream(stdout_putchar,
406 free_packet_list(packets);
409 puts("Key not found");
411 } else if (!strcmp("hget", argv[optind])) {
412 if (!parse_skshash(search, &hash)) {
413 puts("Couldn't parse sks hash.");
414 } else if (dbctx->fetch_key_skshash(dbctx, &hash,
416 logthing(LOGTHING_INFO, "Got key.");
417 flatten_publickey(keys,
420 free_publickey(keys);
422 write_openpgp_stream(stdout_putchar,
426 armor_openpgp_stream(stdout_putchar,
430 free_packet_list(packets);
433 puts("Key not found");
435 } else if (!strcmp("reindex", argv[optind])) {
436 dbctx->starttrans(dbctx);
437 if (dbctx->fetch_key_id(dbctx, keyid, &keys, true)) {
438 dbctx->delete_key(dbctx, keyid, true);
440 dbctx->store_key(dbctx, keys, true, false);
442 puts("Key not found");
444 dbctx->endtrans(dbctx);
446 dbctx->cleanupdb(dbctx);