freedombone-sec 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. IMPORT_FILE=
  45. EXPORT_FILE=
  46. CURRENT_DIR=$(pwd)
  47. REGENERATE_SSH_HOST_KEYS="no"
  48. REGENERATE_DH_KEYS="no"
  49. DH_KEYLENGTH=2048
  50. function get_protocols_from_website {
  51. if [ ! -f $WEBSITES_DIRECTORY/$1 ]; then
  52. return
  53. fi
  54. SSL_PROTOCOLS=$(cat $WEBSITES_DIRECTORY/$1 | grep 'ssl_protocols ' | awk -F "ssl_protocols " '{print $2}' | awk -F ';' '{print $1}')
  55. }
  56. function get_ciphers_from_website {
  57. if [ ! -f $WEBSITES_DIRECTORY/$1 ]; then
  58. return
  59. fi
  60. SSL_CIPHERS=$(cat $WEBSITES_DIRECTORY/$1 | grep 'ssl_ciphers ' | awk -F "ssl_ciphers " '{print $2}' | awk -F "'" '{print $2}')
  61. }
  62. function get_website_settings {
  63. if [ ! -d $WEBSITES_DIRECTORY ]; then
  64. return
  65. fi
  66. cd $WEBSITES_DIRECTORY
  67. for file in `dir -d *` ; do
  68. get_protocols_from_website $file
  69. if [ ${#SSL_PROTOCOLS} -gt $MINIMUM_LENGTH ]; then
  70. get_ciphers_from_website $file
  71. if [ ${#SSL_CIPHERS} -gt $MINIMUM_LENGTH ]; then
  72. break
  73. else
  74. SSL_PROTOCOLS=""
  75. fi
  76. fi
  77. done
  78. }
  79. function get_imap_settings {
  80. if [ ! -f $DOVECOT_CIPHERS ]; then
  81. return
  82. fi
  83. # clear commented out cipher list
  84. sed -i "s|#ssl_cipher_list.*||g" $DOVECOT_CIPHERS
  85. if [ $SSL_CIPHERS ]; then
  86. return
  87. fi
  88. if [ ${#SSL_CIPHERS} -gt $MINIMUM_LENGTH ]; then
  89. return
  90. fi
  91. SSL_CIPHERS=$(cat $DOVECOT_CIPHERS | grep 'ssl_cipher_list' | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')
  92. }
  93. function get_xmpp_settings {
  94. if [ ! -f $XMPP_CONFIG ]; then
  95. return
  96. fi
  97. XMPP_CIPHERS=$(cat $XMPP_CONFIG | grep 'ciphers ' | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  98. XMPP_ECC_CURVE=$(cat $XMPP_CONFIG | grep 'curve ' | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  99. }
  100. function get_ssh_settings {
  101. if [ -f $SSH_CONFIG ]; then
  102. SSH_CIPHERS=$(cat $SSH_CONFIG | grep 'Ciphers ' | awk -F 'Ciphers ' '{print $2}')
  103. SSH_MACS=$(cat $SSH_CONFIG | grep 'MACs ' | awk -F 'MACs ' '{print $2}')
  104. SSH_KEX=$(cat $SSH_CONFIG | grep 'KexAlgorithms ' | awk -F 'KexAlgorithms ' '{print $2}')
  105. SSH_PASSWORDS=$(cat $SSH_CONFIG | grep 'PasswordAuthentication ' | awk -F 'PasswordAuthentication ' '{print $2}')
  106. fi
  107. if [ -f /etc/ssh/ssh_config ]; then
  108. SSH_HOST_KEY_ALGORITHMS=$(cat /etc/ssh/ssh_config | grep 'HostKeyAlgorithms ' | awk -F 'HostKeyAlgorithms ' '{print $2}')
  109. if [ ! $SSH_CIPHERS ]; then
  110. SSH_CIPHERS=$(cat /etc/ssh/ssh_config | grep 'Ciphers ' | awk -F 'Ciphers ' '{print $2}')
  111. fi
  112. if [ ! $SSH_MACS ]; then
  113. SSH_MACS=$(cat /etc/ssh/ssh_config | grep 'MACs ' | awk -F 'MACs ' '{print $2}')
  114. fi
  115. fi
  116. }
  117. function change_website_settings {
  118. if [ ! "$SSL_PROTOCOLS" ]; then
  119. return
  120. fi
  121. if [ ! $SSL_CIPHERS ]; then
  122. return
  123. fi
  124. if [ ${#SSL_PROTOCOLS} -lt $MINIMUM_LENGTH ]; then
  125. return
  126. fi
  127. if [ ${#SSL_CIPHERS} -lt $MINIMUM_LENGTH ]; then
  128. return
  129. fi
  130. if [ ! -d $WEBSITES_DIRECTORY ]; then
  131. return
  132. fi
  133. cd $WEBSITES_DIRECTORY
  134. for file in `dir -d *` ; do
  135. sed -i "s|ssl_protocols .*|ssl_protocols $SSL_PROTOCOLS;|g" $WEBSITES_DIRECTORY/$file
  136. sed -i "s|ssl_ciphers .*|ssl_ciphers '$SSL_CIPHERS';|g" $WEBSITES_DIRECTORY/$file
  137. done
  138. service nginx restart
  139. echo 'Web security settings changed'
  140. }
  141. function change_imap_settings {
  142. if [ ! -f $DOVECOT_CIPHERS ]; then
  143. return
  144. fi
  145. if [ ! $SSL_CIPHERS ]; then
  146. return
  147. fi
  148. if [ ${#SSL_CIPHERS} -lt $MINIMUM_LENGTH ]; then
  149. return
  150. fi
  151. sed -i "s|ssl_cipher_list.*|ssl_cipher_list = '$SSL_CIPHERS'|g" $DOVECOT_CIPHERS
  152. sed -i "s|ssl_protocols.*|ssl_protocols = '$SSL_PROTOCOLS'|g" $DOVECOT_CIPHERS
  153. service dovecot restart
  154. echo 'imap security settings changed'
  155. }
  156. function change_ssh_settings {
  157. if [ -f /etc/ssh/ssh_config ]; then
  158. if [ $SSH_HOST_KEY_ALGORITHMS ]; then
  159. sed -i "s|HostKeyAlgorithms .*|HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS|g" /etc/ssh/ssh_config
  160. echo 'ssh client security settings changed'
  161. fi
  162. fi
  163. if [ -f $SSH_CONFIG ]; then
  164. if [ ! $SSH_CIPHERS ]; then
  165. return
  166. fi
  167. if [ ! $SSH_MACS ]; then
  168. return
  169. fi
  170. if [ ! $SSH_KEX ]; then
  171. return
  172. fi
  173. if [ ! $SSH_PASSWORDS ]; then
  174. return
  175. fi
  176. sed -i "s|Ciphers .*|Ciphers $SSH_CIPHERS|g" $SSH_CONFIG
  177. sed -i "s|MACs .*|MACs $SSH_MACS|g" $SSH_CONFIG
  178. sed -i "s|KexAlgorithms .*|KexAlgorithms $SSH_KEX|g" $SSH_CONFIG
  179. sed -i "s|PasswordAuthentication .*|PasswordAuthentication $SSH_PASSWORDS|g" $SSH_CONFIG
  180. service ssh restart
  181. echo 'ssh server security settings changed'
  182. fi
  183. }
  184. function change_xmpp_settings {
  185. if [ ! -f $XMPP_CONFIG ]; then
  186. return
  187. fi
  188. if [ ! $XMPP_CIPHERS ]; then
  189. return
  190. fi
  191. if [ ! $XMPP_ECC_CURVE ]; then
  192. return
  193. fi
  194. sed -i "s|ciphers =.*|ciphers = \"$XMPP_CIPHERS\";|g" $XMPP_CONFIG
  195. sed -i "s|curve =.*|curve = \"$XMPP_ECC_CURVE\";|g" $XMPP_CONFIG
  196. service prosody restart
  197. echo 'xmpp security settings changed'
  198. }
  199. function interactive_setup {
  200. if [ $SSL_CIPHERS ]; then
  201. data=$(tempfile 2>/dev/null)
  202. trap "rm -f $data" 0 1 2 5 15
  203. dialog --backtitle "Freedombone Security Configuration" \
  204. --form "\nWeb/IMAP Ciphers:" 10 95 2 \
  205. "Protocols:" 1 1 "$SSL_PROTOCOLS" 1 15 90 90 \
  206. "Ciphers:" 2 1 "$SSL_CIPHERS" 2 15 90 512 \
  207. 2> $data
  208. sel=$?
  209. case $sel in
  210. 1) SSL_PROTOCOLS=$(cat $data | sed -n 1p)
  211. SSL_CIPHERS=$(cat $data | sed -n 2p)
  212. ;;
  213. 255) exit 0;;
  214. esac
  215. fi
  216. data=$(tempfile 2>/dev/null)
  217. trap "rm -f $data" 0 1 2 5 15
  218. if [ $SSH_HOST_KEY_ALGORITHMS ]; then
  219. dialog --backtitle "Freedombone Security Configuration" \
  220. --form "\nSecure Shell Ciphers:" 13 95 4 \
  221. "Ciphers:" 1 1 "$SSH_CIPHERS" 1 15 90 512 \
  222. "MACs:" 2 1 "$SSH_MACS" 2 15 90 512 \
  223. "KEX:" 3 1 "$SSH_KEX" 3 15 90 512 \
  224. "Host key algorithms:" 4 1 "$SSH_HOST_KEY_ALGORITHMS" 4 15 90 512 \
  225. 2> $data
  226. sel=$?
  227. case $sel in
  228. 1) SSH_CIPHERS=$(cat $data | sed -n 1p)
  229. SSH_MACS=$(cat $data | sed -n 2p)
  230. SSH_KEX=$(cat $data | sed -n 3p)
  231. SSH_HOST_KEY_ALGORITHMS=$(cat $data | sed -n 4p)
  232. ;;
  233. 255) exit 0;;
  234. esac
  235. else
  236. dialog --backtitle "Freedombone Security Configuration" \
  237. --form "\nSecure Shell Ciphers:" 11 95 3 \
  238. "Ciphers:" 1 1 "$SSH_CIPHERS" 1 15 90 512 \
  239. "MACs:" 2 1 "$SSH_MACS" 2 15 90 512 \
  240. "KEX:" 3 1 "$SSH_KEX" 3 15 90 512 \
  241. 2> $data
  242. sel=$?
  243. case $sel in
  244. 1) SSH_CIPHERS=$(cat $data | sed -n 1p)
  245. SSH_MACS=$(cat $data | sed -n 2p)
  246. SSH_KEX=$(cat $data | sed -n 3p)
  247. ;;
  248. 255) exit 0;;
  249. esac
  250. fi
  251. if [[ $SSH_PASSWORDS == "yes" ]]; then
  252. dialog --title "SSH Passwords" \
  253. --backtitle "Freedombone Security Configuration" \
  254. --yesno "\nAllow SSH login using passwords?" 7 60
  255. else
  256. dialog --title "SSH Passwords" \
  257. --backtitle "Freedombone Security Configuration" \
  258. --defaultno \
  259. --yesno "\nAllow SSH login using passwords?" 7 60
  260. fi
  261. sel=$?
  262. case $sel in
  263. 0) SSH_PASSWORDS="yes";;
  264. 1) SSH_PASSWORDS="no";;
  265. 255) exit 0;;
  266. esac
  267. if [ $XMPP_CIPHERS ]; then
  268. data=$(tempfile 2>/dev/null)
  269. trap "rm -f $data" 0 1 2 5 15
  270. dialog --backtitle "Freedombone Security Configuration" \
  271. --form "\nXMPP Ciphers:" 10 95 2 \
  272. "Ciphers:" 1 1 "$XMPP_CIPHERS" 1 15 90 512 \
  273. "ECC Curve:" 2 1 "$XMPP_ECC_CURVE" 2 15 50 50 \
  274. 2> $data
  275. sel=$?
  276. case $sel in
  277. 1) XMPP_CIPHERS=$(cat $data | sed -n 1p)
  278. XMPP_ECC_CURVE=$(cat $data | sed -n 2p)
  279. ;;
  280. 255) exit 0;;
  281. esac
  282. fi
  283. dialog --title "Final Confirmation" \
  284. --backtitle "Freedombone Security Configuration" \
  285. --defaultno \
  286. --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'." 12 60
  287. sel=$?
  288. case $sel in
  289. 1) clear
  290. echo 'Exiting without changing security settings'
  291. exit 0;;
  292. 255) clear
  293. echo 'Exiting without changing security settings'
  294. exit 0;;
  295. esac
  296. clear
  297. }
  298. function regenerate_ssh_host_keys {
  299. if [[ $REGENERATE_SSH_HOST_KEYS == "yes" ]]; then
  300. rm -f /etc/ssh/ssh_host_*
  301. dpkg-reconfigure openssh-server
  302. echo 'ssh host keys regenerated'
  303. # remove small moduli
  304. awk '$5 > 2000' /etc/ssh/moduli > ~/moduli
  305. mv ~/moduli /etc/ssh/moduli
  306. echo 'ssh small moduli removed'
  307. systemctl restart ssh
  308. fi
  309. }
  310. function regenerate_dh_keys {
  311. if [[ $REGENERATE_DH_KEYS == "yes" ]]; then
  312. if [ ! -d /etc/ssl/mycerts ]; then
  313. echo 'No dhparam certificates were found'
  314. return
  315. fi
  316. data=$(tempfile 2>/dev/null)
  317. trap "rm -f $data" 0 1 2 5 15
  318. dialog --backtitle "Freedombone Security Configuration" \
  319. --title "Diffie-Hellman key length" \
  320. --radiolist "The smaller length is better suited to low power embedded systems:" 12 40 3 \
  321. 1 "1024 bits (WARNING: this may be insecure)" off \
  322. 2 "2048 bits" on \
  323. 3 "3072 bits" off 2> $data
  324. sel=$?
  325. case $sel in
  326. 1) exit 1;;
  327. 255) exit 1;;
  328. esac
  329. case $(cat $data) in
  330. 1) DH_KEYLENGTH=1024;;
  331. 2) DH_KEYLENGTH=2048;;
  332. 3) DH_KEYLENGTH=3072;;
  333. esac
  334. ctr=0
  335. for file in /etc/ssl/mycerts/*
  336. do
  337. if [[ -f $file ]]; then
  338. filename=/etc/ssl/certs/$(echo $file | awk -F '/etc/ssl/mycerts/' '{print $2}' | awk -F '.crt' '{print $1}').dhparam
  339. if [ -f $filename ]; then
  340. openssl dhparam -check -text -5 $DH_KEYLENGTH -out $filename
  341. ctr=$((ctr + 1))
  342. fi
  343. fi
  344. done
  345. echo "$ctr dhparam certificates were regenerated"
  346. fi
  347. }
  348. function renew_startssl {
  349. renew_domain=
  350. data=$(tempfile 2>/dev/null)
  351. trap "rm -f $data" 0 1 2 5 15
  352. dialog --title "Renew a StartSSL certificate" \
  353. --backtitle "Freedombone Security Settings" \
  354. --inputbox "Enter the domain name" 8 60 2>$data
  355. sel=$?
  356. case $sel in
  357. 0)
  358. renew_domain=$(<$data)
  359. ;;
  360. esac
  361. if [ ! $renew_domain ]; then
  362. return
  363. fi
  364. if [[ $renew_domain == "http"* ]]; then
  365. dialog --title "Renew a StartSSL certificate" \
  366. --msgbox "Don't include the https://" 6 40
  367. return
  368. fi
  369. if [ ! -f /etc/ssl/certs/${renew_domain}.dhparam ]; then
  370. dialog --title "Renew a StartSSL certificate" \
  371. --msgbox "An existing certificate for $renew_domain was not found" 6 40
  372. return
  373. fi
  374. if [[ $renew_domain != *"."* ]]; then
  375. dialog --title "Renew a StartSSL certificate" \
  376. --msgbox "Invalid domain name: $renew_domain" 6 40
  377. return
  378. fi
  379. freedombone-renew-cert -h $renew_domain -p startssl
  380. exit 0
  381. }
  382. function renew_letsencrypt {
  383. renew_domain=
  384. data=$(tempfile 2>/dev/null)
  385. trap "rm -f $data" 0 1 2 5 15
  386. dialog --title "Renew a Let's Encrypt certificate" \
  387. --backtitle "Freedombone Security Settings" \
  388. --inputbox "Enter the domain name" 8 60 2>$data
  389. sel=$?
  390. case $sel in
  391. 0)
  392. renew_domain=$(<$data)
  393. ;;
  394. esac
  395. if [ ! $renew_domain ]; then
  396. return
  397. fi
  398. if [[ $renew_domain == "http"* ]]; then
  399. dialog --title "Renew a Let's Encrypt certificate" \
  400. --msgbox "Don't include the https://" 6 40
  401. return
  402. fi
  403. if [ ! -f /etc/ssl/certs/${renew_domain}.dhparam ]; then
  404. dialog --title "Renew a Let's Encrypt certificate" \
  405. --msgbox "An existing certificate for $renew_domain was not found" 6 40
  406. return
  407. fi
  408. if [[ $renew_domain != *"."* ]]; then
  409. dialog --title "Renew a Let's Encrypt certificate" \
  410. --msgbox "Invalid domain name: $renew_domain" 6 40
  411. return
  412. fi
  413. freedombone-renew-cert -h $renew_domain -p 'letsencrypt'
  414. exit 0
  415. }
  416. function housekeeping {
  417. cmd=(dialog --separate-output \
  418. --backtitle "Freedombone Security Configuration" \
  419. --title "Housekeeping options" \
  420. --checklist "If you don't need to do any of these things then just press Enter:" 10 76 16)
  421. options=(1 "Regenerate ssh host keys" off
  422. 2 "Regenerate Diffie-Hellman keys" off
  423. 3 "Renew a StartSSL certificate" off)
  424. 4 "Renew Let's Encrypt certificate" off)
  425. choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
  426. clear
  427. for choice in $choices
  428. do
  429. case $choice in
  430. 1)
  431. REGENERATE_SSH_HOST_KEYS="yes"
  432. ;;
  433. 2)
  434. REGENERATE_DH_KEYS="yes"
  435. ;;
  436. 3)
  437. renew_startssl
  438. ;;
  439. 4)
  440. renew_letsencrypt
  441. ;;
  442. esac
  443. done
  444. }
  445. function import_settings {
  446. cd $CURRENT_DIR
  447. if [ ! $IMPORT_FILE ]; then
  448. return
  449. fi
  450. if [ ! -f $IMPORT_FILE ]; then
  451. echo "Import file $IMPORT_FILE not found"
  452. exit 6393
  453. fi
  454. if grep -q "SSL_PROTOCOLS" $IMPORT_FILE; then
  455. TEMP_VALUE=$(grep "SSL_PROTOCOLS" $IMPORT_FILE | awk -F '=' '{print $2}')
  456. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  457. SSL_PROTOCOLS=$TEMP_VALUE
  458. fi
  459. fi
  460. if grep -q "SSL_CIPHERS" $IMPORT_FILE; then
  461. TEMP_VALUE=$(grep "SSL_CIPHERS" $IMPORT_FILE | awk -F '=' '{print $2}')
  462. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  463. SSL_CIPHERS=$TEMP_VALUE
  464. fi
  465. fi
  466. if grep -q "SSH_CIPHERS" $IMPORT_FILE; then
  467. TEMP_VALUE=$(grep "SSH_CIPHERS" $IMPORT_FILE | awk -F '=' '{print $2}')
  468. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  469. SSH_CIPHERS=$TEMP_VALUE
  470. fi
  471. fi
  472. if grep -q "SSH_MACS" $IMPORT_FILE; then
  473. TEMP_VALUE=$(grep "SSH_MACS" $IMPORT_FILE | awk -F '=' '{print $2}')
  474. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  475. SSH_MACS=$TEMP_VALUE
  476. fi
  477. fi
  478. if grep -q "SSH_KEX" $IMPORT_FILE; then
  479. TEMP_VALUE=$(grep "SSH_KEX" $IMPORT_FILE | awk -F '=' '{print $2}')
  480. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  481. SSH_KEX=$TEMP_VALUE
  482. fi
  483. fi
  484. if grep -q "SSH_HOST_KEY_ALGORITHMS" $IMPORT_FILE; then
  485. TEMP_VALUE=$(grep "SSH_HOST_KEY_ALGORITHMS" $IMPORT_FILE | awk -F '=' '{print $2}')
  486. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  487. SSH_HOST_KEY_ALGORITHMS=$TEMP_VALUE
  488. fi
  489. fi
  490. if grep -q "SSH_PASSWORDS" $IMPORT_FILE; then
  491. TEMP_VALUE=$(grep "SSH_PASSWORDS" $IMPORT_FILE | awk -F '=' '{print $2}')
  492. if [[ $TEMP_VALUE == "yes" || $TEMP_VALUE == "no" ]]; then
  493. SSH_PASSWORDS=$TEMP_VALUE
  494. fi
  495. fi
  496. if grep -q "XMPP_CIPHERS" $IMPORT_FILE; then
  497. TEMP_VALUE=$(grep "XMPP_CIPHERS" $IMPORT_FILE | awk -F '=' '{print $2}')
  498. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  499. XMPP_CIPHERS=$TEMP_VALUE
  500. fi
  501. fi
  502. if grep -q "XMPP_ECC_CURVE" $IMPORT_FILE; then
  503. TEMP_VALUE=$(grep "XMPP_ECC_CURVE" $IMPORT_FILE | awk -F '=' '{print $2}')
  504. if [ ${#TEMP_VALUE} -gt 3 ]; then
  505. XMPP_ECC_CURVE=$TEMP_VALUE
  506. fi
  507. fi
  508. }
  509. function export_settings {
  510. if [ ! $EXPORT_FILE ]; then
  511. return
  512. fi
  513. cd $CURRENT_DIR
  514. if [ ! -f $EXPORT_FILE ]; then
  515. if [ "$SSL_PROTOCOLS" ]; then
  516. echo "SSL_PROTOCOLS=$SSL_PROTOCOLS" >> $EXPORT_FILE
  517. fi
  518. if [ $SSL_CIPHERS ]; then
  519. echo "SSL_CIPHERS=$SSL_CIPHERS" >> $EXPORT_FILE
  520. fi
  521. if [ $SSH_CIPHERS ]; then
  522. echo "SSH_CIPHERS=$SSH_CIPHERS" >> $EXPORT_FILE
  523. fi
  524. if [ $SSH_MACS ]; then
  525. echo "SSH_MACS=$SSH_MACS" >> $EXPORT_FILE
  526. fi
  527. if [ $SSH_KEX ]; then
  528. echo "SSH_KEX=$SSH_KEX" >> $EXPORT_FILE
  529. fi
  530. if [ $SSH_HOST_KEY_ALGORITHMS ]; then
  531. echo "SSH_HOST_KEY_ALGORITHMS=$SSH_HOST_KEY_ALGORITHMS" >> $EXPORT_FILE
  532. fi
  533. if [ $SSH_PASSWORDS ]; then
  534. echo "SSH_PASSWORDS=$SSH_PASSWORDS" >> $EXPORT_FILE
  535. fi
  536. if [ $XMPP_CIPHERS ]; then
  537. echo "XMPP_CIPHERS=$XMPP_CIPHERS" >> $EXPORT_FILE
  538. fi
  539. if [ $XMPP_ECC_CURVE ]; then
  540. echo "XMPP_ECC_CURVE=$XMPP_ECC_CURVE" >> $EXPORT_FILE
  541. fi
  542. echo "Security settings exported to $EXPORT_FILE"
  543. exit 0
  544. fi
  545. if [ "$SSL_PROTOCOLS" ]; then
  546. if grep -q "SSL_PROTOCOLS" $EXPORT_FILE; then
  547. sed -i "s|SSL_PROTOCOLS=.*|SSL_PROTOCOLS=$SSL_PROTOCOLS|g" $EXPORT_FILE
  548. else
  549. echo "SSL_PROTOCOLS=$SSL_PROTOCOLS" >> $EXPORT_FILE
  550. fi
  551. fi
  552. if [ $SSL_CIPHERS ]; then
  553. if grep -q "SSL_CIPHERS" $EXPORT_FILE; then
  554. sed -i "s|SSL_CIPHERS=.*|SSL_CIPHERS=$SSL_CIPHERS|g" $EXPORT_FILE
  555. else
  556. echo "SSL_CIPHERS=$SSL_CIPHERS" >> $EXPORT_FILE
  557. fi
  558. fi
  559. if [ $SSH_CIPHERS ]; then
  560. if grep -q "SSH_CIPHERS" $EXPORT_FILE; then
  561. sed -i "s|SSH_CIPHERS=.*|SSH_CIPHERS=$SSH_CIPHERS|g" $EXPORT_FILE
  562. else
  563. echo "SSH_CIPHERS=$SSH_CIPHERS" >> $EXPORT_FILE
  564. fi
  565. fi
  566. if [ $SSH_MACS ]; then
  567. if grep -q "SSH_MACS" $EXPORT_FILE; then
  568. sed -i "s|SSH_MACS=.*|SSH_MACS=$SSH_MACS|g" $EXPORT_FILE
  569. else
  570. echo "SSH_MACS=$SSH_MACS" >> $EXPORT_FILE
  571. fi
  572. fi
  573. if [ $SSH_KEX ]; then
  574. if grep -q "SSH_KEX" $EXPORT_FILE; then
  575. sed -i "s|SSH_KEX=.*|SSH_KEX=$SSH_KEX|g" $EXPORT_FILE
  576. else
  577. echo "SSH_KEX=$SSH_KEX" >> $EXPORT_FILE
  578. fi
  579. fi
  580. if [ $SSH_HOST_KEY_ALGORITHMS ]; then
  581. if grep -q "SSH_HOST_KEY_ALGORITHMS" $EXPORT_FILE; then
  582. sed -i "s|SSH_HOST_KEY_ALGORITHMS=.*|SSH_HOST_KEY_ALGORITHMS=$SSH_HOST_KEY_ALGORITHMS|g" $EXPORT_FILE
  583. else
  584. echo "SSH_HOST_KEY_ALGORITHMS=$SSH_HOST_KEY_ALGORITHMS" >> $EXPORT_FILE
  585. fi
  586. fi
  587. if [ $SSH_PASSWORDS ]; then
  588. if grep -q "SSH_PASSWORDS" $EXPORT_FILE; then
  589. sed -i "s|SSH_PASSWORDS=.*|SSH_PASSWORDS=$SSH_PASSWORDS|g" $EXPORT_FILE
  590. else
  591. echo "SSH_PASSWORDS=$SSH_PASSWORDS" >> $EXPORT_FILE
  592. fi
  593. fi
  594. if [ $XMPP_CIPHERS ]; then
  595. if grep -q "XMPP_CIPHERS" $EXPORT_FILE; then
  596. sed -i "s|XMPP_CIPHERS=.*|XMPP_CIPHERS=$XMPP_CIPHERS|g" $EXPORT_FILE
  597. else
  598. echo "XMPP_CIPHERS=$XMPP_CIPHERS" >> $EXPORT_FILE
  599. fi
  600. fi
  601. if [ $XMPP_ECC_CURVE ]; then
  602. if grep -q "XMPP_ECC_CURVE" $EXPORT_FILE; then
  603. sed -i "s|XMPP_ECC_CURVE=.*|XMPP_ECC_CURVE=$XMPP_ECC_CURVE|g" $EXPORT_FILE
  604. else
  605. echo "XMPP_ECC_CURVE=$XMPP_ECC_CURVE" >> $EXPORT_FILE
  606. fi
  607. fi
  608. echo "Security settings exported to $EXPORT_FILE"
  609. exit 0
  610. }
  611. function show_help {
  612. echo ''
  613. echo 'freedombone-sec'
  614. echo ''
  615. echo 'Alters the security settings'
  616. echo ''
  617. echo ''
  618. echo ' -h --help Show help'
  619. echo ' -e --export Export security settings to a file'
  620. echo ' -i --import Import security settings from a file'
  621. echo ''
  622. exit 0
  623. }
  624. # Get the commandline options
  625. while [[ $# > 1 ]]
  626. do
  627. key="$1"
  628. case $key in
  629. -h|--help)
  630. show_help
  631. ;;
  632. # Export settings
  633. -e|--export)
  634. shift
  635. EXPORT_FILE="$1"
  636. ;;
  637. # Export settings
  638. -i|--import)
  639. shift
  640. IMPORT_FILE="$1"
  641. ;;
  642. *)
  643. # unknown option
  644. ;;
  645. esac
  646. shift
  647. done
  648. housekeeping
  649. get_website_settings
  650. get_imap_settings
  651. get_ssh_settings
  652. get_xmpp_settings
  653. import_settings
  654. export_settings
  655. interactive_setup
  656. change_website_settings
  657. change_imap_settings
  658. change_ssh_settings
  659. change_xmpp_settings
  660. regenerate_ssh_host_keys
  661. regenerate_dh_keys
  662. exit 0