Only seed database for Debian install if we're using default config
[onak.git] / stripkey.c
1 #include <getopt.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7
8 #include "armor.h"
9 #include "charfuncs.h"
10 #include "cleankey.h"
11 #include "keydb.h"
12 #include "keyid.h"
13 #include "keyindex.h"
14 #include "keystructs.h"
15 #include "log.h"
16 #include "mem.h"
17 #include "merge.h"
18 #include "onak-conf.h"
19 #include "parsekey.h"
20 #include "photoid.h"
21 #include "decodekey.h"
22
23 int main(int argc, char** argv) {
24   struct openpgp_packet_list *packets = NULL;
25   struct openpgp_packet_list *list_end = NULL;
26   struct openpgp_publickey   *keys = NULL;
27   struct openpgp_publickey   *key = NULL;
28   struct openpgp_signedpacket_list *uid = NULL;
29   struct openpgp_packet_list *sig = NULL;
30   struct openpgp_packet_list *prevsig = NULL;
31   int result = 0;
32   uint64_t my_key = 0;
33
34   if( argc > 1 )
35      my_key = strtoull( argv[1], NULL, 16 );
36    
37   /* expect a stream of openpgp packets on stdin comprising some keys */
38   /* strip each key of everything but its pubkey component, uids and
39    * selfsigs and revsigs on those selfsigs */
40
41   result = read_openpgp_stream( stdin_getchar, NULL, &packets, 0 );
42   result = parse_keys( packets, &keys );
43   free_packet_list(packets);
44   packets = NULL;
45   result = cleankeys( keys );
46   /* Iterate over the keys... */
47   for( key = keys; key; key = key->next ) {
48     uint64_t keyid = get_keyid( key );
49     for( uid = key->uids; uid; uid = uid->next ) {
50       REPEATTHISUID: 
51       for( sig = uid->sigs, prevsig = NULL; 
52            sig; 
53            prevsig = sig, sig = sig->next ) {
54         uint64_t thissig = sig_keyid( sig->packet );
55         if( thissig != keyid && thissig != my_key ) {
56           /* Don't care about this packet... */
57           if( prevsig ) {
58             prevsig->next = sig->next;
59           } else {
60             uid->sigs = sig->next;
61           }
62           sig->next = NULL;
63           free_packet_list( sig );
64           goto REPEATTHISUID;
65         }
66       }
67     }
68     flatten_publickey( key, &packets, &list_end );
69   }
70   write_openpgp_stream( stdout_putchar, NULL, packets );
71   return 0;
72 }