freedombone-mesh-install 11KB

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