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.

164 lines
5.2 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. generate_configuration()
  40. {
  41. [ -f /tmp/access.conf.tmp ] && rm /tmp/access.conf.tmp
  42. UCI_ENABLED=0
  43. DEPEND_IFNAME=
  44. NETWORK=
  45. local PCAP_INTF=
  46. local USER_CONFIG_PATH=/etc/fwknop/fwknopd.conf
  47. local DEFAULT_UCI_NETWORK=wan
  48. local DEFAULT_FWKNOPD_IFNAME=
  49. network_get_device DEFAULT_FWKNOPD_IFNAME "${DEFAULT_UCI_NETWORK}"
  50. config_cb() {
  51. local type="${1}"
  52. local name="${2}"
  53. if [ "${type}" = "global" ]; then
  54. option_cb() {
  55. local option="${1}"
  56. local value="${2}"
  57. if [ "${option}" = "uci_enabled" ] && [ "$(get_bool "${value}" 0)" -eq 1 ] ; then
  58. mkdir -p "${FWKNOPD_CFGDIR}"
  59. > "${FWKNOPD_CFGDIR}/fwknopd.conf"
  60. > "${FWKNOPD_CFGDIR}/access.conf"
  61. chmod 600 "${FWKNOPD_CFGDIR}/fwknopd.conf"
  62. chmod 600 "${FWKNOPD_CFGDIR}/access.conf"
  63. UCI_ENABLED=1
  64. fi
  65. }
  66. elif [ "${type}" = "network" ]; then
  67. option_cb() {
  68. local option="${1}"
  69. local value="${2}"
  70. if [ "${UCI_ENABLED}" -eq 1 ] && [ "${option}" = "network" ]; then
  71. NETWORK="${value}"
  72. fi
  73. }
  74. elif [ "${type}" = "config" ]; then
  75. option_cb() {
  76. local option="${1}"
  77. local value="${2}"
  78. if [ "${UCI_ENABLED}" -eq 1 ]; then
  79. if [ "${option}" = "PCAP_INTF" ]; then
  80. PCAP_INTF="${value}"
  81. fi
  82. echo "${option} ${value}" >> "${FWKNOPD_CFGDIR}/fwknopd.conf" #writing each option to fwknopd.conf
  83. fi
  84. }
  85. elif [ "${type}" = "access" ]; then
  86. if [ -f /tmp/access.conf.tmp ] ; then
  87. cat /tmp/access.conf.tmp >> "${FWKNOPD_CFGDIR}/access.conf"
  88. rm /tmp/access.conf.tmp
  89. fi
  90. option_cb() {
  91. local option="${1}"
  92. local value="${2}"
  93. if [ "${UCI_ENABLED}" -eq 1 ]; then
  94. if [ "${option}" = "SOURCE" ]; then
  95. echo "${option} ${value}" >> "${FWKNOPD_CFGDIR}/access.conf" #writing each option to access.conf
  96. else
  97. echo "${option} ${value}" >> /tmp/access.conf.tmp #writing each option to access.conf
  98. fi
  99. fi
  100. }
  101. else
  102. reset_cb
  103. if [ -z "${type}" ]; then
  104. # Finalize reading
  105. if [ -f /tmp/access.conf.tmp ] ; then
  106. cat /tmp/access.conf.tmp >> "${FWKNOPD_CFGDIR}/access.conf"
  107. rm /tmp/access.conf.tmp
  108. fi
  109. fi
  110. fi
  111. }
  112. if [ -f /etc/config/fwknopd ]; then
  113. config_load fwknopd
  114. fi
  115. if [ "${UCI_ENABLED}" -eq 0 ]; then
  116. if [ -f "${USER_CONFIG_PATH}" ] ; then
  117. # Scan user configuration for PCAP_INTF settings and fallback to fwknopd's default
  118. DEPEND_IFNAME="$( sed -ne '/^\s*PCAP_INTF\s\+/ { s/^\s*PCAP_INTF\s\+//; s/\s\+$//; p; q; }' ${USER_CONFIG_PATH} )"
  119. if [ -n "${DEPEND_IFNAME}" ]; then
  120. logger -p daemon.debug -t "fwknopd[$$]" "Found fwknopd.conf configuration, using PCAP_INTF interface ${DEPEND_IFNAME}"
  121. else
  122. logger -p daemon.info -t "fwknopd[$$]" "No PCAP_INTF interface specified in fwknopd.conf, fwknopd's default ${DEFAULT_FWKNOPD_IFNAME} will be used"
  123. DEPEND_IFNAME="${DEFAULT_FWKNOPD_IFNAME}"
  124. fi
  125. else
  126. logger -p daemon.error -t "fwknopd[$$]" "No ${USER_CONFIG_PATH} found, not starting"
  127. exit 1
  128. fi
  129. elif [ "${UCI_ENABLED}" -eq 1 ]; then
  130. if [ -n "${NETWORK}" ] && [ -n "${PCAP_INTF}" ]; then
  131. logger -p daemon.warn -t "fwknopd[$$]" "Specified both network and PCAP_INTF. Ignoring PCAP_INTF"
  132. elif [ -z "${NETWORK}" ] && [ -z "${PCAP_INTF}" ]; then
  133. # Fallback - compatibility with old script, which used wan interface by default
  134. logger -p daemon.info -t "fwknopd[$$]" "Neither network, nor PCAP_INTF interface specified, trying network ${DEFAULT_UCI_NETWORK}"
  135. NETWORK="${DEFAULT_UCI_NETWORK}"
  136. fi
  137. # Resolve network if possible
  138. if [ -n "${NETWORK}" ]; then
  139. network_get_device DEPEND_IFNAME "${NETWORK}"
  140. if [ -n "${DEPEND_IFNAME}" ]; then
  141. logger -p daemon.debug -t "fwknopd[$$]" "Resolved network ${NETWORK} as interface ${DEPEND_IFNAME}"
  142. else
  143. logger -p daemon.warn -t "fwknopd[$$]" "Cannot find interface for network ${NETWORK}, probably the network is not up"
  144. fi
  145. elif [ -n "${PCAP_INTF}" ]; then
  146. DEPEND_IFNAME="${PCAP_INTF}"
  147. logger -p daemon.debug -t "fwknopd[$$]" "Using configured PCAP_INTF interface ${DEPEND_IFNAME}"
  148. fi
  149. fi
  150. }