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