]> git.sommitrealweird.co.uk Git - onak.git/blob - onak.c
Fix Debian changelog versions for real 0.3.5 release.
[onak.git] / onak.c
1 /*
2  * onak.c - An OpenPGP keyserver.
3  *
4  * This is the main swiss army knife binary.
5  *
6  * Jonathan McDowell <noodles@earth.li>
7  * 
8  * Copyright 2002 Project Purple
9  */
10
11 #include <fcntl.h>
12 #include <getopt.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19
20 #include "armor.h"
21 #include "charfuncs.h"
22 #include "cleankey.h"
23 #include "cleanup.h"
24 #include "config.h"
25 #include "keydb.h"
26 #include "keyid.h"
27 #include "keyindex.h"
28 #include "keystructs.h"
29 #include "log.h"
30 #include "mem.h"
31 #include "merge.h"
32 #include "onak-conf.h"
33 #include "parsekey.h"
34 #include "photoid.h"
35
36 void find_keys(char *search, uint64_t keyid, bool ishex,
37                 bool fingerprint, bool exact, bool verbose)
38 {
39         struct openpgp_publickey *publickey = NULL;
40         int count = 0;
41
42         if (ishex) {
43                 count = config.dbbackend->fetch_key(keyid, &publickey, false);
44         } else {
45                 count = config.dbbackend->fetch_key_text(search, &publickey);
46         }
47         if (publickey != NULL) {
48                 key_index(publickey, verbose, fingerprint, false);
49                 free_publickey(publickey);
50         } else if (count == 0) {
51                 puts("Key not found.");
52         } else {
53                 printf("Found %d keys, but maximum number to return is %d.\n",
54                                 count,
55                                 config.maxkeys);
56                 puts("Try again with a more specific search.");
57         }
58 }
59
60 struct dump_ctx {
61         int count;
62         int maxcount;
63         int fd;
64         int filenum;
65         char *filebase;
66 };
67
68 void dump_func(void *ctx, struct openpgp_publickey *key)
69 {
70         struct openpgp_packet_list *packets = NULL;
71         struct openpgp_packet_list *list_end = NULL;
72         struct dump_ctx *state;
73         char filename[1024];
74
75         state = (struct dump_ctx *) ctx;
76
77         if (state->fd == -1 || state->count > state->maxcount) {
78                 if (state->fd != -1) {
79                         close(state->fd);
80                         state->fd = -1;
81                 }
82                 snprintf(filename, 1023, state->filebase, state->filenum);
83                 state->fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
84                 state->filenum++;
85                 state->count = 0;
86         }
87         flatten_publickey(key, &packets, &list_end);
88         write_openpgp_stream(file_putchar, &state->fd, packets);
89         free_packet_list(packets);
90         packets = list_end = NULL;
91
92         return;
93 }
94
95 void usage(void) {
96         puts("onak " PACKAGE_VERSION " - an OpenPGP keyserver.\n");
97         puts("Usage:\n");
98         puts("\tonak [options] <command> <parameters>\n");
99         puts("\tCommands:\n");
100         puts("\tadd      - read armored OpenPGP keys from stdin and add to the"
101                 " keyserver");
102         puts("\tclean    - read armored OpenPGP keys from stdin, run the"
103                 " cleaning\n\t             routines against them and dump to"
104                 " stdout");
105         puts("\tdelete   - delete a given key from the keyserver");
106         puts("\tdump     - dump all the keys from the keyserver to a file or"
107                 " files\n\t           starting keydump*");
108         puts("\tget      - retrieves the key requested from the keyserver");
109         puts("\tgetphoto - retrieves the first photoid on the given key and"
110                 " dumps to\n\t           stdout");
111         puts("\tindex    - search for a key and list it");
112         puts("\tvindex   - search for a key and list it and its signatures");
113 }
114
115 int main(int argc, char *argv[])
116 {
117         struct openpgp_packet_list      *packets = NULL;
118         struct openpgp_packet_list      *list_end = NULL;
119         struct openpgp_publickey        *keys = NULL;
120         char                            *configfile = NULL;
121         int                              rc = EXIT_SUCCESS;
122         int                              result = 0;
123         char                            *search = NULL;
124         char                            *end = NULL;
125         uint64_t                         keyid = 0;
126         bool                             ishex = false;
127         bool                             verbose = false;
128         bool                             update = false;
129         bool                             binary = false;
130         bool                             fingerprint = false;
131         int                              optchar;
132         struct dump_ctx                  dumpstate;
133
134         while ((optchar = getopt(argc, argv, "bc:fuv")) != -1 ) {
135                 switch (optchar) {
136                 case 'b': 
137                         binary = true;
138                         break;
139                 case 'c':
140                         configfile = strdup(optarg);
141                         break;
142                 case 'f': 
143                         fingerprint = true;
144                         break;
145                 case 'u': 
146                         update = true;
147                         break;
148                 case 'v': 
149                         verbose = true;
150                         setlogthreshold(LOGTHING_INFO);
151                         break;
152                 }
153         }
154
155         readconfig(configfile);
156         initlogthing("onak", config.logfile);
157         catchsignals();
158
159         if ((argc - optind) < 1) {
160                 usage();
161         } else if (!strcmp("dump", argv[optind])) {
162                 config.dbbackend->initdb(true);
163                 dumpstate.count = dumpstate.filenum = 0;
164                 dumpstate.maxcount = 1000000;
165                 dumpstate.fd = -1;
166                 dumpstate.filebase = "keydump.%d.pgp";
167                 config.dbbackend->iterate_keys(dump_func, &dumpstate);
168                 if (dumpstate.fd != -1) {
169                         close(dumpstate.fd);
170                         dumpstate.fd = -1;
171                 }
172                 config.dbbackend->cleanupdb();
173         } else if (!strcmp("add", argv[optind])) {
174                 if (binary) {
175                         result = read_openpgp_stream(stdin_getchar, NULL,
176                                  &packets, 0);
177                         logthing(LOGTHING_INFO,
178                                         "read_openpgp_stream: %d", result);
179                 } else {
180                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
181                 }
182                 if (packets != NULL) {
183                         result = parse_keys(packets, &keys);
184                         free_packet_list(packets);
185                         packets = NULL;
186                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
187                                         result);
188
189                         result = cleankeys(keys);
190                         logthing(LOGTHING_INFO, "%d keys cleaned.",
191                                         result);
192
193                         config.dbbackend->initdb(false);
194                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
195                                         config.dbbackend->update_keys(&keys,
196                                         false));
197                         if (keys != NULL && update) {
198                                 flatten_publickey(keys,
199                                         &packets,
200                                         &list_end);
201                                 if (binary) {
202                                         write_openpgp_stream(stdout_putchar,
203                                                         NULL,
204                                                         packets);
205                                 } else {
206                                         armor_openpgp_stream(stdout_putchar,
207                                                 NULL,
208                                                 packets);
209                                 }
210                                 free_packet_list(packets);
211                                 packets = NULL;
212                         }
213                         config.dbbackend->cleanupdb();
214                 } else {
215                         rc = 1;
216                         logthing(LOGTHING_NOTICE, "No keys read.");
217                 }
218
219                 if (keys != NULL) {
220                         free_publickey(keys);
221                         keys = NULL;
222                 } else {
223                         rc = 1;
224                         logthing(LOGTHING_NOTICE, "No changes.");
225                 }
226         } else if (!strcmp("clean", argv[optind])) {
227                 if (binary) {
228                         result = read_openpgp_stream(stdin_getchar, NULL,
229                                  &packets, 0);
230                         logthing(LOGTHING_INFO,
231                                         "read_openpgp_stream: %d", result);
232                 } else {
233                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
234                 }
235
236                 if (packets != NULL) {
237                         result = parse_keys(packets, &keys);
238                         free_packet_list(packets);
239                         packets = NULL;
240                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
241                                         result);
242
243                         if (keys != NULL) {
244                                 result = cleankeys(keys);
245                                 logthing(LOGTHING_INFO, "%d keys cleaned.",
246                                                 result);
247
248                                 flatten_publickey(keys,
249                                         &packets,
250                                         &list_end);
251
252                                 if (binary) {
253                                         write_openpgp_stream(stdout_putchar,
254                                                         NULL,
255                                                         packets);
256                                 } else {
257                                         armor_openpgp_stream(stdout_putchar,
258                                                 NULL,
259                                                 packets);
260                                 }
261                                 free_packet_list(packets);
262                                 packets = NULL;
263                         }
264                 } else {
265                         rc = 1;
266                         logthing(LOGTHING_NOTICE, "No keys read.");
267                 }
268                 
269                 if (keys != NULL) {
270                         free_publickey(keys);
271                         keys = NULL;
272                 }
273         } else if ((argc - optind) == 2) {
274                 search = argv[optind+1];
275                 if (search != NULL) {
276                         keyid = strtoul(search, &end, 16);
277                         if (*search != 0 &&
278                                         end != NULL &&
279                                         *end == 0) {
280                                 ishex = true;
281                         }
282                 }
283                 config.dbbackend->initdb(false);
284                 if (!strcmp("index", argv[optind])) {
285                         find_keys(search, keyid, ishex, fingerprint,
286                                         false, false);
287                 } else if (!strcmp("vindex", argv[optind])) {
288                         find_keys(search, keyid, ishex, fingerprint,
289                                         false, true);
290                 } else if (!strcmp("getphoto", argv[optind])) {
291                         if (!ishex) {
292                                 puts("Can't get a key on uid text."
293                                         " You must supply a keyid.");
294                         } else if (config.dbbackend->fetch_key(keyid, &keys,
295                                         false)) {
296                                 unsigned char *photo = NULL;
297                                 size_t         length = 0;
298
299                                 if (getphoto(keys, 0, &photo, &length)) {
300                                         fwrite(photo,
301                                                 1,
302                                                 length,
303                                                 stdout);
304                                 }
305                                 free_publickey(keys);
306                                 keys = NULL;
307                         } else {
308                                 puts("Key not found");
309                         }
310                 } else if (!strcmp("delete", argv[optind])) {
311                         config.dbbackend->delete_key(
312                                         config.dbbackend->getfullkeyid(keyid),
313                                         false);
314                 } else if (!strcmp("get", argv[optind])) {
315                         if (!ishex) {
316                                 puts("Can't get a key on uid text."
317                                         " You must supply a keyid.");
318                         } else if (config.dbbackend->fetch_key(keyid, &keys,
319                                         false)) {
320                                 logthing(LOGTHING_INFO, "Got key.");
321                                 flatten_publickey(keys,
322                                                 &packets,
323                                                 &list_end);
324                                 free_publickey(keys);
325                                 armor_openpgp_stream(stdout_putchar,
326                                                 NULL,
327                                                 packets);
328                                 free_packet_list(packets);
329                                 packets = NULL;
330                         } else {
331                                 puts("Key not found");
332                         }
333                 }
334                 config.dbbackend->cleanupdb();
335         } else {
336                 usage();
337         }
338
339         cleanuplogthing();
340         cleanupconfig();
341
342         return rc;
343 }