init_ynh-hotspot 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-hotspot
  4. # Required-Start: $network $remote_fs $syslog $all
  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 hotspot.
  9. # Description: Set prerequisites for wifi hotspot.
  10. ### END INIT INFO
  11. # Functions
  12. ## State functions
  13. has_vpnclient_app() {
  14. [ -e /tmp/.ynh-vpnclient-started ]
  15. }
  16. has_ip6delegatedprefix() {
  17. [ "${ynh_ip6_net}" != none ]
  18. }
  19. is_ndproxy_set() {
  20. proxy=$(ip -6 neighbour show proxy)
  21. [ ! -z "${proxy}" ]
  22. }
  23. is_nat_set() {
  24. internet_device=${1}
  25. iptables -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${internet_device}"
  26. }
  27. is_ip4nataddr_set() {
  28. ip address show dev "${ynh_wifi_device}" 2> /dev/null | grep -q "${ynh_ip4_nat_prefix}.1/24"
  29. }
  30. is_ip6addr_set() {
  31. ip address show dev "${ynh_wifi_device}" 2> /dev/null | grep -q "${ynh_ip6_addr}/64"
  32. }
  33. is_forwarding_set() {
  34. ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }')
  35. ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }')
  36. [ "${ip6}" -eq 1 -a "${ip4}" -eq 1 ]
  37. }
  38. is_hostapd_running() {
  39. service hostapd status &> /dev/null
  40. }
  41. is_radvd_running() {
  42. service radvd status &> /dev/null
  43. }
  44. is_dhcpd_running() {
  45. service isc-dhcp-server status &> /dev/null
  46. }
  47. is_running() {
  48. ( has_ip6delegatedprefix && is_ip6addr_set && is_radvd_running && is_ndproxy_set || ! has_ip6delegatedprefix )\
  49. && is_nat_set "${new_internet_device}" && is_ip4nataddr_set && is_forwarding_set && is_hostapd_running\
  50. && is_dhcpd_running
  51. }
  52. ## Setters
  53. set_ndproxy() {
  54. ip -6 neighbour add proxy "${ynh_ip6_addr}" dev "${ynh_wifi_device}"
  55. }
  56. set_nat() {
  57. internet_device=${1}
  58. iptables -t nat -A POSTROUTING -o "${internet_device}" -j MASQUERADE
  59. }
  60. set_ip4nataddr() {
  61. ip address add "${ynh_ip4_nat_prefix}.1/24" dev "${ynh_wifi_device}"
  62. }
  63. set_ip6addr() {
  64. ip address delete "${ynh_ip6_addr}/64" dev tun0 &> /dev/null
  65. ip address add "${ynh_ip6_addr}/64" dev "${ynh_wifi_device}"
  66. }
  67. set_forwarding() {
  68. sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null
  69. sysctl -w net.ipv4.conf.all.forwarding=1 > /dev/null
  70. }
  71. start_hostapd() {
  72. cp /etc/hostapd/hostapd.conf{.tpl,}
  73. sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/hostapd/hostapd.conf
  74. sed "s|<TPL:WIFI_SSID>|${ynh_wifi_ssid}|g" -i /etc/hostapd/hostapd.conf
  75. sed "s|<TPL:WIFI_PASSPHRASE>|${ynh_wifi_passphrase}|g" -i /etc/hostapd/hostapd.conf
  76. sed "s|<TPL:WIFI_CHANNEL>|${ynh_wifi_channel}|g" -i /etc/hostapd/hostapd.conf
  77. if [ "${ynh_wifi_n}" -eq 1 ]; then
  78. sed "s|<TPL:N_COMMENT>||g" -i /etc/hostapd/hostapd.conf
  79. else
  80. sed "s|<TPL:N_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf
  81. fi
  82. service hostapd start
  83. }
  84. start_radvd() {
  85. cp /etc/radvd.conf{.tpl,}
  86. sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/radvd.conf
  87. sed "s|<TPL:IP6_NET>|${ynh_ip6_net}|g" -i /etc/radvd.conf
  88. sed "s|<TPL:IP6_DNS0>|${ynh_ip6_dns0}|g" -i /etc/radvd.conf
  89. sed "s|<TPL:IP6_DNS1>|${ynh_ip6_dns1}|g" -i /etc/radvd.conf
  90. service radvd start
  91. }
  92. start_dhcpd() {
  93. cp /etc/dhcp/dhcpd.conf{.tpl,}
  94. sed "s|<TPL:IP4_DNS0>|${ynh_ip4_dns0}|g" -i /etc/dhcp/dhcpd.conf
  95. sed "s|<TPL:IP4_DNS1>|${ynh_ip4_dns1}|g" -i /etc/dhcp/dhcpd.conf
  96. sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/dhcp/dhcpd.conf
  97. sed "s|<TPL:IP4_NAT_PREFIX>|${ynh_ip4_nat_prefix}|g" -i /etc/dhcp/dhcpd.conf
  98. service isc-dhcp-server start
  99. }
  100. ## Unsetters
  101. unset_ndproxy() {
  102. ip -6 neighbour delete proxy "${ynh_ip6_addr}" dev "${ynh_wifi_device}"
  103. }
  104. unset_nat() {
  105. internet_device=${1}
  106. iptables -t nat -D POSTROUTING -o "${internet_device}" -j MASQUERADE
  107. }
  108. unset_ip4nataddr() {
  109. ip address delete "${ynh_ip4_nat_prefix}.1/24" dev "${ynh_wifi_device}"
  110. }
  111. unset_ip6addr() {
  112. ip address delete "${ynh_ip6_addr}/64" dev "${ynh_wifi_device}"
  113. }
  114. unset_forwarding() {
  115. sysctl -w net.ipv6.conf.all.forwarding=0 > /dev/null
  116. sysctl -w net.ipv4.conf.all.forwarding=0 > /dev/null
  117. }
  118. stop_hostapd() {
  119. service hostapd stop
  120. }
  121. stop_radvd() {
  122. service radvd stop
  123. }
  124. stop_dhcpd() {
  125. service isc-dhcp-server stop
  126. }
  127. ## Tools
  128. moulinette_get() {
  129. var=${1}
  130. value=$(yunohost app setting hotspot "${var}")
  131. if [[ "${value}" =~ "An instance is already running" ]]; then
  132. echo "${value}" >&2
  133. exit 1
  134. fi
  135. echo "${value}"
  136. }
  137. moulinette_vpnclient_get() {
  138. var=${1}
  139. value=$(yunohost app setting vpnclient "${var}")
  140. if [[ "${value}" =~ "An instance is already running" ]]; then
  141. echo "${value}" >&2
  142. exit 1
  143. fi
  144. echo "${value}"
  145. }
  146. moulinette_set() {
  147. var=${1}
  148. value=${2}
  149. msg=$(yunohost app setting hotspot "${var}" -v "${value}")
  150. if [ ! $? -eq 0 ]; then
  151. echo "${msg}" >&2
  152. exit 1
  153. fi
  154. }
  155. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  156. if [ ! -e /tmp/.ynh-hotspot-boot ]; then
  157. touch /tmp/.ynh-hotspot-boot
  158. service php5-fpm restart
  159. fi
  160. # Variables
  161. echo -n "Retrieving Yunohost settings... "
  162. ynh_wifi_device=$(moulinette_get wifi_device)
  163. ynh_wifi_ssid=$(moulinette_get wifi_ssid)
  164. ynh_wifi_passphrase=$(moulinette_get wifi_passphrase)
  165. ynh_wifi_channel=$(moulinette_get wifi_channel)
  166. ynh_wifi_n=$(moulinette_get wifi_n)
  167. ynh_ip6_addr=$(moulinette_get ip6_addr)
  168. ynh_ip6_net=$(moulinette_get ip6_net)
  169. ynh_ip6_dns0=$(moulinette_get ip6_dns0)
  170. ynh_ip6_dns1=$(moulinette_get ip6_dns1)
  171. ynh_ip4_dns0=$(moulinette_get ip4_dns0)
  172. ynh_ip4_dns1=$(moulinette_get ip4_dns1)
  173. ynh_ip4_nat_prefix=$(moulinette_get ip4_nat_prefix)
  174. old_internet_device=$(moulinette_get internet_device)
  175. new_internet_device=$(ip route | awk '/default via/ { print $NF; }')
  176. # Switch the NAT interface if there is a VPN
  177. ip link show dev tun0 &> /dev/null
  178. if [ "$?" -eq 0 ]; then
  179. new_internet_device=tun0
  180. fi
  181. echo "OK"
  182. # Check IPv6 delegated prefix from vpnclient
  183. if [ "${ynh_ip6_net}" == none ]; then
  184. ynh_ip6_net=$(moulinette_vpnclient_get ip6_net)
  185. ynh_ip6_addr=$(moulinette_vpnclient_get ip6_addr)
  186. moulinette_set ip6_net "${ynh_ip6_net}"
  187. moulinette_set ip6_addr "${ynh_ip6_addr}"
  188. else
  189. vpnclient_ip6_net=$(moulinette_vpnclient_get ip6_net)
  190. if [ "${ynh_ip6_net}" != "${vpnclient_ip6_net}" ]; then
  191. echo "[WARN] The IPv6 delegated prefix is different from the vpnclient one"
  192. fi
  193. fi
  194. # Script
  195. case "$1" in
  196. start)
  197. if is_running; then
  198. echo "Already started"
  199. else
  200. echo "[hotspot] Starting..."
  201. touch /tmp/.ynh-hotspot-started
  202. if [ "${new_internet_device}" == tun0 ]; then
  203. moulinette_set vpnclient yes
  204. else
  205. moulinette_set vpnclient no
  206. fi
  207. # Set NDP proxy
  208. if has_ip6delegatedprefix && ! is_ndproxy_set; then
  209. echo "Set NDP proxy"
  210. set_ndproxy
  211. fi
  212. # Check old state of the ipv4 NAT settings
  213. if [ ! -z "${old_internet_device}" -a "${new_internet_device}" != "${old_internet_device}" ]\
  214. && is_nat_set "${old_internet_device}"; then
  215. unset_nat "${old_internet_device}"
  216. fi
  217. # Set ipv4 NAT
  218. if ! is_nat_set "${new_internet_device}"; then
  219. echo "Set NAT"
  220. set_nat "${new_internet_device}"
  221. fi
  222. # Set ipv4 NAT address
  223. if ! is_ip4nataddr_set; then
  224. echo "Set IPv4 NAT address"
  225. set_ip4nataddr
  226. fi
  227. # Set the ipv6 address
  228. if has_ip6delegatedprefix && ! is_ip6addr_set; then
  229. echo "Set IPv6 address"
  230. set_ip6addr
  231. fi
  232. # Set forwarding for ipv6 and ipv4
  233. if ! is_forwarding_set; then
  234. echo "Set forwarding"
  235. set_forwarding
  236. fi
  237. # Run hostapd
  238. if ! is_hostapd_running; then
  239. echo "Run hostapd"
  240. start_hostapd
  241. sleep 1
  242. fi
  243. # Run radvd
  244. # must be running after hostapd
  245. if has_ip6delegatedprefix && ! is_radvd_running; then
  246. echo "Run radvd"
  247. start_radvd
  248. fi
  249. # Run dhcpd
  250. # "options routers" addr (is_ip6addr_set) must be set before
  251. if ! is_dhcpd_running; then
  252. echo "Run dhcpd"
  253. start_dhcpd
  254. fi
  255. # Update dynamic settings
  256. moulinette_set internet_device "${new_internet_device}"
  257. fi
  258. ;;
  259. stop)
  260. echo "[hotspot] Stopping..."
  261. rm /tmp/.ynh-hotspot-started
  262. if has_ip6delegatedprefix && is_ndproxy_set; then
  263. echo "Unset NDP proxy"
  264. unset_ndproxy
  265. fi
  266. if is_nat_set "${old_internet_device}"; then
  267. echo "Unset NAT"
  268. unset_nat "${old_internet_device}"
  269. fi
  270. if is_ip4nataddr_set; then
  271. echo "Unset IPv4 NAT address"
  272. unset_ip4nataddr
  273. fi
  274. if has_ip6delegatedprefix && is_ip6addr_set; then
  275. echo "Unset IPv6 address"
  276. unset_ip6addr
  277. fi
  278. if is_forwarding_set; then
  279. echo "Unset forwarding"
  280. unset_forwarding
  281. fi
  282. if is_hostapd_running; then
  283. echo "Stop hostapd"
  284. stop_hostapd
  285. fi
  286. if has_ip6delegatedprefix && is_radvd_running; then
  287. echo "Stop radvd"
  288. stop_radvd
  289. fi
  290. if is_dhcpd_running; then
  291. echo "Stop dhcpd"
  292. stop_dhcpd
  293. fi
  294. if has_vpnclient_app; then
  295. service ynh-vpnclient start
  296. fi
  297. ;;
  298. status)
  299. exitcode=0
  300. if has_ip6delegatedprefix; then
  301. echo "[INFO] IPv6 delegated prefix found"
  302. if is_ndproxy_set; then
  303. echo "[OK] NDP proxy set"
  304. else
  305. echo "[ERR] No NDP proxy set"
  306. exitcode=1
  307. fi
  308. if is_ip6addr_set; then
  309. echo "[OK] IPv6 address set"
  310. else
  311. echo "[ERR] No IPv6 address set"
  312. exitcode=1
  313. fi
  314. if is_radvd_running; then
  315. echo "[OK] Radvd is running"
  316. else
  317. echo "[ERR] Radvd is not running"
  318. exitcode=1
  319. fi
  320. else
  321. echo "[INFO] No IPv6 delegated prefix found"
  322. fi
  323. if is_nat_set "${new_internet_device}"; then
  324. echo "[OK] IPv4 NAT set"
  325. else
  326. echo "[ERR] No IPv4 NAT set"
  327. exitcode=1
  328. fi
  329. if is_ip4nataddr_set; then
  330. echo "[OK] IPv4 NAT address set"
  331. else
  332. echo "[ERR] No IPv4 NAT address set"
  333. exitcode=1
  334. fi
  335. if is_forwarding_set; then
  336. echo "[OK] IPv6/IPv4 forwarding set"
  337. else
  338. echo "[ERR] No IPv6/IPv4 forwarding set"
  339. exitcode=1
  340. fi
  341. if is_hostapd_running; then
  342. echo "[OK] Hostapd is running"
  343. else
  344. echo "[ERR] Hostapd is not running"
  345. exitcode=1
  346. fi
  347. if is_dhcpd_running; then
  348. echo "[OK] Dhcpd is running"
  349. else
  350. echo "[ERR] Dhcpd is not running"
  351. exitcode=1
  352. fi
  353. exit ${exitcode}
  354. ;;
  355. *)
  356. echo "Usage: $0 {start|stop|status}"
  357. exit 1
  358. ;;
  359. esac
  360. exit 0