freedombone-client 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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@freedombone.net>
  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. # setup for a specific app
  39. SETUP_CLIENT_APP_NAME=
  40. # Version number of this script
  41. VERSION="1.01"
  42. # get the main project file, so that some values can be extracted
  43. MAIN_PROJECT_FILE=/usr/local/bin/${PROJECT_NAME}
  44. if [ ! -f $MAIN_PROJECT_FILE ]; then
  45. MAIN_PROJECT_FILE=/usr/bin/${PROJECT_NAME}
  46. fi
  47. if [ ! -f $MAIN_PROJECT_FILE ]; then
  48. echo "The main project file $MAIN_PROJECT_FILE was not found"
  49. exit 72529
  50. fi
  51. # ssh (from https://stribika.github.io/2015/01/04/secure-secure-shell.html)
  52. UTILS_SSH=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-ssh
  53. SSH_CIPHERS=$(cat $UTILS_SSH | grep 'SSH_CIPHERS=' | head -n 1 | awk -F '"' '{print $2}')
  54. SSH_MACS=$(cat $UTILS_SSH | grep 'SSH_MACS=' | head -n 1 | awk -F '"' '{print $2}')
  55. SSH_KEX=$(cat $UTILS_SSH | grep 'SSH_KEX=' | head -n 1 | awk -F '"' '{print $2}')
  56. SSH_HOST_KEY_ALGORITHMS=$(cat $UTILS_SSH | grep 'SSH_HOST_KEY_ALGORITHMS=' | head -n 1 | awk -F '"' '{print $2}')
  57. # refresh gpg keys every few hours
  58. REFRESH_GPG_KEYS_HOURS=2
  59. function global_rate_limit {
  60. SYSCTL_FILE=/etc/sysctl.conf
  61. if [ -f /usr/bin/pacman ]; then
  62. SYSCTL_FILE=/etc/sysctl.d/99-sysctl.conf
  63. fi
  64. if [ ! -f $SYSCTL_FILE ]; then
  65. sudo touch $SYSCTL_FILE
  66. fi
  67. sudo cp $SYSCTL_FILE ~/sysctl.conf
  68. sudo chown $CURR_USER:$CURR_GROUP ~/sysctl.conf
  69. if ! grep -q "tcp_challenge_ack_limit" ~/sysctl.conf; then
  70. echo 'net.ipv4.tcp_challenge_ack_limit = 999999999' >> ~/sysctl.conf
  71. else
  72. sed -i 's|net.ipv4.tcp_challenge_ack_limit.*|net.ipv4.tcp_challenge_ack_limit = 999999999|g' ~/sysctl.conf
  73. fi
  74. sudo cp ~/sysctl.conf $SYSCTL_FILE
  75. sudo rm ~/sysctl.conf
  76. sudo sysctl -p -q $SYSCTL_FILE
  77. }
  78. function refresh_gpg_keys {
  79. if [ ! -f /usr/bin/gpg ]; then
  80. if [ ! -f /usr/bin/pacman ]; then
  81. sudo apt-get -yq install gnupg
  82. else
  83. sudo pacman -S --noconfirm gnupg
  84. fi
  85. fi
  86. sudo cp /etc/crontab ~/temp_crontab
  87. sudo chown $CURR_USER:$CURR_GROUP ~/temp_crontab
  88. if ! grep -q "gpg --refresh-keys" ~/temp_crontab; then
  89. echo "0 */$REFRESH_GPG_KEYS_HOURS * * * $CURR_USER /usr/bin/gpg --refresh-keys > /dev/null" >> ~/temp_crontab
  90. sudo cp ~/temp_crontab /etc/crontab
  91. sudo chown root:root /etc/crontab
  92. fi
  93. rm ~/temp_crontab
  94. }
  95. # see https://stribika.github.io/2015/01/04/secure-secure-shell.html
  96. function ssh_remove_small_moduli {
  97. sudo awk '$5 > 2000' /etc/ssh/moduli > /home/$CURR_USER/moduli
  98. sudo mv /home/$CURR_USER/moduli /etc/ssh/moduli
  99. }
  100. function configure_ssh_client {
  101. if [ -f /usr/bin/pacman ]; then
  102. sudo pacman --noconfirm -S openbsd-netcat
  103. else
  104. sudo apt-get -yq install tor connect-proxy
  105. fi
  106. #sudo sed -i 's/# PasswordAuthentication.*/ PasswordAuthentication no/g' /etc/ssh/ssh_config
  107. #sudo sed -i 's/# ChallengeResponseAuthentication.*/ ChallengeResponseAuthentication no/g' /etc/ssh/ssh_config
  108. sudo sed -i "s/# HostKeyAlgorithms.*/ HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  109. sudo sed -i "s/# Ciphers.*/ Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  110. sudo sed -i "s/# MACs.*/ MACs $SSH_MACS/g" /etc/ssh/ssh_config
  111. sudo sed -i "s/HostKeyAlgorithms.*/HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  112. if ! grep -q "HostKeyAlgorithms" /etc/ssh/ssh_config; then
  113. sudo cp /etc/ssh/ssh_config ~/ssh_config
  114. sudo chown $CURR_USER:$CURR_GROUP ~/ssh_config
  115. echo " HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS" >> ~/ssh_config
  116. sudo mv ~/ssh_config /etc/ssh/ssh_config
  117. sudo chown root:root /etc/ssh/ssh_config
  118. fi
  119. sudo sed -i "s/HostKeyAlgorithms.*/#HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  120. sudo sed -i "s/Ciphers.*/Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  121. if ! grep -q "Ciphers " /etc/ssh/ssh_config; then
  122. sudo cp /etc/ssh/ssh_config ~/ssh_config
  123. sudo chown $CURR_USER:$CURR_GROUP ~/ssh_config
  124. echo " Ciphers $SSH_CIPHERS" >> ~/ssh_config
  125. sudo mv ~/ssh_config /etc/ssh/ssh_config
  126. sudo chown root:root /etc/ssh/ssh_config
  127. fi
  128. sudo sed -i "s/MACs.*/MACs $SSH_MACS/g" /etc/ssh/ssh_config
  129. if ! grep -q "MACs " /etc/ssh/ssh_config; then
  130. sudo cp /etc/ssh/ssh_config ~/ssh_config
  131. sudo chown $CURR_USER:$CURR_GROUP ~/ssh_config
  132. echo " MACs $SSH_MACS" >> ~/ssh_config
  133. sudo mv ~/ssh_config /etc/ssh/ssh_config
  134. sudo chown root:root /etc/ssh/ssh_config
  135. fi
  136. # Create ssh keys
  137. if [ ! -f /home/$CURR_USER/.ssh/id_ed25519 ]; then
  138. ssh-keygen -t ed25519 -o -a 100
  139. fi
  140. if [ ! -f /home/$CURR_USER/.ssh/id_rsa ]; then
  141. ssh-keygen -t rsa -b 4096 -o -a 100
  142. fi
  143. ssh_remove_small_moduli
  144. if [ ! -d ~/.ssh ]; then
  145. mkdir ~/.ssh
  146. fi
  147. echo 'Host *.onion' > ~/.ssh/config
  148. echo ' ServerAliveInterval 60' >> ~/.ssh/config
  149. echo ' ServerAliveCountMax 3' >> ~/.ssh/config
  150. proxycmd="connect -R remote -5 -S 127.0.0.1:9050 %h %p"
  151. if [ -f /usr/bin/pacman ]; then
  152. proxycmd="/usr/bin/nc -X 5 -x 127.0.0.1:9050 %h %p"
  153. fi
  154. if [[ $ENABLE_MONKEYSPHERE == $'yes' || $ENABLE_MONKEYSPHERE == $'y' ]]; then
  155. echo " ProxyCommand sh -c 'monkeysphere ssh-proxycommand --no-connect %h %p ; $proxycmd'" >> ~/.ssh/config
  156. else
  157. echo " ProxyCommand $proxycmd" >> ~/.ssh/config
  158. fi
  159. echo 'Host *' >> ~/.ssh/config
  160. echo ' ServerAliveInterval 60' >> ~/.ssh/config
  161. echo ' ServerAliveCountMax 3' >> ~/.ssh/config
  162. if [[ $ENABLE_MONKEYSPHERE == $'yes' || $ENABLE_MONKEYSPHERE == $'y' ]]; then
  163. echo ' ProxyCommand monkeysphere ssh-proxycommand %h %p' >> ~/.ssh/config
  164. fi
  165. echo ''
  166. echo $'Copy the following into a file called /home/username/.ssh/authorized_keys on the Freedombone server'
  167. echo ''
  168. echo $(cat /home/$CURR_USER/.ssh/id_rsa.pub)
  169. echo $(cat /home/$CURR_USER/.ssh/id_ed25519.pub)
  170. echo ''
  171. }
  172. function configure_monkeysphere {
  173. if [ -f /usr/bin/pacman ]; then
  174. return
  175. fi
  176. sudo apt-get -yq install monkeysphere
  177. }
  178. function show_help {
  179. echo ''
  180. echo $"${PROJECT_NAME}-client --mesh [yes|no] --monkeysphere [yes|no]"
  181. echo ''
  182. exit 0
  183. }
  184. function setup_client_app_irc {
  185. echo $'Setting up hexchat'
  186. if [ ! -f /usr/bin/pacman ]; then
  187. sudo apt-get -yq install tor hexchat tor
  188. else
  189. sudo pacman -S --noconfirm hexchat tor
  190. fi
  191. if [ ! -d /home/$CURR_USER/.config/hexchat ]; then
  192. mkdir -p /home/$CURR_USER/.config/hexchat
  193. fi
  194. echo 'net_proxy_host = 127.0.0.1' > /home/$CURR_USER/.config/hexchat/hexchat.conf
  195. echo 'net_proxy_port = 9050' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  196. echo 'net_proxy_type = 3' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  197. echo 'net_proxy_use = 0' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  198. echo 'net_proxy_auth = 1' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  199. echo 'net_proxy_pass = HexChat' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  200. echo 'net_proxy_user = HexChat' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  201. echo 'dcc_auto_chat = 0' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  202. echo 'dcc_auto_resume = 0' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  203. echo 'dcc_auto_send = 0' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  204. echo 'irc_hide_version = 1' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  205. echo 'identd = 0' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  206. echo 'away_reason =' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  207. echo 'irc_part_reason =' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  208. echo 'irc_quit_reason =' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  209. echo "irc_real_name = ${USER}" >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  210. echo "irc_user_name = ${USER}" >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  211. echo "irc_nick1 = ${USER}" >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  212. echo "irc_nick2 = ${USER}_" >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  213. echo "irc_nick3 = ${USER}__" >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  214. echo 'completion_suffix = :' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  215. echo 'gui_slist_skip = 1' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  216. echo $'hexchat configured'
  217. }
  218. function setup_client_app {
  219. if [ ! $SETUP_CLIENT_APP_NAME ]; then
  220. return
  221. fi
  222. case $SETUP_CLIENT_APP_NAME in
  223. hexchat|xchat|irc)
  224. setup_client_app_irc
  225. ;;
  226. esac
  227. exit 0
  228. }
  229. while [[ $# > 1 ]]
  230. do
  231. key="$1"
  232. case $key in
  233. -h|--help)
  234. show_help
  235. ;;
  236. --essid)
  237. shift
  238. WIFI_SSID="$1"
  239. ;;
  240. --channel)
  241. shift
  242. WIFI_CHANNEL=${1}
  243. ;;
  244. -s|--setup)
  245. shift
  246. SETUP_CLIENT_APP_NAME=${1}
  247. ;;
  248. -m|--mesh)
  249. shift
  250. MESH_CLIENT_INSTALL=${1}
  251. ;;
  252. --monkeysphere|--ms|--monkey)
  253. shift
  254. ENABLE_MONKEYSPHERE=${1}
  255. ;;
  256. *)
  257. # unknown option
  258. ;;
  259. esac
  260. shift
  261. done
  262. echo $'Configuring client'
  263. setup_client_app
  264. refresh_gpg_keys
  265. configure_ssh_client
  266. global_rate_limit
  267. configure_monkeysphere
  268. if [[ $MESH_CLIENT_INSTALL == $'yes' || $MESH_CLIENT_INSTALL == $'y' || $MESH_CLIENT_INSTALL == $'on' ]]; then
  269. echo $'Installing mesh packages'
  270. if [ ! -f /usr/bin/pacman ]; then
  271. sudo apt-get -yq install avahi-utils avahi-autoipd avahi-dnsconfd
  272. else
  273. sudo pacman -S --noconfirm avahi nss-mdns
  274. sudo sed -i 's|hosts:.*|hosts: files mdns_minimal [NOTFOUND=return] dns myhostname|g' /etc/nsswitch.conf
  275. fi
  276. sudo ${PROJECT_NAME}-mesh-install -f tox_node
  277. sudo ${PROJECT_NAME}-mesh-install -f toxic
  278. ${PROJECT_NAME}-mesh-install -f qtox
  279. sudo ${PROJECT_NAME}-mesh-install -f zeronet
  280. fi
  281. echo $'Configuration complete'
  282. exit 0