init_ynh-piratebox 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-piratebox
  4. # Required-Start: $network $remote_fs $syslog ynh-hotspot
  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_get() {
  93. var=${1}
  94. value=$(yunohost app setting piratebox "${var}")
  95. if [[ "${value}" =~ "An instance is already running" ]]; then
  96. echo "${value}" >&2
  97. exit 1
  98. fi
  99. echo "${value}"
  100. }
  101. moulinette_hotspot_get() {
  102. var=${1}
  103. value=$(yunohost app setting hotspot "${var}")
  104. if [[ "${value}" =~ "An instance is already running" ]]; then
  105. echo "${value}" >&2
  106. exit 1
  107. fi
  108. echo "${value}"
  109. }
  110. if [ "$1" != restart ]; then
  111. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  112. if [ ! -e /tmp/.ynh-piratebox-boot ]; then
  113. touch /tmp/.ynh-piratebox-boot
  114. service php5-fpm restart
  115. fi
  116. # Variables
  117. echo -n "Retrieving Yunohost settings... "
  118. ynh_wifi_device_id=$(moulinette_get wifi_device_id)
  119. if [ "${ynh_wifi_device_id}" -eq 0 ]; then
  120. ynh_wifi_device="$(moulinette_hotspot_get wifi_device)"
  121. else
  122. ynh_wifi_device="hotspot${ynh_wifi_device_id}"
  123. fi
  124. IFS='|' read -a ynh_ip4_nat_prefix <<< "$(moulinette_hotspot_get ip4_nat_prefix)"
  125. ynh_ip4_nat_prefix=${ynh_ip4_nat_prefix[${ynh_wifi_device_id}]}
  126. echo "OK"
  127. fi
  128. # Script
  129. case "$1" in
  130. start)
  131. if is_running; then
  132. echo "Already started"
  133. elif ! has_hotspot_app; then
  134. echo "[ERR] Hotspot is not running"
  135. else
  136. echo "[piratebox] Starting..."
  137. touch /tmp/.ynh-piratebox-started
  138. # Purge tmp folder
  139. rm -rf /var/spool/piratebox/*
  140. # Set IPv4 DNS NAT
  141. if ! is_nat4_dns_set; then
  142. echo "Set IPv4 DNS NAT"
  143. set_nat4_dns
  144. fi
  145. # Set IPv4 Web NAT
  146. if ! is_nat4_web_set; then
  147. echo "Set IPv4 Web NAT"
  148. set_nat4_web
  149. fi
  150. # Set IPv4 No-Https filter rule
  151. if ! is_filt4_nohttps_set; then
  152. echo "Set IPv4 No-Https filter rule"
  153. set_filt4_nohttps
  154. fi
  155. # Set IPv4 No-Forwarding filter rule
  156. if ! is_filt4_nofwd_set; then
  157. echo "Set IPv4 No-Forwarding filter rule"
  158. set_filt4_nofwd
  159. fi
  160. # Run fakedns
  161. if ! is_fakedns_running; then
  162. echo "Run fakedns"
  163. start_fakedns
  164. fi
  165. fi
  166. ;;
  167. stop)
  168. echo "[piratebox] Stopping..."
  169. rm -f /tmp/.ynh-piratebox-started
  170. if is_nat4_dns_set; then
  171. echo "Unset IPv4 DNS NAT"
  172. unset_nat4_dns
  173. fi
  174. if is_nat4_web_set; then
  175. echo "Unset IPv4 Web NAT"
  176. unset_nat4_web
  177. fi
  178. if is_filt4_nohttps_set; then
  179. echo "Unset IPv4 No-Https filter rule"
  180. unset_filt4_nohttps
  181. fi
  182. if is_filt4_nofwd_set; then
  183. echo "Unset IPv4 No-Forwarding filter rule"
  184. unset_filt4_nofwd
  185. fi
  186. if is_fakedns_running; then
  187. echo "Stop fakedns"
  188. stop_fakedns
  189. fi
  190. rm -rf /var/spool/piratebox/*
  191. ;;
  192. restart)
  193. $0 stop
  194. $0 start
  195. ;;
  196. status)
  197. exitcode=0
  198. if ! has_hotspot_app; then
  199. echo "[ERR] Hotspot is not running"
  200. exitcode=1
  201. fi
  202. if is_nat4_dns_set; then
  203. echo "[OK] IPv4 DNS NAT set"
  204. else
  205. echo "[ERR] No IPv4 DNS NAT set"
  206. exitcode=1
  207. fi
  208. if is_nat4_web_set; then
  209. echo "[OK] IPv4 Web NAT set"
  210. else
  211. echo "[ERR] No IPv4 Web NAT set"
  212. exitcode=1
  213. fi
  214. if is_filt4_nohttps_set; then
  215. echo "[OK] IPv4 No-Https filter rule set"
  216. else
  217. echo "[ERR] No IPv4 No-Https filter rule set"
  218. exitcode=1
  219. fi
  220. if is_filt4_nofwd_set; then
  221. echo "[OK] IPv4 No-Forwarding filter rule set"
  222. else
  223. echo "[ERR] No IPv4 No-Forwarding filter rule set"
  224. exitcode=1
  225. fi
  226. if is_fakedns_running; then
  227. echo "[OK] Fakedns is running"
  228. else
  229. echo "[ERR] Fakedns is not running"
  230. exitcode=1
  231. fi
  232. exit ${exitcode}
  233. ;;
  234. *)
  235. echo "Usage: $0 {start|stop|restart|status}"
  236. exit 1
  237. ;;
  238. esac
  239. exit 0