Mercurial > hg > marc_php
annotate anoclaims.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 | 3ac7bd7495fd |
children | c642254dc9ee |
rev | line source |
---|---|
0 | 1 <?php |
2 require_once './marccore.php'; | |
3 error_reporting(E_ALL); | |
4 if (!isset($argv)) $argv = $_SERVER['argv']; | |
5 $argi = 1; | |
6 $database = new MARCDatabaseFlatFile('anoclaims.db'); | |
7 $key = NULL; | |
8 if (file_exists('anoclaims.key')) { | |
9 $key = file_get_contents('anoclaims.key'); | |
10 if (strlen($key) != 32) $key = NULL; | |
11 } | |
12 switch (strtoupper($argv[$argi++])) { | |
13 case 'REGISTER': | |
14 if (is_null($key)) $key = randombytes(32); | |
15 $label = chr(0).nacl_crypto_sign_ed25519_keypair($key, $key); | |
16 $resource = array('label' => $label, 'value' => array('owner' => $argv[$argi++])); | |
17 if (!$database->UpdateResource($resource, $key)) throw new Exception('Could not update resource'); | |
18 break; | |
19 case 'CLAIM': | |
20 if (is_null($key)) throw new Exception('Key not found'); | |
21 $label = argtolabel($argv, $argi); | |
22 $resource = $database->GetResource($label); | |
23 if (!$resource) $resource = array('label' => $label, 'value' => array()); | |
1
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
24 else $resource = $resource->ToArray(); |
0 | 25 if (!$database->UpdateResource($resource, $key)) throw new Exception('Could not update resource'); |
26 break; | |
27 case 'SETNS': | |
28 if (is_null($key)) throw new Exception('Key not found'); | |
29 $label = argtolabel($argv, $argi); | |
30 $resource = $database->GetResource($label); | |
31 if (!$resource) throw new Exception('Resource is not registered'); | |
32 if (!is_array($resource['value'])) $resource['value'] = array(); | |
33 if (!isset($resource['value']) || !is_array($resource['value'])) $resource['value'] = array(); | |
34 if (!isset($resource['value']['ns']) || !is_array($resource['value']['ns'])) $resource['value']['ns'] = array(); | |
35 $nsname = $argv[$argi++]; | |
36 if (strlen($nsname) && $nsname[strlen($nsname)-1] != '.') $resource['value']['ns'] = array($nsname => array()); | |
37 else $resource['value']['ns'] = array($nsname => $argv[$argi++]); | |
38 if (!$database->UpdateResource($resource, $key)) throw new Exception('Could not update resource'); | |
39 break; | |
40 case 'SYNC': | |
41 $database->SyncHTTP($argv[$argi++]); | |
42 break; | |
43 case 'HELP': | |
44 print_help(); | |
45 break; | |
46 default: | |
47 throw new Exception('Unknown operation '.$argv[$argi-1]); | |
48 } | |
49 $database->Save(); | |
50 $database->Close(); | |
51 | |
52 function argtolabel($argv, &$argi) { | |
53 $t = $argv[$argi++]; | |
54 if (preg_match('/^AS[0-9]{1-9}$/', $t)) return chr(3).marc_decode_int32be(substr($argv[$argi++], 2)); | |
55 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 ipv4tolabel($t); | |
56 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 ipv6tolabel($t); | |
57 if (preg_match('/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/i', $t)) return chr(4).strtolower(trim($t, '.')); | |
58 throw new Exception('Could not detect label type for '.$t); | |
59 } | |
60 function ipnettolabel($s) { | |
61 $ip = inet_pton(strtok($s, '/')); | |
62 $pl = intval(strtok('/')); | |
63 if ($pl == 0) throw new Exception('Invalid IP network specified'); | |
64 if (strlen($ip) == 4) return chr(1).$ip.chr($pl); | |
65 if (strlen($ip) == 16) return chr(2).$ip.chr($pl); | |
66 } | |
67 function randombytes($n) { | |
68 $b = ''; | |
69 $file = fopen('/dev/urandom', 'r'); | |
70 for ($i = 0; $i < $n; $i++) $b .= fgetc($file); | |
71 fclose($file); | |
72 return $b; | |
73 } | |
74 | |
75 function print_help() { | |
76 echo 'Usage: anoclaims.php [operation] [arguments] | |
77 register [ownername] - generate a key pair and register it with specified owner name | |
78 claim [resource] - claim a resource (eg 1.2.3.0/24, fd63:1e39:6f73:0203::/64, test.ano, AS1234) | |
79 setns [resource] [nsname]. - define an external DNS server for a domain name or IP network (don\'t forget the .) | |
80 setns [resource] [nsname] [nsglue] - define an in-zone DNS server for a domain name or IP network with glue record | |
81 sync [server] - synchronize the local database with a remote HTTP server (eg http://marc.ucis.ano) | |
82 '; | |
83 } | |
84 |