ynh-piratebox 8.4 KB

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