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