Files
uberspace-webadmin/action.sh

240 lines
5.5 KiB
Bash
Raw Normal View History

2015-07-09 00:06:31 +03:00
########################################################################
# 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
2015-07-09 00:26:45 +03:00
ACTION="$1" # adduser, changepw, listusers, userdetail, deluser, sizeall, sizeuser
2015-07-09 00:06:31 +03:00
USER="$2"
PASS="$3"
2015-07-10 09:28:49 +03:00
SYSUSER=$(whoami)
2015-07-10 09:36:00 +03:00
NOTES="$HOME/$SYSUSER-Notes.txt"
2015-07-10 09:28:49 +03:00
NOTESTMP=.Notes.txt.tmp
2015-07-09 00:06:31 +03:00
## FUNCTIONS
function checkaction {
2015-07-09 00:26:45 +03:00
REGEX="^adduser$|^changepw$|^listusers$|^userdetail$|^deluser$|^sizeall$|^sizeuser$"
2015-07-09 00:06:31 +03:00
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
2015-07-10 09:28:49 +03:00
2015-07-09 00:06:31 +03:00
# # # # #
# 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"
2015-07-10 09:28:49 +03:00
if [ $? == 0 ]; then
# Send infomail
mailsend "New Email account created" \
2015-07-09 00:06:31 +03:00
"A new Email account has been created." \
"User: $USER | Password: $PASS"
2015-07-10 09:28:49 +03:00
# Update datasheet (add new entry in Email section)
TAIL='## < EMAIL'
sed -i "/$TAIL/i User: $USER\nPass: $PASS\n" $NOTES
2015-07-09 00:06:31 +03:00
2015-07-10 09:28:49 +03:00
fi
2015-07-09 00:06:31 +03:00
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"
2015-07-10 09:28:49 +03:00
fi # /changepw
2015-07-09 00:06:31 +03:00
# # # # #
2015-07-09 00:26:45 +03:00
# LIST USERS
2015-07-09 00:06:31 +03:00
# # # # #
if [ "$ACTION" == "listusers" ]; then
echo "Listing all Email accounts..."
echo
listvdomain | column -s $' ' -t
fi # /listusers
2015-07-09 00:26:45 +03:00
# # # # #
# SIZE ALL USERS
# # # # #
if [ "$ACTION" == "sizeall" ]; then
echo "Calculate total size of all Email accounts..."
echo
2015-07-09 01:01:15 +03:00
du -sBM ~/users/* | sed -e "s:/home/$SYSUSER/users/::g"
2015-07-09 00:26:45 +03:00
fi # /sizeall
# # # # #
# SIZE USER
# # # # #
if [ "$ACTION" == "sizeuser" ]; then
echo "Calculate size of all folders of an Email account..."
echo
2015-07-09 01:01:15 +03:00
# Show size in MB, strip long paths, strip tmp and new folders, rename .INBOX cur-folder
du -BM ~/users/"$USER" | sed -e "s:/home/$SYSUSER/users/$USER/::g" | grep -v "/cur$\|new$\|tmp$" | sed "s:cur$:.INBOX:" | grep -v "/home/$SYSUSER/users/$USER" | sort -nr
2015-07-09 00:26:45 +03:00
fi # /sizeuser
2015-07-09 00:06:31 +03:00
# # # # #
# 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