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