Re: [LEDE-DEV] [PATCH v1] dnsmasq: use SIGINT for dnssec time valid

2018-01-15 Thread Hans Dedecker
On Mon, Jan 15, 2018 at 1:45 PM, Kevin Darbyshire-Bryant
 wrote:
> Dnsmasq used SIGHUP to do too many things: 1) set dnssec time validation
> enabled, 2) bump SOA zone serial, 3) clear dns cache, 4) reload hosts
> files, 5) reload resolvers/servers files.
>
> Many subsystems within LEDE can send SIGHUP to dnsmasq: 1) ntpd hotplug
> (to indicate time is valid for dnssec) 2) odhcpd (to indicate a
> new/removed host - typically DHCPv6 leases) 3) procd on interface state
> changes 4) procd on system config state changes, 5) service reload.
>
> If dnssec time validation is enabled before the system clock has been
> set to a sensible time, name resolution will fail.  Because name
> resolution fails, ntpd is unable to resolve time server names to
> addresses, so is unable to set time.  Classic chicken/egg.
>
> Since commits 23bba9cb330cd298739a16e350b0029ed9429eef (service reload) &
> 4f02285d8b4a66359a8fa46f22a3efde391b5419 (system config)  make it more
> likely a SIGHUP will be sent for events other than 'ntpd has set time'
> it is more likely that an errant 'name resolution is failing for
> everything' situation will be encountered.
>
> Fortunately the upstream dnsmasq people agree and have moved 'check
> dnssec timestamp enable' from SIGHUP handler to SIGINT.
>
> Backport the upstream patch to use SIGINT.
> ntpd hotplug script updated to use SIGINT.
>
> Signed-off-by: Kevin Darbyshire-Bryant 
> ---
>  package/network/services/dnsmasq/Makefile  |   2 +-
>  .../services/dnsmasq/files/dnsmasqsec.hotplug  |   2 +-
>  .../dnsmasq/patches/260-dnssec-SIGINT.patch| 120 
> +
>  3 files changed, 122 insertions(+), 2 deletions(-)
>  create mode 100644 
> package/network/services/dnsmasq/patches/260-dnssec-SIGINT.patch
>
> diff --git a/package/network/services/dnsmasq/Makefile 
> b/package/network/services/dnsmasq/Makefile
> index c6d2739f03..1224ad86f8 100644
> --- a/package/network/services/dnsmasq/Makefile
> +++ b/package/network/services/dnsmasq/Makefile
> @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
>
>  PKG_NAME:=dnsmasq
>  PKG_VERSION:=2.78
> -PKG_RELEASE:=7
> +PKG_RELEASE:=8
>
>  PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
>  PKG_SOURCE_URL:=http://thekelleys.org.uk/dnsmasq/
> diff --git a/package/network/services/dnsmasq/files/dnsmasqsec.hotplug 
> b/package/network/services/dnsmasq/files/dnsmasqsec.hotplug
> index a155eb0f6e..781d533734 100644
> --- a/package/network/services/dnsmasq/files/dnsmasqsec.hotplug
> +++ b/package/network/services/dnsmasq/files/dnsmasqsec.hotplug
> @@ -9,6 +9,6 @@ TIMEVALIDFILE="/var/state/dnsmasqsec"
>  [ -f "$TIMEVALIDFILE" ] || {
> echo "ntpd says time is valid" >$TIMEVALIDFILE
> /etc/init.d/dnsmasq enabled && {
> -   procd_send_signal dnsmasq
> +   procd_send_signal dnsmasq '*' INT
> }
>  }
> diff --git a/package/network/services/dnsmasq/patches/260-dnssec-SIGINT.patch 
> b/package/network/services/dnsmasq/patches/260-dnssec-SIGINT.patch
> new file mode 100644
> index 00..e280142f75
> --- /dev/null
> +++ b/package/network/services/dnsmasq/patches/260-dnssec-SIGINT.patch
> @@ -0,0 +1,120 @@
> +From 3c973ad92d317df736d5a8fde67baba6b102d91e Mon Sep 17 00:00:00 2001
> +From: Simon Kelley 
> +Date: Sun, 14 Jan 2018 21:05:37 +
> +Subject: [PATCH] Use SIGINT (instead of overloading SIGHUP) to turn on DNSSEC
> + time validation.
> +
> +---
> + src/dnsmasq.c |   36 +---
> + src/dnsmasq.h |1 +
> + src/helper.c  |3 ++-
> + 5 files changed, 38 insertions(+), 14 deletions(-)
> +
> +--- a/src/dnsmasq.c
>  b/src/dnsmasq.c
> +@@ -137,7 +137,8 @@ int main (int argc, char **argv)
> +   sigaction(SIGTERM, , NULL);
> +   sigaction(SIGALRM, , NULL);
> +   sigaction(SIGCHLD, , NULL);
> +-
> ++  sigaction(SIGINT, , NULL);
> ++
> +   /* ignore SIGPIPE */
> +   sigact.sa_handler = SIG_IGN;
> +   sigaction(SIGPIPE, , NULL);
> +@@ -815,7 +816,7 @@ int main (int argc, char **argv)
> +
> +   daemon->dnssec_no_time_check = option_bool(OPT_DNSSEC_TIME);
> +   if (option_bool(OPT_DNSSEC_TIME) && !daemon->back_to_the_future)
> +-  my_syslog(LOG_INFO, _("DNSSEC signature timestamps not checked until 
> first cache reload"));
> ++  my_syslog(LOG_INFO, _("DNSSEC signature timestamps not checked until 
> receipt of SIGINT"));
> +
> +   if (rc == 1)
> +   my_syslog(LOG_INFO, _("DNSSEC signature timestamps not checked until 
> system time valid"));
> +@@ -1142,7 +1143,7 @@ static void sig_handler(int sig)
> + {
> +   /* ignore anything other than TERM during startup
> +and in helper proc. (helper ignore TERM too) */
> +-  if (sig == SIGTERM)
> ++  if (sig == SIGTERM || sig == SIGINT)
> +   exit(EC_MISC);
> + }
> +   else if (pid != getpid())
> +@@ -1168,6 +1169,15 @@ static void sig_handler(int sig)
> +   event = EVENT_DUMP;
> +   

[LEDE-DEV] [PATCH v1] dnsmasq: use SIGINT for dnssec time valid

2018-01-15 Thread Kevin Darbyshire-Bryant
Dnsmasq used SIGHUP to do too many things: 1) set dnssec time validation
enabled, 2) bump SOA zone serial, 3) clear dns cache, 4) reload hosts
files, 5) reload resolvers/servers files.

Many subsystems within LEDE can send SIGHUP to dnsmasq: 1) ntpd hotplug
(to indicate time is valid for dnssec) 2) odhcpd (to indicate a
new/removed host - typically DHCPv6 leases) 3) procd on interface state
changes 4) procd on system config state changes, 5) service reload.

