ynh-piratebox 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #!/bin/bash
  2. # PirateBox app for YunoHost
  3. # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  4. # Contribute at https://github.com/jvaubourg/piratebox_ynh
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # Functions
  19. ## State functions
  20. has_hotspot_app() {
  21. [ -e /tmp/.ynh-hotspot-started ]
  22. }
  23. is_nat4_dns_set() {
  24. iptables -nvt nat -L PREROUTING | grep 'udp dpt:53' | grep -q "${ynh_wifi_device}" \
  25. && iptables -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${ynh_wifi_device}"
  26. }
  27. is_nat4_web_set() {
  28. iptables -nvt nat -L PREROUTING | grep 'tcp dpt:80' | grep -q "${ynh_wifi_device}"
  29. }
  30. is_filt4_nohttps_set() {
  31. iptables -nv -L INPUT | grep 'tcp dpt:443 reject' | grep -q "${ynh_wifi_device}"
  32. }
  33. is_filt4_nofwd_set() {
  34. iptables -nv -L FORWARD | grep 'reject-with' | grep -q "${ynh_wifi_device}"
  35. }
  36. is_fakedns_running() {
  37. ps aux | grep -v grep | grep -q piratebox_fakedns
  38. }
  39. is_running() {
  40. has_hotspot_app \
  41. && is_nat4_dns_set && is_nat4_web_set \
  42. && is_filt4_nohttps_set && is_filt4_nofwd_set \
  43. && is_captive_set \
  44. && is_fakedns_running
  45. }
  46. is_captive_set() {
  47. ls /etc/nginx/conf.d/captive-piratebox.conf &> /dev/null
  48. }
  49. ## Setters
  50. # The IPv6 NAT features are not available available before the kernel 3.8
  51. # therefore the IPv6-compliance will be add when Debian Stable has a
  52. # compliant kernel
  53. set_nat4_dns() {
  54. iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j DNAT --to-destination "${ynh_ip4_nat_prefix}.1:4253"
  55. iptables -t nat -A POSTROUTING -o "${ynh_wifi_device}" -j MASQUERADE
  56. }
  57. set_nat4_web() {
  58. iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p tcp --dport 80 -j REDIRECT --to-port 4280
  59. }
  60. set_filt4_nohttps() {
  61. iptables -I INPUT 1 -i "${ynh_wifi_device}" -p tcp --dport 443 -j REJECT
  62. }
  63. set_filt4_nofwd() {
  64. iptables -I FORWARD 1 -j REJECT -i "${ynh_wifi_device}"
  65. }
  66. set_captive() {
  67. cp /var/www/piratebox/config.{tpl.,}php
  68. sed -i "s|<TPL:OPT_NAME>|${ynh_opt_name}|" -i /var/www/piratebox/config.php
  69. sed -i "s|<TPL:OPT_RENAMING>|${ynh_opt_renaming}|" -i /var/www/piratebox/config.php
  70. sed -i "s|<TPL:OPT_DELETING>|${ynh_opt_deleting}|" -i /var/www/piratebox/config.php
  71. sed -i "s|<TPL:OPT_CHAT>|${ynh_opt_chat}|" -i /var/www/piratebox/config.php
  72. cp /etc/nginx/{,conf.d/}captive-piratebox.conf
  73. systemctl reload nginx
  74. }
  75. start_fakedns() {
  76. (/usr/local/bin/piratebox_fakedns "${ynh_ip4_nat_prefix}.1" &> /dev/null) &
  77. }
  78. ## Unsetters
  79. unset_nat4_dns() {
  80. iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j DNAT --to-destination "${ynh_ip4_nat_prefix}.1:4253"
  81. iptables -t nat -D POSTROUTING -o "${ynh_wifi_device}" -j MASQUERADE
  82. }
  83. unset_nat4_web() {
  84. iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p tcp --dport 80 -j REDIRECT --to-port 4280
  85. }
  86. unset_filt4_nohttps() {
  87. iptables -D INPUT -i "${ynh_wifi_device}" -p tcp --dport 443 -j REJECT
  88. }
  89. unset_filt4_nofwd() {
  90. iptables -D FORWARD -j REJECT -i "${ynh_wifi_device}"
  91. }
  92. unset_captive() {
  93. rm -f /etc/nginx/conf.d/captive-piratebox.conf
  94. systemctl reload nginx
  95. }
  96. stop_fakedns() {
  97. kill $(ps aux | grep piratebox_fakedns | awk '{ print $2 }' | head -n1)
  98. }
  99. ## Tools
  100. moulinette_get() {
  101. var=${1}
  102. gotcha=0
  103. while [ "${gotcha}" -eq 0 ]; do
  104. value=$(yunohost app setting piratebox "${var}")
  105. if [[ "${value}" =~ "An instance is already running" ]]; then
  106. sleep $(($((RANDOM%5)) + 1))
  107. else
  108. gotcha=1
  109. fi
  110. done
  111. echo "${value}"
  112. }
  113. moulinette_hotspot_get() {
  114. var=${1}
  115. gotcha=0
  116. while [ "${gotcha}" -eq 0 ]; do
  117. value=$(yunohost app setting hotspot "${var}")
  118. if [[ "${value}" =~ "An instance is already running" ]]; then
  119. sleep $(($((RANDOM%5)) + 1))
  120. else
  121. gotcha=1
  122. fi
  123. done
  124. echo "${value}"
  125. }
  126. if [ "$1" != restart ]; then
  127. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  128. if [ ! -e /tmp/.ynh-piratebox-boot ]; then
  129. touch /tmp/.ynh-piratebox-boot
  130. systemctl restart php5-fpm
  131. fi
  132. ynh_wifi_device_id=$(moulinette_get wifi_device_id)
  133. if [[ ! "${1}" =~ stop ]]; then
  134. exitcode=0
  135. if [ "${ynh_wifi_device_id}" -eq -1 ]; then
  136. echo "[WARN] You need to select an associated wifi hotspot (you can do it through the web admin)"
  137. exitcode=1
  138. fi
  139. [ "${exitcode}" -ne 0 ] && exit ${exitcode}
  140. fi
  141. # Variables
  142. echo -n "Retrieving Yunohost settings... "
  143. ynh_service_enabled=$(moulinette_get service_enabled)
  144. ynh_opt_renaming=$(moulinette_get opt_renaming)
  145. ynh_opt_deleting=$(moulinette_get opt_deleting)
  146. ynh_opt_chat=$(moulinette_get opt_chat)
  147. ynh_opt_name=$(moulinette_get opt_name)
  148. if [ "${ynh_wifi_device_id}" -eq 0 ]; then
  149. ynh_wifi_device="$(moulinette_hotspot_get wifi_device)"
  150. else
  151. ynh_wifi_device="hotspot${ynh_wifi_device_id}"
  152. fi
  153. IFS='|' read -a ynh_ip4_nat_prefix <<< "$(moulinette_hotspot_get ip4_nat_prefix)"
  154. ynh_ip4_nat_prefix=${ynh_ip4_nat_prefix[${ynh_wifi_device_id}]}
  155. echo "OK"
  156. fi
  157. # Script
  158. case "$1" in
  159. start)
  160. if is_running; then
  161. echo "Already started"
  162. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  163. echo "Disabled service"
  164. elif ! has_hotspot_app; then
  165. echo "[ERR] Hotspot is not running"
  166. else
  167. echo "[piratebox] Starting..."
  168. touch /tmp/.ynh-piratebox-started
  169. # Purge tmp folder
  170. rm -rf /var/spool/piratebox/*
  171. # Set IPv4 DNS NAT
  172. if ! is_nat4_dns_set; then
  173. echo "Set IPv4 DNS NAT"
  174. set_nat4_dns
  175. fi
  176. # Set IPv4 Web NAT
  177. if ! is_nat4_web_set; then
  178. echo "Set IPv4 Web NAT"
  179. set_nat4_web
  180. fi
  181. # Set IPv4 No Https-filter rule
  182. if ! is_filt4_nohttps_set; then
  183. echo "Set IPv4 No Https-filter rule"
  184. set_filt4_nohttps
  185. fi
  186. # Set IPv4 No Forwarding-filter rule
  187. if ! is_filt4_nofwd_set; then
  188. echo "Set IPv4 No Forwarding-filter rule"
  189. set_filt4_nofwd
  190. fi
  191. # Set captive configuration
  192. if ! is_captive_set; then
  193. echo "Set the captive portal configuration"
  194. set_captive
  195. fi
  196. # Run fakedns
  197. if ! is_fakedns_running; then
  198. echo "Run fakedns"
  199. start_fakedns
  200. fi
  201. fi
  202. ;;
  203. stop)
  204. echo "[piratebox] Stopping..."
  205. rm -f /tmp/.ynh-piratebox-started
  206. if is_nat4_dns_set; then
  207. echo "Unset IPv4 DNS NAT"
  208. unset_nat4_dns
  209. fi
  210. if is_nat4_web_set; then
  211. echo "Unset IPv4 Web NAT"
  212. unset_nat4_web
  213. fi
  214. if is_filt4_nohttps_set; then
  215. echo "Unset IPv4 No Https-filter rule"
  216. unset_filt4_nohttps
  217. fi
  218. if is_filt4_nofwd_set; then
  219. echo "Unset IPv4 No Forwarding-filter rule"
  220. unset_filt4_nofwd
  221. fi
  222. if is_captive_set; then
  223. echo "Unset the captive portal"
  224. unset_captive
  225. fi
  226. if is_fakedns_running; then
  227. echo "Stop fakedns"
  228. stop_fakedns
  229. fi
  230. rm -rf /var/spool/piratebox/*
  231. ;;
  232. restart)
  233. $0 stop
  234. $0 start
  235. ;;
  236. status)
  237. exitcode=0
  238. if [ "${ynh_service_enabled}" -eq 0 ]; then
  239. echo "[ERR] PirateBox Service disabled"
  240. exitcode=1
  241. fi
  242. if ! has_hotspot_app; then
  243. echo "[ERR] Hotspot is not running"
  244. exitcode=1
  245. fi
  246. if is_nat4_dns_set; then
  247. echo "[OK] IPv4 DNS NAT set"
  248. else
  249. echo "[ERR] No IPv4 DNS NAT set"
  250. exitcode=1
  251. fi
  252. if is_nat4_web_set; then
  253. echo "[OK] IPv4 Web NAT set"
  254. else
  255. echo "[ERR] No IPv4 Web NAT set"
  256. exitcode=1
  257. fi
  258. if is_filt4_nohttps_set; then
  259. echo "[OK] IPv4 No Https-filter rule set"
  260. else
  261. echo "[ERR] No IPv4 No Https-filter rule set"
  262. exitcode=1
  263. fi
  264. if is_filt4_nofwd_set; then
  265. echo "[OK] IPv4 No Forwarding-filter rule set"
  266. else
  267. echo "[ERR] No IPv4 No Forwarding-filter rule set"
  268. exitcode=1
  269. fi
  270. if is_captive_set; then
  271. echo "[OK] Captive portal set"
  272. else
  273. echo "[ERR] No captive portal set"
  274. exitcode=1
  275. fi
  276. if is_fakedns_running; then
  277. echo "[OK] Fakedns is running"
  278. else
  279. echo "[ERR] Fakedns is not running"
  280. exitcode=1
  281. fi
  282. exit ${exitcode}
  283. ;;
  284. *)
  285. echo "Usage: $0 {start|stop|restart|status}"
  286. exit 1
  287. ;;
  288. esac
  289. exit 0