2 * hashquery.c - CGI to handle SKS style /pks/hashquery requests
4 * Copyright 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.
26 #include "charfuncs.h"
32 #include "onak-conf.h"
34 void doerror(char *error)
36 printf("Content-Type: text/plain\n\n");
43 int main(int argc, char *argv[])
48 struct buffer_ctx cgipostbuf;
49 struct openpgp_publickey **keys;
52 initlogthing("hashquery", config.logfile);
54 request_method = getenv("REQUEST_METHOD");
55 if (request_method == NULL || strcmp(request_method, "POST") != 0) {
56 doerror("hashquery must be a HTTP POST request.\n");
59 if (!(cgipostbuf.size = atoi(getenv("CONTENT_LENGTH")))) {
60 doerror("Must provide a content length.\n");
63 cgipostbuf.offset = 0;
64 cgipostbuf.buffer = malloc(cgipostbuf.size);
65 if (cgipostbuf.buffer == NULL) {
66 doerror("Couldn't allocate memory for query content.\n");
69 if (!fread(cgipostbuf.buffer, cgipostbuf.size, 1, stdin)) {
70 doerror("Couldn't read query.\n");
73 hashes = (uint8_t **) unmarshal_array(buffer_fetchchar, &cgipostbuf,
74 (void * (*)(int (*)(void *, size_t, void *), void *))
75 unmarshal_skshash, &count);
77 free(cgipostbuf.buffer);
78 cgipostbuf.buffer = NULL;
79 cgipostbuf.size = cgipostbuf.offset = 0;
82 doerror("No hashes supplied.\n");
86 keys = calloc(sizeof(struct openpgp_publickey *), count);
88 doerror("Couldn't allocate memory for reply.\n");
91 if (config.dbbackend->fetch_key_skshash == NULL) {
92 doerror("Can't fetch by skshash with this backend.");
96 config.dbbackend->initdb(false);
98 for (i = 0; i < count; i++) {
99 config.dbbackend->fetch_key_skshash(
100 (struct skshash *) hashes[i], &keys[found]);
101 if (keys[found] != NULL) {
110 config.dbbackend->cleanupdb();
112 puts("Content-Type: pgp/keys\n");
113 marshal_array(stdout_putchar, NULL,
114 (void (*)(int (*)(void *, size_t, void *),
115 void *, const void *))
116 marshal_publickey, (void **) keys, found);
119 for (i = 0; i < found; i++) {
120 free_publickey(keys[i]);