]> git.sommitrealweird.co.uk Git - onak.git/blob - onak.c
Remove bashism from postrm (Debian packaging)
[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 = fetch_key(keyid, &publickey, false);
44         } else {
45                 count = 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                 initdb(true);
163                 dumpstate.count = dumpstate.filenum = 0;
164                 dumpstate.maxcount = 1000000;
165                 dumpstate.fd = -1;
166                 dumpstate.filebase = "keydump.%d.pgp";
167                 iterate_keys(dump_func, &dumpstate);
168                 if (dumpstate.fd != -1) {
169                         close(dumpstate.fd);
170                         dumpstate.fd = -1;
171                 }
172                 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                         initdb(false);
194                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
195                                         update_keys(&keys, false));
196                         if (keys != NULL && update) {
197                                 flatten_publickey(keys,
198                                         &packets,
199                                         &list_end);
200                                 if (binary) {
201                                         write_openpgp_stream(stdout_putchar,
202                                                         NULL,
203                                                         packets);
204                                 } else {
205                                         armor_openpgp_stream(stdout_putchar,
206                                                 NULL,
207                                                 packets);
208                                 }
209                                 free_packet_list(packets);
210                                 packets = NULL;
211                         }
212                         cleanupdb();
213                 } else {
214                         rc = 1;
215                         logthing(LOGTHING_NOTICE, "No keys read.");
216                 }
217
218                 if (keys != NULL) {
219                         free_publickey(keys);
220                         keys = NULL;
221                 } else {
222                         rc = 1;
223                         logthing(LOGTHING_NOTICE, "No changes.");
224                 }
225         } else if (!strcmp("clean", argv[optind])) {
226                 if (binary) {
227                         result = read_openpgp_stream(stdin_getchar, NULL,
228                                  &packets, 0);
229                         logthing(LOGTHING_INFO,
230                                         "read_openpgp_stream: %d", result);
231                 } else {
232                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
233                 }
234
235                 if (packets != NULL) {
236                         result = parse_keys(packets, &keys);
237                         free_packet_list(packets);
238                         packets = NULL;
239                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
240                                         result);
241
242                         if (keys != NULL) {
243                                 result = cleankeys(keys);
244                                 logthing(LOGTHING_INFO, "%d keys cleaned.",
245                                                 result);
246
247                                 flatten_publickey(keys,
248                                         &packets,
249                                         &list_end);
250
251                                 if (binary) {
252                                         write_openpgp_stream(stdout_putchar,
253                                                         NULL,
254                                                         packets);
255                                 } else {
256                                         armor_openpgp_stream(stdout_putchar,
257                                                 NULL,
258                                                 packets);
259                                 }
260                                 free_packet_list(packets);
261                                 packets = NULL;
262                         }
263                 } else {
264                         rc = 1;
265                         logthing(LOGTHING_NOTICE, "No keys read.");
266                 }
267                 
268                 if (keys != NULL) {
269                         free_publickey(keys);
270                         keys = NULL;
271                 }
272         } else if ((argc - optind) == 2) {
273                 search = argv[optind+1];
274                 if (search != NULL) {
275                         keyid = strtoul(search, &end, 16);
276                         if (*search != 0 &&
277                                         end != NULL &&
278                                         *end == 0) {
279                                 ishex = true;
280                         }
281                 }
282                 initdb(false);
283                 if (!strcmp("index", argv[optind])) {
284                         find_keys(search, keyid, ishex, fingerprint,
285                                         false, false);
286                 } else if (!strcmp("vindex", argv[optind])) {
287                         find_keys(search, keyid, ishex, fingerprint,
288                                         false, true);
289                 } else if (!strcmp("getphoto", argv[optind])) {
290                         if (!ishex) {
291                                 puts("Can't get a key on uid text."
292                                         " You must supply a keyid.");
293                         } else if (fetch_key(keyid, &keys, false)) {
294                                 unsigned char *photo = NULL;
295                                 size_t         length = 0;
296
297                                 if (getphoto(keys, 0, &photo, &length)) {
298                                         fwrite(photo,
299                                                 1,
300                                                 length,
301                                                 stdout);
302                                 }
303                                 free_publickey(keys);
304                                 keys = NULL;
305                         } else {
306                                 puts("Key not found");
307                         }
308                 } else if (!strcmp("delete", argv[optind])) {
309                         delete_key(getfullkeyid(keyid), false);
310                 } else if (!strcmp("get", argv[optind])) {
311                         if (!ishex) {
312                                 puts("Can't get a key on uid text."
313                                         " You must supply a keyid.");
314                         } else if (fetch_key(keyid, &keys, false)) {
315                                 logthing(LOGTHING_INFO, "Got key.");
316                                 flatten_publickey(keys,
317                                                 &packets,
318                                                 &list_end);
319                                 free_publickey(keys);
320                                 armor_openpgp_stream(stdout_putchar,
321                                                 NULL,
322                                                 packets);
323                                 free_packet_list(packets);
324                                 packets = NULL;
325                         } else {
326                                 puts("Key not found");
327                         }
328                 }
329                 cleanupdb();
330         } else {
331                 usage();
332         }
333
334         cleanuplogthing();
335         cleanupconfig();
336
337         return rc;
338 }