Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions apps/user_ldap/lib/LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
use OCA\User_LDAP\DataCollector\LdapDataCollector;
use OCA\User_LDAP\Exceptions\ConstraintViolationException;
use OCP\IConfig;
use OCP\ILogger;
use OCP\Profiler\IProfiler;
use OCP\Server;
use Psr\Log\LoggerInterface;

class LDAP implements ILDAPWrapper {
protected string $logFile = '';
protected array $curArgs = [];
protected LoggerInterface $logger;
protected IConfig $config;

private ?LdapDataCollector $dataCollector = null;

Expand All @@ -57,7 +60,8 @@ public function __construct(string $logFile = '') {
$profiler->add($this->dataCollector);
}

$this->logger = \OCP\Server::get(LoggerInterface::class);
$this->logger = Server::get(LoggerInterface::class);
$this->config = Server::get(IConfig::class);
}

/**
Expand Down Expand Up @@ -316,42 +320,49 @@ protected function invokeLDAPMethod(string $func, ...$arguments) {
return null;
}

/**
* Turn resources into string, and removes potentially problematic cookie string to avoid breaking logfiles
*/
private function sanitizeFunctionParameters(array $args): array {
return array_map(function ($item) {
if ($this->isResource($item)) {
return '(resource)';
}
if (isset($item[0]['value']['cookie']) && $item[0]['value']['cookie'] !== '') {
$item[0]['value']['cookie'] = '*opaque cookie*';
}
return $item;
}, $args);
}

private function preFunctionCall(string $functionName, array $args): void {
$this->curArgs = $args;
if(strcasecmp($functionName, 'ldap_bind') === 0 || strcasecmp($functionName, 'ldap_exop_passwd') === 0) {
if (strcasecmp($functionName, 'ldap_bind') === 0 || strcasecmp($functionName, 'ldap_exop_passwd') === 0) {
// The arguments are not key value pairs
// \OCA\User_LDAP\LDAP::bind passes 3 arguments, the 3rd being the pw
// Remove it via direct array access for now, although a better solution could be found mebbe?
// @link https://github.com/nextcloud/server/issues/38461
$args[2] = IConfig::SENSITIVE_VALUE;
}

$this->logger->debug('Calling LDAP function {func} with parameters {args}', [
'app' => 'user_ldap',
'func' => $functionName,
'args' => json_encode($args),
]);
if ($this->config->getSystemValue('loglevel') === ILogger::DEBUG) {
/* Only running this if debug loglevel is on, to avoid processing parameters on production */
$this->logger->debug('Calling LDAP function {func} with parameters {args}', [
'app' => 'user_ldap',
'func' => $functionName,
'args' => $this->sanitizeFunctionParameters($args),
]);
}

if ($this->dataCollector !== null) {
$args = array_map(function ($item) {
if ($this->isResource($item)) {
return '(resource)';
}
if (isset($item[0]['value']['cookie']) && $item[0]['value']['cookie'] !== "") {
$item[0]['value']['cookie'] = "*opaque cookie*";
}
return $item;
}, $this->curArgs);

$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$this->dataCollector->startLdapRequest($functionName, $args, $backtrace);
$this->dataCollector->startLdapRequest($functionName, $this->sanitizeFunctionParameters($args), $backtrace);
}

if ($this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
$args = array_map(fn ($item) => (!$this->isResource($item) ? $item : '(resource)'), $this->curArgs);
file_put_contents(
$this->logFile,
$functionName . '::' . json_encode($args) . "\n",
$functionName . '::' . json_encode($this->sanitizeFunctionParameters($args)) . "\n",
FILE_APPEND
);
}
Expand Down
Loading