If dnssec time validation is enabled before the system clock has been
set to a sensible time, name resolution will fail.  Because name
resolution fails, ntpd is unable to resolve time server names to
addresses, so is unable to set time.  Classic chicken/egg.

Since commits 23bba9cb330cd298739a16e350b0029ed9429eef (service reload) &
4f02285d8b4a66359a8fa46f22a3efde391b5419 (system config)  make it more
likely a SIGHUP will be sent for events other than 'ntpd has set time'
it is more likely that an errant 'name resolution is failing for
everything' situation will be encountered.

Fortunately the upstream dnsmasq people agree and have moved 'check
dnssec timestamp enable' from SIGHUP handler to SIGINT.

Backport the upstream patch to use SIGINT.
ntpd hotplug script updated to use SIGINT.

Signed-off-by: Kevin Darbyshire-Bryant 
---
 package/network/services/dnsmasq/Makefile  |   2 +-
 .../services/dnsmasq/files/dnsmasqsec.hotplug  |   2 +-
 .../dnsmasq/patches/260-dnssec-SIGINT.patch| 120 +
 3 files changed, 122 insertions(+), 2 deletions(-)
 create mode 100644 
package/network/services/dnsmasq/patches/260-dnssec-SIGINT.patch

diff --git a/package/network/services/dnsmasq/Makefile 
b/package/network/services/dnsmasq/Makefile
index c6d2739f03..1224ad86f8 100644
--- a/package/network/services/dnsmasq/Makefile
+++ b/package/network/services/dnsmasq/Makefile
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=dnsmasq
 PKG_VERSION:=2.78
