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, write to the Free Software Foundation, Inc., 51
19 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <sys/types.h>
32 #include "charfuncs.h"
38 #include "keystructs.h"
42 #include "onak-conf.h"
47 void find_keys(char *search, uint64_t keyid, bool ishex,
48 bool fingerprint, bool skshash, bool exact, bool verbose)
50 struct openpgp_publickey *publickey = NULL;
54 count = config.dbbackend->fetch_key(keyid, &publickey, false);
56 count = config.dbbackend->fetch_key_text(search, &publickey);
58 if (publickey != NULL) {
59 key_index(publickey, verbose, fingerprint, skshash, false);
60 free_publickey(publickey);
61 } else if (count == 0) {
62 puts("Key not found.");
64 printf("Found %d keys, but maximum number to return is %d.\n",
67 puts("Try again with a more specific search.");
79 void dump_func(void *ctx, struct openpgp_publickey *key)
81 struct openpgp_packet_list *packets = NULL;
82 struct openpgp_packet_list *list_end = NULL;
83 struct dump_ctx *state;
86 state = (struct dump_ctx *) ctx;
88 if (state->fd == -1 || state->count++ > state->maxcount) {
89 if (state->fd != -1) {
93 snprintf(filename, 1023, state->filebase, state->filenum);
94 state->fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
98 flatten_publickey(key, &packets, &list_end);
99 write_openpgp_stream(file_putchar, &state->fd, packets);
100 free_packet_list(packets);
101 packets = list_end = NULL;
107 puts("onak " ONAK_VERSION " - an OpenPGP keyserver.\n");
109 puts("\tonak [options] <command> <parameters>\n");
110 puts("\tCommands:\n");
111 puts("\tadd - read armored OpenPGP keys from stdin and add to the"
113 puts("\tclean - read armored OpenPGP keys from stdin, run the"
114 " cleaning\n\t routines against them and dump to"
116 puts("\tdelete - delete a given key from the keyserver");
117 puts("\tdump - dump all the keys from the keyserver to a file or"
118 " files\n\t starting keydump*");
119 puts("\tget - retrieves the key requested from the keyserver");
120 puts("\tgetphoto - retrieves the first photoid on the given key and"
121 " dumps to\n\t stdout");
122 puts("\tindex - search for a key and list it");
123 puts("\tvindex - search for a key and list it and its signatures");
126 int main(int argc, char *argv[])
128 struct openpgp_packet_list *packets = NULL;
129 struct openpgp_packet_list *list_end = NULL;
130 struct openpgp_publickey *keys = NULL;
131 char *configfile = NULL;
132 int rc = EXIT_SUCCESS;
138 bool verbose = false;
141 bool fingerprint = false;
142 bool skshash = false;
144 struct dump_ctx dumpstate;
147 while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
153 configfile = strdup(optarg);
166 setlogthreshold(LOGTHING_INFO);
171 readconfig(configfile);
172 initlogthing("onak", config.logfile);
175 if ((argc - optind) < 1) {
177 } else if (!strcmp("dump", argv[optind])) {
178 config.dbbackend->initdb(true);
179 dumpstate.count = dumpstate.filenum = 0;
180 dumpstate.maxcount = 100000;
182 dumpstate.filebase = "keydump.%d.pgp";
183 config.dbbackend->iterate_keys(dump_func, &dumpstate);
184 if (dumpstate.fd != -1) {
188 config.dbbackend->cleanupdb();
189 } else if (!strcmp("add", argv[optind])) {
191 result = read_openpgp_stream(stdin_getchar, NULL,
193 logthing(LOGTHING_INFO,
194 "read_openpgp_stream: %d", result);
196 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
198 if (packets != NULL) {
199 result = parse_keys(packets, &keys);
200 free_packet_list(packets);
202 logthing(LOGTHING_INFO, "Finished reading %d keys.",
205 result = cleankeys(keys);
206 logthing(LOGTHING_INFO, "%d keys cleaned.",
209 config.dbbackend->initdb(false);
210 logthing(LOGTHING_NOTICE, "Got %d new keys.",
211 config.dbbackend->update_keys(&keys,
213 if (keys != NULL && update) {
214 flatten_publickey(keys,
218 write_openpgp_stream(stdout_putchar,
222 armor_openpgp_stream(stdout_putchar,
226 free_packet_list(packets);
229 config.dbbackend->cleanupdb();
232 logthing(LOGTHING_NOTICE, "No keys read.");
236 free_publickey(keys);
240 logthing(LOGTHING_NOTICE, "No changes.");
242 } else if (!strcmp("clean", argv[optind])) {
244 result = read_openpgp_stream(stdin_getchar, NULL,
246 logthing(LOGTHING_INFO,
247 "read_openpgp_stream: %d", result);
249 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
252 if (packets != NULL) {
253 result = parse_keys(packets, &keys);
254 free_packet_list(packets);
256 logthing(LOGTHING_INFO, "Finished reading %d keys.",
260 result = cleankeys(keys);
261 logthing(LOGTHING_INFO, "%d keys cleaned.",
264 flatten_publickey(keys,
269 write_openpgp_stream(stdout_putchar,
273 armor_openpgp_stream(stdout_putchar,
277 free_packet_list(packets);
282 logthing(LOGTHING_NOTICE, "No keys read.");
286 free_publickey(keys);
289 } else if ((argc - optind) == 2) {
290 search = argv[optind+1];
291 if (search != NULL && strlen(search) == 42 &&
292 search[0] == '0' && search[1] == 'x') {
294 * Fingerprint. Truncate to last 64 bits for
297 keyid = strtoull(&search[26], &end, 16);
298 if (end != NULL && *end == 0) {
301 } else if (search != NULL) {
302 keyid = strtoul(search, &end, 16);
309 config.dbbackend->initdb(false);
310 if (!strcmp("index", argv[optind])) {
311 find_keys(search, keyid, ishex, fingerprint, skshash,
313 } else if (!strcmp("vindex", argv[optind])) {
314 find_keys(search, keyid, ishex, fingerprint, skshash,
316 } else if (!strcmp("getphoto", argv[optind])) {
318 puts("Can't get a key on uid text."
319 " You must supply a keyid.");
320 } else if (config.dbbackend->fetch_key(keyid, &keys,
322 unsigned char *photo = NULL;
325 if (getphoto(keys, 0, &photo, &length)) {
331 free_publickey(keys);
334 puts("Key not found");
336 } else if (!strcmp("delete", argv[optind])) {
337 config.dbbackend->delete_key(
338 config.dbbackend->getfullkeyid(keyid),
340 } else if (!strcmp("get", argv[optind])) {
342 puts("Can't get a key on uid text."
343 " You must supply a keyid.");
344 } else if (config.dbbackend->fetch_key(keyid, &keys,
346 logthing(LOGTHING_INFO, "Got key.");
347 flatten_publickey(keys,
350 free_publickey(keys);
352 write_openpgp_stream(stdout_putchar,
356 armor_openpgp_stream(stdout_putchar,
360 free_packet_list(packets);
363 puts("Key not found");
365 } else if (!strcmp("hget", argv[optind])) {
366 if (!parse_skshash(search, &hash)) {
367 puts("Couldn't parse sks hash.");
368 } else if (config.dbbackend->fetch_key_skshash(&hash,
370 logthing(LOGTHING_INFO, "Got key.");
371 flatten_publickey(keys,
374 free_publickey(keys);
376 write_openpgp_stream(stdout_putchar,
380 armor_openpgp_stream(stdout_putchar,
384 free_packet_list(packets);
387 puts("Key not found");
390 config.dbbackend->cleanupdb();