]> git.sommitrealweird.co.uk Git - onak.git/blob - lookup.c
Document our test keys
[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 = fetch_key(keyid, &publickey, false);
44         } else {
45                 count = 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                 initdb(true);
163                 switch (op) {
164                 case OP_GET:
165                         logthing(LOGTHING_NOTICE, "Getting keyid 0x%llX",
166                                         keyid);
167                         if (fetch_key(keyid, &publickey, false)) {
168                                 puts("<pre>");
169                                 cleankeys(publickey);
170                                 flatten_publickey(publickey,
171                                                         &packets,
172                                                         &list_end);
173                                 armor_openpgp_stream(stdout_putchar,
174                                                 NULL,
175                                                 packets);
176                                 puts("</pre>");
177                         } else {
178                                 logthing(LOGTHING_NOTICE,
179                                         "Failed to fetch key.");
180                                 puts("Key not found");
181                         }
182                         break;
183                 case OP_INDEX:
184                         find_keys(search, keyid, ishex, fingerprint, exact,
185                                         false, mrhkp);
186                         break;
187                 case OP_VINDEX:
188                         find_keys(search, keyid, ishex, fingerprint, exact,
189                                         true, mrhkp);
190                         break;
191                 case OP_PHOTO:
192                         if (fetch_key(keyid, &publickey, false)) {
193                                 unsigned char *photo = NULL;
194                                 size_t         length = 0;
195
196                                 if (getphoto(publickey, 0, &photo, &length)) {
197                                         fwrite(photo,
198                                                         1,
199                                                         length,
200                                                         stdout);
201                                 }
202                                 free_publickey(publickey);
203                                 publickey = NULL;
204                         }
205                         break;
206                 default:
207                         puts("Unknown operation!");
208                 }
209                 cleanupdb();
210                 cleanuplogthing();
211                 cleanupconfig();
212         }
213         if (!mrhkp) {
214                 puts("<hr>");
215                 puts("Produced by onak " PACKAGE_VERSION 
216                                 " by Jonathan McDowell");
217                 end_html();
218         }
219
220         if (search != NULL) {
221                 free(search);
222                 search = NULL;
223         }
224         
225         return (EXIT_SUCCESS);
226 }