cscvs to tla changeset 46
[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 <assert.h>
10 #include <inttypes.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <time.h>
16
17 #include "decodekey.h"
18 #include "getcgi.h"
19 #include "hash.h"
20 #include "keydb.h"
21 #include "keyid.h"
22 #include "keyindex.h"
23 #include "keystructs.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(struct openpgp_signedpacket_list *uids, bool verbose, bool html)
64 {
65         char buf[1024];
66
67         while (uids != NULL) {
68                 if (uids->packet->tag == 13) {
69                         snprintf(buf, 1023, "%.*s",
70                                 (int) uids->packet->length,
71                                 uids->packet->data);
72                         printf("uid                             %s\n",
73                                 (html) ? txt2html(buf) : buf);
74                 } else if (uids->packet->tag == 17) {
75                         printf("uid                             "
76                                 "[photo id]\n");
77                 }
78                 if (verbose) {
79                         list_sigs(uids->sigs, html);
80                 }
81                 uids = uids->next;
82         }
83
84         return 0;
85 }
86
87 int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
88                 bool html)
89 {
90         struct tm       *created = NULL;
91         time_t          created_time = 0;
92         int             type = 0;
93         int             length = 0;
94
95         while (subkeys != NULL) {
96                 if (subkeys->packet->tag == 14) {
97
98                         created_time = (subkeys->packet->data[1] << 24) +
99                                         (subkeys->packet->data[2] << 16) +
100                                         (subkeys->packet->data[3] << 8) +
101                                         subkeys->packet->data[4];
102                         created = gmtime(&created_time);
103
104                         switch (subkeys->packet->data[0]) {
105                         case 2:
106                         case 3:
107                                 type = subkeys->packet->data[7];
108                                 length = (subkeys->packet->data[8] << 8) +
109                                         subkeys->packet->data[9];
110                                 break;
111                         case 4:
112                                 type = subkeys->packet->data[5];
113                                 length = (subkeys->packet->data[6] << 8) +
114                                         subkeys->packet->data[7];
115                                 break;
116                         default:
117                                 fprintf(stderr, "Unknown key type: %d\n",
118                                         subkeys->packet->data[0]);
119                         }
120                 
121                         printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
122                                 length,
123                                 (type == 1) ? 'R' : ((type == 16) ? 'g' : 
124                                         ((type == 17) ? 'D' : '?')),
125                                 (uint32_t) (get_packetid(subkeys->packet) &
126                                             0xFFFFFFFF),
127                                 created->tm_year + 1900,
128                                 created->tm_mon + 1,
129                                 created->tm_mday);
130
131                 }
132                 if (verbose) {
133                         list_sigs(subkeys->sigs, html);
134                 }
135                 subkeys = subkeys->next;
136         }
137
138         return 0;
139 }
140
141 /**
142  *      key_index - List a set of OpenPGP keys.
143  *      @keys: The keys to display.
144  *      @verbose: Should we list sigs as well?
145  *      @fingerprint: List the fingerprint?
146  *      @html: Should the output be tailored for HTML?
147  *
148  *      This function takes a list of OpenPGP public keys and displays an index
149  *      of them. Useful for debugging or the keyserver Index function.
150  */
151 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
152                         bool html)
153 {
154         struct openpgp_signedpacket_list        *curuid = NULL;
155         struct tm                               *created = NULL;
156         time_t                                   created_time = 0;
157         int                                      type = 0;
158         int                                      length = 0;
159         char                                     buf[1024];
160
161         if (html) {
162                 puts("<pre>");
163         }
164         puts("Type   bits/keyID    Date       User ID");
165         while (keys != NULL) {
166                 created_time = (keys->publickey->data[1] << 24) +
167                                         (keys->publickey->data[2] << 16) +
168                                         (keys->publickey->data[3] << 8) +
169                                         keys->publickey->data[4];
170                 created = gmtime(&created_time);
171
172                 switch (keys->publickey->data[0]) {
173                 case 2:
174                 case 3:
175                         type = keys->publickey->data[7];
176                         length = (keys->publickey->data[8] << 8) +
177                                         keys->publickey->data[9];
178                         break;
179                 case 4:
180                         type = keys->publickey->data[5];
181                         length = (keys->publickey->data[6] << 8) +
182                                         keys->publickey->data[7];
183                         break;
184                 default:
185                         fprintf(stderr, "Unknown key type: %d\n",
186                                 keys->publickey->data[0]);
187                 }
188                 
189                 printf("pub  %5d%c/%08X %04d/%02d/%02d ",
190                         length,
191                         (type == 1) ? 'R' : ((type == 16) ? 'g' : 
192                                 ((type == 17) ? 'D' : '?')),
193                         (uint32_t) (get_keyid(keys) & 0xFFFFFFFF),
194                         created->tm_year + 1900,
195                         created->tm_mon + 1,
196                         created->tm_mday);
197
198                 curuid = keys->uids;
199                 if (curuid != NULL && curuid->packet->tag == 13) {
200                         snprintf(buf, 1023, "%.*s",
201                                 (int) curuid->packet->length,
202                                 curuid->packet->data);
203                         printf("%s\n", (html) ? txt2html(buf) : buf);
204                         if (verbose) {
205                                 list_sigs(curuid->sigs, html);
206                         }
207                         curuid = curuid->next;
208                 } else {
209                         putchar('\n');
210                 }
211
212                 list_uids(curuid, verbose, html);
213                 list_subkeys(keys->subkeys, verbose, html);
214
215                 keys = keys->next;
216         }
217
218         if (html) {
219                 puts("</pre>");
220         }
221
222         return 0;
223 }