cscvs to tla changeset 77
[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.10 2003/06/04 20:57:06 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 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         } else {
59                 readconfig();
60                 initlogthing("add", config.logfile);
61                 dearmor_openpgp_stream(buffer_fetchchar,
62                                         &ctx,
63                                         &packets);
64                 if (packets != NULL) {
65                         parse_keys(packets, &keys);
66                         initdb();
67                         printf("Got %d new keys.\n",
68                                         update_keys(&keys));
69                         if (keys != NULL) {
70                                 sendkeysync(keys);
71                                 free_publickey(keys);
72                                 keys = NULL;
73                         }
74                         cleanupdb();
75                 } else {
76                         puts("No OpenPGP packets found in input.");
77                 }
78                 cleanuplogthing();
79                 cleanupconfig();
80         }
81         end_html();
82         return (EXIT_SUCCESS);
83 }