]> the.earth.li Git - onak.git/blob - lookup.c
Move to CMake over autoconf
[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                                 fingerprint.length = MAX_FINGERPRINT_LEN;
151                                 for (j = 0; j < MAX_FINGERPRINT_LEN; j++) {
152                                         fingerprint.fp[j] = (hex2bin(
153                                                         search[2 + j * 2])
154                                                                 << 4) +
155                                                 hex2bin(search[3 + j * 2]);
156                                 }
157                                 isfp = true;
158                         } else if (search != NULL) {
159                                 keyid = strtoull(search, &end, 16);
160                                 if (*search != 0 &&
161                                                 end != NULL &&
162                                                 *end == 0) {
163                                         ishex = true;
164                                 }
165                         }
166                 } else if (!strcmp(params[i], "idx")) {
167                         indx = atoi(params[i+1]);
168                 } else if (!strcmp(params[i], "fingerprint")) {
169                         if (!strcmp(params[i+1], "on")) {
170                                 dispfp = true;
171                         }
172                 } else if (!strcmp(params[i], "hash")) {
173                         if (!strcmp(params[i+1], "on")) {
174                                 skshash = true;
175                         }
176                 } else if (!strcmp(params[i], "exact")) {
177                         if (!strcmp(params[i+1], "on")) {
178                                 exact = true;
179                         }
180                 } else if (!strcmp(params[i], "options")) {
181                         /*
182                          * TODO: We should be smarter about this; options may
183                          * have several entries. For now mr is the only valid
184                          * one though.
185                          */
186                         if (!strcmp(params[i+1], "mr")) {
187                                 mrhkp = true;
188                         }
189                 }
190                 free(params[i]);
191                 params[i] = NULL;
192                 if (params[i+1] != NULL) {
193                         free(params[i+1]);
194                         params[i+1] = NULL;
195                 }
196         }
197         if (params != NULL) {
198                 free(params);
199                 params = NULL;
200         }
201
202         if (mrhkp) {
203                 puts("Content-Type: text/plain\n");
204         } else if (op == OP_PHOTO) {
205                 puts("Content-Type: image/jpeg\n");
206         } else {
207                 start_html("Lookup of key");
208         }
209
210         if (op == OP_UNKNOWN) {
211                 puts("Error: No operation supplied.");
212         } else if (search == NULL) {
213                 puts("Error: No key to search for supplied.");
214         } else {
215                 readconfig(NULL);
216                 initlogthing("lookup", config.logfile);
217                 catchsignals();
218                 dbctx = config.dbinit(config.backend, false);
219                 switch (op) {
220                 case OP_GET:
221                 case OP_HGET:
222                         if (op == OP_HGET) {
223                                 parse_skshash(search, &hash);
224                                 result = dbctx->fetch_key_skshash(dbctx,
225                                         &hash, &publickey);
226                         } else if (ishex) {
227                                 result = dbctx->fetch_key_id(dbctx, keyid,
228                                         &publickey, false);
229                         } else if (isfp) {
230                                 result = dbctx->fetch_key_fp(dbctx,
231                                         &fingerprint, &publickey, false);
232                         } else {
233                                 result = dbctx->fetch_key_text(dbctx,
234                                         search,
235                                         &publickey);
236                         }
237                         if (result) {
238                                 logthing(LOGTHING_NOTICE, 
239                                         "Found %d key(s) for search %s",
240                                         result,
241                                         search);
242                                 puts("<pre>");
243                                 cleankeys(&publickey, config.clean_policies);
244                                 flatten_publickey(publickey,
245                                                         &packets,
246                                                         &list_end);
247                                 armor_openpgp_stream(stdout_putchar,
248                                                 NULL,
249                                                 packets);
250                                 puts("</pre>");
251                         } else {
252                                 logthing(LOGTHING_NOTICE,
253                                         "Failed to find key for search %s",
254                                         search);
255                                 puts("Key not found");
256                         }
257                         break;
258                 case OP_INDEX:
259                         find_keys(dbctx, search, keyid, &fingerprint,
260                                         ishex, isfp, dispfp, skshash,
261                                         exact, false, mrhkp);
262                         break;
263                 case OP_VINDEX:
264                         find_keys(dbctx, search, keyid, &fingerprint,
265                                         ishex, isfp, dispfp, skshash,
266                                         exact, true, mrhkp);
267                         break;
268                 case OP_PHOTO:
269                         if (isfp) {
270                                 dbctx->fetch_key_fp(dbctx, &fingerprint,
271                                         &publickey, false);
272                         } else {
273                                 dbctx->fetch_key_id(dbctx, keyid,
274                                         &publickey, false);
275                         }
276                         if (publickey != NULL) {
277                                 unsigned char *photo = NULL;
278                                 size_t         length = 0;
279
280                                 if (getphoto(publickey, indx, &photo,
281                                                 &length) == ONAK_E_OK) {
282                                         fwrite(photo,
283                                                         1,
284                                                         length,
285                                                         stdout);
286                                 }
287                                 free_publickey(publickey);
288                                 publickey = NULL;
289                         }
290                         break;
291                 default:
292                         puts("Unknown operation!");
293                 }
294                 dbctx->cleanupdb(dbctx);
295                 cleanuplogthing();
296                 cleanupconfig();
297         }
298         if (!mrhkp) {
299                 puts("<hr>");
300                 puts("Produced by onak " ONAK_VERSION );
301                 end_html();
302         }
303
304         if (search != NULL) {
305                 free(search);
306                 search = NULL;
307         }
308         
309         return (EXIT_SUCCESS);
310 }