Imported Upstream version 0.6.24+dfsg1

This commit is contained in:
Mario Fetka
2017-05-20 15:26:21 +02:00
commit 32a360eca6
705 changed files with 87250 additions and 0 deletions

View File

@@ -0,0 +1,154 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Retrieves the PNP config files
*/
class Auth_Model extends System_Model {
public $SOCKET = NULL;
public $socketPath = NULL;
public $socketDOMAIN = NULL;
public $socketTYPE = NULL;
public $socketHOST = NULL;
public $socketPORT = 0;
public $socketPROTO = NULL;
public $ERR_TXT = "";
public $AUTH_ENABLED = FALSE;
public $REMOTE_USER = NULL;
public function __construct() {
$this->config = new Config_Model;
$this->config->read_config();
if($this->config->conf['auth_enabled'] == 1){
$this->AUTH_ENABLED = TRUE;
$this->socketPath = $this->config->conf['livestatus_socket'];
}
// Try to get the login of the user
if(isset($_SERVER['REMOTE_USER'])){
$this->REMOTE_USER = $_SERVER['REMOTE_USER'];
}
if($this->REMOTE_USER === NULL && $this->config->conf['auth_multisite_enabled'] == 1) {
$MSAUTH = new Auth_Multisite_Model($this->config->conf['auth_multisite_htpasswd'],
$this->config->conf['auth_multisite_serials'],
$this->config->conf['auth_multisite_secret'],
$this->config->conf['auth_multisite_login_url']);
$this->REMOTE_USER = $MSAUTH->check();
if($this->REMOTE_USER !== null)
return;
}
if($this->AUTH_ENABLED === TRUE && $this->REMOTE_USER === NULL){
throw new Kohana_exception("error.remote_user_missing");
}
}
public function __destruct() {
if($this->SOCKET !== NULL) {
socket_close($this->SOCKET);
$this->SOCKET = NULL;
}
}
public function connect(){
$this->getSocketDetails($this->socketPath);
$this->SOCKET = socket_create($this->socketDOMAIN, $this->socketTYPE, $this->socketPROTO);
if($this->SOCKET === FALSE) {
throw new Kohana_exception("error.livestatus_socket_error", socket_strerror(socket_last_error($this->SOCKET)), $this->socketPath);
}
if($this->socketDOMAIN === AF_UNIX){
$result = @socket_connect($this->SOCKET, $this->socketPATH);
}else{
$result = @socket_connect($this->SOCKET, $this->socketHOST, $this->socketPORT);
}
if(!$result) {
throw new Kohana_exception("error.livestatus_socket_error", socket_strerror(socket_last_error($this->SOCKET)), $this->socketPath);
}
}
private function queryLivestatus($query) {
if($this->SOCKET === NULL) {
$this->connect();
}
@socket_write($this->SOCKET, $query."\nOutputFormat: json\n\n");
// Read 16 bytes to get the status code and body size
$read = @socket_read($this->SOCKET,2048);
if(!$read) {
throw new Kohana_exception("error.livestatus_socket_error", socket_strerror(socket_last_error($this->SOCKET)));
}
# print Kohana::debug("read ". $read);
// Catch problem while reading
if($read === false) {
throw new Kohana_exception("error.livestatus_socket_error", socket_strerror(socket_last_error($this->SOCKET)));
}
// Decode the json response
$obj = json_decode(utf8_encode($read));
socket_close($this->SOCKET);
$this->SOCKET = NULL;
return $obj;
}
public function is_authorized($host = FALSE, $service = NULL){
if($this->AUTH_ENABLED === FALSE){
return TRUE;
}
if($host == "pnp-internal"){
return TRUE;
}
if($service === NULL || $service == "_HOST_" || $service == "Host Perfdata"){
$users = explode(",", $this->config->conf['allowed_for_all_hosts']);
if (in_array($this->REMOTE_USER, $users)) {
return TRUE;
}
$query = "GET hosts\nColumns: name\nFilter: name = $host\nAuthUser: ".$this->REMOTE_USER;
$result = $this->queryLivestatus($query);
}else{
$users = explode(",", $this->config->conf['allowed_for_all_services']);
if (in_array($this->REMOTE_USER, $users)) {
return TRUE;
}
$query = "GET services\nColumns: host_name description\nFilter: host_name = $host\nFilter: description = $service\nAuthUser: ".$this->REMOTE_USER;
$result = $this->queryLivestatus($query);
}
if(sizeof($result) > 0){
return TRUE;
}else{
return FALSE;
}
}
public function getSocketDetails($string=FALSE){
if(preg_match('/^unix:(.*)$/',$string,$match) ){
$this->socketDOMAIN = AF_UNIX;
$this->socketTYPE = SOCK_STREAM;
$this->socketPATH = $match[1];
$this->socketPROTO = 0;
return;
}
if(preg_match('/^tcp:([a-zA-Z0-9-\.]+):([0-9]+)$/',$string,$match) ){
$this->socketDOMAIN = AF_INET;
$this->socketTYPE = SOCK_STREAM;
$this->socketHOST = $match[1];
$this->socketPORT = $match[2];
$this->socketPROTO = SOL_TCP;
return;
}
# Fallback
if(preg_match('/^\/.*$/',$string,$match) ){
$this->socketDOMAIN = AF_UNIX;
$this->socketTYPE = SOCK_STREAM;
$this->socketPATH = $string;
$this->socketPROTO = 0;
return;
}
return FALSE;
}
}

