-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamed.js
134 lines (126 loc) · 4.54 KB
/
named.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var iputils = require("./iputils");
var wait = new (require("./Wait"));
// probably a complete implementation of the legacy namecoin format.
// (modulo a crapton of bugs.)
// XXX: the translate stuff doesn't quite work. ie. www.qr.bit works, but qr.bit doesn't.
function generateNamedConf(obj, confname, dbname, callback) {
var config = [
"# This file was generated by "+process.argv[1]+" on "+new Date,
'',
'zone "bit" { type master; file "'+dbname+'"; allow-query { any; }; };'
]; // will hold the named.conf entries
var dbconfig = [
// The values in here are probably horribly inappropriate. XXX
"; This file was generated by "+process.argv[1]+" on "+new Date,
"$TTL 14400",
"$ORIGIN bit.",
"",
"; Specify the primary nameserver ns1.bit in SOA",
"@ 14400 IN SOA ns1.bit. webmaster.bit. (",
" 2008092902 ; Serial in YYYYMMDDXX (XX is increment)",
" 10800; refresh seconds",
" 3600; retry",
" 604800; expire",
" 38400; minimum",
" );",
"",
" IN NS ns1.bit.",
"ns1 IN A 127.0.0.1",
""
];
function addDbLine(sub, domain, dst) {
var host = (sub==""?domain:sub+"."+domain);
if (!iputils.isPlausibleDomain(host)) return;
if (iputils.isIPv4Address(dst)) {
if (iputils.isRoutableIPv4Address(dst)) {
dbconfig.push(host+" IN A "+dst);
dbconfig.push("*."+host+" IN A "+dst);
}
} else {
iputils.lookupDomain(dst, wait.for(function(err,data) {
if (!err) {
addDbLine(sub, domain, data);
}
}));
}
}
function addDbTranslate(sub, domain, dst) {
var host = (sub==""?domain:sub+"."+domain);
if (!iputils.isPlausibleDomain(host)) return;
if (!iputils.isPlausibleDomain(dst)) return;
dbconfig.push(host+" IN DNAME "+dst+".");
}
function addZone(sub, domain, nss) {
var host = (sub==""?domain:sub+"."+domain)+".bit";
var forwarders = [];
if (!iputils.isPlausibleDomain(host)) return;
for (var i=0;i<nss.length;i++) {
if (!iputils.isIPv4Address(nss[i])) {
// we need to resolve. start resolve and postpone the rest.
iputils.lookupDomain(nss[i], wait.for(function(err, data) {
if (!err) {
nss[i] = data;
} else {
// doesn't resolve. remove item from array.
nss.splice(i,1);
}
addZone(sub, domain, nss);
}));
return;
} else {
// make sure we're routable.
if (iputils.isRoutableIPv4Address(nss[i])) {
forwarders.push(nss[i]);
} else {
// drop.
}
}
}
if (forwarders.length>0) {
// we have at least one valid DNS server left. okay.
config.push('zone "'+host+'" { type forward; forwarders { '+forwarders.join("; ")+"; }; };");
}
}
for (var name in obj) {
var value = obj[name];
if (value.map) {
for (var key in value.map) {
var item = value.map[key];
//console.log("looking at "+name+" value="+value.map+", item is "+JSON.stringify(item));
if (typeof item == "string") {
addDbLine(key, name, item);
} else if (item) {
// probably an object. check for "ns" and "translate" fields.
if (item.translate) {
addDbTranslate(key, name, item.translate);
// if translate, no zone forwarders. until i figure out how to mix the two.
} else if (item.ns) {
if (item.ns instanceof Array) {
// multiple NS. no problem.
addZone(key, name, item.ns);
} else if (typeof item.ns == "string") {
// not sure if legit. accept for now. XXX
addZone(key, name, [item.ns]);
} else {
// bogus ns field. ignore.
}
}
} else {
// invalid map field. probably "null" or somesuch nonsense. ignore.
}
}
}
}
wait.on("done", function () {
var fs = require("fs");
var data = config.join('\n');
fs.writeFile(confname, data, 'utf8', function(){
var data = dbconfig.join('\n');
fs.writeFile(dbname, data, 'utf8', callback);
});
});
wait.start();
}
module.exports = {
generateNamedConf: generateNamedConf
};