]> git.sommitrealweird.co.uk Git - onak.git/blob - lookup.c
e4ac9cefaecbe36fdbfbb7ba815d3de916e63940
[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", (int) 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                         params[i+1] = NULL;
86                         if (search != NULL) {
87                                 keyid = strtoul(search, &end, 16);
88                                 if (*search != 0 &&
89                                                 end != NULL &&
90                                                 *end == 0) {
91                                         ishex = true;
92                                 }
93                         }
94                 } else if (!strcmp(params[i], "fingerprint")) {
95                         if (!strcmp(params[i+1], "on")) {
96                                 fingerprint = true;
97                         }
98                 } else if (!strcmp(params[i], "exact")) {
99                         if (!strcmp(params[i+1], "on")) {
100                                 exact = true;
101                         }
102                 }
103                 free(params[i]);
104                 params[i] = NULL;
105                 if (params[i+1] != NULL) {
106                         free(params[i+1]);
107                         params[i+1] = NULL;
108                 }
109         }
110         if (params != NULL) {
111                 free(params);
112                 params = NULL;
113         }
114
115         start_html("Lookup of key");
116
117         if (op == OP_UNKNOWN) {
118                 puts("Error: No operation supplied.");
119         } else if (search == NULL) {
120                 puts("Error: No key to search for supplied.");
121         } else {
122                 initdb();
123                 switch (op) {
124                 case OP_GET:
125                         if (fetch_key(keyid, &publickey, false)) {
126                                 puts("<pre>");
127                                 flatten_publickey(publickey,
128                                                         &packets,
129                                                         &list_end);
130                                 armor_openpgp_stream(putnextchar,
131                                                 NULL,
132                                                 packets);
133                                 puts("</pre>");
134                         } else {
135                                 puts("Key not found");
136                         }
137                         break;
138                 case OP_INDEX:
139                         find_keys(search, keyid, ishex, fingerprint, exact,
140                                         false);
141                         break;
142                 case OP_VINDEX:
143                         find_keys(search, keyid, ishex, fingerprint, exact,
144                                         true);
145                         break;
146                 default:
147                         puts("Unknown operation!");
148                 }
149                 cleanupdb();
150         }
151         puts("<hr>");
152         puts("Produced by onak " VERSION " by Jonathan McDowell");
153         end_html();
154
155         if (search != NULL) {
156                 free(search);
157                 search = NULL;
158         }
159         
160         return (EXIT_SUCCESS);
161 }