]> git.sommitrealweird.co.uk Git - onak.git/blob - add.c
323373b36232e82de391a7e8782493e866caf1bf
[onak.git] / add.c
1 /*
2  * add.c - CGI to add keys.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "armor.h"
15 #include "getcgi.h"
16 #include "keydb.h"
17 #include "keystructs.h"
18 #include "parsekey.h"
19
20 struct cgi_get_ctx {
21         char *buffer;
22         int offset;
23 };
24
25
26 int cgi_getchar(void *ctx, unsigned char *c)
27 {
28         struct cgi_get_ctx *buf = NULL;
29
30         buf = (struct cgi_get_ctx *) ctx;
31
32         *c = buf->buffer[buf->offset++];
33
34         return (*c == 0);
35 }
36
37 int main(int argc, char *argv[])
38 {
39         struct openpgp_packet_list *packets = NULL;
40         struct openpgp_publickey *keys = NULL;
41         struct openpgp_publickey *curkey = NULL;
42         char **params = NULL;
43         struct cgi_get_ctx ctx;
44         int i;
45
46         memset(&ctx, 0, sizeof(ctx));
47
48         params = getcgivars(argc, argv);
49         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
50                 if (!strcmp(params[i], "keytext")) {
51                         ctx.buffer = params[i+1];
52                 }
53         }
54
55 //      puts("HTTP/1.0 200 OK");
56 //      puts("Server: onak 0.0.1");
57         puts("Content-Type: text/html\n");
58         puts("<html><title>onak : Add</title><body>");
59         if (ctx.buffer == NULL) {
60                 puts("Error: No keytext to add supplied.");
61         } else {
62                 dearmor_openpgp_stream(cgi_getchar,
63                                         &ctx,
64                                         &packets);
65                 if (packets != NULL) {
66                         parse_keys(packets, &keys);
67                         curkey = keys;
68                         initdb();
69                         while (curkey != NULL) {
70                                 if (store_key(curkey)) {
71 //                                      puts("Key added successfully.");
72                                 } else {
73                                         printf("Problem adding key '%s'.\n", strerror(errno));
74                                 }
75                                 curkey = curkey->next;
76                         }
77                         cleanupdb();
78                         puts("Keys added.");
79                 } else {
80                         puts("No OpenPGP packets found in input.");
81                 }
82         }
83         puts("</body></html>");
84         return (EXIT_SUCCESS);
85 }