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