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