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