2 * onak.c - An OpenPGP keyserver.
4 * This is the main swiss army knife binary.
6 * Jonathan McDowell <noodles@earth.li>
8 * Copyright 2002 Project Purple
10 * $Id: onak.c,v 1.22 2004/05/31 14:16:49 noodles Exp $
20 #include "charfuncs.h"
25 #include "keystructs.h"
29 #include "onak-conf.h"
33 void find_keys(char *search, uint64_t keyid, bool ishex,
34 bool fingerprint, bool exact, bool verbose)
36 struct openpgp_publickey *publickey = NULL;
40 count = fetch_key(keyid, &publickey, false);
42 count = fetch_key_text(search, &publickey);
44 if (publickey != NULL) {
45 key_index(publickey, verbose, fingerprint, false);
46 free_publickey(publickey);
47 } else if (count == 0) {
48 puts("Key not found.");
50 printf("Found %d keys, but maximum number to return is %d.\n",
53 puts("Try again with a more specific search.");
58 puts("onak " VERSION " - an OpenPGP keyserver.\n");
60 puts("\tonak [options] <command> <parameters>\n");
61 puts("\tCommands:\n");
62 puts("\tadd - read armored OpenPGP keys from stdin and add to the"
64 puts("\tclean - read armored OpenPGP keys from stdin, run the "
65 " cleaning\n\t routines against them and dump to"
67 puts("\tdelete - delete a given key from the keyserver");
68 puts("\tdump - dump all the keys from the keyserver to a file or"
69 " files\n\t starting keydump*");
70 puts("\tget - retrieves the key requested from the keyserver");
71 puts("\tgetphoto - retrieves the first photoid on the given key and"
72 " dumps to\n\t stdout");
73 puts("\tindex - search for a key and list it");
74 puts("\tvindex - search for a key and list it and its signatures");
77 int main(int argc, char *argv[])
79 struct openpgp_packet_list *packets = NULL;
80 struct openpgp_packet_list *list_end = NULL;
81 struct openpgp_publickey *keys = NULL;
82 char *configfile = NULL;
83 int rc = EXIT_SUCCESS;
92 bool fingerprint = false;
95 while ((optchar = getopt(argc, argv, "bc:fuv")) != -1 ) {
101 configfile = strdup(optarg);
111 setlogthreshold(LOGTHING_INFO);
116 readconfig(configfile);
117 initlogthing("onak", config.logfile);
119 if ((argc - optind) < 1) {
121 } else if (!strcmp("dump", argv[optind])) {
125 } else if (!strcmp("add", argv[optind])) {
127 result = read_openpgp_stream(stdin_getchar, NULL,
129 logthing(LOGTHING_INFO,
130 "read_openpgp_stream: %d", result);
132 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
134 if (packets != NULL) {
135 result = parse_keys(packets, &keys);
136 free_packet_list(packets);
138 logthing(LOGTHING_INFO, "Finished reading %d keys.",
141 result = cleankeys(keys);
142 logthing(LOGTHING_INFO, "%d keys cleaned.",
146 logthing(LOGTHING_NOTICE, "Got %d new keys.",
148 if (keys != NULL && update) {
149 flatten_publickey(keys,
153 write_openpgp_stream(stdout_putchar,
157 armor_openpgp_stream(stdout_putchar,
161 free_packet_list(packets);
167 logthing(LOGTHING_NOTICE, "No keys read.");
171 free_publickey(keys);
175 logthing(LOGTHING_NOTICE, "No changes.");
177 } else if (!strcmp("clean", argv[optind])) {
179 result = read_openpgp_stream(stdin_getchar, NULL,
181 logthing(LOGTHING_INFO,
182 "read_openpgp_stream: %d", result);
184 dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
187 if (packets != NULL) {
188 result = parse_keys(packets, &keys);
189 free_packet_list(packets);
191 logthing(LOGTHING_INFO, "Finished reading %d keys.",
195 result = cleankeys(keys);
196 logthing(LOGTHING_INFO, "%d keys cleaned.",
199 flatten_publickey(keys,
204 write_openpgp_stream(stdout_putchar,
208 armor_openpgp_stream(stdout_putchar,
212 free_packet_list(packets);
217 logthing(LOGTHING_NOTICE, "No keys read.");
221 free_publickey(keys);
224 } else if ((argc - optind) == 2) {
225 search = argv[optind+1];
226 if (search != NULL) {
227 keyid = strtoul(search, &end, 16);
235 if (!strcmp("index", argv[optind])) {
236 find_keys(search, keyid, ishex, fingerprint,
238 } else if (!strcmp("vindex", argv[optind])) {
239 find_keys(search, keyid, ishex, fingerprint,
241 } else if (!strcmp("getphoto", argv[optind])) {
243 puts("Can't get a key on uid text."
244 " You must supply a keyid.");
245 } else if (fetch_key(keyid, &keys, false)) {
246 unsigned char *photo = NULL;
249 if (getphoto(keys, 0, &photo, &length)) {
255 free_publickey(keys);
258 puts("Key not found");
260 } else if (!strcmp("delete", argv[optind])) {
261 delete_key(getfullkeyid(keyid), false);
262 } else if (!strcmp("get", argv[optind])) {
264 puts("Can't get a key on uid text."
265 " You must supply a keyid.");
266 } else if (fetch_key(keyid, &keys, false)) {
267 logthing(LOGTHING_INFO, "Got key.");
268 flatten_publickey(keys,
271 free_publickey(keys);
272 armor_openpgp_stream(stdout_putchar,
275 free_packet_list(packets);
278 puts("Key not found");