]> the.earth.li Git - onak.git/blob - wotsap.c
Move to CMake over autoconf
[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, see <https://www.gnu.org/licenses/>.
23  */
24
25 #include <getopt.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <arpa/inet.h>
30
31 #include "build-config.h"
32
33 #include "hash.h"
34 #include "log.h"
35 #include "onak-conf.h"
36 #include "stats.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(struct onak_dbctx *dbctx,
66                 FILE *names, FILE *keys, uint64_t keyid)
67 {
68         fprintf(names, "%s\n", dbctx->keyid2uid(dbctx, keyid));
69         fprintf(keys, "%c%c%c%c", (int) (keyid >> 24) & 0xFF,
70                         (int) (keyid >> 16) & 0xFF,
71                         (int) (keyid >>  8) & 0xFF,
72                         (int) (keyid      ) & 0xFF);
73 }
74
75 static void wotsap(struct onak_dbctx *dbctx, uint64_t keyid, char *dir)
76 {
77         struct ll *pending, *sigll, *sigsave;
78         uint32_t curidx = 0;
79         struct stats_key *curkey, *addkey;
80         char *uid;
81         FILE *names, *keys, *sigs, *file;
82         char *tmppath;
83         uint32_t sigcount, sigentry;
84
85         /* Length of dir + "/" + "signatures" + NUL */
86         tmppath = malloc(strlen(dir) + 12);
87
88         sprintf(tmppath, "%s/WOTVERSION", dir);
89         file = fopen(tmppath, "w");
90         if (file == NULL) {
91                 fprintf(stderr, "Couldn't open %s\n", tmppath);
92                 return;
93         }
94         fprintf(file, "0.2\n");
95         fclose(file);
96
97         sprintf(tmppath, "%s/README", dir);
98         file = fopen(tmppath, "w");
99         if (file == NULL) {
100                 fprintf(stderr, "Couldn't open %s\n", tmppath);
101                 return;
102         }
103         fprintf(file, "This is a Web of Trust archive.\n");
104         fprintf(file, "The file format is documented at:\n");
105         fprintf(file, "  http://www.lysator.liu.se/~jc/wotsap/wotfileformat.txt\n\n");
106         fprintf(file, "This file was generated by onak " ONAK_VERSION " \n");
107         fclose(file);
108
109         sprintf(tmppath, "%s/names", dir);
110         names = fopen(tmppath, "w");
111         if (names == NULL) {
112                 fprintf(stderr, "Couldn't open %s\n", tmppath);
113                 return;
114         }
115         sprintf(tmppath, "%s/keys", dir);
116         keys = fopen(tmppath, "wb");
117         if (keys == NULL) {
118                 fprintf(stderr, "Couldn't open %s\n", tmppath);
119                 return;
120         }
121         sprintf(tmppath, "%s/signatures", dir);
122         sigs = fopen(tmppath, "wb");
123         if (sigs == NULL) {
124                 fprintf(stderr, "Couldn't open %s\n", tmppath);
125                 return;
126         }
127         free(tmppath);
128
129         dbctx->cached_getkeysigs(dbctx, keyid);
130         curkey = findinhash(keyid);
131         curkey->colour = ++curidx;
132         pending = lladd(NULL, curkey);
133
134         output_key(dbctx, names, keys, curkey->keyid);
135
136         while (pending != NULL) {
137                 curkey = (struct stats_key *) pending->object;
138                 sigll = dbctx->cached_getkeysigs(dbctx, curkey->keyid);
139                 sigsave = sigll = sortkeyll(sigll);
140                 sigcount = 0;
141                 while (sigll != NULL) {
142                         addkey = (struct stats_key *) sigll->object;
143                         if (addkey->colour == 0 && !addkey->revoked) {
144                                 uid = dbctx->keyid2uid(dbctx, addkey->keyid);
145                                 if (uid != NULL) {
146                                         /* Force it to be loaded so we know if it's revoked */
147                                         dbctx->cached_getkeysigs(dbctx,
148                                                         addkey->keyid);
149                                         if (!addkey->revoked) {
150                                                 addkey->colour = ++curidx;
151                                                 pending = lladdend(pending, addkey);
152                                                 output_key(dbctx, names, keys,
153                                                         addkey->keyid);
154                                         }
155                                 }
156                         }
157                         if (addkey->colour != 0) {
158                                 sigcount++;
159                         }
160                         sigll = sigll->next;
161                 }
162                 /* Now output the signatures */
163                 sigcount = htonl(sigcount);
164                 fwrite(&sigcount, sizeof (sigcount), 1, sigs);
165                 sigll = sigsave;
166                 while (sigll != NULL) {
167                         addkey = (struct stats_key *) sigll->object;
168                         if (addkey->colour != 0) {
169                                 sigentry = addkey->colour - 1;
170                                 /* Pretend it's on the primary UID for now */
171                                 sigentry |= 0x40000000;
172                                 sigentry = htonl(sigentry);
173                                 fwrite(&sigentry, sizeof (sigentry), 1, sigs);
174                         }
175                         sigll = sigll->next;
176                 }
177                 pending = pending->next;
178         }
179
180         fclose(sigs);
181         fclose(keys);
182         fclose(names);
183 }
184
185 int main(int argc, char *argv[])
186 {
187         int optchar;
188         char *configfile = NULL, *dir = NULL;
189         uint64_t keyid = 0x2DA8B985;
190         struct onak_dbctx *dbctx;
191
192         while ((optchar = getopt(argc, argv, "c:")) != -1 ) {
193                 switch (optchar) {
194                 case 'c':
195                         configfile = strdup(optarg);
196                         break;
197                 }
198         }
199
200         if (optind < argc) {
201                 dir = argv[optind];
202         }
203
204         readconfig(configfile);
205         initlogthing("wotsap", config.logfile);
206         dbctx = config.dbinit(config.backend, true);
207         if (dbctx != NULL) {
208                 inithash();
209                 wotsap(dbctx, dbctx->getfullkeyid(dbctx, keyid),
210                         dir ? dir : ".");
211                 destroyhash();
212                 dbctx->cleanupdb(dbctx);
213         } else {
214                 fprintf(stderr, "Couldn't initialize key database.\n");
215         }
216         cleanuplogthing();
217         cleanupconfig();
218         free(configfile);
219 }