init_ynh-hotspot 8.5 KB

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