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