From 63e1acc02acd497d3747d9558402d266874bdc2b Mon Sep 17 00:00:00 2001 From: cytopia Date: Sat, 9 Sep 2017 10:40:07 +0200 Subject: [PATCH] DVL-012 #121 Password protect the devilbox intranet --- .devilbox/www/config.php | 3 + .devilbox/www/htdocs/_ajax_callback.php | 36 +++-- .devilbox/www/htdocs/assets/css/custom.css | 166 +++++++++++++-------- .devilbox/www/htdocs/credits.php | 1 + .devilbox/www/htdocs/db_memcd.php | 1 + .devilbox/www/htdocs/db_mongo.php | 1 + .devilbox/www/htdocs/db_mysql.php | 1 + .devilbox/www/htdocs/db_pgsql.php | 1 + .devilbox/www/htdocs/db_redis.php | 1 + .devilbox/www/htdocs/debug.php | 1 + .devilbox/www/htdocs/index.php | 1 + .devilbox/www/htdocs/info_httpd.php | 1 + .devilbox/www/htdocs/info_memcd.php | 1 + .devilbox/www/htdocs/info_mongo.php | 1 + .devilbox/www/htdocs/info_mysql.php | 1 + .devilbox/www/htdocs/info_pgsql.php | 1 + .devilbox/www/htdocs/info_php.php | 1 + .devilbox/www/htdocs/info_redis.php | 1 + .devilbox/www/htdocs/login.php | 66 ++++++++ .devilbox/www/htdocs/logout.php | 11 ++ .devilbox/www/htdocs/mail.php | 3 +- .devilbox/www/htdocs/opcache.php | 1 + .devilbox/www/htdocs/vhosts.php | 1 + .devilbox/www/include/lib/Helper.php | 60 ++++++++ .devilbox/www/include/lib/Html.php | 9 ++ docker-compose.yml | 6 + env-example | 30 ++++ 27 files changed, 327 insertions(+), 80 deletions(-) create mode 100644 .devilbox/www/htdocs/login.php create mode 100644 .devilbox/www/htdocs/logout.php diff --git a/.devilbox/www/config.php b/.devilbox/www/config.php index b44c0740..4686e37b 100644 --- a/.devilbox/www/config.php +++ b/.devilbox/www/config.php @@ -2,6 +2,9 @@ // Measure time $TIME_START = microtime(true); +// Start session +session_start(); + // Turn on all PHP errors error_reporting(-1); diff --git a/.devilbox/www/htdocs/_ajax_callback.php b/.devilbox/www/htdocs/_ajax_callback.php index 37470562..fc9c3304 100644 --- a/.devilbox/www/htdocs/_ajax_callback.php +++ b/.devilbox/www/htdocs/_ajax_callback.php @@ -1,19 +1,27 @@ (string)loadClass('Mysql')->getDBSize($_GET['database']), - 'table' => (string)loadClass('Mysql')->getTableCount($_GET['database']) - )); - } else if (isset($_GET['type']) && $_GET['type'] == 'postgres') { - $schema = isset($_GET['schema']) ? $_GET['schema'] : ''; - echo json_encode(array( - 'size' => (string)loadClass('Pgsql')->getSchemaSize($_GET['database'], $schema), - 'table' => (string)loadClass('Pgsql')->getTableCount($_GET['database'], $schema) - )); +if (loadClass('Helper')->isLoggedIn()) { + + if (isset($_GET['database'])) { + if (isset($_GET['type']) && $_GET['type'] == 'mysql') { + echo json_encode(array( + 'size' => (string)loadClass('Mysql')->getDBSize($_GET['database']), + 'table' => (string)loadClass('Mysql')->getTableCount($_GET['database']) + )); + } else if (isset($_GET['type']) && $_GET['type'] == 'postgres') { + $schema = isset($_GET['schema']) ? $_GET['schema'] : ''; + echo json_encode(array( + 'size' => (string)loadClass('Pgsql')->getSchemaSize($_GET['database'], $schema), + 'table' => (string)loadClass('Pgsql')->getTableCount($_GET['database'], $schema) + )); + } + } else if (isset($_GET['vhost'])) { + echo loadClass('Httpd')->checkVirtualHost($_GET['vhost']); + } else { + loadClass('Helper')->redirect('/'); } -} else if (isset($_GET['vhost'])) { - echo loadClass('Httpd')->checkVirtualHost($_GET['vhost']); + +} else { + loadClass('Helper')->redirect('/'); } diff --git a/.devilbox/www/htdocs/assets/css/custom.css b/.devilbox/www/htdocs/assets/css/custom.css index 1ab8f6c8..945c69c5 100644 --- a/.devilbox/www/htdocs/assets/css/custom.css +++ b/.devilbox/www/htdocs/assets/css/custom.css @@ -1,11 +1,11 @@ .meter { - height: 20px; /* Can be anything */ - position: relative; - background: #555; - -moz-border-radius: 25px; - -webkit-border-radius: 25px; - border-radius: 25px; - box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3); + height: 20px; /* Can be anything */ + position: relative; + background: #555; + -moz-border-radius: 25px; + -webkit-border-radius: 25px; + border-radius: 25px; + box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3); } .meter > span { display: block; @@ -16,49 +16,49 @@ border-bottom-left-radius: 20px; background-color: rgb(43,194,83); background-image: linear-gradient( - center bottom, - rgb(43,194,83) 37%, - rgb(84,240,84) 69% + center bottom, + rgb(43,194,83) 37%, + rgb(84,240,84) 69% ); box-shadow: - inset 0 2px 9px rgba(255,255,255,0.3), - inset 0 -2px 6px rgba(0,0,0,0.4); + inset 0 2px 9px rgba(255,255,255,0.3), + inset 0 -2px 6px rgba(0,0,0,0.4); position: relative; overflow: hidden; } .dash-box { - border: 1px solid #383737; - background-color: #3d3d3d; - color: #999999; - height:100%; + border: 1px solid #383737; + background-color: #3d3d3d; + color: #999999; + height:100%; } .dash-box-head { - background-color: #383737; - color: #999999; - width:100%; - display: inline-block; - padding:10px; - font-weight: bold; + background-color: #383737; + color: #999999; + width:100%; + display: inline-block; + padding:10px; + font-weight: bold; } .dash-box-body { - padding:10px; + padding:10px; } .row-margin { - margin-bottom:20px; + margin-bottom:20px; } .col-margin { - margin-bottom:20px; + margin-bottom:20px; } /* Bootstrap overwrites -------------------------------------------------- */ html, body { - font-size: 14px; + font-size: 14px; } @@ -86,7 +86,7 @@ body { /* Generics -------------------------------------------------- */ .font-small { - font-size: 12px; + font-size: 12px; } @@ -95,52 +95,52 @@ body { -------------------------------------------------- */ .circles{ - margin:0px auto; + margin:0px auto; } .circles > div { - overflow:hidden; - float:left; - width:auto; - height:auto; - position: relative; - border-radius:50%; - -moz-border-radius:50%; - -webkit-border-radius:50%; - -khtml-border-radius: 50%; - background:#eee; + overflow:hidden; + float:left; + width:auto; + height:auto; + position: relative; + border-radius:50%; + -moz-border-radius:50%; + -webkit-border-radius:50%; + -khtml-border-radius: 50%; + background:#eee; } .circles > div > div { - position: absolute; - top: 0; - right: 0; - bottom: 0; - left: 0; + position: absolute; + top: 0; + right: 0; + bottom: 0; + left: 0; } .circles > div > div > div { - display: table; - width: 100%; - height: 100%; + display: table; + width: 100%; + height: 100%; } .circles > div > div > div > div { - display: table-cell; - text-align: center; - vertical-align: middle; + display: table-cell; + text-align: center; + vertical-align: middle; } @media (max-width: 320px) { - .circles > div {padding: 50%;} + .circles > div {padding: 50%;} } @media (min-width: 321px) and (max-width: 800px) { - .circles > div {padding: 50%;} + .circles > div {padding: 50%;} } @media (min-width: 801px) { - .circles > div {padding: 48%;} + .circles > div {padding: 48%;} } @@ -151,17 +151,17 @@ a.navbar-brand { background-color: transparent !important; } a.nav-link { - background-color: transparent !important; + background-color: transparent !important; } img.d-inline-block { - float: none; + float: none; } nav.navbar { - font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif; - font-size: 1rem; - line-height: 1.5; - color: #373a3c; - background-color: #fff; + font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif; + font-size: 1rem; + line-height: 1.5; + color: #373a3c; + background-color: #fff; } @@ -170,26 +170,62 @@ nav.navbar { /* mail.php; -------------------------------------------------- */ tr.subject { - cursor: pointer; + cursor: pointer; } td.break-word { - word-break: break-word; + word-break: break-word; } /* adminer.php; -------------------------------------------------- */ #menu { - position: absolute; - margin: 76px 0 0 !important; + position: absolute; + margin: 76px 0 0 !important; } #breadcrumb { - top: 60px !important; + top: 60px !important; } #lang { - top: 53px !important; + top: 53px !important; } .pages { z-index:1000; } + + + +/* login.php; +-------------------------------------------------- */ +/* Extra markup and styles for table-esque vertical and horizontal centering */ +.site-wrapper { + display: table; + width: 100%; + height: 100%; /* For at least Firefox */ + min-height: 100%; + -webkit-box-shadow: inset 0 0 5rem rgba(0,0,0,.5); + box-shadow: inset 0 0 5rem rgba(0,0,0,.5); +} +.site-wrapper-inner { + display: table-cell; + vertical-align: top; +} +.cover-container { + margin-right: auto; + margin-left: auto; +} +/* Affix and center */ +@media (min-width: 40em) { + /* Start the vertical centering */ + .site-wrapper-inner { + vertical-align: middle; + } +} +@media (min-width: 62em) { + .masthead, + .mastfoot, + .cover-container { + width: 32rem; + } +} diff --git a/.devilbox/www/htdocs/credits.php b/.devilbox/www/htdocs/credits.php index a320a62d..e1d9e695 100644 --- a/.devilbox/www/htdocs/credits.php +++ b/.devilbox/www/htdocs/credits.php @@ -1,4 +1,5 @@ +authPage(); ?> diff --git a/.devilbox/www/htdocs/db_memcd.php b/.devilbox/www/htdocs/db_memcd.php index 3fbb2bdc..028049a0 100644 --- a/.devilbox/www/htdocs/db_memcd.php +++ b/.devilbox/www/htdocs/db_memcd.php @@ -1,4 +1,5 @@ +authPage(); ?> diff --git a/.devilbox/www/htdocs/db_mongo.php b/.devilbox/www/htdocs/db_mongo.php index 1fa6517b..1f866f9a 100644 --- a/.devilbox/www/htdocs/db_mongo.php +++ b/.devilbox/www/htdocs/db_mongo.php @@ -1,4 +1,5 @@ +authPage(); ?> diff --git a/.devilbox/www/htdocs/db_mysql.php b/.devilbox/www/htdocs/db_mysql.php index 2ef79224..957d8503 100644 --- a/.devilbox/www/htdocs/db_mysql.php +++ b/.devilbox/www/htdocs/db_mysql.php @@ -1,4 +1,5 @@ +authPage(); ?> +authPage(); ?> +authPage(); ?> diff --git a/.devilbox/www/htdocs/debug.php b/.devilbox/www/htdocs/debug.php index 878d8f78..678781fb 100644 --- a/.devilbox/www/htdocs/debug.php +++ b/.devilbox/www/htdocs/debug.php @@ -1,4 +1,5 @@ +authPage(); ?> diff --git a/.devilbox/www/htdocs/index.php b/.devilbox/www/htdocs/index.php index 9c9116b3..b9f0c212 100644 --- a/.devilbox/www/htdocs/index.php +++ b/.devilbox/www/htdocs/index.php @@ -1,4 +1,5 @@ +authPage(); ?> +authPage(); ?> diff --git a/.devilbox/www/htdocs/info_memcd.php b/.devilbox/www/htdocs/info_memcd.php index 9399cf87..d23f8cf2 100644 --- a/.devilbox/www/htdocs/info_memcd.php +++ b/.devilbox/www/htdocs/info_memcd.php @@ -1,4 +1,5 @@ +authPage(); ?> diff --git a/.devilbox/www/htdocs/info_mongo.php b/.devilbox/www/htdocs/info_mongo.php index 942475f4..ef44be74 100644 --- a/.devilbox/www/htdocs/info_mongo.php +++ b/.devilbox/www/htdocs/info_mongo.php @@ -1,4 +1,5 @@ +authPage(); ?> diff --git a/.devilbox/www/htdocs/info_mysql.php b/.devilbox/www/htdocs/info_mysql.php index 6b208999..196c0c38 100644 --- a/.devilbox/www/htdocs/info_mysql.php +++ b/.devilbox/www/htdocs/info_mysql.php @@ -1,4 +1,5 @@ +authPage(); ?> diff --git a/.devilbox/www/htdocs/info_pgsql.php b/.devilbox/www/htdocs/info_pgsql.php index 3d930857..64ac02f9 100644 --- a/.devilbox/www/htdocs/info_pgsql.php +++ b/.devilbox/www/htdocs/info_pgsql.php @@ -1,4 +1,5 @@ +authPage(); ?> diff --git a/.devilbox/www/htdocs/info_php.php b/.devilbox/www/htdocs/info_php.php index a4c949af..17b98ae7 100644 --- a/.devilbox/www/htdocs/info_php.php +++ b/.devilbox/www/htdocs/info_php.php @@ -1,4 +1,5 @@ +authPage(); ?> diff --git a/.devilbox/www/htdocs/info_redis.php b/.devilbox/www/htdocs/info_redis.php index a20c5518..4ab906ab 100644 --- a/.devilbox/www/htdocs/info_redis.php +++ b/.devilbox/www/htdocs/info_redis.php @@ -1,4 +1,5 @@ +authPage(); ?> diff --git a/.devilbox/www/htdocs/login.php b/.devilbox/www/htdocs/login.php new file mode 100644 index 00000000..4af6f26f --- /dev/null +++ b/.devilbox/www/htdocs/login.php @@ -0,0 +1,66 @@ + +isloggedIn()) { + loadClass('Helper')->redirect('/'); +} + +// Validate $_POST login + +$login_error = ''; + +if (isset($_POST['username']) && isset($_POST['password'])) { + + // Auth successful + if (loadClass('Helper')->login($_POST['username'], $_POST['password'])) { + loadClass('Helper')->redirect('/'); + } + $login_error = 'Wrong username or password'; + +} + +?> + + + + getHead(true); ?> + + + + +
+
+
+
+
+ + +

