freedombone-client 11KB

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