Mercurial > hg > marc_php
view anoclaims.php @ 4:c642254dc9ee draft default tip
Fixed transfer chain generation and construction of empty updates, some small improvements in tools
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sat, 22 Nov 2014 18:18:52 +0100 |
parents | caa68b502313 |
children |
line wrap: on
line source
<?php require_once './marccore.php'; error_reporting(E_ALL); if (!isset($argv)) $argv = $_SERVER['argv']; $argi = 1; $database = new MARCDatabaseFlatFile('anoclaims.db'); $key = NULL; if (file_exists('anoclaims.key')) { $key = file_get_contents('anoclaims.key'); if (strlen($key) != 32) $key = NULL; } switch (strtoupper($argv[$argi++])) { case 'REGISTER': if (is_null($key)) $key = randombytes(32); $label = chr(0).nacl_crypto_sign_ed25519_keypair($key, $key); $resource = array('label' => $label, 'value' => array('owner' => $argv[$argi++])); if (!$database->UpdateResource($resource, $key)) throw new Exception('Could not update resource'); break; case 'CLAIM': if (is_null($key)) throw new Exception('Key not found'); $label = argtolabel($argv, $argi); $resource = $database->GetResource($label); if (!$resource) $resource = array('label' => $label, 'value' => array()); else $resource = $resource->ToArray(); if (!$database->UpdateResource($resource, $key)) throw new Exception('Could not update resource'); break; case 'SETNS': if (is_null($key)) throw new Exception('Key not found'); $label = argtolabel($argv, $argi); $resource = $database->GetResource($label); if (!$resource) throw new Exception('Resource is not registered'); if (!is_array($resource['value'])) $resource['value'] = array(); if (!isset($resource['value']) || !is_array($resource['value'])) $resource['value'] = array(); if (!isset($resource['value']['ns']) || !is_array($resource['value']['ns'])) $resource['value']['ns'] = array(); $nsname = $argv[$argi++]; if (strlen($nsname) && $nsname[strlen($nsname)-1] != '.') $resource['value']['ns'] = array($nsname => array()); else $resource['value']['ns'] = array($nsname => $argv[$argi++]); if (!$database->UpdateResource($resource, $key)) throw new Exception('Could not update resource'); break; case 'SYNC': $database->SyncHTTP($argv[$argi++]); break; case 'HELP': print_help(); break; default: throw new Exception('Unknown operation '.$argv[$argi-1]); } $database->Save(); $database->Close(); function argtolabel($argv, &$argi) { $t = $argv[$argi++]; if (preg_match('/^AS[0-9]{1-9}$/', $t)) return chr(3).marc_decode_int32be(substr($argv[$argi++], 2)); 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); 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); if (preg_match('/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z0-9]{2,6}$/i', $t)) return chr(4).strtolower(trim($t, '.')); throw new Exception('Could not detect label type for '.$t); } function ipnettolabel($s) { $ip = inet_pton(strtok($s, '/')); $pl = intval(strtok('/')); if ($pl == 0) throw new Exception('Invalid IP network specified'); if (strlen($ip) == 4) return chr(1).$ip.chr($pl); if (strlen($ip) == 16) return chr(2).$ip.chr($pl); } function randombytes($n) { $b = ''; $file = fopen('/dev/urandom', 'r'); for ($i = 0; $i < $n; $i++) $b .= fgetc($file); fclose($file); return $b; } function print_help() { echo 'Usage: anoclaims.php [operation] [arguments] register [ownername] - generate a key pair and register it with specified owner name claim [resource] - claim a resource (eg 1.2.3.0/24, fd63:1e39:6f73:0203::/64, test.ano, AS1234) setns [resource] [nsname]. - define an external DNS server for a domain name or IP network (don\'t forget the .) setns [resource] [nsname] [nsglue] - define an in-zone DNS server for a domain name or IP network with glue record sync [server] - synchronize the local database with a remote HTTP server (eg http://marc.ucis.ano) '; }