ynh-piratebox 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 --quiet
  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 --quiet
  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. value=$(yunohost app setting piratebox "${var}")
  103. if [[ "${value}" =~ "An instance is already running" ]]; then
  104. echo "${value}" >&2
  105. exit 1
  106. fi
  107. echo "${value}"
  108. }
  109. moulinette_hotspot_get() {
  110. var=${1}
  111. value=$(yunohost app setting hotspot "${var}")
  112. if [[ "${value}" =~ "An instance is already running" ]]; then
  113. echo "${value}" >&2
  114. exit 1
  115. fi
  116. echo "${value}"
  117. }
  118. if [ "$1" != restart ]; then
  119. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  120. if [ ! -e /tmp/.ynh-piratebox-boot ]; then
  121. touch /tmp/.ynh-piratebox-boot
  122. systemctl restart php5-fpm --quiet
  123. fi
  124. # Variables
  125. echo -n "Retrieving Yunohost settings... "
  126. ynh_service_enabled=$(moulinette_get service_enabled)
  127. ynh_wifi_device_id=$(moulinette_get wifi_device_id)
  128. ynh_opt_renaming=$(moulinette_get opt_renaming)
  129. ynh_opt_deleting=$(moulinette_get opt_deleting)
  130. ynh_opt_chat=$(moulinette_get opt_chat)
  131. ynh_opt_name=$(moulinette_get opt_name)
  132. if [ "${ynh_wifi_device_id}" -eq 0 ]; then
  133. ynh_wifi_device="$(moulinette_hotspot_get wifi_device)"
  134. else
  135. ynh_wifi_device="hotspot${ynh_wifi_device_id}"
  136. fi
  137. IFS='|' read -a ynh_ip4_nat_prefix <<< "$(moulinette_hotspot_get ip4_nat_prefix)"
  138. ynh_ip4_nat_prefix=${ynh_ip4_nat_prefix[${ynh_wifi_device_id}]}
  139. echo "OK"
  140. fi
  141. # Script
  142. case "$1" in
  143. start)
  144. if is_running; then
  145. echo "Already started"
  146. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  147. echo "Disabled service"
  148. elif ! has_hotspot_app; then
  149. echo "[ERR] Hotspot is not running"
  150. else
  151. echo "[piratebox] Starting..."
  152. touch /tmp/.ynh-piratebox-started
  153. # Purge tmp folder
  154. rm -rf /var/spool/piratebox/*
  155. # Set IPv4 DNS NAT
  156. if ! is_nat4_dns_set; then
  157. echo "Set IPv4 DNS NAT"
  158. set_nat4_dns
  159. fi
  160. # Set IPv4 Web NAT
  161. if ! is_nat4_web_set; then
  162. echo "Set IPv4 Web NAT"
  163. set_nat4_web
  164. fi
  165. # Set IPv4 No Https-filter rule
  166. if ! is_filt4_nohttps_set; then
  167. echo "Set IPv4 No Https-filter rule"
  168. set_filt4_nohttps
  169. fi
  170. # Set IPv4 No Forwarding-filter rule
  171. if ! is_filt4_nofwd_set; then
  172. echo "Set IPv4 No Forwarding-filter rule"
  173. set_filt4_nofwd
  174. fi
  175. # Set captive configuration
  176. if ! is_captive_set; then
  177. echo "Set the captive portal configuration"
  178. set_captive
  179. fi
  180. # Run fakedns
  181. if ! is_fakedns_running; then
  182. echo "Run fakedns"
  183. start_fakedns
  184. fi
  185. fi
  186. ;;
  187. stop)
  188. echo "[piratebox] Stopping..."
  189. rm -f /tmp/.ynh-piratebox-started
  190. if is_nat4_dns_set; then
  191. echo "Unset IPv4 DNS NAT"
  192. unset_nat4_dns
  193. fi
  194. if is_nat4_web_set; then
  195. echo "Unset IPv4 Web NAT"
  196. unset_nat4_web
  197. fi
  198. if is_filt4_nohttps_set; then
  199. echo "Unset IPv4 No Https-filter rule"
  200. unset_filt4_nohttps
  201. fi
  202. if is_filt4_nofwd_set; then
  203. echo "Unset IPv4 No Forwarding-filter rule"
  204. unset_filt4_nofwd
  205. fi
  206. if is_captive_set; then
  207. echo "Unset the captive portal"
  208. unset_captive
  209. fi
  210. if is_fakedns_running; then
  211. echo "Stop fakedns"
  212. stop_fakedns
  213. fi
  214. rm -rf /var/spool/piratebox/*
  215. ;;
  216. restart)
  217. $0 stop
  218. $0 start
  219. ;;
  220. status)
  221. exitcode=0
  222. if [ "${ynh_service_enabled}" -eq 0 ]; then
  223. echo "[ERR] PirateBox Service disabled"
  224. exitcode=1
  225. fi
  226. if ! has_hotspot_app; then
  227. echo "[ERR] Hotspot is not running"
  228. exitcode=1
  229. fi
  230. if is_nat4_dns_set; then
  231. echo "[OK] IPv4 DNS NAT set"
  232. else
  233. echo "[ERR] No IPv4 DNS NAT set"
  234. exitcode=1
  235. fi
  236. if is_nat4_web_set; then
  237. echo "[OK] IPv4 Web NAT set"
  238. else
  239. echo "[ERR] No IPv4 Web NAT set"
  240. exitcode=1
  241. fi
  242. if is_filt4_nohttps_set; then
  243. echo "[OK] IPv4 No Https-filter rule set"
  244. else
  245. echo "[ERR] No IPv4 No Https-filter rule set"
  246. exitcode=1
  247. fi
  248. if is_filt4_nofwd_set; then
  249. echo "[OK] IPv4 No Forwarding-filter rule set"
  250. else
  251. echo "[ERR] No IPv4 No Forwarding-filter rule set"
  252. exitcode=1
  253. fi
  254. if is_captive_set; then
  255. echo "[OK] Captive portal set"
  256. else
  257. echo "[ERR] No captive portal set"
  258. exitcode=1
  259. fi
  260. if is_fakedns_running; then
  261. echo "[OK] Fakedns is running"
  262. else
  263. echo "[ERR] Fakedns is not running"
  264. exitcode=1
  265. fi
  266. exit ${exitcode}
  267. ;;
  268. *)
  269. echo "Usage: $0 {start|stop|restart|status}"
  270. exit 1
  271. ;;
  272. esac
  273. exit 0