]> the.earth.li Git - onak.git/blob - lookup.c
cscvs to tla changeset 1
[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
9 //#include <stdint.h>
10 #include <inttypes.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15
16 #include "armor.h"
17 #include "getcgi.h"
18 #include "keydb.h"
19 #include "keyindex.h"
20 #include "mem.h"
21 #include "parsekey.h"
22
23 #define OP_UNKNOWN 0
24 #define OP_GET     1
25 #define OP_INDEX   2
26 #define OP_VINDEX  3
27
28 int putnextchar(void *ctx, unsigned char c)
29 {
30         return putchar(c);
31 }
32
33 void find_keys(char *search, uint64_t keyid, bool ishex,
34                 bool fingerprint, bool exact, bool verbose)
35 {
36         struct openpgp_publickey *publickey = NULL;
37         bool found = false;
38
39         if (ishex) {
40                 if (fetch_key(keyid, &publickey)) {
41                         if (publickey != NULL) {
42                                 key_index(publickey, verbose, fingerprint,
43                                                 true);
44                                 free_publickey(publickey);
45                                 found = true;
46                         }
47                 }
48         }
49         if (!found) {
50                 puts("Key not found.");
51         }
52 }
53
54 int main(int argc, char *argv[])
55 {
56         char **params = NULL;
57         int op = OP_UNKNOWN;
58         int i;
59         bool fingerprint = false;
60         bool exact = false;
61         bool ishex = false;
62         uint64_t keyid = 0;
63         char *search = NULL;
64         char *end = NULL;
65         struct openpgp_publickey *publickey = NULL;
66         struct openpgp_packet_list *packets = NULL;
67         struct openpgp_packet_list *list_end = NULL;
68
69         params = getcgivars(argc, argv);
70         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
71                 if (!strcmp(params[i], "op")) {
72                         if (!strcmp(params[i+1], "get")) {
73                                 op = OP_GET;
74                         } else if (!strcmp(params[i+1], "index")) {
75                                 op = OP_INDEX;
76                         } else if (!strcmp(params[i+1], "vindex")) {
77                                 op = OP_VINDEX;
78                         }
79                 } else if (!strcmp(params[i], "search")) {
80                         search = params[i+1];
81                         if (search != NULL) {
82                                 keyid = strtoul(search, &end, 16);
83                                 if (*search != 0 &&
84                                                 end != NULL &&
85                                                 *end == 0) {
86                                         ishex = true;
87                                 }
88                         }
89                 } else if (!strcmp(params[i], "fingerprint")) {
90                         if (!strcmp(params[i+1], "on")) {
91                                 fingerprint = true;
92                         }
93                 } else if (!strcmp(params[i], "exact")) {
94                         if (!strcmp(params[i+1], "on")) {
95                                 exact = true;
96                         }
97                 }
98         }
99
100 //      puts("HTTP/1.0 200 OK");
101 //      puts("Server: onak 0.0.1");
102         puts("Content-Type: text/html\n");
103         puts("<html>\n<title>Lookup of key</title>");
104         puts("<body>");
105
106         if (op == OP_UNKNOWN) {
107                 puts("Error: No operation supplied.");
108         } else if (search == NULL) {
109                 puts("Error: No key to search for supplied.");
110         } else {
111                 initdb();
112                 switch (op) {
113                 case OP_GET:
114                         if (fetch_key(keyid, &publickey)) {
115                                 puts("<pre>");
116                                 flatten_publickey(publickey,
117                                                         &packets,
118                                                         &list_end);
119                                 armor_openpgp_stream(putnextchar,
120                                                 NULL,
121                                                 packets);
122                                 puts("</pre>");
123                         } else {
124                                 puts("Key not found");
125                         }
126                         break;
127                 case OP_INDEX:
128                         find_keys(search, keyid, ishex, fingerprint, exact,
129                                         false);
130                         break;
131                 case OP_VINDEX:
132                         find_keys(search, keyid, ishex, fingerprint, exact,
133                                         true);
134                         break;
135                 default:
136                         puts("Unknown operation!");
137                 }
138                 cleanupdb();
139         }
140         puts("<hr>");
141         puts("Produced by onak 0.0.1 by Jonathan McDowell");
142         puts("</body>\n</html>");
143         return (EXIT_SUCCESS);
144 }