]> the.earth.li Git - onak.git/blob - gpgwww.c
Remove unused worddb_cmp() from DB4 backend
[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 "build-config.h"
25
26 #include "armor.h"
27 #include "charfuncs.h"
28 #include "cleanup.h"
29 #include "getcgi.h"
30 #include "hash.h"
31 #include "keydb.h"
32 #include "log.h"
33 #include "mem.h"
34 #include "onak-conf.h"
35 #include "parsekey.h"
36 #include "stats.h"
37
38 #define OP_UNKNOWN 0
39 #define OP_GET     1
40
41 int parsecgistuff(char **cgiparams, uint64_t *from, uint64_t *to)
42 {
43         int i = 0;
44         int op = OP_UNKNOWN;
45
46         if (cgiparams != NULL) {
47                 i = 0;
48                 while (cgiparams[i] != NULL) {
49                         if (!strcmp(cgiparams[i], "to")) {
50                                 *to = strtoul(cgiparams[i+1], NULL, 16);
51                         } else if (!strcmp(cgiparams[i], "from")) {
52                                 *from = strtoul(cgiparams[i+1], NULL, 16);
53                         } else if (!strcmp(cgiparams[i], "op")) {
54                                 if (!strcmp(cgiparams[i+1], "get")) {
55                                         op = OP_GET;
56                                 }
57                         }
58                         i += 2;
59                 }
60         }
61
62         return op;
63 }
64
65 int getkeyspath(struct onak_dbctx *dbctx,
66                 uint64_t have, uint64_t want, int count)
67 {
68         struct openpgp_publickey *publickey = NULL;
69         struct openpgp_packet_list *packets = NULL;
70         struct openpgp_packet_list *list_end = NULL;
71         struct stats_key *keyinfoa, *keyinfob, *curkey;
72         int pathlen = 0;
73
74         /*
75          * Make sure the keys we have and want are in the cache.
76          */
77         dbctx->cached_getkeysigs(dbctx, have);
78         dbctx->cached_getkeysigs(dbctx, want);
79
80         if ((keyinfoa = findinhash(have)) == NULL) {
81                 return 1;
82         }
83         if ((keyinfob = findinhash(want)) == NULL) {
84                 return 1;
85         }
86         
87         while ((!cleanup()) && (pathlen < count)) {
88                 /*
89                  * Fill the tree info up.
90                  */
91                 initcolour(true);
92                 findpath(dbctx, keyinfoa, keyinfob);
93                 keyinfob->parent = 0;
94                 if (keyinfoa->colour == 0) {
95                         pathlen = count;
96                 } else {
97                         /*
98                          * Skip the first key, as the remote user will already
99                          * have it
100                          */
101                         curkey = findinhash(keyinfoa->parent);
102                         while (curkey != NULL && curkey->keyid != 0) {
103                                 if (curkey->keyid != want &&
104                                                 dbctx->fetch_key_id(dbctx,
105                                                 curkey->keyid,
106                                                 &publickey, false)) {
107                                         flatten_publickey(publickey,
108                                                         &packets,
109                                                         &list_end);
110                                         free_publickey(publickey);
111                                         publickey = NULL;
112                                 }
113                                 if (curkey != keyinfoa && curkey != keyinfob) {
114                                         curkey->disabled = true;
115                                 }
116                                 curkey = findinhash(curkey->parent);
117                         }
118                 }
119                 pathlen++;
120         }
121
122         /*
123          * Add the destination key to the list of returned keys.
124          */
125         if (dbctx->fetch_key_id(dbctx, want, &publickey, false)) {
126                 flatten_publickey(publickey,
127                                 &packets,
128                                 &list_end);
129                 free_publickey(publickey);
130                 publickey = NULL;
131         }
132
133         armor_openpgp_stream(stdout_putchar, NULL, packets);
134         free_packet_list(packets);
135         packets = list_end = NULL;
136
137         return 0;
138 }
139
140 int main(int argc, char *argv[])
141 {
142         char     **cgiparams = NULL;    /* Our CGI parameter block */
143         uint64_t   from = 0, to = 0;
144         int        op = OP_UNKNOWN;
145         struct onak_dbctx *dbctx;
146
147         cgiparams = getcgivars(argc, argv);
148
149
150         op = parsecgistuff(cgiparams, &from, &to);
151         
152         if (op != OP_GET) {
153                 start_html("Experimental PGP key path finder results");
154         } else {
155                 puts("Content-Type: text/plain\n");
156         }
157
158         if (from == 0 || to == 0) {
159                 printf("Must pass from & to\n");
160                 puts("</HTML>");
161                 exit(1);
162         }
163
164         if (op != OP_GET) {
165                 printf("<P>Looking for path from 0x%016" PRIX64" to 0x%016"
166                                 PRIX64 ".\n",
167                                 from, to);
168                 printf("<A HREF=\"gpgwww?from=0x%016" PRIX64 "&to=0x%016" PRIX64
169                                 "\">Find reverse path</A>\n",
170                                 to,
171                                 from);
172                 printf("<A HREF=\"gpgwww?from=0x%016" PRIX64 "&to=0x%016" PRIX64
173                                 "&op=get\">"
174                                 "Get all keys listed</A></P>\n",
175                                 from,
176                                 to);
177         }
178
179         readconfig(NULL);
180         initlogthing("gpgwww", config.logfile);
181         catchsignals();
182         dbctx = config.dbinit(config.backend, true);
183         inithash();
184         logthing(LOGTHING_NOTICE, "Looking for path from 0x%016" PRIX64
185                         " to 0x%016"
186                         PRIX64,
187                         from,
188                         to);
189         if (op == OP_GET) {
190                 getkeyspath(dbctx, from, to, 3);
191         } else {
192                 dofindpath(dbctx, from, to, true, 3);
193         }
194         destroyhash();
195         dbctx->cleanupdb(dbctx);
196         cleanuplogthing();
197         cleanupconfig();
198
199         if (op != OP_GET) {
200                 puts("<HR>");
201                 puts("Produced by gpgwww " ONAK_VERSION ", part of onak. ");
202                 end_html();
203         }
204
205         cleanupcgi(cgiparams);
206         cgiparams = NULL;
207
208         return EXIT_SUCCESS;
209 }