freedombone-client 12KB

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