Mercurial > hg > marc_php
comparison anoclaims.php @ 0:3ac7bd7495fd draft
Initial commit
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sat, 08 Nov 2014 22:22:42 +0100 |
parents | |
children | caa68b502313 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3ac7bd7495fd |
---|---|
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()); | |
24 if (!$database->UpdateResource($resource, $key)) throw new Exception('Could not update resource'); | |
25 break; | |
26 case 'SETNS': | |
27 if (is_null($key)) throw new Exception('Key not found'); | |
28 $label = argtolabel($argv, $argi); | |
29 $resource = $database->GetResource($label); | |
30 if (!$resource) throw new Exception('Resource is not registered'); | |
31 if (!is_array($resource['value'])) $resource['value'] = array(); | |
32 if (!isset($resource['value']) || !is_array($resource['value'])) $resource['value'] = array(); | |
33 if (!isset($resource['value']['ns']) || !is_array($resource['value']['ns'])) $resource['value']['ns'] = array(); | |
34 $nsname = $argv[$argi++]; | |
35 if (strlen($nsname) && $nsname[strlen($nsname)-1] != '.') $resource['value']['ns'] = array($nsname => array()); | |
36 else $resource['value']['ns'] = array($nsname => $argv[$argi++]); | |
37 if (!$database->UpdateResource($resource, $key)) throw new Exception('Could not update resource'); | |
38 break; | |
39 case 'SYNC': | |
40 $database->SyncHTTP($argv[$argi++]); | |
41 break; | |
42 case 'HELP': | |
43 print_help(); | |
44 break; | |
45 default: | |
46 throw new Exception('Unknown operation '.$argv[$argi-1]); | |
47 } | |
48 $database->Save(); | |
49 $database->Close(); | |
50 | |
51 function argtolabel($argv, &$argi) { | |
52 $t = $argv[$argi++]; | |
53 if (preg_match('/^AS[0-9]{1-9}$/', $t)) return chr(3).marc_decode_int32be(substr($argv[$argi++], 2)); | |
54 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); | |
55 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); | |
56 if (preg_match('/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/i', $t)) return chr(4).strtolower(trim($t, '.')); | |
57 throw new Exception('Could not detect label type for '.$t); | |
58 } | |
59 function ipnettolabel($s) { | |
60 $ip = inet_pton(strtok($s, '/')); | |
61 $pl = intval(strtok('/')); | |
62 if ($pl == 0) throw new Exception('Invalid IP network specified'); | |
63 if (strlen($ip) == 4) return chr(1).$ip.chr($pl); | |
64 if (strlen($ip) == 16) return chr(2).$ip.chr($pl); | |
65 } | |
66 function randombytes($n) { | |
67 $b = ''; | |
68 $file = fopen('/dev/urandom', 'r'); | |
69 for ($i = 0; $i < $n; $i++) $b .= fgetc($file); | |
70 fclose($file); | |
71 return $b; | |
72 } | |
73 | |
74 function print_help() { | |
75 echo 'Usage: anoclaims.php [operation] [arguments] | |
76 register [ownername] - generate a key pair and register it with specified owner name | |
77 claim [resource] - claim a resource (eg 1.2.3.0/24, fd63:1e39:6f73:0203::/64, test.ano, AS1234) | |
78 setns [resource] [nsname]. - define an external DNS server for a domain name or IP network (don\'t forget the .) | |
79 setns [resource] [nsname] [nsglue] - define an in-zone DNS server for a domain name or IP network with glue record | |
80 sync [server] - synchronize the local database with a remote HTTP server (eg http://marc.ucis.ano) | |
81 '; | |
82 } | |
83 |