cscvs to tla changeset 112
[onak.git] / splitkeys.c
1 /*
2  * splitkeys.c - Split a keyring into smaller chunks.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  * 
6  * Copyright 2003 Project Purple
7  *
8  * $Id: splitkeys.c,v 1.4 2003/10/15 21:15:21 noodles Exp $
9  */
10
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17 #include "charfuncs.h"
18 #include "keystructs.h"
19 #include "log.h"
20 #include "mem.h"
21 #include "onak-conf.h"
22 #include "parsekey.h"
23
24 int main(int argc, char *argv[])
25 {
26         struct openpgp_packet_list      *packets = NULL;
27         struct openpgp_packet_list      *list_end = NULL;
28         struct openpgp_packet_list      *tmp = NULL;
29         int                              result = 0;
30         int                              maxkeys = 10000;
31         int                              outfd = -1;
32         int                              count = 0;
33         char                             splitfile[1024];
34
35         if (argc > 1) {
36                 maxkeys = atoi(argv[1]);
37                 if (maxkeys == 0) {
38                         fprintf(stderr,
39                                 "Couldn't parse %s as a number of keys!\n",
40                                 argv[1]);
41                         exit(EXIT_FAILURE);
42                 }
43         }
44
45         readconfig(NULL);
46         initlogthing("splitkeys", config.logfile);
47
48         do {
49                 result = read_openpgp_stream(stdin_getchar, NULL,
50                                  &packets, maxkeys);
51                 if (packets != NULL) {
52                         list_end = packets;
53                         while (list_end->next != NULL) {
54                                 tmp = list_end;
55                                 list_end = list_end->next;
56                                 if (list_end->next == NULL &&
57                                         list_end->packet->tag == 6) {
58                                         tmp->next = NULL;
59                                 }
60                         }
61                         if (tmp->next != NULL) {
62                                 list_end = NULL;
63                         }
64
65                         snprintf(splitfile, 1023, "splitfile-%d.pgp", count);
66                         outfd = open(splitfile, O_WRONLY | O_CREAT, 0664);
67                         write_openpgp_stream(file_putchar, &outfd,
68                                         packets);
69                         close(outfd);
70                         free_packet_list(packets);
71                         packets = list_end;
72                         count++;
73                 }
74         } while (packets != NULL);
75
76         cleanuplogthing();
77         cleanupconfig();
78
79         return 0;
80 }