Clean up file header copyrights
[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, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "armor.h"
26 #include "charfuncs.h"
27 #include "cleanup.h"
28 #include "getcgi.h"
29 #include "hash.h"
30 #include "keydb.h"
31 #include "log.h"
32 #include "mem.h"
33 #include "onak-conf.h"
34 #include "parsekey.h"
35 #include "stats.h"
36 #include "version.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(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 rec;
73         int pathlen = 0;
74
75         fullhave = config.dbbackend->getfullkeyid(have);
76         fullwant = config.dbbackend->getfullkeyid(want);
77
78         /*
79          * Make sure the keys we have and want are in the cache.
80          */
81         config.dbbackend->cached_getkeysigs(fullhave);
82         config.dbbackend->cached_getkeysigs(fullwant);
83
84         if ((keyinfoa = findinhash(fullhave)) == NULL) {
85                 return 1;
86         }
87         if ((keyinfob = findinhash(fullwant)) == NULL) {
88                 return 1;
89         }
90         
91         while ((!cleanup()) && (pathlen < count)) {
92                 /*
93                  * Fill the tree info up.
94                  */
95                 initcolour(true);
96                 rec = findpath(keyinfoa, keyinfob);
97                 keyinfob->parent = 0;
98                 if (keyinfoa->colour == 0) {
99                         pathlen = count;
100                 } else {
101                         /*
102                          * Skip the first key, as the remote user will already
103                          * have it
104                          */
105                         curkey = findinhash(keyinfoa->parent);
106                         while (curkey != NULL && curkey->keyid != 0) {
107                                 if (curkey->keyid != fullwant &&
108                                                 config.dbbackend->fetch_key(
109                                                 curkey->keyid,
110                                                 &publickey, false)) {
111                                         flatten_publickey(publickey,
112                                                         &packets,
113                                                         &list_end);
114                                         free_publickey(publickey);
115                                         publickey = NULL;
116                                 }
117                                 if (curkey != keyinfoa && curkey != keyinfob) {
118                                         curkey->disabled = true;
119                                 }
120                                 curkey = findinhash(curkey->parent);
121                         }
122                 }
123                 pathlen++;
124         }
125
126         /*
127          * Add the destination key to the list of returned keys.
128          */
129         if (config.dbbackend->fetch_key(fullwant, &publickey, false)) {
130                 flatten_publickey(publickey,
131                                 &packets,
132                                 &list_end);
133                 free_publickey(publickey);
134                 publickey = NULL;
135         }
136
137         armor_openpgp_stream(stdout_putchar, NULL, packets);
138         free_packet_list(packets);
139         packets = list_end = NULL;
140
141         return 0;
142 }
143
144 int main(int argc, char *argv[])
145 {
146         char     **cgiparams = NULL;    /* Our CGI parameter block */
147         uint64_t   from = 0, to = 0;
148         int        op = OP_UNKNOWN;
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         config.dbbackend->initdb(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(from, to, 3);
194         } else {
195                 dofindpath(from, to, true, 3);
196         }
197         destroyhash();
198         config.dbbackend->cleanupdb();
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 }