]> the.earth.li Git - onak.git/blob - cgi/hashquery.c
84efe97e514ae7b89d254b46e626fd9a416e8ac4
[onak.git] / cgi / hashquery.c
1 /*
2  * hashquery.c - CGI to handle SKS style /pks/hashquery requests
3  *
4  * Copyright 2011 Jonathan McDowell <noodles@earth.li>
5  *
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.
9  *
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
13  * more details.
14  *
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/>.
17  */
18
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdint.h>
24 #include <unistd.h>
25
26 #include "build-config.h"
27 #include "charfuncs.h"
28 #include "cleanup.h"
29 #include "keydb.h"
30 #include "log.h"
31 #include "marshal.h"
32 #include "mem.h"
33 #include "onak-conf.h"
34
35 void doerror(char *error)
36 {
37         printf("Content-Type: text/plain\n\n");
38         printf("%s", error);
39         cleanuplogthing();
40         cleanupconfig();
41         exit(EXIT_FAILURE);
42 }
43
44 int main(__unused int argc, __unused char *argv[])
45 {
46         char *request_method, *env;
47         int count, found, i;
48         uint8_t **hashes;
49         struct buffer_ctx cgipostbuf;
50         struct openpgp_publickey **keys;
51         struct onak_dbctx *dbctx;
52
53         readconfig(NULL);
54         initlogthing("hashquery", config.logfile);
55
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");
59         }
60
61         env = getenv("CONTENT_LENGTH");
62         if ((env == NULL) || !(cgipostbuf.size = atoi(env))) {
63                 doerror("Must provide a content length.\n");
64         }
65
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");
70         }
71
72         if (!fread(cgipostbuf.buffer, cgipostbuf.size, 1, stdin)) {
73                 doerror("Couldn't read query.\n");
74         }
75
76         hashes = (uint8_t **) unmarshal_array(buffer_fetchchar, &cgipostbuf,
77                         (void * (*)(int (*)(void *, size_t,  void *), void *))
78                                 unmarshal_skshash, &count);
79
80         free(cgipostbuf.buffer);
81         cgipostbuf.buffer = NULL;
82         cgipostbuf.size = cgipostbuf.offset = 0;
83
84         if (hashes == NULL) {
85                 doerror("No hashes supplied.\n");
86         }
87
88         found = 0;
89         keys = calloc(sizeof(struct openpgp_publickey *), count);
90         if (keys == NULL) {
91                 doerror("Couldn't allocate memory for reply.\n");
92         }
93
94         catchsignals();
95         dbctx = config.dbinit(config.backend, false);
96
97         if (dbctx->fetch_key_skshash == NULL) {
98                 dbctx->cleanupdb(dbctx);
99                 doerror("Can't fetch by skshash with this backend.");
100         }
101
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) {
106                         found++;
107                 }
108                 free(hashes[i]);
109                 hashes[i] = NULL;
110         }
111         free(hashes);
112         hashes = NULL;
113
114         dbctx->cleanupdb(dbctx);
115
116         puts("Content-Type: pgp/keys\n");
117         marshal_array(stdout_putchar, NULL,
118                         (void (*)(int (*)(void *, size_t,  void *),
119                                         void *, const void *))
120                                 marshal_publickey, (void **) keys, found);
121         printf("\n");
122
123         for (i = 0; i < found; i++) {
124                 free_publickey(keys[i]);
125         }
126         free(keys);
127
128         cleanuplogthing();
129         cleanupconfig();
130 }