freedombone-mesh-install 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. if ! grep -q "tunnel6" "$rootdir/etc/modules"; then
  124. echo 'tunnel6' >> "$rootdir/etc/modules"
  125. fi
  126. if ! grep -q "ip6_tunnel" "$rootdir/etc/modules"; then
  127. echo 'ip6_tunnel' >> "$rootdir/etc/modules"
  128. fi
  129. BATMAN_SCRIPT=$rootdir/var/lib/batman
  130. if [ -f /usr/local/bin/${PROJECT_NAME}-mesh-batman ]; then
  131. cp /usr/local/bin/${PROJECT_NAME}-mesh-batman $BATMAN_SCRIPT
  132. else
  133. cp /usr/bin/${PROJECT_NAME}-mesh-batman $BATMAN_SCRIPT
  134. fi
  135. BATMAN_DAEMON=$rootdir/etc/systemd/system/batman.service
  136. { echo '[Unit]';
  137. echo 'Description=B.A.T.M.A.N. Advanced';
  138. echo 'After=network.target';
  139. echo '';
  140. echo '[Service]';
  141. echo 'RemainAfterExit=yes';
  142. echo "ExecStart=/var/lib/batman start";
  143. echo "ExecStop=/var/lib/batman stop";
  144. echo 'Restart=on-failure';
  145. echo 'SuccessExitStatus=3 4';
  146. echo 'RestartForceExitStatus=3 4';
  147. echo '';
  148. echo '# Allow time for the server to start/stop';
  149. echo 'TimeoutSec=300';
  150. echo '';
  151. echo '[Install]';
  152. echo 'WantedBy=multi-user.target'; } > "$BATMAN_DAEMON"
  153. $CHROOT_PREFIX systemctl enable batman
  154. }
  155. function mesh_firewall {
  156. FIREWALL_FILENAME=${rootdir}/etc/systemd/system/meshfirewall.service
  157. MESH_FIREWALL_SCRIPT=${rootdir}/usr/bin/mesh-firewall
  158. { echo '#!/bin/bash';
  159. echo 'iptables -P INPUT ACCEPT';
  160. echo 'ip6tables -P INPUT ACCEPT';
  161. echo 'iptables -F';
  162. echo 'ip6tables -F';
  163. echo 'iptables -t nat -F';
  164. echo 'ip6tables -t nat -F';
  165. echo 'iptables -X';
  166. echo 'ip6tables -X';
  167. echo 'iptables -P INPUT DROP';
  168. echo 'ip6tables -P INPUT DROP';
  169. echo 'iptables -A INPUT -i lo -j ACCEPT';
  170. echo 'ip6tables -A INPUT -i lo -j ACCEPT';
  171. echo 'iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT';
  172. echo 'ip6tables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT';
  173. echo '';
  174. echo '# Make sure incoming tcp connections are SYN packets';
  175. echo 'iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP';
  176. echo 'ip6tables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP';
  177. echo '';
  178. echo '# Drop packets with incoming fragments';
  179. echo 'iptables -A INPUT -f -j DROP';
  180. echo 'ip6tables -A INPUT -f -j DROP';
  181. echo '';
  182. echo '# Drop bogons';
  183. echo 'iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP';
  184. echo 'ip6tables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP';
  185. echo 'iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP';
  186. echo 'ip6tables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP';
  187. echo 'iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP';
  188. echo 'ip6tables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP';
  189. echo '';
  190. echo '# Incoming malformed NULL packets:';
  191. echo 'iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP';
  192. echo 'ip6tables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP';
  193. echo '';
  194. echo "iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT";
  195. echo "ip6tables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT";
  196. echo "iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT";
  197. echo "ip6tables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT";
  198. echo "iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT";
  199. echo "ip6tables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT";
  200. echo "iptables -A INPUT -p udp --dport $TRACKER_PORT -j ACCEPT";
  201. echo "ip6tables -A INPUT -p udp --dport $TRACKER_PORT -j ACCEPT";
  202. echo "iptables -A INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT";
  203. echo "ip6tables -A INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT";
  204. echo "iptables -A INPUT -p udp --dport 1900 -j ACCEPT";
  205. echo "ip6tables -A INPUT -p udp --dport 1900 -j ACCEPT";
  206. echo '# OLSR2/MANET';
  207. echo 'iptables -A INPUT -p udp --dport 269 -j ACCEPT';
  208. echo 'ip6tables -A INPUT -p udp --dport 269 -j ACCEPT';
  209. echo 'iptables -A INPUT -p tcp --dport 138 -j ACCEPT';
  210. echo 'ip6tables -A INPUT -p tcp --dport 138 -j ACCEPT';
  211. echo '# Babel';
  212. echo 'iptables -A INPUT -p udp --dport 6696 -j ACCEPT';
  213. echo 'ip6tables -A INPUT -p udp --dport 6696 -j ACCEPT'; } > "$MESH_FIREWALL_SCRIPT"
  214. chmod +x $MESH_FIREWALL_SCRIPT
  215. { echo '[Unit]';
  216. echo 'Description=Mesh Firewall';
  217. echo '';
  218. echo '[Service]';
  219. echo 'Type=oneshot';
  220. echo 'ExecStart=/usr/bin/mesh-firewall';
  221. echo 'RemainAfterExit=no';
  222. echo '';
  223. echo 'TimeoutSec=30';
  224. echo '';
  225. echo '[Install]';
  226. echo 'WantedBy=multi-user.target'; } > $FIREWALL_FILENAME
  227. chmod +x $FIREWALL_FILENAME
  228. $CHROOT_PREFIX systemctl enable meshfirewall
  229. }
  230. function enable_tox_repo {
  231. sudo sh -c "echo 'deb http://download.opensuse.org/repositories/home:/antonbatenev:/tox/Debian_9.0/ /' > /etc/apt/sources.list.d/tox.list"
  232. wget http://download.opensuse.org/repositories/home:antonbatenev:tox/Debian_9.0/Release.key
  233. sudo sh -c "apt-key add - < Release.key"
  234. sudo apt-get update
  235. echo "Tox Repository Installed."
  236. }
  237. function mesh_tox_client_qtox {
  238. enable_tox_repo
  239. sudo apt-get -yq install qtox
  240. echo "qTox Installed."
  241. }
  242. function mesh_tox_client_toxic_from_repo {
  243. enable_tox_repo
  244. sudo apt-get -yq install toxic
  245. echo "Toxic Installed."
  246. }
  247. while [ $# -gt 1 ]
  248. do
  249. key="$1"
  250. case $key in
  251. -h|--help)
  252. show_help
  253. ;;
  254. -f|--function)
  255. shift
  256. FN="$1"
  257. ;;
  258. -r|--rootdir)
  259. shift
  260. rootdir="$1"
  261. CHROOT_PREFIX="chroot \"\${rootdir}\""
  262. ;;
  263. -w|--wifi|--interface)
  264. shift
  265. WIFI_INTERFACE="$1"
  266. ;;
  267. --remove)
  268. shift
  269. REMOVE="$1"
  270. ;;
  271. *)
  272. # unknown option
  273. ;;
  274. esac
  275. shift
  276. done
  277. if [[ $FN == 'avahi' ]]; then
  278. mesh_avahi
  279. fi
  280. if [[ $FN == 'firewall' ]]; then
  281. mesh_firewall
  282. fi
  283. if [[ $FN == 'batman' ]]; then
  284. if [[ $REMOVE != 'yes' ]]; then
  285. install_batman
  286. else
  287. install_batman_remove
  288. fi
  289. fi
  290. if [[ $FN == 'qtox' ]]; then
  291. mesh_tox_client_qtox
  292. fi
  293. if [[ $FN == 'toxic' ]]; then
  294. mesh_tox_client_toxic_from_repo
  295. fi
  296. exit 0