View File

@@ -0,0 +1,111 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Auth_Multisite_Model {
private $htpasswdPath;
private $serialsPath;
private $secretPath;
private $authFile;
public function __construct($htpasswdPath, $serialsPath, $secretPath, $loginUrl) {
$this->htpasswdPath = $htpasswdPath;
$this->serialsPath = $serialsPath;
$this->secretPath = $secretPath;
$this->loginUrl = $loginUrl;
// When the auth.serial file exists, use this instead of the htpasswd
// for validating the cookie. The structure of the file is equal, so
// the same code can be used.
if(file_exists($this->serialsPath)) {
$this->authFile = 'serial';
} elseif(file_exists($this->htpasswdPath)) {
$this->authFile = 'htpasswd';
} else {
throw new Kohana_exception("error.auth_multisite_missing_htpasswd");
}
if(!file_exists($this->secretPath)) {
$this->redirectToLogin();
}
}
private function loadAuthFile($path) {
$creds = array();
foreach(file($path) AS $line) {
if(strpos($line, ':') !== false) {
list($username, $secret) = explode(':', $line, 2);
$creds[$username] = rtrim($secret);
}
}
return $creds;
}
private function loadSecret() {
return trim(file_get_contents($this->secretPath));
}
private function generateHash($username, $now, $user_secret) {
$secret = $this->loadSecret();
return md5($username . $now . $user_secret . $secret);
}
private function checkAuthCookie($cookieName) {
if(!isset($_COOKIE[$cookieName]) || $_COOKIE[$cookieName] == '') {
throw new Exception();
}
list($username, $issueTime, $cookieHash) = explode(':', $_COOKIE[$cookieName], 3);
if($this->authFile == 'htpasswd')
$users = $this->loadAuthFile($this->htpasswdPath);
else
$users = $this->loadAuthFile($this->serialsPath);
if(!isset($users[$username])) {
throw new Exception();
}
$user_secret = $users[$username];
// Validate the hash
if($cookieHash != $this->generateHash($username, $issueTime, $user_secret)) {
throw new Exception();
}
// FIXME: Maybe renew the cookie here too
return $username;
}
private function checkAuth() {
// Loop all cookies trying to fetch a valid authentication
// cookie for this installation
foreach(array_keys($_COOKIE) AS $cookieName) {
if(substr($cookieName, 0, 5) != 'auth_') {
continue;
}
try {
$name = $this->checkAuthCookie($cookieName);
return $name;
} catch(Exception $e) {}
}
return '';
}
private function redirectToLogin() {
header('Location:' . $this->loginUrl . '?_origtarget=' . $_SERVER['REQUEST_URI']);
}
public function check() {
$username = $this->checkAuth();
if($username === '') {
$this->redirectToLogin();
exit(0);
}
return $username;
}
}
?>

View File

