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];
171 bool fingerprint = false;
172 bool skshash = false;
174 struct dump_ctx dumpstate;
176 struct onak_dbctx *dbctx;
178 while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
184 configfile = strdup(optarg);
196 setlogthreshold(LOGTHING_INFO);
201 readconfig(configfile);
202 initlogthing("onak", config.logfile);
205 if ((argc - optind) < 1) {
207 } else if (!strcmp("dump", argv[optind])) {
208 dbctx = config.dbinit(true);
209 dumpstate.count = dumpstate.filenum = 0;
210 dumpstate.maxcount = 100000;
212 dumpstate.filebase = "keydump.%d.pgp";
213 dbctx->iterate_keys(dbctx, dump_func, &dumpstate);
214 if (dumpstate.fd != -1) {
218 dbctx->cleanupdb(dbctx);
219 } else if (!strcmp("add", argv[optind])) {
221 result = read_openpgp_stream(stdin_getchar, NULL,
223 logthing(LOGTHING_INFO,
224 "read_openpgp_stream: %d", result);
226 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
228 if (packets != NULL) {
229 result = parse_keys(packets, &keys);
230 free_packet_list(packets);
232 logthing(LOGTHING_INFO, "Finished reading %d keys.",
235 result = cleankeys(keys);
236 logthing(LOGTHING_INFO, "%d keys cleaned.",
239 dbctx = config.dbinit(false);
240 logthing(LOGTHING_NOTICE, "Got %d new keys.",
241 dbctx->update_keys(dbctx, &keys,
243 if (keys != NULL && update) {
244 flatten_publickey(keys,
248 write_openpgp_stream(stdout_putchar,
252 armor_openpgp_stream(stdout_putchar,
256 free_packet_list(packets);
259 dbctx->cleanupdb(dbctx);
262 logthing(LOGTHING_NOTICE, "No keys read.");
266 free_publickey(keys);
270 logthing(LOGTHING_NOTICE, "No changes.");
272 } else if (!strcmp("clean", argv[optind])) {
274 result = read_openpgp_stream(stdin_getchar, NULL,
276 logthing(LOGTHING_INFO,
277 "read_openpgp_stream: %d", result);
279 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
282 if (packets != NULL) {
283 result = parse_keys(packets, &keys);
284 free_packet_list(packets);
286 logthing(LOGTHING_INFO, "Finished reading %d keys.",
290 result = cleankeys(keys);
291 logthing(LOGTHING_INFO, "%d keys cleaned.",
294 flatten_publickey(keys,
299 write_openpgp_stream(stdout_putchar,
303 armor_openpgp_stream(stdout_putchar,
307 free_packet_list(packets);
312 logthing(LOGTHING_NOTICE, "No keys read.");
316 free_publickey(keys);
319 } else if ((argc - optind) == 2) {
320 search = argv[optind+1];
321 if (search != NULL && strlen(search) == 42 &&
322 search[0] == '0' && search[1] == 'x') {
323 for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
324 fp[i] = (hex2bin(search[2 + i * 2]) << 4) +
325 hex2bin(search[3 + i * 2]);
328 } else if (search != NULL) {
329 keyid = strtoul(search, &end, 16);
336 dbctx = config.dbinit(false);
337 if (!strcmp("index", argv[optind])) {
338 find_keys(dbctx, search, keyid, fp, ishex, isfp,
339 fingerprint, skshash,
341 } else if (!strcmp("vindex", argv[optind])) {
342 find_keys(dbctx, search, keyid, fp, ishex, isfp,
343 fingerprint, skshash,
345 } else if (!strcmp("getphoto", argv[optind])) {
347 puts("Can't get a key on uid text."
348 " You must supply a keyid.");
349 } else if (dbctx->fetch_key_id(dbctx, keyid, &keys,
351 unsigned char *photo = NULL;
354 if (getphoto(keys, 0, &photo,
355 &length) == ONAK_E_OK) {
361 free_publickey(keys);
364 puts("Key not found");
366 } else if (!strcmp("delete", argv[optind])) {
367 dbctx->delete_key(dbctx,
368 dbctx->getfullkeyid(dbctx, keyid),
370 } else if (!strcmp("get", argv[optind])) {
371 if (!(ishex || isfp)) {
372 puts("Can't get a key on uid text."
373 " You must supply a keyid / "
376 dbctx->fetch_key_fp(dbctx, fp,
380 dbctx->fetch_key_id(dbctx, keyid,
382 logthing(LOGTHING_INFO, "Got key.");
383 flatten_publickey(keys,
386 free_publickey(keys);
388 write_openpgp_stream(stdout_putchar,
392 armor_openpgp_stream(stdout_putchar,
396 free_packet_list(packets);
399 puts("Key not found");
401 } else if (!strcmp("hget", argv[optind])) {
402 if (!parse_skshash(search, &hash)) {
403 puts("Couldn't parse sks hash.");
404 } else if (dbctx->fetch_key_skshash(dbctx, &hash,
406 logthing(LOGTHING_INFO, "Got key.");
407 flatten_publickey(keys,
410 free_publickey(keys);
412 write_openpgp_stream(stdout_putchar,
416 armor_openpgp_stream(stdout_putchar,
420 free_packet_list(packets);
423 puts("Key not found");
426 dbctx->cleanupdb(dbctx);