freedombone-client 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2015-2018 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=$(grep 'SSH_CIPHERS=' "$UTILS_SSH" | head -n 1 | awk -F '"' '{print $2}')
  53. SSH_MACS=$(grep 'SSH_MACS=' "$UTILS_SSH" | head -n 1 | awk -F '"' '{print $2}')
  54. SSH_KEX=$(grep 'SSH_KEX=' "$UTILS_SSH" | head -n 1 | awk -F '"' '{print $2}')
  55. SSH_HOST_KEY_ALGORITHMS=$(grep 'SSH_HOST_KEY_ALGORITHMS=' "$UTILS_SSH" | 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. # shellcheck disable=SC2024
  97. sudo awk '$5 > 2000' /etc/ssh/moduli > "/home/$CURR_USER/moduli"
  98. # shellcheck disable=SC2086
  99. sudo mv /home/$CURR_USER/moduli /etc/ssh/moduli
  100. }
  101. function configure_ssh_client {
  102. if [ -f /usr/bin/pacman ]; then
  103. sudo pacman --noconfirm -S openbsd-netcat
  104. else
  105. sudo apt-get -yq install tor connect-proxy vim-common
  106. fi
  107. #sudo sed -i 's/# PasswordAuthentication.*/ PasswordAuthentication no/g' /etc/ssh/ssh_config
  108. #sudo sed -i 's/# ChallengeResponseAuthentication.*/ ChallengeResponseAuthentication no/g' /etc/ssh/ssh_config
  109. sudo sed -i "s/# HostKeyAlgorithms.*/ HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  110. sudo sed -i "s/# Ciphers.*/ Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  111. sudo sed -i "s/# MACs.*/ MACs $SSH_MACS/g" /etc/ssh/ssh_config
  112. sudo sed -i "s/HostKeyAlgorithms.*/HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  113. if ! grep -q "HostKeyAlgorithms" /etc/ssh/ssh_config; then
  114. sudo cp /etc/ssh/ssh_config ~/ssh_config
  115. sudo chown "$CURR_USER":"$CURR_GROUP" ~/ssh_config
  116. echo " HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS" >> ~/ssh_config
  117. sudo mv ~/ssh_config /etc/ssh/ssh_config
  118. sudo chown root:root /etc/ssh/ssh_config
  119. fi
  120. sudo sed -i "s/HostKeyAlgorithms.*/#HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  121. sudo sed -i "s/Ciphers.*/Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  122. if ! grep -q "Ciphers " /etc/ssh/ssh_config; then
  123. sudo cp /etc/ssh/ssh_config ~/ssh_config
  124. sudo chown "$CURR_USER":"$CURR_GROUP" ~/ssh_config
  125. echo " Ciphers $SSH_CIPHERS" >> ~/ssh_config
  126. sudo mv ~/ssh_config /etc/ssh/ssh_config
  127. sudo chown root:root /etc/ssh/ssh_config
  128. fi
  129. sudo sed -i "s/MACs.*/MACs $SSH_MACS/g" /etc/ssh/ssh_config
  130. if ! grep -q "MACs " /etc/ssh/ssh_config; then
  131. sudo cp /etc/ssh/ssh_config ~/ssh_config
  132. sudo chown "$CURR_USER":"$CURR_GROUP" ~/ssh_config
  133. echo " MACs $SSH_MACS" >> ~/ssh_config
  134. sudo mv ~/ssh_config /etc/ssh/ssh_config
  135. sudo chown root:root /etc/ssh/ssh_config
  136. fi
  137. # Create ssh keys
  138. if [ ! -f "/home/$CURR_USER/.ssh/id_ed25519" ]; then
  139. ssh-keygen -t ed25519 -o -a 100
  140. fi
  141. if [ ! -f "/home/$CURR_USER/.ssh/id_rsa" ]; then
  142. ssh-keygen -t rsa -b 2048 -o -a 100
  143. fi
  144. ssh_remove_small_moduli
  145. if [ ! -d ~/.ssh ]; then
  146. mkdir ~/.ssh
  147. fi
  148. if [ ! -f ~/.ssh/config ]; then
  149. touch ~/.ssh/config
  150. fi
  151. proxycmd="connect -R remote -5 -S 127.0.0.1:9050 %h %p"
  152. if [ -f /usr/bin/pacman ]; then
  153. proxycmd="/usr/bin/nc -X 5 -x 127.0.0.1:9050 %h %p"
  154. fi
  155. if ! grep -q "# ${PROJECT_NAME} settings" ~/.ssh/config; then
  156. if ! grep -q 'ProxyCommand' ~/.ssh/config; then
  157. { echo "# ${PROJECT_NAME} settings start";
  158. echo 'Host *.onion';
  159. echo ' ServerAliveInterval 60';
  160. echo ' ServerAliveCountMax 3'; } >> ~/.ssh/config
  161. if [[ "$ENABLE_MONKEYSPHERE" == $'yes' || "$ENABLE_MONKEYSPHERE" == $'y' ]]; then
  162. echo " ProxyCommand sh -c 'monkeysphere ssh-proxycommand --no-connect %h %p ; $proxycmd'" >> ~/.ssh/config
  163. else
  164. echo " ProxyCommand $proxycmd" >> ~/.ssh/config
  165. fi
  166. { echo 'Host *';
  167. echo ' ServerAliveInterval 60';
  168. echo ' ServerAliveCountMax 3'; } >> ~/.ssh/config
  169. if [[ "$ENABLE_MONKEYSPHERE" == $'yes' || "$ENABLE_MONKEYSPHERE" == $'y' ]]; then
  170. echo ' ProxyCommand monkeysphere ssh-proxycommand %h %p' >> ~/.ssh/config
  171. fi
  172. echo "# ${PROJECT_NAME} settings end" >> ~/.ssh/config
  173. fi
  174. fi
  175. echo ''
  176. echo $'Go to the Administrator Control Panel, select "Manage Users", '
  177. echo $'"Change user ssh public key" then "yes" and paste the following:'
  178. echo ''
  179. cat "/home/$CURR_USER/.ssh/id_rsa.pub"
  180. cat "/home/$CURR_USER/.ssh/id_ed25519.pub"
  181. echo ''
  182. echo $'Then go to "Security Settings", select "Allow ssh login with passwords"'
  183. echo $'and set it to "no".'
  184. }
  185. function configure_monkeysphere {
  186. if [ -f /usr/bin/pacman ]; then
  187. return
  188. fi
  189. sudo apt-get -yq install monkeysphere
  190. }
  191. function show_help {
  192. echo ''
  193. echo $"${PROJECT_NAME}-client --monkeysphere [yes|no]"
  194. echo ''
  195. exit 0
  196. }
  197. function setup_client_app_irc {
  198. echo $'Setting up hexchat'
  199. if [ ! -f /usr/bin/pacman ]; then
  200. sudo apt-get -yq install tor hexchat tor
  201. else
  202. sudo pacman -S --noconfirm hexchat tor
  203. fi
  204. if [ ! -d "/home/$CURR_USER/.config/hexchat" ]; then
  205. mkdir -p "/home/$CURR_USER/.config/hexchat"
  206. fi
  207. { echo 'net_proxy_host = 127.0.0.1';
  208. echo 'net_proxy_port = 9050';
  209. echo 'net_proxy_type = 3';
  210. echo 'net_proxy_use = 0';
  211. echo 'net_proxy_auth = 1';
  212. echo 'net_proxy_pass = HexChat';
  213. echo 'net_proxy_user = HexChat';
  214. echo 'dcc_auto_chat = 0';
  215. echo 'dcc_auto_resume = 0';
  216. echo 'dcc_auto_send = 0';
  217. echo 'irc_hide_version = 1';
  218. echo 'identd = 0';
  219. echo 'away_reason =';
  220. echo 'irc_part_reason =';
  221. echo 'irc_quit_reason =';
  222. echo "irc_real_name = ${USER}";
  223. echo "irc_user_name = ${USER}";
  224. echo "irc_nick1 = ${USER}";
  225. echo "irc_nick2 = ${USER}_";
  226. echo "irc_nick3 = ${USER}__";
  227. echo 'completion_suffix = :';
  228. echo 'gui_slist_skip = 1'; } > "/home/$CURR_USER/.config/hexchat/hexchat.conf"
  229. echo $'hexchat configured'
  230. }
  231. function setup_client_app {
  232. if [ ! $SETUP_CLIENT_APP_NAME ]; then
  233. return
  234. fi
  235. case $SETUP_CLIENT_APP_NAME in
  236. hexchat|xchat|irc)
  237. setup_client_app_irc
  238. ;;
  239. esac
  240. exit 0
  241. }
  242. function remove_known_hosts_entries {
  243. # remove any previous freedombone entries from known hosts
  244. if [ -f "/home/$CURR_USER/.ssh/known_hosts" ]; then
  245. sed -i "/${PROJECT_NAME}.local/d" "/home/$CURR_USER/.ssh/known_hosts"
  246. fi
  247. }
  248. function setup_avahi_client {
  249. echo $'Configuring Avahi'
  250. if [ ! -f /usr/bin/pacman ]; then
  251. sudo apt-get -yq install avahi-utils avahi-dnsconfd
  252. else
  253. sudo pacman -S --noconfirm avahi nss-mdns
  254. sudo sed -i 's|hosts:.*|hosts: files mdns_minimal [NOTFOUND=return] dns myhostname|g' /etc/nsswitch.conf
  255. fi
  256. }
  257. function verify_ssh_server_key {
  258. ssh -o VisualHostKey=yes -o FingerprintHash=sha256 ${PROJECT_NAME}.local -p 2222 &
  259. pid=$!
  260. sleep 15
  261. kill ${pid}
  262. }
  263. while [ $# -gt 0 ]
  264. do
  265. key="$1"
  266. case $key in
  267. -h|--help)
  268. show_help
  269. ;;
  270. --essid)
  271. shift
  272. WIFI_SSID="$1"
  273. ;;
  274. --channel)
  275. shift
  276. WIFI_CHANNEL=${1}
  277. ;;
  278. -s|--setup)
  279. shift
  280. SETUP_CLIENT_APP_NAME=${1}
  281. ;;
  282. -v|--verify|--verifykeys)
  283. verify_ssh_server_key
  284. exit 0
  285. ;;
  286. --monkeysphere|--ms|--monkey)
  287. shift
  288. ENABLE_MONKEYSPHERE=${1}
  289. ;;
  290. *)
  291. # unknown option
  292. ;;
  293. esac
  294. shift
  295. done
  296. echo $'Configuring client'
  297. setup_avahi_client
  298. setup_client_app
  299. refresh_gpg_keys
  300. configure_ssh_client
  301. global_rate_limit
  302. configure_monkeysphere
  303. remove_known_hosts_entries
  304. echo $'Configuration complete'
  305. exit 0