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