]> git.sommitrealweird.co.uk Git - onak.git/blob - add.c
Start pulling non-library material out of core source files
[onak.git] / add.c
1 /*
2  * add.c - CGI to add keys.
3  *
4  * Copyright 2002-2004,2007-2008 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "armor.h"
26 #include "cleankey.h"
27 #include "cleanup.h"
28 #include "charfuncs.h"
29 #include "getcgi.h"
30 #include "keydb.h"
31 #include "keystructs.h"
32 #include "log.h"
33 #include "mem.h"
34 #include "merge.h"
35 #include "onak-conf.h"
36 #include "parsekey.h"
37
38 int main(int argc, char *argv[])
39 {
40         struct openpgp_packet_list  *packets = NULL;
41         struct openpgp_publickey    *keys = NULL;
42         char                       **params = NULL;
43         struct buffer_ctx            ctx;
44         int                          count = 0;
45         int                          i;
46
47         memset(&ctx, 0, sizeof(ctx));
48
49         params = getcgivars(argc, argv);
50         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
51                 if (!strcmp(params[i], "keytext")) {
52                         ctx.buffer = params[i+1];
53                         ctx.size = strlen(ctx.buffer);
54                 } else {
55                         free(params[i+1]);
56                 }
57                 params[i+1] = NULL;
58                 free(params[i]);
59                 params[i] = NULL;
60         }
61         if (params != NULL) {
62                 free(params);
63                 params = NULL;
64         }
65
66         start_html("onak : Add");
67         if (ctx.buffer == NULL) {
68                 puts("Error: No keytext to add supplied.");
69                 end_html();
70         } else {
71                 readconfig(NULL);
72                 initlogthing("add", config.logfile);
73                 dearmor_openpgp_stream(buffer_fetchchar,
74                                         &ctx,
75                                         &packets);
76                 if (packets != NULL) {
77                         count = parse_keys(packets, &keys);
78                         logthing(LOGTHING_NOTICE, "Received %d keys.",
79                                 count);
80                         printf("Key block added to key server database.\n");
81                         printf("  New public keys added: %d\n", count);
82                         end_html();
83                         if (stdout != NULL && fileno(stdout) != -1) {
84                                 fclose(stdout);
85                         }
86                         if (stderr != NULL && stderr != stdout &&
87                                         fileno(stderr) != -1) {
88                                 fclose(stderr);
89                         }
90                         catchsignals();
91                         config.dbbackend->initdb(false);
92                         
93                         count = cleankeys(keys);
94                         logthing(LOGTHING_INFO, "%d keys cleaned.",
95                                         count);
96
97                         count = config.dbbackend->update_keys(&keys, true);
98                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
99                                 count);
100
101                         if (keys != NULL) {
102                                 free_publickey(keys);
103                                 keys = NULL;
104                         }
105                         
106                         config.dbbackend->cleanupdb();
107                 } else {
108                         puts("No OpenPGP packets found in input.");
109                         end_html();
110                 }
111                 cleanuplogthing();
112                 cleanupconfig();
113         }
114         return (EXIT_SUCCESS);
115 }