2013-08-28 22:47:04 +02:00
< ? php
2014-03-05 22:04:36 +01:00
2013-08-28 22:47:04 +02:00
/**
* File : root_dhcp . php .
* Author : Ulrich Block
* Date : 29.04 . 12
* Time : 11 : 56
* Contact : < ulrich . block @ easy - wi . com >
*
* This file is part of Easy - WI .
*
* Easy - WI is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* Easy - WI is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with Easy - WI . If not , see < http :// www . gnu . org / licenses />.
*
* Diese Datei ist Teil von Easy - WI .
*
* Easy - WI ist Freie Software : Sie koennen es unter den Bedingungen
* der GNU General Public License , wie von der Free Software Foundation ,
* Version 3 der Lizenz oder ( nach Ihrer Wahl ) jeder spaeteren
* veroeffentlichten Version , weiterverbreiten und / oder modifizieren .
*
* Easy - WI wird in der Hoffnung , dass es nuetzlich sein wird , aber
* OHNE JEDE GEWAEHELEISTUNG , bereitgestellt ; sogar ohne die implizite
* Gewaehrleistung der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK .
* Siehe die GNU General Public License fuer weitere Details .
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben . Wenn nicht , siehe < http :// www . gnu . org / licenses />.
*/
2013-10-27 00:16:43 +02:00
if ( ! isset ( $admin_id ) or $main != 1 or $reseller_id != 0 or ! $pa [ 'vserversettings' ]) {
2013-08-28 22:47:04 +02:00
header ( 'Location: admin.php' );
die ;
}
2013-09-27 08:22:09 +02:00
include ( EASYWIDIR . '/stuff/keyphrasefile.php' );
2014-02-23 13:13:48 +01:00
include ( EASYWIDIR . '/stuff/methods/functions_ssh_exec.php' );
2013-09-27 08:22:09 +02:00
2014-02-02 11:46:12 +01:00
$sprache = getlanguagefile ( 'reseller' , $user_language , $reseller_id );
2013-09-29 15:29:58 +02:00
$loguserid = $admin_id ;
$logusername = getusername ( $admin_id );
$logusertype = 'admin' ;
2013-10-13 13:11:38 +02:00
if ( $reseller_id == 0 ) {
2013-09-29 15:29:58 +02:00
$logreseller = 0 ;
$logsubuser = 0 ;
2013-08-28 22:47:04 +02:00
} else {
$logsubuser = ( isset ( $_SESSION [ 'oldid' ])) ? $_SESSION [ 'oldid' ] : 0 ;
2013-09-29 15:29:58 +02:00
$logreseller = 0 ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
// Define the ID variable which will be used at the form and SQLs
$id = $ui -> id ( 'id' , 10 , 'get' );
$publickey = $ui -> w ( 'publickey' , 1 , 'post' );
$keyname = $ui -> startparameter ( 'keyname' , 'post' );
$active = $ui -> active ( 'active' , 'post' );
$ip = $ui -> ip ( 'ip' , 'post' );
$port = $ui -> port ( 'port' , 'post' );
$user = $ui -> username ( 'user' , 255 , 'post' );
$pass = $ui -> password ( 'pass' , 255 , 'post' );
$startCmd = $ui -> startparameter ( 'startCmd' , 'post' );
$dhcpFile = $ui -> startparameter ( 'dhcpFile' , 'post' );
$description = $ui -> escaped ( 'description' , 'post' );
// CSFR protection with hidden tokens. If token(true) returns false, we likely have an attack
if ( $ui -> w ( 'action' , 4 , 'post' ) and ! token ( true )) {
2014-03-08 10:57:48 +01:00
unset ( $header , $text );
$errors = array ( $spracheResponse -> token );
$template_file = ( $ui -> st ( 'd' , 'get' ) == 'ad' ) ? 'admin_root_dhcp_ad.tpl' : 'admin_root_dhcp_md.tpl' ;
2014-02-02 11:46:12 +01:00
// Add and modify entries. Same validation can be used.
} else if ( $ui -> st ( 'd' , 'get' ) == 'ad' or $ui -> st ( 'd' , 'get' ) == 'md' ) {
// Error handling. Check if required attributes are set and can be validated
$errors = array ();
// At this point all variables are defined that can come from the user
// Default variables. Mostly needed for the add operation
$defaultVar = ( $ui -> id ( 'id' , 10 , 'get' )) ? $ui -> id ( 'id' , 10 , 'get' ) : 10 ;
// Add or mod is opened
if ( ! $ui -> smallletters ( 'action' , 2 , 'post' )) {
// Gather data for adding if needed and define add template
if ( $ui -> st ( 'd' , 'get' ) == 'ad' ) {
2014-03-01 21:19:39 +01:00
2014-02-02 11:46:12 +01:00
$template_file = 'admin_root_dhcp_ad.tpl' ;
// Gather data for modding in case we have an ID and define mod template
} else if ( $ui -> st ( 'd' , 'get' ) == 'md' and $id ) {
$query = $sql -> prepare ( " SELECT *,AES_DECRYPT(`port`,:aeskey) AS `decryptedport`,AES_DECRYPT(`user`,:aeskey) AS `decrypteduser`,AES_DECRYPT(`pass`,:aeskey) AS `decryptedpass` FROM `rootsDHCP` WHERE `id`=:id AND `resellerid`=:reseller_id LIMIT 1 " );
$query -> execute ( array ( ':aeskey' => $aeskey , ':id' => $id , ':reseller_id' => $reseller_id ));
2014-11-30 12:55:07 +01:00
while ( $row = $query -> fetch ( PDO :: FETCH_ASSOC )) {
2014-02-02 11:46:12 +01:00
$active = $row [ 'active' ];
$ip = $row [ 'ip' ];
$port = $row [ 'decryptedport' ];
$user = $row [ 'decrypteduser' ];
$pass = $row [ 'decryptedpass' ];
$publickey = $row [ 'publickey' ];
$keyname = $row [ 'keyname' ];
$ips = $row [ 'ips' ];
$netmask = $row [ 'netmask' ];
$startCmd = $row [ 'startCmd' ];
$dhcpFile = $row [ 'dhcpFile' ];
$description = $row [ 'description' ];
}
// Check if database entry exists and if not display 404 page
$template_file = ( $query -> rowCount () > 0 ) ? 'admin_root_dhcp_md.tpl' : 'admin_404.tpl' ;
// Show 404 if GET parameters did not add up or no ID was given with mod
} else {
$template_file = 'admin_404.tpl' ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
// Form is submitted
} else if ( $ui -> st ( 'action' , 'post' ) == 'md' or $ui -> st ( 'action' , 'post' ) == 'ad' ) {
2013-10-13 11:42:31 +02:00
if ( ! $ui -> active ( 'active' , 'post' )) {
2014-02-02 11:46:12 +01:00
$errors [ 'active' ] = $sprache -> active ;
}
if ( ! $ui -> w ( 'publickey' , 1 , 'post' )) {
$errors [ 'publickey' ] = $sprache -> keyuse ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
2013-10-13 11:42:31 +02:00
if ( ! $ui -> ip ( 'ip' , 'post' )) {
2014-02-02 11:46:12 +01:00
$errors [ 'ip' ] = $sprache -> ssh_ip ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
2013-10-13 11:42:31 +02:00
if ( ! $ui -> port ( 'port' , 'post' )) {
2014-02-02 11:46:12 +01:00
$errors [ 'port' ] = $sprache -> ssh_port ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
if ( ! $ui -> username ( 'user' , 255 , 'post' )) {
$errors [ 'user' ] = $sprache -> ssh_user ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
if ( $publickey != 'N' and ! is_file ( EASYWIDIR . '/keys/' . $keyname )) {
$errors [ 'keyname' ] = $sprache -> keyname ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
$ssh2Check = ( count ( $errors ) == 0 ) ? ssh_check ( $ip , $port , $user , $publickey , $keyname , $pass ) : true ;
if ( $ssh2Check !== true ) {
if ( $ssh2Check == 'ipport' ) {
$errors [ 'ip' ] = $sprache -> ssh_ip ;
$errors [ 'port' ] = $sprache -> ssh_port ;
2013-08-28 22:47:04 +02:00
} else {
2014-02-02 11:46:12 +01:00
$errors [ 'user' ] = $sprache -> ssh_user ;
$errors [ 'publickey' ] = $sprache -> keyuse ;
if ( $publickey == 'N' ) {
$errors [ 'pass' ] = $sprache -> ssh_pass ;
} else if ( ! $ui -> active ( 'publickey' , 'post' ) == 'B' ) {
$errors [ 'pass' ] = $sprache -> ssh_pass ;
$errors [ 'keyname' ] = $sprache -> keyname ;
} else {
$errors [ 'keyname' ] = $sprache -> keyname ;
}
}
}
// Submitted values are OK
if ( count ( $errors ) == 0 ) {
// Make the inserts or updates define the log entry and get the affected rows from insert
if ( $ui -> st ( 'action' , 'post' ) == 'ad' ) {
$query = $sql -> prepare ( " INSERT INTO `rootsDHCP` (`active`,`ip`,`port`,`user`,`pass`,`publickey`,`keyname`,`startCmd`,`dhcpFile`,`description`,`resellerid`) VALUES (:active,:ip,AES_ENCRYPT(:port,:aeskey),AES_ENCRYPT(:user,:aeskey),AES_ENCRYPT(:pass,:aeskey),:publickey,:keyname,:startCmd,:dhcpFile,:description,:reseller_id) " );
$query -> execute ( array ( ':active' => $active , ':ip' => $ip , ':port' => $port , ':aeskey' => $aeskey , ':user' => $user , ':pass' => $pass , ':publickey' => $publickey , ':keyname' => $keyname , ':startCmd' => $startCmd , ':dhcpFile' => $dhcpFile , ':description' => $description , ':reseller_id' => $reseller_id ));
$rowCount = $query -> rowCount ();
$loguseraction = '%add% DHCP ' . $ip ;
} else if ( $ui -> st ( 'action' , 'post' ) == 'md' and $id ) {
$query = $sql -> prepare ( " UPDATE `rootsDHCP` SET `active`=:active,`ip`=:ip,`port`=AES_ENCRYPT(:port,:aeskey),`user`=AES_ENCRYPT(:user,:aeskey),`pass`=AES_ENCRYPT(:pass,:aeskey),`publickey`=:publickey,`keyname`=:keyname,`startCmd`=:startCmd,`dhcpFile`=:dhcpFile,`description`=:description WHERE `id`=:id AND `resellerid`=:reseller_id " );
$query -> execute ( array ( ':active' => $active , ':ip' => $ip , ':port' => $port , ':aeskey' => $aeskey , ':user' => $user , ':pass' => $pass , ':publickey' => $publickey , ':keyname' => $keyname , ':startCmd' => $startCmd , ':dhcpFile' => $dhcpFile , ':description' => $description , ':id' => $id , ':reseller_id' => $reseller_id ));
$rowCount = $query -> rowCount ();
$loguseraction = '%mod% DHCP ' . $ip ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
// Check if a row was affected during insert or update
if ( isset ( $rowCount ) and $rowCount > 0 ) {
2013-08-28 22:47:04 +02:00
$insertlog -> execute ();
2013-09-29 15:29:58 +02:00
$template_file = $spracheResponse -> table_add ;
2014-02-02 11:46:12 +01:00
// No update or insert failed
} else {
2013-09-29 15:29:58 +02:00
$template_file = $spracheResponse -> error_table ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
// An error occurred during validation unset the redirect information and display the form again
} else {
unset ( $header , $text );
$template_file = ( $ui -> st ( 'd' , 'get' ) == 'ad' ) ? 'admin_root_dhcp_ad.tpl' : 'admin_root_dhcp_md.tpl' ;
2013-08-28 22:47:04 +02:00
}
}
2014-02-02 11:46:12 +01:00
// Remove entries in case we have an ID given with the GET request
} else if ( $ui -> st ( 'd' , 'get' ) == 'dl' and $id ) {
$query = $sql -> prepare ( " SELECT `ip`,`description` FROM `rootsDHCP` WHERE `id`=? AND `resellerid`=? LIMIT 1 " );
$query -> execute ( array ( $id , $reseller_id ));
2014-11-30 12:55:07 +01:00
while ( $row = $query -> fetch ( PDO :: FETCH_ASSOC )) {
2014-02-02 11:46:12 +01:00
$ip = $row [ 'ip' ];
$description = $row [ 'description' ];
}
// Nothing submitted yet, display the delete form
if ( ! $ui -> st ( 'action' , 'post' )) {
// Check if we could find an entry and if not display 404 page
$template_file = ( $query -> rowCount () > 0 ) ? 'admin_root_dhcp_dl.tpl' : 'admin_404.tpl' ;
// User submitted remove the entry
} else if ( $ui -> st ( 'action' , 'post' ) == 'dl' ) {
2013-09-29 15:29:58 +02:00
$query = $sql -> prepare ( " DELETE FROM `rootsDHCP` WHERE `id`=? AND `resellerid`=? LIMIT 1 " );
2014-02-02 11:46:12 +01:00
$query -> execute ( array ( $id , $reseller_id ));
// Check if a row was affected meaning an entry could be deleted. If yes add log entry and display success message
2013-11-24 12:12:07 +01:00
if ( $query -> rowCount () > 0 ) {
2014-02-02 11:46:12 +01:00
2013-09-29 15:29:58 +02:00
$template_file = $spracheResponse -> table_del ;
2014-02-02 11:46:12 +01:00
$loguseraction = '%del% DHCP ' . $ip ;
$insertlog -> execute ();
// Nothing was deleted, display an error
2013-08-28 22:47:04 +02:00
} else {
2013-09-29 15:29:58 +02:00
$template_file = $spracheResponse -> error_table ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
// GET Request did not add up. Display 404 error.
2013-08-28 22:47:04 +02:00
} else {
2013-09-29 15:29:58 +02:00
$template_file = 'admin_404.tpl' ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
// List the available entries
2013-08-28 22:47:04 +02:00
} else {
2014-02-02 11:46:12 +01:00
$table = array ();
2013-10-13 11:42:31 +02:00
$o = $ui -> st ( 'o' , 'get' );
if ( $ui -> st ( 'o' , 'get' ) == 'dd' ) {
2013-10-05 18:39:30 +02:00
$orderby = '`description` DESC' ;
2013-10-13 11:42:31 +02:00
} else if ( $ui -> st ( 'o' , 'get' ) == 'ad' ) {
2013-10-05 18:39:30 +02:00
$orderby = '`description` ASC' ;
2013-10-13 11:42:31 +02:00
} else if ( $ui -> st ( 'o' , 'get' ) == 'dp' ) {
2013-10-05 18:39:30 +02:00
$orderby = '`ip` DESC' ;
2013-10-13 11:42:31 +02:00
} else if ( $ui -> st ( 'o' , 'get' ) == 'ap' ) {
2013-10-05 18:39:30 +02:00
$orderby = '`ip` ASC' ;
2013-10-13 11:42:31 +02:00
} else if ( $ui -> st ( 'o' , 'get' ) == 'ds' ) {
2013-10-05 18:39:30 +02:00
$orderby = '`active` DESC,`notified` DESC' ;
2013-10-13 11:42:31 +02:00
} else if ( $ui -> st ( 'o' , 'get' ) == 'as' ) {
2013-10-05 18:39:30 +02:00
$orderby = '`active` ASC,`notified` ASC' ;
2013-10-13 11:42:31 +02:00
} else if ( $ui -> st ( 'o' , 'get' ) == 'di' ) {
2013-10-05 18:39:30 +02:00
$orderby = '`id` DESC' ;
2013-08-28 22:47:04 +02:00
} else {
2013-10-05 18:39:30 +02:00
$orderby = '`id` ASC' ;
$o = 'ai' ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
2013-09-29 15:29:58 +02:00
$query = $sql -> prepare ( " SELECT `active`,`id`,`ip`,`description`,`notified` FROM `rootsDHCP` WHERE `resellerid`=? ORDER BY $orderby " );
2013-08-28 22:47:04 +02:00
$query -> execute ( array ( $reseller_id ));
2014-11-30 12:55:07 +01:00
while ( $row = $query -> fetch ( PDO :: FETCH_ASSOC )) {
2014-02-02 11:46:12 +01:00
if ( $row [ 'active' ] == 'Y' and $row [ 'notified' ] > 0 ) {
2013-10-13 17:12:44 +02:00
$imgName = '16_error' ;
$imgAlt = 'Crashed' ;
2013-10-03 12:49:13 +02:00
} else if ( $row [ 'active' ] == 'Y' ) {
2013-10-13 17:12:44 +02:00
$imgName = '16_ok' ;
$imgAlt = 'Active' ;
2013-08-28 22:47:04 +02:00
} else {
2013-10-13 17:12:44 +02:00
$imgName = '16_bad' ;
$imgAlt = 'Inactive' ;
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
2013-10-10 08:06:37 +02:00
$table [] = array ( 'id' => $row [ 'id' ], 'active' => $row [ 'active' ], 'ip' => $row [ 'ip' ], 'description' => $row [ 'description' ], 'img' => $imgName , 'alt' => $imgAlt );
2014-02-02 11:46:12 +01:00
2013-08-28 22:47:04 +02:00
}
2014-02-02 11:46:12 +01:00
2013-09-29 15:29:58 +02:00
$template_file = 'admin_root_dhcp_list.tpl' ;
2014-02-02 11:46:12 +01:00
2013-08-28 22:47:04 +02:00
}