]> git.sommitrealweird.co.uk Git - onak.git/blob - lookup.c
9a9ccbd7a80d68c46a300db30ccb0430058b4aa3
[onak.git] / lookup.c
1 /*
2  * lookup.c - CGI to lookup keys.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include <inttypes.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include "armor.h"
17 #include "charfuncs.h"
18 #include "cleankey.h"
19 #include "cleanup.h"
20 #include "getcgi.h"
21 #include "keydb.h"
22 #include "keyid.h"
23 #include "keyindex.h"
24 #include "log.h"
25 #include "mem.h"
26 #include "onak-conf.h"
27 #include "parsekey.h"
28 #include "photoid.h"
29 #include "version.h"
30
31 #define OP_UNKNOWN 0
32 #define OP_GET     1
33 #define OP_INDEX   2
34 #define OP_VINDEX  3
35 #define OP_PHOTO   4
36 #define OP_HGET    5
37
38 void find_keys(char *search, uint64_t keyid, bool ishex,
39                 bool fingerprint, bool skshash, bool exact, bool verbose,
40                 bool mrhkp)
41 {
42         struct openpgp_publickey *publickey = NULL;
43         int count = 0;
44
45         if (ishex) {
46                 count = config.dbbackend->fetch_key(keyid, &publickey, false);
47         } else {
48                 count = config.dbbackend->fetch_key_text(search, &publickey);
49         }
50         if (publickey != NULL) {
51                 if (mrhkp) {
52                         printf("info:1:%d\n", count);
53                         mrkey_index(publickey);
54                 } else {
55                         key_index(publickey, verbose, fingerprint, skshash,
56                                 true);
57                 }
58                 free_publickey(publickey);
59         } else if (count == 0) {
60                 if (mrhkp) {
61                         puts("info:1:0");
62                 } else {
63                         puts("Key not found.");
64                 }
65         } else {
66                 if (mrhkp) {
67                         puts("info:1:0");
68                 } else {
69                         printf("Found %d keys, but maximum number to return"
70                                 " is %d.\n",
71                                 count,
72                                 config.maxkeys);
73                         puts("Try again with a more specific search.");
74                 }
75         }
76 }
77
78 int main(int argc, char *argv[])
79 {
80         char **params = NULL;
81         int op = OP_UNKNOWN;
82         int i;
83         int indx = 0;
84         bool fingerprint = false;
85         bool skshash = false;
86         bool exact = false;
87         bool ishex = false;
88         bool mrhkp = false;
89         uint64_t keyid = 0;
90         char *search = NULL;
91         char *end = NULL;
92         struct openpgp_publickey *publickey = NULL;
93         struct openpgp_packet_list *packets = NULL;
94         struct openpgp_packet_list *list_end = NULL;
95         int result;
96         struct skshash hash;
97
98         params = getcgivars(argc, argv);
99         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
100                 if (!strcmp(params[i], "op")) {
101                         if (!strcmp(params[i+1], "get")) {
102                                 op = OP_GET;
103                         } else if (!strcmp(params[i+1], "hget")) {
104                                 op = OP_HGET;
105                         } else if (!strcmp(params[i+1], "index")) {
106                                 op = OP_INDEX;
107                         } else if (!strcmp(params[i+1], "vindex")) {
108                                 op = OP_VINDEX;
109                         } else if (!strcmp(params[i+1], "photo")) {
110                                 op = OP_PHOTO;
111                         }
112                 } else if (!strcmp(params[i], "search")) {
113                         search = params[i+1];
114                         params[i+1] = NULL;
115                         if (search != NULL) {
116                                 keyid = strtoull(search, &end, 16);
117                                 if (*search != 0 &&
118                                                 end != NULL &&
119                                                 *end == 0) {
120                                         ishex = true;
121                                 }
122                         }
123                 } else if (!strcmp(params[i], "idx")) {
124                         indx = atoi(params[i+1]);
125                 } else if (!strcmp(params[i], "fingerprint")) {
126                         if (!strcmp(params[i+1], "on")) {
127                                 fingerprint = true;
128                         }
129                 } else if (!strcmp(params[i], "hash")) {
130                         if (!strcmp(params[i+1], "on")) {
131                                 skshash = true;
132                         }
133                 } else if (!strcmp(params[i], "exact")) {
134                         if (!strcmp(params[i+1], "on")) {
135                                 exact = true;
136                         }
137                 } else if (!strcmp(params[i], "options")) {
138                         /*
139                          * TODO: We should be smarter about this; options may
140                          * have several entries. For now mr is the only valid
141                          * one though.
142                          */
143                         if (!strcmp(params[i+1], "mr")) {
144                                 mrhkp = true;
145                         }
146                 }
147                 free(params[i]);
148                 params[i] = NULL;
149                 if (params[i+1] != NULL) {
150                         free(params[i+1]);
151                         params[i+1] = NULL;
152                 }
153         }
154         if (params != NULL) {
155                 free(params);
156                 params = NULL;
157         }
158
159         if (mrhkp) {
160                 puts("Content-Type: text/plain\n");
161         } else if (op == OP_PHOTO) {
162                 puts("Content-Type: image/jpeg\n");
163         } else {
164                 start_html("Lookup of key");
165         }
166
167         if (op == OP_UNKNOWN) {
168                 puts("Error: No operation supplied.");
169         } else if (search == NULL) {
170                 puts("Error: No key to search for supplied.");
171         } else {
172                 readconfig(NULL);
173                 initlogthing("lookup", config.logfile);
174                 catchsignals();
175                 config.dbbackend->initdb(false);
176                 switch (op) {
177                 case OP_GET:
178                 case OP_HGET:
179                         if (op == OP_HGET) {
180                                 parse_skshash(search, &hash);
181                                 result = config.dbbackend->fetch_key_skshash(
182                                         &hash, &publickey);
183                         } else if (ishex) {
184                                 result = config.dbbackend->fetch_key(keyid,
185                                         &publickey, false);
186                         } else {
187                                 result = config.dbbackend->fetch_key_text(
188                                         search,
189                                         &publickey);
190                         }
191                         if (result) {
192                                 logthing(LOGTHING_NOTICE, 
193                                         "Found %d key(s) for search %s",
194                                         result,
195                                         search);
196                                 puts("<pre>");
197                                 cleankeys(publickey);
198                                 flatten_publickey(publickey,
199                                                         &packets,
200                                                         &list_end);
201                                 armor_openpgp_stream(stdout_putchar,
202                                                 NULL,
203                                                 packets);
204                                 puts("</pre>");
205                         } else {
206                                 logthing(LOGTHING_NOTICE,
207                                         "Failed to find key for search %s",
208                                         search);
209                                 puts("Key not found");
210                         }
211                         break;
212                 case OP_INDEX:
213                         find_keys(search, keyid, ishex, fingerprint, skshash,
214                                         exact, false, mrhkp);
215                         break;
216                 case OP_VINDEX:
217                         find_keys(search, keyid, ishex, fingerprint, skshash,
218                                         exact, true, mrhkp);
219                         break;
220                 case OP_PHOTO:
221                         if (config.dbbackend->fetch_key(keyid, &publickey,
222                                         false)) {
223                                 unsigned char *photo = NULL;
224                                 size_t         length = 0;
225
226                                 if (getphoto(publickey, indx, &photo,
227                                                 &length)) {
228                                         fwrite(photo,
229                                                         1,
230                                                         length,
231                                                         stdout);
232                                 }
233                                 free_publickey(publickey);
234                                 publickey = NULL;
235                         }
236                         break;
237                 default:
238                         puts("Unknown operation!");
239                 }
240                 config.dbbackend->cleanupdb();
241                 cleanuplogthing();
242                 cleanupconfig();
243         }
244         if (!mrhkp) {
245                 puts("<hr>");
246                 puts("Produced by onak " ONAK_VERSION );
247                 end_html();
248         }
249
250         if (search != NULL) {
251                 free(search);
252                 search = NULL;
253         }
254         
255         return (EXIT_SUCCESS);
256 }