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.

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