]> git.sommitrealweird.co.uk Git - onak.git/blob - lookup.c
50426004a4a6074dcd2cb27d5efc775795e2bc2f
[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  * $Id: lookup.c,v 1.17 2004/05/31 14:16:49 noodles Exp $
9  */
10
11 #include <inttypes.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include "armor.h"
19 #include "charfuncs.h"
20 #include "cleankey.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 = strtoul(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                 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 }