init_ynh-piratebox 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. # Purge tmp folder
  124. rm -rf /var/spool/piratebox/*
  125. # Set IPv4 DNS NAT
  126. if ! is_nat4_dns_set; then
  127. echo "Set IPv4 DNS NAT"
  128. set_nat4_dns
  129. fi
  130. # Set IPv4 Web NAT
  131. if ! is_nat4_web_set; then
  132. echo "Set IPv4 Web NAT"
  133. set_nat4_web
  134. fi
  135. # Set IPv4 No-Https filter rule
  136. if ! is_filt4_nohttps_set; then
  137. echo "Set IPv4 No-Https filter rule"
  138. set_filt4_nohttps
  139. fi
  140. # Set IPv4 No-Forwarding filter rule
  141. if ! is_filt4_nofwd_set; then
  142. echo "Set IPv4 No-Forwarding filter rule"
  143. set_filt4_nofwd
  144. fi
  145. # Run fakedns
  146. if ! is_fakedns_running; then
  147. echo "Run fakedns"
  148. start_fakedns
  149. fi
  150. fi
  151. ;;
  152. stop)
  153. echo "[piratebox] Stopping..."
  154. rm -f /tmp/.ynh-piratebox-started
  155. if is_nat4_dns_set; then
  156. echo "Unset IPv4 DNS NAT"
  157. unset_nat4_dns
  158. fi
  159. if is_nat4_web_set; then
  160. echo "Unset IPv4 Web NAT"
  161. unset_nat4_web
  162. fi
  163. if is_filt4_nohttps_set; then
  164. echo "Unset IPv4 No-Https filter rule"
  165. unset_filt4_nohttps
  166. fi
  167. if is_filt4_nofwd_set; then
  168. echo "Unset IPv4 No-Forwarding filter rule"
  169. unset_filt4_nofwd
  170. fi
  171. if is_fakedns_running; then
  172. echo "Stop fakedns"
  173. stop_fakedns
  174. fi
  175. rm -rf /var/spool/piratebox/*
  176. ;;
  177. restart)
  178. $0 stop
  179. $0 start
  180. ;;
  181. status)
  182. exitcode=0
  183. if ! has_hotspot_app; then
  184. echo "[ERR] Hotspot is not running"
  185. exitcode=1
  186. fi
  187. if is_nat4_dns_set; then
  188. echo "[OK] IPv4 DNS NAT set"
  189. else
  190. echo "[ERR] No IPv4 DNS NAT set"
  191. exitcode=1
  192. fi
  193. if is_nat4_web_set; then
  194. echo "[OK] IPv4 Web NAT set"
  195. else
  196. echo "[ERR] No IPv4 Web NAT set"
  197. exitcode=1
  198. fi
  199. if is_filt4_nohttps_set; then
  200. echo "[OK] IPv4 No-Https filter rule set"
  201. else
  202. echo "[ERR] No IPv4 No-Https filter rule set"
  203. exitcode=1
  204. fi
  205. if is_filt4_nofwd_set; then
  206. echo "[OK] IPv4 No-Forwarding filter rule set"
  207. else
  208. echo "[ERR] No IPv4 No-Forwarding filter rule set"
  209. exitcode=1
  210. fi
  211. if is_fakedns_running; then
  212. echo "[OK] Fakedns is running"
  213. else
  214. echo "[ERR] Fakedns is not running"
  215. exitcode=1
  216. fi
  217. exit ${exitcode}
  218. ;;
  219. *)
  220. echo "Usage: $0 {start|stop|restart|status}"
  221. exit 1
  222. ;;
  223. esac
  224. exit 0