2 * onak.c - An OpenPGP keyserver.
4 * This is the main swiss army knife binary.
6 * Jonathan McDowell <noodles@earth.li>
8 * Copyright 2002 Project Purple
16 #include <sys/types.h>
21 #include "charfuncs.h"
27 #include "keystructs.h"
31 #include "onak-conf.h"
36 void find_keys(char *search, uint64_t keyid, bool ishex,
37 bool fingerprint, bool skshash, bool exact, bool verbose)
39 struct openpgp_publickey *publickey = NULL;
43 count = config.dbbackend->fetch_key(keyid, &publickey, false);
45 count = config.dbbackend->fetch_key_text(search, &publickey);
47 if (publickey != NULL) {
48 key_index(publickey, verbose, fingerprint, skshash, false);
49 free_publickey(publickey);
50 } else if (count == 0) {
51 puts("Key not found.");
53 printf("Found %d keys, but maximum number to return is %d.\n",
56 puts("Try again with a more specific search.");
68 void dump_func(void *ctx, struct openpgp_publickey *key)
70 struct openpgp_packet_list *packets = NULL;
71 struct openpgp_packet_list *list_end = NULL;
72 struct dump_ctx *state;
75 state = (struct dump_ctx *) ctx;
77 if (state->fd == -1 || state->count++ > state->maxcount) {
78 if (state->fd != -1) {
82 snprintf(filename, 1023, state->filebase, state->filenum);
83 state->fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
87 flatten_publickey(key, &packets, &list_end);
88 write_openpgp_stream(file_putchar, &state->fd, packets);
89 free_packet_list(packets);
90 packets = list_end = NULL;
96 puts("onak " ONAK_VERSION " - an OpenPGP keyserver.\n");
98 puts("\tonak [options] <command> <parameters>\n");
99 puts("\tCommands:\n");
100 puts("\tadd - read armored OpenPGP keys from stdin and add to the"
102 puts("\tclean - read armored OpenPGP keys from stdin, run the"
103 " cleaning\n\t routines against them and dump to"
105 puts("\tdelete - delete a given key from the keyserver");
106 puts("\tdump - dump all the keys from the keyserver to a file or"
107 " files\n\t starting keydump*");
108 puts("\tget - retrieves the key requested from the keyserver");
109 puts("\tgetphoto - retrieves the first photoid on the given key and"
110 " dumps to\n\t stdout");
111 puts("\tindex - search for a key and list it");
112 puts("\tvindex - search for a key and list it and its signatures");
115 int main(int argc, char *argv[])
117 struct openpgp_packet_list *packets = NULL;
118 struct openpgp_packet_list *list_end = NULL;
119 struct openpgp_publickey *keys = NULL;
120 char *configfile = NULL;
121 int rc = EXIT_SUCCESS;
127 bool verbose = false;
130 bool fingerprint = false;
131 bool skshash = false;
133 struct dump_ctx dumpstate;
136 while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
142 configfile = strdup(optarg);
155 setlogthreshold(LOGTHING_INFO);
160 readconfig(configfile);
161 initlogthing("onak", config.logfile);
164 if ((argc - optind) < 1) {
166 } else if (!strcmp("dump", argv[optind])) {
167 config.dbbackend->initdb(true);
168 dumpstate.count = dumpstate.filenum = 0;
169 dumpstate.maxcount = 100000;
171 dumpstate.filebase = "keydump.%d.pgp";
172 config.dbbackend->iterate_keys(dump_func, &dumpstate);
173 if (dumpstate.fd != -1) {
177 config.dbbackend->cleanupdb();
178 } else if (!strcmp("add", argv[optind])) {
180 result = read_openpgp_stream(stdin_getchar, NULL,
182 logthing(LOGTHING_INFO,
183 "read_openpgp_stream: %d", result);
185 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
187 if (packets != NULL) {
188 result = parse_keys(packets, &keys);
189 free_packet_list(packets);
191 logthing(LOGTHING_INFO, "Finished reading %d keys.",
194 result = cleankeys(keys);
195 logthing(LOGTHING_INFO, "%d keys cleaned.",
198 config.dbbackend->initdb(false);
199 logthing(LOGTHING_NOTICE, "Got %d new keys.",
200 config.dbbackend->update_keys(&keys,
202 if (keys != NULL && update) {
203 flatten_publickey(keys,
207 write_openpgp_stream(stdout_putchar,
211 armor_openpgp_stream(stdout_putchar,
215 free_packet_list(packets);
218 config.dbbackend->cleanupdb();
221 logthing(LOGTHING_NOTICE, "No keys read.");
225 free_publickey(keys);
229 logthing(LOGTHING_NOTICE, "No changes.");
231 } else if (!strcmp("clean", argv[optind])) {
233 result = read_openpgp_stream(stdin_getchar, NULL,
235 logthing(LOGTHING_INFO,
236 "read_openpgp_stream: %d", result);
238 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
241 if (packets != NULL) {
242 result = parse_keys(packets, &keys);
243 free_packet_list(packets);
245 logthing(LOGTHING_INFO, "Finished reading %d keys.",
249 result = cleankeys(keys);
250 logthing(LOGTHING_INFO, "%d keys cleaned.",
253 flatten_publickey(keys,
258 write_openpgp_stream(stdout_putchar,
262 armor_openpgp_stream(stdout_putchar,
266 free_packet_list(packets);
271 logthing(LOGTHING_NOTICE, "No keys read.");
275 free_publickey(keys);
278 } else if ((argc - optind) == 2) {
279 search = argv[optind+1];
280 if (search != NULL) {
281 keyid = strtoul(search, &end, 16);
288 config.dbbackend->initdb(false);
289 if (!strcmp("index", argv[optind])) {
290 find_keys(search, keyid, ishex, fingerprint, skshash,
292 } else if (!strcmp("vindex", argv[optind])) {
293 find_keys(search, keyid, ishex, fingerprint, skshash,
295 } else if (!strcmp("getphoto", argv[optind])) {
297 puts("Can't get a key on uid text."
298 " You must supply a keyid.");
299 } else if (config.dbbackend->fetch_key(keyid, &keys,
301 unsigned char *photo = NULL;
304 if (getphoto(keys, 0, &photo, &length)) {
310 free_publickey(keys);
313 puts("Key not found");
315 } else if (!strcmp("delete", argv[optind])) {
316 config.dbbackend->delete_key(
317 config.dbbackend->getfullkeyid(keyid),
319 } else if (!strcmp("get", argv[optind])) {
321 puts("Can't get a key on uid text."
322 " You must supply a keyid.");
323 } else if (config.dbbackend->fetch_key(keyid, &keys,
325 logthing(LOGTHING_INFO, "Got key.");
326 flatten_publickey(keys,
329 free_publickey(keys);
331 write_openpgp_stream(stdout_putchar,
335 armor_openpgp_stream(stdout_putchar,
339 free_packet_list(packets);
342 puts("Key not found");
344 } else if (!strcmp("hget", argv[optind])) {
345 if (!parse_skshash(search, &hash)) {
346 puts("Couldn't parse sks hash.");
347 } else if (config.dbbackend->fetch_key_skshash(&hash,
349 logthing(LOGTHING_INFO, "Got key.");
350 flatten_publickey(keys,
353 free_publickey(keys);
355 write_openpgp_stream(stdout_putchar,
359 armor_openpgp_stream(stdout_putchar,
363 free_packet_list(packets);
366 puts("Key not found");
369 config.dbbackend->cleanupdb();