init_ynh-hotspot 11 KB

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