phpldapadmin/app/Classes/LDAP/Server.php

158 lines
3.6 KiB
PHP
Raw Normal View History

2020-08-22 12:26:06 +00:00
<?php
2020-08-23 02:30:18 +00:00
namespace App\Classes\LDAP;
2020-08-22 12:26:06 +00:00
use App\Ldap\Entry;
2020-09-18 14:08:00 +00:00
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
2020-09-18 14:08:00 +00:00
use Illuminate\Support\Facades\Cache;
2020-08-22 12:26:06 +00:00
class Server
{
2020-09-18 14:08:00 +00:00
/**
* 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]);
}
}
2020-08-22 12:26:06 +00:00
/**
* 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).
*
2020-09-18 14:08:00 +00:00
* @return Collection|null array|NULL The root DN(s) of the server on success (string) or NULL if it cannot be determine.
2020-08-22 12:26:06 +00:00
* @todo Sort the entries, so that they are in the correct DN order.
* @testedby GetBaseDNTest::testBaseDNExists()
2020-08-22 12:26:06 +00:00
*/
public function getBaseDN(): ?Collection
2020-08-22 12:26:06 +00:00
{
//findBaseDn()?
2020-08-22 12:26:06 +00:00
// If the base is set in the configuration file, then just return that after validating it exists.
// @todo
if (false) {
// We need to work out the baseDN
} else {
$result = $this->getDNAttrValues('',['namingcontexts']);
return $result ? collect($result->namingcontexts) : NULL;
2020-08-22 12:26:06 +00:00
}
}
/**
* Search for a DN and return its attributes
*
* @param $dn
* @param array $attrs
* @param int $deref // @todo
* @return Entry|bool
*/
protected function getDNAttrValues(string $dn,array $attrs=['*','+'],int $deref=LDAP_DEREF_NEVER): ?Entry
{
try {
return ($x=(new Entry)
->query()
2020-08-22 12:26:06 +00:00
->select($attrs)
->find($dn)) ? $x : NULL;
2020-08-22 12:26:06 +00:00
// @todo Tidy up this exception
} catch (\Exception $e) {
2020-08-23 02:30:18 +00:00
dd(['e'=>$e]);
2020-08-22 12:26:06 +00:00
}
}
2020-09-13 11:30:04 +00:00
/**
2020-09-18 14:08:00 +00:00
* 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.
2020-09-13 11:30:04 +00:00
*
2020-09-18 14:08:00 +00:00
* <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 $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
* @testedby TranslateOidTest::testRootDSE()
2020-09-13 11:30:04 +00:00
*/
2020-09-18 14:08:00 +00:00
public static function getOID(string $oid,string $key): ?string
2020-09-13 11:30:04 +00:00
{
2020-09-18 14:08:00 +00:00
$oids = Cache::remember('oids',86400,function() {
2020-09-13 11:30:04 +00:00
2020-09-18 14:08:00 +00:00
try {
$f = fopen(config_path('ldap_supported_oids.txt'),'r');
2020-09-13 11:30:04 +00:00
2020-09-18 14:08:00 +00:00
} catch (\Exception $e) {
return NULL;
}
2020-09-18 14:08:00 +00:00
$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 : NULL))
2020-09-18 14:08:00 +00:00
);
}
2020-08-23 02:30:18 +00:00
}