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