]> the.earth.li Git - onak.git/blob - onak.c
Use dynamic context for all backend databases
[onak.git] / onak.c
1 /*
2  * onak.c - An OpenPGP keyserver.
3  *
4  * This is the main swiss army knife binary.
5  *
6  * Copyright 2002 Jonathan McDowell <noodles@earth.li>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc., 51
19  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21
22 #include <fcntl.h>
23 #include <getopt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #include "armor.h"
32 #include "charfuncs.h"
33 #include "cleankey.h"
34 #include "cleanup.h"
35 #include "keydb.h"
36 #include "keyid.h"
37 #include "keyindex.h"
38 #include "keystructs.h"
39 #include "log.h"
40 #include "mem.h"
41 #include "merge.h"
42 #include "onak-conf.h"
43 #include "parsekey.h"
44 #include "photoid.h"
45 #include "version.h"
46
47 void find_keys(struct onak_dbctx *dbctx,
48                 char *search, uint64_t keyid, uint8_t *fp, bool ishex,
49                 bool isfp, bool fingerprint, bool skshash, bool exact,
50                 bool verbose)
51 {
52         struct openpgp_publickey *publickey = NULL;
53         int count = 0;
54
55         if (ishex) {
56                 count = dbctx->fetch_key_id(dbctx, keyid, &publickey,
57                                 false);
58         } else if (isfp) {
59                 count = dbctx->fetch_key_fp(dbctx, fp, MAX_FINGERPRINT_LEN,
60                                 &publickey, false);
61         } else {
62                 count = dbctx->fetch_key_text(dbctx, search, &publickey);
63         }
64         if (publickey != NULL) {
65                 key_index(dbctx, publickey, verbose, fingerprint, skshash,
66                         false);
67                 free_publickey(publickey);
68         } else if (count == 0) {
69                 puts("Key not found.");
70         } else {
71                 printf("Found %d keys, but maximum number to return is %d.\n",
72                                 count,
73                                 config.maxkeys);
74                 puts("Try again with a more specific search.");
75         }
76 }
77
78 /**
79  * @brief Context for the keyserver dumping function
80  */
81 struct dump_ctx {
82         /** Keys we've dumped so far to this file */
83         int count;
84         /** Maximum keys to dump per file */
85         int maxcount;
86         /** File descriptor for the current dump file */
87         int fd;
88         /** Number of the current dump file */
89         int filenum;
90         /** Base filename to use for dump files */
91         char *filebase;
92 };
93
94 void dump_func(void *ctx, struct openpgp_publickey *key)
95 {
96         struct openpgp_packet_list *packets = NULL;
97         struct openpgp_packet_list *list_end = NULL;
98         struct dump_ctx *state;
99         char filename[1024];
100
101         state = (struct dump_ctx *) ctx;
102
103         if (state->fd == -1 || state->count++ > state->maxcount) {
104                 if (state->fd != -1) {
105                         close(state->fd);
106                         state->fd = -1;
107                 }
108                 snprintf(filename, 1023, state->filebase, state->filenum);
109                 state->fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
110                 state->filenum++;
111                 state->count = 0;
112         }
113         flatten_publickey(key, &packets, &list_end);
114         write_openpgp_stream(file_putchar, &state->fd, packets);
115         free_packet_list(packets);
116         packets = list_end = NULL;
117
118         return;
119 }
120
121 static uint8_t hex2bin(char c)
122 {
123         if (c >= '0' && c <= '9') {
124                 return (c - '0');
125         } else if (c >= 'a' && c <= 'f') {
126                 return (c - 'a' + 10);
127         } else if (c >= 'A' && c <= 'F') {
128                 return (c - 'A' + 10);
129         }
130
131         return 255;
132 }
133
134 void usage(void) {
135         puts("onak " ONAK_VERSION " - an OpenPGP keyserver.\n");
136         puts("Usage:\n");
137         puts("\tonak [options] <command> <parameters>\n");
138         puts("\tCommands:\n");
139         puts("\tadd      - read armored OpenPGP keys from stdin and add to the"
140                 " keyserver");
141         puts("\tclean    - read armored OpenPGP keys from stdin, run the"
142                 " cleaning\n\t             routines against them and dump to"
143                 " stdout");
144         puts("\tdelete   - delete a given key from the keyserver");
145         puts("\tdump     - dump all the keys from the keyserver to a file or"
146                 " files\n\t           starting keydump*");
147         puts("\tget      - retrieves the key requested from the keyserver");
148         puts("\tgetphoto - retrieves the first photoid on the given key and"
149                 " dumps to\n\t           stdout");
150         puts("\tindex    - search for a key and list it");
151         puts("\tvindex   - search for a key and list it and its signatures");
152 }
153
154 int main(int argc, char *argv[])
155 {
156         struct openpgp_packet_list      *packets = NULL;
157         struct openpgp_packet_list      *list_end = NULL;
158         struct openpgp_publickey        *keys = NULL;
159         char                            *configfile = NULL;
160         int                              rc = EXIT_SUCCESS;
161         int                              result = 0;
162         char                            *search = NULL;
163         char                            *end = NULL;
164         uint64_t                         keyid = 0;
165         uint8_t                          fp[MAX_FINGERPRINT_LEN];
166         int                              i;
167         bool                             ishex = false;
168         bool                             isfp = false;
169         bool                             verbose = false;
170         bool                             update = false;
171         bool                             binary = false;
172         bool                             fingerprint = false;
173         bool                             skshash = false;
174         int                              optchar;
175         struct dump_ctx                  dumpstate;
176         struct skshash                   hash;
177         struct onak_dbctx               *dbctx;
178
179         while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
180                 switch (optchar) {
181                 case 'b': 
182                         binary = true;
183                         break;
184                 case 'c':
185                         configfile = strdup(optarg);
186                         break;
187                 case 'f': 
188                         fingerprint = true;
189                         break;
190                 case 's': 
191                         skshash = true;
192                         break;
193                 case 'u': 
194                         update = true;
195                         break;
196                 case 'v': 
197                         verbose = true;
198                         setlogthreshold(LOGTHING_INFO);
199                         break;
200                 }
201         }
202
203         readconfig(configfile);
204         initlogthing("onak", config.logfile);
205         catchsignals();
206
207         if ((argc - optind) < 1) {
208                 usage();
209         } else if (!strcmp("dump", argv[optind])) {
210                 dbctx = config.dbinit(true);
211                 dumpstate.count = dumpstate.filenum = 0;
212                 dumpstate.maxcount = 100000;
213                 dumpstate.fd = -1;
214                 dumpstate.filebase = "keydump.%d.pgp";
215                 dbctx->iterate_keys(dbctx, dump_func, &dumpstate);
216                 if (dumpstate.fd != -1) {
217                         close(dumpstate.fd);
218                         dumpstate.fd = -1;
219                 }
220                 dbctx->cleanupdb(dbctx);
221         } else if (!strcmp("add", argv[optind])) {
222                 if (binary) {
223                         result = read_openpgp_stream(stdin_getchar, NULL,
224                                  &packets, 0);
225                         logthing(LOGTHING_INFO,
226                                         "read_openpgp_stream: %d", result);
227                 } else {
228                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
229                 }
230                 if (packets != NULL) {
231                         result = parse_keys(packets, &keys);
232                         free_packet_list(packets);
233                         packets = NULL;
234                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
235                                         result);
236
237                         result = cleankeys(keys);
238                         logthing(LOGTHING_INFO, "%d keys cleaned.",
239                                         result);
240
241                         dbctx = config.dbinit(false);
242                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
243                                         dbctx->update_keys(dbctx, &keys,
244                                         false));
245                         if (keys != NULL && update) {
246                                 flatten_publickey(keys,
247                                         &packets,
248                                         &list_end);
249                                 if (binary) {
250                                         write_openpgp_stream(stdout_putchar,
251                                                         NULL,
252                                                         packets);
253                                 } else {
254                                         armor_openpgp_stream(stdout_putchar,
255                                                 NULL,
256                                                 packets);
257                                 }
258                                 free_packet_list(packets);
259                                 packets = NULL;
260                         }
261                         dbctx->cleanupdb(dbctx);
262                 } else {
263                         rc = 1;
264                         logthing(LOGTHING_NOTICE, "No keys read.");
265                 }
266
267                 if (keys != NULL) {
268                         free_publickey(keys);
269                         keys = NULL;
270                 } else {
271                         rc = 1;
272                         logthing(LOGTHING_NOTICE, "No changes.");
273                 }
274         } else if (!strcmp("clean", argv[optind])) {
275                 if (binary) {
276                         result = read_openpgp_stream(stdin_getchar, NULL,
277                                  &packets, 0);
278                         logthing(LOGTHING_INFO,
279                                         "read_openpgp_stream: %d", result);
280                 } else {
281                         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
282                 }
283
284                 if (packets != NULL) {
285                         result = parse_keys(packets, &keys);
286                         free_packet_list(packets);
287                         packets = NULL;
288                         logthing(LOGTHING_INFO, "Finished reading %d keys.",
289                                         result);
290
291                         if (keys != NULL) {
292                                 result = cleankeys(keys);
293                                 logthing(LOGTHING_INFO, "%d keys cleaned.",
294                                                 result);
295
296                                 flatten_publickey(keys,
297                                         &packets,
298                                         &list_end);
299
300                                 if (binary) {
301                                         write_openpgp_stream(stdout_putchar,
302                                                         NULL,
303                                                         packets);
304                                 } else {
305                                         armor_openpgp_stream(stdout_putchar,
306                                                 NULL,
307                                                 packets);
308                                 }
309                                 free_packet_list(packets);
310                                 packets = NULL;
311                         }
312                 } else {
313                         rc = 1;
314                         logthing(LOGTHING_NOTICE, "No keys read.");
315                 }
316                 
317                 if (keys != NULL) {
318                         free_publickey(keys);
319                         keys = NULL;
320                 }
321         } else if ((argc - optind) == 2) {
322                 search = argv[optind+1];
323                 if (search != NULL && strlen(search) == 42 &&
324                                 search[0] == '0' && search[1] == 'x') {
325                         for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
326                                 fp[i] = (hex2bin(search[2 + i * 2]) << 4) +
327                                                 hex2bin(search[3 + i * 2]);
328                         }
329                         isfp = true;
330                 } else if (search != NULL) {
331                         keyid = strtoul(search, &end, 16);
332                         if (*search != 0 &&
333                                         end != NULL &&
334                                         *end == 0) {
335                                 ishex = true;
336                         }
337                 }
338                 dbctx = config.dbinit(false);
339                 if (!strcmp("index", argv[optind])) {
340                         find_keys(dbctx, search, keyid, fp, ishex, isfp,
341                                         fingerprint, skshash,
342                                         false, false);
343                 } else if (!strcmp("vindex", argv[optind])) {
344                         find_keys(dbctx, search, keyid, fp, ishex, isfp,
345                                         fingerprint, skshash,
346                                         false, true);
347                 } else if (!strcmp("getphoto", argv[optind])) {
348                         if (!ishex) {
349                                 puts("Can't get a key on uid text."
350                                         " You must supply a keyid.");
351                         } else if (dbctx->fetch_key_id(dbctx, keyid, &keys,
352                                         false)) {
353                                 unsigned char *photo = NULL;
354                                 size_t         length = 0;
355
356                                 if (getphoto(keys, 0, &photo,
357                                                 &length) == ONAK_E_OK) {
358                                         fwrite(photo,
359                                                 1,
360                                                 length,
361                                                 stdout);
362                                 }
363                                 free_publickey(keys);
364                                 keys = NULL;
365                         } else {
366                                 puts("Key not found");
367                         }
368                 } else if (!strcmp("delete", argv[optind])) {
369                         dbctx->delete_key(dbctx,
370                                         dbctx->getfullkeyid(dbctx, keyid),
371                                         false);
372                 } else if (!strcmp("get", argv[optind])) {
373                         if (!(ishex || isfp)) {
374                                 puts("Can't get a key on uid text."
375                                         " You must supply a keyid / "
376                                         "fingerprint.");
377                         } else if ((isfp &&
378                                         dbctx->fetch_key_fp(dbctx, fp,
379                                                 MAX_FINGERPRINT_LEN,
380                                                 &keys, false)) ||
381                                         (ishex &&
382                                         dbctx->fetch_key_id(dbctx, keyid,
383                                                 &keys, false))) {
384                                 logthing(LOGTHING_INFO, "Got key.");
385                                 flatten_publickey(keys,
386                                                 &packets,
387                                                 &list_end);
388                                 free_publickey(keys);
389                                 if (binary) {
390                                         write_openpgp_stream(stdout_putchar,
391                                                 NULL,
392                                                 packets);
393                                 } else {
394                                         armor_openpgp_stream(stdout_putchar,
395                                                 NULL,
396                                                 packets);
397                                 }
398                                 free_packet_list(packets);
399                                 packets = NULL;
400                         } else {
401                                 puts("Key not found");
402                         }
403                 } else if (!strcmp("hget", argv[optind])) {
404                         if (!parse_skshash(search, &hash)) {
405                                 puts("Couldn't parse sks hash.");
406                         } else if (dbctx->fetch_key_skshash(dbctx, &hash,
407                                         &keys)) {
408                                 logthing(LOGTHING_INFO, "Got key.");
409                                 flatten_publickey(keys,
410                                                 &packets,
411                                                 &list_end);
412                                 free_publickey(keys);
413                                 if (binary) {
414                                         write_openpgp_stream(stdout_putchar,
415                                                 NULL,
416                                                 packets);
417                                 } else {
418                                         armor_openpgp_stream(stdout_putchar,
419                                                 NULL,
420                                                 packets);
421                                 }
422                                 free_packet_list(packets);
423                                 packets = NULL;
424                         } else {
425                                 puts("Key not found");
426                         }
427                 }
428                 dbctx->cleanupdb(dbctx);
429         } else {
430                 usage();
431         }
432
433         cleanuplogthing();
434         cleanupconfig();
435
436         return rc;
437 }