freedombone-sec 43KB

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