init_ynh-hotspot 8.9 KB

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