]> git.sommitrealweird.co.uk Git - onak.git/blob - lookup.c
5d638eab6ef224100a691bff9f8c170ee9498be5
[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.15 2004/05/27 01:25:37 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 %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                                 puts("Key not found");
176                         }
177                         break;
178                 case OP_INDEX:
179                         find_keys(search, keyid, ishex, fingerprint, exact,
180                                         false, mrhkp);
181                         break;
182                 case OP_VINDEX:
183                         find_keys(search, keyid, ishex, fingerprint, exact,
184                                         true, mrhkp);
185                         break;
186                 case OP_PHOTO:
187                         if (fetch_key(keyid, &publickey, false)) {
188                                 struct openpgp_packet *photo = NULL;
189                                 photo = getphoto(publickey, 0);
190                                 if (photo != NULL) {
191                                         fwrite(photo->data+19,
192                                                         1,
193                                                         (photo->length - 19),
194                                                         stdout);
195                                 }
196                                 free_publickey(publickey);
197                                 publickey = NULL;
198                         }
199                         break;
200                 default:
201                         puts("Unknown operation!");
202                 }
203                 cleanupdb();
204                 cleanuplogthing();
205                 cleanupconfig();
206         }
207         if (!mrhkp) {
208                 puts("<hr>");
209                 puts("Produced by onak " VERSION " by Jonathan McDowell");
210                 end_html();
211         }
212
213         if (search != NULL) {
214                 free(search);
215                 search = NULL;
216         }
217         
218         return (EXIT_SUCCESS);
219 }