@@ -0,0 +1,88 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Retrieves the PNP config files
*/
class Config_Model extends System_Model
{
public $conf = array();
public $views = array();
public $scheme = array();
public function read_config(){
if(getenv('PNP_CONFIG_FILE') != ""){
$config = getenv('PNP_CONFIG_FILE');
}elseif(OMD){
$config = OMD_SITE_ROOT.'/etc/pnp4nagios/config';
}else{
$config = Kohana::config('core.pnp_etc_path')."/config";
}
# Default Values
$conf['doc_language'] = Kohana::config('core.doc_language');
$conf['graph_width'] = Kohana::config('core.graph_width');
$conf['graph_height'] = Kohana::config('core.graph_height');
$conf['zgraph_width'] = Kohana::config('core.zgraph_width');
$conf['zgraph_height'] = Kohana::config('core.zgraph_height');
$conf['pdf_width'] = Kohana::config('core.pdf_width');
$conf['pdf_height'] = Kohana::config('core.pdf_height');
$conf['right_zoom_offset'] = Kohana::config('core.right_zoom_offset');
$conf['mobile_devices'] = Kohana::config('core.mobile_devices');
$conf['pdf_page_size'] = Kohana::config('core.pdf_page_size');
$conf['pdf_margin_left'] = Kohana::config('core.pdf_margin_left');
$conf['pdf_margin_right'] = Kohana::config('core.pdf_margin_right');
$conf['pdf_margin_top'] = Kohana::config('core.pdf_margin_top');
$conf['auth_multisite_enabled'] = Kohana::config('core.auth_multisite_enabled');
$conf['auth_multisite_serials'] = Kohana::config('core.auth_multisite_serials');
$conf['auth_multisite_htpasswd'] = Kohana::config('core.auth_multisite_htpasswd');
$conf['auth_multisite_secret'] = Kohana::config('core.auth_multisite_secret');
$conf['auth_multisite_login_url'] = Kohana::config('core.auth_multisite_login_url');
$scheme['Reds'] = array ('#FEE0D2','#FCBBA1','#FC9272','#FB6A4A','#EF3B2C','#CB181D','#A50F15','#67000D');
$views = Kohana::config('core.views');
if (is_readable($config . ".php")) {
include ($config . ".php");
}else {
throw new Kohana_Exception('error.config-not-found', $config.'.php');
}
// Load optional config files
// a) the _local.php config
// b) all .php files which do not start with a "." in config.d/
$custom_configs = array($config . "_local.php");
if (file_exists($config . ".d") && is_dir($config . ".d")) {
$dh = opendir($config . ".d");
while (($file = readdir($dh)) !== false) {
if ($file[0] != '.' && substr($file, -4) == '.php') {
$custom_configs[] = $config . ".d/" .$file;
}
}
closedir($dh);
}
foreach($custom_configs AS $config_file) {
if (is_readable($config_file)) {
$array_a = $views;
$views = array();
include ($config_file);
$array_b = $views;
if(sizeof($views) == 0 ){
$views = $array_a;
}
}
}
// Use graph_height & graph_width from URL if present
// Hint: In Kohana 3 Input class is removed
$input = Input::instance();
if($input->get('h') != "" ) $conf['graph_height'] = intval($input->get('h'));
if($input->get('w') != "" ) $conf['graph_width'] = intval($input->get('w'));
if($input->get('graph_height') != "" ) $conf['graph_height'] = intval($input->get('graph_height'));
if($input->get('graph_width') != "" ) $conf['graph_width'] = intval($input->get('graph_width'));
$this->conf = $conf;
$this->views = $views;
$this->scheme = $scheme;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,243 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Retrieves and manipulates current status of hosts (and services?)
*/
class Rrdtool_Model extends System_Model
{
private $RRD_CMD = FALSE;
/*
*
*
*/
public function __construct(){
$this->config = new Config_Model();
$this->config->read_config();
#print Kohana::debug($this->config->views);
}
private function rrdtool_execute() {
$descriptorspec = array (
0 => array ("pipe","r"), // stdin is a pipe that the child will read from
1 => array ("pipe","w"), // stdout is a pipe that the child will write to
2 => array ("pipe","w") // stderr is a pipe that the child will write to
);
if(!isset($this->config->conf['rrdtool']) )
return FALSE;
if ( !is_executable($this->config->conf['rrdtool']) ) {
$data = "ERROR: ".$this->config->conf['rrdtool']." is not executable by PHP\n\n";
return $data;
}
$rrdtool = $this->config->conf['rrdtool'] . " - ";
$command = $this->RRD_CMD;
$process = proc_open($rrdtool, $descriptorspec, $pipes);
$debug = Array();
$data = "";
if (is_resource($process)) {
fwrite($pipes[0], $command);
fclose($pipes[0]);
stream_set_timeout($pipes[1],1);
$data = stream_get_contents($pipes[1]);
stream_set_timeout($pipes[2],1);
$stderr = stream_get_contents($pipes[2]);
$stdout_meta = stream_get_meta_data($pipes[1]);
if($stdout_meta['timed_out'] == 1){
$data = "ERROR: Timeout while reading rrdtool data.\n\n";
}
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
// Catch STDERR
if($stderr && strlen($stderr) >= 0 ){
$data = "ERROR: STDERR => ".$stderr."\n\n";
return $data;
}
// Catch STDOUT < 50 Characters
if($data && strlen($data) < 50 ){
$data = "ERROR: STDOUT => ".$data."\n\n";
return $data;
}
}else{
$data = "ERROR: proc_open(".$rrdtool." ... failed";
}
return $data;
}
public function doImage($RRD_CMD, $out='STDOUT') {
$conf = $this->config->conf;
# construct $command to rrdtool
if(isset($conf['RRD_DAEMON_OPTS']) && $conf['RRD_DAEMON_OPTS'] != '' ){
$command = " graph --daemon=" . $conf['RRD_DAEMON_OPTS'] . " - ";
}else{
$command = " graph - ";
}
$width = 0;
$height = 0;
if ($out == 'PDF'){
if($conf['pdf_graph_opt']){
$command .= $conf['pdf_graph_opt'];
}
if (isset($conf['pdf_width']) && is_numeric($conf['pdf_width'])){
$width = abs($conf['pdf_width']);
}
if (isset($conf['pdf_height']) && is_numeric($conf['pdf_height'])){
$height = abs($conf['pdf_height']);
}
}else{
if($conf['graph_opt']){
$command .= $conf['graph_opt'];
}
if(is_numeric($conf['graph_width'])){
$width = abs($conf['graph_width']);
}
if(is_numeric($conf['graph_height'])){
$height = abs($conf['graph_height']);
}
}
if ($width > 0){
$command .= " --width=$width";
}
if ($height > 0){
$command .= " --height=$height";
}
if ($height < 81 ){
$command .= " --only-graph ";
}
$command .= $RRD_CMD;
# Force empty vertical label
if( ! preg_match_all('/(-l|--vertical-label)/i',$command,$match)){
$command .= " --vertical-label=' ' ";
}
$this->RRD_CMD = $command;
$data = $this->rrdtool_execute();
if($data){
return $data;
}else{
return FALSE;
}
}
/*
*
*/
public function doXport($RRD_CMD){
$conf = $this->config->conf;
if(isset($conf['RRD_DAEMON_OPTS']) && $conf['RRD_DAEMON_OPTS'] != '' ){
$command = " xport --daemon=" . $conf['RRD_DAEMON_OPTS'];
}else{
$command = " xport ";
}
$command .= $RRD_CMD;
$this->RRD_CMD = $command;
$data = $this->rrdtool_execute();
$data = preg_replace('/OK.*/','',$data);
if($data){
return $data;
}else{
return FALSE;
}
}
public function streamImage($data = FALSE){
if ( $data === FALSE ){
header("Content-type: image/png");
echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A
/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9kCCAoDKSKZ0rEAAAAZdEVYdENv
bW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAADUlEQVQI12NgYGBgAAAABQABXvMqOgAAAABJ
RU5ErkJggg==');
return;
}
if (preg_match('/^ERROR/', $data)) {
if(preg_match('/NOT_AUTHORIZED/', $data)){
// TODO: i18n
$data .= "\n\nYou are not authorized to view this Image";
// Set font size
$font_size = 3;
}else{
$data .= $this->format_rrd_debug( $this->config->conf['rrdtool'] . $this->RRD_CMD) ;
// Set font size
$font_size = 1.5;
}
$ts=explode("\n",$data);
$width=0;
foreach ($ts as $k=>$string) {
$width=max($width,strlen($string));
}
$width = imagefontwidth($font_size)*$width;
if($width <= $this->config->conf['graph_width']+100){
$width = $this->config->conf['graph_width']+100;
}
$height = imagefontheight($font_size)*count($ts);
if($height <= $this->config->conf['graph_height']+60){
$height = $this->config->conf['graph_height']+60;
}
$el=imagefontheight($font_size);
$em=imagefontwidth($font_size);
// Create the image pallette
$img = imagecreatetruecolor($width,$height);
// Dark red background
$bg = imagecolorallocate($img, 0xAA, 0x00, 0x00);
imagefilledrectangle($img, 0, 0,$width ,$height , $bg);
// White font color
$color = imagecolorallocate($img, 255, 255, 255);
foreach ($ts as $k=>$string) {
// Length of the string
$len = strlen($string);
// Y-coordinate of character, X changes, Y is static
$ypos_offset = 5;
$xpos_offset = 5;
// Loop through the string
for($i=0;$i<$len;$i++){
// Position of the character horizontally
$xpos = $i * $em + $ypos_offset;
$ypos = $k * $el + $xpos_offset;
// Draw character
imagechar($img, $font_size, $xpos, $ypos, $string, $color);
// Remove character from string
$string = substr($string, 1);
}
}
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
}else{
header("Content-type: image/png");
echo $data;
}
}
public function saveImage($data = FALSE){
$img = array();
$img['file'] = tempnam($this->config->conf['temp'],"PNP");
if(!$fh = fopen($img['file'],'w') ){
throw new Kohana_Exception('save-rrd-image', $img['file']);
}
fwrite($fh, $data);
fclose($fh);
if (function_exists('imagecreatefrompng')) {
$image = imagecreatefrompng($img['file']);
imagepng($image, $img['file']);
list ($img['width'], $img['height'], $img['type'], $img['attr']) = getimagesize($img['file']);
}else{
throw new Kohana_Exception('error.gd-missing');
}
return $img;
}
private function format_rrd_debug($data) {
$data = preg_replace('/(HRULE|VDEF|DEF|CDEF|GPRINT|LINE|AREA|COMMENT)/',"\n\${1}", $data);
return $data;
}
}

View File

@@ -0,0 +1,14 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Retrieves the PNP config files
*/
class System_Model extends Model {
public $ERROR = NULL;
public function __construct() {
}
}