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