freedombone-client 11KB

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