cscvs to tla changeset 4
[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                 } else {
55                         free(params[i+1]);
56                 }
57                 params[i+1] = NULL;
58                 free(params[i]);
59                 params[i] = NULL;
60         }
61         if (params != NULL) {
62                 free(params);
63                 params = NULL;
64         }
65
66         start_html("onak : Add");
67         if (ctx.buffer == NULL) {
68                 puts("Error: No keytext to add supplied.");
69         } else {
70                 dearmor_openpgp_stream(cgi_getchar,
71                                         &ctx,
72                                         &packets);
73                 if (packets != NULL) {
74                         parse_keys(packets, &keys);
75                         initdb();
76                         printf("Got %d new keys.\n",
77                                         update_keys(&keys, false));
78                         cleanupdb();
79                 } else {
80                         puts("No OpenPGP packets found in input.");
81                 }
82         }
83         end_html();
84         return (EXIT_SUCCESS);
85 }