You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.0 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2011 OpenWrt.org
  3. START=65
  4. STOP=65
  5. USE_PROCD=1
  6. PROG=/sbin/ntpd
  7. HOTPLUG_HELPER=/usr/sbin/ntpd.hotplug-helper
  8. config_file=/var/etc/ntpd.conf
  9. trunc() {
  10. echo -n "" > $config_file
  11. }
  12. emit() {
  13. echo -e "$@" >> $config_file
  14. }
  15. validate_ntp_section() {
  16. uci_load_validate system timeserver "$1" "$2" \
  17. 'server:list(host)' 'enabled:bool:1' 'enable_server:bool:0' \
  18. 'interface:list(string)'
  19. }
  20. start_ntpd_instance() {
  21. local intf i
  22. [ "$2" = 0 ] || {
  23. echo "validation failed"
  24. return 1
  25. }
  26. [ "$enabled" = 0 ] && return
  27. [ -z "$server" -a "$enable_server" = 0 ] && return
  28. # not sure that the interfaces enumerated should be validated,
  29. # since some of them might be dynamic interfaces (like IPsec
  30. # tunnels) which aren't known by ubus.
  31. mkdir -p "$(dirname "$config_file")"
  32. trunc
  33. emit "driftfile /var/lib/ntp/ntp.drift\n"
  34. if [ "$enable_server" != 0 ]; then
  35. emit "restrict default limited kod nomodify notrap nopeer"
  36. emit "restrict -6 default limited kod nomodify notrap nopeer"
  37. else
  38. emit "restrict -4 default noserve"
  39. emit "restrict -6 default noserve"
  40. fi
  41. emit "restrict source noquery"
  42. emit "\n# No limits for local monitoring"
  43. emit "restrict 127.0.0.1"
  44. emit "restrict -6 ::1\n"
  45. if [ -n "$interface" ]; then
  46. local loopback=$(ubus call network.interface dump | jsonfilter -e "@.interface[@.interface='loopback']['device']")
  47. local saw_lo=
  48. for intf in $interface; do
  49. emit "interface listen $intf"
  50. [ "$intf" = "$loopback" ] && saw_lo=1
  51. done
  52. [ -z "$saw_lo" ] && emit "interface listen $loopback"
  53. emit ""
  54. fi
  55. for i in $server
  56. do
  57. emit "server $i iburst"
  58. done
  59. mkdir -p /var/lib/ntp
  60. chown -R ntp:ntp /var/lib/ntp
  61. procd_open_instance
  62. procd_set_param command $PROG -g -u ntp:ntp -p /var/run/ntpd.pid -n \
  63. -c $config_file
  64. procd_close_instance
  65. procd_open_instance
  66. procd_set_param command $HOTPLUG_HELPER
  67. procd_close_instance
  68. }
  69. start_service() {
  70. validate_ntp_section ntp start_ntpd_instance
  71. }
  72. service_triggers() {
  73. procd_add_reload_trigger "system"
  74. procd_add_validation validate_ntp_section
  75. }