Skip to content
Snippets Groups Projects
freedombone-adduser 10.7 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud

# Adds an user to the system

# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2015-2019 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
Bob Mottram's avatar
Bob Mottram committed
# it under the terms of the GNU Affero General Public License as published by
# 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
Bob Mottram's avatar
Bob Mottram committed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
Bob Mottram's avatar
Bob Mottram committed
# 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/>.
Bob Mottram's avatar
Bob Mottram committed
PROJECT_NAME='freedombone'

Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAIN=${PROJECT_NAME}-adduser
Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAINDIR="/usr/share/locale"

Bob Mottram's avatar
Bob Mottram committed
CONFIGURATION_FILE="$HOME/${PROJECT_NAME}.cfg"
Bob Mottram's avatar
Bob Mottram committed
UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
Bob Mottram's avatar
Bob Mottram committed
for f in $UTILS_FILES
do
Bob Mottram's avatar
Bob Mottram committed
    source "$f"
Bob Mottram's avatar
Bob Mottram committed
done

Bob Mottram's avatar
Bob Mottram committed
ADD_USERNAME=$1

if [[ $(is_valid_user "$ADD_USERNAME") == '0' ]]; then
    echo $'Invalid username'
    exit 50
fi

Bob Mottram's avatar
Bob Mottram committed
APP_FILES="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*"
Bob Mottram's avatar
Bob Mottram committed
for f in $APP_FILES
do
Bob Mottram's avatar
Bob Mottram committed
    source "$f"
Bob Mottram's avatar
Bob Mottram committed
done

Bob Mottram's avatar
Bob Mottram committed
SSH_PUBLIC_KEY="$2"
password_param=
if [[ "$2" == "password="* ]]; then
    SSH_PUBLIC_KEY=
    password_param=$(echo "$2" | awk -F '=' '{print $2}')
fi
Bob Mottram's avatar
Bob Mottram committed
GPG_KEYSERVER='hkp://keys.gnupg.net'
SSH_PORT=2222
Bob Mottram's avatar
Bob Mottram committed
COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
Bob Mottram's avatar
Bob Mottram committed
DEFAULT_DOMAIN_NAME=
Bob Mottram's avatar
Bob Mottram committed
if [ ! "$ADD_USERNAME" ]; then
Bob Mottram's avatar
Bob Mottram committed
    echo $'No username was given'
    exit 1
Bob Mottram's avatar
Bob Mottram committed
if [ -d "/home/$ADD_USERNAME" ]; then
    echo $"The user $ADD_USERNAME already exists"
Bob Mottram's avatar
Bob Mottram committed
    exit 2
Bob Mottram's avatar
Bob Mottram committed
if [ ! -f "$COMPLETION_FILE" ]; then
Bob Mottram's avatar
Bob Mottram committed
    echo $"$COMPLETION_FILE not found"
Bob Mottram's avatar
Bob Mottram committed
    userdel -r "$ADD_USERNAME"
Bob Mottram's avatar
Bob Mottram committed
    exit 3
# shellcheck disable=SC2126
no_of_users=$(find /home/ -maxdepth 1 -type d  | grep -Fxv "/home/znc" | grep -Fxv "/home/turtl" | grep -Fxv "/home/go" | grep -Fxv "/home/gogs" | grep -Fxv "/home/git" | grep -Fxv "/home/pihole" | grep -Fxv "/home/" | grep -Fxv "/home/tahoelafs" | grep -Fxv "/home/sync" | wc -l)

# shellcheck disable=SC2086
Bob Mottram's avatar
Bob Mottram committed
if [ $no_of_users -ge $MAXIMUM_USERS ]; then
    echo $"Maximum number of users reached"
    exit 4
fi

# Minimum number of characters in a password
Bob Mottram's avatar
Bob Mottram committed
MINIMUM_PASSWORD_LENGTH=$(grep 'MINIMUM_PASSWORD_LENGTH=' "/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-passwords" | head -n 1 | awk -F '=' '{print $2}')
if [ ! "$password_param" ]; then
    NEW_USER_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
else
    NEW_USER_PASSWORD="$password_param"
