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.

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