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(struct onak_dbctx *dbctx,
48 char *search, uint64_t keyid, uint8_t *fp, bool ishex,
49 bool isfp, bool fingerprint, bool skshash, bool exact,
52 struct openpgp_publickey *publickey = NULL;
56 count = dbctx->fetch_key_id(dbctx, keyid, &publickey,
59 count = dbctx->fetch_key_fp(dbctx, fp, MAX_FINGERPRINT_LEN,
62 count = dbctx->fetch_key_text(dbctx, search, &publickey);
64 if (publickey != NULL) {
65 key_index(dbctx, publickey, verbose, fingerprint, 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("\tvindex - search for a key and list it and its signatures");
154 int main(int argc, char *argv[])
156 struct openpgp_packet_list *packets = NULL;
157 struct openpgp_packet_list *list_end = NULL;
158 struct openpgp_publickey *keys = NULL;
159 char *configfile = NULL;
160 int rc = EXIT_SUCCESS;
165 uint8_t fp[MAX_FINGERPRINT_LEN];
169 bool verbose = false;
172 bool fingerprint = false;
173 bool skshash = false;
175 struct dump_ctx dumpstate;
177 struct onak_dbctx *dbctx;
179 while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
185 configfile = strdup(optarg);
198 setlogthreshold(LOGTHING_INFO);
203 readconfig(configfile);
204 initlogthing("onak", config.logfile);
207 if ((argc - optind) < 1) {
209 } else if (!strcmp("dump", argv[optind])) {
210 dbctx = config.dbinit(true);
211 dumpstate.count = dumpstate.filenum = 0;
212 dumpstate.maxcount = 100000;
214 dumpstate.filebase = "keydump.%d.pgp";
215 dbctx->iterate_keys(dbctx, dump_func, &dumpstate);
216 if (dumpstate.fd != -1) {
220 dbctx->cleanupdb(dbctx);
221 } else if (!strcmp("add", argv[optind])) {
223 result = read_openpgp_stream(stdin_getchar, NULL,
225 logthing(LOGTHING_INFO,
226 "read_openpgp_stream: %d", result);
228 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
230 if (packets != NULL) {
231 result = parse_keys(packets, &keys);
232 free_packet_list(packets);
234 logthing(LOGTHING_INFO, "Finished reading %d keys.",
237 result = cleankeys(keys);
238 logthing(LOGTHING_INFO, "%d keys cleaned.",
241 dbctx = config.dbinit(false);
242 logthing(LOGTHING_NOTICE, "Got %d new keys.",
243 dbctx->update_keys(dbctx, &keys,
245 if (keys != NULL && update) {
246 flatten_publickey(keys,
250 write_openpgp_stream(stdout_putchar,
254 armor_openpgp_stream(stdout_putchar,
258 free_packet_list(packets);
261 dbctx->cleanupdb(dbctx);
264 logthing(LOGTHING_NOTICE, "No keys read.");
268 free_publickey(keys);
272 logthing(LOGTHING_NOTICE, "No changes.");
274 } else if (!strcmp("clean", argv[optind])) {
276 result = read_openpgp_stream(stdin_getchar, NULL,
278 logthing(LOGTHING_INFO,
279 "read_openpgp_stream: %d", result);
281 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
284 if (packets != NULL) {
285 result = parse_keys(packets, &keys);
286 free_packet_list(packets);
288 logthing(LOGTHING_INFO, "Finished reading %d keys.",
292 result = cleankeys(keys);
293 logthing(LOGTHING_INFO, "%d keys cleaned.",
296 flatten_publickey(keys,
301 write_openpgp_stream(stdout_putchar,
305 armor_openpgp_stream(stdout_putchar,
309 free_packet_list(packets);
314 logthing(LOGTHING_NOTICE, "No keys read.");
318 free_publickey(keys);
321 } else if ((argc - optind) == 2) {
322 search = argv[optind+1];
323 if (search != NULL && strlen(search) == 42 &&
324 search[0] == '0' && search[1] == 'x') {
325 for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
326 fp[i] = (hex2bin(search[2 + i * 2]) << 4) +
327 hex2bin(search[3 + i * 2]);
330 } else if (search != NULL) {
331 keyid = strtoul(search, &end, 16);
338 dbctx = config.dbinit(false);
339 if (!strcmp("index", argv[optind])) {
340 find_keys(dbctx, search, keyid, fp, ishex, isfp,
341 fingerprint, skshash,
343 } else if (!strcmp("vindex", argv[optind])) {
344 find_keys(dbctx, search, keyid, fp, ishex, isfp,
345 fingerprint, skshash,
347 } else if (!strcmp("getphoto", argv[optind])) {
349 puts("Can't get a key on uid text."
350 " You must supply a keyid.");
351 } else if (dbctx->fetch_key_id(dbctx, keyid, &keys,
353 unsigned char *photo = NULL;
356 if (getphoto(keys, 0, &photo,
357 &length) == ONAK_E_OK) {
363 free_publickey(keys);
366 puts("Key not found");
368 } else if (!strcmp("delete", argv[optind])) {
369 dbctx->delete_key(dbctx,
370 dbctx->getfullkeyid(dbctx, keyid),
372 } else if (!strcmp("get", argv[optind])) {
373 if (!(ishex || isfp)) {
374 puts("Can't get a key on uid text."
375 " You must supply a keyid / "
378 dbctx->fetch_key_fp(dbctx, fp,
382 dbctx->fetch_key_id(dbctx, keyid,
384 logthing(LOGTHING_INFO, "Got key.");
385 flatten_publickey(keys,
388 free_publickey(keys);
390 write_openpgp_stream(stdout_putchar,
394 armor_openpgp_stream(stdout_putchar,
398 free_packet_list(packets);
401 puts("Key not found");
403 } else if (!strcmp("hget", argv[optind])) {
404 if (!parse_skshash(search, &hash)) {
405 puts("Couldn't parse sks hash.");
406 } else if (dbctx->fetch_key_skshash(dbctx, &hash,
408 logthing(LOGTHING_INFO, "Got key.");
409 flatten_publickey(keys,
412 free_publickey(keys);
414 write_openpgp_stream(stdout_putchar,
418 armor_openpgp_stream(stdout_putchar,
422 free_packet_list(packets);
425 puts("Key not found");
428 dbctx->cleanupdb(dbctx);