2015-10-26 15:25:58 +01:00
|
|
|
#!/bin/bash
|
2015-11-02 09:13:11 +01:00
|
|
|
#
|
|
|
|
# .---. . .
|
|
|
|
# | | |
|
|
|
|
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
|
|
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
|
|
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
|
|
#
|
|
|
|
# Freedom in the Cloud
|
|
|
|
#
|
|
|
|
|
|
|
|
# Adds an user to the system
|
|
|
|
|
|
|
|
# License
|
|
|
|
# =======
|
|
|
|
#
|
2016-10-31 17:24:49 +01:00
|
|
|
# Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
|
2015-11-02 09:13:11 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2016-02-13 23:09:27 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2015-11-02 09:13:11 +01:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2016-02-13 23:09:27 +01:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
2015-11-02 09:13:11 +01:00
|
|
|
#
|
2016-02-13 23:09:27 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-11-02 09:13:11 +01:00
|
|
|
|
2015-11-27 12:42:16 +01:00
|
|
|
PROJECT_NAME='freedombone'
|
|
|
|
|
2015-11-27 17:52:23 +01:00
|
|
|
export TEXTDOMAIN=${PROJECT_NAME}-adduser
|
2015-11-27 12:42:16 +01:00
|
|
|
export TEXTDOMAINDIR="/usr/share/locale"
|
|
|
|
|
2016-10-09 21:18:04 +02:00
|
|
|
CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
|
2016-02-27 17:14:44 +01:00
|
|
|
|
2016-10-02 00:39:56 +02:00
|
|
|
UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
|
|
|
|
for f in $UTILS_FILES
|
|
|
|
do
|
2016-10-26 00:04:56 +02:00
|
|
|
source $f
|
2016-10-02 00:39:56 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
APP_FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
|
|
|
|
for f in $APP_FILES
|
|
|
|
do
|
2016-10-26 00:04:56 +02:00
|
|
|
source $f
|
2016-10-02 00:39:56 +02:00
|
|
|
done
|
|
|
|
|
2015-10-26 15:25:58 +01:00
|
|
|
MY_USERNAME=$1
|
2015-10-31 21:26:00 +01:00
|
|
|
SSH_PUBLIC_KEY="$2"
|
2015-10-26 15:25:58 +01:00
|
|
|
GPG_KEYSERVER='hkp://keys.gnupg.net'
|
|
|
|
SSH_PORT=2222
|
2015-12-08 17:22:48 +01:00
|
|
|
COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
|
2016-03-25 17:06:57 +01:00
|
|
|
DEFAULT_DOMAIN_NAME=
|
2015-10-26 15:25:58 +01:00
|
|
|
|
|
|
|
if [ ! $MY_USERNAME ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
echo $'No username was given'
|
|
|
|
exit 1
|
2015-10-26 15:25:58 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d /home/$MY_USERNAME ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
echo $"The user $MY_USERNAME already exists"
|
|
|
|
exit 2
|
2015-10-26 15:25:58 +01:00
|
|
|
fi
|
|
|
|
|
2015-10-27 11:50:41 +01:00
|
|
|
if [ ! -f $COMPLETION_FILE ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
echo $"$COMPLETION_FILE not found"
|
|
|
|
userdel -r $MY_USERNAME
|
|
|
|
exit 3
|
2015-10-27 11:50:41 +01:00
|
|
|
fi
|
|
|
|
|
2016-06-04 15:25:46 +02:00
|
|
|
# Minimum number of characters in a password
|
2016-07-06 21:27:18 +02:00
|
|
|
MINIMUM_PASSWORD_LENGTH=$(cat /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-passwords | grep 'MINIMUM_PASSWORD_LENGTH=' | head -n 1 | awk -F '=' '{print $2}')
|
2016-06-04 15:25:46 +02:00
|
|
|
|
|
|
|
NEW_USER_PASSWORD="$(openssl rand -base64 30 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
|
2015-10-26 15:25:58 +01:00
|
|
|
useradd -m -p "$NEW_USER_PASSWORD" -s /bin/bash $MY_USERNAME
|
|
|
|
adduser $MY_USERNAME sasl
|
|
|
|
|
|
|
|
if [ ! -d /home/$MY_USERNAME ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
echo $'Home directory was not created'
|
|
|
|
exit 4
|
2015-10-26 15:25:58 +01:00
|
|
|
fi
|
|
|
|
|
2015-10-31 21:26:00 +01:00
|
|
|
if [ "$SSH_PUBLIC_KEY" ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
if [ ${#SSH_PUBLIC_KEY} -gt 5 ]; then
|
2016-08-28 10:55:47 +02:00
|
|
|
if [ -f "$SSH_PUBLIC_KEY" ]; then
|
|
|
|
mkdir /home/$MY_USERNAME/.ssh
|
|
|
|
cp $SSH_PUBLIC_KEY /home/$MY_USERNAME/.ssh/authorized_keys
|
|
|
|
chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
|
|
|
|
echo $'ssh public key installed'
|
|
|
|
else
|
|
|
|
if [[ "$SSH_PUBLIC_KEY" == "ssh-"* ]]; then
|
|
|
|
mkdir /home/$MY_USERNAME/.ssh
|
|
|
|
echo "$SSH_PUBLIC_KEY" > /home/$MY_USERNAME/.ssh/authorized_keys
|
|
|
|
chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
|
|
|
|
echo $'ssh public key installed'
|
|
|
|
else
|
|
|
|
echo $'The second parameter does not look like an ssh key'
|
|
|
|
exit 5
|
|
|
|
fi
|
|
|
|
fi
|
2016-07-24 14:19:56 +02:00
|
|
|
fi
|
2015-10-29 10:37:27 +01:00
|
|
|
fi
|
|
|
|
|
2015-12-22 21:06:02 +01:00
|
|
|
if [ -d /home/$MY_USERNAME/Maildir ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
if grep -q "set from=" /home/$MY_USERNAME/.muttrc; then
|
2016-08-28 10:55:47 +02:00
|
|
|
sed -i "s|set from=.*|set from='$MY_USERNAME <$MY_USERNAME@$HOSTNAME>'|g" /home/$MY_USERNAME/.muttrc
|
2016-07-24 14:19:56 +02:00
|
|
|
else
|
2016-08-28 10:55:47 +02:00
|
|
|
echo "set from='$MY_USERNAME <$MY_USERNAME@$HOSTNAME>'" >> /home/$MY_USERNAME/.muttrc
|
2016-07-24 14:19:56 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
USERN='$USER@'
|
|
|
|
sed -i "s|$USERN|$MY_USERNAME@|g" /home/$MY_USERNAME/.procmailrc
|
2015-10-26 20:16:26 +01:00
|
|
|
fi
|
|
|
|
|
2015-10-26 15:25:58 +01:00
|
|
|
# generate a gpg key
|
|
|
|
echo "Making a GPG key for $MY_USERNAME@$HOSTNAME"
|
|
|
|
mkdir /home/$MY_USERNAME/.gnupg
|
|
|
|
echo "keyserver $GPG_KEYSERVER" >> /home/$MY_USERNAME/.gnupg/gpg.conf
|
|
|
|
echo 'keyserver-options auto-key-retrieve' >> /home/$MY_USERNAME/.gnupg/gpg.conf
|
|
|
|
echo '' >> /home/$MY_USERNAME/.gnupg/gpg.conf
|
|
|
|
echo '# default preferences' >> /home/$MY_USERNAME/.gnupg/gpg.conf
|
|
|
|
echo 'personal-digest-preferences SHA256' >> /home/$MY_USERNAME/.gnupg/gpg.conf
|
|
|
|
echo 'cert-digest-algo SHA256' >> /home/$MY_USERNAME/.gnupg/gpg.conf
|
|
|
|
echo 'default-preference-list SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP Uncompressed' >> /home/$MY_USERNAME/.gnupg/gpg.conf
|
|
|
|
|
|
|
|
chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
|
|
|
|
chmod 700 /home/$MY_USERNAME/.gnupg
|
|
|
|
chmod 600 /home/$MY_USERNAME/.gnupg/*
|
|
|
|
|
|
|
|
# Generate a GPG key
|
|
|
|
echo 'Key-Type: 1' > /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo 'Key-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo 'Subkey-Type: 1' >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo 'Subkey-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo "Name-Real: $MY_USERNAME" >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo "Name-Email: $MY_USERNAME@$HOSTNAME" >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo 'Expire-Date: 0' >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
su -c "gpg --batch --gen-key /home/$MY_USERNAME/gpg-genkey.conf" - $MY_USERNAME
|
|
|
|
shred -zu /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
MY_GPG_PUBLIC_KEY_ID=$(su -c "gpg --list-keys $MY_USERNAME@$HOSTNAME | grep 'pub '" - $MY_USERNAME | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
|
2015-10-26 18:21:38 +01:00
|
|
|
MY_GPG_PUBLIC_KEY=/home/$MY_USERNAME/public_key.gpg
|
2015-10-26 15:25:58 +01:00
|
|
|
su -c "gpg --output $MY_GPG_PUBLIC_KEY --armor --export $MY_GPG_PUBLIC_KEY_ID" - $MY_USERNAME
|
|
|
|
|
2015-10-26 18:21:38 +01:00
|
|
|
if [ ! -f $MY_GPG_PUBLIC_KEY ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
echo "GPG public key was not generated for $MY_USERNAME@$HOSTNAME $MY_GPG_PUBLIC_KEY_ID"
|
|
|
|
userdel -r $MY_USERNAME
|
|
|
|
exit 7
|
2015-10-26 18:21:38 +01:00
|
|
|
fi
|
2016-04-29 18:00:30 +02:00
|
|
|
|
2016-04-28 11:29:24 +02:00
|
|
|
# add a monkeysphere subkey
|
|
|
|
su -c "monkeysphere gen-subkey $MY_GPG_PUBLIC_KEY_ID" - $MY_USERNAME
|
2016-04-29 18:00:30 +02:00
|
|
|
# add authorized GPG email address
|
|
|
|
mkdir /home/$MY_USERNAME/.monkeysphere
|
|
|
|
chmod 755 /home/$MY_USERNAME/.monkeysphere
|
2016-06-05 15:51:25 +02:00
|
|
|
echo "$MY_USERNAME <$MY_USERNAME@$HOSTNAME>" > /home/$MY_USERNAME/.monkeysphere/authorized_user_ids
|
2016-04-29 20:47:48 +02:00
|
|
|
chmod 644 /home/$MY_USERNAME/.monkeysphere/authorized_user_ids
|
2016-04-29 18:00:30 +02:00
|
|
|
chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.monkeysphere
|
2016-11-19 23:43:46 +01:00
|
|
|
#monkeysphere-authentication update-users
|
2015-10-26 18:21:38 +01:00
|
|
|
|
2015-12-22 21:06:02 +01:00
|
|
|
if [ -f /home/$MY_USERNAME/.muttrc ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
# encrypt outgoing mail to the "sent" folder
|
|
|
|
if ! grep -q "pgp_encrypt_only_command" /home/$MY_USERNAME/.muttrc; then
|
2016-08-28 10:55:47 +02:00
|
|
|
echo '' >> /home/$MY_USERNAME/.muttrc
|
|
|
|
echo $'# Encrypt items in the Sent folder' >> /home/$MY_USERNAME/.muttrc
|
|
|
|
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/$MY_USERNAME/.muttrc
|
2016-07-24 14:19:56 +02:00
|
|
|
else
|
2016-08-28 10:55:47 +02:00
|
|
|
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/$MY_USERNAME/.muttrc
|
2016-07-24 14:19:56 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if ! grep -q "pgp_encrypt_sign_command" /home/$MY_USERNAME/.muttrc; then
|
2016-08-28 10:55:47 +02:00
|
|
|
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/$MY_USERNAME/.muttrc
|
2016-07-24 14:19:56 +02:00
|
|
|
else
|
2016-08-28 10:55:47 +02:00
|
|
|
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/$MY_USERNAME/.muttrc
|
2016-07-24 14:19:56 +02:00
|
|
|
fi
|
2015-10-26 20:16:26 +01:00
|
|
|
fi
|
|
|
|
|
2015-10-26 15:25:58 +01:00
|
|
|
if ! grep -q "Change your GPG password" /home/$MY_USERNAME/README; then
|
2016-07-24 14:19:56 +02:00
|
|
|
echo '' >> /home/$MY_USERNAME/README
|
|
|
|
echo '' >> /home/$MY_USERNAME/README
|
2016-10-28 13:44:48 +02:00
|
|
|
echo $'# Change your GPG password' >> /home/$MY_USERNAME/README
|
2016-07-24 14:19:56 +02:00
|
|
|
echo $"It's very important to add a password to your GPG key so that" >> /home/$MY_USERNAME/README
|
|
|
|
echo $"if anyone does get access to your email they still won't be able" >> /home/$MY_USERNAME/README
|
|
|
|
echo $'to read them without knowning the GPG password.' >> /home/$MY_USERNAME/README
|
|
|
|
echo $'You can change the it with:' >> /home/$MY_USERNAME/README
|
|
|
|
echo '' >> /home/$MY_USERNAME/README
|
|
|
|
echo " gpg --edit-key $MY_GPG_PUBLIC_KEY_ID" >> /home/$MY_USERNAME/README
|
|
|
|
echo ' passwd' >> /home/$MY_USERNAME/README
|
|
|
|
echo ' save' >> /home/$MY_USERNAME/README
|
|
|
|
echo ' quit' >> /home/$MY_USERNAME/README
|
2015-10-26 15:25:58 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
|
2015-10-26 18:21:38 +01:00
|
|
|
chown $MY_USERNAME:$MY_USERNAME $MY_GPG_PUBLIC_KEY
|
2015-10-26 15:25:58 +01:00
|
|
|
chmod 600 /home/$MY_USERNAME/README
|
|
|
|
|
2016-10-02 00:39:56 +02:00
|
|
|
echo $'Detecting installed apps...'
|
|
|
|
detect_apps
|
|
|
|
get_apps_installed_names
|
|
|
|
for app_name in "${APPS_INSTALLED_NAMES[@]}"
|
|
|
|
do
|
|
|
|
if [[ $(function_exists add_user_${app_name}) == "1" ]]; then
|
|
|
|
echo $"Adding user to ${app_name}"
|
2016-10-05 23:33:41 +02:00
|
|
|
app_load_variables ${app_name}
|
2016-10-02 00:39:56 +02:00
|
|
|
retval=$(add_user_${app_name} "$MY_USERNAME" "$NEW_USER_PASSWORD")
|
|
|
|
if [[ $retval != '0' ]]; then
|
|
|
|
echo $"Failed with error code ${retval}"
|
|
|
|
${PROJECT_NAME}-rmuser $MY_USERNAME
|
|
|
|
exit 672392
|
2016-08-28 10:55:47 +02:00
|
|
|
fi
|
2016-10-26 00:04:56 +02:00
|
|
|
if ! grep -q "${app_name}_${MY_USERNAME}" $APP_USERS_FILE; then
|
|
|
|
echo "${app_name}_${MY_USERNAME}" >> $APP_USERS_FILE
|
|
|
|
fi
|
2016-07-24 14:19:56 +02:00
|
|
|
fi
|
2016-10-02 00:39:56 +02:00
|
|
|
done
|
2016-02-27 17:14:44 +01:00
|
|
|
|
2016-03-05 19:43:01 +01:00
|
|
|
if [ -f /etc/nginx/.htpasswd ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
if ! grep "${MY_USERNAME}:" /etc/nginx/.htpasswd; then
|
2016-08-28 10:55:47 +02:00
|
|
|
echo "$NEW_USER_PASSWORD" | htpasswd -i -s /etc/nginx/.htpasswd $MY_USERNAME
|
2016-07-24 14:19:56 +02:00
|
|
|
fi
|
2016-03-05 19:43:01 +01:00
|
|
|
fi
|
|
|
|
|
2016-02-25 15:21:07 +01:00
|
|
|
# add user menu on ssh login
|
2016-02-25 15:24:08 +01:00
|
|
|
if ! grep -q 'control' /home/$MY_USERNAME/.bashrc; then
|
2016-07-24 14:19:56 +02:00
|
|
|
echo 'control' >> /home/$MY_USERNAME/.bashrc
|
2016-02-25 15:21:07 +01:00
|
|
|
fi
|
|
|
|
|
2016-11-19 22:23:21 +01:00
|
|
|
${PROJECT_NAME}-pass -u $MY_USERNAME -a login -p "$NEW_USER_PASSWORD"
|
2015-10-26 15:25:58 +01:00
|
|
|
clear
|
2015-11-27 16:29:43 +01:00
|
|
|
echo $"New user $MY_USERNAME was created"
|
|
|
|
echo $"Their login password is $NEW_USER_PASSWORD"
|
2015-10-26 15:25:58 +01:00
|
|
|
echo ''
|
2015-11-27 16:29:43 +01:00
|
|
|
echo $"They can download their GPG keys with:"
|
2015-10-26 15:25:58 +01:00
|
|
|
echo ''
|
|
|
|
echo " scp -P $SSH_PORT -r $MY_USERNAME@$HOSTNAME:/home/$MY_USERNAME/.gnupg ~/"
|
|
|
|
echo ''
|
2015-12-08 17:22:48 +01:00
|
|
|
echo $"They should also run ${PROJECT_NAME}-client on their system to ensure"
|
2015-11-27 16:29:43 +01:00
|
|
|
echo $'the best security.'
|
2015-10-26 15:25:58 +01:00
|
|
|
|
|
|
|
exit 0
|