]> git.sommitrealweird.co.uk Git - onak.git/blob - lookup.c
95ab6af01f2f54454474e87c4143bcdfb29f4e6f
[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                 readconfig();
123                 initdb();
124                 switch (op) {
125                 case OP_GET:
126                         if (fetch_key(keyid, &publickey, false)) {
127                                 puts("<pre>");
128                                 flatten_publickey(publickey,
129                                                         &packets,
130                                                         &list_end);
131                                 armor_openpgp_stream(putnextchar,
132                                                 NULL,
133                                                 packets);
134                                 puts("</pre>");
135                         } else {
136                                 puts("Key not found");
137                         }
138                         break;
139                 case OP_INDEX:
140                         find_keys(search, keyid, ishex, fingerprint, exact,
141                                         false);
142                         break;
143                 case OP_VINDEX:
144                         find_keys(search, keyid, ishex, fingerprint, exact,
145                                         true);
146                         break;
147                 default:
148                         puts("Unknown operation!");
149                 }
150                 cleanupdb();
151         }
152         puts("<hr>");
153         puts("Produced by onak " VERSION " by Jonathan McDowell");
154         end_html();
155
156         if (search != NULL) {
157                 free(search);
158                 search = NULL;
159         }
160         
161         return (EXIT_SUCCESS);
162 }