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.

178 lines
5.5 KiB

  1. #!/bin/sh /etc/rc.common
  2. #
  3. # Fwknop is developed primarily by the people listed in the file 'AUTHORS'.
  4. # Copyright (C) 2009-2014 fwknop developers and contributors. For a full
  5. # list of contributors, see the file 'CREDITS'.
  6. #
  7. USE_PROCD=1
  8. START=95
  9. FWKNOPD_BIN=/usr/sbin/fwknopd
  10. start_service()
  11. {
  12. generate_configuration
  13. if [ -n "$DEPEND_IFNAME" ] ; then
  14. # We know the interface, so we can start
  15. procd_open_instance
  16. procd_set_param command "$FWKNOPD_BIN" --foreground --syslog-enable
  17. procd_set_param respawn
  18. if [ $UCI_ENABLED -eq 1 ]; then
  19. procd_append_param command -c /var/etc/fwknopd.conf
  20. procd_append_param command -a /var/etc/access.conf
  21. fi
  22. procd_append_param command -i "$DEPEND_IFNAME"
  23. procd_set_param netdev "$DEPEND_IFNAME"
  24. procd_close_instance
  25. else
  26. logger -p daemon.info -t "fwknopd[----]" "Postponing start-up of fwknopd, network $NETWORK is not up"
  27. fi
  28. }
  29. service_triggers()
  30. {
  31. procd_add_reload_trigger "fwknopd"
  32. if [ -n "$NETWORK" ] ; then
  33. logger -p daemon.info -t "fwknopd[----]" "Listening for changes on network $NETWORK"
  34. procd_add_reload_interface_trigger "$NETWORK"
  35. fi
  36. }
  37. get_bool()
  38. {
  39. local _tmp="$1"
  40. case "$_tmp" in
  41. 1|on|true|yes|enabled) _tmp=1;;
  42. 0|off|false|no|disabled) _tmp=0;;
  43. *) _tmp="$2";;
  44. esac
  45. echo -n "$_tmp"
  46. }
  47. generate_configuration()
  48. {
  49. [ -f /tmp/access.conf.tmp ] && rm /tmp/access.conf.tmp
  50. UCI_ENABLED=0
  51. DEPEND_IFNAME=
  52. NETWORK=
  53. local PCAP_INTF=
  54. local USER_CONFIG_PATH=/etc/fwknop/fwknopd.conf
  55. local DEFAULT_UCI_NETWORK=wan
  56. local DEFAULT_FWKNOPD_IFNAME=eth0
  57. config_cb() {
  58. local type="$1"
  59. local name="$2"
  60. if [ "$type" = "global" ]; then
  61. option_cb() {
  62. local option="$1"
  63. local value="$2"
  64. if [ "$option" = "uci_enabled" ] && [ "$(get_bool "$value" 0)" -eq 1 ] ; then
  65. > /var/etc/fwknopd.conf
  66. > /var/etc/access.conf
  67. chmod 600 /var/etc/fwknopd.conf
  68. chmod 600 /var/etc/access.conf
  69. UCI_ENABLED=1
  70. # Forced defaults
  71. # Do not let fwknopd to shut-down when interface goes down,
  72. # control it from the start-up script instead:
  73. # https://bugs.openwrt.org/index.php?do=details&task_id=1481
  74. echo "EXIT_AT_INTF_DOWN n" >> /var/etc/fwknopd.conf
  75. fi
  76. }
  77. elif [ "$type" = "network" ]; then
  78. option_cb() {
  79. local option="$1"
  80. local value="$2"
  81. if [ $UCI_ENABLED -eq 1 ] && [ $option = "network" ]; then
  82. NETWORK="$value"
  83. fi
  84. }
  85. elif [ "$type" = "config" ]; then
  86. option_cb() {
  87. local option="$1"
  88. local value="$2"
  89. if [ $UCI_ENABLED -eq 1 ] && [ $option = "PCAP_INTF" ]; then
  90. PCAP_INTF="$value"
  91. echo "$option $value" >> /var/etc/fwknopd.conf #writing each option to fwknopd.conf
  92. elif [ $UCI_ENABLED -eq 1 ] && [ $option = "EXIT_AT_INTF_DOWN" ]; then
  93. logger -p daemon.warn -t "fwknopd[----]" "Ignoring EXIT_AT_INTF_DOWN option, forced to N (no) to work reliably with procd"
  94. elif [ $UCI_ENABLED -eq 1 ]; then
  95. echo "$option $value" >> /var/etc/fwknopd.conf #writing each option to fwknopd.conf
  96. fi
  97. }
  98. elif [ "$type" = "access" ]; then
  99. if [ -f /tmp/access.conf.tmp ] ; then
  100. cat /tmp/access.conf.tmp >> /var/etc/access.conf
  101. rm /tmp/access.conf.tmp
  102. fi
  103. option_cb() {
  104. local option="$1"
  105. local value="$2"
  106. if [ $UCI_ENABLED -eq 1 ] && [ $option = "SOURCE" ]; then
  107. echo "$option $value" >> /var/etc/access.conf #writing each option to access.conf
  108. fi
  109. if [ $UCI_ENABLED -eq 1 ] && [ $option != "SOURCE" ]; then
  110. echo "$option $value" >> /tmp/access.conf.tmp #writing each option to access.conf
  111. fi
  112. }
  113. else
  114. reset_cb
  115. if [ -z "$type" ]; then
  116. # Finalize reading
  117. if [ -f /tmp/access.conf.tmp ] ; then
  118. cat /tmp/access.conf.tmp >> /var/etc/access.conf
  119. rm /tmp/access.conf.tmp
  120. fi
  121. fi
  122. fi
  123. }
  124. if [ -f /etc/config/fwknopd ]; then
  125. config_load fwknopd
  126. fi
  127. if [ $UCI_ENABLED -eq 0 ]; then
  128. if [ -f $USER_CONFIG_PATH ] ; then
  129. # Scan user configuration for PCAP_INTF settings and fallback to fwknopd's default
  130. DEPEND_IFNAME="$( sed -ne '/^\s*PCAP_INTF\s\+/ { s/^\s*PCAP_INTF\s\+//; s/\s\+$//; p; q; }' $USER_CONFIG_PATH )"
  131. if [ -n "$DEPEND_IFNAME" ]; then
  132. logger -p daemon.debug -t "fwknopd[----]" "Found fwknopd.conf configuration, using PCAP_INTF interface $DEPEND_IFNAME"
  133. else
  134. logger -p daemon.info -t "fwknopd[----]" "No PCAP_INTF interface specified in fwknopd.conf, fwknopd's default $DEFAULT_FWKNOPD_IFNAME will be used"
  135. DEPEND_IFNAME="$DEFAULT_FWKNOPD_IFNAME"
  136. fi
  137. else
  138. logger -p daemon.error -t "fwknopd[----]" "No $USER_CONFIG_PATH found, not starting"
  139. exit 1
  140. fi
  141. elif [ $UCI_ENABLED -eq 1 ]; then
  142. if [ -n "$NETWORK" ] && [ -n "$PCAP_INTF" ]; then
  143. logger -p daemon.warn -t "fwknopd[----]" "Specified both network and PCAP_INTF. Ignoring PCAP_INTF"
  144. elif [ -z "$NETWORK" ] && [ -z "$PCAP_INTF" ]; then
  145. # Fallback - compatibility with old script, which used wan interface by default
  146. logger -p daemon.info -t "fwknopd[----]" "Neither network, nor PCAP_INTF interface specified, trying network $DEFAULT_UCI_NETWORK"
  147. NETWORK="$DEFAULT_UCI_NETWORK"
  148. fi
  149. # Resolve network if possible
  150. if [ -n "$NETWORK" ]; then
  151. . /lib/functions/network.sh
  152. network_get_device DEPEND_IFNAME "$NETWORK"
  153. if [ -n "$DEPEND_IFNAME" ]; then
  154. logger -p daemon.debug -t "fwknopd[----]" "Resolved network $NETWORK as interface $DEPEND_IFNAME"
  155. else
  156. logger -p daemon.warn -t "fwknopd[----]" "Cannot find interface for network $NETWORK, probably the network is not up"
  157. fi
  158. elif [ -n "$PCAP_INTF" ]; then
  159. DEPEND_IFNAME="$PCAP_INTF"
  160. logger -p daemon.debug -t "fwknopd[----]" "Using configured PCAP_INTF interface $DEPEND_IFNAME"
  161. fi
  162. fi
  163. }