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, see <https://www.gnu.org/licenses/>.
26 #include "build-config.h"
27 #include "charfuncs.h"
33 #include "onak-conf.h"
35 void doerror(char *error)
37 printf("Content-Type: text/plain\n\n");
44 int main(__unused int argc, __unused char *argv[])
46 char *request_method, *env;
49 struct buffer_ctx cgipostbuf;
50 struct openpgp_publickey **keys;
51 struct onak_dbctx *dbctx;
54 initlogthing("hashquery", config.logfile);
56 request_method = getenv("REQUEST_METHOD");
57 if (request_method == NULL || strcmp(request_method, "POST") != 0) {
58 doerror("hashquery must be a HTTP POST request.\n");
61 env = getenv("CONTENT_LENGTH");
62 if ((env == NULL) || !(cgipostbuf.size = atoi(env))) {
63 doerror("Must provide a content length.\n");
66 cgipostbuf.offset = 0;
67 cgipostbuf.buffer = malloc(cgipostbuf.size);
68 if (cgipostbuf.buffer == NULL) {
69 doerror("Couldn't allocate memory for query content.\n");
72 if (!fread(cgipostbuf.buffer, cgipostbuf.size, 1, stdin)) {
73 doerror("Couldn't read query.\n");
76 hashes = (uint8_t **) unmarshal_array(buffer_fetchchar, &cgipostbuf,
77 (void * (*)(size_t (*)(void *, size_t, void *), void *))
78 unmarshal_skshash, &count);
80 free(cgipostbuf.buffer);
81 cgipostbuf.buffer = NULL;
82 cgipostbuf.size = cgipostbuf.offset = 0;
85 doerror("No hashes supplied.\n");
89 keys = calloc(sizeof(struct openpgp_publickey *), count);
91 doerror("Couldn't allocate memory for reply.\n");
95 dbctx = config.dbinit(config.backend, false);
97 if (dbctx->fetch_key_skshash == NULL) {
98 dbctx->cleanupdb(dbctx);
99 doerror("Can't fetch by skshash with this backend.");
102 for (i = 0; i < count; i++) {
103 dbctx->fetch_key_skshash(dbctx,
104 (struct skshash *) hashes[i], &keys[found]);
105 if (keys[found] != NULL) {
114 dbctx->cleanupdb(dbctx);
116 puts("Content-Type: pgp/keys\n");
117 marshal_array(stdout_putchar, NULL,
118 (void (*)(size_t (*)(void *, size_t, void *),
119 void *, const void *))
120 marshal_publickey, (void **) keys, found);
123 for (i = 0; i < found; i++) {
124 free_publickey(keys[i]);