Add links to HTML index output. (Debian bug #275227)
[onak.git] / keyindex.c
1 /*
2  * keyindex.c - Routines to list an OpenPGP key.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include <inttypes.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15
16 #include "decodekey.h"
17 #include "getcgi.h"
18 #include "hash.h"
19 #include "keydb.h"
20 #include "keyid.h"
21 #include "keyindex.h"
22 #include "keystructs.h"
23 #include "log.h"
24
25 int list_sigs(struct openpgp_packet_list *sigs, bool html)
26 {
27         char *uid = NULL;
28         uint64_t sigid = 0;
29
30         while (sigs != NULL) {
31                 sigid = sig_keyid(sigs->packet);
32                 uid = keyid2uid(sigid);
33                 if (html && uid != NULL) {
34                         printf("sig         <a href=\"lookup?op=get&"
35                                 "search=%08llX\">%08llX</a>             "
36                                 "<a href=\"lookup?op=vindex&search=0x%08llX\">"
37                                 "%s</a>\n",
38                                 sigid & 0xFFFFFFFF,
39                                 sigid & 0xFFFFFFFF,
40                                 sigid & 0xFFFFFFFF,
41                                 txt2html(uid));
42                 } else if (html && uid == NULL) {
43                         printf("sig         %08llX             "
44                                 "[User id not found]\n",
45                                 sigid & 0xFFFFFFFF);
46                 } else {
47                         printf("sig         %08llX"
48                                 "             %s\n",
49                                 sigid & 0xFFFFFFFF,
50                                 (uid != NULL) ? uid :
51                                 "[User id not found]");
52                 }
53                 if (uid != NULL) {
54                         free(uid);
55                         uid = NULL;
56                 }
57                 sigs = sigs->next;
58         }
59
60         return 0;
61 }
62
63 int list_uids(uint64_t keyid, struct openpgp_signedpacket_list *uids,
64                 bool verbose, bool html)
65 {
66         char buf[1024];
67         int  imgindx = 0;
68
69         while (uids != NULL) {
70                 if (uids->packet->tag == 13) {
71                         snprintf(buf, 1023, "%.*s",
72                                 (int) uids->packet->length,
73                                 uids->packet->data);
74                         printf("                                %s\n",
75                                 (html) ? txt2html(buf) : buf);
76                 } else if (uids->packet->tag == 17) {
77                         printf("                                ");
78                         if (html) {
79                                 printf("<img src=\"lookup?op=photo&search=0x%llX&idx=%d\" alt=\"[photo id]\">\n",
80                                                 keyid,
81                                                 imgindx);
82                                 imgindx++;
83                         } else {
84                                 printf("[photo id]\n");
85                         }
86                 }
87                 if (verbose) {
88                         list_sigs(uids->sigs, html);
89                 }
90                 uids = uids->next;
91         }
92
93         return 0;
94 }
95
96 int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
97                 bool html)
98 {
99         struct tm       *created = NULL;
100         time_t          created_time = 0;
101         int             type = 0;
102         int             length = 0;
103
104         while (subkeys != NULL) {
105                 if (subkeys->packet->tag == 14) {
106
107                         created_time = (subkeys->packet->data[1] << 24) +
108                                         (subkeys->packet->data[2] << 16) +
109                                         (subkeys->packet->data[3] << 8) +
110                                         subkeys->packet->data[4];
111                         created = gmtime(&created_time);
112
113                         switch (subkeys->packet->data[0]) {
114                         case 2:
115                         case 3:
116                                 type = subkeys->packet->data[7];
117                                 length = (subkeys->packet->data[8] << 8) +
118                                         subkeys->packet->data[9];
119                                 break;
120                         case 4:
121                                 type = subkeys->packet->data[5];
122                                 length = (subkeys->packet->data[6] << 8) +
123                                         subkeys->packet->data[7];
124                                 break;
125                         default:
126                                 logthing(LOGTHING_ERROR,
127                                         "Unknown key type: %d",
128                                         subkeys->packet->data[0]);
129                         }
130                 
131                         printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
132                                 length,
133                                 (type == 1) ? 'R' : ((type == 16) ? 'g' : 
134                                         ((type == 17) ? 'D' : '?')),
135                                 (uint32_t) (get_packetid(subkeys->packet) &
136                                             0xFFFFFFFF),
137                                 created->tm_year + 1900,
138                                 created->tm_mon + 1,
139                                 created->tm_mday);
140
141                 }
142                 if (verbose) {
143                         list_sigs(subkeys->sigs, html);
144                 }
145                 subkeys = subkeys->next;
146         }
147
148         return 0;
149 }
150
151 void display_fingerprint(struct openpgp_publickey *key)
152 {
153         int             i = 0;
154         size_t          length = 0;
155         unsigned char   fp[20];
156
157         get_fingerprint(key->publickey, fp, &length);
158         printf("      Key fingerprint =");
159         for (i = 0; i < length; i++) {
160                 if ((length == 16) ||
161                         (i % 2 == 0)) {
162                         printf(" ");
163                 }
164                 printf("%02X", fp[i]);
165                 if ((i * 2) == length) {
166                         printf(" ");
167                 }
168         }
169         printf("\n");
170
171         return;
172 }
173
174 /**
175  *      key_index - List a set of OpenPGP keys.
176  *      @keys: The keys to display.
177  *      @verbose: Should we list sigs as well?
178  *      @fingerprint: List the fingerprint?
179  *      @html: Should the output be tailored for HTML?
180  *
181  *      This function takes a list of OpenPGP public keys and displays an index
182  *      of them. Useful for debugging or the keyserver Index function.
183  */
184 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
185                         bool html)
186 {
187         struct openpgp_signedpacket_list        *curuid = NULL;
188         struct tm                               *created = NULL;
189         time_t                                   created_time = 0;
190         int                                      type = 0;
191         int                                      length = 0;
192         char                                     buf[1024];
193         uint64_t                                 keyid;
194
195         if (html) {
196                 puts("<pre>");
197         }
198         puts("Type   bits/keyID    Date       User ID");
199         while (keys != NULL) {
200                 created_time = (keys->publickey->data[1] << 24) +
201                                         (keys->publickey->data[2] << 16) +
202                                         (keys->publickey->data[3] << 8) +
203                                         keys->publickey->data[4];
204                 created = gmtime(&created_time);
205
206                 switch (keys->publickey->data[0]) {
207                 case 2:
208                 case 3:
209                         type = keys->publickey->data[7];
210                         length = (keys->publickey->data[8] << 8) +
211                                         keys->publickey->data[9];
212                         break;
213                 case 4:
214                         type = keys->publickey->data[5];
215                         length = (keys->publickey->data[6] << 8) +
216                                         keys->publickey->data[7];
217                         break;
218                 default:
219                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
220                                 keys->publickey->data[0]);
221                 }
222                 
223                 keyid = (get_keyid(keys) & 0xFFFFFFFF);
224
225                 if (html) {
226                         printf("pub  %5d%c/<a href=\"lookup?op=get&"
227                                 "search=%08X\">%08X</a> %04d/%02d/%02d ",
228                                 length,
229                                 (type == 1) ? 'R' : ((type == 16) ? 'g' : 
230                                         ((type == 17) ? 'D' : '?')),
231                                 (uint32_t) keyid,
232                                 (uint32_t) keyid,
233                                 created->tm_year + 1900,
234                                 created->tm_mon + 1,
235                                 created->tm_mday);
236                 } else {
237                         printf("pub  %5d%c/%08X %04d/%02d/%02d ",
238                                 length,
239                                 (type == 1) ? 'R' : ((type == 16) ? 'g' : 
240                                         ((type == 17) ? 'D' : '?')),
241                                 (uint32_t) keyid,
242                                 created->tm_year + 1900,
243                                 created->tm_mon + 1,
244                                 created->tm_mday);
245                 }
246
247                 curuid = keys->uids;
248                 if (curuid != NULL && curuid->packet->tag == 13) {
249                         snprintf(buf, 1023, "%.*s",
250                                 (int) curuid->packet->length,
251                                 curuid->packet->data);
252                         if (html) {
253                                 printf("<a href=\"lookup?op=vindex&"
254                                         "search=0x%08X\">",
255                                         (uint32_t) keyid);
256                         }
257                         printf("%s%s%s\n", 
258                                 (html) ? txt2html(buf) : buf,
259                                 (html) ? "</a>" : "",
260                                 (keys->revocations == NULL) ? "" :
261                                         " *** REVOKED ***");
262                         if (fingerprint) {
263                                 display_fingerprint(keys);
264                         }
265                         if (verbose) {
266                                 list_sigs(curuid->sigs, html);
267                         }
268                         curuid = curuid->next;
269                 } else {
270                         printf("%s\n", 
271                                 (keys->revocations == NULL) ? "" :
272                                         "*** REVOKED ***");
273                         if (fingerprint) {
274                                 display_fingerprint(keys);
275                         }
276                 }
277
278                 list_uids(keyid, curuid, verbose, html);
279                 if (verbose) {
280                         list_subkeys(keys->subkeys, verbose, html);
281                 }
282
283                 keys = keys->next;
284         }
285
286         if (html) {
287                 puts("</pre>");
288         }
289
290         return 0;
291 }
292
293 /**
294  *      mrkey_index - List a set of OpenPGP keys in the MRHKP format.
295  *      @keys: The keys to display.
296  *
297  *      This function takes a list of OpenPGP public keys and displays a
298  *      machine readable list of them.
299  */
300 int mrkey_index(struct openpgp_publickey *keys)
301 {
302         struct openpgp_signedpacket_list        *curuid = NULL;
303         time_t                                   created_time = 0;
304         int                                      type = 0;
305         int                                      length = 0;
306         int                                      i = 0;
307         size_t                                   fplength = 0;
308         unsigned char                            fp[20];
309
310         while (keys != NULL) {
311                 created_time = (keys->publickey->data[1] << 24) +
312                                         (keys->publickey->data[2] << 16) +
313                                         (keys->publickey->data[3] << 8) +
314                                         keys->publickey->data[4];
315
316                 printf("pub:");
317
318                 switch (keys->publickey->data[0]) {
319                 case 2:
320                 case 3:
321                         printf("%016llX", get_keyid(keys));
322                         type = keys->publickey->data[7];
323                         length = (keys->publickey->data[8] << 8) +
324                                         keys->publickey->data[9];
325                         break;
326                 case 4:
327                         (void) get_fingerprint(keys->publickey, fp, &fplength);
328
329                         for (i = 0; i < fplength; i++) {
330                                 printf("%02X", fp[i]);
331                         }
332
333                         type = keys->publickey->data[5];
334                         length = (keys->publickey->data[6] << 8) +
335                                         keys->publickey->data[7];
336                         break;
337                 default:
338                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
339                                 keys->publickey->data[0]);
340                 }
341
342                 printf(":%d:%d:%ld::%s\n",
343                         type,
344                         length,
345                         created_time,
346                         (keys->revocations == NULL) ? "" : "r");
347         
348                 for (curuid = keys->uids; curuid != NULL;
349                          curuid = curuid->next) {
350                 
351                         if (curuid->packet->tag == 13) {
352                                 printf("uid:%.*s\n",
353                                         (int) curuid->packet->length,
354                                         curuid->packet->data);
355                         }
356                 }
357                 keys = keys->next;
358         }
359         return 0;
360 }