]> git.sommitrealweird.co.uk Git - onak.git/blob - add.c
0.3.5 release.
[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
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                         if (stdout != NULL && fileno(stdout) != -1) {
72                                 fclose(stdout);
73                         }
74                         if (stderr != NULL && stderr != stdout &&
75                                         fileno(stderr) != -1) {
76                                 fclose(stderr);
77                         }
78                         catchsignals();
79                         config.dbbackend->initdb(false);
80                         
81                         count = cleankeys(keys);
82                         logthing(LOGTHING_INFO, "%d keys cleaned.",
83                                         count);
84
85                         count = config.dbbackend->update_keys(&keys, true);
86                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
87                                 count);
88
89                         if (keys != NULL) {
90                                 free_publickey(keys);
91                                 keys = NULL;
92                         }
93                         
94                         config.dbbackend->cleanupdb();
95                 } else {
96                         puts("No OpenPGP packets found in input.");
97                         end_html();
98                 }
99                 cleanuplogthing();
100                 cleanupconfig();
101         }
102         return (EXIT_SUCCESS);
103 }