init_ynh-piratebox 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-piratebox
  4. # Required-Start: $network $remote_fs $syslog $all
  5. # Required-Stop: $network $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Set prerequisites for wifi piratebox.
  9. # Description: Set prerequisites for wifi piratebox.
  10. ### END INIT INFO
  11. # PirateBox app for YunoHost
  12. # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  13. # Contribute at https://github.com/jvaubourg/piratebox_ynh
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. # Functions
  28. ## State functions
  29. has_hotspot_app() {
  30. [ -e /tmp/.ynh-hotspot-started ]
  31. }
  32. is_nat4_dns_set() {
  33. iptables -nvt nat -L PREROUTING | grep 'udp dpt:53' | grep -q "${ynh_wifi_device}" \
  34. && iptables -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${ynh_wifi_device}"
  35. }
  36. is_nat4_web_set() {
  37. iptables -nvt nat -L PREROUTING | grep 'tcp dpt:80' | grep -q "${ynh_wifi_device}"
  38. }
  39. is_filt4_nohttps_set() {
  40. iptables -nv -L INPUT | grep 'tcp dpt:443 reject' | grep -q "${ynh_wifi_device}"
  41. }
  42. is_filt4_nofwd_set() {
  43. iptables -nv -L FORWARD | grep 'reject-with' | grep -q "${ynh_wifi_device}"
  44. }
  45. is_fakedns_running() {
  46. ps aux | grep -v grep | grep -q piratebox_fakedns
  47. }
  48. is_running() {
  49. has_hotspot_app \
  50. && is_nat4_dns_set && is_nat4_web_set \
  51. && is_filt4_nohttps_set && is_filt4_nofwd_set \
  52. && is_fakedns_running
  53. }
  54. ## Setters
  55. # The IPv6 NAT features are not available available before the kernel 3.8
  56. # therefore the IPv6-compliance will be add when Debian Stable has a
  57. # compliant kernel
  58. set_nat4_dns() {
  59. iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j DNAT --to-destination "${ynh_ip4_nat_prefix}.1:4253"
  60. iptables -t nat -A POSTROUTING -o "${ynh_wifi_device}" -j MASQUERADE
  61. }
  62. set_nat4_web() {
  63. iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p tcp --dport 80 -j REDIRECT --to-port 4280
  64. }
  65. set_filt4_nohttps() {
  66. iptables -I INPUT 1 -i "${ynh_wifi_device}" -p tcp --dport 443 -j REJECT
  67. }
  68. set_filt4_nofwd() {
  69. iptables -I FORWARD 1 -j REJECT -i "${ynh_wifi_device}"
  70. }
  71. start_fakedns() {
  72. /usr/local/bin/piratebox_fakedns "${ynh_ip4_nat_prefix}.1" 2>&1 > /dev/null &
  73. }
  74. ## Unsetters
  75. unset_nat4_dns() {
  76. iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j DNAT --to-destination "${ynh_ip4_nat_prefix}.1:4253"
  77. iptables -t nat -D POSTROUTING -o "${ynh_wifi_device}" -j MASQUERADE
  78. }
  79. unset_nat4_web() {
  80. iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p tcp --dport 80 -j REDIRECT --to-port 4280
  81. }
  82. unset_filt4_nohttps() {
  83. iptables -D INPUT -i "${ynh_wifi_device}" -p tcp --dport 443 -j REJECT
  84. }
  85. unset_filt4_nofwd() {
  86. iptables -D FORWARD -j REJECT -i "${ynh_wifi_device}"
  87. }
  88. stop_fakedns() {
  89. kill $(ps aux | grep piratebox_fakedns | awk '{ print $2 }' | head -n1)
  90. }
  91. ## Tools
  92. moulinette_hotspot_get() {
  93. var=${1}
  94. value=$(yunohost app setting hotspot "${var}")
  95. if [[ "${value}" =~ "An instance is already running" ]]; then
  96. echo "${value}" >&2
  97. exit 1
  98. fi
  99. echo "${value}"
  100. }
  101. if [ "$1" != restart ]; then
  102. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  103. if [ ! -e /tmp/.ynh-piratebox-boot ]; then
  104. touch /tmp/.ynh-piratebox-boot
  105. service php5-fpm restart
  106. fi
  107. # Variables
  108. echo -n "Retrieving Yunohost settings... "
  109. ynh_wifi_device=$(moulinette_hotspot_get wifi_device)
  110. ynh_ip4_nat_prefix=$(moulinette_hotspot_get ip4_nat_prefix)
  111. echo "OK"
  112. fi
  113. # Script
  114. case "$1" in
  115. start)
  116. if is_running; then
  117. echo "Already started"
  118. elif ! has_hotspot_app; then
  119. echo "[ERR] Hotspot is not running"
  120. else
  121. echo "[piratebox] Starting..."
  122. touch /tmp/.ynh-piratebox-started
  123. # Set IPv4 DNS NAT
  124. if ! is_nat4_dns_set; then
  125. echo "Set IPv4 DNS NAT"
  126. set_nat4_dns
  127. fi
  128. # Set IPv4 Web NAT
  129. if ! is_nat4_web_set; then
  130. echo "Set IPv4 Web NAT"
  131. set_nat4_web
  132. fi
  133. # Set IPv4 No-Https filter rule
  134. if ! is_filt4_nohttps_set; then
  135. echo "Set IPv4 No-Https filter rule"
  136. set_filt4_nohttps
  137. fi
  138. # Set IPv4 No-Forwarding filter rule
  139. if ! is_filt4_nofwd_set; then
  140. echo "Set IPv4 No-Forwarding filter rule"
  141. set_filt4_nofwd
  142. fi
  143. # Run fakedns
  144. if ! is_fakedns_running; then
  145. echo "Run fakedns"
  146. start_fakedns
  147. fi
  148. fi
  149. ;;
  150. stop)
  151. echo "[piratebox] Stopping..."
  152. rm -f /tmp/.ynh-piratebox-started
  153. if is_nat4_dns_set; then
  154. echo "Unset IPv4 DNS NAT"
  155. unset_nat4_dns
  156. fi
  157. if is_nat4_web_set; then
  158. echo "Unset IPv4 Web NAT"
  159. unset_nat4_web
  160. fi
  161. if is_filt4_nohttps_set; then
  162. echo "Unset IPv4 No-Https filter rule"
  163. unset_filt4_nohttps
  164. fi
  165. if is_filt4_nofwd_set; then
  166. echo "Unset IPv4 No-Forwarding filter rule"
  167. unset_filt4_nofwd
  168. fi
  169. if is_fakedns_running; then
  170. echo "Stop fakedns"
  171. stop_fakedns
  172. fi
  173. ;;
  174. restart)
  175. $0 stop
  176. $0 start
  177. ;;
  178. status)
  179. exitcode=0
  180. if ! has_hotspot_app; then
  181. echo "[ERR] Hotspot is not running"
  182. exitcode=1
  183. fi
  184. if is_nat4_dns_set; then
  185. echo "[OK] IPv4 DNS NAT set"
  186. else
  187. echo "[ERR] No IPv4 DNS NAT set"
  188. exitcode=1
  189. fi
  190. if is_nat4_web_set; then
  191. echo "[OK] IPv4 Web NAT set"
  192. else
  193. echo "[ERR] No IPv4 Web NAT set"
  194. exitcode=1
  195. fi
  196. if is_filt4_nohttps_set; then
  197. echo "[OK] IPv4 No-Https filter rule set"
  198. else
  199. echo "[ERR] No IPv4 No-Https filter rule set"
  200. exitcode=1
  201. fi
  202. if is_filt4_nofwd_set; then
  203. echo "[OK] IPv4 No-Forwarding filter rule set"
  204. else
  205. echo "[ERR] No IPv4 No-Forwarding filter rule set"
  206. exitcode=1
  207. fi
  208. if is_fakedns_running; then
  209. echo "[OK] Fakedns is running"
  210. else
  211. echo "[ERR] Fakedns is not running"
  212. exitcode=1
  213. fi
  214. exit ${exitcode}
  215. ;;
  216. *)
  217. echo "Usage: $0 {start|stop|restart|status}"
  218. exit 1
  219. ;;
  220. esac
  221. exit 0