Skip to content

Commit 1007b74

Browse files
dns/bind: Make builtin ACLs available
Makes the builtin ACLs (none, any. localhost and localnets) available for selection: 1. Created new custom field types: * AclField extending ArrayField * AclModelRelationField extending ModelRelationField * AclNetField extending NetworkField 2. Adds builtin ACLs as child nodes to ACL list via new AclField field type 3. Removes builtin name RegEx constraint from name field in Acl model 4. Ensures "any" and "none" builtins cannot be part of an ACL multi-select via new AclModelRelationField field type 5. Ensures network validation is skipped for builtin ACLs via new AclNetField field type 6. Updates the General and Domain models to use AclModelRelationField 7. Updates general.volt to: * Disable command buttons for builtin ACLs * Ensure the builtin ACLs are added to config.xml 8. Updates named.conf to exclude builtin ACLs from custom name list 9. Bumps model versions: * Acl to v1.0.1 * General to v1.0.13 * Domain to v1.1.3 Signed-off-by: benyamin-codez <[email protected]>
1 parent c3e9db2 commit 1007b74

File tree

8 files changed

+429
-13
lines changed

8 files changed

+429
-13
lines changed

dns/bind/src/opnsense/mvc/app/models/OPNsense/Bind/Acl.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<model>
22
<mount>//OPNsense/bind/acl</mount>
33
<description>BIND ACL configuration</description>
4-
<version>1.0.0</version>
4+
<version>1.0.1</version>
55
<items>
66
<acls>
7-
<acl type="ArrayField">
7+
<acl type=".\AclField">
88
<enabled type="BooleanField">
99
<Default>1</Default>
1010
<Required>Y</Required>
1111
</enabled>
1212
<name type="TextField">
1313
<Required>Y</Required>
14-
<Mask>/^(?!any$|localhost$|localnets$|none$)[0-9a-zA-Z_\-]{1,32}$/u</Mask>
15-
<ValidationMessage>Should be a string between 1 and 32 characters. Allowed characters are 0-9, a-z, A-Z, _ and -. Built-in ACL names must not be used: any, localhost, localnets, none.</ValidationMessage>
14+
<Mask>/^[0-9a-zA-Z_\-]{1,32}$/u</Mask>
15+
<ValidationMessage>Should be a string between 1 and 32 characters. Allowed characters are 0-9, a-z, A-Z, _ and -.</ValidationMessage>
1616
<Constraints>
1717
<check001>
1818
<ValidationMessage>An ACL with this name already exists.</ValidationMessage>
1919
<type>UniqueConstraint</type>
2020
</check001>
2121
</Constraints>
2222
</name>
23-
<networks type="NetworkField">
23+
<networks type=".\AclNetField">
2424
<Required>Y</Required>
2525
<AsList>Y</AsList>
2626
</networks>

