2 * wotsap.c - Output a set of wotsap files from an onak keyring
6 * http://www.lysator.liu.se/~jc/wotsap/wotfileformat.txt
8 * for more details of the format.
10 * Copyright 2013 Jonathan McDowell <noodles@earth.li>
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.
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
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.
30 #include <arpa/inet.h>
34 #include "onak-conf.h"
38 static struct ll *sortkeyll(struct ll *keys)
40 struct ll *newll, *tmp, **curobj;
41 struct stats_key *curkey, *toadd;
45 toadd = (struct stats_key *) keys->object;
48 curkey = (struct stats_key *) (*curobj)->object;
49 if (curkey->keyid >= toadd->keyid) {
52 curobj = &((*curobj)->next);
56 if (*curobj == NULL || curkey->keyid != toadd->keyid) {
65 static void output_key(FILE *names, FILE *keys, uint64_t keyid)
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);
74 static void wotsap(uint64_t keyid, char *dir)
76 struct ll *pending, *sigll, *sigsave;
78 struct stats_key *curkey, *addkey;
80 FILE *names, *keys, *sigs, *file;
82 uint32_t sigcount, sigentry;
84 /* Length of dir + "/" + "signatures" + NUL */
85 tmppath = malloc(strlen(dir) + 12);
87 sprintf(tmppath, "%s/WOTVERSION", dir);
88 file = fopen(tmppath, "w");
90 fprintf(stderr, "Couldn't open %s\n", tmppath);
93 fprintf(file, "0.2\n");
96 sprintf(tmppath, "%s/README", dir);
97 file = fopen(tmppath, "w");
99 fprintf(stderr, "Couldn't open %s\n", tmppath);
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");
108 sprintf(tmppath, "%s/names", dir);
109 names = fopen(tmppath, "w");
111 fprintf(stderr, "Couldn't open %s\n", tmppath);
114 sprintf(tmppath, "%s/keys", dir);
115 keys = fopen(tmppath, "wb");
117 fprintf(stderr, "Couldn't open %s\n", tmppath);
120 sprintf(tmppath, "%s/signatures", dir);
121 sigs = fopen(tmppath, "wb");
123 fprintf(stderr, "Couldn't open %s\n", tmppath);
128 config.dbbackend->cached_getkeysigs(keyid);
129 curkey = findinhash(keyid);
130 curkey->colour = ++curidx;
131 pending = lladd(NULL, curkey);
133 output_key(names, keys, curkey->keyid);
135 while (pending != NULL) {
136 curkey = (struct stats_key *) pending->object;
137 sigll = config.dbbackend->cached_getkeysigs(curkey->keyid);
138 sigsave = sigll = sortkeyll(sigll);
140 while (sigll != NULL) {
141 addkey = (struct stats_key *) sigll->object;
142 if (addkey->colour == 0 && !addkey->revoked) {
143 uid = config.dbbackend->keyid2uid(addkey->keyid);
145 /* Force it to be loaded so we know if it's revoked */
146 config.dbbackend->cached_getkeysigs(addkey->keyid);
147 if (!addkey->revoked) {
148 addkey->colour = ++curidx;
149 pending = lladdend(pending, addkey);
150 output_key(names, keys, addkey->keyid);
154 if (addkey->colour != 0) {
159 /* Now output the signatures */
160 sigcount = htonl(sigcount);
161 fwrite(&sigcount, sizeof (sigcount), 1, sigs);
163 while (sigll != NULL) {
164 addkey = (struct stats_key *) sigll->object;
165 if (addkey->colour != 0) {
166 sigentry = addkey->colour - 1;
167 /* Pretend it's on the primary UID for now */
168 sigentry |= 0x40000000;
169 sigentry = htonl(sigentry);
170 fwrite(&sigentry, sizeof (sigentry), 1, sigs);
174 pending = pending->next;
182 int main(int argc, char *argv[])
185 char *configfile = NULL, *dir = NULL;
186 uint64_t keyid = 0x2DA8B985;
188 while ((optchar = getopt(argc, argv, "c:")) != -1 ) {
191 configfile = strdup(optarg);
200 readconfig(configfile);
201 initlogthing("wotsap", config.logfile);
202 config.dbbackend->initdb(true);
204 wotsap(config.dbbackend->getfullkeyid(keyid), dir ? dir : ".");
206 config.dbbackend->cleanupdb();