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 : ''))
);
}
}