Various compiler warning cleanups.
[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  * $Id: onak.c,v 1.22 2004/05/31 14:16:49 noodles Exp $
11  */
12
13 #include <getopt.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 #include "armor.h"
20 #include "charfuncs.h"
21 #include "cleankey.h"
22 #include "keydb.h"
23 #include "keyid.h"
24 #include "keyindex.h"
25 #include "keystructs.h"
26 #include "log.h"
27 #include "mem.h"
28 #include "merge.h"
29 #include "onak-conf.h"
30 #include "parsekey.h"
31 #include "photoid.h"
32
33 void find_keys(char *search, uint64_t keyid, bool ishex,
34                 bool fingerprint, bool exact, bool verbose)
35 {
36         struct openpgp_publickey *publickey = NULL;
37         int count = 0;
38
39         if (ishex) {
40                 count = fetch_key(keyid, &publickey, false);
41         } else {
42                 count = fetch_key_text(search, &publickey);
43         }
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.");
49         } else {
50                 printf("Found %d keys, but maximum number to return is %d.\n",
51                                 count,
52                                 config.maxkeys);
53                 puts("Try again with a more specific search.");
54         }
55 }
56
57 void usage(void) {
58         puts("onak " VERSION " - an OpenPGP keyserver.\n");
59         puts("Usage:\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"
63                 " keyserver");
64         puts("\tclean    - read armored OpenPGP keys from stdin, run the "
65                 " cleaning\n\t             routines against them and dump to"
66                 " stdout");
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");
75 }
76
77 int main(int argc, char *argv[])
78 {
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;
84         int                              result = 0;
85         char                            *search = NULL;
86         char                            *end = NULL;
87         uint64_t                         keyid = 0;
88         bool                             ishex = false;
89         bool                             verbose = false;
90         bool                             update = false;
91         bool                             binary = false;
92         bool                             fingerprint = false;
93         int                              optchar;
94
95         while ((optchar = getopt(argc, argv, "bc:fuv")) != -1 ) {
96                 switch (optchar) {
97                 case 'b': 
98                         binary = true;
99                         break;
100                 case 'c':
101                         configfile = strdup(optarg);
102                         break;
103                 case 'f': 
104                         fingerprint = true;
105                         break;
106                 case 'u': 
107                         update = true;
108                         break;
109                 case 'v': 
110                         verbose = true;
111                         setlogthreshold(LOGTHING_INFO);
112                         break;
113                 }
114         }
115
116         readconfig(configfile);
117         initlogthing("onak", config.logfile);
118
119         if ((argc - optind) < 1) {
120                 usage();
121         } else if (!strcmp("dump", argv[optind])) {
122                 initdb(true);
123                 dumpdb("keydump");
124                 cleanupdb();
125         } else if (!strcmp("add", argv[optind])) {
126                 if (binary) {
127                         result = read_openpgp_stream(stdin_getchar, NULL,
128                                  &packets, 0);
129                         logthing(LOGTHING_INFO,
130                                         "read_openpgp_stream: %d", result);
131                 } else {
132                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
133                 }
134                 if (packets != NULL) {
135                         result = parse_keys(packets, &keys);
136                         free_packet_list(packets);
137                         packets = NULL;
138                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
139                                         result);
140
141                         result = cleankeys(keys);
142                         logthing(LOGTHING_INFO, "%d keys cleaned.",
143                                         result);
144
145                         initdb(false);
146                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
147                                         update_keys(&keys));
148                         if (keys != NULL && update) {
149                                 flatten_publickey(keys,
150                                         &packets,
151                                         &list_end);
152                                 if (binary) {
153                                         write_openpgp_stream(stdout_putchar,
154                                                         NULL,
155                                                         packets);
156                                 } else {
157                                         armor_openpgp_stream(stdout_putchar,
158                                                 NULL,
159                                                 packets);
160                                 }
161                                 free_packet_list(packets);
162                                 packets = NULL;
163                         }
164                         cleanupdb();
165                 } else {
166                         rc = 1;
167                         logthing(LOGTHING_NOTICE, "No keys read.");
168                 }
169
170                 if (keys != NULL) {
171                         free_publickey(keys);
172                         keys = NULL;
173                 } else {
174                         rc = 1;
175                         logthing(LOGTHING_NOTICE, "No changes.");
176                 }
177         } else if (!strcmp("clean", argv[optind])) {
178                 if (binary) {
179                         result = read_openpgp_stream(stdin_getchar, NULL,
180                                  &packets, 0);
181                         logthing(LOGTHING_INFO,
182                                         "read_openpgp_stream: %d", result);
183                 } else {
184                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
185                 }
186
187                 if (packets != NULL) {
188                         result = parse_keys(packets, &keys);
189                         free_packet_list(packets);
190                         packets = NULL;
191                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
192                                         result);
193
194                         if (keys != NULL) {
195                                 result = cleankeys(keys);
196                                 logthing(LOGTHING_INFO, "%d keys cleaned.",
197                                                 result);
198
199                                 flatten_publickey(keys,
200                                         &packets,
201                                         &list_end);
202
203                                 if (binary) {
204                                         write_openpgp_stream(stdout_putchar,
205                                                         NULL,
206                                                         packets);
207                                 } else {
208                                         armor_openpgp_stream(stdout_putchar,
209                                                 NULL,
210                                                 packets);
211                                 }
212                                 free_packet_list(packets);
213                                 packets = NULL;
214                         }
215                 } else {
216                         rc = 1;
217                         logthing(LOGTHING_NOTICE, "No keys read.");
218                 }
219                 
220                 if (keys != NULL) {
221                         free_publickey(keys);
222                         keys = NULL;
223                 }
224         } else if ((argc - optind) == 2) {
225                 search = argv[optind+1];
226                 if (search != NULL) {
227                         keyid = strtoul(search, &end, 16);
228                         if (*search != 0 &&
229                                         end != NULL &&
230                                         *end == 0) {
231                                 ishex = true;
232                         }
233                 }
234                 initdb(false);
235                 if (!strcmp("index", argv[optind])) {
236                         find_keys(search, keyid, ishex, fingerprint,
237                                         false, false);
238                 } else if (!strcmp("vindex", argv[optind])) {
239                         find_keys(search, keyid, ishex, fingerprint,
240                                         false, true);
241                 } else if (!strcmp("getphoto", argv[optind])) {
242                         if (!ishex) {
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;
247                                 size_t         length = 0;
248
249                                 if (getphoto(keys, 0, &photo, &length)) {
250                                         fwrite(photo,
251                                                 1,
252                                                 length,
253                                                 stdout);
254                                 }
255                                 free_publickey(keys);
256                                 keys = NULL;
257                         } else {
258                                 puts("Key not found");
259                         }
260                 } else if (!strcmp("delete", argv[optind])) {
261                         delete_key(getfullkeyid(keyid), false);
262                 } else if (!strcmp("get", argv[optind])) {
263                         if (!ishex) {
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,
269                                                 &packets,
270                                                 &list_end);
271                                 free_publickey(keys);
272                                 armor_openpgp_stream(stdout_putchar,
273                                                 NULL,
274                                                 packets);
275                                 free_packet_list(packets);
276                                 packets = NULL;
277                         } else {
278                                 puts("Key not found");
279                         }
280                 }
281                 cleanupdb();
282         } else {
283                 usage();
284         }
285
286         cleanuplogthing();
287         cleanupconfig();
288
289         return rc;
290 }