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