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