init_ynh-hotspot 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. ;;
  210. stop)
  211. echo "Stopping..."
  212. if is_ndproxy_set; then
  213. echo "Unset NDP proxy"
  214. unset_ndproxy
  215. fi
  216. if is_nat_set "${old_internet_device}"; then
  217. echo "Unset NAT"
  218. unset_nat "${old_internet_device}"
  219. fi
  220. if is_ip4nataddr_set; then
  221. echo "Unset IPv4 NAT address"
  222. unset_ip4nataddr
  223. fi
  224. if is_ip6addr_set; then
  225. echo "Unset IPv6 address"
  226. unset_ip6addr
  227. fi
  228. if is_forwarding_set; then
  229. echo "Unset forwarding"
  230. unset_forwarding
  231. fi
  232. if is_hostapd_running; then
  233. echo "Stop hostapd"
  234. stop_hostapd
  235. fi
  236. if is_radvd_running; then
  237. echo "Stop radvd"
  238. stop_radvd
  239. fi
  240. if is_dhcpd_running; then
  241. echo "Stop dhcpd"
  242. stop_dhcpd
  243. fi
  244. ;;
  245. restart)
  246. $0 stop
  247. $0 start
  248. ;;
  249. status)
  250. exitcode=0
  251. if is_ndproxy_set; then
  252. echo "NDP proxy is correctly set"
  253. else
  254. echo "NDP proxy is NOT set"
  255. exitcode=1
  256. fi
  257. if is_nat_set "${new_internet_device}"; then
  258. echo "NAT is correctly set"
  259. else
  260. echo "NAT is NOT set"
  261. exitcode=1
  262. fi
  263. if is_ip4nataddr_set; then
  264. echo "IPv4 NAT address is correctly set"
  265. else
  266. echo "IPv4 NAT address is NOT set"
  267. exitcode=1
  268. fi
  269. if is_ip6addr_set; then
  270. echo "IPv6 address is correctly set"
  271. else
  272. echo "IPv6 address is NOT set"
  273. exitcode=1
  274. fi
  275. if is_forwarding_set; then
  276. echo "Forwarding is correctly set"
  277. else
  278. echo "Forwarding is NOT set"
  279. exitcode=1
  280. fi
  281. if is_hostapd_running; then
  282. echo "Hostapd is running"
  283. else
  284. echo "Hostapd is NOT running"
  285. exitcode=1
  286. fi
  287. if is_radvd_running; then
  288. echo "Radvd is running"
  289. else
  290. echo "Radvd is NOT running"
  291. exitcode=1
  292. fi
  293. if is_dhcpd_running; then
  294. echo "Dhcpd is running"
  295. else
  296. echo "Dhcpd is NOT running"
  297. exitcode=1
  298. fi
  299. exit ${exitcode}
  300. ;;
  301. *)
  302. echo "Usage: $0 {start|stop|restart|status}"
  303. exit 1
  304. ;;
  305. esac
  306. exit 0