fi
chmod 600 /etc/shadow
chmod 600 /etc/gshadow
Bob Mottram's avatar
Bob Mottram committed
useradd -m -p "$NEW_USER_PASSWORD" -s /bin/bash "$ADD_USERNAME"
adduser "$ADD_USERNAME" sasl
groupadd "$ADD_USERNAME"
chmod 0000 /etc/shadow
chmod 0000 /etc/gshadow
Bob Mottram's avatar
Bob Mottram committed
if [ ! -d "/home/$ADD_USERNAME" ]; then
Bob Mottram's avatar
Bob Mottram committed
    echo $'Home directory was not created'
    exit 4
Bob Mottram's avatar
Bob Mottram committed
if [ "$SSH_PUBLIC_KEY" ]; then
Bob Mottram's avatar
Bob Mottram committed
    if [ ${#SSH_PUBLIC_KEY} -gt 5 ]; then
        if [ -f "$SSH_PUBLIC_KEY" ]; then
Bob Mottram's avatar
Bob Mottram committed
            mkdir "/home/$ADD_USERNAME/.ssh"
            cp "$SSH_PUBLIC_KEY" "/home/$ADD_USERNAME/.ssh/authorized_keys"
            chown -R "$ADD_USERNAME":"$ADD_USERNAME" "/home/$ADD_USERNAME/.ssh"
            echo $'ssh public key installed'
        else
Bob Mottram's avatar
Bob Mottram committed
            if [[ "$SSH_PUBLIC_KEY" == 'ssh-'* ]]; then
Bob Mottram's avatar
Bob Mottram committed
                mkdir "/home/$ADD_USERNAME/.ssh"
                echo "$SSH_PUBLIC_KEY" > "/home/$ADD_USERNAME/.ssh/authorized_keys"
                chown -R "$ADD_USERNAME":"$ADD_USERNAME" "/home/$ADD_USERNAME/.ssh"
                echo $'ssh public key installed'
            else
                echo $'The second parameter does not look like an ssh key'
                exit 5
            fi
        fi
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
if [ -d "/home/$ADD_USERNAME/Maildir" ]; then
    if grep -q "set from=" "/home/$ADD_USERNAME/.muttrc"; then
        sed -i "s|set from=.*|set from='$ADD_USERNAME <$ADD_USERNAME@$HOSTNAME>'|g" "/home/$ADD_USERNAME/.muttrc"
Bob Mottram's avatar
Bob Mottram committed
    else
Bob Mottram's avatar
Bob Mottram committed
        echo "set from='$ADD_USERNAME <$ADD_USERNAME@$HOSTNAME>'" >> "/home/$ADD_USERNAME/.muttrc"
Bob Mottram's avatar
Bob Mottram committed
    fi

Bob Mottram's avatar
Bob Mottram committed
    sed -i "s|\$USER@|$ADD_USERNAME@|g" "/home/$ADD_USERNAME/.procmailrc"
Bob Mottram's avatar
Bob Mottram committed
# generate a gpg key
echo "Making a GPG key for $ADD_USERNAME@$HOSTNAME"
Bob Mottram's avatar
Bob Mottram committed
mkdir "/home/$ADD_USERNAME/.gnupg"
if [[ "$GPG_KEYSERVER" != 'hkps://hkps.pool.sks-keyservers.net' ]]; then
    { echo "keyserver $GPG_KEYSERVER";
      echo 'keyserver hkps://hkps.pool.sks-keyservers.net'; } >> "/home/$ADD_USERNAME/.gnupg/gpg.conf"
else
      echo 'keyserver hkps://hkps.pool.sks-keyservers.net' >> "/home/$ADD_USERNAME/.gnupg/gpg.conf"
fi

{ echo 'keyserver-options auto-key-retrieve';
Bob Mottram's avatar
Bob Mottram committed
  echo '';
  echo '# default preferences';
  echo 'personal-digest-preferences SHA256';
  echo 'cert-digest-algo SHA256';
  echo 'default-preference-list SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP Uncompressed'; } >> "/home/$ADD_USERNAME/.gnupg/gpg.conf"

chown -R "$ADD_USERNAME":"$ADD_USERNAME" "/home/$ADD_USERNAME/.gnupg"
chmod 700 "/home/$ADD_USERNAME/.gnupg"
Bob Mottram's avatar
Bob Mottram committed
chmod 600 "/home/$ADD_USERNAME/.gnupg/"*
Bob Mottram's avatar
Bob Mottram committed

# Generate a GPG key
Bob Mottram's avatar
Bob Mottram committed
{ echo 'Key-Type: eddsa';
  echo 'Key-Curve: Ed25519';
  echo 'Subkey-Type: eddsa';
  echo "Name-Real:  $ADD_USERNAME";
  echo "Name-Email: $ADD_USERNAME@$HOSTNAME";
  echo 'Expire-Date: 0';
  echo "Passphrase: $NEW_USER_PASSWORD"; } > "/home/$ADD_USERNAME/gpg-genkey.conf"
chown "$ADD_USERNAME":"$ADD_USERNAME" "/home/$ADD_USERNAME/gpg-genkey.conf"
su -m root -c "gpg --homedir /home/$ADD_USERNAME/.gnupg --batch --full-gen-key /home/$ADD_USERNAME/gpg-genkey.conf" - "$ADD_USERNAME"
chown -R "$ADD_USERNAME":"$ADD_USERNAME" "/home/$ADD_USERNAME/.gnupg"
Bob Mottram's avatar
Bob Mottram committed
rm "/home/$ADD_USERNAME/gpg-genkey.conf"
MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$ADD_USERNAME" "$ADD_USERNAME@$HOSTNAME")
Bob Mottram's avatar
Bob Mottram committed
MY_GPG_PUBLIC_KEY="/home/$ADD_USERNAME/public_key.gpg"
su -m root -c "gpg --output $MY_GPG_PUBLIC_KEY --armor --export $MY_GPG_PUBLIC_KEY_ID" - "$ADD_USERNAME"
Bob Mottram's avatar
Bob Mottram committed
if [ ! -f "$MY_GPG_PUBLIC_KEY" ]; then
    echo "GPG public key was not generated for $ADD_USERNAME@$HOSTNAME $MY_GPG_PUBLIC_KEY_ID"
Bob Mottram's avatar
Bob Mottram committed
    userdel -r "$ADD_USERNAME"
Bob Mottram's avatar
Bob Mottram committed
    exit 7
Bob Mottram's avatar
Bob Mottram committed
gpg_agent_setup "$ADD_USERNAME"
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
if [ -f "/home/$ADD_USERNAME/.muttrc" ]; then
Bob Mottram's avatar
Bob Mottram committed
    # encrypt outgoing mail to the "sent" folder
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "pgp_encrypt_only_command" "/home/$ADD_USERNAME/.muttrc"; then
        { echo '';
          echo $'# Encrypt items in the Sent folder';
          echo "set pgp_encrypt_only_command=\"/usr/lib/mutt/pgpewrap gpg --batch --quiet --no-verbose --output - --encrypt --textmode --armor --always-trust --encrypt-to $MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\""; } >> "/home/$ADD_USERNAME/.muttrc"
Bob Mottram's avatar
Bob Mottram committed
    else
Bob Mottram's avatar
Bob Mottram committed
        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 $MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\"|g" "/home/$ADD_USERNAME/.muttrc"
Bob Mottram's avatar
Bob Mottram committed
    fi

Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "pgp_encrypt_sign_command" "/home/$ADD_USERNAME/.muttrc"; then
        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 $MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\"" >> "/home/$ADD_USERNAME/.muttrc"
Bob Mottram's avatar
Bob Mottram committed
    else
Bob Mottram's avatar
Bob Mottram committed
        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 $MY_GPG_PUBLIC_KEY_ID -- -r %r -- %f\"|g" "/home/$ADD_USERNAME/.muttrc"
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
chown "$ADD_USERNAME":"$ADD_USERNAME" "$MY_GPG_PUBLIC_KEY"
Bob Mottram's avatar
Bob Mottram committed
echo $'Detecting installed apps...'
detect_apps
get_apps_installed_names
Bob Mottram's avatar
Bob Mottram committed
# shellcheck disable=SC2068
for app_name in ${APPS_INSTALLED_NAMES[@]}
Bob Mottram's avatar
Bob Mottram committed
do
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(function_exists "add_user_${app_name}") == "1" ]]; then
Bob Mottram's avatar
Bob Mottram committed
        echo $"Adding user to ${app_name}"
