freedombone-mesh-install 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Installs mesh applications. This avoids duplicated functions
  12. # within freedombone and freedombone-image-customize and also
  13. # for client installs
  14. #
  15. # License
  16. # =======
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. PROJECT_NAME='freedombone'
  31. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  32. export TEXTDOMAIN=${PROJECT_NAME}-mesh-install
  33. export TEXTDOMAINDIR="/usr/share/locale"
  34. # for mesh installs
  35. TRACKER_PORT=6969
  36. WIFI_CHANNEL=2
  37. WIFI_INTERFACE='wlan0'
  38. # B.A.T.M.A.N settings
  39. BATMAN_CELLID='02:BA:00:00:03:01'
  40. WIFI_SSID='mesh'
  41. rootdir=''
  42. FN=
  43. CHROOT_PREFIX=''
  44. # To avoid confusions these are obtained from the main project file
  45. TOXID_REPO=
  46. TOX_PORT=
  47. TOXCORE_REPO=
  48. TOXIC_REPO=
  49. TOXCORE_COMMIT=
  50. TOXIC_COMMIT=
  51. # These are some default nodes, but you can replace them with trusted nodes
  52. # as you prefer. See https://wiki.tox.im/Nodes
  53. TOX_NODES=
  54. #TOX_NODES=(
  55. # '192.254.75.102,2607:5600:284::2,33445,951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F,Tox RELENG,US'
  56. # '144.76.60.215,2a01:4f8:191:64d6::1,33445,04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F,sonOfRa,DE'
  57. #)
  58. # To avoid confusions these are obtained from the main project file
  59. ZERONET_REPO=
  60. ZERONET_COMMIT=
  61. ZERONET_PORT=
  62. # Directory where source code is downloaded and compiled
  63. INSTALL_DIR=$HOME/build
  64. MESH_INSTALL_DIR=/var/lib
  65. REMOVE='no'
  66. source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-git
  67. function show_help {
  68. echo ''
  69. echo $"${PROJECT_NAME}-mesh-install -f [function] -r [rootdir]"
  70. echo ''
  71. echo $'Runs a mesh network install function'
  72. echo ''
  73. echo $' -h --help Show help'
  74. echo $' -f --function [name] Name of the function to be run'
  75. echo $' -r --rootdir [directory] Root directory'
  76. echo $' -w --wifi [interface] e.g. wlan0'
  77. echo ''
  78. exit 0
  79. }
  80. function mesh_avahi {
  81. $CHROOT_PREFIX apt-get -yq install avahi-utils avahi-autoipd avahi-dnsconfd
  82. decarray=( 1 2 3 4 5 6 7 8 9 0 )
  83. PEER_ID=${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}
  84. sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" $rootdir/etc/avahi/avahi-daemon.conf
  85. if [ ! -d $rootdir/etc/avahi/services ]; then
  86. mkdir -p $rootdir/etc/avahi/services
  87. fi
  88. # remove an avahi service which isn't used
  89. if [ -f $rootdir/etc/avahi/services/udisks.service ]; then
  90. rm $rootdir/etc/avahi/services/udisks.service
  91. fi
  92. # Add an ssh service
  93. echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->' > $rootdir/etc/avahi/services/ssh.service
  94. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">' >> $rootdir/etc/avahi/services/ssh.service
  95. echo '<service-group>' >> $rootdir/etc/avahi/services/ssh.service
  96. echo ' <name replace-wildcards="yes">%h SSH</name>' >> $rootdir/etc/avahi/services/ssh.service
  97. echo ' <service>' >> $rootdir/etc/avahi/services/ssh.service
  98. echo ' <type>_ssh._tcp</type>' >> $rootdir/etc/avahi/services/ssh.service
  99. echo " <port>$SSH_PORT</port>" >> $rootdir/etc/avahi/services/ssh.service
  100. echo ' </service>' >> $rootdir/etc/avahi/services/ssh.service
  101. echo '</service-group>' >> $rootdir/etc/avahi/services/ssh.service
  102. # keep the daemon running
  103. WATCHDOG_SCRIPT_NAME="keepon"
  104. echo '' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  105. echo '# keep avahi daemon running' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  106. echo 'AVAHI_RUNNING=$(pgrep avahi-daemon > /dev/null && echo Running)' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  107. echo 'if [ ! $AVAHI_RUNNING ]; then' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  108. echo ' systemctl start avahi-daemon' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  109. echo ' echo -n $CURRENT_DATE >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  110. echo ' echo " Avahi daemon restarted" >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  111. echo 'fi' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  112. chmod +x $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  113. }
  114. function install_batman_remove {
  115. systemctl stop batman
  116. rm $rootdir/var/lib/batman
  117. rm $rootdir/etc/systemd/system/batman.service
  118. }
  119. function install_batman {
  120. $CHROOT_PREFIX apt-get -yq install iproute bridge-utils libnetfilter-conntrack3 batctl
  121. $CHROOT_PREFIX apt-get -yq install python-dev libevent-dev ebtables python-pip git
  122. $CHROOT_PREFIX apt-get -yq install wireless-tools rfkill
  123. if ! grep -q "batman_adv" $rootdir/etc/modules; then
  124. echo 'batman_adv' >> $rootdir/etc/modules
  125. fi
  126. BATMAN_SCRIPT=$rootdir/var/lib/batman
  127. if [ -f /usr/local/bin/${PROJECT_NAME}-mesh-batman ]; then
  128. cp /usr/local/bin/${PROJECT_NAME}-mesh-batman $BATMAN_SCRIPT
  129. else
  130. cp /usr/bin/${PROJECT_NAME}-mesh-batman $BATMAN_SCRIPT
  131. fi
  132. BATMAN_DAEMON=$rootdir/etc/systemd/system/batman.service
  133. echo '[Unit]' > $BATMAN_DAEMON
  134. echo 'Description=B.A.T.M.A.N. Advanced' >> $BATMAN_DAEMON
  135. echo 'After=network.target' >> $BATMAN_DAEMON
  136. echo '' >> $BATMAN_DAEMON
  137. echo '[Service]' >> $BATMAN_DAEMON
  138. echo 'RemainAfterExit=yes' >> $BATMAN_DAEMON
  139. echo "ExecStart=/var/lib/batman start" >> $BATMAN_DAEMON
  140. echo "ExecStop=/var/lib/batman stop" >> $BATMAN_DAEMON
  141. echo 'Restart=on-failure' >> $BATMAN_DAEMON
  142. echo 'SuccessExitStatus=3 4' >> $BATMAN_DAEMON
  143. echo 'RestartForceExitStatus=3 4' >> $BATMAN_DAEMON
  144. echo '' >> $BATMAN_DAEMON
  145. echo '# Allow time for the server to start/stop' >> $BATMAN_DAEMON
  146. echo 'TimeoutSec=300' >> $BATMAN_DAEMON
  147. echo '' >> $BATMAN_DAEMON
  148. echo '[Install]' >> $BATMAN_DAEMON
  149. echo 'WantedBy=multi-user.target' >> $BATMAN_DAEMON
  150. $CHROOT_PREFIX systemctl enable batman
  151. }
  152. function mesh_firewall {
  153. FIREWALL_FILENAME=${rootdir}/etc/systemd/system/meshfirewall.service
  154. MESH_FIREWALL_SCRIPT=${rootdir}/usr/bin/mesh-firewall
  155. echo '#!/bin/bash' > $MESH_FIREWALL_SCRIPT
  156. echo 'iptables -P INPUT ACCEPT' >> $MESH_FIREWALL_SCRIPT
  157. echo 'ip6tables -P INPUT ACCEPT' >> $MESH_FIREWALL_SCRIPT
  158. echo 'iptables -F' >> $MESH_FIREWALL_SCRIPT
  159. echo 'ip6tables -F' >> $MESH_FIREWALL_SCRIPT
  160. echo 'iptables -t nat -F' >> $MESH_FIREWALL_SCRIPT
  161. echo 'ip6tables -t nat -F' >> $MESH_FIREWALL_SCRIPT
  162. echo 'iptables -X' >> $MESH_FIREWALL_SCRIPT
  163. echo 'ip6tables -X' >> $MESH_FIREWALL_SCRIPT
  164. echo 'iptables -P INPUT DROP' >> $MESH_FIREWALL_SCRIPT
  165. echo 'ip6tables -P INPUT DROP' >> $MESH_FIREWALL_SCRIPT
  166. echo 'iptables -A INPUT -i lo -j ACCEPT' >> $MESH_FIREWALL_SCRIPT
  167. echo 'iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT' >> $MESH_FIREWALL_SCRIPT
  168. echo '' >> $MESH_FIREWALL_SCRIPT
  169. echo '# Make sure incoming tcp connections are SYN packets' >> $MESH_FIREWALL_SCRIPT
  170. echo 'iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP' >> $MESH_FIREWALL_SCRIPT
  171. echo '' >> $MESH_FIREWALL_SCRIPT
  172. echo '# Drop packets with incoming fragments' >> $MESH_FIREWALL_SCRIPT
  173. echo 'iptables -A INPUT -f -j DROP' >> $MESH_FIREWALL_SCRIPT
  174. echo '' >> $MESH_FIREWALL_SCRIPT
  175. echo '# Drop bogons' >> $MESH_FIREWALL_SCRIPT
  176. echo 'iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP' >> $MESH_FIREWALL_SCRIPT
  177. echo 'iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP' >> $MESH_FIREWALL_SCRIPT
  178. echo 'iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP' >> $MESH_FIREWALL_SCRIPT
  179. echo '' >> $MESH_FIREWALL_SCRIPT
  180. echo '# Incoming malformed NULL packets:' >> $MESH_FIREWALL_SCRIPT
  181. echo 'iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP' >> $MESH_FIREWALL_SCRIPT
  182. echo '' >> $MESH_FIREWALL_SCRIPT
  183. echo "iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  184. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $ZERONET_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  185. echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $ZERONET_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  186. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $TRACKER_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  187. echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $TRACKER_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  188. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport 1900 -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  189. chmod +x $MESH_FIREWALL_SCRIPT
  190. echo '[Unit]' > $FIREWALL_FILENAME
  191. echo 'Description=Mesh Firewall' >> $FIREWALL_FILENAME
  192. echo '' >> $FIREWALL_FILENAME
  193. echo '[Service]' >> $FIREWALL_FILENAME
  194. echo 'Type=oneshot' >> $FIREWALL_FILENAME
  195. echo 'ExecStart=/usr/bin/mesh-firewall' >> $FIREWALL_FILENAME
  196. echo 'RemainAfterExit=no' >> $FIREWALL_FILENAME
  197. echo '' >> $FIREWALL_FILENAME
  198. echo 'TimeoutSec=30' >> $FIREWALL_FILENAME
  199. echo '' >> $FIREWALL_FILENAME
  200. echo '[Install]' >> $FIREWALL_FILENAME
  201. echo 'WantedBy=multi-user.target' >> $FIREWALL_FILENAME
  202. chmod +x $FIREWALL_FILENAME
  203. $CHROOT_PREFIX systemctl enable meshfirewall
  204. }
  205. function enable_tox_repo {
  206. sudo sh -c "echo 'deb http://download.opensuse.org/repositories/home:/antonbatenev:/tox/Debian_9.0/ /' > /etc/apt/sources.list.d/tox.list"
  207. wget http://download.opensuse.org/repositories/home:antonbatenev:tox/Debian_9.0/Release.key
  208. sudo sh -c "apt-key add - < Release.key"
  209. sudo apt-get update
  210. echo "Tox Repository Installed."
  211. }
  212. function mesh_tox_client_qtox {
  213. enable_tox_repo
  214. sudo apt-get -yq install qtox
  215. echo "qTox Installed."
  216. }
  217. function mesh_tox_client_toxic_from_repo {
  218. enable_tox_repo
  219. sudo apt-get -yq install toxic
  220. echo "Toxic Installed."
  221. }
  222. while [[ $# > 1 ]]
  223. do
  224. key="$1"
  225. case $key in
  226. -h|--help)
  227. show_help
  228. ;;
  229. -f|--function)
  230. shift
  231. FN="$1"
  232. ;;
  233. -r|--rootdir)
  234. shift
  235. rootdir="$1"
  236. CHROOT_PREFIX='chroot "${rootdir}"'
  237. ;;
  238. -w|--wifi|--interface)
  239. shift
  240. WIFI_INTERFACE="$1"
  241. ;;
  242. --remove)
  243. shift
  244. REMOVE="$1"
  245. ;;
  246. *)
  247. # unknown option
  248. ;;
  249. esac
  250. shift
  251. done
  252. if [[ $FN == 'avahi' ]]; then
  253. mesh_avahi
  254. fi
  255. if [[ $FN == 'firewall' ]]; then
  256. mesh_firewall
  257. fi
  258. if [[ $FN == 'batman' ]]; then
  259. if [[ $REMOVE != 'yes' ]]; then
  260. install_batman
  261. else
  262. install_batman_remove
  263. fi
  264. fi
  265. if [[ $FN == 'qtox' ]]; then
  266. mesh_tox_client_qtox
  267. fi
  268. if [[ $FN == 'toxic' ]]; then
  269. mesh_tox_client_toxic_from_repo
  270. fi
  271. exit 0