]> the.earth.li Git - onak.git/blob - add.c
3c8c38b44c92711aed527beb84cf37cb8576474b
[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 <stdbool.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 "log.h"
32 #include "mem.h"
33 #include "onak-conf.h"
34 #include "parsekey.h"
35
36 int main(int argc, char *argv[])
37 {
38         struct openpgp_packet_list  *packets = NULL;
39         struct openpgp_publickey    *keys = NULL;
40         char                       **params = NULL;
41         struct buffer_ctx            ctx;
42         int                          count = 0;
43         int                          i;
44         struct onak_dbctx           *dbctx;
45
46         memset(&ctx, 0, sizeof(ctx));
47
48         params = getcgivars(argc, argv);
49         for (i = 0; params != NULL && params[i] != NULL; i += 2) {
50                 if (!strcmp(params[i], "keytext")) {
51                         ctx.buffer = params[i+1];
52                         ctx.size = strlen(ctx.buffer);
53                 } else {
54                         free(params[i+1]);
55                 }
56                 params[i+1] = NULL;
57                 free(params[i]);
58                 params[i] = NULL;
59         }
60         if (params != NULL) {
61                 free(params);
62                 params = NULL;
63         }
64
65         start_html("onak : Add");
66         if (ctx.buffer == NULL) {
67                 puts("Error: No keytext to add supplied.");
68                 end_html();
69         } else {
70                 readconfig(NULL);
71                 initlogthing("add", config.logfile);
72                 dearmor_openpgp_stream(buffer_fetchchar,
73                                         &ctx,
74                                         &packets);
75                 if (packets != NULL) {
76                         count = parse_keys(packets, &keys);
77                         logthing(LOGTHING_NOTICE, "Received %d keys.",
78                                 count);
79                         printf("Key block added to key server database.\n");
80                         printf("  New public keys added: %d\n", count);
81                         end_html();
82                         if (stdout != NULL && fileno(stdout) != -1) {
83                                 fclose(stdout);
84                         }
85                         if (stderr != NULL && stderr != stdout &&
86                                         fileno(stderr) != -1) {
87                                 fclose(stderr);
88                         }
89                         catchsignals();
90                         dbctx = config.dbinit(config.backend, false);
91                         
92                         count = cleankeys(keys);
93                         logthing(LOGTHING_INFO, "%d keys cleaned.",
94                                         count);
95
96                         count = dbctx->update_keys(dbctx, &keys, true);
97                         logthing(LOGTHING_NOTICE, "Got %d new keys.",
98                                 count);
99
100                         if (keys != NULL) {
101                                 free_publickey(keys);
102                                 keys = NULL;
103                         }
104                         
105                         dbctx->cleanupdb(dbctx);
106                 } else {
107                         puts("No OpenPGP packets found in input.");
108                         end_html();
109                 }
110                 cleanuplogthing();
111                 cleanupconfig();
112         }
113         return (EXIT_SUCCESS);
114 }