cscvs to tla changeset 39
[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 "charfuncs.h"
16 #include "getcgi.h"
17 #include "keydb.h"
18 #include "keystructs.h"
19 #include "mem.h"
20 #include "merge.h"
21 #include "onak-conf.h"
22 #include "parsekey.h"
23 #include "sendsync.h"
24
25 int main(int argc, char *argv[])
26 {
27         struct openpgp_packet_list *packets = NULL;
28         struct openpgp_publickey *keys = NULL;
29         char **params = NULL;
30         struct buffer_ctx ctx;
31         int i;
32
33         memset(&ctx, 0, sizeof(ctx));
34
35         params = getcgivars(argc, argv);
36         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
37                 if (!strcmp(params[i], "keytext")) {
38                         ctx.buffer = params[i+1];
39                         ctx.size = strlen(ctx.buffer);
40                 } else {
41                         free(params[i+1]);
42                 }
43                 params[i+1] = NULL;
44                 free(params[i]);
45                 params[i] = NULL;
46         }
47         if (params != NULL) {
48                 free(params);
49                 params = NULL;
50         }
51
52         start_html("onak : Add");
53         if (ctx.buffer == NULL) {
54                 puts("Error: No keytext to add supplied.");
55         } else {
56                 dearmor_openpgp_stream(buffer_fetchchar,
57                                         &ctx,
58                                         &packets);
59                 if (packets != NULL) {
60                         parse_keys(packets, &keys);
61                         readconfig();
62                         initdb();
63                         printf("Got %d new keys.\n",
64                                         update_keys(&keys, false));
65                         if (keys != NULL) {
66                                 sendkeysync(keys);
67                                 free_publickey(keys);
68                                 keys = NULL;
69                         }
70                         cleanupdb();
71                         cleanupconfig();
72                 } else {
73                         puts("No OpenPGP packets found in input.");
74                 }
75         }
76         end_html();
77         return (EXIT_SUCCESS);
78 }