summaryrefslogtreecommitdiff
path: root/root-topology/mktopology.cpp
blob: 2a551cf737573c8aee4c99fbb8004043e25ae671 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <string>
#include <iostream>
#include <map>

#include "../node/Utils.hpp"
#include "../node/Identity.hpp"
#include "../node/Dictionary.hpp"

using namespace ZeroTier;

int main(int argc,char **argv)
{
	std::string buf;

	// Read root-topology-authority.secret signing authority, must be symlinked and online
	if (!Utils::readFile("root-topology-authority.secret",buf)) {
		std::cerr << "Cannot read root-topology-authority.secret" << std::endl;
		return 1;
	}
	Identity topologyAuthority(buf);

	Dictionary topology;

	// Read template.dict to populate default fields in root topology
	// if this file exists. Otherwise we just start empty.
	buf.clear();
	if (Utils::readFile("template.dict",buf))
		topology.fromString(buf);

	// Read all entries in supernodes/ that correspond to supernode entry dictionaries
	// and add them to topology under supernodes/ subkey.
	Dictionary supernodes;
	std::map<std::string,bool> supernodeDictionaries(Utils::listDirectory("supernodes"));
	for(std::map<std::string,bool>::iterator sn(supernodeDictionaries.begin());sn!=supernodeDictionaries.end();++sn) {
		if ((sn->first.length() == 10)&&(!sn->second)) {
			buf.clear();
			if (!Utils::readFile((std::string("supernodes/")+sn->first).c_str(),buf)) {
				std::cerr << "Cannot read supernodes/" << sn->first << std::endl;
				return 1;
			}
			supernodes[sn->first] = buf;
		}
	}
	topology["supernodes"] = supernodes.toString();

	// Sign topology with root-topology-authority.secret
	if (!topology.sign(topologyAuthority)) {
		std::cerr << "Unable to sign!" << std::endl;
		return 1;
	}

	// Test signature to make sure signing worked
	Dictionary test(topology.toString());
	if (!test.verify(topologyAuthority)) {
		std::cerr << "Test verification of signed dictionary failed!" << std::endl;
		return 1;
	}

	// Output to stdout
	std::cout << topology.toString();

	return 0;
}