init_ynh-hotspot 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. vpnclient_ip6_net=$(moulinette_vpnclient_get ip6_net)
  184. if [ ! -z "${vpnclient_ip6_addr}" ]; then
  185. if [ "${ynh_ip6_net}" == none ]; then
  186. ynh_ip6_net=$vpnclient_ip6_net
  187. ynh_ip6_addr=$(moulinette_vpnclient_get ip6_addr)
  188. moulinette_set ip6_net "${ynh_ip6_net}"
  189. moulinette_set ip6_addr "${ynh_ip6_addr}"
  190. else
  191. if [ "${ynh_ip6_net}" != "${vpnclient_ip6_net}" ]; then
  192. echo "[WARN] The IPv6 delegated prefix is different from the vpnclient one"
  193. fi
  194. fi
  195. fi
  196. # Script
  197. case "$1" in
  198. start)
  199. if is_running; then
  200. echo "Already started"
  201. else
  202. echo "[hotspot] Starting..."
  203. touch /tmp/.ynh-hotspot-started
  204. if [ "${new_internet_device}" == tun0 ]; then
  205. moulinette_set vpnclient yes
  206. else
  207. moulinette_set vpnclient no
  208. fi
  209. # Set NDP proxy
  210. if has_ip6delegatedprefix && ! is_ndproxy_set; then
  211. echo "Set NDP proxy"
  212. set_ndproxy
  213. fi
  214. # Check old state of the ipv4 NAT settings
  215. if [ ! -z "${old_internet_device}" -a "${new_internet_device}" != "${old_internet_device}" ]\
  216. && is_nat_set "${old_internet_device}"; then
  217. unset_nat "${old_internet_device}"
  218. fi
  219. # Set ipv4 NAT
  220. if ! is_nat_set "${new_internet_device}"; then
  221. echo "Set NAT"
  222. set_nat "${new_internet_device}"
  223. fi
  224. # Set ipv4 NAT address
  225. if ! is_ip4nataddr_set; then
  226. echo "Set IPv4 NAT address"
  227. set_ip4nataddr
  228. fi
  229. # Set the ipv6 address
  230. if has_ip6delegatedprefix && ! is_ip6addr_set; then
  231. echo "Set IPv6 address"
  232. set_ip6addr
  233. fi
  234. # Set forwarding for ipv6 and ipv4
  235. if ! is_forwarding_set; then
  236. echo "Set forwarding"
  237. set_forwarding
  238. fi
  239. # Run hostapd
  240. if ! is_hostapd_running; then
  241. echo "Run hostapd"
  242. start_hostapd
  243. sleep 1
  244. fi
  245. # Run radvd
  246. # must be running after hostapd
  247. if has_ip6delegatedprefix && ! is_radvd_running; then
  248. echo "Run radvd"
  249. start_radvd
  250. fi
  251. # Run dhcpd
  252. # "options routers" addr (is_ip6addr_set) must be set before
  253. if ! is_dhcpd_running; then
  254. echo "Run dhcpd"
  255. start_dhcpd
  256. fi
  257. # Update dynamic settings
  258. moulinette_set internet_device "${new_internet_device}"
  259. fi
  260. ;;
  261. stop)
  262. echo "[hotspot] Stopping..."
  263. rm /tmp/.ynh-hotspot-started
  264. if has_ip6delegatedprefix && is_ndproxy_set; then
  265. echo "Unset NDP proxy"
  266. unset_ndproxy
  267. fi
  268. if is_nat_set "${old_internet_device}"; then
  269. echo "Unset NAT"
  270. unset_nat "${old_internet_device}"
  271. fi
  272. if is_ip4nataddr_set; then
  273. echo "Unset IPv4 NAT address"
  274. unset_ip4nataddr
  275. fi
  276. if has_ip6delegatedprefix && is_ip6addr_set; then
  277. echo "Unset IPv6 address"
  278. unset_ip6addr
  279. fi
  280. if is_forwarding_set; then
  281. echo "Unset forwarding"
  282. unset_forwarding
  283. fi
  284. if is_hostapd_running; then
  285. echo "Stop hostapd"
  286. stop_hostapd
  287. fi
  288. if has_ip6delegatedprefix && is_radvd_running; then
  289. echo "Stop radvd"
  290. stop_radvd
  291. fi
  292. if is_dhcpd_running; then
  293. echo "Stop dhcpd"
  294. stop_dhcpd
  295. fi
  296. if has_vpnclient_app; then
  297. service ynh-vpnclient start
  298. fi
  299. ;;
  300. status)
  301. exitcode=0
  302. echo "[INFO] Autodetected internet interface: ${new_internet_device} (last start: ${old_internet_device})"
  303. if has_ip6delegatedprefix; then
  304. echo "[INFO] IPv6 delegated prefix found"
  305. echo "[INFO] IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
  306. if is_ndproxy_set; then
  307. echo "[OK] NDP proxy set"
  308. else
  309. echo "[ERR] No NDP proxy set"
  310. exitcode=1
  311. fi
  312. if is_ip6addr_set; then
  313. echo "[OK] IPv6 address set"
  314. else
  315. echo "[ERR] No IPv6 address set"
  316. exitcode=1
  317. fi
  318. if is_radvd_running; then
  319. echo "[OK] Radvd is running"
  320. else
  321. echo "[ERR] Radvd is not running"
  322. exitcode=1
  323. fi
  324. else
  325. echo "[INFO] No IPv6 delegated prefix found"
  326. fi
  327. if is_nat_set "${new_internet_device}"; then
  328. echo "[OK] IPv4 NAT set"
  329. else
  330. echo "[ERR] No IPv4 NAT set"
  331. exitcode=1
  332. fi
  333. if is_ip4nataddr_set; then
  334. echo "[OK] IPv4 NAT address set"
  335. else
  336. echo "[ERR] No IPv4 NAT address set"
  337. exitcode=1
  338. fi
  339. if is_forwarding_set; then
  340. echo "[OK] IPv6/IPv4 forwarding set"
  341. else
  342. echo "[ERR] No IPv6/IPv4 forwarding set"
  343. exitcode=1
  344. fi
  345. if is_hostapd_running; then
  346. echo "[OK] Hostapd is running"
  347. else
  348. echo "[ERR] Hostapd is not running"
  349. exitcode=1
  350. fi
  351. if is_dhcpd_running; then
  352. echo "[OK] Dhcpd is running"
  353. else
  354. echo "[ERR] Dhcpd is not running"
  355. exitcode=1
  356. fi
  357. exit ${exitcode}
  358. ;;
  359. *)
  360. echo "Usage: $0 {start|stop|status}"
  361. exit 1
  362. ;;
  363. esac
  364. exit 0