From 14039932fe95b112d64b6e013c5c966bace2345a Mon Sep 17 00:00:00 2001 From: Lukas Metzger Date: Thu, 12 Apr 2018 16:24:36 +0200 Subject: [PATCH] Added setup to backend --- backend/src/config/ConfigDefault.php | 4 +- backend/src/controllers/Setup.php | 109 +++++++++++++++ backend/src/public/index.php | 6 + backend/src/public/setup.php | 23 ++++ backend/src/sql/setup.sql | 199 +++++++++++++++++++++++++++ 5 files changed, 340 insertions(+), 1 deletion(-) create mode 100644 backend/src/controllers/Setup.php create mode 100644 backend/src/public/setup.php create mode 100644 backend/src/sql/setup.sql diff --git a/backend/src/config/ConfigDefault.php b/backend/src/config/ConfigDefault.php index d7e3118..7154429 100644 --- a/backend/src/config/ConfigDefault.php +++ b/backend/src/config/ConfigDefault.php @@ -38,8 +38,10 @@ $defaultConfig = [ if (file_exists('../config/ConfigOverride.php')) { $userConfig = require('ConfigOverride.php'); -} else { +} elseif (file_exists('../config/ConfigUser.php')) { $userConfig = require('ConfigUser.php'); +} else { + return false; } return array('config' => array_replace_recursive($defaultConfig, $userConfig)); diff --git a/backend/src/controllers/Setup.php b/backend/src/controllers/Setup.php new file mode 100644 index 0000000..179fa5a --- /dev/null +++ b/backend/src/controllers/Setup.php @@ -0,0 +1,109 @@ +getParsedBody(); + + if ($body === null) { + return $res->withJson(['error' => 'The supplied body was empty'], 400); + } + + if (!array_key_exists('db', $body) || !array_key_exists('admin', $body)) { + return $res->withJson(['error' => 'One of the required fields is missing.'], 422); + } + + $db = $body['db']; + $admin = $body['admin']; + + if (!array_key_exists('host', $db) || !array_key_exists('user', $db) || + !array_key_exists('password', $db) || !array_key_exists('database', $db) || + !array_key_exists('port', $db) || !array_key_exists('name', $admin) || + !array_key_exists('password', $admin)) { + return $res->withJson(['error' => 'One of the required fields is missing.'], 422); + } + + // Check if pdo exists + if (!extension_loaded('pdo')) { + return $res->withJson(['error' => 'PDO extension is not enabled.'], 500); + } + if (!extension_loaded('pdo_mysql')) { + return $res->withJson(['error' => 'PDO mysql extension is not enabled.'], 500); + } + + // Check if apcu exists + if (!extension_loaded('apcu')) { + return $res->withJson(['error' => 'APCU extension is not enabled.'], 500); + } + + try { + // Test database connection + $pdo = new \PDO( + 'mysql:host=' . $db['host'] . ';port=' . $db['port'] . ';dbname=' . $db['database'], + $db['user'], + $db['password'] + ); + + // Configure db connection + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); + + // Check if database is empty + $query = $pdo->prepare('SHOW TABLES'); + $query->execute(); + if ($query->fetch() !== false) { + return $res->withJson(['error' => 'The database is not empty.'], 500); + } + + // Check if config can be written + if (file_put_contents('../config/ConfigUser.php', 'test') === false) { + return $res->withJson(['error' => 'Write of config file failed, check that the PHP user can write in the config directory.'], 500); + } else { + unlink('../config/ConfigUser.php'); + } + + // Execute sql from setup file + $sqlLines = explode(';', file_get_contents('../sql/setup.sql')); + + foreach ($sqlLines as $sql) { + if (strlen(trim($sql)) > 0) { + var_dump($sql); + $pdo->exec($sql); + } + } + + // Create admin user + $query = $pdo->prepare('INSERT INTO users (name, backend, type, password) VALUES (:name, :backend, :type, :password)'); + $query->bindValue(':name', $admin['name']); + $query->bindValue(':backend', 'native'); + $query->bindValue(':type', 'admin'); + $query->bindValue(':password', password_hash($admin['password'], PASSWORD_DEFAULT)); + $query->execute(); + + // Save config file + $config = [ + 'db' => [ + 'host' => 'mysql.dmz.intranet', + 'user' => 'pdnsnew', + 'password' => 'pdnsnew', + 'dbname' => 'pdnsnew' + ] + ]; + $configFile = 'withJson(['error' => $e->getMessage()], 500); + } + + return $res->withStatus(204); + } +} diff --git a/backend/src/public/index.php b/backend/src/public/index.php index a8405f8..89743ca 100644 --- a/backend/src/public/index.php +++ b/backend/src/public/index.php @@ -8,6 +8,12 @@ use \Slim\Http\Response as Response; // Load config $config = require('../config/ConfigDefault.php'); +// If no config exists load installer +if ($config === false) { + require('setup.php'); + exit(); +} + // Prepare dependency container $container = new \Slim\Container($config); diff --git a/backend/src/public/setup.php b/backend/src/public/setup.php new file mode 100644 index 0000000..a6cd0d8 --- /dev/null +++ b/backend/src/public/setup.php @@ -0,0 +1,23 @@ +post('/v1/setup', '\Controllers\Setup:setup'); + +// Run application +$app->run(); diff --git a/backend/src/sql/setup.sql b/backend/src/sql/setup.sql new file mode 100644 index 0000000..6a32d52 --- /dev/null +++ b/backend/src/sql/setup.sql @@ -0,0 +1,199 @@ +-- -------------------------------------------------------- + +-- +-- Table structure for table `comments` +-- + +CREATE TABLE `comments` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `domain_id` int(11) NOT NULL, + `name` varchar(255) NOT NULL, + `type` varchar(10) NOT NULL, + `modified_at` int(11) NOT NULL, + `account` varchar(40) CHARACTER SET utf8 DEFAULT NULL, + `comment` text CHARACTER SET utf8 NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `cryptokeys` +-- + +CREATE TABLE `cryptokeys` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `domain_id` int(11) NOT NULL, + `flags` int(11) NOT NULL, + `active` tinyint(1) DEFAULT NULL, + `content` text, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `domainmetadata` +-- + +CREATE TABLE `domainmetadata` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `domain_id` int(11) NOT NULL, + `kind` varchar(32) DEFAULT NULL, + `content` text, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `domains` +-- + +CREATE TABLE `domains` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL, + `master` varchar(128) DEFAULT NULL, + `last_check` int(11) DEFAULT NULL, + `type` varchar(6) NOT NULL, + `notified_serial` int(10) UNSIGNED DEFAULT NULL, + `account` varchar(40) CHARACTER SET utf8 DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `permissions` +-- + +DROP TABLE IF EXISTS `permissions`; +CREATE TABLE `permissions` ( + `domain_id` int(11) NOT NULL, + `user_id` int(11) NOT NULL, + PRIMARY KEY (`domain_id`, `user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `records` +-- + +DROP TABLE IF EXISTS `records`; +CREATE TABLE `records` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `domain_id` int(11) DEFAULT NULL, + `name` varchar(255) DEFAULT NULL, + `type` varchar(10) DEFAULT NULL, + `content` varchar(64000) DEFAULT NULL, + `ttl` int(11) DEFAULT NULL, + `prio` int(11) DEFAULT NULL, + `change_date` int(11) DEFAULT NULL, + `disabled` tinyint(1) DEFAULT '0', + `ordername` varchar(255) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL, + `auth` tinyint(1) DEFAULT '1', + PRIMARY KEY(`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `remote` +-- + +DROP TABLE IF EXISTS `remote`; +CREATE TABLE `remote` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `record` int(11) NOT NULL, + `description` varchar(255) NOT NULL, + `type` varchar(20) NOT NULL, + `security` varchar(2000) NOT NULL, + `nonce` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `supermasters` +-- + +DROP TABLE IF EXISTS `supermasters`; +CREATE TABLE `supermasters` ( + `ip` varchar(64) NOT NULL, + `nameserver` varchar(255) NOT NULL, + `account` varchar(40) CHARACTER SET utf8 NOT NULL, + PRIMARY KEY (`ip`, `nameserver`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `tsigkeys` +-- + +DROP TABLE IF EXISTS `tsigkeys`; +CREATE TABLE `tsigkeys` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) DEFAULT NULL, + `algorithm` varchar(50) DEFAULT NULL, + `secret` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `users` +-- + +DROP TABLE IF EXISTS `users`; +CREATE TABLE `users` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(50) NOT NULL, + `backend` varchar(50) NOT NULL, + `type` varchar(20) NOT NULL, + `password` varchar(255) DEFAULT NULL, + PRIMARY KEY(`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Indexes for dumped tables +-- + +-- +-- Indexes for table `comments` +-- +ALTER TABLE `comments` + ADD KEY `comments_name_type_idx` (`name`,`type`), + ADD KEY `comments_order_idx` (`domain_id`,`modified_at`); + +-- +-- Indexes for table `cryptokeys` +-- +ALTER TABLE `cryptokeys` + ADD KEY `domainidindex` (`domain_id`); + +-- +-- Indexes for table `domainmetadata` +-- +ALTER TABLE `domainmetadata` + ADD KEY `domainmetadata_idx` (`domain_id`,`kind`); + +-- +-- Indexes for table `records` +-- +ALTER TABLE `records` + ADD KEY `nametype_index` (`name`,`type`), + ADD KEY `domain_id` (`domain_id`), + ADD KEY `ordername` (`ordername`); + + +-- +-- Indexes for table `tsigkeys` +-- +ALTER TABLE `tsigkeys` + ADD UNIQUE KEY `namealgoindex` (`name`,`algorithm`);