#!/opt/bitninja-dojo/run/bin/bitninja-dojo -c=/opt/bitninja/etc * @copyright © 2021 BitNinja Inc. * @package blue * @subpackage main * @version 1.0 */ if (!is_dir('/etc/bitninja')) { mkdir('/etc/bitninja'); } if ($argv[1] !== '--set') { $license_v2_file = "/etc/bitninja/license-v2.json"; $options = getopt("y", ["provision-key::", "type::", "license::", "serverConfigurationMetadata::", "environmentSpecification::"]); $serverConfigMeta = $options['serverConfigurationMetadata'] ?? ""; $envSpec = $options['environmentSpecification'] ?? ""; $metaFile = '/etc/bitninja/server-meta.json'; if ($serverConfigMeta || $envSpec) { setMetaData($metaFile, 'serverConfigurationMetadata', $serverConfigMeta); setMetaData($metaFile, 'environmentSpecification', $envSpec); echo PHP_EOL . "Metadata set." . PHP_EOL; exit(0); } $license_key = $options['provision-key'] ?? ""; if ($license_key) { configLegacy("license_key", trim($license_key)); } else { help(); } $type = trim($options['type'] ?? "server-protection"); $type = in_array($type, ["vps", "server-protection"]) ? $type : "server-protection"; $license = trim($options['license'] ?? ""); $license_v2 = []; if (file_exists($license_v2_file)) { $license_v2 = json_decode(file_get_contents($license_v2_file), true); } $license_v2["createdAt"] = gmdate("Y-m-d H:i:s"); if ($license) { $license_v2["license"] = $license; } $license_v2["type"] = trim($type); file_put_contents($license_v2_file, json_encode($license_v2, JSON_PRETTY_PRINT)); chmod($license_v2_file, 0400); } else { if (strpos($argv[2], '=') === false){ help("legacy"); } $argv2 = explode('=', $argv[2]); $key = trim($argv2[0]); $value = trim($argv2[1]); configLegacy($key, $value); } function setMetaData($metaFile, $metaKey, $metaData) { if (!$metaData) { return; } $currentMeta = []; if (file_exists($metaFile)) { $currentMeta = json_decode(file_get_contents($metaFile), true) ?? []; if (!is_array($currentMeta)) { $currentMeta = []; } } $currentMeta[$metaKey] = json_decode($metaData, true); file_put_contents($metaFile, json_encode($currentMeta, JSON_PRETTY_PRINT)); } function configLegacy($key, $value) { $license_file = '/etc/bitninja/license.php'; if (!is_file($license_file) || (filesize($license_file) == 0)) { file_put_contents($license_file, getEmptyLicense()); } $lines = file('/etc/bitninja/license.php'); foreach ($lines as $line) { if (strpos($line, $key) !== false){ $line = "\t'".$key."' => '".$value."',\n"; } $res[] =$line; } $res_txt = implode("", $res); file_put_contents($license_file, $res_txt); chmod($license_file, 0400); } function help($mode = "") { $message = "Usage: bitninja-config --provision-key=XXXXXX --license=xxxx-xxxx-xxxx-xxxx-xx"; if ($mode === "legacy") { $message = "Usage: bitninja-config --set license_key=XXXXXX"; } echo "\n$message\n\n"; exit(1); } function getEmptyLicense() { $str= << array ( 'license_key' => '', ), ); EOD; return $str; }