]> git.sommitrealweird.co.uk Git - onak.git/blob - splitkeys.c
fdc20a288dfb1250a5ce218ab647714fc495d98d
[onak.git] / splitkeys.c
1 /*
2  * splitkeys.c - Split a keyring into smaller chunks.
3  *
4  * Copyright 2003 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 <fcntl.h>
21 #include <stdio.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include "charfuncs.h"
27 #include "keystructs.h"
28 #include "log.h"
29 #include "mem.h"
30 #include "onak-conf.h"
31 #include "openpgp.h"
32 #include "parsekey.h"
33
34 int main(int argc, char *argv[])
35 {
36         struct openpgp_packet_list      *packets = NULL;
37         struct openpgp_packet_list      *list_end = NULL;
38         struct openpgp_packet_list      *tmp = NULL;
39         int                              result = 0;
40         int                              maxkeys = 10000;
41         int                              outfd = -1;
42         int                              count = 0;
43         char                             splitfile[1024];
44
45         if (argc > 1) {
46                 maxkeys = atoi(argv[1]);
47                 if (maxkeys == 0) {
48                         fprintf(stderr,
49                                 "Couldn't parse %s as a number of keys!\n",
50                                 argv[1]);
51                         exit(EXIT_FAILURE);
52                 }
53         }
54
55         readconfig(NULL);
56         initlogthing("splitkeys", config.logfile);
57
58         do {
59                 result = read_openpgp_stream(stdin_getchar, NULL,
60                                  &packets, maxkeys);
61                 if (packets != NULL) {
62                         list_end = packets;
63                         while (list_end->next != NULL) {
64                                 tmp = list_end;
65                                 list_end = list_end->next;
66                                 if (list_end->next == NULL &&
67                                         list_end->packet->tag ==
68                                                 OPENPGP_PACKET_PUBLICKEY) {
69                                         tmp->next = NULL;
70                                 }
71                         }
72                         if (tmp->next != NULL) {
73                                 list_end = NULL;
74                         }
75
76                         snprintf(splitfile, 1023, "splitfile-%d.pgp", count);
77                         outfd = open(splitfile, O_WRONLY | O_CREAT, 0664);
78                         write_openpgp_stream(file_putchar, &outfd,
79                                         packets);
80                         close(outfd);
81                         free_packet_list(packets);
82                         packets = list_end;
83                         count++;
84                 }
85         } while (packets != NULL);
86
87         cleanuplogthing();
88         cleanupconfig();
89
90         return 0;
91 }