Define OpenPGP constants and use them rather than magic numbers
[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 "openpgp.h"
21 #include "parsekey.h"
22
23 int main(int argc, char *argv[])
24 {
25         struct openpgp_packet_list      *packets = NULL;
26         struct openpgp_packet_list      *list_end = NULL;
27         struct openpgp_packet_list      *tmp = NULL;
28         int                              result = 0;
29         int                              maxkeys = 10000;
30         int                              outfd = -1;
31         int                              count = 0;
32         char                             splitfile[1024];
33
34         if (argc > 1) {
35                 maxkeys = atoi(argv[1]);
36                 if (maxkeys == 0) {
37                         fprintf(stderr,
38                                 "Couldn't parse %s as a number of keys!\n",
39                                 argv[1]);
40                         exit(EXIT_FAILURE);
41                 }
42         }
43
44         readconfig(NULL);
45         initlogthing("splitkeys", config.logfile);
46
47         do {
48                 result = read_openpgp_stream(stdin_getchar, NULL,
49                                  &packets, maxkeys);
50                 if (packets != NULL) {
51                         list_end = packets;
52                         while (list_end->next != NULL) {
53                                 tmp = list_end;
54                                 list_end = list_end->next;
55                                 if (list_end->next == NULL &&
56                                         list_end->packet->tag ==
57                                                 OPENPGP_PACKET_PUBLICKEY) {
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 }