freedombone-deploy 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # An initialisation script which can be run after installing
  12. # a disk image
  13. #
  14. # License
  15. # =======
  16. #
  17. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  18. #
  19. # This program is free software: you can redistribute it and/or modify
  20. # it under the terms of the GNU General Public License as published by
  21. # the Free Software Foundation, either version 3 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. PROJECT_NAME='freedombone'
  32. export TEXTDOMAIN=${PROJECT_NAME}-deploy
  33. export TEXTDOMAINDIR="/usr/share/locale"
  34. # Default username for disk images
  35. DEFAULT_IMAGE_USERNAME='freedom'
  36. MY_NAME='fbn'
  37. MY_USERNAME=$MY_NAME
  38. MY_EMAIL_ADDRESS=$MY_USERNAME@$(hostname)
  39. # various passwords
  40. CJDNS_PASSWORD=
  41. MARIADB_PASSWORD=
  42. MICROBLOG_ADMIN_PASSWORD=
  43. GIT_ADMIN_PASSWORD=
  44. HUBZILLA_ADMIN_PASSWORD=
  45. OWNCLOUD_ADMIN_PASSWORD=
  46. WIKI_ADMIN_PASSWORD=
  47. FULLBLOG_ADMIN_PASSWORD=
  48. VOIP_SERVER_PASSWORD=
  49. SIP_SERVER_PASSWORD=
  50. function create_backup_gpg_key {
  51. echo 'Key-Type: 1' > /home/$MY_USERNAME/gpg-genkey.conf
  52. echo 'Key-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  53. echo 'Subkey-Type: 1' >> /home/$MY_USERNAME/gpg-genkey.conf
  54. echo 'Subkey-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  55. echo "Name-Real: $MY_NAME" >> /home/$MY_USERNAME/gpg-genkey.conf
  56. echo "Name-Email: $MY_EMAIL_ADDRESS" >> /home/$MY_USERNAME/gpg-genkey.conf
  57. echo "Name-Comment: backup key" >> /home/$MY_USERNAME/gpg-genkey.conf
  58. echo 'Expire-Date: 0' >> /home/$MY_USERNAME/gpg-genkey.conf
  59. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/gpg-genkey.conf
  60. su -c "gpg --batch --gen-key /home/$MY_USERNAME/gpg-genkey.conf" - $MY_USERNAME
  61. shred -zu /home/$MY_USERNAME/gpg-genkey.conf
  62. BACKUP_KEY_EXISTS=$(su -c "gpg --list-keys \"$MY_NAME (backup key)\"" - $MY_USERNAME)
  63. if [ ! "$?" = "0" ]; then
  64. echo 'Backup key could not be created'
  65. exit 43382
  66. fi
  67. MY_BACKUP_KEY_ID=$(su -c "gpg --list-keys \"$MY_NAME (backup key)\" | grep 'pub '" - $MY_USERNAME | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  68. echo "Backup key: $MY_BACKUP_KEY_ID"
  69. MY_BACKUP_KEY=/home/$MY_USERNAME/backup_key
  70. su -c "gpg --output ${MY_BACKUP_KEY}_public.asc --armor --export $MY_BACKUP_KEY_ID" - $MY_USERNAME
  71. su -c "gpg --output ${MY_BACKUP_KEY}_private.asc --armor --export-secret-key $MY_BACKUP_KEY_ID" - $MY_USERNAME
  72. if [ ! -f ${MY_BACKUP_KEY}_public.asc ]; then
  73. echo 'Public backup key could not be exported'
  74. exit 36829
  75. fi
  76. if [ ! -f ${MY_BACKUP_KEY}_private.asc ]; then
  77. echo 'Private backup key could not be exported'
  78. exit 29235
  79. fi
  80. # import backup key to root user
  81. gpg --import --import ${MY_BACKUP_KEY}_public.asc
  82. gpg --allow-secret-key-import --import ${MY_BACKUP_KEY}_private.asc
  83. shred -zu ${MY_BACKUP_KEY}_public.asc
  84. shred -zu ${MY_BACKUP_KEY}_private.asc
  85. echo 'New backup gpg key created'
  86. }
  87. function regenerate_ssh_host_keys {
  88. rm -f /etc/ssh/ssh_host_*
  89. dpkg-reconfigure openssh-server
  90. echo 'ssh host keys regenerated'
  91. # remove small moduli
  92. awk '$5 > 2000' /etc/ssh/moduli > ~/moduli
  93. mv ~/moduli /etc/ssh/moduli
  94. echo 'ssh small moduli removed'
  95. systemctl restart ssh
  96. }
  97. function get_passwords_from_readme {
  98. readme_file=$1
  99. if [ ! -f $readme_file ]; then
  100. return
  101. fi
  102. if grep -q "cjdns password" $readme_file; then
  103. if [ ! $CJDNS_PASSWORD ]; then
  104. CJDNS_PASSWORD=$(cat $readme_file | grep "cjdns password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  105. fi
  106. fi
  107. if grep -q "MariaDB password" $readme_file; then
  108. if [ -f $DATABASE_PASSWORD_FILE ]; then
  109. MARIADB_PASSWORD=$(cat $DATABASE_PASSWORD_FILE)
  110. else
  111. MARIADB_PASSWORD=$(cat $readme_file | grep "MariaDB password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  112. echo "$MARIADB_PASSWORD" > $DATABASE_PASSWORD_FILE
  113. chmod 600 $DATABASE_PASSWORD_FILE
  114. fi
  115. fi
  116. if grep -q "MariaDB gnusocial admin password" $readme_file; then
  117. MICROBLOG_ADMIN_PASSWORD=$(cat $readme_file | grep "MariaDB gnusocial admin password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  118. fi
  119. if grep -q "Gogs admin user password" $readme_file; then
  120. GIT_ADMIN_PASSWORD=$(cat $readme_file | grep "Gogs admin user password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  121. fi
  122. if grep -q "MariaDB Hubzilla admin password" $readme_file; then
  123. HUBZILLA_ADMIN_PASSWORD=$(cat $readme_file | grep "MariaDB Hubzilla admin password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  124. fi
  125. if grep -q "Owncloud database password" $readme_file; then
  126. OWNCLOUD_ADMIN_PASSWORD=$(cat $readme_file | grep "Owncloud database password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  127. fi
  128. if grep -q "Wiki password" $readme_file; then
  129. WIKI_ADMIN_PASSWORD=$(cat $readme_file | grep "Wiki password:" | awk -F ':' '{print $2}' | sed 's/^ *//')
  130. fi
  131. }
  132. function set_admin_user {
  133. sed -i "s|Admin user:.*|Admin user:$MY_USERNAME|g" $COMPLETION_FILE
  134. }
  135. if [ ! -d /home/$DEFAULT_IMAGE_USERNAME ]; then
  136. echo "User $DEFAULT_IMAGE_USERNAME not found"
  137. exit 52372
  138. fi
  139. if [ -d /home/$MY_USERNAME ]; then
  140. echo "User $MY_USERNAME already exists"
  141. exit 73538
  142. fi
  143. get_passwords_from_readme /home/$DEFAULT_IMAGE_USERNAME/README
  144. #${PROJECT_NAME}-adduser $MY_USERNAME > ~/setup.txt
  145. #set_admin_user
  146. #create_backup_gpg_key
  147. #regenerate_ssh_host_keys
  148. exit 0