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