]> git.sommitrealweird.co.uk Git - onak.git/blob - add.c
2aba92216a39893c362aad6f16ed5264437cd730
[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.13 2004/05/26 18:53:14 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 "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         } else {
60                 readconfig(NULL);
61                 initlogthing("add", config.logfile);
62                 dearmor_openpgp_stream(buffer_fetchchar,
63                                         &ctx,
64                                         &packets);
65                 if (packets != NULL) {
66                         parse_keys(packets, &keys);
67                         initdb(false);
68                         count = update_keys(&keys);
69                         printf("Got %d new keys.\n", count);
70                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
71                                 count);
72                         if (keys != NULL) {
73                                 sendkeysync(keys);
74                                 free_publickey(keys);
75                                 keys = NULL;
76                         }
77                         cleanupdb();
78                 } else {
79                         puts("No OpenPGP packets found in input.");
80                 }
81                 cleanuplogthing();
82                 cleanupconfig();
83         }
84         end_html();
85         return (EXIT_SUCCESS);
86 }