freedombone-sec 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Alters the security settings
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. SSL_PROTOCOLS=
  31. SSL_CIPHERS=
  32. SSH_CIPHERS=
  33. SSH_MACS=
  34. SSH_KEX=
  35. SSH_HOST_KEY_ALGORITHMS=
  36. SSH_PASSWORDS=
  37. XMPP_CIPHERS=
  38. XMPP_ECC_CURVE=
  39. WEBSITES_DIRECTORY='/etc/nginx/sites-available'
  40. DOVECOT_CIPHERS='/etc/dovecot/conf.d/10-ssl.conf'
  41. SSH_CONFIG='/etc/ssh/sshd_config'
  42. XMPP_CONFIG='/etc/prosody/conf.avail/xmpp.cfg.lua'
  43. MINIMUM_LENGTH=6
  44. function get_protocols_from_website {
  45. if [ ! -f $WEBSITES_DIRECTORY/$1 ]; then
  46. return
  47. fi
  48. SSL_PROTOCOLS=$(cat $WEBSITES_DIRECTORY/$1 | grep 'ssl_protocols ' | awk -F "ssl_protocols " '{print $2}' | awk -F ';' '{print $1}')
  49. }
  50. function get_ciphers_from_website {
  51. if [ ! -f $WEBSITES_DIRECTORY/$1 ]; then
  52. return
  53. fi
  54. SSL_CIPHERS=$(cat $WEBSITES_DIRECTORY/$1 | grep 'ssl_ciphers ' | awk -F "ssl_ciphers " '{print $2}' | awk -F "'" '{print $2}')
  55. }
  56. function get_website_settings {
  57. if [ ! -d $WEBSITES_DIRECTORY ]; then
  58. return
  59. fi
  60. cd $WEBSITES_DIRECTORY
  61. for file in `dir -d *` ; do
  62. get_protocols_from_website $file
  63. if [ ${#SSL_PROTOCOLS} -gt $MINIMUM_LENGTH ]; then
  64. get_ciphers_from_website $file
  65. if [ ${#SSL_CIPHERS} -gt $MINIMUM_LENGTH ]; then
  66. break
  67. else
  68. SSL_PROTOCOLS=""
  69. fi
  70. fi
  71. done
  72. }
  73. function get_imap_settings {
  74. if [ ! -f $DOVECOT_CIPHERS ]; then
  75. return
  76. fi
  77. # clear commented out cipher list
  78. sed -i "s|#ssl_cipher_list.*||g" $DOVECOT_CIPHERS
  79. if [ $SSL_CIPHERS ]; then
  80. return
  81. fi
  82. if [ ${#SSL_CIPHERS} -gt $MINIMUM_LENGTH ]; then
  83. return
  84. fi
  85. SSL_CIPHERS=$(cat $DOVECOT_CIPHERS | grep 'ssl_cipher_list' | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')
  86. }
  87. function get_xmpp_settings {
  88. if [ ! -f $XMPP_CONFIG ]; then
  89. return
  90. fi
  91. XMPP_CIPHERS=$(cat $XMPP_CONFIG | grep 'ciphers ' | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  92. XMPP_ECC_CURVE=$(cat $XMPP_CONFIG | grep 'curve ' | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  93. }
  94. function get_ssh_settings {
  95. if [ -f $SSH_CONFIG ]; then
  96. SSH_CIPHERS=$(cat $SSH_CONFIG | grep 'Ciphers ' | awk -F 'Ciphers ' '{print $2}')
  97. SSH_MACS=$(cat $SSH_CONFIG | grep 'MACs ' | awk -F 'MACs ' '{print $2}')
  98. SSH_KEX=$(cat $SSH_CONFIG | grep 'KexAlgorithms ' | awk -F 'KexAlgorithms ' '{print $2}')
  99. SSH_PASSWORDS=$(cat $SSH_CONFIG | grep 'PasswordAuthentication ' | awk -F 'PasswordAuthentication ' '{print $2}')
  100. fi
  101. if [ -f /etc/ssh/ssh_config ]; then
  102. SSH_HOST_KEY_ALGORITHMS=$(cat /etc/ssh/ssh_config | grep 'HostKeyAlgorithms ' | awk -F 'HostKeyAlgorithms ' '{print $2}')
  103. if [ ! $SSH_CIPHERS ]; then
  104. SSH_CIPHERS=$(cat /etc/ssh/ssh_config | grep 'Ciphers ' | awk -F 'Ciphers ' '{print $2}')
  105. fi
  106. if [ ! $SSH_MACS ]; then
  107. SSH_MACS=$(cat /etc/ssh/ssh_config | grep 'MACs ' | awk -F 'MACs ' '{print $2}')
  108. fi
  109. fi
  110. }
  111. function change_website_settings {
  112. if [ ! "$SSL_PROTOCOLS" ]; then
  113. return
  114. fi
  115. if [ ! $SSL_CIPHERS ]; then
  116. return
  117. fi
  118. if [ ${#SSL_PROTOCOLS} -lt $MINIMUM_LENGTH ]; then
  119. return
  120. fi
  121. if [ ${#SSL_CIPHERS} -lt $MINIMUM_LENGTH ]; then
  122. return
  123. fi
  124. if [ ! -d $WEBSITES_DIRECTORY ]; then
  125. return
  126. fi
  127. cd $WEBSITES_DIRECTORY
  128. for file in `dir -d *` ; do
  129. sed -i "s|ssl_protocols .*|ssl_protocols $SSL_PROTOCOLS;|g" $WEBSITES_DIRECTORY/$file
  130. sed -i "s|ssl_ciphers .*|ssl_ciphers '$SSL_CIPHERS';|g" $WEBSITES_DIRECTORY/$file
  131. done
  132. service nginx restart
  133. echo 'Web security settings changed'
  134. }
  135. function change_imap_settings {
  136. if [ ! -f $DOVECOT_CIPHERS ]; then
  137. return
  138. fi
  139. if [ ! $SSL_CIPHERS ]; then
  140. return
  141. fi
  142. if [ ${#SSL_CIPHERS} -lt $MINIMUM_LENGTH ]; then
  143. return
  144. fi
  145. sed -i "s|ssl_cipher_list.*|ssl_cipher_list = '$SSL_CIPHERS'|g" $DOVECOT_CIPHERS
  146. service dovecot restart
  147. echo 'imap security settings changed'
  148. }
  149. function change_ssh_settings {
  150. if [ -f /etc/ssh/ssh_config ]; then
  151. if [ $SSH_HOST_KEY_ALGORITHMS ]; then
  152. sed -i "s|HostKeyAlgorithms .*|HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS|g" /etc/ssh/ssh_config
  153. echo 'ssh client security settings changed'
  154. fi
  155. fi
  156. if [ -f $SSH_CONFIG ]; then
  157. if [ ! $SSH_CIPHERS ]; then
  158. return
  159. fi
  160. if [ ! $SSH_MACS ]; then
  161. return
  162. fi
  163. if [ ! $SSH_KEX ]; then
  164. return
  165. fi
  166. if [ ! $SSH_PASSWORDS ]; then
  167. return
  168. fi
  169. sed -i "s|Ciphers .*|Ciphers $SSH_CIPHERS|g" $SSH_CONFIG
  170. sed -i "s|MACs .*|MACs $SSH_MACS|g" $SSH_CONFIG
  171. sed -i "s|KexAlgorithms .*|KexAlgorithms $SSH_KEX|g" $SSH_CONFIG
  172. sed -i "s|PasswordAuthentication .*|PasswordAuthentication $SSH_PASSWORDS|g" $SSH_CONFIG
  173. service ssh restart
  174. echo 'ssh server security settings changed'
  175. fi
  176. }
  177. function change_xmpp_settings {
  178. if [ ! -f $XMPP_CONFIG ]; then
  179. return
  180. fi
  181. if [ ! $XMPP_CIPHERS ]; then
  182. return
  183. fi
  184. if [ ! $XMPP_ECC_CURVE ]; then
  185. return
  186. fi
  187. sed -i "s|ciphers =.*|ciphers = \"$XMPP_CIPHERS\";|g" $XMPP_CONFIG
  188. sed -i "s|curve =.*|curve = \"$XMPP_ECC_CURVE\";|g" $XMPP_CONFIG
  189. service prosody restart
  190. echo 'xmpp security settings changed'
  191. }
  192. function interactive_setup {
  193. if [ $SSL_CIPHERS ]; then
  194. data=$(tempfile 2>/dev/null)
  195. trap "rm -f $data" 0 1 2 5 15
  196. dialog --backtitle "Freedombone Security Configuration" \
  197. --form "\nWeb/IMAP Ciphers:" 10 95 2 \
  198. "Protocols:" 1 1 "$SSL_PROTOCOLS" 1 15 90 90 \
  199. "Ciphers:" 2 1 "$SSL_CIPHERS" 2 15 90 512 \
  200. 2> $data
  201. sel=$?
  202. case $sel in
  203. 1) SSL_PROTOCOLS=$(cat $data | sed -n 1p)
  204. SSL_CIPHERS=$(cat $data | sed -n 2p)
  205. ;;
  206. 255) exit 0;;
  207. esac
  208. fi
  209. data=$(tempfile 2>/dev/null)
  210. trap "rm -f $data" 0 1 2 5 15
  211. if [ $SSH_HOST_KEY_ALGORITHMS ]; then
  212. dialog --backtitle "Freedombone Security Configuration" \
  213. --form "\nSecure Shell Ciphers:" 13 95 4 \
  214. "Ciphers:" 1 1 "$SSH_CIPHERS" 1 15 90 512 \
  215. "MACs:" 2 1 "$SSH_MACS" 2 15 90 512 \
  216. "KEX:" 3 1 "$SSH_KEX" 3 15 90 512 \
  217. "Host key algorithms:" 4 1 "$SSH_HOST_KEY_ALGORITHMS" 4 15 90 512 \
  218. 2> $data
  219. sel=$?
  220. case $sel in
  221. 1) SSH_CIPHERS=$(cat $data | sed -n 1p)
  222. SSH_MACS=$(cat $data | sed -n 2p)
  223. SSH_KEX=$(cat $data | sed -n 3p)
  224. SSH_HOST_KEY_ALGORITHMS=$(cat $data | sed -n 4p)
  225. ;;
  226. 255) exit 0;;
  227. esac
  228. else
  229. dialog --backtitle "Freedombone Security Configuration" \
  230. --form "\nSecure Shell Ciphers:" 11 95 3 \
  231. "Ciphers:" 1 1 "$SSH_CIPHERS" 1 15 90 512 \
  232. "MACs:" 2 1 "$SSH_MACS" 2 15 90 512 \
  233. "KEX:" 3 1 "$SSH_KEX" 3 15 90 512 \
  234. 2> $data
  235. sel=$?
  236. case $sel in
  237. 1) SSH_CIPHERS=$(cat $data | sed -n 1p)
  238. SSH_MACS=$(cat $data | sed -n 2p)
  239. SSH_KEX=$(cat $data | sed -n 3p)
  240. ;;
  241. 255) exit 0;;
  242. esac
  243. fi
  244. if [[ $SSH_PASSWORDS == "yes" ]]; then
  245. dialog --title "SSH Passwords" \
  246. --backtitle "Freedombone Security Configuration" \
  247. --yesno "\nAllow SSH login using passwords?" 7 60
  248. else
  249. dialog --title "SSH Passwords" \
  250. --backtitle "Freedombone Security Configuration" \
  251. --defaultno \
  252. --yesno "\nAllow SSH login using passwords?" 7 60
  253. fi
  254. sel=$?
  255. case $sel in
  256. 0) SSH_PASSWORDS="yes";;
  257. 1) SSH_PASSWORDS="no";;
  258. 255) exit 0;;
  259. esac
  260. if [ $XMPP_CIPHERS ]; then
  261. data=$(tempfile 2>/dev/null)
  262. trap "rm -f $data" 0 1 2 5 15
  263. dialog --backtitle "Freedombone Security Configuration" \
  264. --form "\nXMPP Ciphers:" 10 95 2 \
  265. "Ciphers:" 1 1 "$XMPP_CIPHERS" 1 15 90 512 \
  266. "ECC Curve:" 2 1 "$XMPP_ECC_CURVE" 2 15 50 50 \
  267. 2> $data
  268. sel=$?
  269. case $sel in
  270. 1) XMPP_CIPHERS=$(cat $data | sed -n 1p)
  271. XMPP_ECC_CURVE=$(cat $data | sed -n 2p)
  272. ;;
  273. 255) exit 0;;
  274. esac
  275. fi
  276. dialog --title "Final Confirmation" \
  277. --backtitle "Freedombone Security Configuration" \
  278. --defaultno \
  279. --yesno "\nPlease confirm that you wish your security settings to be changed?\n\nWARNING: any mistakes made in the security settings could compromise your system, so be extra careful when answering 'yes'." 7 60
  280. sel=$?
  281. case $sel in
  282. 1) echo 'Exiting without changing security settings'
  283. exit 0;;
  284. 255) echo 'Exiting without changing security settings'
  285. exit 0;;
  286. esac
  287. clear
  288. }
  289. get_website_settings
  290. get_imap_settings
  291. get_ssh_settings
  292. get_xmpp_settings
  293. interactive_setup
  294. change_website_settings
  295. change_imap_settings
  296. change_ssh_settings
  297. change_xmpp_settings
  298. exit 0