freedombone-deploy 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. # Default username for disk images
  32. DEFAULT_IMAGE_USERNAME='freedom'
  33. MY_NAME='fbn'
  34. MY_USERNAME=$MY_NAME
  35. MY_EMAIL_ADDRESS=$MY_USERNAME@$(hostname)
  36. # various passwords
  37. CJDNS_PASSWORD=
  38. MARIADB_PASSWORD=
  39. MICROBLOG_ADMIN_PASSWORD=
  40. GIT_ADMIN_PASSWORD=
  41. HUBZILLA_ADMIN_PASSWORD=
  42. OWNCLOUD_ADMIN_PASSWORD=
  43. WIKI_ADMIN_PASSWORD=
  44. FULLBLOG_ADMIN_PASSWORD=
  45. VOIP_SERVER_PASSWORD=
  46. SIP_SERVER_PASSWORD=
  47. function create_backup_gpg_key {
  48. echo 'Key-Type: 1' > /home/$MY_USERNAME/gpg-genkey.conf
  49. echo 'Key-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  50. echo 'Subkey-Type: 1' >> /home/$MY_USERNAME/gpg-genkey.conf
  51. echo 'Subkey-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  52. echo "Name-Real: $MY_NAME" >> /home/$MY_USERNAME/gpg-genkey.conf
  53. echo "Name-Email: $MY_EMAIL_ADDRESS" >> /home/$MY_USERNAME/gpg-genkey.conf
  54. echo "Name-Comment: backup key" >> /home/$MY_USERNAME/gpg-genkey.conf
  55. echo 'Expire-Date: 0' >> /home/$MY_USERNAME/gpg-genkey.conf
  56. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/gpg-genkey.conf
  57. su -c "gpg --batch --gen-key /home/$MY_USERNAME/gpg-genkey.conf" - $MY_USERNAME
  58. shred -zu /home/$MY_USERNAME/gpg-genkey.conf
  59. BACKUP_KEY_EXISTS=$(su -c "gpg --list-keys \"$MY_NAME (backup key)\"" - $MY_USERNAME)
  60. if [ ! "$?" = "0" ]; then
  61. echo 'Backup key could not be created'
  62. exit 43382
  63. fi
  64. 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}')
  65. echo "Backup key: $MY_BACKUP_KEY_ID"
  66. MY_BACKUP_KEY=/home/$MY_USERNAME/backup_key
  67. su -c "gpg --output ${MY_BACKUP_KEY}_public.asc --armor --export $MY_BACKUP_KEY_ID" - $MY_USERNAME
  68. su -c "gpg --output ${MY_BACKUP_KEY}_private.asc --armor --export-secret-key $MY_BACKUP_KEY_ID" - $MY_USERNAME
  69. if [ ! -f ${MY_BACKUP_KEY}_public.asc ]; then
  70. echo 'Public backup key could not be exported'
  71. exit 36829
  72. fi
  73. if [ ! -f ${MY_BACKUP_KEY}_private.asc ]; then
  74. echo 'Private backup key could not be exported'
  75. exit 29235
  76. fi
  77. # import backup key to root user
  78. gpg --import --import ${MY_BACKUP_KEY}_public.asc
  79. gpg --allow-secret-key-import --import ${MY_BACKUP_KEY}_private.asc
  80. shred -zu ${MY_BACKUP_KEY}_public.asc
  81. shred -zu ${MY_BACKUP_KEY}_private.asc
  82. echo 'New backup gpg key created'
  83. }
  84. function regenerate_ssh_host_keys {
  85. rm -f /etc/ssh/ssh_host_*
  86. dpkg-reconfigure openssh-server
  87. echo 'ssh host keys regenerated'
  88. # remove small moduli
  89. awk '$5 > 2000' /etc/ssh/moduli > ~/moduli
  90. mv ~/moduli /etc/ssh/moduli
  91. echo 'ssh small moduli removed'
  92. systemctl restart ssh
  93. }
  94. function get_passwords_from_readme {
  95. readme_file=$1
  96. if [ ! -f $readme_file ]; then
  97. return
  98. fi
  99. if grep -q "cjdns password" $readme_file; then
  100. if [ ! $CJDNS_PASSWORD ]; then
  101. CJDNS_PASSWORD=$(cat $readme_file | grep "cjdns password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  102. fi
  103. fi
  104. if grep -q "MariaDB password" $readme_file; then
  105. if [ -f $DATABASE_PASSWORD_FILE ]; then
  106. MARIADB_PASSWORD=$(cat $DATABASE_PASSWORD_FILE)
  107. else
  108. MARIADB_PASSWORD=$(cat $readme_file | grep "MariaDB password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  109. echo "$MARIADB_PASSWORD" > $DATABASE_PASSWORD_FILE
  110. chmod 600 $DATABASE_PASSWORD_FILE
  111. fi
  112. fi
  113. if grep -q "MariaDB gnusocial admin password" $readme_file; then
  114. MICROBLOG_ADMIN_PASSWORD=$(cat $readme_file | grep "MariaDB gnusocial admin password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  115. fi
  116. if grep -q "Gogs admin user password" $readme_file; then
  117. GIT_ADMIN_PASSWORD=$(cat $readme_file | grep "Gogs admin user password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  118. fi
  119. if grep -q "MariaDB Hubzilla admin password" $readme_file; then
  120. HUBZILLA_ADMIN_PASSWORD=$(cat $readme_file | grep "MariaDB Hubzilla admin password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  121. fi
  122. if grep -q "Owncloud database password" $readme_file; then
  123. OWNCLOUD_ADMIN_PASSWORD=$(cat $readme_file | grep "Owncloud database password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  124. fi
  125. if grep -q "Wiki password" $readme_file; then
  126. WIKI_ADMIN_PASSWORD=$(cat $readme_file | grep "Wiki password:" | awk -F ':' '{print $2}' | sed 's/^ *//')
  127. fi
  128. }
  129. function set_admin_user {
  130. sed -i "s|Admin user:.*|Admin user:$MY_USERNAME|g" $COMPLETION_FILE
  131. }
  132. if [ ! -d /home/$DEFAULT_IMAGE_USERNAME ]; then
  133. echo "User $DEFAULT_IMAGE_USERNAME not found"
  134. exit 52372
  135. fi
  136. if [ -d /home/$MY_USERNAME ]; then
  137. echo "User $MY_USERNAME already exists"
  138. exit 73538
  139. fi
  140. get_passwords_from_readme /home/$DEFAULT_IMAGE_USERNAME/README
  141. #freedombone-adduser $MY_USERNAME > ~/setup.txt
  142. #set_admin_user
  143. #create_backup_gpg_key
  144. #regenerate_ssh_host_keys
  145. exit 0