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 && strlen(search) == 42 &&
127 search[0] == '0' && search[1] == 'x') {
129 * Fingerprint. Truncate to last 64 bits for
132 keyid = strtoull(&search[26], &end, 16);
133 if (end != NULL && *end == 0) {
136 } else if (search != NULL) {
137 keyid = strtoull(search, &end, 16);
144 } else if (!strcmp(params[i], "idx")) {
145 indx = atoi(params[i+1]);
146 } else if (!strcmp(params[i], "fingerprint")) {
147 if (!strcmp(params[i+1], "on")) {
150 } else if (!strcmp(params[i], "hash")) {
151 if (!strcmp(params[i+1], "on")) {
154 } else if (!strcmp(params[i], "exact")) {
155 if (!strcmp(params[i+1], "on")) {
158 } else if (!strcmp(params[i], "options")) {
160 * TODO: We should be smarter about this; options may
161 * have several entries. For now mr is the only valid
164 if (!strcmp(params[i+1], "mr")) {
170 if (params[i+1] != NULL) {
175 if (params != NULL) {
181 puts("Content-Type: text/plain\n");
182 } else if (op == OP_PHOTO) {
183 puts("Content-Type: image/jpeg\n");
185 start_html("Lookup of key");
188 if (op == OP_UNKNOWN) {
189 puts("Error: No operation supplied.");
190 } else if (search == NULL) {
191 puts("Error: No key to search for supplied.");
194 initlogthing("lookup", config.logfile);
196 config.dbbackend->initdb(false);
201 parse_skshash(search, &hash);
202 result = config.dbbackend->fetch_key_skshash(
205 result = config.dbbackend->fetch_key(keyid,
208 result = config.dbbackend->fetch_key_text(
213 logthing(LOGTHING_NOTICE,
214 "Found %d key(s) for search %s",
218 cleankeys(publickey);
219 flatten_publickey(publickey,
222 armor_openpgp_stream(stdout_putchar,
227 logthing(LOGTHING_NOTICE,
228 "Failed to find key for search %s",
230 puts("Key not found");
234 find_keys(search, keyid, ishex, fingerprint, skshash,
235 exact, false, mrhkp);
238 find_keys(search, keyid, ishex, fingerprint, skshash,
242 if (config.dbbackend->fetch_key(keyid, &publickey,
244 unsigned char *photo = NULL;
247 if (getphoto(publickey, indx, &photo,
254 free_publickey(publickey);
259 puts("Unknown operation!");
261 config.dbbackend->cleanupdb();
267 puts("Produced by onak " ONAK_VERSION );
271 if (search != NULL) {
276 return (EXIT_SUCCESS);