- renamed tftproot => data
- Added strict_types=1 - Added NameSpaces - Fixed config::replaceSubdirTreeStructure function - Fix tftp server Signed-off-by: Diederik de Groot <ddegroot@talon.nl>
This commit is contained in:
@@ -22,7 +22,7 @@ cert_pub = NULL
|
|||||||
hash = NULL
|
hash = NULL
|
||||||
|
|
||||||
;[subdirs]
|
;[subdirs]
|
||||||
;tftproot = tftpboot
|
;tftproot = data
|
||||||
;firmware = firmware
|
;firmware = firmware
|
||||||
;settings = settings
|
;settings = settings
|
||||||
;wallpapers = wallpapers
|
;wallpapers = wallpapers
|
||||||
|
@@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace SCCP\Config;
|
||||||
|
|
||||||
include_once("logger.php");
|
include_once("logger.php");
|
||||||
include_once("utils.php");
|
include_once("utils.php");
|
||||||
$base_path = realpath(__DIR__ . DIRECTORY_SEPARATOR . "..");
|
$base_path = realpath(__DIR__ . DIRECTORY_SEPARATOR . "..");
|
||||||
@@ -24,8 +27,8 @@ class ConfigParser {
|
|||||||
hash = NULL
|
hash = NULL
|
||||||
|
|
||||||
[subdirs]
|
[subdirs]
|
||||||
etc = ../etc
|
etc = etc
|
||||||
tftproot = tftpboot
|
data = data
|
||||||
firmware = firmware
|
firmware = firmware
|
||||||
settings = settings
|
settings = settings
|
||||||
wallpapers = wallpapers
|
wallpapers = wallpapers
|
||||||
@@ -72,7 +75,7 @@ class ConfigParser {
|
|||||||
$config = array_merge($default_config, $ini_config);
|
$config = array_merge($default_config, $ini_config);
|
||||||
}
|
}
|
||||||
$config['main']['base_path'] = $base_path;
|
$config['main']['base_path'] = $base_path;
|
||||||
$config['main']['tftproot'] = (!empty($config['main']['tftproot'])) ? $base_path . "tftpboot" : '/tftpboot';
|
$config['main']['data'] = (!empty($config['main']['data'])) ? $base_path . "data" : DIRECTORY_SEPARATOR . 'data';
|
||||||
$config['subdirs'] = $this->replaceSubdirTreeStructure($config['subdirs']);
|
$config['subdirs'] = $this->replaceSubdirTreeStructure($config['subdirs']);
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
}
|
}
|
||||||
@@ -85,12 +88,12 @@ class ConfigParser {
|
|||||||
private function replaceSubdirTreeStructure($tmpSubdirs) {
|
private function replaceSubdirTreeStructure($tmpSubdirs) {
|
||||||
$tree_structure = Array(
|
$tree_structure = Array(
|
||||||
'etc' => array('parent' => NULL, "strip" => true),
|
'etc' => array('parent' => NULL, "strip" => true),
|
||||||
'tftproot' => array('parent' => NULL, "strip" => true),
|
'data' => array('parent' => NULL, "strip" => true),
|
||||||
'settings' => array('parent' => 'tftproot', "strip" => true),
|
'settings' => array('parent' => 'data', "strip" => true),
|
||||||
'wallpapers' => array('parent' => 'tftproot', "strip" => false),
|
'wallpapers' => array('parent' => 'data', "strip" => false),
|
||||||
'ringtones' => array('parent' => 'tftproot', "strip" => true),
|
'ringtones' => array('parent' => 'data', "strip" => true),
|
||||||
'locales' => array('parent' => 'tftproot', "strip" => true),
|
'locales' => array('parent' => 'data', "strip" => true),
|
||||||
'firmware' => array('parent' => 'tftproot', "strip" => true),
|
'firmware' => array('parent' => 'data', "strip" => true),
|
||||||
'languages' => array('parent' => 'locales', "strip" => false),
|
'languages' => array('parent' => 'locales', "strip" => false),
|
||||||
'countries' => array('parent' => 'locales', "strip" => false),
|
'countries' => array('parent' => 'locales', "strip" => false),
|
||||||
);
|
);
|
||||||
@@ -98,16 +101,10 @@ class ConfigParser {
|
|||||||
$subdirs = Array();
|
$subdirs = Array();
|
||||||
foreach ($tree_structure as $key => $value) {
|
foreach ($tree_structure as $key => $value) {
|
||||||
if (!empty($tmpSubdirs[$key])) {
|
if (!empty($tmpSubdirs[$key])) {
|
||||||
if (substr($tmpSubdirs[$key], 0, 1) !== "/") {
|
if (!$value['parent']) {
|
||||||
if (!$value['parent']) {
|
$path = $tmpSubdirs[$key];
|
||||||
$path = $tmpSubdirs[$key];
|
} else {
|
||||||
} else {
|
$path = $subdirs[$value['parent']]['path'] . DIRECTORY_SEPARATOR . $tmpSubdirs[$key];
|
||||||
if (is_array($tmpSubdirs[$value['parent']])) {
|
|
||||||
$path = $tmpSubdirs[$value['parent']]['path'].'/'.$tmpSubdirs[$key];
|
|
||||||
} else {
|
|
||||||
$path = $tmpSubdirs[$value['parent']].'/'.$tmpSubdirs[$key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$subdirs[$key] = array('path' => $path, 'strip' => $value['strip']);
|
$subdirs[$key] = array('path' => $path, 'strip' => $value['strip']);
|
||||||
}
|
}
|
||||||
@@ -190,7 +187,7 @@ $config = $configParser->getConfiguration();
|
|||||||
|
|
||||||
switch($config['main']['log_type']) {
|
switch($config['main']['log_type']) {
|
||||||
case 'SYSLOG':
|
case 'SYSLOG':
|
||||||
$logger = new Logger_Syslog($config['main']['log_level']);
|
$logger = new \SCCP\Logger\Logger_Syslog($config['main']['log_level']);
|
||||||
break;
|
break;
|
||||||
case 'FILE':
|
case 'FILE':
|
||||||
if (!isempty($config['main']['log_file'])) {
|
if (!isempty($config['main']['log_file'])) {
|
||||||
|
@@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace SCCP\Logger;
|
||||||
|
|
||||||
/* Note about the Logger class:
|
/* Note about the Logger class:
|
||||||
* The "priority" and "minimum should be one of the constants used for syslog.
|
* The "priority" and "minimum should be one of the constants used for syslog.
|
||||||
* See: http://php.net/manual/en/function.syslog.php
|
* See: http://php.net/manual/en/function.syslog.php
|
||||||
|
@@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace SCCP\ResolveCache;
|
||||||
|
use SCCP\Utils as Utils;
|
||||||
|
|
||||||
abstract class resolveCache {
|
abstract class resolveCache {
|
||||||
abstract protected function addFile($filename, $realpath);
|
abstract protected function addFile($filename, $realpath);
|
||||||
abstract protected function removeFile($filename);
|
abstract protected function removeFile($filename);
|
||||||
@@ -27,7 +30,7 @@ class fileCache extends resolveCache {
|
|||||||
log_error_and_throw("Could not write to file '".$this->_cache_file."' at Resolver::destruct");
|
log_error_and_throw("Could not write to file '".$this->_cache_file."' at Resolver::destruct");
|
||||||
}*/
|
}*/
|
||||||
if (!file_put_contents($this->_cache_file, serialize($this->_cache))) {
|
if (!file_put_contents($this->_cache_file, serialize($this->_cache))) {
|
||||||
log_error_and_throw("Could not write to file '".$this->_cache_file."' at Resolver::destruct");
|
Utils\log_error_and_throw("Could not write to file '".$this->_cache_file."' at Resolver::destruct");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,7 +45,7 @@ class fileCache extends resolveCache {
|
|||||||
|
|
||||||
public function addFile($filename, $realpath) {
|
public function addFile($filename, $realpath) {
|
||||||
if ($this->check($filename))
|
if ($this->check($filename))
|
||||||
log_error("Duplicate file:$filename"); /* should we prevent this ? */
|
Utils\log_error("Duplicate file:$filename"); /* should we prevent this ? */
|
||||||
$this->_cache[$filename] = $realpath;
|
$this->_cache[$filename] = $realpath;
|
||||||
$this->_isDirty =true;
|
$this->_isDirty =true;
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace SCCP\Resolve;
|
||||||
include_once("config.php");
|
include_once("config.php");
|
||||||
include_once("utils.php");
|
include_once("utils.php");
|
||||||
include_once("resolveCache.php");
|
include_once("resolveCache.php");
|
||||||
|
|
||||||
|
use SCCP\Utils as Utils;
|
||||||
|
use SCCP\ResolveCache as ResolveCache;
|
||||||
|
|
||||||
/* Todo:
|
/* Todo:
|
||||||
✔️ setup logging
|
✔️ setup logging
|
||||||
✔️ read config.file
|
✔️ read config.file
|
||||||
@@ -16,6 +21,7 @@ include_once("resolveCache.php");
|
|||||||
|
|
||||||
- Could use some more test-cases, especially error ones
|
- Could use some more test-cases, especially error ones
|
||||||
*/
|
*/
|
||||||
|
//class ResolveResult extends Enum {
|
||||||
class ResolveResult {
|
class ResolveResult {
|
||||||
const Ok = 0;
|
const Ok = 0;
|
||||||
const EmptyRequest = 1;
|
const EmptyRequest = 1;
|
||||||
@@ -33,43 +39,57 @@ class Resolver {
|
|||||||
private $config;
|
private $config;
|
||||||
function __construct($config) {
|
function __construct($config) {
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->cache = new fileCache($this->config['main']['cache_filename']);
|
$this->cache = new ResolveCache\fileCache($this->config['main']['cache_filename']);
|
||||||
if ($this->cache->isDirty()) {
|
if ($this->cache->isDirty()) {
|
||||||
$this->rebuildCache();
|
$this->rebuildCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function searchForFile($filename) {
|
public function searchForFile($filename) {
|
||||||
|
$path = "";
|
||||||
foreach($this->config['subdirs'] as $key => $value) {
|
foreach($this->config['subdirs'] as $key => $value) {
|
||||||
if ($key === "firmware" || $key === "tftproot" ) {
|
if ($key === "firmware" || $key === "data" || $key === "etc") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$path = realpath($this->config['main']['base_path'] . "/" . $value['path'] . "/$filename");
|
$path = realpath($this->config['main']['base_path'] . DIRECTORY_SEPARATOR . $value['path'] . DIRECTORY_SEPARATOR . $filename);
|
||||||
if (file_exists($path)) {
|
if (!$path) {
|
||||||
$this->cache->addFile($filename, $path);
|
print("path: '" . $this->config['main']['base_path'] . DIRECTORY_SEPARATOR . $value['path'] . DIRECTORY_SEPARATOR . $filename . "' not found\n");
|
||||||
return $path;
|
return ResolveResult::FileNotFound;
|
||||||
}
|
}
|
||||||
|
$this->cache->addFile($filename, $path);
|
||||||
|
return $path;
|
||||||
}
|
}
|
||||||
log_error("File '$filename' does not exist");
|
Utils\log_error("File '$filename' does not exist");
|
||||||
return ResolveResult::FileNotFound;
|
return ResolveResult::FileNotFound;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rebuildCache() {
|
public function rebuildCache() {
|
||||||
log_debug("Rebuilding Cache, standby...");
|
Utils\log_debug("Rebuilding Cache, standby...");
|
||||||
foreach($this->config['subdirs'] as $key =>$value) {
|
foreach($this->config['subdirs'] as $key =>$value) {
|
||||||
if ($key === "tftproot") {
|
if ($key === "data" || $key === "etc") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$path = $this->config['main']['base_path'] . "/" . $value['path'] . "/";
|
$path = realpath($this->config['main']['base_path'] . DIRECTORY_SEPARATOR . $value['path'] . DIRECTORY_SEPARATOR);
|
||||||
$dir_iterator = new RecursiveDirectoryIterator($path);
|
if (!$path) {
|
||||||
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
|
print("path: '" . $this->config['main']['base_path'] . DIRECTORY_SEPARATOR . $value['path'] . "' not found\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$dir_iterator = new \RecursiveDirectoryIterator($path);
|
||||||
|
$filter = new \RecursiveCallbackFilterIterator($dir_iterator, function ($current, $key, $iterator) {
|
||||||
|
// Skip hidden files and directories.
|
||||||
|
if ($current->getFilename()[0] === '.' || $current->getFilename() == "bak") {
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
});
|
||||||
|
$iterator = new \RecursiveIteratorIterator($filter, \RecursiveIteratorIterator::SELF_FIRST);
|
||||||
foreach ($iterator as $file) {
|
foreach ($iterator as $file) {
|
||||||
if ($file->isFile()) {
|
if ($file->isFile()) {
|
||||||
if ($value['strip']) {
|
if ($value['strip']) {
|
||||||
$this->cache->addFile($file->getFileName(), $file->getPathname());
|
$this->cache->addFile($file->getFileName(), $file->getPathname());
|
||||||
} else {
|
} else {
|
||||||
$subdir = basename(dirname($file->getPathname()));
|
$subdir = basename(dirname($file->getPathname()));
|
||||||
$this->cache->addFile('$subpath/'.$file->getFileName(), $file->getPathname());
|
$this->cache->addFile($subdir. DIRECTORY_SEPARATOR . $file->getFileName(), $file->getPathname());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,14 +109,14 @@ class Resolver {
|
|||||||
log_error("Request is not a string");
|
log_error("Request is not a string");
|
||||||
return ResolveResult::RequestNotAString;
|
return ResolveResult::RequestNotAString;
|
||||||
}
|
}
|
||||||
log_debug($request . ":" . escapeshellarg($request) . ":" . utf8_urldecode($request) . "\n");
|
Utils\log_debug($request . ":" . escapeshellarg($request) . ":" . Utils\utf8_urldecode($request) . "\n");
|
||||||
$escaped_request = escapeshellarg(utf8_urldecode($request));
|
$escaped_request = escapeshellarg(Utils\utf8_urldecode($request));
|
||||||
if ($escaped_request !== "'" . $request . "'") {
|
if ($escaped_request !== "'" . $request . "'") {
|
||||||
log_error("Request '$request' contains invalid characters");
|
log_error("Request '$request' contains invalid characters");
|
||||||
return ResolveResult::RequestContainsInvalidChar;
|
return ResolveResult::RequestContainsInvalidChar;
|
||||||
}
|
}
|
||||||
if (strstr($escaped_request, "..")) {
|
if (strstr($escaped_request, "..")) {
|
||||||
log_error("Request '$request' contains '..'");
|
Utils\log_error("Request '$request' contains '..'");
|
||||||
return ResolveResult::RequestContainsPathWalk;
|
return ResolveResult::RequestContainsPathWalk;
|
||||||
}
|
}
|
||||||
return ResolveResult::Ok;
|
return ResolveResult::Ok;
|
||||||
@@ -132,11 +152,11 @@ class Resolver {
|
|||||||
if(defined('STDIN') ) {
|
if(defined('STDIN') ) {
|
||||||
$resolver = new Resolver($config);
|
$resolver = new Resolver($config);
|
||||||
$test_cases = Array(
|
$test_cases = Array(
|
||||||
Array('request' => 'jar70sccp.9-4-2ES26.sbn', 'expected' => '/tftpboot/firmware/7970/jar70sccp.9-4-2ES26.sbn'),
|
Array('request' => 'jar70sccp.9-4-2ES26.sbn', 'expected' => '/data/firmware/7970/jar70sccp.9-4-2ES26.sbn'),
|
||||||
Array('request' => 'Russian_Russian_Federation/be-sccp.jar', 'expected' => '/tftpboot/locales/languages/Russian_Russian_Federation/be-sccp.jar'),
|
Array('request' => 'Russian_Russian_Federation/be-sccp.jar', 'expected' => '/data/locales/languages/Russian_Russian_Federation/be-sccp.jar'),
|
||||||
Array('request' => 'Spain/g3-tones.xml', 'expected' => '/tftpboot/locales/countries/Spain/g3-tones.xml'),
|
Array('request' => 'Spain/g3-tones.xml', 'expected' => '/data/locales/countries/Spain/g3-tones.xml'),
|
||||||
Array('request' => '320x196x4/Chan-SCCP-b.png', 'expected' => '/tftpboot/wallpapers/320x196x4/Chan-SCCP-b.png'),
|
Array('request' => '320x196x4/Chan-SCCP-b.png', 'expected' => '/data/wallpapers/320x196x4/Chan-SCCP-b.png'),
|
||||||
Array('request' => 'XMLDefault.cnf.xml', 'expected' => '/tftpboot/settings/XMLDefault.cnf.xml'),
|
Array('request' => 'XMLDefault.cnf.xml', 'expected' => '/data/settings/XMLDefault.cnf.xml'),
|
||||||
Array('request' => '../XMLDefault.cnf.xml', 'expected' => ResolveResult::RequestContainsPathWalk),
|
Array('request' => '../XMLDefault.cnf.xml', 'expected' => ResolveResult::RequestContainsPathWalk),
|
||||||
Array('request' => 'XMLDefault.cnf.xml/../../text.xml', 'expected' => ResolveResult::RequestContainsPathWalk),
|
Array('request' => 'XMLDefault.cnf.xml/../../text.xml', 'expected' => ResolveResult::RequestContainsPathWalk),
|
||||||
);
|
);
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace SCCP\TFTP;
|
||||||
/*
|
/*
|
||||||
* PHP TFTP Server
|
* PHP TFTP Server
|
||||||
*
|
*
|
||||||
|
@@ -2,10 +2,12 @@
|
|||||||
//
|
//
|
||||||
// Helper functions
|
// Helper functions
|
||||||
//
|
//
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace SCCP\Utils;
|
||||||
|
|
||||||
function utf8_urldecode($str) {
|
function utf8_urldecode($str) {
|
||||||
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));
|
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));
|
||||||
return html_entity_decode($str,null,'UTF-8');;
|
return html_entity_decode($str,0,'UTF-8');;
|
||||||
}
|
}
|
||||||
|
|
||||||
function log_debug($message) {
|
function log_debug($message) {
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace SCCP\XML;
|
||||||
include_once("config.php");
|
include_once("config.php");
|
||||||
|
|
||||||
class XML {
|
class XML {
|
||||||
|
@@ -1,10 +1,14 @@
|
|||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
require_once("lib/tftp.php");
|
require_once("lib/tftp.php");
|
||||||
require_once("lib/config.php");
|
require_once("lib/config.php");
|
||||||
require_once("lib/resolver.php");
|
require_once("lib/resolver.php");
|
||||||
|
|
||||||
class TFTPProvisioner extends TFTPServer
|
use SCCP\TFTP as TFTP;
|
||||||
|
use SCCP\Resolve as Resolve;
|
||||||
|
class TFTPProvisioner extends TFTP\TFTPServer
|
||||||
{
|
{
|
||||||
private $_debug;
|
private $_debug;
|
||||||
private $_resolver;
|
private $_resolver;
|
||||||
@@ -19,7 +23,7 @@ class TFTPProvisioner extends TFTPServer
|
|||||||
parent::__construct($server_url, $logger);
|
parent::__construct($server_url, $logger);
|
||||||
$this->_debug = $debug;
|
$this->_debug = $debug;
|
||||||
$this->max_put_size = 60000000;
|
$this->max_put_size = 60000000;
|
||||||
$this->_resolver = new Resolver($config);
|
$this->_resolver = new Resolve\Resolver($config);
|
||||||
$this->_settings_path = $this->_config['main']['base_path'] . DIRECTORY_SEPARATOR
|
$this->_settings_path = $this->_config['main']['base_path'] . DIRECTORY_SEPARATOR
|
||||||
. $this->_config['subdirs']['settings']['path'] . DIRECTORY_SEPARATOR;
|
. $this->_config['subdirs']['settings']['path'] . DIRECTORY_SEPARATOR;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user