freedombone-adduser 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Adds an user to the system
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=${PROJECT_NAME}-adduser
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  33. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  34. for f in $UTILS_FILES
  35. do
  36. source $f
  37. done
  38. APP_FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  39. for f in $APP_FILES
  40. do
  41. source $f
  42. done
  43. ADD_USERNAME=$1
  44. SSH_PUBLIC_KEY="$2"
  45. GPG_KEYSERVER='hkp://keys.gnupg.net'
  46. SSH_PORT=2222
  47. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  48. DEFAULT_DOMAIN_NAME=
  49. if [ ! $ADD_USERNAME ]; then
  50. echo $'No username was given'
  51. exit 1
  52. fi
  53. if [ -d /home/$ADD_USERNAME ]; then
  54. echo $"The user $ADD_USERNAME already exists"
  55. exit 2
  56. fi
  57. if [ ! -f $COMPLETION_FILE ]; then
  58. echo $"$COMPLETION_FILE not found"
  59. userdel -r $ADD_USERNAME
  60. exit 3
  61. fi
  62. # Minimum number of characters in a password
  63. MINIMUM_PASSWORD_LENGTH=$(cat /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-passwords | grep 'MINIMUM_PASSWORD_LENGTH=' | head -n 1 | awk -F '=' '{print $2}')
  64. NEW_USER_PASSWORD="$(openssl rand -base64 30 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
  65. chmod 600 /etc/shadow
  66. chmod 600 /etc/gshadow
  67. useradd -m -p "$NEW_USER_PASSWORD" -s /bin/bash $ADD_USERNAME
  68. adduser $ADD_USERNAME sasl
  69. groupadd $ADD_USERNAME
  70. chmod 0000 /etc/shadow
  71. chmod 0000 /etc/gshadow
  72. if [ ! -d /home/$ADD_USERNAME ]; then
  73. echo $'Home directory was not created'
  74. exit 4
  75. fi
  76. if [ "$SSH_PUBLIC_KEY" ]; then
  77. if [ ${#SSH_PUBLIC_KEY} -gt 5 ]; then
  78. if [ -f "$SSH_PUBLIC_KEY" ]; then
  79. mkdir /home/$ADD_USERNAME/.ssh
  80. cp $SSH_PUBLIC_KEY /home/$ADD_USERNAME/.ssh/authorized_keys
  81. chown -R $ADD_USERNAME:$ADD_USERNAME /home/$ADD_USERNAME/.ssh
  82. echo $'ssh public key installed'
  83. else
  84. if [[ "$SSH_PUBLIC_KEY" == "ssh-"* ]]; then
  85. mkdir /home/$ADD_USERNAME/.ssh
  86. echo "$SSH_PUBLIC_KEY" > /home/$ADD_USERNAME/.ssh/authorized_keys
  87. chown -R $ADD_USERNAME:$ADD_USERNAME /home/$ADD_USERNAME/.ssh
  88. echo $'ssh public key installed'
  89. else
  90. echo $'The second parameter does not look like an ssh key'
  91. exit 5
  92. fi
  93. fi
  94. fi
  95. fi
  96. if [ -d /home/$ADD_USERNAME/Maildir ]; then
  97. if grep -q "set from=" /home/$ADD_USERNAME/.muttrc; then
  98. sed -i "s|set from=.*|set from='$ADD_USERNAME <$ADD_USERNAME@$HOSTNAME>'|g" /home/$ADD_USERNAME/.muttrc
  99. else
  100. echo "set from='$ADD_USERNAME <$ADD_USERNAME@$HOSTNAME>'" >> /home/$ADD_USERNAME/.muttrc
  101. fi
  102. USERN='$USER@'
  103. sed -i "s|$USERN|$ADD_USERNAME@|g" /home/$ADD_USERNAME/.procmailrc
  104. fi
  105. # generate a gpg key
  106. echo "Making a GPG key for $ADD_USERNAME@$HOSTNAME"
  107. mkdir /home/$ADD_USERNAME/.gnupg
  108. echo "keyserver $GPG_KEYSERVER" >> /home/$ADD_USERNAME/.gnupg/gpg.conf
  109. echo 'keyserver-options auto-key-retrieve' >> /home/$ADD_USERNAME/.gnupg/gpg.conf
  110. echo '' >> /home/$ADD_USERNAME/.gnupg/gpg.conf
  111. echo '# default preferences' >> /home/$ADD_USERNAME/.gnupg/gpg.conf
  112. echo 'personal-digest-preferences SHA256' >> /home/$ADD_USERNAME/.gnupg/gpg.conf
  113. echo 'cert-digest-algo SHA256' >> /home/$ADD_USERNAME/.gnupg/gpg.conf
  114. echo 'default-preference-list SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP Uncompressed' >> /home/$ADD_USERNAME/.gnupg/gpg.conf
  115. chown -R $ADD_USERNAME:$ADD_USERNAME /home/$ADD_USERNAME/.gnupg
  116. chmod 700 /home/$ADD_USERNAME/.gnupg
  117. chmod 600 /home/$ADD_USERNAME/.gnupg/*
  118. # Generate a GPG key
  119. echo 'Key-Type: 1' > /home/$ADD_USERNAME/gpg-genkey.conf
  120. echo 'Key-Length: 4096' >> /home/$ADD_USERNAME/gpg-genkey.conf
  121. echo 'Subkey-Type: 1' >> /home/$ADD_USERNAME/gpg-genkey.conf
  122. echo 'Subkey-Length: 4096' >> /home/$ADD_USERNAME/gpg-genkey.conf
  123. echo "Name-Real: $ADD_USERNAME" >> /home/$ADD_USERNAME/gpg-genkey.conf
  124. echo "Name-Email: $ADD_USERNAME@$HOSTNAME" >> /home/$ADD_USERNAME/gpg-genkey.conf
  125. echo 'Expire-Date: 0' >> /home/$ADD_USERNAME/gpg-genkey.conf
  126. chown $ADD_USERNAME:$ADD_USERNAME /home/$ADD_USERNAME/gpg-genkey.conf
  127. su -c "gpg --batch --gen-key /home/$ADD_USERNAME/gpg-genkey.conf" - $ADD_USERNAME
  128. shred -zu /home/$ADD_USERNAME/gpg-genkey.conf
  129. MY_GPG_PUBLIC_KEY_ID=$(su -c "gpg --list-keys $ADD_USERNAME@$HOSTNAME | grep 'pub '" - $ADD_USERNAME | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  130. MY_GPG_PUBLIC_KEY=/home/$ADD_USERNAME/public_key.gpg
  131. su -c "gpg --output $MY_GPG_PUBLIC_KEY --armor --export $MY_GPG_PUBLIC_KEY_ID" - $ADD_USERNAME
  132. if [ ! -f $MY_GPG_PUBLIC_KEY ]; then
  133. echo "GPG public key was not generated for $ADD_USERNAME@$HOSTNAME $MY_GPG_PUBLIC_KEY_ID"
  134. userdel -r $ADD_USERNAME
  135. exit 7
  136. fi
  137. # add a monkeysphere subkey
  138. #echo $'Adding monkeysphere subkey'
  139. #su -c "monkeysphere gen-subkey" - $ADD_USERNAME
  140. #echo $'Adding monkeysphere subkey to ssh-agent'
  141. #su -c "monkeysphere s" - $ADD_USERNAME
  142. # add authorized GPG email address
  143. mkdir /home/$ADD_USERNAME/.monkeysphere
  144. chmod 755 /home/$ADD_USERNAME/.monkeysphere
  145. echo "$ADD_USERNAME <$ADD_USERNAME@$HOSTNAME>" > /home/$ADD_USERNAME/.monkeysphere/authorized_user_ids
  146. chmod 644 /home/$ADD_USERNAME/.monkeysphere/authorized_user_ids
  147. chown -R $ADD_USERNAME:$ADD_USERNAME /home/$ADD_USERNAME/.monkeysphere
  148. echo $'Updating monkeysphere users'
  149. monkeysphere-authentication update-users
  150. if [ -f /home/$ADD_USERNAME/.muttrc ]; then
  151. # encrypt outgoing mail to the "sent" folder
  152. if ! grep -q "pgp_encrypt_only_command" /home/$ADD_USERNAME/.muttrc; then
  153. echo '' >> /home/$ADD_USERNAME/.muttrc
  154. echo $'# Encrypt items in the Sent folder' >> /home/$ADD_USERNAME/.muttrc
  155. echo "set pgp_encrypt_only_command=\"/usr/lib/mutt/pgpewrap gpg --batch --quiet --no-verbose --output - --encrypt --textmode --armor --always-trust --encrypt-to 0x$MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\"" >> /home/$ADD_USERNAME/.muttrc
  156. else
  157. sed -i "s|set pgp_encrypt_only_command.*|set pgp_encrypt_only_command=\"/usr/lib/mutt/pgpewrap gpg --batch --quiet --no-verbose --output - --encrypt --textmode --armor --always-trust --encrypt-to 0x$MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\"|g" /home/$ADD_USERNAME/.muttrc
  158. fi
  159. if ! grep -q "pgp_encrypt_sign_command" /home/$ADD_USERNAME/.muttrc; then
  160. echo "set pgp_encrypt_sign_command=\"/usr/lib/mutt/pgpewrap gpg %?p?--passphrase-fd 0? --batch --quiet --no-verbose --textmode --output - --encrypt --sign %?a?-u %a? --armor --always-trust --encrypt-to 0x$MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\"" >> /home/$ADD_USERNAME/.muttrc
  161. else
  162. sed -i "s|set pgp_encrypt_sign_command.*|set pgp_encrypt_sign_command=\"/usr/lib/mutt/pgpewrap gpg %?p?--passphrase-fd 0? --batch --quiet --no-verbose --textmode --output - --encrypt --sign %?a?-u %a? --armor --always-trust --encrypt-to 0x$MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\"|g" /home/$ADD_USERNAME/.muttrc
  163. fi
  164. fi
  165. if ! grep -q "Change your GPG password" /home/$ADD_USERNAME/README; then
  166. echo '' >> /home/$ADD_USERNAME/README
  167. echo '' >> /home/$ADD_USERNAME/README
  168. echo $'# Change your GPG password' >> /home/$ADD_USERNAME/README
  169. echo $"It's very important to add a password to your GPG key so that" >> /home/$ADD_USERNAME/README
  170. echo $"if anyone does get access to your email they still won't be able" >> /home/$ADD_USERNAME/README
  171. echo $'to read them without knowning the GPG password.' >> /home/$ADD_USERNAME/README
  172. echo $'You can change the it with:' >> /home/$ADD_USERNAME/README
  173. echo '' >> /home/$ADD_USERNAME/README
  174. echo " gpg --edit-key $MY_GPG_PUBLIC_KEY_ID" >> /home/$ADD_USERNAME/README
  175. echo ' passwd' >> /home/$ADD_USERNAME/README
  176. echo ' save' >> /home/$ADD_USERNAME/README
  177. echo ' quit' >> /home/$ADD_USERNAME/README
  178. fi
  179. chown $ADD_USERNAME:$ADD_USERNAME /home/$ADD_USERNAME/README
  180. chown $ADD_USERNAME:$ADD_USERNAME $MY_GPG_PUBLIC_KEY
  181. chmod 600 /home/$ADD_USERNAME/README
  182. echo $'Detecting installed apps...'
  183. detect_apps
  184. get_apps_installed_names
  185. for app_name in "${APPS_INSTALLED_NAMES[@]}"
  186. do
  187. if [[ $(function_exists add_user_${app_name}) == "1" ]]; then
  188. echo $"Adding user to ${app_name}"
  189. app_load_variables ${app_name}
  190. retval=$(add_user_${app_name} "$ADD_USERNAME" "$NEW_USER_PASSWORD" | tail -n 1)
  191. if [[ $retval != '0' ]]; then
  192. echo $"Failed with error code ${retval}"
  193. ${PROJECT_NAME}-rmuser $ADD_USERNAME --force
  194. exit 672392
  195. fi
  196. if ! grep -q "${app_name}_${ADD_USERNAME}" $APP_USERS_FILE; then
  197. echo "${app_name}_${ADD_USERNAME}" >> $APP_USERS_FILE
  198. fi
  199. fi
  200. done
  201. if [ -f /etc/nginx/.htpasswd ]; then
  202. if ! grep "${ADD_USERNAME}:" /etc/nginx/.htpasswd; then
  203. echo "$NEW_USER_PASSWORD" | htpasswd -i -s /etc/nginx/.htpasswd $ADD_USERNAME
  204. fi
  205. fi
  206. # add user menu on ssh login
  207. if ! grep -q 'control' /home/$ADD_USERNAME/.bashrc; then
  208. echo 'control' >> /home/$ADD_USERNAME/.bashrc
  209. fi
  210. ${PROJECT_NAME}-pass -u $ADD_USERNAME -a login -p "$NEW_USER_PASSWORD"
  211. clear
  212. echo $"New user $ADD_USERNAME was created"
  213. echo $"Their login password is $NEW_USER_PASSWORD"
  214. echo ''
  215. echo $"They can download their GPG keys with:"
  216. echo ''
  217. echo " scp -P $SSH_PORT -r $ADD_USERNAME@$HOSTNAME:/home/$ADD_USERNAME/.gnupg ~/"
  218. echo ''
  219. echo $"They should also run ${PROJECT_NAME}-client on their system to ensure"
  220. echo $'the best security.'
  221. exit 0