2 * lookup.c - CGI to lookup keys.
4 * Copyright 2002-2005,2007-2009,2011 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include "charfuncs.h"
37 #include "onak-conf.h"
49 void find_keys(char *search, uint64_t keyid, bool ishex,
50 bool fingerprint, bool skshash, bool exact, bool verbose,
53 struct openpgp_publickey *publickey = NULL;
57 count = config.dbbackend->fetch_key(keyid, &publickey, false);
59 count = config.dbbackend->fetch_key_text(search, &publickey);
61 if (publickey != NULL) {
63 printf("info:1:%d\n", count);
64 mrkey_index(publickey);
66 key_index(publickey, verbose, fingerprint, skshash,
69 free_publickey(publickey);
70 } else if (count == 0) {
74 puts("Key not found.");
80 printf("Found %d keys, but maximum number to return"
84 puts("Try again with a more specific search.");
89 int main(int argc, char *argv[])
95 bool fingerprint = false;
103 struct openpgp_publickey *publickey = NULL;
104 struct openpgp_packet_list *packets = NULL;
105 struct openpgp_packet_list *list_end = NULL;
109 params = getcgivars(argc, argv);
110 for (i = 0; params != NULL && params[i] != NULL; i += 2) {
111 if (!strcmp(params[i], "op")) {
112 if (!strcmp(params[i+1], "get")) {
114 } else if (!strcmp(params[i+1], "hget")) {
116 } else if (!strcmp(params[i+1], "index")) {
118 } else if (!strcmp(params[i+1], "vindex")) {
120 } else if (!strcmp(params[i+1], "photo")) {
123 } else if (!strcmp(params[i], "search")) {
124 search = params[i+1];
126 if (search != NULL) {
127 keyid = strtoull(search, &end, 16);
134 } else if (!strcmp(params[i], "idx")) {
135 indx = atoi(params[i+1]);
136 } else if (!strcmp(params[i], "fingerprint")) {
137 if (!strcmp(params[i+1], "on")) {
140 } else if (!strcmp(params[i], "hash")) {
141 if (!strcmp(params[i+1], "on")) {
144 } else if (!strcmp(params[i], "exact")) {
145 if (!strcmp(params[i+1], "on")) {
148 } else if (!strcmp(params[i], "options")) {
150 * TODO: We should be smarter about this; options may
151 * have several entries. For now mr is the only valid
154 if (!strcmp(params[i+1], "mr")) {
160 if (params[i+1] != NULL) {
165 if (params != NULL) {
171 puts("Content-Type: text/plain\n");
172 } else if (op == OP_PHOTO) {
173 puts("Content-Type: image/jpeg\n");
175 start_html("Lookup of key");
178 if (op == OP_UNKNOWN) {
179 puts("Error: No operation supplied.");
180 } else if (search == NULL) {
181 puts("Error: No key to search for supplied.");
184 initlogthing("lookup", config.logfile);
186 config.dbbackend->initdb(false);
191 parse_skshash(search, &hash);
192 result = config.dbbackend->fetch_key_skshash(
195 result = config.dbbackend->fetch_key(keyid,
198 result = config.dbbackend->fetch_key_text(
203 logthing(LOGTHING_NOTICE,
204 "Found %d key(s) for search %s",
208 cleankeys(publickey);
209 flatten_publickey(publickey,
212 armor_openpgp_stream(stdout_putchar,
217 logthing(LOGTHING_NOTICE,
218 "Failed to find key for search %s",
220 puts("Key not found");
224 find_keys(search, keyid, ishex, fingerprint, skshash,
225 exact, false, mrhkp);
228 find_keys(search, keyid, ishex, fingerprint, skshash,
232 if (config.dbbackend->fetch_key(keyid, &publickey,
234 unsigned char *photo = NULL;
237 if (getphoto(publickey, indx, &photo,
244 free_publickey(publickey);
249 puts("Unknown operation!");
251 config.dbbackend->cleanupdb();
257 puts("Produced by onak " ONAK_VERSION );
261 if (search != NULL) {
266 return (EXIT_SUCCESS);