Added server info

This commit is contained in:
Deon George
2020-09-19 00:08:00 +10:00
parent db61e0d1ce
commit d20a17d3fe
12 changed files with 397 additions and 330 deletions

View File

@@ -3,16 +3,61 @@
namespace App\Classes\LDAP;
use App\Ldap\Entry;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
class Server
{
/**
* Query the server for a DN
*
* @param string $dn
* @return array|\LdapRecord\Query\Collection|null
*/
public function children(string $dn)
{
try {
return ($x=(new Entry)
->query()
->select(['dn','hassubordinates'])
->setDn($dn)
->listing()
->get()) ? $x : NULL;
// @todo Tidy up this exception
} catch (\Exception $e) {
dd(['e'=>$e]);
}
}
/**
* Fetch a DN from the server
*
* @param string $dn
* @param array $attrs
* @return array|\LdapRecord\Models\Model|\LdapRecord\Query\Collection|\LdapRecord\Query\Model\Builder|null
*/
public function fetch(string $dn,array $attrs=['*','+'])
{
try {
return ($x=(new Entry)
->query()
->select($attrs)
->find($dn)) ? $x : NULL;
// @todo Tidy up this exception
} catch (\Exception $e) {
dd(['e'=>$e]);
}
}
/**
* Gets the root DN of the specified LDAPServer, or NULL if it
* can't find it (ie, the server won't give it to us, or it isnt
* specified in the configuration file).
*
* @return array array|NULL The root DN(s) of the server on success (string) or NULL if it cannot be determine.
* @return Collection|null array|NULL The root DN(s) of the server on success (string) or NULL if it cannot be determine.
* @todo Sort the entries, so that they are in the correct DN order.
*/
public function getBaseDN(): ?Collection
@@ -53,44 +98,58 @@ class Server
}
/**
* Fetch a DN from the server
* Given an LDAP OID number, returns a verbose description of the OID.
* This function parses ldap_supported_oids.txt and looks up the specified
* OID, and returns the verbose message defined in that file.
*
* @param $dn
* @return |null
*/
public function fetch(string $dn,array $attrs=['*','+'])
{
try {
return ($x=(new Entry)
->query()
->select($attrs)
->find($dn)) ? $x : NULL;
// @todo Tidy up this exception
} catch (\Exception $e) {
dd(['e'=>$e]);
}
}
/**
* Query the server for a DN
* <code>
* Array (
* [title] => All Operational Attribute
* [ref] => RFC 3673
* [desc] => An LDAP extension which clients may use to request the return of all operational attributes.
* )
* </code>
*
* @param string $dn
* @return |null
* @param string $oid The OID number (ie, "1.3.6.1.4.1.4203.1.5.1") of the OID of interest.
* @param string $key The title|ref|desc to return
* @return string|null
*/
public function children(string $dn)
public static function getOID(string $oid,string $key): ?string
{
try {
return ($x=(new Entry)
->query()
->select(['dn','hassubordinates'])
->setDn($dn)
->listing()
->get()) ? $x : NULL;
$oids = Cache::remember('oids',86400,function() {
// @todo Tidy up this exception
} catch (\Exception $e) {
dd(['e'=>$e]);
}
try {
$f = fopen(config_path('ldap_supported_oids.txt'),'r');
} catch (\Exception $e) {
return NULL;
}
$result = collect();
while (! feof($f)) {
$line = trim(fgets($f));
if (! $line OR preg_match('/^#/',$line))
continue;
$fields = explode(':',$line);
$result->put(Arr::get($fields,0),[
'title'=>Arr::get($fields,1),
'ref'=>Arr::get($fields,2),
'desc'=>Arr::get($fields,3),
]);
}
fclose($f);
return $result;
});
return Arr::get(
($oids ? $oids->get($oid) : []),
$key,
($key == 'desc' ? 'No description available, can you help with one?' : ($key == 'title' ? $oid : ''))
);
}
}

View File

@@ -4,15 +4,18 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use App\Classes\LDAP\Server;
use Illuminate\Support\Facades\File;
use App\Ldap\Entry;
use App\Classes\LDAP\Server;
class HomeController extends Controller
{
public function home() {
public function home()
{
$o = new Server;
return view('home')
@@ -28,12 +31,49 @@ class HomeController extends Controller
}));
}
public function render(Request $request) {
public function info()
{
$attrs = collect((new Entry)->rootDSE()->getAttributes())
->transform(function($item,$key) {
foreach ($item as $k=>$v) {
if (preg_match('/[0-9]+\.[0-9]+\.[0-9]+/',$v)) {
$format = sprintf(
'<abbr class="pb-1" title="%s"><i class="fas fa-list-ol pr-2"></i>%s</abbr>%s<p class="mb-0">%s</p>',
$v,
Server::getOID($v,'title'),
($x=Server::getOID($v,'ref')) ? sprintf('<abbr class="pl-2" title="%s"><i class="fas fa-comment-dots"></i></abbr>',$x) : '',
Server::getOID($v,'desc'),
);
$item[$k] = $format;
}
}
return $item;
});
return view('widgets.dn')
->with('dn','Server Info')
->with('attributes',$this->sortAttrs($attrs));
}
public function render(Request $request)
{
$dn = Crypt::decryptString($request->post('key'));
return view('widgets.dn')
->with('dn',$dn)
->with('leaf',(new Server())->fetch($dn));
->with('leaf',$x=(new Server())->fetch($dn))
->with('attributes',$this->sortAttrs(collect($x->getAttributes())));
}
/**
* Sort the attributes
*
* @param Collection $attrs
* @return Collection
*/
private function sortAttrs(Collection $attrs): Collection
{
return $attrs->sortKeys();
}
/**

View File

@@ -12,4 +12,14 @@ class Entry extends Model
* @var array
*/
public static $objectClasses = [];
public function rootDSE($connection = null)
{
return static::on($connection ?? (new static)->getConnectionName())
->in(null)
->read()
->select(['+'])
->whereHas('objectclass')
->firstOrFail();
}
}