Files
uberspace-webadmin/action.sh
2015-07-09 00:06:31 +03:00

205 lines
4.5 KiB
Bash
Executable File

########################################################################
# Copyright (C) 2015 Max Mehl <mail@mehl.mx>
########################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
#
# This script handles calls from submit.php.
# It checks the validity of usernames, executes basic command
# When password entries are required, it starts the respective python
# scripts
#
########################################################################
#!/bin/bash
# Test if config.cfg exists and set needed variables
if [ ! -e config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
source config.cfg
ACTION="$1" # adduser, changepw, listusers, userdetail, deluser
USER="$2"
PASS="$3"
## FUNCTIONS
function checkaction {
REGEX="^adduser$|^changepw$|^listusers$|^userdetail$|^deluser$"
if [[ $1 =~ $REGEX ]]; then
echo "true"
else
echo "false"
fi
}
function checkuser {
REGEX="^[A-Za-z0-9._+-]+$"
if [[ $1 =~ $REGEX ]]; then
echo "true"
else
echo "false"
fi
}
function checkpass {
REGEX="^[A-Za-z0-9.\"§$/%&\(\)=@:;#*_+-]+$"
if [[ $1 =~ $REGEX ]]; then
echo "true"
else
echo "false"
fi
}
function userexists {
STATUS=$(listvdomain | cut -d" " -f 1 | sed '1d' | grep -q "^$1$" ; echo $?)
if [ $STATUS == 0 ]; then
echo "true"
else
echo "false"
fi
}
function fappend {
echo -e "$2">>$1;
}
function mailsend {
TOEMAIL="$TOEMAIL";
FREMAIL="$FREMAIL";
SUBJECT="[$DOMAIN] $1";
MSGBODY1="$2"
MSGBODY2="$3"
TMP=`mktemp`
fappend $TMP "From: $FREMAIL";
fappend $TMP "To: $TOEMAIL";
fappend $TMP "Reply-To: $FREMAIL";
fappend $TMP "Subject: $SUBJECT";
fappend $TMP "";
fappend $TMP "$MSGBODY1";
fappend $TMP "";
fappend $TMP "$MSGBODY2";
fappend $TMP "";
cat $TMP | "$SENDMAILPATH" -t;
rm $TMP;
}
## FIRST CHECKS
if ! $(checkaction "$ACTION"); then
echo "No valid action chosen"
exit 1
fi
# # # # #
# ADDING USER
# # # # #
if [ "$ACTION" == "adduser" ]; then
echo "Adding new Email user..."
echo
if ! $(checkuser "$USER"); then
echo "Username \"$USER\" invalid"
exit 1
fi
if $(userexists "$USER"); then
echo "User \"$USER\" does already exist!"
exit 1
fi
if ! $(checkpass "$PASS"); then
echo "Password \"$PASS\" invalid"
exit 1
fi
python adduser.py "$USER" "$PASS"
mailsend "New Email account created" \
"A new Email account has been created." \
"User: $USER | Password: $PASS"
fi # /adduser
# # # # #
# CHANGE PASSWORD
# # # # #
if [ "$ACTION" == "changepw" ]; then
echo "Changing password of Email account..."
echo
if ! $(userexists "$USER"); then
echo "User \"$USER\" does not exist!"
exit 1
fi
if ! $(checkpass "$PASS"); then
echo "Password \"$PASS\" invalid"
exit 1
fi
python changepw.py "$USER" "$PASS"
mailsend "Email password changed" \
"An Email account password has been changed." \
"User: $USER | New Password: $PASS"
fi # /adduser
# # # # #
# DETAIL USER
# # # # #
if [ "$ACTION" == "listusers" ]; then
echo "Listing all Email accounts..."
echo
listvdomain | column -s $' ' -t
fi # /listusers
# # # # #
# USER DETAIL
# # # # #
if [ "$ACTION" == "userdetail" ]; then
echo "Extracting details of Email account..."
echo
if ! $(userexists "$USER"); then
echo "User \"$USER\" does not exist!"
exit 1
fi
dumpvuser "$USER" | column -s $' ' -t
fi # /userdetail
# # # # #
# DELETE USER
# # # # #
if [ "$ACTION" == "deluser" ]; then
echo "Extracting details of Email account..."
echo
if ! $(userexists "$USER"); then
echo "User \"$USER\" does not exist!"
exit 1
fi
vdeluser "$USER"
mailsend "Email account deleted" \
"An Email account has been deleted." \
"User: $USER"
fi # /deluser