comparison marns_import.php @ 1:caa68b502313 draft

Added the MARC DNS server (and small fixes in marcus and anoclaims)
author Ivo Smits <Ivo@UCIS.nl>
date Thu, 13 Nov 2014 17:22:12 +0100
parents
children 5c8c4fa95803
comparison
equal deleted inserted replaced
0:3ac7bd7495fd 1:caa68b502313
1 <?php
2 require_once './marccore.php';
3
4 if (!isset($argv)) $argv = $_SERVER['argv'];
5 if (count($argv) < 4) {
6 echo "Usage: ".$argv[0]." [marc-flatfile-database-filename] [domain-name-or-IP-network] [secret-key-or-file] < [zone-file]\n";
7 die();
8 }
9
10 $marclabel = argtolabel($argv[2]);
11 switch (ord($marclabel[0])) {
12 case 1:
13 $bits = ord($marclabel[5]);
14 $orgzonename = 'in-addr.arpa';
15 if (($bits % 8) != 0) throw new Exception('Invalid prefix length for IPv4 network');
16 for ($i = 0; $i < $bits / 8; $i++) $orgzonename = ord($marclabel[$i + 1]).'.'.$orgzonename;
17 break;
18 case 2:
19 $bits = ord($marclabel[17]);
20 $hex = bin2hex(substr($marclabel, 1, 16));
21 $orgzonename = 'ip6.arpa';
22 if (($bits % 4) != 0) throw new Exception('Invalid prefix length for IPv6 network');
23 for ($i = 0; $i < $bits / 4; $i++) $orgzonename = $hex[$i].'.'.$orgzonename;
24 break;
25 case 4:
26 $orgzonename = substr($marclabel, 1);
27 break;
28 default:
29 throw new Exception('Unsupported zone type');
30 }
31 $orgzonename .= '.';
32
33 $stdin = fopen('php://stdin', 'r');
34 $dnsrecords = array();
35 while (($line = fgets($stdin)) !== FALSE) {
36 $line = rtrim($line, "\r\n");
37 if (!strlen($line) || $line[0] == ';') continue;
38 $token = strtok($line, " \t");
39 if ($line[0] != ' ' && $line[0] != "\t") {
40 if (!strlen($token) || $token[0] == ';') continue;
41 if ($token == '@' || strcasecmp($token, $orgzonename) == 0) $name = '';
42 elseif (strlen($token) > strlen($orgzonename) && strcasecmp(substr($token, -strlen($orgzonename)-1), '.'.$orgzonename) == 0) $name = substr($token, 0, -strlen($orgzonename)-1);
43 elseif (strlen($token) && $token[strlen($token) - 1] == '.') continue;
44 else $name = $token;
45 $name = strtolower($name);
46 $token = strtok(" \t");
47 }
48 $rr = array();
49 if (is_numeric($token)) {
50 $rr['ttl'] = intval($token);
51 $token = strtok(" \t");
52 if ($token == 'IN') {
53 $rr['class'] = $token;
54 $token = strtok(" \t");
55 }
56 } else if ($token == 'IN') {
57 $rr['class'] = $token;
58 $token = strtok(" \t");
59 if (is_numeric($token)) {
60 $rr['ttl'] = intval($token);
61 $token = strtok(" \t");
62 }
63 }
64 $rr['type'] = strtoupper($token);
65 switch ($rr['type']) {
66 case 'A':
67 case 'AAAA':
68 $rr['target'] = strtok(" \t");
69 break;
70 case 'CNAME':
71 case 'NS':
72 case 'PTR':
73 $rr['target'] = strtok(" \t");
74 break;
75 case 'MX':
76 $rr['priority'] = strtok(" \t");
77 $rr['target'] = strtok(" \t");
78 break;
79 default:
80 $rr = NULL;
81 }
82 if ($rr === NULL) continue;
83 if (!isset($dnsrecords[$name])) $dnsrecords[$name] = array();
84 $dnsrecords[$name][] = $rr;
85 }
86 fclose($stdin);
87
88 print_r($dnsrecords);
89
90 $skey = $argv[3];
91 if (file_exists($skey)) {
92 $skey = file_get_contents($skey);
93 } else if (strlen($skey) == 64) {
94 $skey = bin2hex($skey);
95 } else if ($skey == '-') {
96 nacl_crypto_sign_ed25519_keypair($skey);
97 }
98 if (strlen($skey) != 32 && strlen($skey) != 64) throw new Exception('Invalid signing key specified');
99
100 error_reporting(E_ALL);
101 $database = new MARCDatabaseFlatFile();
102 $database->Open($argv[1]);
103 $label = chr(4).'ucis-test.ano';
104 $resource = $database->GetResource($marclabel);
105 if ($resource) {
106 $resource = $resource->ToArray();
107 unset($resource['key']);
108 } else {
109 $resource = array('label' => $marclabel, 'value' => array(), 'transfer' => '', 'expiration' => time() + 3600 * 24 * 7);
110 }
111 $resource['value']['dnsrecords'] = $dnsrecords;
112 if (!$database->UpdateResource($resource, $skey)) throw new Exception('Could not update resource');
113 $database->Save();
114 $database->Close();
115
116 function argtolabel($t) {
117 if (preg_match('_^[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}/[0-9]{1-2}$_', $t)) return ipnettolabel($t);
118 if (preg_match('_^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})\z/[0-9]{1-3}_i', $t)) return ipnettolabel($t);
119 if (preg_match('/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/i', $t)) return chr(4).strtolower(trim($t, '.'));
120 throw new Exception('Could not detect label type for '.$t);
121 }
122 function ipnettolabel($s) {
123 $ip = inet_pton(strtok($s, '/'));
124 $pl = intval(strtok('/'));
125 if ($pl == 0) throw new Exception('Invalid IP network specified');
126 if (strlen($ip) == 4) return chr(1).$ip.chr($pl);
127 if (strlen($ip) == 16) return chr(2).$ip.chr($pl);
128 }