Bob Mottram's avatar
Bob Mottram committed
        app_load_variables "${app_name}"
        retval=$("add_user_${app_name}" "$ADD_USERNAME" "$NEW_USER_PASSWORD")
        retval_last_value=$(echo "$retval" | tail -n 1)
        if [[ $retval_last_value != '0' ]]; then
            echo $"Failed with error code ${retval_last_value}"
Bob Mottram's avatar
Bob Mottram committed
            "${PROJECT_NAME}-rmuser" "$ADD_USERNAME" --force
Bob Mottram's avatar
Bob Mottram committed
        if ! grep -q "${app_name}_${ADD_USERNAME}" "$APP_USERS_FILE"; then
            echo "${app_name}_${ADD_USERNAME}" >> "$APP_USERS_FILE"
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
done
if [ -f /etc/nginx/.htpasswd ]; then
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "${ADD_USERNAME}:" /etc/nginx/.htpasswd; then
Bob Mottram's avatar
Bob Mottram committed
        echo "$NEW_USER_PASSWORD" | htpasswd -i -s /etc/nginx/.htpasswd "$ADD_USERNAME"
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
# add user menu on ssh login
Bob Mottram's avatar
Bob Mottram committed
if ! grep -q 'controluser' "/home/$ADD_USERNAME/.bashrc"; then
    echo 'export PS1="\W \$"' >> "/home/$ADD_USERNAME/.bashrc"
