Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions docs/Server/Access-Control.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,8 @@ A denied assertion is folded away before the query runs, so the attribute behave
(|(cn=bob)(telephoneNumber=555-0100)) -> the cn=bob match, the other branch drops
```

The search itself still runs as usual. Its base must exist and its controls still apply.

This is what stops a value being recovered a guess at a time. Reads are unaffected: denying the filter does not strip
the value, and denying the read does not stop the filter. Pair the two to get both. A value-level modify on a
filter-denied attribute is gated the same way; see [Value-Level Modify](#value-level-modify).
Expand Down
3 changes: 3 additions & 0 deletions docs/Server/Schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ Raise or remove the bound with `maxLinkedValues` on the search limits, including
rule. A client can read a large attribute either one slice at a time with the range option, covered in
[Range Retrieval](../Client/Range-Retrieval.md), or as entries through a paged search on the back-link.

The Pre-Read and Post-Read controls return a linked attribute such as `member` in full. They bound a back-link by the
server's `maxLinkedValues` alone, never by a per-identity rule.

The in-memory backend keeps linked values on the entry as written. It does not refuse a name that resolves to nothing,
does not follow a rename, does not remove a reference when its target is deleted, and does not bound how many values a
read returns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ private function makeWithheldAttributeMiddleware(Container $container): Withheld

private function makeAssertionMiddleware(Container $container): AssertionMiddleware
{
return new AssertionMiddleware($container->get(AssertionEvaluator::class));
return new AssertionMiddleware(
$container->get(AssertionEvaluator::class),
$container->get(ServerProtocolHandlerFactory::class),
);
}

private function makeResourceLimitMiddleware(Container $container): ResourceLimitMiddleware
Expand Down
41 changes: 23 additions & 18 deletions src/FreeDSx/Ldap/Container/Provider/DecoderContainerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use FreeDSx\Ldap\Protocol\Decoder\Control\PwdPolicyResponseControlDecoder;
use FreeDSx\Ldap\Protocol\Decoder\Control\ReadEntryControlDecoder;
use FreeDSx\Ldap\Protocol\Decoder\Control\ReadEntryResponseControlDecoder;
use FreeDSx\Ldap\Protocol\Decoder\Control\RequestControlDecoders;
use FreeDSx\Ldap\Protocol\Decoder\Control\SortingControlDecoder;
use FreeDSx\Ldap\Protocol\Decoder\Control\SortingResponseControlDecoder;
use FreeDSx\Ldap\Protocol\Decoder\Control\SubentriesControlDecoder;
Expand Down Expand Up @@ -82,6 +83,7 @@
use FreeDSx\Ldap\Search\Filter\GreaterThanOrEqualFilter;
use FreeDSx\Ldap\Search\Filter\LessThanOrEqualFilter;
use FreeDSx\Ldap\Search\Filter\OrFilter;
use FreeDSx\Ldap\Server\Middleware\ServerControlRegistry;

/**
* Registers the decoders that turn received messages into requests and responses.
Expand Down Expand Up @@ -265,26 +267,29 @@ private function makeResponseMessageDecoder(Container $container): ResponseMessa
}

/**
* Types only the controls a client sends, holding any other OID to RFC 4511 4.1.11 criticality.
* Types only the controls a request's operation takes, holding any other to RFC 4511 4.1.11 criticality.
*/
private function makeRequestControlDecoder(Container $container): ControlDecoder
private function makeRequestControlDecoder(Container $container): RequestControlDecoders
{
return new ControlDecoder([
Control::OID_PAGING => $container->get(PagingControlDecoder::class),
Control::OID_SORTING => $container->get(SortingControlDecoder::class),
Control::OID_SYNC_REQUEST => $container->get(SyncRequestControlDecoder::class),
Control::OID_PROXY_AUTHORIZATION => $container->get(ProxyAuthorizationControlDecoder::class),
Control::OID_ASSERTION => $container->get(AssertionControlDecoder::class),
Control::OID_PRE_READ => new ReadEntryControlDecoder(
PreReadControl::class,
$container->get(AttributeSelectionDecoder::class),
),
Control::OID_POST_READ => new ReadEntryControlDecoder(
PostReadControl::class,
$container->get(AttributeSelectionDecoder::class),
),
Control::OID_SUBENTRIES => $container->get(SubentriesControlDecoder::class),
]);
return new RequestControlDecoders(
[
Control::OID_PAGING => $container->get(PagingControlDecoder::class),
Control::OID_SORTING => $container->get(SortingControlDecoder::class),
Control::OID_SYNC_REQUEST => $container->get(SyncRequestControlDecoder::class),
Control::OID_PROXY_AUTHORIZATION => $container->get(ProxyAuthorizationControlDecoder::class),
Control::OID_ASSERTION => $container->get(AssertionControlDecoder::class),
Control::OID_PRE_READ => new ReadEntryControlDecoder(
PreReadControl::class,
$container->get(AttributeSelectionDecoder::class),
),
Control::OID_POST_READ => new ReadEntryControlDecoder(
PostReadControl::class,
$container->get(AttributeSelectionDecoder::class),
),
Control::OID_SUBENTRIES => $container->get(SubentriesControlDecoder::class),
],
new ServerControlRegistry(),
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
use FreeDSx\Ldap\Server\Backend\Storage\Contract\WriteEntryInterface;
use FreeDSx\Ldap\Server\Backend\Storage\Directory\AliasResolver;
use FreeDSx\Ldap\Server\Backend\Storage\Derived\DerivedResolver;
use FreeDSx\Ldap\Server\Backend\Storage\Link\BacklinkReader;
use FreeDSx\Ldap\Server\Backend\Storage\Export\DirectoryDumper;
use FreeDSx\Ldap\Server\Backend\Write\Replay\WriteRequestReplayer;
use FreeDSx\Ldap\Server\Backend\Storage\Filter\FilterEvaluator;
Expand Down Expand Up @@ -103,6 +104,7 @@
use FreeDSx\Ldap\Server\Backend\Storage\Adapter\Operation\RdnAttributeValues;
use FreeDSx\Ldap\Server\Backend\Storage\Adapter\Operation\WriteEntryOperationHandler;
use FreeDSx\Ldap\Server\Backend\Write\OperationalAttributeGenerator;
use FreeDSx\Ldap\Server\Backend\Storage\Search\EntryProjection;
use FreeDSx\Ldap\Server\Backend\Storage\Search\SearchStreamBuilder;
use FreeDSx\Ldap\Server\Backend\Storage\Search\StorageListOptionsFactory;
use FreeDSx\Ldap\Server\Backend\ReadBackendInterface;
Expand Down Expand Up @@ -191,6 +193,7 @@ public function factories(): array
PasswordAuthenticatableInterface::class => $this->makePasswordAuthenticator(...),
FilterEvaluatorInterface::class => $this->makeFilterEvaluator(...),
DerivedResolver::class => $this->makeDerivedResolver(...),
BacklinkReader::class => $this->makeBacklinkReader(...),
SchemaValidator::class => $this->buildSchemaValidator(...),
EqualityComparatorResolver::class => $this->makeEqualityComparatorResolver(...),
DirectoryDumper::class => $this->makeDirectoryDumper(...),
Expand Down Expand Up @@ -339,6 +342,18 @@ private function makeDerivedResolver(Container $container): DerivedResolver
return new DerivedResolver($container->get(ReadEntryInterface::class));
}

/**
* Back-links are read under the server's default link cap, as a search reads them.
*/
private function makeBacklinkReader(Container $container): BacklinkReader
{
return new BacklinkReader(
$container->get(ReadEntryInterface::class),
$container->get(LinkedAttributes::class)->backlinks(),
EntryProjection::linkCapFor($container->get(ServerOptions::class)->makeSearchLimits()->maxLinkedValues()),
);
}

/**
* Replay runs the pipeline stages that need no connection, so it cannot drift from the wire path.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use FreeDSx\Ldap\Server\Backend\Auth\PasswordHashService;
use FreeDSx\Ldap\Server\Backend\Storage\Contract\ReadEntryInterface;
use FreeDSx\Ldap\Server\Backend\Storage\Derived\DerivedResolver;
use FreeDSx\Ldap\Server\Backend\Storage\Link\BacklinkReader;
use FreeDSx\Ldap\Server\Backend\Storage\Filter\FilterEvaluatorInterface;
use FreeDSx\Ldap\Server\Backend\Storage\Filter\LinkedLeafWitness;
use FreeDSx\Ldap\Server\Backend\Storage\Filter\StrippedEntryMatcher;
Expand Down Expand Up @@ -251,6 +252,7 @@ private function makeDispatchHandler(
),
assertions: $container->get(AssertionEvaluator::class),
derived: $container->get(DerivedResolver::class),
backlinks: $container->get(BacklinkReader::class),
readEntryControlHandler: new ReadEntryControlHandler(
$container->get(ServerOptions::class)->getSchema(),
$container->get(AccessControlInterface::class),
Expand Down
29 changes: 16 additions & 13 deletions src/FreeDSx/Ldap/Protocol/Bind/Sasl/SaslExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use FreeDSx\Ldap\Operation\ResultCode;
use FreeDSx\Ldap\Protocol\Bind\Sasl\OptionsBuilder\MechanismOptionsBuilderFactory;
use FreeDSx\Ldap\Protocol\Bind\Sasl\UsernameExtractor\SaslUsernameExtractorFactory;
use FreeDSx\Ldap\Protocol\DecodeFailureOutcome;
use FreeDSx\Ldap\Protocol\DecodeFailureResponder;
use FreeDSx\Ldap\Protocol\Factory\ResponseFactory;
use FreeDSx\Ldap\Protocol\LdapMessageRequest;
Expand Down Expand Up @@ -297,33 +298,35 @@ private function nextMessage(): LdapMessageRequest
$message = $this->readMessage();

// Continuations skip the validation middleware, and the challenge echoes back whatever ID arrives.
if ($message->getMessageId() === 0) {
if ($message?->getMessageId() === 0) {
throw new RequestValidationException('The message ID 0 cannot be used in a client request.');
}
} while ($message->getRequest() instanceof AbandonRequest);
} while ($message === null || $message->getRequest() instanceof AbandonRequest);

return $message;
}

/**
* Reads a message, or null when a refused request that takes no response was dropped.
*
* @throws ResponseAlreadySentException when a message that fails to decode is answered on its own ID.
* @throws MessageDecodeException when a message that fails to decode cannot be answered.
*/
private function readMessage(): LdapMessageRequest
private function readMessage(): ?LdapMessageRequest
{
try {
return $this->queue->getMessage();
} catch (MessageDecodeException $e) {
if (!$this->decodeFailures->answer($e)) {
throw $e;
}

// The bind ends with the message that broke it, which is already answered.
throw new ResponseAlreadySentException(
$e->getMessage(),
$e->getResultCode(),
$e,
);
return match ($this->decodeFailures->answer($e)) {
DecodeFailureOutcome::Dropped => null,
// The bind ends with the message that broke it, which is already answered.
DecodeFailureOutcome::Answered => throw new ResponseAlreadySentException(
$e->getMessage(),
$e->getResultCode(),
$e,
),
DecodeFailureOutcome::Unanswerable => throw $e,
};
}
}

Expand Down
38 changes: 38 additions & 0 deletions src/FreeDSx/Ldap/Protocol/DecodeFailureOutcome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* This file is part of the FreeDSx LDAP package.
*
* (c) Chad Sikorra <Chad.Sikorra@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FreeDSx\Ldap\Protocol;

/**
* What became of a message that failed to decode.
*
* @internal
* @author Chad Sikorra <Chad.Sikorra@gmail.com>
*/
enum DecodeFailureOutcome
{
/**
* A response was sent on the message's ID.
*/
case Answered;

/**
* A refused request that takes no response was recorded and dropped.
*/
case Dropped;

/**
* It cannot be answered and ends the session.
*/
case Unanswerable;
}
12 changes: 6 additions & 6 deletions src/FreeDSx/Ldap/Protocol/DecodeFailureResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public function __construct(
/**
* @throws EncoderException
*/
public function answer(MessageDecodeException $e): bool
public function answer(MessageDecodeException $e): DecodeFailureOutcome
{
// A response on the reserved message ID 0 would read as an unsolicited notification.
if ($e->getMessageId() === 0) {
return false;
return DecodeFailureOutcome::Unanswerable;
}

$response = $this->responseFactory->getDecodeFailureResponse(
Expand All @@ -69,20 +69,20 @@ public function answer(MessageDecodeException $e): bool
$this->record($e);
$this->queue->sendMessage($response);

return true;
return DecodeFailureOutcome::Answered;
}

/**
* A request refused for room that takes no response is recorded and dropped instead of ending the session.
*/
private function dropRefused(MessageDecodeException $e): bool
private function dropRefused(MessageDecodeException $e): DecodeFailureOutcome
{
if (!$e instanceof RequestRefusedException) {
return false;
return DecodeFailureOutcome::Unanswerable;
}
$this->record($e);

return true;
return DecodeFailureOutcome::Dropped;
}

private function record(MessageDecodeException $e): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* This file is part of the FreeDSx LDAP package.
*
* (c) Chad Sikorra <Chad.Sikorra@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FreeDSx\Ldap\Protocol\Decoder\Control;

use FreeDSx\Ldap\Operation\Request\RequestInterface;
use FreeDSx\Ldap\Server\Middleware\ServerControlRegistry;

use function array_flip;
use function array_intersect_key;

/**
* Decodes a request's controls, typing only the ones its operation takes and leaving the rest generic.
*
* @internal
* @author Chad Sikorra <Chad.Sikorra@gmail.com>
*/
class RequestControlDecoders
{
/**
* @var array<class-string<RequestInterface>, ControlDecoder>
*/
private array $byOperation = [];

/**
* @param array<string, ControlDecoderInterface> $typed the decoder of each typed control, keyed by its OID.
*/
public function __construct(
private readonly array $typed,
private readonly ServerControlRegistry $registry,
) {}

public function for(RequestInterface $request): ControlDecoder
{
// Keyed by class, as the operation alone decides which controls it takes.
return $this->byOperation[$request::class] ??= new ControlDecoder(array_intersect_key(
$this->typed,
array_flip($this->registry->controlsTakenBy($request)),
));
}
}
32 changes: 21 additions & 11 deletions src/FreeDSx/Ldap/Protocol/Decoder/MessageDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
public function __construct(
private MessageEnvelopeReader $envelopes,
private array $operations,
private ControlDecoderInterface $controls,
) {}

/**
Expand All @@ -58,18 +57,22 @@ public function decode(

[$operation, $controls] = $this->envelopes->answerable(
$envelope,
fn(): array => [
$decoder->decode(
function () use ($decoder, $envelope, $context): array {
$operation = $decoder->decode(
$envelope->operation,
$context,
),
$this->envelopes->controlsOf(
$envelope,
$context,
$this->controls,
$this->maxControls(),
),
],
);

return [
$operation,
$this->envelopes->controlsOf(
$envelope,
$context,
$this->controlsFor($operation),
$this->maxControls(),
),
];
},
);

return $this->message(
Expand All @@ -87,6 +90,13 @@ protected function maxControls(): ?int
return null;
}

/**
* The decoder for the controls of a decoded operation.
*
* @param TOperation $operation
*/
abstract protected function controlsFor(ProtocolElementInterface $operation): ControlDecoderInterface;

/**
* Wraps the decoded operation and its controls in the message this side receives.
*
Expand Down
Loading
Loading