-PKG_RELEASE:=7
+PKG_RELEASE:=8
 
 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
 PKG_SOURCE_URL:=http://thekelleys.org.uk/dnsmasq/
diff --git a/package/network/services/dnsmasq/files/dnsmasqsec.hotplug 
b/package/network/services/dnsmasq/files/dnsmasqsec.hotplug
index a155eb0f6e..781d533734 100644
--- a/package/network/services/dnsmasq/files/dnsmasqsec.hotplug
+++ b/package/network/services/dnsmasq/files/dnsmasqsec.hotplug
@@ -9,6 +9,6 @@ TIMEVALIDFILE="/var/state/dnsmasqsec"
 [ -f "$TIMEVALIDFILE" ] || {
echo "ntpd says time is valid" >$TIMEVALIDFILE
/etc/init.d/dnsmasq enabled && {
-   procd_send_signal dnsmasq
+   procd_send_signal dnsmasq '*' INT
}
 }
diff --git a/package/network/services/dnsmasq/patches/260-dnssec-SIGINT.patch 
b/package/network/services/dnsmasq/patches/260-dnssec-SIGINT.patch
new file mode 100644
index 00..e280142f75
--- /dev/null
+++ b/package/network/services/dnsmasq/patches/260-dnssec-SIGINT.patch
@@ -0,0 +1,120 @@
+From 3c973ad92d317df736d5a8fde67baba6b102d91e Mon Sep 17 00:00:00 2001
+From: Simon Kelley 
+Date: Sun, 14 Jan 2018 21:05:37 +
+Subject: [PATCH] Use SIGINT (instead of overloading SIGHUP) to turn on DNSSEC
+ time validation.
+
+---
+ src/dnsmasq.c |   36 +---
+ src/dnsmasq.h |1 +
+ src/helper.c  |3 ++-
+ 5 files changed, 38 insertions(+), 14 deletions(-)
+
+--- a/src/dnsmasq.c
 b/src/dnsmasq.c
+@@ -137,7 +137,8 @@ int main (int argc, char **argv)
+   sigaction(SIGTERM, , NULL);
+   sigaction(SIGALRM, , NULL);
+   sigaction(SIGCHLD, , NULL);
+-
++  sigaction(SIGINT, , NULL);
++  
+   /* ignore SIGPIPE */
+   sigact.sa_handler = SIG_IGN;
+   sigaction(SIGPIPE, , NULL);
+@@ -815,7 +816,7 @@ int main (int argc, char **argv)
+   
+   daemon->dnssec_no_time_check = option_bool(OPT_DNSSEC_TIME);
+   if (option_bool(OPT_DNSSEC_TIME) && !daemon->back_to_the_future)
+-  my_syslog(LOG_INFO, _("DNSSEC signature timestamps not checked until 
first cache reload"));
++  my_syslog(LOG_INFO, _("DNSSEC signature timestamps not checked until 
receipt of SIGINT"));
+   
+   if (rc == 1)
+   my_syslog(LOG_INFO, _("DNSSEC signature timestamps not checked until 
system time valid"));
+@@ -1142,7 +1143,7 @@ static void sig_handler(int sig)
+ {
+   /* ignore anything other than TERM during startup
+and in helper proc. (helper ignore TERM too) */
+-  if (sig == SIGTERM)
++  if (sig == SIGTERM || sig == SIGINT)
+   exit(EC_MISC);
+ }
+   else if (pid != getpid())
+@@ -1168,6 +1169,15 @@ static void sig_handler(int sig)
+   event = EVENT_DUMP;
+   else if (sig == SIGUSR2)
+   event = EVENT_REOPEN;
++  else if (sig == SIGINT)
++  {
++/* Handle SIGINT normally in debug mode, so
++   ctrl-c continues to operate. */
++if (option_bool(OPT_DEBUG))
++  exit(EC_MISC);
++else
++  event =