]> git.sommitrealweird.co.uk Git - onak.git/blob - add.c
f15a145bd3c9e0ffb5138eaaaeb956490c4af6c1
[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  * $Id: add.c,v 1.15 2004/05/31 14:16:49 noodles Exp $
9  */
10
11 #include <errno.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "armor.h"
17 #include "cleankey.h"
18 #include "charfuncs.h"
19 #include "getcgi.h"
20 #include "keydb.h"
21 #include "keystructs.h"
22 #include "log.h"
23 #include "mem.h"
24 #include "merge.h"
25 #include "onak-conf.h"
26 #include "parsekey.h"
27 #include "sendsync.h"
28
29 int main(int argc, char *argv[])
30 {
31         struct openpgp_packet_list  *packets = NULL;
32         struct openpgp_publickey    *keys = NULL;
33         char                       **params = NULL;
34         struct buffer_ctx            ctx;
35         int                          count = 0;
36         int                          i;
37
38         memset(&ctx, 0, sizeof(ctx));
39
40         params = getcgivars(argc, argv);
41         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
42                 if (!strcmp(params[i], "keytext")) {
43                         ctx.buffer = params[i+1];
44                         ctx.size = strlen(ctx.buffer);
45                 } else {
46                         free(params[i+1]);
47                 }
48                 params[i+1] = NULL;
49                 free(params[i]);
50                 params[i] = NULL;
51         }
52         if (params != NULL) {
53                 free(params);
54                 params = NULL;
55         }
56
57         start_html("onak : Add");
58         if (ctx.buffer == NULL) {
59                 puts("Error: No keytext to add supplied.");
60                 end_html();
61         } else {
62                 readconfig(NULL);
63                 initlogthing("add", config.logfile);
64                 dearmor_openpgp_stream(buffer_fetchchar,
65                                         &ctx,
66                                         &packets);
67                 if (packets != NULL) {
68                         printf("Storing %d keys.\n",
69                                 parse_keys(packets, &keys));
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 }