From ee94024dcaa59333f03d90f342e550a5f3ab40f2 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 13 Apr 2023 00:32:13 +0200 Subject: dhcp-to-dns: get domain from dhcp server's network definition --- dhcp-to-dns.rsc | 15 ++++++++------- doc/dhcp-to-dns.md | 23 +++++++++++++++++++---- global-config.rsc | 3 --- global-functions.rsc | 2 +- news-and-changes.rsc | 1 + 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/dhcp-to-dns.rsc b/dhcp-to-dns.rsc index d310a73..b15511e 100644 --- a/dhcp-to-dns.rsc +++ b/dhcp-to-dns.rsc @@ -13,10 +13,7 @@ :while ($GlobalFunctionsReady != true) do={ :delay 500ms; } :global Domain; -:global HostNameInZone; :global Identity; -:global PrefixInZone; -:global ServerNameInZone; :global CharacterReplace; :global EitherOr; @@ -27,9 +24,6 @@ $ScriptLock $0 false 10; -:local Zone \ - ([ $IfThenElse ($PrefixInZone = true) "dhcp." ] . \ - [ $IfThenElse ($HostNameInZone = true) ($Identity . ".") ] . $Domain); :local Ttl 5m; :local CommentPrefix ("managed by " . $0 . " for "); :local CommentString ("--- " . $0 . " above ---"); @@ -70,7 +64,14 @@ $ScriptLock $0 false 10; :local Comment ($CommentPrefix . $LeaseVal->"mac-address"); :local MacDash [ $CharacterReplace ($LeaseVal->"mac-address") ":" "-" ]; :local HostName [ $CharacterReplace [ $EitherOr ([ $ParseKeyValueStore ($LeaseVal->"comment") ]->"hostname") ($LeaseVal->"host-name") ] " " "" ]; - :local NetDomain ([ $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 comment=$Comment (!type or type=A) ]; :if ([ :len $DnsRecord ] > 0) do={ diff --git a/doc/dhcp-to-dns.md b/doc/dhcp-to-dns.md index a2caf0c..0c443a0 100644 --- a/doc/dhcp-to-dns.md +++ b/doc/dhcp-to-dns.md @@ -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 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 -* `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 diff --git a/global-config.rsc b/global-config.rsc index 770efd0..71be3f2 100644 --- a/global-config.rsc +++ b/global-config.rsc @@ -14,9 +14,6 @@ # This is used for DNS and backup file. :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 # (/tool/e-mail), then install the module: diff --git a/global-functions.rsc b/global-functions.rsc index 45fda9f..e6b1d36 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -12,7 +12,7 @@ :local 0 "global-functions"; # expected configuration version -:global ExpectedConfigVersion 98; +:global ExpectedConfigVersion 99; # global variables not to be changed by user :global GlobalFunctionsReady false; diff --git a/news-and-changes.rsc b/news-and-changes.rsc index 08ff3fb..7087c6c 100644 --- a/news-and-changes.rsc +++ b/news-and-changes.rsc @@ -12,6 +12,7 @@ 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."; 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 -- cgit v1.2.3-70-g09d2