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.

96 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. trunc
  32. emit "driftfile /var/lib/ntp/ntp.drift\n"
  33. if [ "$enable_server" != 0 ]; then
  34. emit "restrict default limited kod nomodify notrap nopeer"
  35. emit "restrict -6 default limited kod nomodify notrap nopeer"
  36. else
  37. emit "restrict -4 default noserve"
  38. emit "restrict -6 default noserve"
  39. fi
  40. emit "restrict source noquery"
  41. emit "\n# No limits for local monitoring"
  42. emit "restrict 127.0.0.1"
  43. emit "restrict -6 ::1\n"
  44. if [ -n "$interface" ]; then
  45. local loopback=$(ubus call network.interface dump | jsonfilter -e "@.interface[@.interface='loopback']['device']")
  46. local saw_lo=
  47. for intf in $interface; do
  48. emit "interface listen $intf"
  49. [ "$intf" = "$loopback" ] && saw_lo=1
  50. done
  51. [ -z "$saw_lo" ] && emit "interface listen $loopback"
  52. emit ""
  53. fi
  54. for i in $server
  55. do
  56. emit "server $i iburst"
  57. done
  58. mkdir -p /var/lib/ntp
  59. chown -R ntp:ntp /var/lib/ntp
  60. procd_open_instance
  61. procd_set_param command $PROG -g -u ntp:ntp -p /var/run/ntpd.pid -n \
  62. -c $config_file
  63. procd_close_instance
  64. procd_open_instance
  65. procd_set_param command $HOTPLUG_HELPER
  66. procd_close_instance
  67. }
  68. start_service() {
  69. validate_ntp_section ntp start_ntpd_instance
  70. }
  71. service_triggers() {
  72. procd_add_reload_trigger "system"
  73. procd_add_validation validate_ntp_section
  74. }