]> the.earth.li Git - onak.git/blob - cgi/lookup.c
Fix compilation with later versions of Nettle
[onak.git] / cgi / 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(dbctx, &publickey,
256                                                 config.clean_policies);
257                                 flatten_publickey(publickey,
258                                                         &packets,
259                                                         &list_end);
260                                 armor_openpgp_stream(stdout_putchar,
261                                                 NULL,
262                                                 packets);
263                                 puts("</pre>");
264                         } else {
265                                 logthing(LOGTHING_NOTICE,
266                                         "Failed to find key for search %s",
267                                         search);
268                                 puts("Key not found");
269                         }
270                         break;
271                 case OP_INDEX:
272                         find_keys(dbctx, search, keyid, &fingerprint,
273                                         ishex, isfp, dispfp, skshash,
274                                         exact, false, mrhkp);
275                         break;
276                 case OP_VINDEX:
277                         find_keys(dbctx, search, keyid, &fingerprint,
278                                         ishex, isfp, dispfp, skshash,
279                                         exact, true, mrhkp);
280                         break;
281                 case OP_PHOTO:
282                         if (isfp) {
283                                 dbctx->fetch_key_fp(dbctx, &fingerprint,
284                                         &publickey, false);
285                         } else {
286                                 dbctx->fetch_key_id(dbctx, keyid,
287                                         &publickey, false);
288                         }
289                         if (publickey != NULL) {
290                                 unsigned char *photo = NULL;
291                                 size_t         length = 0;
292
293                                 if (getphoto(publickey, indx, &photo,
294                                                 &length) == ONAK_E_OK) {
295                                         fwrite(photo,
296                                                         1,
297                                                         length,
298                                                         stdout);
299                                 }
300                                 free_publickey(publickey);
301                                 publickey = NULL;
302                         }
303                         break;
304                 default:
305                         puts("Unknown operation!");
306                 }
307                 dbctx->cleanupdb(dbctx);
308                 cleanuplogthing();
309                 cleanupconfig();
310         }
311         if (!mrhkp) {
312                 puts("<hr>");
313                 puts("Produced by onak " ONAK_VERSION );
314                 end_html();
315         }
316
317         if (search != NULL) {
318                 free(search);
319                 search = NULL;
320         }
321         
322         return (EXIT_SUCCESS);
323 }