index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. //
  2. // ZeroTier One - Global Peer to Peer Ethernet
  3. // Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. // --
  19. //
  20. // ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. // are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. //
  23. // If you would like to embed ZeroTier into a commercial application or
  24. // redistribute it in a modified binary form, please contact ZeroTier Networks
  25. // LLC. Start here: http://www.zerotier.com/
  26. //
  27. // Fields in netconf response dictionary
  28. var ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES = "et";
  29. var ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID = "nwid";
  30. var ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP = "ts";
  31. var ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO = "id";
  32. var ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS = "mpb";
  33. var ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH = "md";
  34. var ZT_NETWORKCONFIG_DICT_KEY_PRIVATE = "p";
  35. var ZT_NETWORKCONFIG_DICT_KEY_NAME = "n";
  36. var ZT_NETWORKCONFIG_DICT_KEY_DESC = "d";
  37. var ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC = "v4s";
  38. var ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC = "v6s";
  39. var ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES = "mr";
  40. var ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP = "com";
  41. // Path to zerotier-idtool binary, invoked to enerate certificates of membership
  42. var ZEROTIER_IDTOOL = '/usr/local/bin/zerotier-idtool';
  43. // From Constants.hpp in node/
  44. var ZT_NETWORK_AUTOCONF_DELAY = 60000;
  45. // Connect to redis, assuming database 0 and no auth (for now)
  46. var redis = require('redis');
  47. var DB = redis.createClient();
  48. DB.on("error",function(err) {
  49. console.error('redis query error: '+err);
  50. });
  51. // Global variables -- these are initialized on startup or netconf-init message
  52. var netconfSigningIdentity = null; // identity of netconf master, with private key portion
  53. var spawn = require('child_process').spawn;
  54. function ztDbTrue(v) { return ((v === '1')||(v === 'true')||(v > 0)); }
  55. function csvToArray(csv) { return (((typeof csv === 'string')&&(csv.length > 0)) ? csv.split(',') : []); }
  56. function arrayToCsv(a) { return ((Array.isArray(a)) ? ((a.length > 0) ? a.join(',') : '') : (((a !== null)&&(typeof a !== 'undefined')) ? a.toString() : '')); }
  57. //
  58. // ZeroTier One Dictionary -- encoding-compatible with Dictionary in C++ code base
  59. //
  60. function Dictionary(fromStr)
  61. {
  62. var thiz = this;
  63. this.data = {};
  64. this._esc = function(data) {
  65. var es = '';
  66. for(var i=0;i<data.length;++i) {
  67. var c = data.charAt(i);
  68. switch(c) {
  69. case '\0': es += '\\0'; break;
  70. case '\r': es += '\\r'; break;
  71. case '\n': es += '\\n'; break;
  72. case '\\': es += '\\\\'; break;
  73. case '=': es += '\\='; break;
  74. default: es += c; break;
  75. }
  76. }
  77. return es;
  78. };
  79. this._unesc = function(s) {
  80. if (typeof s !== 'string')
  81. return '';
  82. var uns = '';
  83. var escapeState = false;
  84. for(var i=0;i<s.length;++i) {
  85. var c = s.charAt(i);
  86. if (escapeState) {
  87. escapeState = false;
  88. switch(c) {
  89. case '0': uns += '\0'; break;
  90. case 'r': uns += '\r'; break;
  91. case 'n': uns += '\n'; break;
  92. default: uns += c; break;
  93. }
  94. } else{
  95. if ((c !== '\r')&&(c !== '\n')&&(c !== '\0')) {
  96. if (c === '\\')
  97. escapeState = true;
  98. else uns += c;
  99. }
  100. }
  101. }
  102. return uns;
  103. };
  104. this.toString = function() {
  105. var str = '';
  106. for(var key in thiz.data) {
  107. str += thiz._esc(key);
  108. str += '=';
  109. var value = thiz.data[key];
  110. if (value)
  111. str += thiz._esc(value.toString());
  112. str += '\n';
  113. }
  114. return str;
  115. };
  116. this.fromString = function(str) {
  117. thiz.data = {};
  118. if (typeof str !== 'string')
  119. return thiz;
  120. var lines = str.split('\n');
  121. for(var l=0;l<lines.length;++l) {
  122. var escapeState = false;
  123. var eqAt = 0;
  124. for(;eqAt<lines[l].length;++eqAt) {
  125. var c = lines[l].charAt(eqAt);
  126. if (escapeState)
  127. escapeState = false;
  128. else if (c === '\\')
  129. escapeState = true;
  130. else if (c === '=')
  131. break;
  132. }
  133. var k = thiz._unesc(lines[l].substr(0,eqAt));
  134. ++eqAt;
  135. if ((k)&&(k.length > 0))
  136. thiz.data[k] = thiz._unesc((eqAt < lines[l].length) ? lines[l].substr(eqAt) : '');
  137. }
  138. return thiz;
  139. };
  140. if ((typeof fromStr === 'string')&&(fromStr.length > 0))
  141. thiz.fromString(fromStr);
  142. };
  143. //
  144. // Identity implementation using zerotier-idtool as subprocess to do actual crypto work
  145. //
  146. function Identity(idstr)
  147. {
  148. var thiz = this;
  149. this.str = '';
  150. this.fields = [];
  151. this.toString = function() {
  152. return thiz.str;
  153. };
  154. this.address = function() {
  155. return ((thiz.fields.length > 0) ? thiz.fields[0] : '0000000000');
  156. };
  157. this.fromString = function(str) {
  158. thiz.str = '';
  159. thiz.fields = [];
  160. if (typeof str !== 'string')
  161. return;
  162. for(var i=0;i<str.length;++i) {
  163. if ("0123456789abcdef:ABCDEF".indexOf(str.charAt(i)) < 0)
  164. return; // invalid character in identity
  165. }
  166. var fields = str.split(':');
  167. if ((fields.length < 3)||(fields[0].length !== 10)||(fields[1] !== '0'))
  168. return;
  169. thiz.fields = fields;
  170. };
  171. this.isValid = function() {
  172. if ((thiz.fields.length < 3)||(thiz.fields[0].length !== 10)||(thiz.fields[1] !== '0'))
  173. return true;
  174. return false;
  175. };
  176. this.hasPrivate = function() {
  177. return ((thiz.isValid())&&(thiz.fields.length >= 4));
  178. };
  179. if (typeof idstr === 'string')
  180. thiz.fromString(idstr);
  181. };
  182. //
  183. // Invokes zerotier-idtool to generate certificates for private networks
  184. //
  185. function generateCertificateOfMembership(nwid,peerAddress,callback)
  186. {
  187. var comTimestamp = '0,' + Date.now().toString(16) + ',' + (ZT_NETWORK_AUTOCONF_DELAY * 4).toString(16);
  188. var comNwid = '1,' + nwid + ',0';
  189. var comIssuedTo = '2,' + peerAddress + ',ffffffffffffffff';
  190. var cert = '';
  191. var certErr = '';
  192. var idtool = spawn(ZEROTIER_IDTOOL,[ 'mkcom',netconfSigningIdentity,comTimestamp,comNwid,comIssuedTo ]);
  193. idtool.stdout.on('data',function(data) {
  194. cert += data;
  195. });
  196. idtool.stderr.on('data',function(data) {
  197. certErr += data;
  198. });
  199. idtool.on('close',function(exitCode) {
  200. if (certErr.length > 0)
  201. console.error('zerotier-idtool stderr returned: '+certErr);
  202. return callback((cert.length > 0) ? cert : null,exitCode);
  203. });
  204. }
  205. //
  206. // Message handler for messages over ZeroTier One service bus
  207. //
  208. function doNetconfInit(message)
  209. {
  210. netconfSigningIdentity = new Identity(message.data['netconfId']);
  211. if (!netconfSigningIdentity.hasPrivate()) {
  212. netconfSigningIdentity = null;
  213. console.error('got invalid netconf signing identity in netconf-init');
  214. }
  215. }
  216. function doNetconfRequest(message)
  217. {
  218. if ((!netconfSigningIdentity)||(!netconfSigningIdentity.hasPrivate())) {
  219. console.error('got netconf-request before netconf-init, ignored');
  220. return;
  221. }
  222. var peerId = new Identity(message.data['peerId']);
  223. var fromIpAndPort = message.data['from'];
  224. var nwid = message.data['nwid'];
  225. var requestId = message.data['requestId'];
  226. if ((!peerId)||(!peerId.isValid())||(!fromIpAndPort)||(!nwid)||(nwid.length !== 16)||(!requestId)) {
  227. console.error('missing one or more required fields in netconf-request');
  228. return;
  229. }
  230. var memberKey = 'zt1:network:'+nwid+':member:'+peerId.address()+':~';
  231. var ipAssignmentsKey = 'zt1:network:'+nwid+':ipAssignments';
  232. var network = null;
  233. var member = null;
  234. var authorized = false;
  235. var v4NeedAssign = false;
  236. var v6NeedAssign = false;
  237. var v4Assignments = [];
  238. var v6Assignments = [];
  239. var ipAssignments = []; // both v4 and v6
  240. async.series([function(next) {
  241. // network lookup
  242. DB.hgetall('zt1:network:'+nwid+':~',function(err,obj) {
  243. network = obj;
  244. return next(err);
  245. });
  246. },function(next) {
  247. // member record lookup, unless public network
  248. if ((!network)||(!('nwid' in network))||(network['nwid'] !== nwid))
  249. return next(null);
  250. DB.hgetall(memberKey,function(err,obj) {
  251. if (err)
  252. return next(err);
  253. if (obj) {
  254. // Update existing member record with new last seen time, etc.
  255. member = obj;
  256. authorized = (ztDbTrue(network['private']) || ztDbTrue(member['authorized']));
  257. DB.hmset(memberKey,{
  258. 'lastSeen': Date.now(),
  259. 'lastAt': fromIpAndPort,
  260. 'clientVersion': (message.data['clientVersion']) ? message.data['clientVersion'] : '?.?.?',
  261. 'clientOs': (message.data['clientOs']) ? message.data['clientOs'] : '?'
  262. },next);
  263. } else {
  264. // Add member record to network for newly seen peer
  265. authorized = ztDbTrue(network['private']) ? false : true; // public networks authorize everyone by default
  266. var now = Date.now().toString();
  267. member = {
  268. 'id': peerId.address(),
  269. 'nwid': nwid,
  270. 'authorized': authorized ? '1' : '0',
  271. 'identity': peerId.toString(),
  272. 'firstSeen': now,
  273. 'lastSeen': now,
  274. 'lastAt': fromIpAndPort,
  275. 'clientVersion': (message.data['clientVersion']) ? message.data['clientVersion'] : '?.?.?',
  276. 'clientOs': (message.data['clientOs']) ? message.data['clientOs'] : '?'
  277. };
  278. DB.hmset(memberKey,member,next);
  279. }
  280. });
  281. },function(next) {
  282. // Figure out which IP address auto-assignments we need to look up or make
  283. if (!authorized)
  284. return next(null);
  285. v4NeedAssign = (network['v4AssignMode'] === 'zt');
  286. v6NeedAssign = (network['v6AssignMode'] === 'zt');
  287. var ipa = csvToArray(member['ipAssignments']);
  288. for(var i=0;i<ipa.length;++i) {
  289. if (ipa[i])
  290. ipAssignments.push(ipa[i]);
  291. if ((ipa[i].indexOf('.') > 0)&&(v4NeedAssign))
  292. v4Assignments.push(ipa[i]);
  293. else if ((ipa[i].indexOf(':') > 0)&&(v6NeedAssign))
  294. v6Assignments.push(ipa[i]);
  295. }
  296. return next(null);
  297. },function(next) {
  298. // assign IPv4 if needed
  299. if ((!authorized)||(!v4NeedAssign)||(v4Assignments.length > 0))
  300. return next(null);
  301. var peerAddress = peerId.address();
  302. var network = 0;
  303. var netmask = 0;
  304. var netmaskBits = 0;
  305. var v4pool = network['v4AssignPool']; // technically csv but only one netblock currently supported
  306. if (v4pool) {
  307. var v4poolSplit = v4Pool.split('/');
  308. if (v4poolSplit.length === 2) {
  309. var networkSplit = v4poolSplit[0].split('.');
  310. if (networkSplit.length === 4) {
  311. network |= (parseInt(networkSplit[0],10) << 24) & 0xff000000;
  312. network |= (parseInt(networkSplit[1],10) << 16) & 0x00ff0000;
  313. network |= (parseInt(networkSplit[2],10) << 8) & 0x0000ff00;
  314. network |= parseInt(networkSplit[3],10) & 0x000000ff;
  315. netmaskBits = parseInt(v4poolSplit[1],10);
  316. if (netmaskBits > 32)
  317. netmaskBits = 32; // sanity check
  318. for(var i=0;i<netmaskBits;++i)
  319. netmask |= (0x80000000 >> i);
  320. netmask &= 0xffffffff;
  321. }
  322. }
  323. }
  324. if ((network === 0)||(netmask === 0xffffffff))
  325. return next(null);
  326. var invmask = netmask ^ 0xffffffff;
  327. var abcd = 0;
  328. var ipAssignmentAttempts = 0;
  329. async.whilst(
  330. function() { return ((v4Assignments.length === 0)&&(ipAssignmentAttempts < 1000)); },
  331. function(next2) {
  332. ++ipAssignmentAttempts;
  333. // Generate or increment IP address source bits
  334. if (abcd === 0) {
  335. var a = parseInt(peerAddress.substr(2,2),16) & 0xff;
  336. var b = parseInt(peerAddress.substr(4,2),16) & 0xff;
  337. var c = parseInt(peerAddress.substr(6,2),16) & 0xff;
  338. var d = parseInt(peerAddress.substr(8,2),16) & 0xff;
  339. abcd = (a << 24) | (b << 16) | (c << 8) | d;
  340. } else ++abcd;
  341. if ((abcd & 0xff) === 0)
  342. abcd |= 1;
  343. abcd &= 0xffffffff;
  344. // Derive an IP to test and generate assignment ip/bits string
  345. var ip = (abcd & invmask) | (network & netmask);
  346. var assignment = ((ip >> 24) & 0xff).toString(10) + '.' + ((ip >> 16) & 0xff).toString(10) + '.' + ((ip >> 8) & 0xff).toString(10) + '.' + (ip & 0xff).toString(10) + '/' + netmaskBits.toString(10);
  347. // Check :ipAssignments to see if this IP is already taken
  348. DB.hget(ipAssignmentsKey,assignment,function(err,value) {
  349. if (err)
  350. return next2(err);
  351. // IP is already taken, try again via async.whilst()
  352. if ((value)&&(value !== peerAddress))
  353. return next2(null); // if someone's already got this IP, keep looking
  354. v4Assignments.push(assignment);
  355. ipAssignments.push(assignment);
  356. // Save assignment to :ipAssignments hash
  357. DB.hset(ipAssignmentsKey,assignment,peerAddress,function(err) {
  358. if (err)
  359. return next2(err);
  360. // Save updated CSV list of assignments to member record
  361. var ipacsv = ipAssignments.join(',');
  362. member['ipAssignments'] = ipacsv;
  363. DB.hset(memberKey,'ipAssignments',ipacsv,next2);
  364. });
  365. });
  366. },
  367. next
  368. );
  369. },function(next) {
  370. // assign IPv6 if needed -- TODO
  371. if ((!authorized)||(!v6NeedAssign)||(v6Assignments.length > 0))
  372. return next(null);
  373. return next(null);
  374. }],function(err) {
  375. if (err) {
  376. console.log('error composing response for '+peerId.address()+': '+err);
  377. return;
  378. }
  379. var response = new Dictionary();
  380. response.data['peer'] = peerId.address();
  381. response.data['nwid'] = nwid;
  382. response.data['type'] = 'netconf-response';
  383. response.data['requestId'] = requestId;
  384. if (authorized) {
  385. var certificateOfMembership = null;
  386. var privateNetwork = ztDbTrue(network['private']);
  387. async.series([function(next) {
  388. // Generate certificate of membership if necessary
  389. if (privateNetwork) {
  390. generateCertificateOfMembership(nwid,peerId.address(),function(cert,exitCode) {
  391. if (cert) {
  392. certificateOfMembership = cert;
  393. return next(null);
  394. } else return next(new Error('zerotier-idtool returned '+exitCode));
  395. });
  396. } else return next(null);
  397. }],function(err) {
  398. if (err) {
  399. console.error('unable to generate certificate for peer '+peerId.address()+' on network '+nwid+': '+err);
  400. response.data['error'] = 'ACCESS_DENIED'; // unable to generate certificate
  401. } else {
  402. var netconf = new Dictionary();
  403. netconf.data[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = network['etherTypes'];
  404. netconf.data[ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID] = nwid;
  405. netconf.data[ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP] = Date.now().toString(16);
  406. netconf.data[ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO] = peerId.address();
  407. //netconf.data[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS] = 0;
  408. //netconf.data[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH] = 0;
  409. //netconf.data[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES] = '';
  410. netconf.data[ZT_NETWORKCONFIG_DICT_KEY_PRIVATE] = privateNetwork ? '1' : '0';
  411. netconf.data[ZT_NETWORKCONFIG_DICT_KEY_NAME] = network['name'];
  412. if (network['desc'])
  413. netconf.data[ZT_NETWORKCONFIG_DICT_KEY_DESC] = network['desc'];
  414. if ((v4NeedAssign)&&(v4Assignments.length > 0))
  415. netconf.data[ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC] = v4Assignments.join(',');
  416. if ((v6NeedAssign)&&(v6Assignments.length > 0))
  417. netconf.data[ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC] = v6Assignments.join(',');
  418. if (certificateOfMembership !== null)
  419. netconf.data[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP] = certificateOfMembership;
  420. response.data['netconf'] = netconf.toString();
  421. }
  422. process.stdout.write(response.toString()+'\n');
  423. });
  424. } else {
  425. // Peer not authorized to join network
  426. response.data['error'] = 'ACCESS_DENIED';
  427. process.stdout.write(response.toString()+'\n');
  428. }
  429. });
  430. }
  431. function handleMessage(dictStr)
  432. {
  433. var message = new Dictionary(dictStr);
  434. if (!('type' in message.data)) {
  435. console.error('ignored message without request type field');
  436. return;
  437. } else if (message.data['type'] === 'netconf-init') {
  438. doNetconfInit(message);
  439. } else if (message.data['type'] === 'netconf-request') {
  440. doNetconfRequest(message);
  441. } else {
  442. console.error('ignored unrecognized message type: '+message.data['type']);
  443. }
  444. };
  445. //
  446. // Read stream of double-CR-terminated dictionaries from stdin until close/EOF
  447. //
  448. var stdinReadBuffer = '';
  449. process.stdin.on('readable',function() {
  450. var chunk = process.stdin.read();
  451. if (chunk)
  452. stdinReadBuffer += chunk;
  453. if ((stdinReadBuffer.length >= 2)&&(stdinReadBuffer.substr(stdinReadBuffer.length - 2) === '\n\n')) {
  454. handleMessage(stdinReadBuffer);
  455. stdinReadBuffer = '';
  456. }
  457. });
  458. process.stdin.on('end',function() {
  459. process.exit(0);
  460. });
  461. process.stdin.on('close',function() {
  462. process.exit(0);
  463. });
  464. process.stdin.on('error',function() {
  465. process.exit(0);
  466. });
  467. // Tell ZeroTier One that the service is running, solicit netconf-init
  468. process.stdout.write('type=ready\n\n');