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.

112 lines
2.3 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. if [ -d "/etc/ntpd.d" ]; then
  60. local entry
  61. for entry in "/etc/ntpd.d"/*.conf; do
  62. emit "includefile ${entry}\n"
  63. done
  64. fi
  65. if [ -d "/tmp/ntpd.d" ]; then
  66. local entry
  67. for entry in "/tmp/ntpd.d"/*.conf; do
  68. emit "includefile ${entry}\n"
  69. done
  70. fi
  71. mkdir -p /var/lib/ntp
  72. chown -R ntp:ntp /var/lib/ntp
  73. procd_open_instance
  74. procd_set_param command $PROG -g -u ntp:ntp -p /var/run/ntpd.pid -n \
  75. -c $config_file
  76. procd_close_instance
  77. procd_open_instance
  78. procd_set_param command $HOTPLUG_HELPER
  79. procd_close_instance
  80. }
  81. start_service() {
  82. validate_ntp_section ntp start_ntpd_instance
  83. }
  84. service_triggers() {
  85. procd_add_reload_trigger "system"
  86. procd_add_validation validate_ntp_section
  87. }