ynh-piratebox 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 ynh-hotspot
  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_captive_set \
  53. && is_fakedns_running
  54. }
  55. is_captive_set() {
  56. ls /etc/nginx/conf.d/captive-piratebox.conf &> /dev/null
  57. }
  58. ## Setters
  59. # The IPv6 NAT features are not available available before the kernel 3.8
  60. # therefore the IPv6-compliance will be add when Debian Stable has a
  61. # compliant kernel
  62. set_nat4_dns() {
  63. iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j DNAT --to-destination "${ynh_ip4_nat_prefix}.1:4253"
  64. iptables -t nat -A POSTROUTING -o "${ynh_wifi_device}" -j MASQUERADE
  65. }
  66. set_nat4_web() {
  67. iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p tcp --dport 80 -j REDIRECT --to-port 4280
  68. }
  69. set_filt4_nohttps() {
  70. iptables -I INPUT 1 -i "${ynh_wifi_device}" -p tcp --dport 443 -j REJECT
  71. }
  72. set_filt4_nofwd() {
  73. iptables -I FORWARD 1 -j REJECT -i "${ynh_wifi_device}"
  74. }
  75. set_captive() {
  76. cp /var/www/piratebox/config.{tpl.,}php
  77. sed -i "s|<TPL:OPT_NAME>|${ynh_opt_name}|" -i /var/www/piratebox/config.php
  78. sed -i "s|<TPL:OPT_RENAMING>|${ynh_opt_renaming}|" -i /var/www/piratebox/config.php
  79. sed -i "s|<TPL:OPT_DELETING>|${ynh_opt_deleting}|" -i /var/www/piratebox/config.php
  80. sed -i "s|<TPL:OPT_CHAT>|${ynh_opt_chat}|" -i /var/www/piratebox/config.php
  81. cp /etc/nginx/{,conf.d/}captive-piratebox.conf
  82. systemctl reload nginx --quiet
  83. }
  84. start_fakedns() {
  85. (/usr/local/bin/piratebox_fakedns "${ynh_ip4_nat_prefix}.1" &> /dev/null) &
  86. }
  87. ## Unsetters
  88. unset_nat4_dns() {
  89. iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j DNAT --to-destination "${ynh_ip4_nat_prefix}.1:4253"
  90. iptables -t nat -D POSTROUTING -o "${ynh_wifi_device}" -j MASQUERADE
  91. }
  92. unset_nat4_web() {
  93. iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p tcp --dport 80 -j REDIRECT --to-port 4280
  94. }
  95. unset_filt4_nohttps() {
  96. iptables -D INPUT -i "${ynh_wifi_device}" -p tcp --dport 443 -j REJECT
  97. }
  98. unset_filt4_nofwd() {
  99. iptables -D FORWARD -j REJECT -i "${ynh_wifi_device}"
  100. }
  101. unset_captive() {
  102. rm -f /etc/nginx/conf.d/captive-piratebox.conf
  103. systemctl reload nginx --quiet
  104. }
  105. stop_fakedns() {
  106. kill $(ps aux | grep piratebox_fakedns | awk '{ print $2 }' | head -n1)
  107. }
  108. ## Tools
  109. moulinette_get() {
  110. var=${1}
  111. value=$(yunohost app setting piratebox "${var}")
  112. if [[ "${value}" =~ "An instance is already running" ]]; then
  113. echo "${value}" >&2
  114. exit 1
  115. fi
  116. echo "${value}"
  117. }
  118. moulinette_hotspot_get() {
  119. var=${1}
  120. value=$(yunohost app setting hotspot "${var}")
  121. if [[ "${value}" =~ "An instance is already running" ]]; then
  122. echo "${value}" >&2
  123. exit 1
  124. fi
  125. echo "${value}"
  126. }
  127. if [ "$1" != restart ]; then
  128. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  129. if [ ! -e /tmp/.ynh-piratebox-boot ]; then
  130. touch /tmp/.ynh-piratebox-boot
  131. systemctl restart php5-fpm --quiet
  132. fi
  133. # Variables
  134. echo -n "Retrieving Yunohost settings... "
  135. ynh_service_enabled=$(moulinette_get service_enabled)
  136. ynh_wifi_device_id=$(moulinette_get wifi_device_id)
  137. ynh_opt_renaming=$(moulinette_get opt_renaming)
  138. ynh_opt_deleting=$(moulinette_get opt_deleting)
  139. ynh_opt_chat=$(moulinette_get opt_chat)
  140. ynh_opt_name=$(moulinette_get opt_name)
  141. if [ "${ynh_wifi_device_id}" -eq 0 ]; then
  142. ynh_wifi_device="$(moulinette_hotspot_get wifi_device)"
  143. else
  144. ynh_wifi_device="hotspot${ynh_wifi_device_id}"
  145. fi
  146. IFS='|' read -a ynh_ip4_nat_prefix <<< "$(moulinette_hotspot_get ip4_nat_prefix)"
  147. ynh_ip4_nat_prefix=${ynh_ip4_nat_prefix[${ynh_wifi_device_id}]}
  148. echo "OK"
  149. fi
  150. # Script
  151. case "$1" in
  152. start)
  153. if is_running; then
  154. echo "Already started"
  155. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  156. echo "Disabled service"
  157. elif ! has_hotspot_app; then
  158. echo "[ERR] Hotspot is not running"
  159. else
  160. echo "[piratebox] Starting..."
  161. touch /tmp/.ynh-piratebox-started
  162. # Purge tmp folder
  163. rm -rf /var/spool/piratebox/*
  164. # Set IPv4 DNS NAT
  165. if ! is_nat4_dns_set; then
  166. echo "Set IPv4 DNS NAT"
  167. set_nat4_dns
  168. fi
  169. # Set IPv4 Web NAT
  170. if ! is_nat4_web_set; then
  171. echo "Set IPv4 Web NAT"
  172. set_nat4_web
  173. fi
  174. # Set IPv4 No Https-filter rule
  175. if ! is_filt4_nohttps_set; then
  176. echo "Set IPv4 No Https-filter rule"
  177. set_filt4_nohttps
  178. fi
  179. # Set IPv4 No Forwarding-filter rule
  180. if ! is_filt4_nofwd_set; then
  181. echo "Set IPv4 No Forwarding-filter rule"
  182. set_filt4_nofwd
  183. fi
  184. # Set captive configuration
  185. if ! is_captive_set; then
  186. echo "Set the captive portal configuration"
  187. set_captive
  188. fi
  189. # Run fakedns
  190. if ! is_fakedns_running; then
  191. echo "Run fakedns"
  192. start_fakedns
  193. fi
  194. fi
  195. ;;
  196. stop)
  197. echo "[piratebox] Stopping..."
  198. rm -f /tmp/.ynh-piratebox-started
  199. if is_nat4_dns_set; then
  200. echo "Unset IPv4 DNS NAT"
  201. unset_nat4_dns
  202. fi
  203. if is_nat4_web_set; then
  204. echo "Unset IPv4 Web NAT"
  205. unset_nat4_web
  206. fi
  207. if is_filt4_nohttps_set; then
  208. echo "Unset IPv4 No Https-filter rule"
  209. unset_filt4_nohttps
  210. fi
  211. if is_filt4_nofwd_set; then
  212. echo "Unset IPv4 No Forwarding-filter rule"
  213. unset_filt4_nofwd
  214. fi
  215. if is_captive_set; then
  216. echo "Unset the captive portal"
  217. unset_captive
  218. fi
  219. if is_fakedns_running; then
  220. echo "Stop fakedns"
  221. stop_fakedns
  222. fi
  223. rm -rf /var/spool/piratebox/*
  224. ;;
  225. restart)
  226. $0 stop
  227. $0 start
  228. ;;
  229. status)
  230. exitcode=0
  231. if [ "${ynh_service_enabled}" -eq 0 ]; then
  232. echo "[ERR] PirateBox Service disabled"
  233. exitcode=1
  234. fi
  235. if ! has_hotspot_app; then
  236. echo "[ERR] Hotspot is not running"
  237. exitcode=1
  238. fi
  239. if is_nat4_dns_set; then
  240. echo "[OK] IPv4 DNS NAT set"
  241. else
  242. echo "[ERR] No IPv4 DNS NAT set"
  243. exitcode=1
  244. fi
  245. if is_nat4_web_set; then
  246. echo "[OK] IPv4 Web NAT set"
  247. else
  248. echo "[ERR] No IPv4 Web NAT set"
  249. exitcode=1
  250. fi
  251. if is_filt4_nohttps_set; then
  252. echo "[OK] IPv4 No Https-filter rule set"
  253. else
  254. echo "[ERR] No IPv4 No Https-filter rule set"
  255. exitcode=1
  256. fi
  257. if is_filt4_nofwd_set; then
  258. echo "[OK] IPv4 No Forwarding-filter rule set"
  259. else
  260. echo "[ERR] No IPv4 No Forwarding-filter rule set"
  261. exitcode=1
  262. fi
  263. if is_captive_set; then
  264. echo "[OK] Captive portal set"
  265. else
  266. echo "[ERR] No captive portal set"
  267. exitcode=1
  268. fi
  269. if is_fakedns_running; then
  270. echo "[OK] Fakedns is running"
  271. else
  272. echo "[ERR] Fakedns is not running"
  273. exitcode=1
  274. fi
  275. exit ${exitcode}
  276. ;;
  277. *)
  278. echo "Usage: $0 {start|stop|restart|status}"
  279. exit 1
  280. ;;
  281. esac
  282. exit 0