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.

163 lines
4.8 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. procd_open_instance
  14. procd_set_param command "$FWKNOPD_BIN" --foreground --syslog-enable
  15. procd_set_param respawn
  16. if [ $UCI_ENABLED -eq 1 ]; then
  17. procd_append_param command -c /var/etc/fwknopd.conf
  18. procd_append_param command -a /var/etc/access.conf
  19. fi
  20. procd_append_param command -i "$DEPEND_IFNAME"
  21. procd_set_param netdev "$DEPEND_IFNAME"
  22. procd_close_instance
  23. }
  24. service_triggers()
  25. {
  26. procd_add_reload_trigger "fwknopd"
  27. }
  28. get_bool()
  29. {
  30. local _tmp="$1"
  31. case "$_tmp" in
  32. 1|on|true|yes|enabled) _tmp=1;;
  33. 0|off|false|no|disabled) _tmp=0;;
  34. *) _tmp="$2";;
  35. esac
  36. echo -n "$_tmp"
  37. }
  38. generate_configuration()
  39. {
  40. [ -f /tmp/access.conf.tmp ] && rm /tmp/access.conf.tmp
  41. UCI_ENABLED=0
  42. DEPEND_IFNAME=
  43. local NETWORK=
  44. local PCAP_INTF=
  45. local USER_CONFIG_PATH=/etc/fwknop/fwknopd.conf
  46. local DEFAULT_UCI_NETWORK=wan
  47. local DEFAULT_FWKNOPD_IFNAME=eth0
  48. config_cb() {
  49. local type="$1"
  50. local name="$2"
  51. if [ "$type" = "global" ]; then
  52. option_cb() {
  53. local option="$1"
  54. local value="$2"
  55. if [ "$option" = "uci_enabled" ] && [ "$(get_bool "$value" 0)" -eq 1 ] ; then
  56. > /var/etc/fwknopd.conf
  57. > /var/etc/access.conf
  58. chmod 600 /var/etc/fwknopd.conf
  59. chmod 600 /var/etc/access.conf
  60. UCI_ENABLED=1
  61. fi
  62. }
  63. elif [ "$type" = "network" ]; then
  64. option_cb() {
  65. local option="$1"
  66. local value="$2"
  67. if [ $UCI_ENABLED -eq 1 ] && [ $option = "network" ]; then
  68. NETWORK="$value"
  69. fi
  70. }
  71. elif [ "$type" = "config" ]; then
  72. option_cb() {
  73. local option="$1"
  74. local value="$2"
  75. if [ $UCI_ENABLED -eq 1 ] && [ $option = "PCAP_INTF" ]; then
  76. PCAP_INTF="$value"
  77. echo "$option $value" >> /var/etc/fwknopd.conf #writing each option to fwknopd.conf
  78. elif [ $UCI_ENABLED -eq 1 ]; then
  79. echo "$option $value" >> /var/etc/fwknopd.conf #writing each option to fwknopd.conf
  80. fi
  81. }
  82. elif [ "$type" = "access" ]
  83. then
  84. if [ -f /tmp/access.conf.tmp ] ; then
  85. cat /tmp/access.conf.tmp >> /var/etc/access.conf
  86. rm /tmp/access.conf.tmp
  87. fi
  88. option_cb() {
  89. local option="$1"
  90. local value="$2"
  91. if [ $UCI_ENABLED -eq 1 ] && [ $option = "SOURCE" ]; then
  92. echo "$option $value" >> /var/etc/access.conf #writing each option to access.conf
  93. fi
  94. if [ $UCI_ENABLED -eq 1 ] && [ $option != "SOURCE" ]; then
  95. echo "$option $value" >> /tmp/access.conf.tmp #writing each option to access.conf
  96. fi
  97. }
  98. else
  99. option_cb() { return; }
  100. if [ -z "$type" ]; then
  101. # Finalize reading
  102. if [ -f /tmp/access.conf.tmp ] ; then
  103. cat /tmp/access.conf.tmp >> /var/etc/access.conf
  104. rm /tmp/access.conf.tmp
  105. fi
  106. fi
  107. fi
  108. }
  109. if [ -f /etc/config/fwknopd ]; then
  110. config_load fwknopd
  111. fi
  112. if [ $UCI_ENABLED -eq 0 ]; then
  113. if [ -f $USER_CONFIG_PATH ] ; then
  114. # Scan user configuration for PCAP_INTF settings
  115. DEPEND_IFNAME="$( sed -ne '/^\s*PCAP_INTF\s\+/ { s/^\s*PCAP_INTF\s\+//; s/\s\+$//; p; q; }' /etc/fwknop/fwknopd.conf )"
  116. if [ -n "$DEPEND_IFNAME" ]; then
  117. logger -p daemon.debug -t "fwknopd[----]" "Found fwknopd.conf configuration, using PCAP_INTF interface $DEPEND_IFNAME"
  118. else
  119. logger -p daemon.info -t "fwknopd[----]" "No PCAP_INTF interface specified in fwknopd.conf, fwknopd's default $DEFAULT_FWKNOPD_IFNAME will be used"
  120. DEPEND_IFNAME="$DEFAULT_FWKNOPD_IFNAME"
  121. fi
  122. else
  123. logger -p daemon.error -t "fwknopd[----]" "No $USER_CONFIG_PATH found, not starting"
  124. exit 1
  125. fi
  126. elif [ $UCI_ENABLED -eq 1 ]; then
  127. if [ -n "$NETWORK" ] && [ -n "$PCAP_INTF" ]; then
  128. logger -p daemon.warn -t "fwknopd[----]" "Specified both network and PCAP_INTF. Ignoring PCAP_INTF"
  129. elif [ -z "$NETWORK" ] && [ -z "$PCAP_INTF" ]; then
  130. # Fallback - compatibility with old script, which used wan interface by default
  131. logger -p daemon.info -t "fwknopd[----]" "Neither network, nor PCAP_INTF interface specified, trying network $DEFAULT_UCI_NETWORK"
  132. NETWORK="$DEFAULT_UCI_NETWORK"
  133. fi
  134. if [ -n "$NETWORK" ]; then
  135. . /lib/functions/network.sh
  136. network_get_physdev DEPEND_IFNAME "$NETWORK"
  137. if [ -n "$DEPEND_IFNAME" ]; then
  138. logger -p daemon.debug -t "fwknopd[----]" "Resolved network $NETWORK as interface $DEPEND_IFNAME"
  139. else
  140. logger -p daemon.warn -t "fwknopd[----]" "Cannot find interface for network $NETWORK, fwknopd's default $DEFAULT_FWKNOPD_IFNAME will be used"
  141. DEPEND_IFNAME="$DEFAULT_FWKNOPD_IFNAME"
  142. fi
  143. elif [ -n "$PCAP_INTF" ]; then
  144. DEPEND_IFNAME="$PCAP_INTF"
  145. logger -p daemon.debug -t "fwknopd[----]" "Using configured PCAP_INTF interface $DEPEND_IFNAME"
  146. fi
  147. fi
  148. }