Merge branch 'dhcp-to-dns' into next

This commit is contained in:
Christian Hesse 2023-04-24 21:57:32 +02:00
commit 19eb8653af
5 changed files with 51 additions and 37 deletions

View file

@ -13,10 +13,7 @@
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } :while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:global Domain; :global Domain;
:global HostNameInZone;
:global Identity; :global Identity;
:global PrefixInZone;
:global ServerNameInZone;
:global CharacterReplace; :global CharacterReplace;
:global EitherOr; :global EitherOr;
@ -27,9 +24,6 @@
$ScriptLock $0 false 10; $ScriptLock $0 false 10;
:local Zone \
([ $IfThenElse ($PrefixInZone = true) "dhcp." ] . \
[ $IfThenElse ($HostNameInZone = true) ($Identity . ".") ] . $Domain);
:local Ttl 5m; :local Ttl 5m;
:local CommentPrefix ("managed by " . $0 . " for "); :local CommentPrefix ("managed by " . $0 . " for ");
:local CommentString ("--- " . $0 . " above ---"); :local CommentString ("--- " . $0 . " above ---");
@ -49,7 +43,7 @@ $ScriptLock $0 false 10;
:local Found false; :local Found false;
$LogPrintExit2 info $0 ("Lease expired for " . $MacAddress . " (" . $DnsRecordVal->"name" . "), deleting DNS entry.") false; $LogPrintExit2 info $0 ("Lease expired for " . $MacAddress . " (" . $DnsRecordVal->"name" . "), deleting DNS entry.") false;
/ip/dns/static/remove $DnsRecord; /ip/dns/static/remove $DnsRecord;
/ip/dns/static/remove [ find where type=CNAME cname=($DnsRecordVal->"name") comment=($DnsRecordVal->"comment") ]; /ip/dns/static/remove [ find where type=CNAME comment=($DnsRecordVal->"comment") ];
} }
} }
@ -57,6 +51,11 @@ $ScriptLock $0 false 10;
:local LeaseVal; :local LeaseVal;
:do { :do {
:set LeaseVal [ /ip/dhcp-server/lease/get $Lease ]; :set LeaseVal [ /ip/dhcp-server/lease/get $Lease ];
:local DupMacLeases [ /ip/dhcp-server/lease/find where mac-address=($LeaseVal->"mac-address") status=bound ];
:if ([ :len $DupMacLeases ] > 1) do={
$LogPrintExit2 debug $0 ("Multiple bound leases found for mac-address " . ($LeaseVal->"mac-address") . ", using last one.") false;
:set LeaseVal [ /ip/dhcp-server/lease/get ($DupMacLeases->([ :len $DupMacLeases ] - 1)) ];
}
} on-error={ } on-error={
$LogPrintExit2 debug $0 ("A lease just vanished, ignoring.") false; $LogPrintExit2 debug $0 ("A lease just vanished, ignoring.") false;
} }
@ -65,39 +64,41 @@ $ScriptLock $0 false 10;
:local Comment ($CommentPrefix . $LeaseVal->"mac-address"); :local Comment ($CommentPrefix . $LeaseVal->"mac-address");
:local MacDash [ $CharacterReplace ($LeaseVal->"mac-address") ":" "-" ]; :local MacDash [ $CharacterReplace ($LeaseVal->"mac-address") ":" "-" ];
:local HostName [ $CharacterReplace [ $EitherOr ([ $ParseKeyValueStore ($LeaseVal->"comment") ]->"hostname") ($LeaseVal->"host-name") ] " " "" ]; :local HostName [ $CharacterReplace [ $EitherOr ([ $ParseKeyValueStore ($LeaseVal->"comment") ]->"hostname") ($LeaseVal->"host-name") ] " " "" ];
:local Domain ([ $IfThenElse ($ServerNameInZone = true) ($LeaseVal->"server" . ".") ] . $Zone); :local Network [ /ip/dhcp-server/network/find where ($LeaseVal->"address") in address ];
:local NetworkVal;
:if ([ :len $Network ] > 0) do={
:set NetworkVal [ /ip/dhcp-server/network/get ($Network->0) ];
}
:local NetworkInfo [ $ParseKeyValueStore ($NetworkVal->"comment") ];
:local NetDomain ([ $IfThenElse ([ :len ($NetworkInfo->"name-extra") ] > 0) ($NetworkInfo->"name-extra" . ".") ] . \
[ $EitherOr [ $EitherOr ($NetworkInfo->"domain") ($NetworkVal->"domain") ] $Domain ]);
:local DnsRecord [ /ip/dns/static/find where name=($MacDash . "." . $Domain) ]; :local DnsRecord [ /ip/dns/static/find where comment=$Comment (!type or type=A) ];
:if ([ :len $DnsRecord ] > 0) do={ :if ([ :len $DnsRecord ] > 0) do={
:local DnsIp [ /ip/dns/static/get $DnsRecord address ]; :local DnsRecordVal [ /ip/dns/static/get $DnsRecord ];
:local DupMacLeases [ /ip/dhcp-server/lease/find where mac-address=($LeaseVal->"mac-address") status=bound ]; :if ($DnsRecordVal->"address" = $LeaseVal->"address" && $DnsRecordVal->"name" = ($MacDash . "." . $NetDomain)) do={
:if ([ :len $DupMacLeases ] > 1) do={ $LogPrintExit2 debug $0 ("DNS entry for " . $LeaseVal->"mac-address" . " does not need updating.") false;
$LogPrintExit2 debug $0 ("Multiple bound leases found for mac-address " . ($LeaseVal->"mac-address") . ", using ip address of last one.") false;
:set ($LeaseVal->"address") [ /ip/dhcp-server/lease/get ($DupMacLeases->([ :len $DupMacLeases ] - 1)) address ];
}
:if ($DnsIp = $LeaseVal->"address") do={
$LogPrintExit2 debug $0 ("DNS entry for " . ($MacDash . "." . $Domain) . " does not need updating.") false;
} else={ } else={
$LogPrintExit2 info $0 ("Replacing DNS entry for " . ($MacDash . "." . $Domain) . ", new address is " . $LeaseVal->"address" . ".") false; $LogPrintExit2 info $0 ("Replacing DNS entry for " . $LeaseVal->"mac-address" . " (" . ($MacDash . "." . $NetDomain) . " -> " . $LeaseVal->"address" . ").") false;
/ip/dns/static/set address=($LeaseVal->"address") $DnsRecord; /ip/dns/static/set address=($LeaseVal->"address") name=($MacDash . "." . $NetDomain) $DnsRecord;
} }
:local Cname [ /ip/dns/static/find where type=CNAME cname=($MacDash . "." . $Domain) comment=$Comment ]; :local Cname [ /ip/dns/static/find where comment=$Comment type=CNAME ];
:if ([ :len $Cname ] = 0 && [ :len $HostName ] > 0) do={ :if ([ :len $Cname ] = 0 && [ :len $HostName ] > 0) do={
$LogPrintExit2 info $0 ("Host name appeared, adding CNAME " . ($HostName . "." . $Domain) . " pointing to " . ($MacDash . "." . $Domain) . ".") false; $LogPrintExit2 info $0 ("Host name appeared, adding CNAME (" . ($HostName . "." . $NetDomain) . " -> " . ($MacDash . "." . $NetDomain) . ").") false;
/ip/dns/static/add name=($HostName . "." . $Domain) type=CNAME cname=($MacDash . "." . $Domain) ttl=$Ttl comment=$Comment place-before=$PlaceBefore; /ip/dns/static/add name=($HostName . "." . $NetDomain) type=CNAME cname=($MacDash . "." . $NetDomain) ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
} }
:if ([ :len $Cname ] > 0 && [ /ip/dns/static/get $Cname name ] != ($HostName . "." . $Domain)) do={ :if ([ :len $Cname ] > 0 && [ /ip/dns/static/get $Cname name ] != ($HostName . "." . $NetDomain)) do={
$LogPrintExit2 info $0 ("Host name changed, updating CNAME (pointing to " . ($MacDash . "." . $Domain) . ") to " . ($HostName . "." . $Domain) . ".") false; $LogPrintExit2 info $0 ("Host name or domain changed, updating CNAME (" . ($HostName . "." . $NetDomain) . " -> " . ($MacDash . "." . $NetDomain) . ").") false;
/ip/dns/static/set name=($HostName . "." . $Domain) $Cname; /ip/dns/static/set name=($HostName . "." . $NetDomain) cname=($MacDash . "." . $NetDomain) $Cname;
} }
} else={ } else={
$LogPrintExit2 info $0 ("Adding new DNS entry for " . ($MacDash . "." . $Domain) . ", address is " . $LeaseVal->"address" . ".") false; $LogPrintExit2 info $0 ("Adding new DNS entry for " . $LeaseVal->"mac-address" . " (" . ($MacDash . "." . $NetDomain) . " -> " . $LeaseVal->"address" . ").") false;
/ip/dns/static/add name=($MacDash . "." . $Domain) type=A address=($LeaseVal->"address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore; /ip/dns/static/add name=($MacDash . "." . $NetDomain) type=A address=($LeaseVal->"address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
:if ([ :len $HostName ] > 0) do={ :if ([ :len $HostName ] > 0) do={
/ip/dns/static/add name=($HostName . "." . $Domain) type=CNAME cname=($MacDash . "." . $Domain) ttl=$Ttl comment=$Comment place-before=$PlaceBefore; $LogPrintExit2 info $0 ("Adding new CNAME (" . ($HostName . "." . $NetDomain) . " -> " . ($MacDash . "." . $NetDomain) . ").") false;
/ip/dns/static/add name=($HostName . "." . $NetDomain) type=CNAME cname=($MacDash . "." . $NetDomain) ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
} }
} }
} else={ } else={

View file

@ -34,12 +34,27 @@ On first run a disabled static dns record acting as marker (with comment
"`--- dhcp-to-dns above ---`") is added. Move this entry to define where new "`--- dhcp-to-dns above ---`") is added. Move this entry to define where new
entries are to be added. entries are to be added.
The configuration goes to `global-config-overlay`, these are the parameters: The configuration goes to dhcp server's network definition. The domain is
used to form the dns name:
/ip/dhcp-server/network/add address=10.0.0.0/24 domain=example.com;
A bound lease for mac address `00:11:22:33:44:55` with ip address
`10.0.0.50` would result in an A record `00-11-22-33-44-55.example.com`
pointing to the given ip address.
Additional options can be given from comment, to add an extra level in
dns name or define a different domain.
/ip/dhcp-server/network/add address=10.0.0.0/24 domain=example.com comment="domain=another-domain.com, name-extra=dhcp";
This example would result in name `00-11-22-33-44-55.dhcp.another-domain.com`
for the same lease.
If no domain is found in dhcp server's network definition a fallback from
`global-config-overlay` is used. This is the parameter:
* `Domain`: the domain used for dns records * `Domain`: the domain used for dns records
* `HostNameInZone`: whether or not to add the dhcp/dns server's hostname
* `PrefixInZone`: whether or not to add prefix `dhcp`
* `ServerNameInZone`: whether or not to add DHCP server name
### Host name from DHCP lease comment ### Host name from DHCP lease comment

