init_ynh-hotspot 8.2 KB

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