]> the.earth.li Git - onak.git/blob - onak.c
Add ability to drop overly large packets
[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, see <https://www.gnu.org/licenses/>.
19  */
20
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29
30 #include "armor.h"
31 #include "charfuncs.h"
32 #include "cleankey.h"
33 #include "cleanup.h"
34 #include "keydb.h"
35 #include "keyid.h"
36 #include "keyindex.h"
37 #include "keystructs.h"
38 #include "log.h"
39 #include "mem.h"
40 #include "merge.h"
41 #include "onak-conf.h"
42 #include "parsekey.h"
43 #include "photoid.h"
44 #include "version.h"
45
46 void find_keys(struct onak_dbctx *dbctx,
47                 char *search, uint64_t keyid,
48                 struct openpgp_fingerprint *fingerprint,
49                 bool ishex, bool isfp, bool dispfp, bool skshash,
50                 bool exact, 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, fingerprint,
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, dispfp, 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("\treindex  - retrieve and re-store a key in the backend db");
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(config.backend, 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, config.clean_policies);
237                         logthing(LOGTHING_INFO, "%d keys cleaned.",
238                                         result);
239
240                         dbctx = config.dbinit(config.backend, 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                                                 config.clean_policies);
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 (!strcmp("dumpconfig", argv[optind])) {
322                 if ((argc - optind) == 2) {
323                         writeconfig(argv[optind + 1]);
324                 } else {
325                         /* Dump config to stdout */
326                         writeconfig(NULL);
327                 }
328         } else if ((argc - optind) == 2) {
329                 search = argv[optind+1];
330                 if (search != NULL && strlen(search) == 42 &&
331                                 search[0] == '0' && search[1] == 'x') {
332                         fingerprint.length = MAX_FINGERPRINT_LEN;
333                         for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
334                                 fingerprint.fp[i] =
335                                         (hex2bin(search[2 + i * 2]) << 4) +
336                                                 hex2bin(search[3 + i * 2]);
337                         }
338                         isfp = true;
339                 } else if (search != NULL) {
340                         keyid = strtouq(search, &end, 16);
341                         if (*search != 0 &&
342                                         end != NULL &&
343                                         *end == 0) {
344                                 ishex = true;
345                         }
346                 }
347                 dbctx = config.dbinit(config.backend, false);
348                 if (!strcmp("index", argv[optind])) {
349                         find_keys(dbctx, search, keyid, &fingerprint, ishex,
350                                         isfp, dispfp, skshash,
351                                         false, false);
352                 } else if (!strcmp("vindex", argv[optind])) {
353                         find_keys(dbctx, search, keyid, &fingerprint, ishex,
354                                         isfp, dispfp, skshash,
355                                         false, true);
356                 } else if (!strcmp("getphoto", argv[optind])) {
357                         if (!ishex) {
358                                 puts("Can't get a key on uid text."
359                                         " You must supply a keyid.");
360                         } else if (dbctx->fetch_key_id(dbctx, keyid, &keys,
361                                         false)) {
362                                 unsigned char *photo = NULL;
363                                 size_t         length = 0;
364
365                                 if (getphoto(keys, 0, &photo,
366                                                 &length) == ONAK_E_OK) {
367                                         fwrite(photo,
368                                                 1,
369                                                 length,
370                                                 stdout);
371                                 }
372                                 free_publickey(keys);
373                                 keys = NULL;
374                         } else {
375                                 puts("Key not found");
376                         }
377                 } else if (!strcmp("delete", argv[optind])) {
378                         dbctx->delete_key(dbctx,
379                                         dbctx->getfullkeyid(dbctx, keyid),
380                                         false);
381                 } else if (!strcmp("get", argv[optind])) {
382                         if (!(ishex || isfp)) {
383                                 puts("Can't get a key on uid text."
384                                         " You must supply a keyid / "
385                                         "fingerprint.");
386                         } else if ((isfp &&
387                                         dbctx->fetch_key_fp(dbctx,
388                                                 &fingerprint,
389                                                 &keys, false)) ||
390                                         (ishex &&
391                                         dbctx->fetch_key_id(dbctx, keyid,
392                                                 &keys, false))) {
393                                 logthing(LOGTHING_INFO, "Got key.");
394                                 flatten_publickey(keys,
395                                                 &packets,
396                                                 &list_end);
397                                 free_publickey(keys);
398                                 if (binary) {
399                                         write_openpgp_stream(stdout_putchar,
400                                                 NULL,
401                                                 packets);
402                                 } else {
403                                         armor_openpgp_stream(stdout_putchar,
404                                                 NULL,
405                                                 packets);
406                                 }
407                                 free_packet_list(packets);
408                                 packets = NULL;
409                         } else {
410                                 puts("Key not found");
411                         }
412                 } else if (!strcmp("hget", argv[optind])) {
413                         if (!parse_skshash(search, &hash)) {
414                                 puts("Couldn't parse sks hash.");
415                         } else if (dbctx->fetch_key_skshash(dbctx, &hash,
416                                         &keys)) {
417                                 logthing(LOGTHING_INFO, "Got key.");
418                                 flatten_publickey(keys,
419                                                 &packets,
420                                                 &list_end);
421                                 free_publickey(keys);
422                                 if (binary) {
423                                         write_openpgp_stream(stdout_putchar,
424                                                 NULL,
425                                                 packets);
426                                 } else {
427                                         armor_openpgp_stream(stdout_putchar,
428                                                 NULL,
429                                                 packets);
430                                 }
431                                 free_packet_list(packets);
432                                 packets = NULL;
433                         } else {
434                                 puts("Key not found");
435                         }
436                 } else if (!strcmp("reindex", argv[optind])) {
437                         dbctx->starttrans(dbctx);
438                         if (dbctx->fetch_key_id(dbctx, keyid, &keys, true)) {
439                                 dbctx->delete_key(dbctx, keyid, true);
440                                 cleankeys(&keys, config.clean_policies);
441                                 dbctx->store_key(dbctx, keys, true, false);
442                         } else {
443                                 puts("Key not found");
444                         }
445                         dbctx->endtrans(dbctx);
446                 }
447                 dbctx->cleanupdb(dbctx);
448         } else {
449                 usage();
450         }
451
452         cleanuplogthing();
453         cleanupconfig();
454         free(configfile);
455
456         return rc;
457 }