freedombone-client 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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="2.00"
  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 vim-common
  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 2048 -o -a 100
  142. fi
  143. ssh_remove_small_moduli
  144. if [ ! -d ~/.ssh ]; then
  145. mkdir ~/.ssh
  146. fi
  147. if [ ! -f ~/.ssh/config ]; then
  148. touch ~/.ssh/config
  149. fi
  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 ! grep -q "# ${PROJECT_NAME} settings" ~/.ssh/config; then
  155. if ! grep -q 'ProxyCommand' ~/.ssh/config; then
  156. echo "# ${PROJECT_NAME} settings start" >> ~/.ssh/config
  157. echo 'Host *.onion' >> ~/.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 sh -c 'monkeysphere ssh-proxycommand --no-connect %h %p ; $proxycmd'" >> ~/.ssh/config
  162. else
  163. echo " ProxyCommand $proxycmd" >> ~/.ssh/config
  164. fi
  165. echo 'Host *' >> ~/.ssh/config
  166. echo ' ServerAliveInterval 60' >> ~/.ssh/config
  167. echo ' ServerAliveCountMax 3' >> ~/.ssh/config
  168. if [[ $ENABLE_MONKEYSPHERE == $'yes' || $ENABLE_MONKEYSPHERE == $'y' ]]; then
  169. echo ' ProxyCommand monkeysphere ssh-proxycommand %h %p' >> ~/.ssh/config
  170. fi
  171. echo "# ${PROJECT_NAME} settings end" >> ~/.ssh/config
  172. fi
  173. fi
  174. echo ''
  175. echo $'Go to the Administrator Control Panel, select "Manage Users", '
  176. echo $'"Change user ssh public key" then "yes" and paste the following:'
  177. echo ''
  178. echo $(cat /home/$CURR_USER/.ssh/id_rsa.pub)
  179. echo $(cat /home/$CURR_USER/.ssh/id_ed25519.pub)
  180. echo ''
  181. echo $'Then go to "Security Settings", select "Allow ssh login with passwords"'
  182. echo $'and set it to "no".'
  183. }
  184. function configure_monkeysphere {
  185. if [ -f /usr/bin/pacman ]; then
  186. return
  187. fi
  188. sudo apt-get -yq install monkeysphere
  189. }
  190. function show_help {
  191. echo ''
  192. echo $"${PROJECT_NAME}-client --mesh [yes|no] --monkeysphere [yes|no]"
  193. echo ''
  194. exit 0
  195. }
  196. function setup_client_app_irc {
  197. echo $'Setting up hexchat'
  198. if [ ! -f /usr/bin/pacman ]; then
  199. sudo apt-get -yq install tor hexchat tor
  200. else
  201. sudo pacman -S --noconfirm hexchat tor
  202. fi
  203. if [ ! -d /home/$CURR_USER/.config/hexchat ]; then
  204. mkdir -p /home/$CURR_USER/.config/hexchat
  205. fi
  206. echo 'net_proxy_host = 127.0.0.1' > /home/$CURR_USER/.config/hexchat/hexchat.conf
  207. echo 'net_proxy_port = 9050' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  208. echo 'net_proxy_type = 3' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  209. echo 'net_proxy_use = 0' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  210. echo 'net_proxy_auth = 1' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  211. echo 'net_proxy_pass = HexChat' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  212. echo 'net_proxy_user = HexChat' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  213. echo 'dcc_auto_chat = 0' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  214. echo 'dcc_auto_resume = 0' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  215. echo 'dcc_auto_send = 0' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  216. echo 'irc_hide_version = 1' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  217. echo 'identd = 0' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  218. echo 'away_reason =' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  219. echo 'irc_part_reason =' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  220. echo 'irc_quit_reason =' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  221. echo "irc_real_name = ${USER}" >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  222. echo "irc_user_name = ${USER}" >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  223. echo "irc_nick1 = ${USER}" >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  224. echo "irc_nick2 = ${USER}_" >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  225. echo "irc_nick3 = ${USER}__" >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  226. echo 'completion_suffix = :' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  227. echo 'gui_slist_skip = 1' >> /home/$CURR_USER/.config/hexchat/hexchat.conf
  228. echo $'hexchat configured'
  229. }
  230. function setup_client_app {
  231. if [ ! $SETUP_CLIENT_APP_NAME ]; then
  232. return
  233. fi
  234. case $SETUP_CLIENT_APP_NAME in
  235. hexchat|xchat|irc)
  236. setup_client_app_irc
  237. ;;
  238. esac
  239. exit 0
  240. }
  241. function remove_known_hosts_entries {
  242. # remove any previous freedombone entries from known hosts
  243. if [ -f /home/$CURR_USER/.ssh/known_hosts ]; then
  244. sed -i "/${PROJECT_NAME}.local/d" /home/$CURR_USER/.ssh/known_hosts
  245. fi
  246. }
  247. function setup_avahi_client {
  248. echo $'Configuring Avahi'
  249. if [ ! -f /usr/bin/pacman ]; then
  250. sudo apt-get -yq install avahi-utils avahi-autoipd avahi-dnsconfd
  251. else
  252. sudo pacman -S --noconfirm avahi nss-mdns
  253. sudo sed -i 's|hosts:.*|hosts: files mdns_minimal [NOTFOUND=return] dns myhostname|g' /etc/nsswitch.conf
  254. fi
  255. }
  256. function verify_ssh_server_key {
  257. ssh -o VisualHostKey=yes -o FingerprintHash=sha256 ${PROJECT_NAME}.local -p 2222 &
  258. pid=$!
  259. sleep 15
  260. kill ${pid}
  261. }
  262. while [[ $# > 0 ]]
  263. do
  264. key="$1"
  265. case $key in
  266. -h|--help)
  267. show_help
  268. ;;
  269. --essid)
  270. shift
  271. WIFI_SSID="$1"
  272. ;;
  273. --channel)
  274. shift
  275. WIFI_CHANNEL=${1}
  276. ;;
  277. -s|--setup)
  278. shift
  279. SETUP_CLIENT_APP_NAME=${1}
  280. ;;
  281. -v|--verify|--verifykeys)
  282. verify_ssh_server_key
  283. exit 0
  284. ;;
  285. -m|--mesh)
  286. shift
  287. MESH_CLIENT_INSTALL=${1}
  288. ;;
  289. --monkeysphere|--ms|--monkey)
  290. shift
  291. ENABLE_MONKEYSPHERE=${1}
  292. ;;
  293. *)
  294. # unknown option
  295. ;;
  296. esac
  297. shift
  298. done
  299. echo $'Configuring client'
  300. setup_avahi_client
  301. setup_client_app
  302. refresh_gpg_keys
  303. configure_ssh_client
  304. global_rate_limit
  305. configure_monkeysphere
  306. remove_known_hosts_entries
  307. if [[ $MESH_CLIENT_INSTALL == $'yes' || $MESH_CLIENT_INSTALL == $'y' || $MESH_CLIENT_INSTALL == $'on' ]]; then
  308. echo $'Installing mesh packages'
  309. sudo ${PROJECT_NAME}-mesh-install -f tox_node
  310. sudo ${PROJECT_NAME}-mesh-install -f toxic
  311. ${PROJECT_NAME}-mesh-install -f qtox
  312. sudo ${PROJECT_NAME}-mesh-install -f zeronet
  313. fi
  314. echo $'Configuration complete'
  315. exit 0