]> the.earth.li Git - onak.git/blob - hashquery.c
Add ability to drop overly large packets
[onak.git] / 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 "charfuncs.h"
27 #include "cleanup.h"
28 #include "keydb.h"
29 #include "log.h"
30 #include "marshal.h"
31 #include "mem.h"
32 #include "onak-conf.h"
33
34 void doerror(char *error)
35 {
36         printf("Content-Type: text/plain\n\n");
37         printf("%s", error);
38         cleanuplogthing();
39         cleanupconfig();
40         exit(EXIT_FAILURE);
41 }
42
43 int main(int argc, char *argv[])
44 {
45         char *request_method;
46         int count, found, i;
47         uint8_t **hashes;
48         struct buffer_ctx cgipostbuf;
49         struct openpgp_publickey **keys;
50         struct onak_dbctx *dbctx;
51
52         readconfig(NULL);
53         initlogthing("hashquery", config.logfile);
54
55         request_method = getenv("REQUEST_METHOD");
56         if (request_method == NULL || strcmp(request_method, "POST") != 0) {
57                 doerror("hashquery must be a HTTP POST request.\n");
58         }
59
60         if (!(cgipostbuf.size = atoi(getenv("CONTENT_LENGTH")))) {
61                 doerror("Must provide a content length.\n");
62         }
63
64         cgipostbuf.offset = 0;
65         cgipostbuf.buffer = malloc(cgipostbuf.size);
66         if (cgipostbuf.buffer == NULL) {
67                 doerror("Couldn't allocate memory for query content.\n");
68         }
69
70         if (!fread(cgipostbuf.buffer, cgipostbuf.size, 1, stdin)) {
71                 doerror("Couldn't read query.\n");
72         }
73
74         hashes = (uint8_t **) unmarshal_array(buffer_fetchchar, &cgipostbuf,
75                         (void * (*)(int (*)(void *, size_t,  void *), void *))
76                                 unmarshal_skshash, &count);
77
78         free(cgipostbuf.buffer);
79         cgipostbuf.buffer = NULL;
80         cgipostbuf.size = cgipostbuf.offset = 0;
81
82         if (hashes == NULL) {
83                 doerror("No hashes supplied.\n");
84         }
85
86         found = 0;
87         keys = calloc(sizeof(struct openpgp_publickey *), count);
88         if (keys == NULL) {
89                 doerror("Couldn't allocate memory for reply.\n");
90         }
91
92         catchsignals();
93         dbctx = config.dbinit(config.backend, false);
94
95         if (dbctx->fetch_key_skshash == NULL) {
96                 dbctx->cleanupdb(dbctx);
97                 doerror("Can't fetch by skshash with this backend.");
98         }
99
100         for (i = 0; i < count; i++) {
101                 dbctx->fetch_key_skshash(dbctx,
102                                 (struct skshash *) hashes[i], &keys[found]);
103                 if (keys[found] != NULL) {
104                         found++;
105                 }
106                 free(hashes[i]);
107                 hashes[i] = NULL;
108         }
109         free(hashes);
110         hashes = NULL;
111
112         dbctx->cleanupdb(dbctx);
113
114         puts("Content-Type: pgp/keys\n");
115         marshal_array(stdout_putchar, NULL,
116                         (void (*)(int (*)(void *, size_t,  void *),
117                                         void *, const void *))
118                                 marshal_publickey, (void **) keys, found);
119         printf("\n");
120
121         for (i = 0; i < found; i++) {
122                 free_publickey(keys[i]);
123         }
124         free(keys);
125
126         cleanuplogthing();
127         cleanupconfig();
128 }