Added lib/logger.php (copied from tftpserver.php, so that it can be reused for that).

Signed-off-by: Diederik de Groot <ddegroot@talon.nl>
This commit is contained in:
Diederik de Groot
2020-02-16 09:46:55 +01:00
parent d039261022
commit 36c5b036f7
4 changed files with 146 additions and 12 deletions

View File

@@ -2,7 +2,11 @@
debug = on ; The output in the browser window for more information
cache_filename = "/tmp/provision_sccp_resolver.cache"
default_language = English_United_States
;log = provision.log
;log_type = SYSLOG|STDERR|STDOUT|NULL|FILE;
;log_level = LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG
;example log_type = FILE ; log_filename = provision.log
log_type = STDOUT
log_level = LOG_INFO
[subdirs]
tftproot = tftpboot

View File

@@ -1,9 +1,13 @@
<?php
include_once("logger.php");
$base_path = !empty($_SERVER['DOCUMENT_ROOT']) ? realpath($_SERVER['DOCUMENT_ROOT'] . "/../"): realpath(getcwd()."/../");
$base_config = Array(
'main' => Array(
'debug' => 1,
'default_language' => 'English_United_States',
'log_type' => "NULL",
'log_level' => LOG_EMERG
),
'subdirs' => Array(
'tftproot' => 'tftpboot',
@@ -51,6 +55,19 @@ foreach ($tree_base as $key => $value) {
$config['main']['base_path'] = $base_path;
$config['main']['tftproot'] = (!empty($config['main']['tftproot'])) ? $base_path . "tftpboot" : '/tftpboot';
switch($config['main']['log_type']) {
case 'SYSLOG':
$config['main']['logger']=new Logger_Syslog($config['main']['log_level']);
case 'FILE':
$config['main']['logger']=new Logger_Filename($config['main']['log_level'], $config['main']['log_file']);
case 'STDOUT':
$config['main']['logger']=new Logger_Stdout($config['main']['log_level']);
case 'STDERR':
$config['main']['logger']=new Logger_Stderr($config['main']['log_level']);
default:
$config['main']['logger']=new Logger_Null($config['main']['log_level']);
}
# Fixup debug
$print_debug = (!empty($config['main']['debug'])) ? $config['main']['debug'] : 'off';
$print_debug = ($print_debug == 1) ? 'on' : $print_debug;

94
lib/logger.php Normal file
View File

@@ -0,0 +1,94 @@
<?php
/* Note about the Logger class:
* The "priority" and "minimum should be one of the constants used for syslog.
* See: http://php.net/manual/en/function.syslog.php
* They are: LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE,
* LOG_INFO, LOG_DEBUG
* Note that LOG_EMERG, LOG_ALERT, and LOG_CRIT are not really relevant to a
* tftp server - these represent instability in the entire operating system.
* Note that the number they are represented by are in reverse order -
* LOG_EMERG is the lowest, LOG_DEBUG the highest.
*/
abstract class Logger
{
function __construct($minimum)
{
$this->minimum = $minimum;
}
function shouldlog($priority)
{
// Note: this looks reversed, but is correct
// the priority must be AT LEAST the minimum,
// because higher priorities represent lower numbers.
return $priority <= $this->minimum;
}
abstract function log($priority, $message);
}
class Logger_Null extends Logger
{
function log($priority, $message)
{
}
}
class Logger_Syslog extends Logger
{
function log($priority, $message)
{
if($this->shouldlog($priority))
syslog($priority,$message);
}
}
class Logger_Filehandle extends Logger
{
private $priority_map = array(
LOG_DEBUG => "D",
LOG_INFO => "I",
LOG_NOTICE => "N",
LOG_WARNING => "W",
LOG_ERR => "E",
LOG_CRIT => "C",
LOG_ALERT => "A",
LOG_EMERG => "!"
);
function __construct($minimum, $filehandle, $dateformat = "r")
{
$this->filehandle = $filehandle;
$this->dateformat = $dateformat;
return parent::__construct($minimum);
}
function log($priority, $message)
{
if($this->shouldlog($priority))
fwrite($this->filehandle, date($this->dateformat) . ": " . $this->priority_map[$priority] . " $message\n");
}
}
class Logger_Filename extends Logger_Filehandle
{
function __construct($minimum, $filename, $dateformat = "r")
{
return parent::__construct($minimum, fopen($filename, "a"), $dateformat);
}
}
class Logger_Stderr extends Logger_Filehandle
{
function __construct($minimum, $dateformat = "r")
{
return parent::__construct($minimum, STDERR, $dateformat);
}
}
class Logger_Stdout extends Logger_Filehandle
{
function __construct($minimum, $dateformat = "r")
{
return parent::__construct($minimum, STDOUT, $dateformat);
}
}
?>

View File

@@ -4,7 +4,7 @@ include_once("config.php");
include_once("utils.php");
/* Todo:
- setup logging
✔️ setup logging
✔️ read config.file
- improve error handling
?✔️ secure urlencoding/urldecoding
@@ -17,8 +17,10 @@ class Resolver {
private $isDirty = FALSE;
private $cache = array();
private $config;
private $logger;
function __construct($config) {
$this->config = $config;
$this->logger = $config['main']['logger'];
if(file_exists($this->config['main']['cache_filename'])) {
$this->cache = unserialize(file_get_contents($config['main']['cache_filename']));
} else {
@@ -29,10 +31,14 @@ class Resolver {
// $this->printCache()
if ($this->isDirty) {
if (!file_put_contents($this->config['main']['cache_filename'], serialize($this->cache))) {
throw new Exception("Could not write to file '".$this->config['cache_filename']."' at Resolver::destruct");
$this->log_error_and_throw("Could not write to file '".$this->config['cache_filename']."' at Resolver::destruct");
}
}
}
function log_error_and_throw($message) {
$this->logger->log('LOG_ERROR', $message);
throw new Exception($message);
}
function searchForFile($filename) {
foreach($this->config['subdirs'] as $key => $value) {
if ($key === "firmware" || $key === "tftproot" ) {
@@ -44,7 +50,7 @@ class Resolver {
return $path;
}
}
throw new Exception("File '$filename' does not exist");
$this->log_error_and_throw("File '$filename' does not exist");
}
function buildCleanCache() {
foreach($this->config['subdirs'] as $key =>$value) {
@@ -68,12 +74,12 @@ class Resolver {
$this->isDirty = TRUE;
}
function addFile($requestpath, $truepath) {
//echo 'Adding $hash\n';
$this->logger->log('LOG_DEBUG', "Adding $requestpath");
$this->cache[$requestpath] = $truepath;
$this->isDirty =TRUE;
}
function removeFile($requestpath) {
//echo 'Removing $hash\n';
$this->logger->log('LOG_DEBUG', "Removing $hash");
unset($this->cache[$requestpath]);
$this->isDirty = TRUE;
}
@@ -84,12 +90,10 @@ class Resolver {
//print($request . ":" . escapeshellarg($request) . ":" . $this->utf8_urldecode($request) . "\n");
$escaped_request = escapeshellarg(utf8_urldecode($request));
if ($escaped_request !== "'" . $request . "'") {
// log error
throw new Exception("Request '$request' contains invalid characters");
$this->log_error_and_throw("Request '$request' contains invalid characters");
}
if (strstr($escaped_request, "..")) {
// log error
throw new Exception("Request '$request' contains '..'");
$this->log_error_and_throw("Request '$request' containst '..'");
}
}
function resolve($request) /* canthrow */ {
@@ -99,7 +103,7 @@ class Resolver {
if ($path = $this->cache[$request]) {
if (!file_exists($path)) {
$this->removeFile($request);
throw new Exception("File '$request' does not exist on FS");
$this->log_error_and_throw("File '$request' does not exist on FS");
}
return $path;
}
@@ -114,7 +118,6 @@ class Resolver {
}
}
# Some simple inline testing
$resolver = new Resolver($config);
$test_cases = Array(
Array('request' => 'jar70sccp.9-4-2ES26.sbn', 'expected' => '/tftpboot/firmware/7970/jar70sccp.9-4-2ES26.sbn', 'throws' => FALSE),
@@ -126,6 +129,7 @@ $test_cases = Array(
Array('request' => 'XMLDefault.cnf.xml/../../text.xml', 'expected' => '', 'throws' => TRUE),
);
foreach($test_cases as $test) {
try {
$result = $resolver->resolve($test['request']);
@@ -143,6 +147,21 @@ foreach($test_cases as $test) {
}
}
}
/*
try {
print($resolver->resolve("jar70sccp.9-4-2ES26.sbn")."\n");
print($resolver->resolve("Russian_Russian_Federation/be-sccp.jar")."\n");
print($resolver->resolve("Spain/g3-tones.xml")."\n");
print($resolver->resolve("320x196x4/Chan-SCCP-b.png")."\n");
} catch (Exception $e) {
print($e . "\n");
}
try {
print($resolver->resolve("XMLDefault.cnf.xml")."\n");
} catch (Exception $e) {
print($e . "\n");
}
*/
unset($resolver);
#unlink($CACHEFILE_NAME);
?>