]> git.sommitrealweird.co.uk Git - onak.git/blob - lookup.c
273afc225c04d567636d92bcfc50d44eaa541752
[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, bool ishex,
50                 bool fingerprint, bool skshash, bool exact, bool verbose,
51                 bool mrhkp)
52 {
53         struct openpgp_publickey *publickey = NULL;
54         int count = 0;
55
56         if (ishex) {
57                 count = config.dbbackend->fetch_key(keyid, &publickey, false);
58         } else {
59                 count = config.dbbackend->fetch_key_text(search, &publickey);
60         }
61         if (publickey != NULL) {
62                 if (mrhkp) {
63                         printf("info:1:%d\n", count);
64                         mrkey_index(publickey);
65                 } else {
66                         key_index(publickey, verbose, fingerprint, skshash,
67                                 true);
68                 }
69                 free_publickey(publickey);
70         } else if (count == 0) {
71                 if (mrhkp) {
72                         puts("info:1:0");
73                 } else {
74                         puts("Key not found.");
75                 }
76         } else {
77                 if (mrhkp) {
78                         puts("info:1:0");
79                 } else {
80                         printf("Found %d keys, but maximum number to return"
81                                 " is %d.\n",
82                                 count,
83                                 config.maxkeys);
84                         puts("Try again with a more specific search.");
85                 }
86         }
87 }
88
89 int main(int argc, char *argv[])
90 {
91         char **params = NULL;
92         int op = OP_UNKNOWN;
93         int i;
94         int indx = 0;
95         bool fingerprint = false;
96         bool skshash = false;
97         bool exact = false;
98         bool ishex = false;
99         bool mrhkp = false;
100         uint64_t keyid = 0;
101         char *search = NULL;
102         char *end = NULL;
103         struct openpgp_publickey *publickey = NULL;
104         struct openpgp_packet_list *packets = NULL;
105         struct openpgp_packet_list *list_end = NULL;
106         int result;
107         struct skshash hash;
108
109         params = getcgivars(argc, argv);
110         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
111                 if (!strcmp(params[i], "op")) {
112                         if (!strcmp(params[i+1], "get")) {
113                                 op = OP_GET;
114                         } else if (!strcmp(params[i+1], "hget")) {
115                                 op = OP_HGET;
116                         } else if (!strcmp(params[i+1], "index")) {
117                                 op = OP_INDEX;
118                         } else if (!strcmp(params[i+1], "vindex")) {
119                                 op = OP_VINDEX;
120                         } else if (!strcmp(params[i+1], "photo")) {
121                                 op = OP_PHOTO;
122                         }
123                 } else if (!strcmp(params[i], "search")) {
124                         search = params[i+1];
125                         params[i+1] = NULL;
126                         if (search != NULL && strlen(search) == 42 &&
127                                         search[0] == '0' && search[1] == 'x') {
128                                 /*
129                                  * Fingerprint. Truncate to last 64 bits for
130                                  * now.
131                                  */
132                                 keyid = strtoull(&search[26], &end, 16);
133                                 if (end != NULL && *end == 0) {
134                                         ishex = true;
135                                 }
136                         } else if (search != NULL) {
137                                 keyid = strtoull(search, &end, 16);
138                                 if (*search != 0 &&
139                                                 end != NULL &&
140                                                 *end == 0) {
141                                         ishex = true;
142                                 }
143                         }
144                 } else if (!strcmp(params[i], "idx")) {
145                         indx = atoi(params[i+1]);
146                 } else if (!strcmp(params[i], "fingerprint")) {
147                         if (!strcmp(params[i+1], "on")) {
148                                 fingerprint = true;
149                         }
150                 } else if (!strcmp(params[i], "hash")) {
151                         if (!strcmp(params[i+1], "on")) {
152                                 skshash = true;
153                         }
154                 } else if (!strcmp(params[i], "exact")) {
155                         if (!strcmp(params[i+1], "on")) {
156                                 exact = true;
157                         }
158                 } else if (!strcmp(params[i], "options")) {
159                         /*
160                          * TODO: We should be smarter about this; options may
161                          * have several entries. For now mr is the only valid
162                          * one though.
163                          */
164                         if (!strcmp(params[i+1], "mr")) {
165                                 mrhkp = true;
166                         }
167                 }
168                 free(params[i]);
169                 params[i] = NULL;
170                 if (params[i+1] != NULL) {
171                         free(params[i+1]);
172                         params[i+1] = NULL;
173                 }
174         }
175         if (params != NULL) {
176                 free(params);
177                 params = NULL;
178         }
179
180         if (mrhkp) {
181                 puts("Content-Type: text/plain\n");
182         } else if (op == OP_PHOTO) {
183                 puts("Content-Type: image/jpeg\n");
184         } else {
185                 start_html("Lookup of key");
186         }
187
188         if (op == OP_UNKNOWN) {
189                 puts("Error: No operation supplied.");
190         } else if (search == NULL) {
191                 puts("Error: No key to search for supplied.");
192         } else {
193                 readconfig(NULL);
194                 initlogthing("lookup", config.logfile);
195                 catchsignals();
196                 config.dbbackend->initdb(false);
197                 switch (op) {
198                 case OP_GET:
199                 case OP_HGET:
200                         if (op == OP_HGET) {
201                                 parse_skshash(search, &hash);
202                                 result = config.dbbackend->fetch_key_skshash(
203                                         &hash, &publickey);
204                         } else if (ishex) {
205                                 result = config.dbbackend->fetch_key(keyid,
206                                         &publickey, false);
207                         } else {
208                                 result = config.dbbackend->fetch_key_text(
209                                         search,
210                                         &publickey);
211                         }
212                         if (result) {
213                                 logthing(LOGTHING_NOTICE, 
214                                         "Found %d key(s) for search %s",
215                                         result,
216                                         search);
217                                 puts("<pre>");
218                                 cleankeys(publickey);
219                                 flatten_publickey(publickey,
220                                                         &packets,
221                                                         &list_end);
222                                 armor_openpgp_stream(stdout_putchar,
223                                                 NULL,
224                                                 packets);
225                                 puts("</pre>");
226                         } else {
227                                 logthing(LOGTHING_NOTICE,
228                                         "Failed to find key for search %s",
229                                         search);
230                                 puts("Key not found");
231                         }
232                         break;
233                 case OP_INDEX:
234                         find_keys(search, keyid, ishex, fingerprint, skshash,
235                                         exact, false, mrhkp);
236                         break;
237                 case OP_VINDEX:
238                         find_keys(search, keyid, ishex, fingerprint, skshash,
239                                         exact, true, mrhkp);
240                         break;
241                 case OP_PHOTO:
242                         if (config.dbbackend->fetch_key(keyid, &publickey,
243                                         false)) {
244                                 unsigned char *photo = NULL;
245                                 size_t         length = 0;
246
247                                 if (getphoto(publickey, indx, &photo,
248                                                 &length)) {
249                                         fwrite(photo,
250                                                         1,
251                                                         length,
252                                                         stdout);
253                                 }
254                                 free_publickey(publickey);
255                                 publickey = NULL;
256                         }
257                         break;
258                 default:
259                         puts("Unknown operation!");
260                 }
261                 config.dbbackend->cleanupdb();
262                 cleanuplogthing();
263                 cleanupconfig();
264         }
265         if (!mrhkp) {
266                 puts("<hr>");
267                 puts("Produced by onak " ONAK_VERSION );
268                 end_html();
269         }
270
271         if (search != NULL) {
272                 free(search);
273                 search = NULL;
274         }
275         
276         return (EXIT_SUCCESS);
277 }