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.");
72 * @brief Context for the keyserver dumping function
75 /** Keys we've dumped so far to this file */
77 /** Maximum keys to dump per file */
79 /** File descriptor for the current dump file */
81 /** Number of the current dump file */
83 /** Base filename to use for dump files */
87 void dump_func(void *ctx, struct openpgp_publickey *key)
89 struct openpgp_packet_list *packets = NULL;
90 struct openpgp_packet_list *list_end = NULL;
91 struct dump_ctx *state;
94 state = (struct dump_ctx *) ctx;
96 if (state->fd == -1 || state->count++ > state->maxcount) {
97 if (state->fd != -1) {
101 snprintf(filename, 1023, state->filebase, state->filenum);
102 state->fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
106 flatten_publickey(key, &packets, &list_end);
107 write_openpgp_stream(file_putchar, &state->fd, packets);
108 free_packet_list(packets);
109 packets = list_end = NULL;
115 puts("onak " ONAK_VERSION " - an OpenPGP keyserver.\n");
117 puts("\tonak [options] <command> <parameters>\n");
118 puts("\tCommands:\n");
119 puts("\tadd - read armored OpenPGP keys from stdin and add to the"
121 puts("\tclean - read armored OpenPGP keys from stdin, run the"
122 " cleaning\n\t routines against them and dump to"
124 puts("\tdelete - delete a given key from the keyserver");
125 puts("\tdump - dump all the keys from the keyserver to a file or"
126 " files\n\t starting keydump*");
127 puts("\tget - retrieves the key requested from the keyserver");
128 puts("\tgetphoto - retrieves the first photoid on the given key and"
129 " dumps to\n\t stdout");
130 puts("\tindex - search for a key and list it");
131 puts("\tvindex - search for a key and list it and its signatures");
134 int main(int argc, char *argv[])
136 struct openpgp_packet_list *packets = NULL;
137 struct openpgp_packet_list *list_end = NULL;
138 struct openpgp_publickey *keys = NULL;
139 char *configfile = NULL;
140 int rc = EXIT_SUCCESS;
146 bool verbose = false;
149 bool fingerprint = false;
150 bool skshash = false;
152 struct dump_ctx dumpstate;
155 while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
161 configfile = strdup(optarg);
174 setlogthreshold(LOGTHING_INFO);
179 readconfig(configfile);
180 initlogthing("onak", config.logfile);
183 if ((argc - optind) < 1) {
185 } else if (!strcmp("dump", argv[optind])) {
186 config.dbbackend->initdb(true);
187 dumpstate.count = dumpstate.filenum = 0;
188 dumpstate.maxcount = 100000;
190 dumpstate.filebase = "keydump.%d.pgp";
191 config.dbbackend->iterate_keys(dump_func, &dumpstate);
192 if (dumpstate.fd != -1) {
196 config.dbbackend->cleanupdb();
197 } else if (!strcmp("add", argv[optind])) {
199 result = read_openpgp_stream(stdin_getchar, NULL,
201 logthing(LOGTHING_INFO,
202 "read_openpgp_stream: %d", result);
204 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
206 if (packets != NULL) {
207 result = parse_keys(packets, &keys);
208 free_packet_list(packets);
210 logthing(LOGTHING_INFO, "Finished reading %d keys.",
213 result = cleankeys(keys);
214 logthing(LOGTHING_INFO, "%d keys cleaned.",
217 config.dbbackend->initdb(false);
218 logthing(LOGTHING_NOTICE, "Got %d new keys.",
219 config.dbbackend->update_keys(&keys,
221 if (keys != NULL && update) {
222 flatten_publickey(keys,
226 write_openpgp_stream(stdout_putchar,
230 armor_openpgp_stream(stdout_putchar,
234 free_packet_list(packets);
237 config.dbbackend->cleanupdb();
240 logthing(LOGTHING_NOTICE, "No keys read.");
244 free_publickey(keys);
248 logthing(LOGTHING_NOTICE, "No changes.");
250 } else if (!strcmp("clean", argv[optind])) {
252 result = read_openpgp_stream(stdin_getchar, NULL,
254 logthing(LOGTHING_INFO,
255 "read_openpgp_stream: %d", result);
257 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
260 if (packets != NULL) {
261 result = parse_keys(packets, &keys);
262 free_packet_list(packets);
264 logthing(LOGTHING_INFO, "Finished reading %d keys.",
268 result = cleankeys(keys);
269 logthing(LOGTHING_INFO, "%d keys cleaned.",
272 flatten_publickey(keys,
277 write_openpgp_stream(stdout_putchar,
281 armor_openpgp_stream(stdout_putchar,
285 free_packet_list(packets);
290 logthing(LOGTHING_NOTICE, "No keys read.");
294 free_publickey(keys);
297 } else if ((argc - optind) == 2) {
298 search = argv[optind+1];
299 if (search != NULL && strlen(search) == 42 &&
300 search[0] == '0' && search[1] == 'x') {
302 * Fingerprint. Truncate to last 64 bits for
305 keyid = strtoull(&search[26], &end, 16);
306 if (end != NULL && *end == 0) {
309 } else if (search != NULL) {
310 keyid = strtoul(search, &end, 16);
317 config.dbbackend->initdb(false);
318 if (!strcmp("index", argv[optind])) {
319 find_keys(search, keyid, ishex, fingerprint, skshash,
321 } else if (!strcmp("vindex", argv[optind])) {
322 find_keys(search, keyid, ishex, fingerprint, skshash,
324 } else if (!strcmp("getphoto", argv[optind])) {
326 puts("Can't get a key on uid text."
327 " You must supply a keyid.");
328 } else if (config.dbbackend->fetch_key(keyid, &keys,
330 unsigned char *photo = NULL;
333 if (getphoto(keys, 0, &photo, &length)) {
339 free_publickey(keys);
342 puts("Key not found");
344 } else if (!strcmp("delete", argv[optind])) {
345 config.dbbackend->delete_key(
346 config.dbbackend->getfullkeyid(keyid),
348 } else if (!strcmp("get", argv[optind])) {
350 puts("Can't get a key on uid text."
351 " You must supply a keyid.");
352 } else if (config.dbbackend->fetch_key(keyid, &keys,
354 logthing(LOGTHING_INFO, "Got key.");
355 flatten_publickey(keys,
358 free_publickey(keys);
360 write_openpgp_stream(stdout_putchar,
364 armor_openpgp_stream(stdout_putchar,
368 free_packet_list(packets);
371 puts("Key not found");
373 } else if (!strcmp("hget", argv[optind])) {
374 if (!parse_skshash(search, &hash)) {
375 puts("Couldn't parse sks hash.");
376 } else if (config.dbbackend->fetch_key_skshash(&hash,
378 logthing(LOGTHING_INFO, "Got key.");
379 flatten_publickey(keys,
382 free_publickey(keys);
384 write_openpgp_stream(stdout_putchar,
388 armor_openpgp_stream(stdout_putchar,
392 free_packet_list(packets);
395 puts("Key not found");
398 config.dbbackend->cleanupdb();