cscvs to tla changeset 2
[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 #include "merge.h"
20
21 struct cgi_get_ctx {
22         char *buffer;
23         int offset;
24 };
25
26
27 int cgi_getchar(void *ctx, size_t count, unsigned char *c)
28 {
29         struct cgi_get_ctx *buf = NULL;
30
31         buf = (struct cgi_get_ctx *) ctx;
32
33         while (count-- > 0 && *c != 0) {
34                 *c = buf->buffer[buf->offset++];
35         }
36
37         return (*c == 0);
38 }
39
40 int main(int argc, char *argv[])
41 {
42         struct openpgp_packet_list *packets = NULL;
43         struct openpgp_publickey *keys = NULL;
44         char **params = NULL;
45         struct cgi_get_ctx ctx;
46         int i;
47
48         memset(&ctx, 0, sizeof(ctx));
49
50         params = getcgivars(argc, argv);
51         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
52                 if (!strcmp(params[i], "keytext")) {
53                         ctx.buffer = params[i+1];
54                 }
55         }
56
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                         initdb();
68                         printf("Got %d new keys.\n",
69                                         update_keys(&keys));
70                         cleanupdb();
71                 } else {
72                         puts("No OpenPGP packets found in input.");
73                 }
74         }
75         puts("</body></html>");
76         return (EXIT_SUCCESS);
77 }