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