]> the.earth.li Git - onak.git/blob - hashquery.c
Cleanup various includes
[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, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <unistd.h>
26
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(int argc, char *argv[])
45 {
46         char *request_method;
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         if (!(cgipostbuf.size = atoi(getenv("CONTENT_LENGTH")))) {
62                 doerror("Must provide a content length.\n");
63         }
64
65         cgipostbuf.offset = 0;
66         cgipostbuf.buffer = malloc(cgipostbuf.size);
67         if (cgipostbuf.buffer == NULL) {
68                 doerror("Couldn't allocate memory for query content.\n");
69         }
70
71         if (!fread(cgipostbuf.buffer, cgipostbuf.size, 1, stdin)) {
72                 doerror("Couldn't read query.\n");
73         }
74
75         hashes = (uint8_t **) unmarshal_array(buffer_fetchchar, &cgipostbuf,
76                         (void * (*)(int (*)(void *, size_t,  void *), void *))
77                                 unmarshal_skshash, &count);
78
79         free(cgipostbuf.buffer);
80         cgipostbuf.buffer = NULL;
81         cgipostbuf.size = cgipostbuf.offset = 0;
82
83         if (hashes == NULL) {
84                 doerror("No hashes supplied.\n");
85         }
86
87         found = 0;
88         keys = calloc(sizeof(struct openpgp_publickey *), count);
89         if (keys == NULL) {
90                 doerror("Couldn't allocate memory for reply.\n");
91         }
92
93         catchsignals();
94         dbctx = config.dbinit(config.backend, false);
95
96         if (dbctx->fetch_key_skshash == NULL) {
97                 dbctx->cleanupdb(dbctx);
98                 doerror("Can't fetch by skshash with this backend.");
99         }
100
101         for (i = 0; i < count; i++) {
102                 dbctx->fetch_key_skshash(dbctx,
103                                 (struct skshash *) hashes[i], &keys[found]);
104                 if (keys[found] != NULL) {
105                         found++;
106                 }
107                 free(hashes[i]);
108                 hashes[i] = NULL;
109         }
110         free(hashes);
111         hashes = NULL;
112
113         dbctx->cleanupdb(dbctx);
114
115         puts("Content-Type: pgp/keys\n");
116         marshal_array(stdout_putchar, NULL,
117                         (void (*)(int (*)(void *, size_t,  void *),
118                                         void *, const void *))
119                                 marshal_publickey, (void **) keys, found);
120         printf("\n");
121
122         for (i = 0; i < found; i++) {
123                 free_publickey(keys[i]);
124         }
125         free(keys);
126
127         cleanuplogthing();
128         cleanupconfig();
129 }