Hey again,
so, I found a nice and reliable solution 
Basically, the trick is to use nslookup to lookup the ip addresses of the NTP servers' domain names. With nslookup you can bypass dnsmasq and dnssec validation
Try for example:
nslookup www.openwrt.org 208.67.222.222
It will lookup www.openwrt.org using the DNS server 208.67.222.222. It also works when dnsmasq would not return any IP because of an incorrect system time.
With this neat workaround at hand, I wrote an init script that will keep the system time in sync when dnsseccheckunsigned is enabled. What it basically does is:
1) Ping the specified DNS servers to see if the internet connection is up and DNS servers available
2) Lookup the IP addresses of the specified NTP servers using the specified DNS servers
3) Use the retrieved IP addresses with ntpd to sync the time
4) After a successfull sync, restart dnsmasq to ensure it runs with DNSSEC validation enforced
In addition, the script does a lot of error handling:
- If any of the steps 1-3 fails, it will pause for a bit and retry until the retry limit is reached
- If it ultimately fails and reaches the retry limit (e.g. if the internet connection is down), we can enter a "enter a "fallback" mode, if it is enabled (option FALLBACK=1). Fallback mode means, the script will disable dnsseccheckunsigned and restart dnsmasq. It will also create a temporary file to keep track of the fallback state. If the system is shut down and the temporary file is found, dnsseccheckunsigned will be reenabled for the next boot.
- I also implemented a command "enforce" to be used in conjunction with the fallback mode. The enforce command will check if the fallback mode was entered (checks for the temporary file that's created when fallback mode is entered), try to sync time and if successful, reenable dnsseccheckunsigned and restart dnsmasq (and remove the temporary file). A good idea would be to add something like this to your crontab:
0 * * * * [ -e /tmp/dnssectime-fallback ] && /etc/init.d/dnssectime enforce
This way, dnsseccheckunsigned will be reenabled as soon as possible (checked every hour) and not only when the system is shut down.
- Last but not least, there is the possibility to automatically reenable dnsseccheckunsigned during startup if it's found disabled (option FORCE_DNSSEC=1). This is useful in case the fallback mode was active just before a power loss or hard reset of the router.
All that should help to make this a really robust solution and to cover even unlikely errors and to keep dnsmasq running reliably.
Plus, this init script logs all actions or errors to make it easier to debug (or just to verify everythings running well). You can use
logread | grep dnssectime
to view the log messages.
The script has a few configurable options in its upper section to make it portable and customizable.
The script can be found here: http://pastebin.com/SuYmKPB7
Please give it a try. I'd appreciate any feedback.
Limitations:
- Obviously, the code is not the most elegant or efficient. But it should just work. I might clean it up/streamline it later.
- At this point, the code only uses IPv4 addresses. Simply because almost everybody should have a working IPv4 connection, but not everybody might have a working IPv6 connection. Plus, I didn't even check how nslookup and ntpd handle IPv6 addresses.
- Another idea I have is to use uci to query the DNS and NTP servers in the router configuration, instead of having the user hardcode them in this init script. But that's not a priority, especially since not everybody defines DNS servers in their configuration (but receives them via DHCP instead).
- The script expects the option dnsmasqcheckunsigned to be found as dhcp.@dnsmasq[0].dnsseccheckunsigned in the uci system. I don't know if a different setup where the option would appear as e.g. dhcp.@dnsmasq[2].dnsseccheckunsigned is even possible. But if it is, the relevant uci calls would have to be adjusted. (Btw. if no occurrence of dnsseccheckunsigned is found in uci at all - i.e. it is neither 0 or 1, but undefined - the script will skip execution and just print a notice to the system log.)
Regards,
Timo
(Last edited by silentcreek on 4 Feb 2016, 15:14)