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, see <https://www.gnu.org/licenses/>.
27 #include "charfuncs.h"
36 #include "onak-conf.h"
48 void find_keys(struct onak_dbctx *dbctx,
49 char *search, uint64_t keyid,
50 struct openpgp_fingerprint *fingerprint,
51 bool ishex, bool isfp, bool dispfp, 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, fingerprint, &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, dispfp,
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;
114 bool skshash = false;
120 struct openpgp_fingerprint fingerprint;
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 fingerprint.length = MAX_FINGERPRINT_LEN;
150 for (j = 0; j < MAX_FINGERPRINT_LEN; j++) {
151 fingerprint.fp[j] = (hex2bin(
154 hex2bin(search[3 + j * 2]);
157 } else if (search != NULL) {
158 keyid = strtoull(search, &end, 16);
165 } else if (!strcmp(params[i], "idx")) {
166 indx = atoi(params[i+1]);
167 } else if (!strcmp(params[i], "fingerprint")) {
168 if (!strcmp(params[i+1], "on")) {
171 } else if (!strcmp(params[i], "hash")) {
172 if (!strcmp(params[i+1], "on")) {
175 } else if (!strcmp(params[i], "exact")) {
176 if (!strcmp(params[i+1], "on")) {
179 } else if (!strcmp(params[i], "options")) {
181 * TODO: We should be smarter about this; options may
182 * have several entries. For now mr is the only valid
185 if (!strcmp(params[i+1], "mr")) {
191 if (params[i+1] != NULL) {
196 if (params != NULL) {
202 puts("Content-Type: text/plain\n");
203 } else if (op == OP_PHOTO) {
204 puts("Content-Type: image/jpeg\n");
206 start_html("Lookup of key");
209 if (op == OP_UNKNOWN) {
210 puts("Error: No operation supplied.");
211 } else if (search == NULL) {
212 puts("Error: No key to search for supplied.");
215 initlogthing("lookup", config.logfile);
217 dbctx = config.dbinit(config.backend, false);
222 parse_skshash(search, &hash);
223 result = dbctx->fetch_key_skshash(dbctx,
226 result = dbctx->fetch_key_id(dbctx, keyid,
229 result = dbctx->fetch_key_fp(dbctx,
230 &fingerprint, &publickey, false);
232 result = dbctx->fetch_key_text(dbctx,
237 logthing(LOGTHING_NOTICE,
238 "Found %d key(s) for search %s",
242 cleankeys(&publickey, config.clean_policies);
243 flatten_publickey(publickey,
246 armor_openpgp_stream(stdout_putchar,
251 logthing(LOGTHING_NOTICE,
252 "Failed to find key for search %s",
254 puts("Key not found");
258 find_keys(dbctx, search, keyid, &fingerprint,
259 ishex, isfp, dispfp, skshash,
260 exact, false, mrhkp);
263 find_keys(dbctx, search, keyid, &fingerprint,
264 ishex, isfp, dispfp, skshash,
269 dbctx->fetch_key_fp(dbctx, &fingerprint,
272 dbctx->fetch_key_id(dbctx, keyid,
275 if (publickey != NULL) {
276 unsigned char *photo = NULL;
279 if (getphoto(publickey, indx, &photo,
280 &length) == ONAK_E_OK) {
286 free_publickey(publickey);
291 puts("Unknown operation!");
293 dbctx->cleanupdb(dbctx);
299 puts("Produced by onak " ONAK_VERSION );
303 if (search != NULL) {
308 return (EXIT_SUCCESS);