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