freedombone-mesh-install 11KB

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