Added setup to backend

This commit is contained in:
Lukas Metzger 2018-04-12 16:24:36 +02:00
parent 29f97e781e
commit 14039932fe
5 changed files with 340 additions and 1 deletions

View file

@ -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));

View file

@ -0,0 +1,109 @@
<?php
namespace Controllers;
require '../vendor/autoload.php';
use \Slim\Http\Request as Request;
use \Slim\Http\Response as Response;
class Setup
{
public function setup(Request $req, Response $res, array $args)
{
// Check if supplied data has all fields
$body = $req->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 = '<?php' . "\n\n" . 'return ' . var_export($config, true) . ';';
file_put_contents('../config/ConfigUser.php', $configFile);
} catch (\PDOException $e) {
return $res->withJson(['error' => $e->getMessage()], 500);
}
return $res->withStatus(204);
}
}

View file

@ -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);

View file

@ -0,0 +1,23 @@
<?php
require '../vendor/autoload.php';
use \Slim\Http\Request as Request;
use \Slim\Http\Response as Response;
if (file_exists('../config/ConfigUser.php')) {
echo "Not accessible!";
http_response_code(403);
exit();
}
// Prepare dependency container
$container = new \Slim\Container();
// Create application
$app = new \Slim\App($container);
// Create route
$app->post('/v1/setup', '\Controllers\Setup:setup');
// Run application
$app->run();

199
backend/src/sql/setup.sql Normal file
View file

@ -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`);