]> the.earth.li Git - onak.git/blob - lookup.c
Add support for v5 keys
[onak.git] / lookup.c
1 /*
2  * lookup.c - CGI to lookup keys.
3  *
4  * Copyright 2002-2005,2007-2009,2011 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, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <inttypes.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "build-config.h"
27
28 #include "armor.h"
29 #include "charfuncs.h"
30 #include "cleankey.h"
31 #include "cleanup.h"
32 #include "getcgi.h"
33 #include "keydb.h"
34 #include "keyid.h"
35 #include "keyindex.h"
36 #include "log.h"
37 #include "mem.h"
38 #include "onak-conf.h"
39 #include "parsekey.h"
40 #include "photoid.h"
41
42 #define OP_UNKNOWN 0
43 #define OP_GET     1
44 #define OP_INDEX   2
45 #define OP_VINDEX  3
46 #define OP_PHOTO   4
47 #define OP_HGET    5
48
49 void find_keys(struct onak_dbctx *dbctx,
50                 char *search, uint64_t keyid,
51                 struct openpgp_fingerprint *fingerprint,
52                 bool ishex, bool isfp, bool dispfp, bool skshash,
53                 bool exact, bool verbose, bool mrhkp)
54 {
55         struct openpgp_publickey *publickey = NULL;
56         int count = 0;
57
58         if (ishex) {
59                 count = dbctx->fetch_key_id(dbctx, keyid, &publickey,
60                                 false);
61         } else if (isfp) {
62                 count = dbctx->fetch_key_fp(dbctx, fingerprint, &publickey,
63                                 false);
64         } else {
65                 count = dbctx->fetch_key_text(dbctx, search, &publickey);
66         }
67         if (publickey != NULL) {
68                 if (mrhkp) {
69                         printf("info:1:%d\n", count);
70                         mrkey_index(publickey);
71                 } else {
72                         key_index(dbctx, publickey, verbose, dispfp,
73                                 skshash, true);
74                 }
75                 free_publickey(publickey);
76         } else if (count == 0) {
77                 if (mrhkp) {
78                         puts("info:1:0");
79                 } else {
80                         puts("Key not found.");
81                 }
82         } else {
83                 if (mrhkp) {
84                         puts("info:1:0");
85                 } else {
86                         printf("Found %d keys, but maximum number to return"
87                                 " is %d.\n",
88                                 count,
89                                 config.maxkeys);
90                         puts("Try again with a more specific search.");
91                 }
92         }
93 }
94
95 static uint8_t hex2bin(char c)
96 {
97         if (c >= '0' && c <= '9') {
98                 return (c - '0');
99         } else if (c >= 'a' && c <= 'f') {
100                 return (c - 'a' + 10);
101         } else if (c >= 'A' && c <= 'F') {
102                 return (c - 'A' + 10);
103         }
104
105         return 255;
106 }
107
108 int main(int argc, char *argv[])
109 {
110         char **params = NULL;
111         int op = OP_UNKNOWN;
112         int i, j;
113         int indx = 0;
114         bool dispfp = false;
115         bool skshash = false;
116         bool exact = false;
117         bool ishex = false;
118         bool isfp = false;
119         bool mrhkp = false;
120         uint64_t keyid = 0;
121         struct openpgp_fingerprint fingerprint;
122         char *search = NULL;
123         char *end = NULL;
124         struct openpgp_publickey *publickey = NULL;
125         struct openpgp_packet_list *packets = NULL;
126         struct openpgp_packet_list *list_end = NULL;
127         int result;
128         struct skshash hash;
129         struct onak_dbctx *dbctx;
130
131         params = getcgivars(argc, argv);
132         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
133                 if (!strcmp(params[i], "op")) {
134                         if (!strcmp(params[i+1], "get")) {
135                                 op = OP_GET;
136                         } else if (!strcmp(params[i+1], "hget")) {
137                                 op = OP_HGET;
138                         } else if (!strcmp(params[i+1], "index")) {
139                                 op = OP_INDEX;
140                         } else if (!strcmp(params[i+1], "vindex")) {
141                                 op = OP_VINDEX;
142                         } else if (!strcmp(params[i+1], "photo")) {
143                                 op = OP_PHOTO;
144                         }
145                 } else if (!strcmp(params[i], "search")) {
146                         search = params[i+1];
147                         params[i+1] = NULL;
148                         if (search != NULL && strlen(search) == 42 &&
149                                         search[0] == '0' && search[1] == 'x') {
150                                 /* v4 fingerprint */
151                                 fingerprint.length = 20;
152                                 for (j = 0; j < 20; j++) {
153                                         fingerprint.fp[j] = (hex2bin(
154                                                         search[2 + j * 2])
155                                                                 << 4) +
156                                                 hex2bin(search[3 + j * 2]);
157                                 }
158                                 isfp = true;
159                         } else if (search != NULL && strlen(search) == 66 &&
160                                         search[0] == '0' && search[1] == 'x') {
161                                 /* v5 fingerprint */
162                                 fingerprint.length = 32;
163                                 for (j = 0; j < 32; j++) {
164                                         fingerprint.fp[j] = (hex2bin(
165                                                         search[2 + j * 2])
166                                                                 << 4) +
167                                                 hex2bin(search[3 + j * 2]);
168                                 }
169                                 isfp = true;
170                         } else if (search != NULL) {
171                                 keyid = strtoull(search, &end, 16);
172                                 if (*search != 0 &&
173                                                 end != NULL &&
174                                                 *end == 0) {
175                                         ishex = true;
176                                 }
177                         }
178                 } else if (!strcmp(params[i], "idx")) {
179                         indx = atoi(params[i+1]);
180                 } else if (!strcmp(params[i], "fingerprint")) {
181                         if (!strcmp(params[i+1], "on")) {
182                                 dispfp = true;
183                         }
184                 } else if (!strcmp(params[i], "hash")) {
185                         if (!strcmp(params[i+1], "on")) {
186                                 skshash = true;
187                         }
188                 } else if (!strcmp(params[i], "exact")) {
189                         if (!strcmp(params[i+1], "on")) {
190                                 exact = true;
191                         }
192                 } else if (!strcmp(params[i], "options")) {
193                         /*
194                          * TODO: We should be smarter about this; options may
195                          * have several entries. For now mr is the only valid
196                          * one though.
197                          */
198                         if (!strcmp(params[i+1], "mr")) {
199                                 mrhkp = true;
200                         }
201                 }
202                 free(params[i]);
203                 params[i] = NULL;
204                 if (params[i+1] != NULL) {
205                         free(params[i+1]);
206                         params[i+1] = NULL;
207                 }
208         }
209         if (params != NULL) {
210                 free(params);
211                 params = NULL;
212         }
213
214         if (mrhkp) {
215                 puts("Content-Type: text/plain\n");
216         } else if (op == OP_PHOTO) {
217                 puts("Content-Type: image/jpeg\n");
218         } else {
219                 start_html("Lookup of key");
220         }
221
222         if (op == OP_UNKNOWN) {
223                 puts("Error: No operation supplied.");
224         } else if (search == NULL) {
225                 puts("Error: No key to search for supplied.");
226         } else {
227                 readconfig(NULL);
228                 initlogthing("lookup", config.logfile);
229                 catchsignals();
230                 dbctx = config.dbinit(config.backend, false);
231                 switch (op) {
232                 case OP_GET:
233                 case OP_HGET:
234                         if (op == OP_HGET) {
235                                 parse_skshash(search, &hash);
236                                 result = dbctx->fetch_key_skshash(dbctx,
237                                         &hash, &publickey);
238                         } else if (ishex) {
239                                 result = dbctx->fetch_key_id(dbctx, keyid,
240                                         &publickey, false);
241                         } else if (isfp) {
242                                 result = dbctx->fetch_key_fp(dbctx,
243                                         &fingerprint, &publickey, false);
244                         } else {
245                                 result = dbctx->fetch_key_text(dbctx,
246                                         search,
247                                         &publickey);
248                         }
249                         if (result) {
250                                 logthing(LOGTHING_NOTICE, 
251                                         "Found %d key(s) for search %s",
252                                         result,
253                                         search);
254                                 puts("<pre>");
255                                 cleankeys(&publickey, config.clean_policies);
256                                 flatten_publickey(publickey,
257                                                         &packets,
258                                                         &list_end);
259                                 armor_openpgp_stream(stdout_putchar,
260                                                 NULL,
261                                                 packets);
262                                 puts("</pre>");
263                         } else {
264                                 logthing(LOGTHING_NOTICE,
265                                         "Failed to find key for search %s",
266                                         search);
267                                 puts("Key not found");
268                         }
269                         break;
270                 case OP_INDEX:
271                         find_keys(dbctx, search, keyid, &fingerprint,
272                                         ishex, isfp, dispfp, skshash,
273                                         exact, false, mrhkp);
274                         break;
275                 case OP_VINDEX:
276                         find_keys(dbctx, search, keyid, &fingerprint,
277                                         ishex, isfp, dispfp, skshash,
278                                         exact, true, mrhkp);
279                         break;
280                 case OP_PHOTO:
281                         if (isfp) {
282                                 dbctx->fetch_key_fp(dbctx, &fingerprint,
283                                         &publickey, false);
284                         } else {
285                                 dbctx->fetch_key_id(dbctx, keyid,
286                                         &publickey, false);
287                         }
288                         if (publickey != NULL) {
289                                 unsigned char *photo = NULL;
290                                 size_t         length = 0;
291
292                                 if (getphoto(publickey, indx, &photo,
293                                                 &length) == ONAK_E_OK) {
294                                         fwrite(photo,
295                                                         1,
296                                                         length,
297                                                         stdout);
298                                 }
299                                 free_publickey(publickey);
300                                 publickey = NULL;
301                         }
302                         break;
303                 default:
304                         puts("Unknown operation!");
305                 }
306                 dbctx->cleanupdb(dbctx);
307                 cleanuplogthing();
308                 cleanupconfig();
309         }
310         if (!mrhkp) {
311                 puts("<hr>");
312                 puts("Produced by onak " ONAK_VERSION );
313                 end_html();
314         }
315
316         if (search != NULL) {
317                 free(search);
318                 search = NULL;
319         }
320         
321         return (EXIT_SUCCESS);
322 }