cscvs to tla changeset 133
[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.16 2004/05/27 21:58:18 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 "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                 initdb(true);
161                 switch (op) {
162                 case OP_GET:
163                         logthing(LOGTHING_NOTICE, "Getting keyid 0x%llX",
164                                         keyid);
165                         if (fetch_key(keyid, &publickey, false)) {
166                                 puts("<pre>");
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 }