freedombone-sec 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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-2016 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero 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 Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. PROJECT_NAME='freedombone'
  31. export TEXTDOMAIN=${PROJECT_NAME}-sec
  32. export TEXTDOMAINDIR="/usr/share/locale"
  33. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  34. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  35. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  36. for f in $UTILS_FILES
  37. do
  38. source $f
  39. done
  40. SSL_PROTOCOLS=
  41. SSL_CIPHERS=
  42. SSH_CIPHERS=
  43. SSH_MACS=
  44. SSH_KEX=
  45. SSH_HOST_KEY_ALGORITHMS=
  46. SSH_PASSWORDS=
  47. XMPP_CIPHERS=
  48. XMPP_ECC_CURVE=
  49. WEBSITES_DIRECTORY='/etc/nginx/sites-available'
  50. DOVECOT_CIPHERS='/etc/dovecot/conf.d/10-ssl.conf'
  51. SSH_CONFIG='/etc/ssh/sshd_config'
  52. XMPP_CONFIG='/etc/prosody/conf.avail/xmpp.cfg.lua'
  53. MINIMUM_LENGTH=6
  54. IMPORT_FILE=
  55. EXPORT_FILE=
  56. CURRENT_DIR=$(pwd)
  57. DH_KEYLENGTH=2048
  58. LETSENCRYPT_SERVER='https://acme-v01.api.letsencrypt.org/directory'
  59. MY_USERNAME=
  60. function export_passwords {
  61. detect_usb_drive
  62. data=$(tempfile 2>/dev/null)
  63. trap "rm -f $data" 0 1 2 5 15
  64. dialog --title $"Export passwords to USB drive $USB_DRIVE" \
  65. --backtitle $"Security Settings" \
  66. --defaultno \
  67. --yesno $"\nPlease confirm that you wish to export passwords to a LUKS formatted USB drive. The drive should be plugged in." 10 60
  68. sel=$?
  69. case $sel in
  70. 1) return;;
  71. 255) return;;
  72. esac
  73. data=$(tempfile 2>/dev/null)
  74. trap "rm -f $data" 0 1 2 5 15
  75. dialog --title $"Export passwords to USB drive $USB_DRIVE" \
  76. --backtitle $"Security Settings" \
  77. --defaultno \
  78. --yesno $"\nDo you need to format the drive as LUKS encrypted?" 12 60
  79. sel=$?
  80. case $sel in
  81. 0) ${PROJECT_NAME}-format $USB_DRIVE;;
  82. esac
  83. clear
  84. backup_mount_drive ${USB_DRIVE}
  85. ${PROJECT_NAME}-pass --export ${USB_MOUNT}/${PROJECT_NAME}-passwords.xml
  86. backup_unmount_drive ${USB_DRIVE}
  87. }
  88. function get_protocols_from_website {
  89. if [ ! -f $WEBSITES_DIRECTORY/$1 ]; then
  90. return
  91. fi
  92. SSL_PROTOCOLS=$(cat $WEBSITES_DIRECTORY/$1 | grep 'ssl_protocols ' | awk -F "ssl_protocols " '{print $2}' | awk -F ';' '{print $1}')
  93. }
  94. function get_ciphers_from_website {
  95. if [ ! -f $WEBSITES_DIRECTORY/$1 ]; then
  96. return
  97. fi
  98. SSL_CIPHERS=$(cat $WEBSITES_DIRECTORY/$1 | grep 'ssl_ciphers ' | awk -F "ssl_ciphers " '{print $2}' | awk -F "'" '{print $2}')
  99. }
  100. function get_imap_settings {
  101. if [ ! -f $DOVECOT_CIPHERS ]; then
  102. return
  103. fi
  104. # clear commented out cipher list
  105. sed -i "s|#ssl_cipher_list.*||g" $DOVECOT_CIPHERS
  106. if [ $SSL_CIPHERS ]; then
  107. return
  108. fi
  109. if [ ${#SSL_CIPHERS} -gt $MINIMUM_LENGTH ]; then
  110. return
  111. fi
  112. SSL_CIPHERS=$(cat $DOVECOT_CIPHERS | grep 'ssl_cipher_list' | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')
  113. }
  114. function get_xmpp_settings {
  115. if [ ! -f $XMPP_CONFIG ]; then
  116. return
  117. fi
  118. XMPP_CIPHERS=$(cat $XMPP_CONFIG | grep 'ciphers ' | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  119. XMPP_ECC_CURVE=$(cat $XMPP_CONFIG | grep 'curve ' | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  120. }
  121. function get_ssh_settings {
  122. if [ -f $SSH_CONFIG ]; then
  123. SSH_PASSWORDS=$(cat $SSH_CONFIG | grep 'PasswordAuthentication ' | awk -F 'PasswordAuthentication ' '{print $2}')
  124. fi
  125. if [ -f /etc/ssh/ssh_config ]; then
  126. SSH_HOST_KEY_ALGORITHMS=$(cat /etc/ssh/ssh_config | grep 'HostKeyAlgorithms ' | awk -F 'HostKeyAlgorithms ' '{print $2}')
  127. fi
  128. }
  129. function change_website_settings {
  130. if [ ! "$SSL_PROTOCOLS" ]; then
  131. return
  132. fi
  133. if [ ! $SSL_CIPHERS ]; then
  134. return
  135. fi
  136. if [ ${#SSL_PROTOCOLS} -lt $MINIMUM_LENGTH ]; then
  137. return
  138. fi
  139. if [ ${#SSL_CIPHERS} -lt $MINIMUM_LENGTH ]; then
  140. return
  141. fi
  142. if [ ! -d $WEBSITES_DIRECTORY ]; then
  143. return
  144. fi
  145. cd $WEBSITES_DIRECTORY
  146. for file in `dir -d *` ; do
  147. sed -i "s|ssl_protocols .*|ssl_protocols $SSL_PROTOCOLS;|g" $WEBSITES_DIRECTORY/$file
  148. sed -i "s|ssl_ciphers .*|ssl_ciphers '$SSL_CIPHERS';|g" $WEBSITES_DIRECTORY/$file
  149. done
  150. systemctl restart nginx
  151. echo $'Web security settings changed'
  152. }
  153. function change_imap_settings {
  154. if [ ! -f $DOVECOT_CIPHERS ]; then
  155. return
  156. fi
  157. if [ ! $SSL_CIPHERS ]; then
  158. return
  159. fi
  160. if [ ${#SSL_CIPHERS} -lt $MINIMUM_LENGTH ]; then
  161. return
  162. fi
  163. sed -i "s|ssl_cipher_list.*|ssl_cipher_list = '$SSL_CIPHERS'|g" $DOVECOT_CIPHERS
  164. sed -i "s|ssl_protocols.*|ssl_protocols = '$SSL_PROTOCOLS'|g" $DOVECOT_CIPHERS
  165. systemctl restart dovecot
  166. echo $'imap security settings changed'
  167. }
  168. function change_ssh_settings {
  169. if [ -f /etc/ssh/ssh_config ]; then
  170. if [ $SSH_HOST_KEY_ALGORITHMS ]; then
  171. sed -i "s|HostKeyAlgorithms .*|HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS|g" /etc/ssh/ssh_config
  172. echo $'ssh client security settings changed'
  173. fi
  174. fi
  175. if [ -f $SSH_CONFIG ]; then
  176. if [ ! $SSH_CIPHERS ]; then
  177. return
  178. fi
  179. if [ ! $SSH_MACS ]; then
  180. return
  181. fi
  182. if [ ! $SSH_KEX ]; then
  183. return
  184. fi
  185. if [ ! $SSH_PASSWORDS ]; then
  186. SSH_PASSWORDS='yes'
  187. fi
  188. sed -i "s|Ciphers .*|Ciphers $SSH_CIPHERS|g" $SSH_CONFIG
  189. sed -i "s|MACs .*|MACs $SSH_MACS|g" $SSH_CONFIG
  190. sed -i "s|KexAlgorithms .*|KexAlgorithms $SSH_KEX|g" $SSH_CONFIG
  191. sed -i "s|#PasswordAuthentication .*|PasswordAuthentication $SSH_PASSWORDS|g" $SSH_CONFIG
  192. sed -i "s|PasswordAuthentication .*|PasswordAuthentication $SSH_PASSWORDS|g" $SSH_CONFIG
  193. systemctl restart ssh
  194. echo $'ssh server security settings changed'
  195. fi
  196. }
  197. function change_xmpp_settings {
  198. if [ ! -f $XMPP_CONFIG ]; then
  199. return
  200. fi
  201. if [ ! $XMPP_CIPHERS ]; then
  202. return
  203. fi
  204. if [ ! $XMPP_ECC_CURVE ]; then
  205. return
  206. fi
  207. sed -i "s|ciphers =.*|ciphers = \"$XMPP_CIPHERS\";|g" $XMPP_CONFIG
  208. sed -i "s|curve =.*|curve = \"$XMPP_ECC_CURVE\";|g" $XMPP_CONFIG
  209. systemctl restart prosody
  210. echo $'xmpp security settings changed'
  211. }
  212. function allow_ssh_passwords {
  213. dialog --title $"SSH Passwords" \
  214. --backtitle $"Freedombone Security Configuration" \
  215. --yesno $"\nAllow SSH login using passwords?" 7 60
  216. sel=$?
  217. case $sel in
  218. 0) SSH_PASSWORDS="yes";;
  219. 1) SSH_PASSWORDS="no";;
  220. 255) exit 0;;
  221. esac
  222. }
  223. function interactive_setup {
  224. if [ $SSL_CIPHERS ]; then
  225. data=$(tempfile 2>/dev/null)
  226. trap "rm -f $data" 0 1 2 5 15
  227. dialog --backtitle $"Freedombone Security Configuration" \
  228. --form $"\nWeb/IMAP Ciphers:" 10 95 2 \
  229. $"Protocols:" 1 1 "$SSL_PROTOCOLS" 1 15 90 90 \
  230. $"Ciphers:" 2 1 "$SSL_CIPHERS" 2 15 90 512 \
  231. 2> $data
  232. sel=$?
  233. case $sel in
  234. 1) SSL_PROTOCOLS=$(cat $data | sed -n 1p)
  235. SSL_CIPHERS=$(cat $data | sed -n 2p)
  236. ;;
  237. 255) exit 0;;
  238. esac
  239. fi
  240. data=$(tempfile 2>/dev/null)
  241. trap "rm -f $data" 0 1 2 5 15
  242. if [ $SSH_HOST_KEY_ALGORITHMS ]; then
  243. dialog --backtitle $"Freedombone Security Configuration" \
  244. --form $"\nSecure Shell Ciphers:" 13 95 4 \
  245. $"Ciphers:" 1 1 "$SSH_CIPHERS" 1 15 90 512 \
  246. $"MACs:" 2 1 "$SSH_MACS" 2 15 90 512 \
  247. $"KEX:" 3 1 "$SSH_KEX" 3 15 90 512 \
  248. $"Host key algorithms:" 4 1 "$SSH_HOST_KEY_ALGORITHMS" 4 15 90 512 \
  249. 2> $data
  250. sel=$?
  251. case $sel in
  252. 1) SSH_CIPHERS=$(cat $data | sed -n 1p)
  253. SSH_MACS=$(cat $data | sed -n 2p)
  254. SSH_KEX=$(cat $data | sed -n 3p)
  255. SSH_HOST_KEY_ALGORITHMS=$(cat $data | sed -n 4p)
  256. ;;
  257. 255) exit 0;;
  258. esac
  259. else
  260. dialog --backtitle $"Freedombone Security Configuration" \
  261. --form $"\nSecure Shell Ciphers:" 11 95 3 \
  262. $"Ciphers:" 1 1 "$SSH_CIPHERS" 1 15 90 512 \
  263. $"MACs:" 2 1 "$SSH_MACS" 2 15 90 512 \
  264. $"KEX:" 3 1 "$SSH_KEX" 3 15 90 512 \
  265. 2> $data
  266. sel=$?
  267. case $sel in
  268. 1) SSH_CIPHERS=$(cat $data | sed -n 1p)
  269. SSH_MACS=$(cat $data | sed -n 2p)
  270. SSH_KEX=$(cat $data | sed -n 3p)
  271. ;;
  272. 255) exit 0;;
  273. esac
  274. fi
  275. if [ $XMPP_CIPHERS ]; then
  276. data=$(tempfile 2>/dev/null)
  277. trap "rm -f $data" 0 1 2 5 15
  278. dialog --backtitle $"Freedombone Security Configuration" \
  279. --form $"\nXMPP Ciphers:" 10 95 2 \
  280. $"Ciphers:" 1 1 "$XMPP_CIPHERS" 1 15 90 512 \
  281. $"ECC Curve:" 2 1 "$XMPP_ECC_CURVE" 2 15 50 50 \
  282. 2> $data
  283. sel=$?
  284. case $sel in
  285. 1) XMPP_CIPHERS=$(cat $data | sed -n 1p)
  286. XMPP_ECC_CURVE=$(cat $data | sed -n 2p)
  287. ;;
  288. 255) exit 0;;
  289. esac
  290. fi
  291. dialog --title $"Final Confirmation" \
  292. --backtitle $"Freedombone Security Configuration" \
  293. --defaultno \
  294. --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
  295. sel=$?
  296. case $sel in
  297. 1) clear
  298. echo $'Exiting without changing security settings'
  299. exit 0;;
  300. 255) clear
  301. echo $'Exiting without changing security settings'
  302. exit 0;;
  303. esac
  304. clear
  305. }
  306. function send_monkeysphere_server_keys_to_users {
  307. monkeysphere_server_keys=$(monkeysphere-host show-key | grep $"OpenPGP fingerprint" | awk -F ' ' '{print $3}')
  308. for d in /home/*/ ; do
  309. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  310. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  311. if [ ! -d /home/$USERNAME/.monkeysphere ]; then
  312. mkdir /home/$USERNAME/.monkeysphere
  313. fi
  314. echo $monkeysphere_server_keys > /home/$USERNAME/.monkeysphere/server_keys
  315. chown -R $USERNAME:$USERNAME /home/$USERNAME/.monkeysphere
  316. fi
  317. done
  318. }
  319. function regenerate_ssh_host_keys {
  320. rm -f /etc/ssh/ssh_host_*
  321. dpkg-reconfigure openssh-server
  322. echo $'ssh host keys regenerated'
  323. # remove small moduli
  324. awk '$5 > 2000' /etc/ssh/moduli > ~/moduli
  325. mv ~/moduli /etc/ssh/moduli
  326. echo $'ssh small moduli removed'
  327. # update monkeysphere
  328. DEFAULT_DOMAIN_NAME=
  329. read_config_param "DEFAULT_DOMAIN_NAME"
  330. monkeysphere-host import-key /etc/ssh/ssh_host_rsa_key ssh://$DEFAULT_DOMAIN_NAME
  331. SSH_ONION_HOSTNAME=$(cat ${COMPLETION_FILE} | grep 'ssh onion domain' | awk -F ':' '{print $2}')
  332. monkeysphere-host import-key /etc/ssh/ssh_host_rsa_key ssh://$SSH_ONION_HOSTNAME
  333. monkeysphere-host publish-key
  334. send_monkeysphere_server_keys_to_users
  335. echo $'updated monkeysphere ssh host key'
  336. systemctl restart ssh
  337. }
  338. function regenerate_dh_keys {
  339. if [ ! -d /etc/ssl/mycerts ]; then
  340. echo $'No dhparam certificates were found'
  341. return
  342. fi
  343. data=$(tempfile 2>/dev/null)
  344. trap "rm -f $data" 0 1 2 5 15
  345. dialog --backtitle "Freedombone Security Configuration" \
  346. --title "Diffie-Hellman key length" \
  347. --radiolist "The smaller length is better suited to low power embedded systems:" 12 40 3 \
  348. 1 "2048 bits" off \
  349. 2 "3072 bits" on \
  350. 3 "4096 bits" off 2> $data
  351. sel=$?
  352. case $sel in
  353. 1) exit 1;;
  354. 255) exit 1;;
  355. esac
  356. case $(cat $data) in
  357. 1) DH_KEYLENGTH=2048;;
  358. 2) DH_KEYLENGTH=3072;;
  359. 3) DH_KEYLENGTH=4096;;
  360. esac
  361. ${PROJECT_NAME}-dhparam --recalc yes -l ${DH_KEYLENGTH}
  362. }
  363. function renew_startssl {
  364. renew_domain=
  365. data=$(tempfile 2>/dev/null)
  366. trap "rm -f $data" 0 1 2 5 15
  367. dialog --title $"Renew a StartSSL certificate" \
  368. --backtitle $"Freedombone Security Settings" \
  369. --inputbox $"Enter the domain name" 8 60 2>$data
  370. sel=$?
  371. case $sel in
  372. 0)
  373. renew_domain=$(<$data)
  374. ;;
  375. esac
  376. if [ ! $renew_domain ]; then
  377. return
  378. fi
  379. if [[ $renew_domain == "http"* ]]; then
  380. dialog --title $"Renew a StartSSL certificate" \
  381. --msgbox $"Don't include the https://" 6 40
  382. return
  383. fi
  384. if [ ! -f /etc/ssl/certs/${renew_domain}.dhparam ]; then
  385. dialog --title $"Renew a StartSSL certificate" \
  386. --msgbox $"An existing certificate for $renew_domain was not found" 6 40
  387. return
  388. fi
  389. if [[ $renew_domain != *"."* ]]; then
  390. dialog --title $"Renew a StartSSL certificate" \
  391. --msgbox $"Invalid domain name: $renew_domain" 6 40
  392. return
  393. fi
  394. ${PROJECT_NAME}-renew-cert -h $renew_domain -p startssl
  395. exit 0
  396. }
  397. function renew_letsencrypt {
  398. renew_domain=
  399. data=$(tempfile 2>/dev/null)
  400. trap "rm -f $data" 0 1 2 5 15
  401. dialog --title $"Renew a Let's Encrypt certificate" \
  402. --backtitle $"Freedombone Security Settings" \
  403. --inputbox $"Enter the domain name" 8 60 2>$data
  404. sel=$?
  405. case $sel in
  406. 0)
  407. renew_domain=$(<$data)
  408. ;;
  409. esac
  410. if [ ! $renew_domain ]; then
  411. return
  412. fi
  413. if [[ $renew_domain == "http"* ]]; then
  414. dialog --title $"Renew a Let's Encrypt certificate" \
  415. --msgbox $"Don't include the https://" 6 40
  416. return
  417. fi
  418. if [ ! -f /etc/ssl/certs/${renew_domain}.dhparam ]; then
  419. dialog --title $"Renew a Let's Encrypt certificate" \
  420. --msgbox $"An existing certificate for $renew_domain was not found" 6 40
  421. return
  422. fi
  423. if [[ $renew_domain != *"."* ]]; then
  424. dialog --title $"Renew a Let's Encrypt certificate" \
  425. --msgbox $"Invalid domain name: $renew_domain" 6 40
  426. return
  427. fi
  428. ${PROJECT_NAME}-renew-cert -h $renew_domain -p 'letsencrypt'
  429. exit 0
  430. }
  431. function create_letsencrypt {
  432. new_domain=
  433. data=$(tempfile 2>/dev/null)
  434. trap "rm -f $data" 0 1 2 5 15
  435. dialog --title $"Create a new Let's Encrypt certificate" \
  436. --backtitle $"Freedombone Security Settings" \
  437. --inputbox $"Enter the domain name" 8 60 2>$data
  438. sel=$?
  439. case $sel in
  440. 0)
  441. new_domain=$(<$data)
  442. ;;
  443. esac
  444. if [ ! $new_domain ]; then
  445. return
  446. fi
  447. if [[ $new_domain == "http"* ]]; then
  448. dialog --title $"Create a new Let's Encrypt certificate" \
  449. --msgbox $"Don't include the https://" 6 40
  450. return
  451. fi
  452. if [[ $new_domain != *"."* ]]; then
  453. dialog --title $"Create a new Let's Encrypt certificate" \
  454. --msgbox $"Invalid domain name: $new_domain" 6 40
  455. return
  456. fi
  457. if [ ! -d /var/www/${new_domain} ]; then
  458. domain_found=
  459. if [ -f /etc/nginx/sites-available/radicale ]; then
  460. if grep "${new_domain}" /etc/nginx/sites-available/radicale; then
  461. domain_found=1
  462. fi
  463. fi
  464. if [ -f /etc/nginx/sites-available/${new_domain} ]; then
  465. domain_found=1
  466. fi
  467. if [[ "${new_domain}" == "jitsi"* || "${new_domain}" == "meet"* ]]; then
  468. domain_found=1
  469. fi
  470. if [ ! $domain_found ]; then
  471. dialog --title $"Create a new Let's Encrypt certificate" \
  472. --msgbox $'Domain not found within /var/www' 6 40
  473. return
  474. fi
  475. fi
  476. ${PROJECT_NAME}-addcert -e $new_domain -s $LETSENCRYPT_SERVER --dhkey $DH_KEYLENGTH
  477. exit 0
  478. }
  479. function update_ciphersuite {
  480. RECOMMENDED_SSL_CIPHERS="$SSL_CIPHERS"
  481. if [ ${#RECOMMENDED_SSL_CIPHERS} -lt 5 ]; then
  482. return
  483. fi
  484. RECOMMENDED_SSL_PROTOCOLS="$SSL_PROTOCOLS"
  485. if [ ${#RECOMMENDED_SSL_PROTOCOLS} -lt 5 ]; then
  486. return
  487. fi
  488. RECOMMENDED_SSH_CIPHERS="$SSH_CIPHERS"
  489. if [ ${#RECOMMENDED_SSH_CIPHERS} -lt 5 ]; then
  490. return
  491. fi
  492. RECOMMENDED_SSH_MACS="$SSH_MACS"
  493. if [ ${#RECOMMENDED_SSH_MACS} -lt 5 ]; then
  494. return
  495. fi
  496. RECOMMENDED_SSH_KEX="$SSH_KEX"
  497. if [ ${#RECOMMENDED_SSH_KEX} -lt 5 ]; then
  498. return
  499. fi
  500. cd $WEBSITES_DIRECTORY
  501. for file in `dir -d *` ; do
  502. sed -i "s|ssl_protocols .*|ssl_protocols $RECOMMENDED_SSL_PROTOCOLS;|g" $WEBSITES_DIRECTORY/$file
  503. sed -i "s|ssl_ciphers .*|ssl_ciphers '$RECOMMENDED_SSL_CIPHERS';|g" $WEBSITES_DIRECTORY/$file
  504. done
  505. systemctl restart nginx
  506. write_config_param "SSL_PROTOCOLS" "$RECOMMENDED_SSL_PROTOCOLS"
  507. write_config_param "SSL_CIPHERS" "$RECOMMENDED_SSL_CIPHERS"
  508. sed -i "s|Ciphers .*|Ciphers $RECOMMENDED_SSH_CIPHERS|g" $SSH_CONFIG
  509. sed -i "s|MACs .*|MACs $RECOMMENDED_SSH_MACS|g" $SSH_CONFIG
  510. sed -i "s|KexAlgorithms .*|KexAlgorithms $RECOMMENDED_SSH_KEX|g" $SSH_CONFIG
  511. systemctl restart ssh
  512. write_config_param "SSH_CIPHERS" "$RECOMMENDED_SSH_CIPHERS"
  513. write_config_param "SSH_MACS" "$RECOMMENDED_SSH_MACS"
  514. write_config_param "SSH_KEX" "$RECOMMENDED_SSH_KEX"
  515. dialog --title $"Update ciphersuite" \
  516. --msgbox $"The ciphersuite has been updated to recommended versions" 6 40
  517. exit 0
  518. }
  519. function gpg_pubkey_from_email {
  520. key_owner_username=$1
  521. key_email_address=$2
  522. key_id=
  523. if [[ $key_owner_username != "root" ]]; then
  524. key_id=$(su -c "gpg --list-keys $key_email_address | grep 'pub '" - $key_owner_username | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  525. else
  526. key_id=$(gpg --list-keys $key_email_address | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  527. fi
  528. echo $key_id
  529. }
  530. function enable_monkeysphere {
  531. monkey=
  532. dialog --title $"GPG based authentication" \
  533. --backtitle $"Freedombone Security Configuration" \
  534. --defaultno \
  535. --yesno $"\nEnable GPG based authentication with monkeysphere ?" 7 60
  536. sel=$?
  537. case $sel in
  538. 0) monkey='yes';;
  539. 255) exit 0;;
  540. esac
  541. if [ $monkey ]; then
  542. read_config_param "MY_USERNAME"
  543. if [ ! -f /home/$MY_USERNAME/.monkeysphere/authorized_user_ids ]; then
  544. dialog --title $"GPG based authentication" \
  545. --msgbox $"$MY_USERNAME does not currently have any ids within ~/.monkeysphere/authorized_user_ids" 6 40
  546. exit 0
  547. fi
  548. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$MY_USERNAME" "$MY_USERNAME@$HOSTNAME")
  549. if [ ${#MY_GPG_PUBLIC_KEY_ID} -lt 4 ]; then
  550. echo $'monkeysphere unable to get GPG key ID for user $MY_USERNAME'
  551. exit 52825
  552. fi
  553. sed -i 's|#AuthorizedKeysFile|AuthorizedKeysFile|g' /etc/ssh/sshd_config
  554. sed -i 's|AuthorizedKeysFile.*|AuthorizedKeysFile /var/lib/monkeysphere/authorized_keys/%u|g' /etc/ssh/sshd_config
  555. monkeysphere-authentication update-users
  556. # The admin user is the identity certifier
  557. fpr=$(gpg --with-colons --fingerprint $MY_GPG_PUBLIC_KEY_ID | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  558. monkeysphere-authentication add-identity-certifier $fpr
  559. monkeysphere-host publish-key
  560. send_monkeysphere_server_keys_to_users
  561. else
  562. sed -i 's|#AuthorizedKeysFile|AuthorizedKeysFile|g' /etc/ssh/sshd_config
  563. sed -i 's|AuthorizedKeysFile.*|AuthorizedKeysFile %h/.ssh/authorized_keys|g' /etc/ssh/sshd_config
  564. fi
  565. systemctl restart ssh
  566. if [ $monkey ]; then
  567. dialog --title $"GPG based authentication" \
  568. --msgbox $"GPG based authentication was enabled" 6 40
  569. else
  570. dialog --title $"GPG based authentication" \
  571. --msgbox $"GPG based authentication was disabled" 6 40
  572. fi
  573. exit 0
  574. }
  575. function register_website {
  576. domain="$1"
  577. if [[ ${domain} == *".local" ]]; then
  578. echo $"Can't register local domains"
  579. return
  580. fi
  581. if [ ! -f /etc/ssl/private/${domain}.key ]; then
  582. echo $"No SSL/TLS private key found for ${domain}"
  583. return
  584. fi
  585. if [ ! -f /etc/nginx/sites-available/${domain} ]; then
  586. echo $"No virtual host found for ${domain}"
  587. return
  588. fi
  589. monkeysphere-host import-key /etc/ssl/private/${domain}.key https://${domain}
  590. monkeysphere-host publish-key
  591. echo "0"
  592. }
  593. function register_website_interactive {
  594. data=$(tempfile 2>/dev/null)
  595. trap "rm -f $data" 0 1 2 5 15
  596. dialog --title $"Register a website with monkeysphere" \
  597. --backtitle $"Freedombone Security Settings" \
  598. --inputbox $"Enter the website domain name (without https://)" 8 60 2>$data
  599. sel=$?
  600. case $sel in
  601. 0)
  602. domain=$(<$data)
  603. register_website "$domain"
  604. if [ ! "$?" = "0" ]; then
  605. dialog --title $"Register a website with monkeysphere" \
  606. --msgbox "$?" 6 40
  607. else
  608. dialog --title $"Register a website with monkeysphere" \
  609. --msgbox $"$domain has been registered" 6 40
  610. fi
  611. ;;
  612. esac
  613. }
  614. function pin_all_tls_certs {
  615. ${PROJECT_NAME}-pin-cert all
  616. }
  617. function remove_pinning {
  618. data=$(tempfile 2>/dev/null)
  619. trap "rm -f $data" 0 1 2 5 15
  620. dialog --title $"Remove pinning for a domain" \
  621. --backtitle $"Freedombone Security Settings" \
  622. --inputbox $"Enter the website domain name (without https://)" 8 60 2>$data
  623. sel=$?
  624. case $sel in
  625. 0)
  626. domain=$(<$data)
  627. ${PROJECT_NAME}-pin-cert "$domain" remove
  628. if [ ! "$?" = "0" ]; then
  629. dialog --title $"Removed pinning from $domain" \
  630. --msgbox "$?" 6 40
  631. fi
  632. ;;
  633. esac
  634. }
  635. function store_passwords {
  636. dialog --title $"Store Passwords" \
  637. --backtitle $"Freedombone Security Configuration" \
  638. --yesno $"\nDo you wish to store passwords on the system? Stored passwords are convenient but carry some additional security risk." 10 60
  639. sel=$?
  640. case $sel in
  641. 0)
  642. if [ -f /root/.nostore ]; then
  643. read_config_param "MY_USERNAME"
  644. if [ ! -f /home/$MY_USERNAME/.ssh/authorized_keys ]; then
  645. dialog --title $"Store Passwords" \
  646. --msgbox $"\nYou should first enable key based ssh login to improve security" 8 60
  647. return
  648. fi
  649. if [[ $SSH_PASSWORDS == 'yes' ]]; then
  650. dialog --title $"Store Passwords" \
  651. --msgbox $"\nYou should disable ssh passwords to improve security" 8 60
  652. return
  653. fi
  654. ${PROJECT_NAME}-pass --enable yes
  655. dialog --title $"Store Passwords" \
  656. --msgbox $"\nUser passwords will now be stored on the system" 8 60
  657. fi
  658. return
  659. ;;
  660. 1)
  661. ${PROJECT_NAME}-pass --clear yes
  662. dialog --title $"Passwords were removed and will not be stored" \
  663. --msgbox $"\nFor the best security you should now manually change passwords via web interfaces so that there is no possibility of them being recovered from the disk" 9 60
  664. return
  665. ;;
  666. 255) return;;
  667. esac
  668. }
  669. function show_tor_bridges {
  670. clear
  671. bridges_list=$(grep "Bridge " /etc/tor/torrc | grep -v '##')
  672. if [ ${#bridges_list} -eq 0 ]; then
  673. echo $'No Tor bridges have been added'
  674. return
  675. fi
  676. echo "${bridges_list}"
  677. }
  678. function add_tor_bridge {
  679. data=$(tempfile 2>/dev/null)
  680. trap "rm -f $data" 0 1 2 5 15
  681. dialog --backtitle $"Freedombone Control Panel" \
  682. --title $"Add obfs4 Tor bridge" \
  683. --form "\n" 9 60 4 \
  684. $"IP address: " 1 1 " . . . " 1 17 16 16 \
  685. $"Port: " 2 1 "" 2 17 8 8 \
  686. $"Key/Nickname: " 3 1 "" 3 17 250 250 \
  687. 2> $data
  688. sel=$?
  689. case $sel in
  690. 1) return;;
  691. 255) return;;
  692. esac
  693. bridge_ip_address=$(cat $data | sed -n 1p)
  694. bridge_port=$(cat $data | sed -n 2p)
  695. bridge_key=$(cat $data | sed -n 3p)
  696. if [[ "${bridge_ip_address}" == *" "* ]]; then
  697. return
  698. fi
  699. if [[ "${bridge_ip_address}" != *"."* ]]; then
  700. return
  701. fi
  702. if [ ${#bridge_port} -eq 0 ]; then
  703. return
  704. fi
  705. if [ ${#bridge_key} -eq 0 ]; then
  706. return
  707. fi
  708. tor_add_bridge "${bridge_ip_address}" "${bridge_port}" "${bridge_key}"
  709. dialog --title $"Add obfs4 Tor bridge" \
  710. --msgbox $"Bridge added" 6 40
  711. }
  712. function remove_tor_bridge {
  713. bridge_removed=
  714. data=$(tempfile 2>/dev/null)
  715. trap "rm -f $data" 0 1 2 5 15
  716. dialog --title $"Remove obfs4 Tor bridge" \
  717. --backtitle $"Freedombone Control Panel" \
  718. --inputbox $"Enter the IP address, key or Nickname of the bridge" 8 65 2>$data
  719. sel=$?
  720. case $sel in
  721. 0)
  722. response=$(<$data)
  723. if [ ${#response} -gt 2 ]; then
  724. if [[ "${response}" != *" "* ]]; then
  725. if [[ "${response}" == *"."* ]]; then
  726. if grep "Bridge ${response}" /etc/tor/torrc; then
  727. tor_remove_bridge "${response}"
  728. bridge_removed=1
  729. fi
  730. else
  731. if grep " $response" /etc/tor/torrc; then
  732. tor_remove_bridge "${response}"
  733. bridge_removed=1
  734. fi
  735. fi
  736. fi
  737. fi
  738. ;;
  739. esac
  740. if [ $bridge_removed ]; then
  741. dialog --title $"Remove obfs4 Tor bridge" \
  742. --msgbox $"Bridge removed" 6 40
  743. fi
  744. }
  745. function add_tor_bridge_relay {
  746. read_config_param 'TOR_BRIDGE_NICKNAME'
  747. read_config_param 'TOR_BRIDGE_PORT'
  748. # remove any previous bridge port from the firewall
  749. if [ ${#TOR_BRIDGE_PORT} -gt 0 ]; then
  750. firewall_remove $TOR_BRIDGE_PORT tcp
  751. fi
  752. data=$(tempfile 2>/dev/null)
  753. trap "rm -f $data" 0 1 2 5 15
  754. dialog --backtitle $"Freedombone Control Panel" \
  755. --title $"Become an obfs4 Tor bridge relay" \
  756. --form "\n" 8 60 2 \
  757. $"Bridge Nickname: " 1 1 "$TOR_BRIDGE_NICKNAME" 1 20 250 250 \
  758. 2> $data
  759. sel=$?
  760. case $sel in
  761. 1) return;;
  762. 255) return;;
  763. esac
  764. bridge_nickname=$(cat $data | sed -n 1p)
  765. if [[ "${bridge_nickname}" == *" "* ]]; then
  766. return
  767. fi
  768. if [ ${#bridge_nickname} -eq 0 ]; then
  769. return
  770. fi
  771. TOR_BRIDGE_NICKNAME="$bridge_nickname"
  772. TOR_BRIDGE_PORT=$((20000 + RANDOM % 40000))
  773. write_config_param 'TOR_BRIDGE_NICKNAME' "$TOR_BRIDGE_NICKNAME"
  774. write_config_param 'TOR_BRIDGE_PORT' "$TOR_BRIDGE_PORT"
  775. tor_create_bridge_relay
  776. dialog --title $"You are now an obfs4 Tor bridge relay" \
  777. --msgbox $"\nIP address: $(get_ipv4_address)\n\nPort: ${TOR_BRIDGE_PORT}\n\nNickname: ${TOR_BRIDGE_NICKNAME}" 10 65
  778. }
  779. function remove_tor_bridge_relay {
  780. tor_remove_bridge_relay
  781. dialog --title $"Remove Tor bridge relay" \
  782. --msgbox $"Bridge relay removed" 10 60
  783. }
  784. function menu_tor_bridges {
  785. data=$(tempfile 2>/dev/null)
  786. trap "rm -f $data" 0 1 2 5 15
  787. dialog --backtitle $"Freedombone Control Panel" \
  788. --title $"Tor Bridges" \
  789. --radiolist $"Choose an operation:" 14 50 6 \
  790. 1 $"Show bridges" off \
  791. 2 $"Add a bridge" off \
  792. 3 $"Remove a bridge" off \
  793. 4 $"Make this system into a bridge" off \
  794. 5 $"Stop being a bridge" off \
  795. 6 $"Go Back/Exit" on 2> $data
  796. sel=$?
  797. case $sel in
  798. 1) exit 1;;
  799. 255) exit 1;;
  800. esac
  801. case $(cat $data) in
  802. 1)
  803. show_tor_bridges
  804. exit 0
  805. ;;
  806. 2)
  807. add_tor_bridge
  808. exit 0
  809. ;;
  810. 3)
  811. remove_tor_bridge
  812. exit 0
  813. ;;
  814. 4)
  815. add_tor_bridge_relay
  816. exit 0
  817. ;;
  818. 5)
  819. remove_tor_bridge_relay
  820. exit 0
  821. ;;
  822. 6)
  823. exit 0
  824. ;;
  825. esac
  826. }
  827. function menu_security_settings {
  828. data=$(tempfile 2>/dev/null)
  829. trap "rm -f $data" 0 1 2 5 15
  830. dialog --backtitle $"Freedombone Control Panel" \
  831. --title $"Security Settings" \
  832. --radiolist $"Choose an operation:" 21 76 21 \
  833. 1 $"Run STIG tests" off \
  834. 2 $"Show ssh host public key" off \
  835. 3 $"Tor bridges" off \
  836. 4 $"Password storage" off \
  837. 5 $"Export passwords" off \
  838. 6 $"Regenerate ssh host keys" off \
  839. 7 $"Regenerate Diffie-Hellman keys" off \
  840. 8 $"Update cipersuite" off \
  841. 9 $"Create a new Let's Encrypt certificate" off \
  842. 10 $"Renew Let's Encrypt certificate" off \
  843. 11 $"Enable GPG based authentication (monkeysphere)" off \
  844. 12 $"Register a website with monkeysphere" off \
  845. 13 $"Allow ssh login with passwords" off \
  846. 14 $"Go Back/Exit" on 2> $data
  847. sel=$?
  848. case $sel in
  849. 1) exit 1;;
  850. 255) exit 1;;
  851. esac
  852. clear
  853. read_config_param SSL_CIPHERS
  854. read_config_param SSL_PROTOCOLS
  855. read_config_param SSH_CIPHERS
  856. read_config_param SSH_MACS
  857. read_config_param SSH_KEX
  858. get_imap_settings
  859. get_ssh_settings
  860. get_xmpp_settings
  861. import_settings
  862. export_settings
  863. case $(cat $data) in
  864. 1)
  865. clear
  866. echo $'Running STIG tests...'
  867. echo ''
  868. ${PROJECT_NAME}-tests --stig showall
  869. exit 0
  870. ;;
  871. 2)
  872. dialog --title $"SSH host public keys" \
  873. --msgbox "\n$(get_ssh_server_key)" 12 60
  874. exit 0
  875. ;;
  876. 3)
  877. menu_tor_bridges
  878. exit 0
  879. ;;
  880. 4)
  881. store_passwords
  882. exit 0
  883. ;;
  884. 5)
  885. export_passwords
  886. exit 0
  887. ;;
  888. 6)
  889. regenerate_ssh_host_keys
  890. ;;
  891. 7)
  892. regenerate_dh_keys
  893. ;;
  894. 8)
  895. interactive_setup
  896. update_ciphersuite
  897. ;;
  898. 9)
  899. create_letsencrypt
  900. ;;
  901. 10)
  902. renew_letsencrypt
  903. ;;
  904. 11)
  905. enable_monkeysphere
  906. ;;
  907. 12)
  908. register_website
  909. ;;
  910. 13)
  911. allow_ssh_passwords
  912. change_ssh_settings
  913. exit 0
  914. ;;
  915. 14)
  916. exit 0
  917. ;;
  918. esac
  919. change_website_settings
  920. change_imap_settings
  921. change_ssh_settings
  922. change_xmpp_settings
  923. }
  924. function import_settings {
  925. cd $CURRENT_DIR
  926. if [ ! $IMPORT_FILE ]; then
  927. return
  928. fi
  929. if [ ! -f $IMPORT_FILE ]; then
  930. echo $"Import file $IMPORT_FILE not found"
  931. exit 6393
  932. fi
  933. if grep -q "SSL_PROTOCOLS" $IMPORT_FILE; then
  934. TEMP_VALUE=$(grep "SSL_PROTOCOLS" $IMPORT_FILE | awk -F '=' '{print $2}')
  935. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  936. SSL_PROTOCOLS=$TEMP_VALUE
  937. fi
  938. fi
  939. if grep -q "SSL_CIPHERS" $IMPORT_FILE; then
  940. TEMP_VALUE=$(grep "SSL_CIPHERS" $IMPORT_FILE | awk -F '=' '{print $2}')
  941. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  942. SSL_CIPHERS=$TEMP_VALUE
  943. fi
  944. fi
  945. if grep -q "SSH_CIPHERS" $IMPORT_FILE; then
  946. TEMP_VALUE=$(grep "SSH_CIPHERS" $IMPORT_FILE | awk -F '=' '{print $2}')
  947. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  948. SSH_CIPHERS=$TEMP_VALUE
  949. fi
  950. fi
  951. if grep -q "SSH_MACS" $IMPORT_FILE; then
  952. TEMP_VALUE=$(grep "SSH_MACS" $IMPORT_FILE | awk -F '=' '{print $2}')
  953. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  954. SSH_MACS=$TEMP_VALUE
  955. fi
  956. fi
  957. if grep -q "SSH_KEX" $IMPORT_FILE; then
  958. TEMP_VALUE=$(grep "SSH_KEX" $IMPORT_FILE | awk -F '=' '{print $2}')
  959. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  960. SSH_KEX=$TEMP_VALUE
  961. fi
  962. fi
  963. if grep -q "SSH_HOST_KEY_ALGORITHMS" $IMPORT_FILE; then
  964. TEMP_VALUE=$(grep "SSH_HOST_KEY_ALGORITHMS" $IMPORT_FILE | awk -F '=' '{print $2}')
  965. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  966. SSH_HOST_KEY_ALGORITHMS=$TEMP_VALUE
  967. fi
  968. fi
  969. if grep -q "SSH_PASSWORDS" $IMPORT_FILE; then
  970. TEMP_VALUE=$(grep "SSH_PASSWORDS" $IMPORT_FILE | awk -F '=' '{print $2}')
  971. if [[ $TEMP_VALUE == "yes" || $TEMP_VALUE == "no" ]]; then
  972. SSH_PASSWORDS=$TEMP_VALUE
  973. fi
  974. fi
  975. if grep -q "XMPP_CIPHERS" $IMPORT_FILE; then
  976. TEMP_VALUE=$(grep "XMPP_CIPHERS" $IMPORT_FILE | awk -F '=' '{print $2}')
  977. if [ ${#TEMP_VALUE} -gt $MINIMUM_LENGTH ]; then
  978. XMPP_CIPHERS=$TEMP_VALUE
  979. fi
  980. fi
  981. if grep -q "XMPP_ECC_CURVE" $IMPORT_FILE; then
  982. TEMP_VALUE=$(grep "XMPP_ECC_CURVE" $IMPORT_FILE | awk -F '=' '{print $2}')
  983. if [ ${#TEMP_VALUE} -gt 3 ]; then
  984. XMPP_ECC_CURVE=$TEMP_VALUE
  985. fi
  986. fi
  987. }
  988. function export_settings {
  989. if [ ! $EXPORT_FILE ]; then
  990. return
  991. fi
  992. cd $CURRENT_DIR
  993. if [ ! -f $EXPORT_FILE ]; then
  994. if [ "$SSL_PROTOCOLS" ]; then
  995. echo "SSL_PROTOCOLS=$SSL_PROTOCOLS" >> $EXPORT_FILE
  996. fi
  997. if [ $SSL_CIPHERS ]; then
  998. echo "SSL_CIPHERS=$SSL_CIPHERS" >> $EXPORT_FILE
  999. fi
  1000. if [ $SSH_CIPHERS ]; then
  1001. echo "SSH_CIPHERS=$SSH_CIPHERS" >> $EXPORT_FILE
  1002. fi
  1003. if [ $SSH_MACS ]; then
  1004. echo "SSH_MACS=$SSH_MACS" >> $EXPORT_FILE
  1005. fi
  1006. if [ $SSH_KEX ]; then
  1007. echo "SSH_KEX=$SSH_KEX" >> $EXPORT_FILE
  1008. fi
  1009. if [ $SSH_HOST_KEY_ALGORITHMS ]; then
  1010. echo "SSH_HOST_KEY_ALGORITHMS=$SSH_HOST_KEY_ALGORITHMS" >> $EXPORT_FILE
  1011. fi
  1012. if [ $SSH_PASSWORDS ]; then
  1013. echo "SSH_PASSWORDS=$SSH_PASSWORDS" >> $EXPORT_FILE
  1014. fi
  1015. if [ $XMPP_CIPHERS ]; then
  1016. echo "XMPP_CIPHERS=$XMPP_CIPHERS" >> $EXPORT_FILE
  1017. fi
  1018. if [ $XMPP_ECC_CURVE ]; then
  1019. echo "XMPP_ECC_CURVE=$XMPP_ECC_CURVE" >> $EXPORT_FILE
  1020. fi
  1021. echo "Security settings exported to $EXPORT_FILE"
  1022. exit 0
  1023. fi
  1024. if [ "$SSL_PROTOCOLS" ]; then
  1025. if grep -q "SSL_PROTOCOLS" $EXPORT_FILE; then
  1026. sed -i "s|SSL_PROTOCOLS=.*|SSL_PROTOCOLS=$SSL_PROTOCOLS|g" $EXPORT_FILE
  1027. else
  1028. echo "SSL_PROTOCOLS=$SSL_PROTOCOLS" >> $EXPORT_FILE
  1029. fi
  1030. fi
  1031. if [ $SSL_CIPHERS ]; then
  1032. if grep -q "SSL_CIPHERS" $EXPORT_FILE; then
  1033. sed -i "s|SSL_CIPHERS=.*|SSL_CIPHERS=$SSL_CIPHERS|g" $EXPORT_FILE
  1034. else
  1035. echo "SSL_CIPHERS=$SSL_CIPHERS" >> $EXPORT_FILE
  1036. fi
  1037. fi
  1038. if [ $SSH_CIPHERS ]; then
  1039. if grep -q "SSH_CIPHERS" $EXPORT_FILE; then
  1040. sed -i "s|SSH_CIPHERS=.*|SSH_CIPHERS=$SSH_CIPHERS|g" $EXPORT_FILE
  1041. else
  1042. echo "SSH_CIPHERS=$SSH_CIPHERS" >> $EXPORT_FILE
  1043. fi
  1044. fi
  1045. if [ $SSH_MACS ]; then
  1046. if grep -q "SSH_MACS" $EXPORT_FILE; then
  1047. sed -i "s|SSH_MACS=.*|SSH_MACS=$SSH_MACS|g" $EXPORT_FILE
  1048. else
  1049. echo "SSH_MACS=$SSH_MACS" >> $EXPORT_FILE
  1050. fi
  1051. fi
  1052. if [ $SSH_KEX ]; then
  1053. if grep -q "SSH_KEX" $EXPORT_FILE; then
  1054. sed -i "s|SSH_KEX=.*|SSH_KEX=$SSH_KEX|g" $EXPORT_FILE
  1055. else
  1056. echo "SSH_KEX=$SSH_KEX" >> $EXPORT_FILE
  1057. fi
  1058. fi
  1059. if [ $SSH_HOST_KEY_ALGORITHMS ]; then
  1060. if grep -q "SSH_HOST_KEY_ALGORITHMS" $EXPORT_FILE; then
  1061. sed -i "s|SSH_HOST_KEY_ALGORITHMS=.*|SSH_HOST_KEY_ALGORITHMS=$SSH_HOST_KEY_ALGORITHMS|g" $EXPORT_FILE
  1062. else
  1063. echo "SSH_HOST_KEY_ALGORITHMS=$SSH_HOST_KEY_ALGORITHMS" >> $EXPORT_FILE
  1064. fi
  1065. fi
  1066. if [ $SSH_PASSWORDS ]; then
  1067. if grep -q "SSH_PASSWORDS" $EXPORT_FILE; then
  1068. sed -i "s|SSH_PASSWORDS=.*|SSH_PASSWORDS=$SSH_PASSWORDS|g" $EXPORT_FILE
  1069. else
  1070. echo "SSH_PASSWORDS=$SSH_PASSWORDS" >> $EXPORT_FILE
  1071. fi
  1072. fi
  1073. if [ $XMPP_CIPHERS ]; then
  1074. if grep -q "XMPP_CIPHERS" $EXPORT_FILE; then
  1075. sed -i "s|XMPP_CIPHERS=.*|XMPP_CIPHERS=$XMPP_CIPHERS|g" $EXPORT_FILE
  1076. else
  1077. echo "XMPP_CIPHERS=$XMPP_CIPHERS" >> $EXPORT_FILE
  1078. fi
  1079. fi
  1080. if [ $XMPP_ECC_CURVE ]; then
  1081. if grep -q "XMPP_ECC_CURVE" $EXPORT_FILE; then
  1082. sed -i "s|XMPP_ECC_CURVE=.*|XMPP_ECC_CURVE=$XMPP_ECC_CURVE|g" $EXPORT_FILE
  1083. else
  1084. echo "XMPP_ECC_CURVE=$XMPP_ECC_CURVE" >> $EXPORT_FILE
  1085. fi
  1086. fi
  1087. echo $"Security settings exported to $EXPORT_FILE"
  1088. exit 0
  1089. }
  1090. function refresh_gpg_keys {
  1091. for d in /home/*/ ; do
  1092. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  1093. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  1094. su -c 'gpg --refresh-keys' - $USERNAME
  1095. fi
  1096. done
  1097. exit 0
  1098. }
  1099. function monkeysphere_sign_server_keys {
  1100. server_keys_file=/home/$USER/.monkeysphere/server_keys
  1101. if [ ! -f $server_keys_file ]; then
  1102. exit 0
  1103. fi
  1104. keys_signed=
  1105. while read line; do
  1106. echo $line
  1107. if [ ${#line} -gt 2 ]; then
  1108. fpr=$(gpg --with-colons --fingerprint "$line" | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  1109. if [ ${#fpr} -gt 2 ]; then
  1110. gpg --sign-key $fpr
  1111. if [ "$?" = "0" ]; then
  1112. gpg --update-trustdb
  1113. keys_signed=1
  1114. fi
  1115. fi
  1116. fi
  1117. done <$server_keys_file
  1118. if [ $keys_signed ]; then
  1119. rm $server_keys_file
  1120. fi
  1121. exit 0
  1122. }
  1123. function htmly_hash {
  1124. # produces a hash corresponding to a htmly password
  1125. pass="$1"
  1126. HTMLYHASH_FILENAME=/usr/bin/htmlyhash
  1127. echo '<?php' > $HTMLYHASH_FILENAME
  1128. echo 'parse_str(implode("&", array_slice($argv, 1)), $_GET);' >> $HTMLYHASH_FILENAME
  1129. echo '$password = $_GET["password"];' >> $HTMLYHASH_FILENAME
  1130. echo '$hash = password_hash($password, PASSWORD_BCRYPT);' >> $HTMLYHASH_FILENAME
  1131. echo 'if (password_verify($password, $hash)) {' >> $HTMLYHASH_FILENAME
  1132. echo ' echo $hash;' >> $HTMLYHASH_FILENAME
  1133. echo '}' >> $HTMLYHASH_FILENAME
  1134. echo '?>' >> $HTMLYHASH_FILENAME
  1135. php $HTMLYHASH_FILENAME password="$pass"
  1136. }
  1137. function show_help {
  1138. echo ''
  1139. echo "${PROJECT_NAME}-sec"
  1140. echo ''
  1141. echo $'Alters the security settings'
  1142. echo ''
  1143. echo ''
  1144. echo $' -h --help Show help'
  1145. echo $' -e --export Export security settings to a file'
  1146. echo $' -i --import Import security settings from a file'
  1147. echo $' -r --refresh Refresh GPG keys for all users'
  1148. echo $' -s --sign Sign monkeysphere server keys'
  1149. echo $' --register [domain] Register a https domain with monkeysphere'
  1150. echo $' -b --htmlyhash [password] Returns the hash of a password for a htmly blog'
  1151. echo ''
  1152. exit 0
  1153. }
  1154. # Get the commandline options
  1155. while [[ $# > 1 ]]
  1156. do
  1157. key="$1"
  1158. case $key in
  1159. -h|--help)
  1160. show_help
  1161. ;;
  1162. # Export settings
  1163. -e|--export)
  1164. shift
  1165. EXPORT_FILE="$1"
  1166. ;;
  1167. # Export settings
  1168. -i|--import)
  1169. shift
  1170. IMPORT_FILE="$1"
  1171. ;;
  1172. # Refresh GPG keys
  1173. -r|--refresh)
  1174. shift
  1175. refresh_gpg_keys
  1176. ;;
  1177. # register a website
  1178. --register|--reg|--site)
  1179. shift
  1180. register_website "$1"
  1181. ;;
  1182. # user signs monkeysphere server keys
  1183. -s|--sign)
  1184. shift
  1185. monkeysphere_sign_server_keys
  1186. ;;
  1187. # get a hash of the given htmly password
  1188. -b|--htmlyhash)
  1189. shift
  1190. htmly_hash "$1"
  1191. exit 0
  1192. ;;
  1193. *)
  1194. # unknown option
  1195. ;;
  1196. esac
  1197. shift
  1198. done
  1199. menu_security_settings
  1200. exit 0