Bob Mottram's avatar
Bob Mottram committed
    echo 'controluser' >> "/home/$ADD_USERNAME/.bashrc"
Bob Mottram's avatar
Bob Mottram committed
fi

# fix some gpg strangeness when searching for keys
Bob Mottram's avatar
Bob Mottram committed
printf '%%Assuan%%\nsocket=/dev/shm/S.dirmngr\n' > "/home/$ADD_USERNAME/.gnupg/S.dirmngr"
if [ -d "/home/$ADD_USERNAME/.gnupg/crls.d" ]; then
    chmod +x "/home/$ADD_USERNAME/.gnupg/crls.d"
Bob Mottram's avatar
Bob Mottram committed
"${PROJECT_NAME}-pass" -u "$ADD_USERNAME" -a login -p "$NEW_USER_PASSWORD"
Bob Mottram's avatar
Bob Mottram committed
gpg_agent_enable "$ADD_USERNAME"
echo "Updating web admin for $ADD_USERNAME"
web_admin_create_users

# create qrcode for the user's public key
local_hostname=$(grep 'host-name' /etc/avahi/avahi-daemon.conf | awk -F '=' '{print $2}').local
webadmin_install_dir="/var/www/${local_hostname}/htdocs/admin"
if [ -d "$webadmin_install_dir" ]; then
Bob Mottram's avatar
Bob Mottram committed
    #pubkey_qrcode="$webadmin_install_dir/images/userprofile_${ADD_USERNAME}.png"
    #su -c "gpg --armor --export \"$MY_GPG_PUBLIC_KEY_ID\"" - "$ADD_USERNAME" | qrencode -t PNG -o "$pubkey_qrcode"

    xmpp_qrcode="$webadmin_install_dir/images/userprofile_${ADD_USERNAME}_xmpp.png"
    if [ ! -f /usr/local/bin/myqr ]; then
        echo -n "${ADD_USERNAME}@${HOSTNAME}" | qrencode -t PNG -o "$xmpp_qrcode"
    else
Bob Mottram's avatar
Bob Mottram committed
        myqr "${ADD_USERNAME}@${HOSTNAME}" -p /root/freedombone/img/android-app/xmpp.png -c -v 8 -n "$xmpp_qrcode"
echo "Notifying"
${PROJECT_NAME}-notification -m $"A new user was added: $ADD_USERNAME $(date)" -s $"[${PROJECT_NAME}] New user added"
Bob Mottram's avatar
Bob Mottram committed
clear
echo $"New user $ADD_USERNAME was created"
Bob Mottram's avatar
Bob Mottram committed
#echo $"Password: $NEW_USER_PASSWORD"
Bob Mottram's avatar
Bob Mottram committed
echo ''
echo $"They can download their GPG keys with:"
Bob Mottram's avatar
Bob Mottram committed
echo ''
echo "    scp -P $SSH_PORT -r $ADD_USERNAME@$HOSTNAME:/home/$ADD_USERNAME/.gnupg ~/"
Bob Mottram's avatar
Bob Mottram committed
echo ''
Bob Mottram's avatar
Bob Mottram committed
echo $"They should also run ${PROJECT_NAME}-client on their system to ensure"
echo $'the best security.'