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, uint8_t *fp, bool ishex,
48 bool isfp, bool fingerprint, bool skshash, bool exact,
51 struct openpgp_publickey *publickey = NULL;
55 count = config.dbbackend->fetch_key_id(keyid, &publickey,
58 count = config.dbbackend->fetch_key_fp(fp, MAX_FINGERPRINT_LEN,
61 count = config.dbbackend->fetch_key_text(search, &publickey);
63 if (publickey != NULL) {
64 key_index(publickey, verbose, fingerprint, skshash, false);
65 free_publickey(publickey);
66 } else if (count == 0) {
67 puts("Key not found.");
69 printf("Found %d keys, but maximum number to return is %d.\n",
72 puts("Try again with a more specific search.");
77 * @brief Context for the keyserver dumping function
80 /** Keys we've dumped so far to this file */
82 /** Maximum keys to dump per file */
84 /** File descriptor for the current dump file */
86 /** Number of the current dump file */
88 /** Base filename to use for dump files */
92 void dump_func(void *ctx, struct openpgp_publickey *key)
94 struct openpgp_packet_list *packets = NULL;
95 struct openpgp_packet_list *list_end = NULL;
96 struct dump_ctx *state;
99 state = (struct dump_ctx *) ctx;
101 if (state->fd == -1 || state->count++ > state->maxcount) {
102 if (state->fd != -1) {
106 snprintf(filename, 1023, state->filebase, state->filenum);
107 state->fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
111 flatten_publickey(key, &packets, &list_end);
112 write_openpgp_stream(file_putchar, &state->fd, packets);
113 free_packet_list(packets);
114 packets = list_end = NULL;
119 static uint8_t hex2bin(char c)
121 if (c >= '0' && c <= '9') {
123 } else if (c >= 'a' && c <= 'f') {
124 return (c - 'a' + 10);
125 } else if (c >= 'A' && c <= 'F') {
126 return (c - 'A' + 10);
133 puts("onak " ONAK_VERSION " - an OpenPGP keyserver.\n");
135 puts("\tonak [options] <command> <parameters>\n");
136 puts("\tCommands:\n");
137 puts("\tadd - read armored OpenPGP keys from stdin and add to the"
139 puts("\tclean - read armored OpenPGP keys from stdin, run the"
140 " cleaning\n\t routines against them and dump to"
142 puts("\tdelete - delete a given key from the keyserver");
143 puts("\tdump - dump all the keys from the keyserver to a file or"
144 " files\n\t starting keydump*");
145 puts("\tget - retrieves the key requested from the keyserver");
146 puts("\tgetphoto - retrieves the first photoid on the given key and"
147 " dumps to\n\t stdout");
148 puts("\tindex - search for a key and list it");
149 puts("\tvindex - search for a key and list it and its signatures");
152 int main(int argc, char *argv[])
154 struct openpgp_packet_list *packets = NULL;
155 struct openpgp_packet_list *list_end = NULL;
156 struct openpgp_publickey *keys = NULL;
157 char *configfile = NULL;
158 int rc = EXIT_SUCCESS;
163 uint8_t fp[MAX_FINGERPRINT_LEN];
167 bool verbose = false;
170 bool fingerprint = false;
171 bool skshash = false;
173 struct dump_ctx dumpstate;
176 while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
182 configfile = strdup(optarg);
195 setlogthreshold(LOGTHING_INFO);
200 readconfig(configfile);
201 initlogthing("onak", config.logfile);
204 if ((argc - optind) < 1) {
206 } else if (!strcmp("dump", argv[optind])) {
207 config.dbbackend->initdb(true);
208 dumpstate.count = dumpstate.filenum = 0;
209 dumpstate.maxcount = 100000;
211 dumpstate.filebase = "keydump.%d.pgp";
212 config.dbbackend->iterate_keys(dump_func, &dumpstate);
213 if (dumpstate.fd != -1) {
217 config.dbbackend->cleanupdb();
218 } else if (!strcmp("add", argv[optind])) {
220 result = read_openpgp_stream(stdin_getchar, NULL,
222 logthing(LOGTHING_INFO,
223 "read_openpgp_stream: %d", result);
225 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
227 if (packets != NULL) {
228 result = parse_keys(packets, &keys);
229 free_packet_list(packets);
231 logthing(LOGTHING_INFO, "Finished reading %d keys.",
234 result = cleankeys(keys);
235 logthing(LOGTHING_INFO, "%d keys cleaned.",
238 config.dbbackend->initdb(false);
239 logthing(LOGTHING_NOTICE, "Got %d new keys.",
240 config.dbbackend->update_keys(&keys,
242 if (keys != NULL && update) {
243 flatten_publickey(keys,
247 write_openpgp_stream(stdout_putchar,
251 armor_openpgp_stream(stdout_putchar,
255 free_packet_list(packets);
258 config.dbbackend->cleanupdb();
261 logthing(LOGTHING_NOTICE, "No keys read.");
265 free_publickey(keys);
269 logthing(LOGTHING_NOTICE, "No changes.");
271 } else if (!strcmp("clean", argv[optind])) {
273 result = read_openpgp_stream(stdin_getchar, NULL,
275 logthing(LOGTHING_INFO,
276 "read_openpgp_stream: %d", result);
278 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
281 if (packets != NULL) {
282 result = parse_keys(packets, &keys);
283 free_packet_list(packets);
285 logthing(LOGTHING_INFO, "Finished reading %d keys.",
289 result = cleankeys(keys);
290 logthing(LOGTHING_INFO, "%d keys cleaned.",
293 flatten_publickey(keys,
298 write_openpgp_stream(stdout_putchar,
302 armor_openpgp_stream(stdout_putchar,
306 free_packet_list(packets);
311 logthing(LOGTHING_NOTICE, "No keys read.");
315 free_publickey(keys);
318 } else if ((argc - optind) == 2) {
319 search = argv[optind+1];
320 if (search != NULL && strlen(search) == 42 &&
321 search[0] == '0' && search[1] == 'x') {
322 for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
323 fp[i] = (hex2bin(search[2 + i * 2]) << 4) +
324 hex2bin(search[3 + i * 2]);
327 } else if (search != NULL) {
328 keyid = strtoul(search, &end, 16);
335 config.dbbackend->initdb(false);
336 if (!strcmp("index", argv[optind])) {
337 find_keys(search, keyid, fp, ishex, isfp,
338 fingerprint, skshash,
340 } else if (!strcmp("vindex", argv[optind])) {
341 find_keys(search, keyid, fp, ishex, isfp,
342 fingerprint, skshash,
344 } else if (!strcmp("getphoto", argv[optind])) {
346 puts("Can't get a key on uid text."
347 " You must supply a keyid.");
348 } else if (config.dbbackend->fetch_key_id(keyid, &keys,
350 unsigned char *photo = NULL;
353 if (getphoto(keys, 0, &photo,
354 &length) == ONAK_E_OK) {
360 free_publickey(keys);
363 puts("Key not found");
365 } else if (!strcmp("delete", argv[optind])) {
366 config.dbbackend->delete_key(
367 config.dbbackend->getfullkeyid(keyid),
369 } else if (!strcmp("get", argv[optind])) {
370 if (!(ishex || isfp)) {
371 puts("Can't get a key on uid text."
372 " You must supply a keyid / "
375 config.dbbackend->fetch_key_fp(fp,
379 config.dbbackend->fetch_key_id(keyid,
381 logthing(LOGTHING_INFO, "Got key.");
382 flatten_publickey(keys,
385 free_publickey(keys);
387 write_openpgp_stream(stdout_putchar,
391 armor_openpgp_stream(stdout_putchar,
395 free_packet_list(packets);
398 puts("Key not found");
400 } else if (!strcmp("hget", argv[optind])) {
401 if (!parse_skshash(search, &hash)) {
402 puts("Couldn't parse sks hash.");
403 } else if (config.dbbackend->fetch_key_skshash(&hash,
405 logthing(LOGTHING_INFO, "Got key.");
406 flatten_publickey(keys,
409 free_publickey(keys);
411 write_openpgp_stream(stdout_putchar,
415 armor_openpgp_stream(stdout_putchar,
419 free_packet_list(packets);
422 puts("Key not found");
425 config.dbbackend->cleanupdb();