View file

@ -14,9 +14,6 @@
# This is used for DNS and backup file. # This is used for DNS and backup file.
:global Domain "example.com"; :global Domain "example.com";
:global HostNameInZone true;
:global PrefixInZone true;
:global ServerNameInZone false;
# You can send e-mail notifications. Configure the system's mail settings # You can send e-mail notifications. Configure the system's mail settings
# (/tool/e-mail), then install the module: # (/tool/e-mail), then install the module:

View file

@ -12,7 +12,7 @@
:local 0 "global-functions"; :local 0 "global-functions";
# expected configuration version # expected configuration version
:global ExpectedConfigVersion 98; :global ExpectedConfigVersion 99;
# global variables not to be changed by user # global variables not to be changed by user
:global GlobalFunctionsReady false; :global GlobalFunctionsReady false;

View file

@ -12,6 +12,7 @@
96="Added support for notes in 'netwatch-notify', these are included verbatim into the notification."; 96="Added support for notes in 'netwatch-notify', these are included verbatim into the notification.";
97="Modified 'dhcp-to-dns' to always add A records for names with mac address, and optionally add CNAME records if the host name is available."; 97="Modified 'dhcp-to-dns' to always add A records for names with mac address, and optionally add CNAME records if the host name is available.";
98="Extended 'check-certificates' to download new certificate by SubjectAltNames if download by CommonName fails."; 98="Extended 'check-certificates' to download new certificate by SubjectAltNames if download by CommonName fails.";
99="Modified 'dhcp-to-dns', which dropped global configuration. Settings moved to dhcp server's network definitions.";
}; };
# Migration steps to be applied on script updates # Migration steps to be applied on script updates