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, uint8_t *fp, size_t fpsize,
50 bool ishex, bool isfp, bool fingerprint, bool skshash,
51 bool exact, bool verbose, bool mrhkp)
53 struct openpgp_publickey *publickey = NULL;
57 count = config.dbbackend->fetch_key_id(keyid, &publickey,
60 count = config.dbbackend->fetch_key_fp(fp, fpsize, &publickey,
63 count = config.dbbackend->fetch_key_text(search, &publickey);
65 if (publickey != NULL) {
67 printf("info:1:%d\n", count);
68 mrkey_index(publickey);
70 key_index(publickey, verbose, fingerprint, skshash,
73 free_publickey(publickey);
74 } else if (count == 0) {
78 puts("Key not found.");
84 printf("Found %d keys, but maximum number to return"
88 puts("Try again with a more specific search.");
93 static uint8_t hex2bin(char c)
95 if (c >= '0' && c <= '9') {
97 } else if (c >= 'a' && c <= 'f') {
98 return (c - 'a' + 10);
99 } else if (c >= 'A' && c <= 'F') {
100 return (c - 'A' + 10);
106 int main(int argc, char *argv[])
108 char **params = NULL;
112 bool fingerprint = false;
113 bool skshash = false;
119 uint8_t fp[MAX_FINGERPRINT_LEN];
122 struct openpgp_publickey *publickey = NULL;
123 struct openpgp_packet_list *packets = NULL;
124 struct openpgp_packet_list *list_end = NULL;
128 params = getcgivars(argc, argv);
129 for (i = 0; params != NULL && params[i] != NULL; i += 2) {
130 if (!strcmp(params[i], "op")) {
131 if (!strcmp(params[i+1], "get")) {
133 } else if (!strcmp(params[i+1], "hget")) {
135 } else if (!strcmp(params[i+1], "index")) {
137 } else if (!strcmp(params[i+1], "vindex")) {
139 } else if (!strcmp(params[i+1], "photo")) {
142 } else if (!strcmp(params[i], "search")) {
143 search = params[i+1];
145 if (search != NULL && strlen(search) == 42 &&
146 search[0] == '0' && search[1] == 'x') {
147 for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
148 fp[i] = (hex2bin(search[2 + i * 2])
150 hex2bin(search[3 + i * 2]);
153 } else if (search != NULL) {
154 keyid = strtoull(search, &end, 16);
161 } else if (!strcmp(params[i], "idx")) {
162 indx = atoi(params[i+1]);
163 } else if (!strcmp(params[i], "fingerprint")) {
164 if (!strcmp(params[i+1], "on")) {
167 } else if (!strcmp(params[i], "hash")) {
168 if (!strcmp(params[i+1], "on")) {
171 } else if (!strcmp(params[i], "exact")) {
172 if (!strcmp(params[i+1], "on")) {
175 } else if (!strcmp(params[i], "options")) {
177 * TODO: We should be smarter about this; options may
178 * have several entries. For now mr is the only valid
181 if (!strcmp(params[i+1], "mr")) {
187 if (params[i+1] != NULL) {
192 if (params != NULL) {
198 puts("Content-Type: text/plain\n");
199 } else if (op == OP_PHOTO) {
200 puts("Content-Type: image/jpeg\n");
202 start_html("Lookup of key");
205 if (op == OP_UNKNOWN) {
206 puts("Error: No operation supplied.");
207 } else if (search == NULL) {
208 puts("Error: No key to search for supplied.");
211 initlogthing("lookup", config.logfile);
213 config.dbbackend->initdb(false);
218 parse_skshash(search, &hash);
219 result = config.dbbackend->fetch_key_skshash(
222 result = config.dbbackend->fetch_key_id(keyid,
225 result = config.dbbackend->fetch_key_fp(fp,
226 MAX_FINGERPRINT_LEN, &publickey, false);
228 result = config.dbbackend->fetch_key_text(
233 logthing(LOGTHING_NOTICE,
234 "Found %d key(s) for search %s",
238 cleankeys(publickey);
239 flatten_publickey(publickey,
242 armor_openpgp_stream(stdout_putchar,
247 logthing(LOGTHING_NOTICE,
248 "Failed to find key for search %s",
250 puts("Key not found");
254 find_keys(search, keyid, fp, MAX_FINGERPRINT_LEN,
255 ishex, isfp, fingerprint, skshash,
256 exact, false, mrhkp);
259 find_keys(search, keyid, fp, MAX_FINGERPRINT_LEN,
260 ishex, isfp, fingerprint, skshash,
265 config.dbbackend->fetch_key_fp(fp,
269 config.dbbackend->fetch_key_id(keyid,
272 if (publickey != NULL) {
273 unsigned char *photo = NULL;
276 if (getphoto(publickey, indx, &photo,
277 &length) == ONAK_E_OK) {
283 free_publickey(publickey);
288 puts("Unknown operation!");
290 config.dbbackend->cleanupdb();
296 puts("Produced by onak " ONAK_VERSION );
300 if (search != NULL) {
305 return (EXIT_SUCCESS);