]> git.sommitrealweird.co.uk Git - onak.git/blob - wotsap.c
Switch debian build system to dh
[onak.git] / wotsap.c
1 /*
2  * wotsap.c - Output a set of wotsap files from an onak keyring
3  *
4  * See:
5  *
6  * http://www.lysator.liu.se/~jc/wotsap/wotfileformat.txt
7  *
8  * for more details of the format.
9  *
10  * Copyright 2013 Jonathan McDowell <noodles@earth.li>
11  *
12  * This program is free software: you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the Free
14  * Software Foundation; version 2 of the License.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19  * more details.
20  *
21  * You should have received a copy of the GNU General Public License along with
22  * this program; if not, write to the Free Software Foundation, Inc., 51
23  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  */
25
26 #include <getopt.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <arpa/inet.h>
31
32 #include "hash.h"
33 #include "log.h"
34 #include "onak-conf.h"
35 #include "stats.h"
36 #include "version.h"
37
38 static struct ll *sortkeyll(struct ll *keys)
39 {
40         struct ll *newll, *tmp, **curobj;
41         struct stats_key *curkey, *toadd;
42
43         newll = NULL;
44         while (keys) {
45                 toadd = (struct stats_key *) keys->object;
46                 curobj = &newll;
47                 while (*curobj) {
48                         curkey = (struct stats_key *) (*curobj)->object;
49                         if (curkey->keyid >= toadd->keyid) {
50                                 break;
51                         }
52                         curobj = &((*curobj)->next);
53                 }
54
55                 tmp = keys->next;
56                 if (*curobj == NULL || curkey->keyid != toadd->keyid) {
57                         keys->next = *curobj;
58                         *curobj = keys;
59                 }
60                 keys = tmp;
61         }
62         return newll;
63 }
64
65 static void output_key(FILE *names, FILE *keys, uint64_t keyid)
66 {
67         fprintf(names, "%s\n", config.dbbackend->keyid2uid(keyid));
68         fprintf(keys, "%c%c%c%c", (int) (keyid >> 24) & 0xFF,
69                         (int) (keyid >> 16) & 0xFF,
70                         (int) (keyid >>  8) & 0xFF,
71                         (int) (keyid      ) & 0xFF);
72 }
73
74 static void wotsap(uint64_t keyid, char *dir)
75 {
76         struct ll *pending, *sigll, *sigsave;
77         uint32_t curidx = 0;
78         struct stats_key *curkey, *addkey;
79         char *uid;
80         FILE *names, *keys, *sigs, *file;
81         char *tmppath;
82         uint32_t sigcount, sigentry;
83
84         /* Length of dir + "/" + "signatures" + NUL */
85         tmppath = malloc(strlen(dir) + 12);
86
87         sprintf(tmppath, "%s/WOTVERSION", dir);
88         file = fopen(tmppath, "w");
89         if (file == NULL) {
90                 fprintf(stderr, "Couldn't open %s\n", tmppath);
91                 return;
92         }
93         fprintf(file, "0.2\n");
94         fclose(file);
95
96         sprintf(tmppath, "%s/README", dir);
97         file = fopen(tmppath, "w");
98         if (file == NULL) {
99                 fprintf(stderr, "Couldn't open %s\n", tmppath);
100                 return;
101         }
102         fprintf(file, "This is a Web of Trust archive.\n");
103         fprintf(file, "The file format is documented at:\n");
104         fprintf(file, "  http://www.lysator.liu.se/~jc/wotsap/wotfileformat.txt\n\n");
105         fprintf(file, "This file was generated by onak " ONAK_VERSION " \n");
106         fclose(file);
107
108         sprintf(tmppath, "%s/names", dir);
109         names = fopen(tmppath, "w");
110         if (names == NULL) {
111                 fprintf(stderr, "Couldn't open %s\n", tmppath);
112                 return;
113         }
114         sprintf(tmppath, "%s/keys", dir);
115         keys = fopen(tmppath, "wb");
116         if (keys == NULL) {
117                 fprintf(stderr, "Couldn't open %s\n", tmppath);
118                 return;
119         }
120         sprintf(tmppath, "%s/signatures", dir);
121         sigs = fopen(tmppath, "wb");
122         if (sigs == NULL) {
123                 fprintf(stderr, "Couldn't open %s\n", tmppath);
124                 return;
125         }
126         free(tmppath);
127
128         config.dbbackend->cached_getkeysigs(keyid);
129         curkey = findinhash(keyid);
130         curkey->colour = ++curidx;
131         pending = lladd(NULL, curkey);
132
133         output_key(names, keys, curkey->keyid);
134
135         while (pending != NULL) {
136                 curkey = (struct stats_key *) pending->object;
137                 sigll = config.dbbackend->cached_getkeysigs(curkey->keyid);
138                 sigsave = sigll = sortkeyll(sigll);
139                 sigcount = 0;
140                 while (sigll != NULL) {
141                         addkey = (struct stats_key *) sigll->object;
142                         if (addkey->colour == 0) {
143                                 uid = config.dbbackend->keyid2uid(addkey->keyid);
144                                 if (uid != NULL) {
145                                         addkey->colour = ++curidx;
146                                         pending = lladdend(pending, addkey);
147                                         output_key(names, keys, addkey->keyid);
148                                 }
149                         }
150                         if (addkey->colour != 0) {
151                                 sigcount++;
152                         }
153                         sigll = sigll->next;
154                 }
155                 /* Now output the signatures */
156                 sigcount = htonl(sigcount);
157                 fwrite(&sigcount, sizeof (sigcount), 1, sigs);
158                 sigll = sigsave;
159                 while (sigll != NULL) {
160                         addkey = (struct stats_key *) sigll->object;
161                         if (addkey->colour != 0) {
162                                 sigentry = addkey->colour - 1;
163                                 /* Pretend it's on the primary UID for now */
164                                 sigentry |= 0x40000000;
165                                 sigentry = htonl(sigentry);
166                                 fwrite(&sigentry, sizeof (sigentry), 1, sigs);
167                         }
168                         sigll = sigll->next;
169                 }
170                 pending = pending->next;
171         }
172
173         fclose(sigs);
174         fclose(keys);
175         fclose(names);
176 }
177
178 int main(int argc, char *argv[])
179 {
180         int optchar;
181         char *configfile = NULL, *dir = NULL;
182         uint64_t keyid = 0x2DA8B985;
183
184         while ((optchar = getopt(argc, argv, "c:")) != -1 ) {
185                 switch (optchar) {
186                 case 'c':
187                         configfile = strdup(optarg);
188                         break;
189                 }
190         }
191
192         if (optind < argc) {
193                 dir = argv[optind];
194         }
195
196         readconfig(configfile);
197         initlogthing("wotsap", config.logfile);
198         config.dbbackend->initdb(true);
199         inithash();
200         wotsap(config.dbbackend->getfullkeyid(keyid), dir ? dir : ".");
201         destroyhash();
202         config.dbbackend->cleanupdb();
203         cleanuplogthing();
204         cleanupconfig();
205 }