]> git.sommitrealweird.co.uk Git - onak.git/blob - lookup.c
0.3.5 release.
[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
91         params = getcgivars(argc, argv);
92         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
93                 if (!strcmp(params[i], "op")) {
94                         if (!strcmp(params[i+1], "get")) {
95                                 op = OP_GET;
96                         } else if (!strcmp(params[i+1], "index")) {
97                                 op = OP_INDEX;
98                         } else if (!strcmp(params[i+1], "vindex")) {
99                                 op = OP_VINDEX;
100                         } else if (!strcmp(params[i+1], "photo")) {
101                                 op = OP_PHOTO;
102                         }
103                 } else if (!strcmp(params[i], "search")) {
104                         search = params[i+1];
105                         params[i+1] = NULL;
106                         if (search != NULL) {
107                                 keyid = strtoull(search, &end, 16);
108                                 if (*search != 0 &&
109                                                 end != NULL &&
110                                                 *end == 0) {
111                                         ishex = true;
112                                 }
113                         }
114                 } else if (!strcmp(params[i], "idx")) {
115                         indx = atoi(params[i+1]);
116                 } else if (!strcmp(params[i], "fingerprint")) {
117                         if (!strcmp(params[i+1], "on")) {
118                                 fingerprint = true;
119                         }
120                 } else if (!strcmp(params[i], "exact")) {
121                         if (!strcmp(params[i+1], "on")) {
122                                 exact = true;
123                         }
124                 } else if (!strcmp(params[i], "options")) {
125                         /*
126                          * TODO: We should be smarter about this; options may
127                          * have several entries. For now mr is the only valid
128                          * one though.
129                          */
130                         if (!strcmp(params[i+1], "mr")) {
131                                 mrhkp = true;
132                         }
133                 }
134                 free(params[i]);
135                 params[i] = NULL;
136                 if (params[i+1] != NULL) {
137                         free(params[i+1]);
138                         params[i+1] = NULL;
139                 }
140         }
141         if (params != NULL) {
142                 free(params);
143                 params = NULL;
144         }
145
146         if (mrhkp) {
147                 puts("Content-Type: text/plain\n");
148         } else if (op == OP_PHOTO) {
149                 puts("Content-Type: image/jpeg\n");
150         } else {
151                 start_html("Lookup of key");
152         }
153
154         if (op == OP_UNKNOWN) {
155                 puts("Error: No operation supplied.");
156         } else if (search == NULL) {
157                 puts("Error: No key to search for supplied.");
158         } else {
159                 readconfig(NULL);
160                 initlogthing("lookup", config.logfile);
161                 catchsignals();
162                 config.dbbackend->initdb(true);
163                 switch (op) {
164                 case OP_GET:
165                         logthing(LOGTHING_NOTICE, "Getting keyid 0x%llX",
166                                         keyid);
167                         if (config.dbbackend->fetch_key(keyid, &publickey,
168                                         false)) {
169                                 puts("<pre>");
170                                 cleankeys(publickey);
171                                 flatten_publickey(publickey,
172                                                         &packets,
173                                                         &list_end);
174                                 armor_openpgp_stream(stdout_putchar,
175                                                 NULL,
176                                                 packets);
177                                 puts("</pre>");
178                         } else {
179                                 logthing(LOGTHING_NOTICE,
180                                         "Failed to fetch key.");
181                                 puts("Key not found");
182                         }
183                         break;
184                 case OP_INDEX:
185                         find_keys(search, keyid, ishex, fingerprint, exact,
186                                         false, mrhkp);
187                         break;
188                 case OP_VINDEX:
189                         find_keys(search, keyid, ishex, fingerprint, exact,
190                                         true, mrhkp);
191                         break;
192                 case OP_PHOTO:
193                         if (config.dbbackend->fetch_key(keyid, &publickey,
194                                         false)) {
195                                 unsigned char *photo = NULL;
196                                 size_t         length = 0;
197
198                                 if (getphoto(publickey, 0, &photo, &length)) {
199                                         fwrite(photo,
200                                                         1,
201                                                         length,
202                                                         stdout);
203                                 }
204                                 free_publickey(publickey);
205                                 publickey = NULL;
206                         }
207                         break;
208                 default:
209                         puts("Unknown operation!");
210                 }
211                 config.dbbackend->cleanupdb();
212                 cleanuplogthing();
213                 cleanupconfig();
214         }
215         if (!mrhkp) {
216                 puts("<hr>");
217                 puts("Produced by onak " PACKAGE_VERSION 
218                                 " by Jonathan McDowell");
219                 end_html();
220         }
221
222         if (search != NULL) {
223                 free(search);
224                 search = NULL;
225         }
226         
227         return (EXIT_SUCCESS);
228 }