dns/bind/src/opnsense/mvc/app/models/OPNsense/Bind/Domain.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<model>
22
<mount>//OPNsense/bind/domain</mount>
33
<description>BIND domain configuration</description>
4-
<version>1.1.2</version>
4+
<version>1.1.3</version>
55
<items>
66
<domains>
77
<domain type="ArrayField">
@@ -42,7 +42,7 @@
4242
<domainname type="TextField">
4343
<Required>Y</Required>
4444
</domainname>
45-
<allowtransfer type="ModelRelationField">
45+
<allowtransfer type=".\AclModelRelationField">
4646
<Model>
4747
<template>
4848
<source>OPNsense.Bind.Acl</source>
@@ -53,7 +53,7 @@
5353
<Multiple>Y</Multiple>
5454
</allowtransfer>
5555
<allowrndctransfer type="BooleanField"/>
56-
<allowquery type="ModelRelationField">
56+
<allowquery type=".\AclModelRelationField">
5757
<Model>
5858
<template>
5959
<source>OPNsense.Bind.Acl</source>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/*
4+
* Copyright (C) 2025 Deciso B.V.
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19+
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20+
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21+
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
namespace OPNsense\BIND\FieldTypes;
30+
31+
use OPNsense\Base\FieldTypes\ArrayField;
32+
33+
class ACLField extends ArrayField
34+
{
35+
/*
36+
* Extends ArrayField to programmatically add BIND's builtin ACL types to
37+
* the model. The private property $internalTemplateNode is duplicated.
38+
* The actionPostLoadingEvent() method is replaced to add the builtin ACLs
39+
* as child nodes. The ability to add static children is removed. The builtin
40+
* ACL names are defined by the static $builtinNames property. Values for the
41+
* builtin ACLs are populated by the getBuiltinChildren() method.
42+
*/
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
private $internalTemplateNode = null;
48+
49+
/**
50+
* @var list to define builtin BIND ACL names
51+
*/
52+
private static $builtinNames = ['none', 'localhost', 'localnets', 'any'];
53+
54+
/**
55+
* @return array of builtin BIND ACLs
56+
*/
57+
protected function getBuiltinChildren()
58+
{
59+
$builtins = [];
60+
foreach (self::$builtinNames as $aclName) {
61+
$builtins [] = [
62+
'enabled' => '1',
63+
'name' => $aclName,
64+
'networks' => 'system derived'
65+
];
66+
}
67+
return $builtins;
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
protected function actionPostLoadingEvent()
74+
{
75+
// always make sure there's a node to copy our structure from
76+
if ($this->internalTemplateNode == null) {
77+
$firstKey = array_keys($this->internalChildnodes)[0];
78+
$this->internalTemplateNode = $this->internalChildnodes[$firstKey];
79+
/**
80+
* if first node is empty, remove reference node.
81+
*/
82+
if ($this->internalChildnodes[$firstKey]->getInternalIsVirtual()) {
83+
unset($this->internalChildnodes[$firstKey]);
84+
}
85+
}
86+
87+
// init builtin entries returned by getBuiltinChildren()
88+
foreach (static::getBuiltinChildren() as $skey => $payload) {
89+
$nodeUUID = $this->generateUUID();
90+
$container_node = $this->newContainerField($this->__reference . "." . $nodeUUID, $this->internalXMLTagName);
91+
$container_node->setAttributeValue("uuid", $nodeUUID);
92+
$template_ref = $this->internalTemplateNode->__reference;
93+
foreach ($this->internalTemplateNode->iterateItems() as $key => $value) {
94+
if ($key == 'name') {
95+
foreach ($this->iterateItems() as $pkey => $pnode) {
96+
foreach ($pnode->iterateItems() as $subkey => $subnode) {
97+
if ($subkey == 'name' && $subnode == $payload[$key]) {
98+
// The builtin ACL already exists, let's skip it...
99+
continue 4;
100+
}
101+
}
102+
}
103+
}
104+
$node = clone $value;
105+
$node->setInternalReference($container_node->__reference . "." . $key);
106+
if (isset($payload[$key])) {
107+
$node->setValue($payload[$key]);
108+
}
109+
$node->setChanged();
110+
$container_node->addChildNode($key, $node);
111+
}
112+
$this->addChildNode($nodeUUID, $container_node);
113+
}
114+
}
115+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<?php
2+
3+
/*
4+
* Copyright (C) 2015-2025 Deciso B.V.
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19+
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20+
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21+
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
namespace OPNsense\BIND\FieldTypes;
30+
31+
use OPNsense\Base\Validators\CallbackValidator;
32+
use OPNsense\Base\FieldTypes\BaseListField;
33+
use OPNsense\Base\FieldTypes\ModelRelationField;
34+
35+
class AclModelRelationField extends ModelRelationField
36+
{
37+
/*
38+
* Extends ModelRelationField but all private properties and the private
39+
* member loadModelOptions() require duplication. Public methods
40+
* getNodeData() and getValidators() are altered to use the grandparent
41+
* BaseListField:: rather than parent::. The getValidators() method is
42+
* also modified to call new isValidComboSelection() method via new
43+
* CallbackValidator(). We also require public methods actionPostLoadingEvent()
44+
* and setModel() too for this to work.
45+
*/
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
private $internalIsSorted = false;
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
private $mdlStructure = null;
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
private $internalOptionsFromThisModel = false;
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
private $internalCacheKey = "";
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
private static $internalCacheOptionList = [];
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
private static $internalCacheModelStruct = [];
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
private function loadModelOptions($force = false)
81+
{
82+
// only collect options once per source/filter combination, we use a static to save our unique option
83+
// combinations over the running application.
84+
if (!isset(self::$internalCacheOptionList[$this->internalCacheKey]) || $force) {
85+
self::$internalCacheOptionList[$this->internalCacheKey] = [];
86+
foreach ($this->mdlStructure as $modelData) {
87+
// only handle valid model sources
88+
if (!isset($modelData['source']) || !isset($modelData['items']) || !isset($modelData['display'])) {
89+
continue;
90+
}
91+
$className = str_replace('.', '\\', $modelData['source']);
92+
$groupKey = isset($modelData['group']) ? $modelData['group'] : null;
93+
$displayKeys = explode(',', $modelData['display']);
94+
$displayFormat = !empty($modelData['display_format']) ? $modelData['display_format'] : "%s";
95+
96+
$searchItems = $this->getCachedData($className, $modelData['items'], $force);
97+
$groups = [];
98+
foreach ($searchItems as $uuid => $node) {
99+
$descriptions = [];
100+
foreach ($displayKeys as $displayKey) {
101+
$descriptions[] = $node['%' . $displayKey] ?? $node[$displayKey] ?? '';
102+
}
103+
if (isset($modelData['filters'])) {
104+
foreach ($modelData['filters'] as $filterKey => $filterValue) {
105+
$fieldData = $node[$filterKey] ?? null;
106+
if (!preg_match($filterValue, $fieldData) && $fieldData != null) {
107+
continue 2;
108+
}
109+
}
110+
}
111+
if (!empty($groupKey)) {
112+
if (!isset($node[$groupKey]) || isset($groups[$node[$groupKey]])) {
113+
continue;
114+
}
115+
$groups[$node[$groupKey]] = 1;
116+
}
117+
self::$internalCacheOptionList[$this->internalCacheKey][$uuid] = vsprintf(
118+
$displayFormat,
119+
$descriptions
120+
);
121+
}
122+
}
123+
124+
if (!$this->internalIsSorted) {
125+
natcasesort(self::$internalCacheOptionList[$this->internalCacheKey]);
126+
}
127+
}
128+
// Set for use in BaseListField->getNodeData()
129+
$this->internalOptionList = self::$internalCacheOptionList[$this->internalCacheKey];
130+
}
131+
132+
/**
133+
* {@inheritdoc}
134+
*/
135+
public function setModel($mdlStructure)
136+
{
137+
// only handle array type input
138+
if (!is_array($mdlStructure)) {
139+
return;
140+
} else {
141+
$this->mdlStructure = $mdlStructure;
142+
// set internal key for this node based on sources and filter criteria
143+
$this->internalCacheKey = md5(serialize($mdlStructure));
144+
}
145+
}
146+
147+
/**
148+
* {@inheritdoc}
149+
*/
150+
protected function actionPostLoadingEvent()
151+
{
152+
$this->loadModelOptions();
153+
}
154+
155+
/**
156+
* {@inheritdoc}
157+
*/
158+
public function getNodeData()
159+
{
160+
if ($this->internalIsSorted) {
161+
$optKeys = array_merge(explode(',', $this->internalValue), array_keys($this->internalOptionList));
162+
$ordered_option_list = [];
163+
foreach (array_unique($optKeys) as $key) {
164+
if (in_array($key, array_keys($this->internalOptionList))) {
165+
$ordered_option_list[$key] = $this->internalOptionList[$key];
166+
}
167+
}
168+
$this->internalOptionList = $ordered_option_list;
169+
}
170+
171+
return BaseListField::getNodeData();
172+
}
173+
174+
/**
175+
* @param string $input list of ACLs selected to validate
176+
* @return bool if valid combination of ACLs
177+
*/
178+
protected function isValidComboSelection($input)
179+
{
180+
if (strpos($input, ",") !== false) {
181+
// Pass validation if we only have a single-select.
182+
// Otherwise, get the ACL selection data and iterate to see if "any or "none" are included in the multi-select...
183+
$acls = $this->getNodeData();
184+
foreach ($acls as $node => $acl_sel_data) {
185+
if (($acl_sel_data['value'] == 'any' || $acl_sel_data['value'] == 'none') && $acl_sel_data['selected'] == '1') {
186+
$this->setValidationMessage("This ACL cannot be used in combination with others: " . $acl_sel_data['value']);
187+
return false;
188+
}
189+
}
190+
}
191+
return true;
192+
}
193+
194+
/**
195+
* {@inheritdoc}
196+
*/
197+
public function getValidators()
198+
{
199+
// Use validators from BaseListField, includes validations for multi-select, and single-select.
200+
$validators = BaseListField::getValidators();
201+
if ($this->internalValue != null) {
202+
// XXX: may be improved a bit to prevent the same object being constructed multiple times when used
203+
// in different fields (passing of $force parameter)
204+
$this->loadModelOptions($this->internalOptionsFromThisModel);
205+
$that = $this;
206+
$validators[] = new CallbackValidator(["callback" => function ($data) use ($that) {
207+
$messages = [];
208+
if (!$that->isValidComboSelection($data)) {
209+
$messages[] = $this->getValidationMessage();
210+
}
211+
return $messages;
212+
}]);
213+
}
214+
return $validators;
215+
}
216+
}

0 commit comments

Comments
 (0)