freedombone-client 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
  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. export TEXTDOMAIN=${PROJECT_NAME}-client
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. CURR_USER=$USER
  32. CURR_GROUP=$USER
  33. if [ -f /usr/bin/pacman ]; then
  34. CURR_GROUP='users'
  35. fi
  36. MESH_CLIENT_INSTALL=
  37. ENABLE_MONKEYSPHERE=
  38. # Version number of this script
  39. VERSION="1.01"
  40. # get the main project file, so that some values can be extracted
  41. MAIN_PROJECT_FILE=/usr/local/bin/${PROJECT_NAME}
  42. if [ ! -f $MAIN_PROJECT_FILE ]; then
  43. MAIN_PROJECT_FILE=/usr/bin/${PROJECT_NAME}
  44. fi
  45. if [ ! -f $MAIN_PROJECT_FILE ]; then
  46. echo "The main project file $MAIN_PROJECT_FILE was not found"
  47. exit 72529
  48. fi
  49. # ssh (from https://stribika.github.io/2015/01/04/secure-secure-shell.html)
  50. UTILS_SSH=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-ssh
  51. SSH_CIPHERS=$(cat $UTILS_SSH | grep 'SSH_CIPHERS=' | head -n 1 | awk -F '"' '{print $2}')
  52. SSH_MACS=$(cat $UTILS_SSH | grep 'SSH_MACS=' | head -n 1 | awk -F '"' '{print $2}')
  53. SSH_KEX=$(cat $UTILS_SSH | grep 'SSH_KEX=' | head -n 1 | awk -F '"' '{print $2}')
  54. SSH_HOST_KEY_ALGORITHMS=$(cat $UTILS_SSH | grep 'SSH_HOST_KEY_ALGORITHMS=' | head -n 1 | awk -F '"' '{print $2}')
  55. # refresh gpg keys every few hours
  56. REFRESH_GPG_KEYS_HOURS=2
  57. function global_rate_limit {
  58. SYSCTL_FILE=/etc/sysctl.conf
  59. if [ -f /usr/bin/pacman ]; then
  60. SYSCTL_FILE=/etc/sysctl.d/99-sysctl.conf
  61. fi
  62. if [ ! -f $SYSCTL_FILE ]; then
  63. sudo touch $SYSCTL_FILE
  64. fi
  65. sudo cp $SYSCTL_FILE ~/sysctl.conf
  66. sudo chown $CURR_USER:$CURR_GROUP ~/sysctl.conf
  67. if ! grep -q "tcp_challenge_ack_limit" ~/sysctl.conf; then
  68. echo 'net.ipv4.tcp_challenge_ack_limit = 999999999' >> ~/sysctl.conf
  69. else
  70. sed -i 's|net.ipv4.tcp_challenge_ack_limit.*|net.ipv4.tcp_challenge_ack_limit = 999999999|g' ~/sysctl.conf
  71. fi
  72. sudo cp ~/sysctl.conf $SYSCTL_FILE
  73. sudo rm ~/sysctl.conf
  74. sudo sysctl -p -q $SYSCTL_FILE
  75. }
  76. function refresh_gpg_keys {
  77. if [ ! -f /usr/bin/gpg ]; then
  78. if [ ! -f /usr/bin/pacman ]; then
  79. sudo apt-get -y install gnupg
  80. else
  81. sudo pacman -S --noconfirm gnupg
  82. fi
  83. fi
  84. sudo cp /etc/crontab ~/temp_crontab
  85. sudo chown $CURR_USER:$CURR_GROUP ~/temp_crontab
  86. if ! grep -q "gpg --refresh-keys" ~/temp_crontab; then
  87. echo "0 */$REFRESH_GPG_KEYS_HOURS * * * $CURR_USER /usr/bin/gpg --refresh-keys > /dev/null" >> ~/temp_crontab
  88. sudo cp ~/temp_crontab /etc/crontab
  89. sudo chown root:root /etc/crontab
  90. fi
  91. rm ~/temp_crontab
  92. }
  93. # see https://stribika.github.io/2015/01/04/secure-secure-shell.html
  94. function ssh_remove_small_moduli {
  95. sudo awk '$5 > 2000' /etc/ssh/moduli > /home/$CURR_USER/moduli
  96. sudo mv /home/$CURR_USER/moduli /etc/ssh/moduli
  97. }
  98. function configure_ssh_client {
  99. if [ -f /usr/bin/pacman ]; then
  100. sudo pacman --noconfirm -S openbsd-netcat
  101. else
  102. sudo apt-get -y install tor connect-proxy
  103. fi
  104. #sudo sed -i 's/# PasswordAuthentication.*/ PasswordAuthentication no/g' /etc/ssh/ssh_config
  105. #sudo sed -i 's/# ChallengeResponseAuthentication.*/ ChallengeResponseAuthentication no/g' /etc/ssh/ssh_config
  106. sudo sed -i "s/# HostKeyAlgorithms.*/ HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  107. sudo sed -i "s/# Ciphers.*/ Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  108. sudo sed -i "s/# MACs.*/ MACs $SSH_MACS/g" /etc/ssh/ssh_config
  109. sudo sed -i "s/HostKeyAlgorithms.*/HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  110. if ! grep -q "HostKeyAlgorithms" /etc/ssh/ssh_config; then
  111. sudo cp /etc/ssh/ssh_config ~/ssh_config
  112. sudo chown $CURR_USER:$CURR_GROUP ~/ssh_config
  113. echo " HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS" >> ~/ssh_config
  114. sudo mv ~/ssh_config /etc/ssh/ssh_config
  115. sudo chown root:root /etc/ssh/ssh_config
  116. fi
  117. sudo sed -i "s/HostKeyAlgorithms.*/#HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  118. sudo sed -i "s/Ciphers.*/Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  119. if ! grep -q "Ciphers " /etc/ssh/ssh_config; then
  120. sudo cp /etc/ssh/ssh_config ~/ssh_config
  121. sudo chown $CURR_USER:$CURR_GROUP ~/ssh_config
  122. echo " Ciphers $SSH_CIPHERS" >> ~/ssh_config
  123. sudo mv ~/ssh_config /etc/ssh/ssh_config
  124. sudo chown root:root /etc/ssh/ssh_config
  125. fi
  126. sudo sed -i "s/MACs.*/MACs $SSH_MACS/g" /etc/ssh/ssh_config
  127. if ! grep -q "MACs " /etc/ssh/ssh_config; then
  128. sudo cp /etc/ssh/ssh_config ~/ssh_config
  129. sudo chown $CURR_USER:$CURR_GROUP ~/ssh_config
  130. echo " MACs $SSH_MACS" >> ~/ssh_config
  131. sudo mv ~/ssh_config /etc/ssh/ssh_config
  132. sudo chown root:root /etc/ssh/ssh_config
  133. fi
  134. # Create ssh keys
  135. if [ ! -f /home/$CURR_USER/.ssh/id_ed25519 ]; then
  136. ssh-keygen -t ed25519 -o -a 100
  137. fi
  138. if [ ! -f /home/$CURR_USER/.ssh/id_rsa ]; then
  139. ssh-keygen -t rsa -b 4096 -o -a 100
  140. fi
  141. ssh_remove_small_moduli
  142. if [ ! -d ~/.ssh ]; then
  143. mkdir ~/.ssh
  144. fi
  145. echo 'Host *.onion' > ~/.ssh/config
  146. echo ' ServerAliveInterval 60' >> ~/.ssh/config
  147. echo ' ServerAliveCountMax 3' >> ~/.ssh/config
  148. proxycmd="connect -R remote -5 -S 127.0.0.1:9050 %h %p"
  149. if [ -f /usr/bin/pacman ]; then
  150. proxycmd="/usr/bin/nc -X 5 -x 127.0.0.1:9050 %h %p"
  151. fi
  152. if [[ $ENABLE_MONKEYSPHERE == $'yes' || $ENABLE_MONKEYSPHERE == $'y' ]]; then
  153. echo " ProxyCommand sh -c 'monkeysphere ssh-proxycommand --no-connect %h %p ; $proxycmd'" >> ~/.ssh/config
  154. else
  155. echo " ProxyCommand $proxycmd" >> ~/.ssh/config
  156. fi
  157. echo 'Host *' >> ~/.ssh/config
  158. echo ' ServerAliveInterval 60' >> ~/.ssh/config
  159. echo ' ServerAliveCountMax 3' >> ~/.ssh/config
  160. if [[ $ENABLE_MONKEYSPHERE == $'yes' || $ENABLE_MONKEYSPHERE == $'y' ]]; then
  161. echo ' ProxyCommand monkeysphere ssh-proxycommand %h %p' >> ~/.ssh/config
  162. fi
  163. echo ''
  164. echo $'Copy the following into a file called /home/username/.ssh/authorized_keys on the Freedombone server'
  165. echo ''
  166. echo $(cat /home/$CURR_USER/.ssh/id_rsa.pub)
  167. echo $(cat /home/$CURR_USER/.ssh/id_ed25519.pub)
  168. echo ''
  169. }
  170. function configure_monkeysphere {
  171. if [ -f /usr/bin/pacman ]; then
  172. return
  173. fi
  174. sudo apt-get -y install monkeysphere
  175. }
  176. function show_help {
  177. echo ''
  178. echo $"${PROJECT_NAME}-client --mesh [yes|no] --monkeysphere [yes|no]"
  179. echo ''
  180. exit 0
  181. }
  182. while [[ $# > 1 ]]
  183. do
  184. key="$1"
  185. case $key in
  186. -h|--help)
  187. show_help
  188. ;;
  189. --essid)
  190. shift
  191. WIFI_SSID="$1"
  192. ;;
  193. --channel)
  194. shift
  195. WIFI_CHANNEL=${1}
  196. ;;
  197. -m|--mesh)
  198. shift
  199. MESH_CLIENT_INSTALL=${1}
  200. ;;
  201. --monkeysphere|--ms|--monkey)
  202. shift
  203. ENABLE_MONKEYSPHERE=${1}
  204. ;;
  205. *)
  206. # unknown option
  207. ;;
  208. esac
  209. shift
  210. done
  211. echo $'Configuring client'
  212. refresh_gpg_keys
  213. configure_ssh_client
  214. global_rate_limit
  215. configure_monkeysphere
  216. if [[ $MESH_CLIENT_INSTALL == $'yes' || $MESH_CLIENT_INSTALL == $'y' || $MESH_CLIENT_INSTALL == $'on' ]]; then
  217. echo $'Installing mesh packages'
  218. if [ ! -f /usr/bin/pacman ]; then
  219. sudo apt-get -y install avahi-utils avahi-autoipd avahi-dnsconfd
  220. else
  221. sudo pacman -S --noconfirm avahi nss-mdns
  222. sudo sed -i 's|hosts:.*|hosts: files mdns_minimal [NOTFOUND=return] dns myhostname|g' /etc/nsswitch.conf
  223. fi
  224. sudo ${PROJECT_NAME}-mesh-install -f tox_node
  225. sudo ${PROJECT_NAME}-mesh-install -f toxic
  226. ${PROJECT_NAME}-mesh-install -f qtox
  227. sudo ${PROJECT_NAME}-mesh-install -f zeronet
  228. fi
  229. echo $'Configuration complete'
  230. exit 0