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(struct onak_dbctx *dbctx,
50 char *search, uint64_t keyid, uint8_t *fp, size_t fpsize,
51 bool ishex, bool isfp, bool fingerprint, bool skshash,
52 bool exact, bool verbose, bool mrhkp)
54 struct openpgp_publickey *publickey = NULL;
58 count = dbctx->fetch_key_id(dbctx, keyid, &publickey,
61 count = dbctx->fetch_key_fp(dbctx, fp, fpsize, &publickey,
64 count = dbctx->fetch_key_text(dbctx, search, &publickey);
66 if (publickey != NULL) {
68 printf("info:1:%d\n", count);
69 mrkey_index(publickey);
71 key_index(dbctx, publickey, verbose, fingerprint,
74 free_publickey(publickey);
75 } else if (count == 0) {
79 puts("Key not found.");
85 printf("Found %d keys, but maximum number to return"
89 puts("Try again with a more specific search.");
94 static uint8_t hex2bin(char c)
96 if (c >= '0' && c <= '9') {
98 } else if (c >= 'a' && c <= 'f') {
99 return (c - 'a' + 10);
100 } else if (c >= 'A' && c <= 'F') {
101 return (c - 'A' + 10);
107 int main(int argc, char *argv[])
109 char **params = NULL;
113 bool fingerprint = false;
114 bool skshash = false;
120 uint8_t fp[MAX_FINGERPRINT_LEN];
123 struct openpgp_publickey *publickey = NULL;
124 struct openpgp_packet_list *packets = NULL;
125 struct openpgp_packet_list *list_end = NULL;
128 struct onak_dbctx *dbctx;
130 params = getcgivars(argc, argv);
131 for (i = 0; params != NULL && params[i] != NULL; i += 2) {
132 if (!strcmp(params[i], "op")) {
133 if (!strcmp(params[i+1], "get")) {
135 } else if (!strcmp(params[i+1], "hget")) {
137 } else if (!strcmp(params[i+1], "index")) {
139 } else if (!strcmp(params[i+1], "vindex")) {
141 } else if (!strcmp(params[i+1], "photo")) {
144 } else if (!strcmp(params[i], "search")) {
145 search = params[i+1];
147 if (search != NULL && strlen(search) == 42 &&
148 search[0] == '0' && search[1] == 'x') {
149 for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
150 fp[i] = (hex2bin(search[2 + i * 2])
152 hex2bin(search[3 + i * 2]);
155 } else if (search != NULL) {
156 keyid = strtoull(search, &end, 16);
163 } else if (!strcmp(params[i], "idx")) {
164 indx = atoi(params[i+1]);
165 } else if (!strcmp(params[i], "fingerprint")) {
166 if (!strcmp(params[i+1], "on")) {
169 } else if (!strcmp(params[i], "hash")) {
170 if (!strcmp(params[i+1], "on")) {
173 } else if (!strcmp(params[i], "exact")) {
174 if (!strcmp(params[i+1], "on")) {
177 } else if (!strcmp(params[i], "options")) {
179 * TODO: We should be smarter about this; options may
180 * have several entries. For now mr is the only valid
183 if (!strcmp(params[i+1], "mr")) {
189 if (params[i+1] != NULL) {
194 if (params != NULL) {
200 puts("Content-Type: text/plain\n");
201 } else if (op == OP_PHOTO) {
202 puts("Content-Type: image/jpeg\n");
204 start_html("Lookup of key");
207 if (op == OP_UNKNOWN) {
208 puts("Error: No operation supplied.");
209 } else if (search == NULL) {
210 puts("Error: No key to search for supplied.");
213 initlogthing("lookup", config.logfile);
215 dbctx = config.dbinit(false);
220 parse_skshash(search, &hash);
221 result = dbctx->fetch_key_skshash(dbctx,
224 result = dbctx->fetch_key_id(dbctx, keyid,
227 result = dbctx->fetch_key_fp(dbctx, fp,
228 MAX_FINGERPRINT_LEN, &publickey, false);
230 result = dbctx->fetch_key_text(dbctx,
235 logthing(LOGTHING_NOTICE,
236 "Found %d key(s) for search %s",
240 cleankeys(publickey);
241 flatten_publickey(publickey,
244 armor_openpgp_stream(stdout_putchar,
249 logthing(LOGTHING_NOTICE,
250 "Failed to find key for search %s",
252 puts("Key not found");
256 find_keys(dbctx, search, keyid, fp, MAX_FINGERPRINT_LEN,
257 ishex, isfp, fingerprint, skshash,
258 exact, false, mrhkp);
261 find_keys(dbctx, search, keyid, fp, MAX_FINGERPRINT_LEN,
262 ishex, isfp, fingerprint, skshash,
267 dbctx->fetch_key_fp(dbctx, fp,
271 dbctx->fetch_key_id(dbctx, keyid,
274 if (publickey != NULL) {
275 unsigned char *photo = NULL;
278 if (getphoto(publickey, indx, &photo,
279 &length) == ONAK_E_OK) {
285 free_publickey(publickey);
290 puts("Unknown operation!");
292 dbctx->cleanupdb(dbctx);
298 puts("Produced by onak " ONAK_VERSION );
302 if (search != NULL) {
307 return (EXIT_SUCCESS);