Devilbox Login

+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+ +
+
+
+
+
+ + diff --git a/.devilbox/www/htdocs/logout.php b/.devilbox/www/htdocs/logout.php new file mode 100644 index 00000000..ac9302bc --- /dev/null +++ b/.devilbox/www/htdocs/logout.php @@ -0,0 +1,11 @@ + +logout(); + loadClass('Helper')->redirect('/login.php'); + } +} + +loadClass('Helper')->redirect('/'); diff --git a/.devilbox/www/htdocs/mail.php b/.devilbox/www/htdocs/mail.php index d9a3174f..0a726399 100644 --- a/.devilbox/www/htdocs/mail.php +++ b/.devilbox/www/htdocs/mail.php @@ -1,5 +1,6 @@ + +authPage(); ?> +authPage(); ?> +authPage(); ?> diff --git a/.devilbox/www/include/lib/Helper.php b/.devilbox/www/include/lib/Helper.php index a80d9f41..8b91e129 100644 --- a/.devilbox/www/include/lib/Helper.php +++ b/.devilbox/www/include/lib/Helper.php @@ -172,4 +172,64 @@ class Helper exec($cmd, $output, $exit_code); return implode ("\n", $output); } + + + public function redirect($url) + { + header('Location: '.$url); + exit; + } + + + /********************************************************************************* + * + * Login Helper Functions + * + *********************************************************************************/ + + public function login($username, $password) + { + $dvl_password = loadClass('Helper')->getEnv('DEVILBOX_UI_PASSWORD'); + + if ($username == 'devilbox' && $password == $dvl_password) { + $_SESSION['auth'] = 1; + return true; + } + return false; + } + public function logout() + { + if (isset($_SESSION['auth'])) { + $_SESSION['auth'] = 0; + unset($_SESSION['auth']); + } + } + public function isLoginProtected() + { + // No password protection enabled + if (loadClass('Helper')->getEnv('DEVILBOX_UI_PROTECT') != 1) { + return false; + } + return true; + + } + public function isloggedIn() + { + // No password protection enabled + if (!$this->isLoginProtected()) { + return true; + } + + // Alredy logged in + if (isset($_SESSION['auth']) && $_SESSION['auth'] == 1) { + return true; + } + return false; + } + public function authPage() + { + if (!$this->isloggedIn()) { + $this->redirect('/login.php'); + } + } } diff --git a/.devilbox/www/include/lib/Html.php b/.devilbox/www/include/lib/Html.php index ec18ff9d..ca771808 100644 --- a/.devilbox/www/include/lib/Html.php +++ b/.devilbox/www/include/lib/Html.php @@ -183,6 +183,12 @@ HTML; public function getNavbar() { $menu = $this->_buildMenu(); + $logout = ''; + if (loadClass('Helper')->isLoginProtected()) { + $logout = ''; + } $html = << @@ -198,8 +204,11 @@ HTML; {$menu} + + {$logout} +
HTML; diff --git a/docker-compose.yml b/docker-compose.yml index 79a2105e..da32693b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -97,6 +97,12 @@ services: - TLD_SUFFIX=${TLD_SUFFIX:-loc} - DNS_CHECK_TIMEOUT=${DNS_CHECK_TIMEOUT:-1} + ## + ## Password protect the devilbox Intranet + ## + - DEVILBOX_UI_PROTECT=${DEVILBOX_UI_PROTECT:-0} + - DEVILBOX_UI_PASSWORD=${DEVILBOX_UI_PASSWORD} + ## ## PHP Xdebug ## diff --git a/env-example b/env-example index e41f2542..f5d89855 100644 --- a/env-example +++ b/env-example @@ -102,6 +102,36 @@ NEW_GID=1000 TIMEZONE=Europe/Berlin +### +### Devilbox UI Password protection enable/disable (1/0) +### +### Set DEVILBOX_UI_PROTECT to 1 in order to password protect the +### intranet. +### +### Example: +### DEVILBOX_UI_PROTECT=1 +### DEVILBOX_UI_PROTECT=0 +### +DEVILBOX_UI_PROTECT=0 + + +### +### Devilbox UI Password +### +### When DEVILBOX_UI_PROTECT=1, use the following password +### to log in. The password can always be changed. +### When changing the password, make sure to restart your +### PHP container. +### +### Example: +### DEVILBOX_UI_PASSWORD=my-very-secure-password +### DEVILBOX_UI_PASSWORD=Some pass with spaces +### +### The default username is 'devilbox' +### +DEVILBOX_UI_PASSWORD=password + + ################################################################################ ###