2 * hashquery.c - CGI to handle SKS style /pks/hashquery requests
4 * Copyright 2011 Jonathan McDowell <noodles@earth.li>
13 #include "charfuncs.h"
19 #include "onak-conf.h"
21 void doerror(char *error)
23 printf("Content-Type: text/plain\n\n");
30 int main(int argc, char *argv[])
35 struct buffer_ctx cgipostbuf;
36 struct openpgp_publickey **keys;
39 initlogthing("hashquery", config.logfile);
41 request_method = getenv("REQUEST_METHOD");
42 if (request_method == NULL || strcmp(request_method, "POST") != 0) {
43 doerror("hashquery must be a HTTP POST request.\n");
46 if (!(cgipostbuf.size = atoi(getenv("CONTENT_LENGTH")))) {
47 doerror("Must provide a content length.\n");
50 cgipostbuf.offset = 0;
51 cgipostbuf.buffer = malloc(cgipostbuf.size);
52 if (cgipostbuf.buffer == NULL) {
53 doerror("Couldn't allocate memory for query content.\n");
56 if (!fread(cgipostbuf.buffer, cgipostbuf.size, 1, stdin)) {
57 doerror("Couldn't read query.\n");
60 hashes = (uint8_t **) unmarshal_array(buffer_fetchchar, &cgipostbuf,
61 (void * (*)(int (*)(void *, size_t, void *), void *))
62 unmarshal_skshash, &count);
64 free(cgipostbuf.buffer);
65 cgipostbuf.buffer = NULL;
66 cgipostbuf.size = cgipostbuf.offset = 0;
69 doerror("No hashes supplied.\n");
73 keys = calloc(sizeof(struct openpgp_publickey *), count);
75 doerror("Couldn't allocate memory for reply.\n");
78 if (config.dbbackend->fetch_key_skshash == NULL) {
79 doerror("Can't fetch by skshash with this backend.");
83 config.dbbackend->initdb(false);
85 for (i = 0; i < count; i++) {
86 config.dbbackend->fetch_key_skshash(
87 (struct skshash *) hashes[i], &keys[found]);
88 if (keys[found] != NULL) {
97 config.dbbackend->cleanupdb();
99 puts("Content-Type: pgp/keys\n");
100 marshal_array(stdout_putchar, NULL,
101 (void (*)(int (*)(void *, size_t, void *),
102 void *, const void *))
103 marshal_publickey, (void **) keys, found);
106 for (i = 0; i < found; i++) {
107 free_publickey(keys[i]);