]> git.sommitrealweird.co.uk Git - onak.git/blob - add.c
autoconf onak-mail.pl config
[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                         count = parse_keys(packets, &keys);
67                         logthing(LOGTHING_NOTICE, "Received %d keys.",
68                                 count);
69                         printf("Storing %d keys.\n", count);
70                         end_html();
71                         fclose(stdout);
72                         fclose(stderr);
73                         initdb(false);
74                         
75                         count = cleankeys(keys);
76                         logthing(LOGTHING_INFO, "%d keys cleaned.",
77                                         count);
78
79                         count = update_keys(&keys);
80                         printf("Got %d new keys.\n", count);
81                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
82                                 count);
83                         if (keys != NULL) {
84                                 sendkeysync(keys);
85                                 free_publickey(keys);
86                                 keys = NULL;
87                         }
88                         cleanupdb();
89                 } else {
90                         puts("No OpenPGP packets found in input.");
91                         end_html();
92                 }
93                 cleanuplogthing();
94                 cleanupconfig();
95         }
96         return (EXIT_SUCCESS);
97 }