]> git.sommitrealweird.co.uk Git - onak.git/blob - add.c
a784fac358fb4d9565c84dfb274734b2c61d89df
[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 "cleankey.h"
16 #include "charfuncs.h"
17 #include "getcgi.h"
18 #include "keydb.h"
19 #include "keystructs.h"
20 #include "log.h"
21 #include "mem.h"
22 #include "merge.h"
23 #include "onak-conf.h"
24 #include "parsekey.h"
25 #include "sendsync.h"
26
27 int main(int argc, char *argv[])
28 {
29         struct openpgp_packet_list  *packets = NULL;
30         struct openpgp_publickey    *keys = NULL;
31         char                       **params = NULL;
32         struct buffer_ctx            ctx;
33         int                          count = 0;
34         int                          i;
35
36         memset(&ctx, 0, sizeof(ctx));
37
38         params = getcgivars(argc, argv);
39         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
40                 if (!strcmp(params[i], "keytext")) {
41                         ctx.buffer = params[i+1];
42                         ctx.size = strlen(ctx.buffer);
43                 } else {
44                         free(params[i+1]);
45                 }
46                 params[i+1] = NULL;
47                 free(params[i]);
48                 params[i] = NULL;
49         }
50         if (params != NULL) {
51                 free(params);
52                 params = NULL;
53         }
54
55         start_html("onak : Add");
56         if (ctx.buffer == NULL) {
57                 puts("Error: No keytext to add supplied.");
58                 end_html();
59         } else {
60                 readconfig(NULL);
61                 initlogthing("add", config.logfile);
62                 dearmor_openpgp_stream(buffer_fetchchar,
63                                         &ctx,
64                                         &packets);
65                 if (packets != NULL) {
66                         printf("Storing %d keys.\n",
67                                 parse_keys(packets, &keys));
68                         end_html();
69                         fclose(stdout);
70                         fclose(stderr);
71                         initdb(false);
72                         
73                         count = cleankeys(keys);
74                         logthing(LOGTHING_INFO, "%d keys cleaned.",
75                                         count);
76
77                         count = update_keys(&keys);
78                         printf("Got %d new keys.\n", count);
79                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
80                                 count);
81                         if (keys != NULL) {
82                                 sendkeysync(keys);
83                                 free_publickey(keys);
84                                 keys = NULL;
85                         }
86                         cleanupdb();
87                 } else {
88                         puts("No OpenPGP packets found in input.");
89                         end_html();
90                 }
91                 cleanuplogthing();
92                 cleanupconfig();
93         }
94         return (EXIT_SUCCESS);
95 }