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