Add -1 to Debian package version
[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("Key block added to key server database.\n");
70                         printf("  New public keys added: %d\n", count);
71                         end_html();
72                         if (stdout != NULL && fileno(stdout) != -1) {
73                                 fclose(stdout);
74                         }
75                         if (stderr != NULL && stderr != stdout &&
76                                         fileno(stderr) != -1) {
77                                 fclose(stderr);
78                         }
79                         catchsignals();
80                         config.dbbackend->initdb(false);
81                         
82                         count = cleankeys(keys);
83                         logthing(LOGTHING_INFO, "%d keys cleaned.",
84                                         count);
85
86                         count = config.dbbackend->update_keys(&keys, true);
87                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
88                                 count);
89
90                         if (keys != NULL) {
91                                 free_publickey(keys);
92                                 keys = NULL;
93                         }
94                         
95                         config.dbbackend->cleanupdb();
96                 } else {
97                         puts("No OpenPGP packets found in input.");
98                         end_html();
99                 }
100                 cleanuplogthing();
101                 cleanupconfig();
102         }
103         return (EXIT_SUCCESS);
104 }