]> git.sommitrealweird.co.uk Git - onak.git/blob - splitkeys.c
Update version to mark arch build.
[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
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14
15 #include "charfuncs.h"
16 #include "keystructs.h"
17 #include "log.h"
18 #include "mem.h"
19 #include "onak-conf.h"
20 #include "parsekey.h"
21
22 int main(int argc, char *argv[])
23 {
24         struct openpgp_packet_list      *packets = NULL;
25         struct openpgp_packet_list      *list_end = NULL;
26         struct openpgp_packet_list      *tmp = NULL;
27         int                              result = 0;
28         int                              maxkeys = 10000;
29         int                              outfd = -1;
30         int                              count = 0;
31         char                             splitfile[1024];
32
33         if (argc > 1) {
34                 maxkeys = atoi(argv[1]);
35                 if (maxkeys == 0) {
36                         fprintf(stderr,
37                                 "Couldn't parse %s as a number of keys!\n",
38                                 argv[1]);
39                         exit(EXIT_FAILURE);
40                 }
41         }
42
43         readconfig(NULL);
44         initlogthing("splitkeys", config.logfile);
45
46         do {
47                 result = read_openpgp_stream(stdin_getchar, NULL,
48                                  &packets, maxkeys);
49                 if (packets != NULL) {
50                         list_end = packets;
51                         while (list_end->next != NULL) {
52                                 tmp = list_end;
53                                 list_end = list_end->next;
54                                 if (list_end->next == NULL &&
55                                         list_end->packet->tag == 6) {
56                                         tmp->next = NULL;
57                                 }
58                         }
59                         if (tmp->next != NULL) {
60                                 list_end = NULL;
61                         }
62
63                         snprintf(splitfile, 1023, "splitfile-%d.pgp", count);
64                         outfd = open(splitfile, O_WRONLY | O_CREAT, 0664);
65                         write_openpgp_stream(file_putchar, &outfd,
66                                         packets);
67                         close(outfd);
68                         free_packet_list(packets);
69                         packets = list_end;
70                         count++;
71                 }
72         } while (packets != NULL);
73
74         cleanuplogthing();
75         cleanupconfig();
76
77         return 0;
78 }