]> git.sommitrealweird.co.uk Git - onak.git/blob - lookup.c
cdd8c72c23e52bbe2a7d102302abee1d492d8422
[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) {
127                                 keyid = strtoull(search, &end, 16);
128                                 if (*search != 0 &&
129                                                 end != NULL &&
130                                                 *end == 0) {
131                                         ishex = true;
132                                 }
133                         }
134                 } else if (!strcmp(params[i], "idx")) {
135                         indx = atoi(params[i+1]);
136                 } else if (!strcmp(params[i], "fingerprint")) {
137                         if (!strcmp(params[i+1], "on")) {
138                                 fingerprint = true;
139                         }
140                 } else if (!strcmp(params[i], "hash")) {
141                         if (!strcmp(params[i+1], "on")) {
142                                 skshash = true;
143                         }
144                 } else if (!strcmp(params[i], "exact")) {
145                         if (!strcmp(params[i+1], "on")) {
146                                 exact = true;
147                         }
148                 } else if (!strcmp(params[i], "options")) {
149                         /*
150                          * TODO: We should be smarter about this; options may
151                          * have several entries. For now mr is the only valid
152                          * one though.
153                          */
154                         if (!strcmp(params[i+1], "mr")) {
155                                 mrhkp = true;
156                         }
157                 }
158                 free(params[i]);
159                 params[i] = NULL;
160                 if (params[i+1] != NULL) {
161                         free(params[i+1]);
162                         params[i+1] = NULL;
163                 }
164         }
165         if (params != NULL) {
166                 free(params);
167                 params = NULL;
168         }
169
170         if (mrhkp) {
171                 puts("Content-Type: text/plain\n");
172         } else if (op == OP_PHOTO) {
173                 puts("Content-Type: image/jpeg\n");
174         } else {
175                 start_html("Lookup of key");
176         }
177
178         if (op == OP_UNKNOWN) {
179                 puts("Error: No operation supplied.");
180         } else if (search == NULL) {
181                 puts("Error: No key to search for supplied.");
182         } else {
183                 readconfig(NULL);
184                 initlogthing("lookup", config.logfile);
185                 catchsignals();
186                 config.dbbackend->initdb(false);
187                 switch (op) {
188                 case OP_GET:
189                 case OP_HGET:
190                         if (op == OP_HGET) {
191                                 parse_skshash(search, &hash);
192                                 result = config.dbbackend->fetch_key_skshash(
193                                         &hash, &publickey);
194                         } else if (ishex) {
195                                 result = config.dbbackend->fetch_key(keyid,
196                                         &publickey, false);
197                         } else {
198                                 result = config.dbbackend->fetch_key_text(
199                                         search,
200                                         &publickey);
201                         }
202                         if (result) {
203                                 logthing(LOGTHING_NOTICE, 
204                                         "Found %d key(s) for search %s",
205                                         result,
206                                         search);
207                                 puts("<pre>");
208                                 cleankeys(publickey);
209                                 flatten_publickey(publickey,
210                                                         &packets,
211                                                         &list_end);
212                                 armor_openpgp_stream(stdout_putchar,
213                                                 NULL,
214                                                 packets);
215                                 puts("</pre>");
216                         } else {
217                                 logthing(LOGTHING_NOTICE,
218                                         "Failed to find key for search %s",
219                                         search);
220                                 puts("Key not found");
221                         }
222                         break;
223                 case OP_INDEX:
224                         find_keys(search, keyid, ishex, fingerprint, skshash,
225                                         exact, false, mrhkp);
226                         break;
227                 case OP_VINDEX:
228                         find_keys(search, keyid, ishex, fingerprint, skshash,
229                                         exact, true, mrhkp);
230                         break;
231                 case OP_PHOTO:
232                         if (config.dbbackend->fetch_key(keyid, &publickey,
233                                         false)) {
234                                 unsigned char *photo = NULL;
235                                 size_t         length = 0;
236
237                                 if (getphoto(publickey, indx, &photo,
238                                                 &length)) {
239                                         fwrite(photo,
240                                                         1,
241                                                         length,
242                                                         stdout);
243                                 }
244                                 free_publickey(publickey);
245                                 publickey = NULL;
246                         }
247                         break;
248                 default:
249                         puts("Unknown operation!");
250                 }
251                 config.dbbackend->cleanupdb();
252                 cleanuplogthing();
253                 cleanupconfig();
254         }
255         if (!mrhkp) {
256                 puts("<hr>");
257                 puts("Produced by onak " ONAK_VERSION );
258                 end_html();
259         }
260
261         if (search != NULL) {
262                 free(search);
263                 search = NULL;
264         }
265         
266         return (EXIT_SUCCESS);
267 }