]> the.earth.li Git - onak.git/blob - gpgwww.c
Add ability to drop overly large packets
[onak.git] / gpgwww.c
1 /*
2  * gpgwww.c - www interface to path finder.
3  *
4  * Copyright 2001-2004 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "armor.h"
25 #include "charfuncs.h"
26 #include "cleanup.h"
27 #include "getcgi.h"
28 #include "hash.h"
29 #include "keydb.h"
30 #include "log.h"
31 #include "mem.h"
32 #include "onak-conf.h"
33 #include "parsekey.h"
34 #include "stats.h"
35 #include "version.h"
36
37 #define OP_UNKNOWN 0
38 #define OP_GET     1
39
40 int parsecgistuff(char **cgiparams, uint64_t *from, uint64_t *to)
41 {
42         int i = 0;
43         int op = OP_UNKNOWN;
44
45         if (cgiparams != NULL) {
46                 i = 0;
47                 while (cgiparams[i] != NULL) {
48                         if (!strcmp(cgiparams[i], "to")) {
49                                 *to = strtoul(cgiparams[i+1], NULL, 16);
50                         } else if (!strcmp(cgiparams[i], "from")) {
51                                 *from = strtoul(cgiparams[i+1], NULL, 16);
52                         } else if (!strcmp(cgiparams[i], "op")) {
53                                 if (!strcmp(cgiparams[i+1], "get")) {
54                                         op = OP_GET;
55                                 }
56                         }
57                         i += 2;
58                 }
59         }
60
61         return op;
62 }
63
64 int getkeyspath(struct onak_dbctx *dbctx,
65                 uint64_t have, uint64_t want, int count)
66 {
67         struct openpgp_publickey *publickey = NULL;
68         struct openpgp_packet_list *packets = NULL;
69         struct openpgp_packet_list *list_end = NULL;
70         struct stats_key *keyinfoa, *keyinfob, *curkey;
71         uint64_t fullhave, fullwant;
72         int pathlen = 0;
73
74         fullhave = dbctx->getfullkeyid(dbctx, have);
75         fullwant = dbctx->getfullkeyid(dbctx, want);
76
77         /*
78          * Make sure the keys we have and want are in the cache.
79          */
80         dbctx->cached_getkeysigs(dbctx, fullhave);
81         dbctx->cached_getkeysigs(dbctx, fullwant);
82
83         if ((keyinfoa = findinhash(fullhave)) == NULL) {
84                 return 1;
85         }
86         if ((keyinfob = findinhash(fullwant)) == NULL) {
87                 return 1;
88         }
89         
90         while ((!cleanup()) && (pathlen < count)) {
91                 /*
92                  * Fill the tree info up.
93                  */
94                 initcolour(true);
95                 findpath(dbctx, keyinfoa, keyinfob);
96                 keyinfob->parent = 0;
97                 if (keyinfoa->colour == 0) {
98                         pathlen = count;
99                 } else {
100                         /*
101                          * Skip the first key, as the remote user will already
102                          * have it
103                          */
104                         curkey = findinhash(keyinfoa->parent);
105                         while (curkey != NULL && curkey->keyid != 0) {
106                                 if (curkey->keyid != fullwant &&
107                                                 dbctx->fetch_key_id(dbctx,
108                                                 curkey->keyid,
109                                                 &publickey, false)) {
110                                         flatten_publickey(publickey,
111                                                         &packets,
112                                                         &list_end);
113                                         free_publickey(publickey);
114                                         publickey = NULL;
115                                 }
116                                 if (curkey != keyinfoa && curkey != keyinfob) {
117                                         curkey->disabled = true;
118                                 }
119                                 curkey = findinhash(curkey->parent);
120                         }
121                 }
122                 pathlen++;
123         }
124
125         /*
126          * Add the destination key to the list of returned keys.
127          */
128         if (dbctx->fetch_key_id(dbctx, fullwant, &publickey, false)) {
129                 flatten_publickey(publickey,
130                                 &packets,
131                                 &list_end);
132                 free_publickey(publickey);
133                 publickey = NULL;
134         }
135
136         armor_openpgp_stream(stdout_putchar, NULL, packets);
137         free_packet_list(packets);
138         packets = list_end = NULL;
139
140         return 0;
141 }
142
143 int main(int argc, char *argv[])
144 {
145         char     **cgiparams = NULL;    /* Our CGI parameter block */
146         uint64_t   from = 0, to = 0;
147         int        op = OP_UNKNOWN;
148         struct onak_dbctx *dbctx;
149
150         cgiparams = getcgivars(argc, argv);
151
152
153         op = parsecgistuff(cgiparams, &from, &to);
154         
155         if (op != OP_GET) {
156                 start_html("Experimental PGP key path finder results");
157         } else {
158                 puts("Content-Type: text/plain\n");
159         }
160
161         if (from == 0 || to == 0) {
162                 printf("Must pass from & to\n");
163                 puts("</HTML>");
164                 exit(1);
165         }
166
167         if (op != OP_GET) {
168                 printf("<P>Looking for path from 0x%016" PRIX64" to 0x%016"
169                                 PRIX64 ".\n",
170                                 from, to);
171                 printf("<A HREF=\"gpgwww?from=0x%016" PRIX64 "&to=0x%016" PRIX64
172                                 "\">Find reverse path</A>\n",
173                                 to,
174                                 from);
175                 printf("<A HREF=\"gpgwww?from=0x%08" PRIX64 "&to=0x%08" PRIX64
176                                 "&op=get\">"
177                                 "Get all keys listed</A></P>\n",
178                                 from,
179                                 to);
180         }
181
182         readconfig(NULL);
183         initlogthing("gpgwww", config.logfile);
184         catchsignals();
185         dbctx = config.dbinit(config.backend, true);
186         inithash();
187         logthing(LOGTHING_NOTICE, "Looking for path from 0x%016" PRIX64
188                         " to 0x%016"
189                         PRIX64,
190                         from,
191                         to);
192         if (op == OP_GET) {
193                 getkeyspath(dbctx, from, to, 3);
194         } else {
195                 dofindpath(dbctx, from, to, true, 3);
196         }
197         destroyhash();
198         dbctx->cleanupdb(dbctx);
199         cleanuplogthing();
200         cleanupconfig();
201
202         if (op != OP_GET) {
203                 puts("<HR>");
204                 puts("Produced by gpgwww " ONAK_VERSION ", part of onak. ");
205                 end_html();
206         }
207
208         cleanupcgi(cgiparams);
209         cgiparams = NULL;
210
211         return EXIT_SUCCESS;
212 }