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