dhcrelay: split out plugin file, move some more around #6983

This commit is contained in:
Franco Fichtner 2024-03-13 16:56:15 +01:00
parent 0256a2754f
commit 53a95b0190
4 changed files with 137 additions and 91 deletions

1
plist
View File

@ -21,6 +21,7 @@
/usr/local/etc/inc/plugins.inc
/usr/local/etc/inc/plugins.inc.d/core.inc
/usr/local/etc/inc/plugins.inc.d/dhcpd.inc
/usr/local/etc/inc/plugins.inc.d/dhcrelay.inc
/usr/local/etc/inc/plugins.inc.d/dnsmasq.inc
/usr/local/etc/inc/plugins.inc.d/dpinger.inc
/usr/local/etc/inc/plugins.inc.d/ipfw.inc

View File

@ -343,7 +343,7 @@ function core_syslog()
$logfacilities['audit'] = array('facility' => array('audit'));
$logfacilities['configd'] = array('facility' => array('configd.py'));
$logfacilities['dhcpd'] = array('facility' => array('dhcpd', 'dhcrelay'));
$logfacilities['dhcpd'] = array('facility' => array('dhcpd'));
$logfacilities['lighttpd'] = array('facility' => array('lighttpd'));
$logfacilities['pkg'] = array('facility' => array('pkg', 'pkg-static'));
$logfacilities['portalauth'] = array('facility' => array('captiveportal'));
@ -398,12 +398,6 @@ function core_xmlrpc_sync()
'id' => 'dhcpd',
'services' => ["dhcpd"],
);
$result[] = array(
'description' => gettext('DHCPv4: Relay'),
'section' => 'dhcrelay,OPNsense.DHCRelay',
'id' => 'dhcrelay',
'services' => ["dhcrelay"],
);
$result[] = array(
'description' => gettext('DHCPDv6'),
'help' => gettext('Synchronize DHCPv6 including router advertisements.'),

View File

@ -32,10 +32,8 @@
function dhcpd_configure()
{
return [
'bootup' => ['dhcpd_dhcrelay_configure'],
'dhcp' => ['dhcpd_dhcp_configure:3'],
'dhcrelay' => ['dhcpd_dhcrelay_configure:3'],
'local' => ['dhcpd_dhcp_configure', 'dhcpd_dhcrelay_configure'],
'local' => ['dhcpd_dhcp_configure'],
];
}
@ -117,19 +115,6 @@ function dhcpd_services()
$services[] = $pconfig;
}
foreach ((new \OPNsense\DHCRelay\DHCRelay())->relays->iterateItems() as $relay) {
if ((string)$relay->enabled == '1') {
$pconfig = [];
$pconfig['name'] = 'dhcrelay';
$pconfig['description'] = gettext('DHCPv4 Relay') . " ({$relay->interface})";
$pconfig['php']['restart'] = ['dhcpd_dhcrelay4_configure'];
$pconfig['php']['start'] = ['dhcpd_dhcrelay4_configure'];
$pconfig['pidfile'] = "/var/run/dhcrelay-{$relay->getAttribute('uuid')}.pid";
$pconfig['id'] = $relay->getAttribute('uuid');
$services[] = $pconfig;
}
}
if (isset($config['dhcrelay6']['enable'])) {
$pconfig = array();
$pconfig['name'] = "dhcrelay6";
@ -1616,79 +1601,11 @@ EOD;
function dhcpd_dhcrelay_configure($verbose = false, $family = null, $id = null)
{
if ($family == null || $family == 'inet') {
dhcpd_dhcrelay4_configure($verbose, $id);
}
if ($family == null || $family == 'inet6') {
dhcpd_dhcrelay6_configure($verbose);
}
}
function dhcpd_dhcrelay4_configure($verbose = false, $id = null)
{
$mdl = new \OPNsense\DHCRelay\DHCRelay();
$relays = [];
foreach ($mdl->relays->iterateItems() as $relay) {
$target_id = $relay->getAttribute('uuid');
if ($id != null && $id != $target_id) {
continue;
}
if ((string)$relay->enabled == '1') {
$relays[] = $relay;
}
killbypid("/var/run/dhcrelay-{$target_id}.pid");
}
if (!count($relays)) {
return;
}
service_log('Starting DHCPv4 relays...', $verbose);
$iflist = get_configured_interface_with_descr();
$ifconfig_details = legacy_interfaces_details();
foreach ($relays as $relay) {
$interface = (string)$relay->interface;
$device = get_real_interface($interface);
if (!isset($iflist[$interface]) || !get_interface_ip($interface, $ifconfig_details)) {
log_msg("dhcpd_dhcrelay4_configure() found no IP address for $interface($device)", LOG_WARNING);
continue;
}
if (empty($device) || isset($ifconfig_details[$device]) && $ifconfig_details[$device]['macaddr'] == '00:00:00:00:00:00') {
log_msg("dhcpd_dhcrelay4_configure() found no ethernet address for $interface($device)", LOG_WARNING);
continue;
}
$destination = $mdl->getNodeByReference("destinations.{$relay->destination}");
if ($destination == null) {
log_msg("dhcpd_dhcrelay4_configure() found no destination server for $interface($device)", LOG_WARNING);
continue;
}
$cmd = [exec_safe('daemon -f -p %s', "/var/run/dhcrelay-{$relay->getAttribute('uuid')}.pid")];
$cmd[] = '/usr/local/sbin/dhcrelay -d';
if (!empty((string)$relay->agent_info)) {
$cmd[] = '-or';
}
$cmd[] = exec_safe('-i %s', $device);
foreach (explode(',', (string)$destination->server) as $server) {
$cmd[] = exec_safe('%s', $server);
}
mwexec(join(' ', $cmd));
}
service_log("done.\n", $verbose);
}
function dhcpd_dhcrelay6_configure($verbose = false)
{
global $config;

View File

@ -0,0 +1,134 @@
<?php
/*
* Copyright (C) 2024 Franco Fichtner <franco@opnsense.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
function dhcrelay_configure()
{
return [
'bootup' => ['dhcrelay_configure_do'],
'dhcrelay' => ['dhcrelay_configure_do:3'],
'local' => ['dhcrelay_configure_do'],
];
}
function dhcrelay_services()
{
$services = [];
foreach ((new \OPNsense\DHCRelay\DHCRelay())->relays->iterateItems() as $relay) {
if ((string)$relay->enabled == '1') {
$pconfig = [];
$pconfig['name'] = 'dhcrelay';
$pconfig['description'] = gettext('DHCPv4 Relay') . " ({$relay->interface})";
$pconfig['php']['restart'] = ['dhcrelay_configure_do'];
$pconfig['php']['start'] = ['dhcrelay_configure_do'];
$pconfig['pidfile'] = "/var/run/dhcrelay-{$relay->getAttribute('uuid')}.pid";
$pconfig['id'] = $relay->getAttribute('uuid');
$services[] = $pconfig;
}
}
return $services;
}
function dhcrelay_xmlrpc_sync()
{
$result = [];
$result[] = [
'description' => gettext('DHCPv4: Relay'),
'section' => 'dhcrelay,OPNsense.DHCRelay',
'services' => ['dhcrelay'],
'id' => 'dhcrelay',
];
return $result;
}
function dhcrelay_configure_do($verbose = false, $id = null)
{
$mdl = new \OPNsense\DHCRelay\DHCRelay();
$relays = [];
foreach ($mdl->relays->iterateItems() as $relay) {
$target_id = $relay->getAttribute('uuid');
if ($id != null && $id != $target_id) {
continue;
}
if ((string)$relay->enabled == '1') {
$relays[] = $relay;
}
killbypid("/var/run/dhcrelay-{$target_id}.pid");
}
if (!count($relays)) {
return;
}
service_log('Starting DHCP relays...', $verbose);
$iflist = get_configured_interface_with_descr();
$ifconfig_details = legacy_interfaces_details();
foreach ($relays as $relay) {
$interface = (string)$relay->interface;
$device = get_real_interface($interface);
if (!isset($iflist[$interface]) || !get_interface_ip($interface, $ifconfig_details)) {
log_msg("dhcrelay_configure_do() found no IP address for $interface($device)", LOG_WARNING);
continue;
}
if (empty($device) || isset($ifconfig_details[$device]) && $ifconfig_details[$device]['macaddr'] == '00:00:00:00:00:00') {
log_msg("dhcrelay_configure_do() found no ethernet address for $interface($device)", LOG_WARNING);
continue;
}
$destination = $mdl->getNodeByReference("destinations.{$relay->destination}");
if ($destination == null) {
log_msg("dhcrelay_configure_do() found no destination server for $interface($device)", LOG_WARNING);
continue;
}
$cmd = [exec_safe('daemon -f -p %s', "/var/run/dhcrelay-{$relay->getAttribute('uuid')}.pid")];
$cmd[] = '/usr/local/sbin/dhcrelay -d';
if (!empty((string)$relay->agent_info)) {
$cmd[] = '-or';
}
$cmd[] = exec_safe('-i %s', $device);
foreach (explode(',', (string)$destination->server) as $server) {
$cmd[] = exec_safe('%s', $server);
}
mwexec(join(' ', $cmd));
}
service_log("done.\n", $verbose);
}