342 lines
7.7 KiB
Bash
Executable File
342 lines
7.7 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, sizeall, sizeuser, viewdata
|
|
USER=$2
|
|
PASSFILE=$3 # $3 is a file containing the password
|
|
PASS=$(cat "$3")
|
|
|
|
## FUNCTIONS
|
|
function checkaction {
|
|
REGEX="^adduser$|^changepw$|^listusers$|^userdetail$|^deluser$|^sizeall$|^sizeuser$|^viewdata$"
|
|
if [[ $1 =~ $REGEX ]]; then
|
|
echo "true"
|
|
else
|
|
echo "false"
|
|
fi
|
|
}
|
|
|
|
function checkuser {
|
|
REGEX="^[A-Za-z0-9._+-]+$" # Allowed symbols
|
|
if [[ $1 =~ $REGEX ]]; then
|
|
echo "true"
|
|
else
|
|
echo "false"
|
|
fi
|
|
}
|
|
|
|
function checkpass {
|
|
REGEX="[ 'x\\]" # Not allowed symbols
|
|
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 mailsend {
|
|
TOEMAIL="$TOEMAIL";
|
|
FREMAIL="$FREMAIL";
|
|
SUBJECT="[$DOMAIN] $1";
|
|
MSGBODY1="$2"
|
|
MSGBODY2="$3"
|
|
|
|
printf '%s\n' "From: $FREMAIL
|
|
To: $TOEMAIL
|
|
Reply-To: $FREMAIL
|
|
Subject: $SUBJECT
|
|
|
|
$MSGBODY1
|
|
|
|
$MSGBODY2
|
|
" > $MAILTMP
|
|
cat $MAILTMP | "$SENDMAILPATH" -t;
|
|
rm $MAILTMP;
|
|
}
|
|
|
|
function mailsendenc {
|
|
|
|
if [ ! -e $SSLKEY ]; then
|
|
#echo "Encryption key \"$SSLKEY\" is not available. Aborting."
|
|
#exit 1
|
|
|
|
openssl genrsa -out $SSLKEY 2048
|
|
fi
|
|
|
|
TOEMAIL="$TOEMAIL";
|
|
FREMAIL="$FREMAIL";
|
|
SUBJECT="[$DOMAIN] $1";
|
|
MSGBODY1="$2"
|
|
BOUNDARY="ZZafgwejwepfgkl.9453x1q"
|
|
ATTACHMENT=$(echo $3 | openssl rsautl -inkey $SSLKEY -encrypt | base64)
|
|
|
|
printf '%s\n' "From: $FREMAIL
|
|
To: $TOEMAIL
|
|
Reply-To: $FREMAIL
|
|
Subject: $SUBJECT
|
|
Mime-Version: 1.0
|
|
Content-Type: multipart/mixed; boundary=\"$BOUNDARY\"
|
|
|
|
--${BOUNDARY}
|
|
Content-Type: text/plain; charset=\"us-ascii\"
|
|
Content-Transfer-Encoding: 7bit
|
|
Content-Disposition: inline
|
|
|
|
$MSGBODY1
|
|
|
|
Upload the attached encrypted file to your Account Administration Panel
|
|
in order to see sensitive details. Please visit the section \"Decrypt
|
|
system email\" for more details.
|
|
|
|
--${BOUNDARY}
|
|
Content-Type: text/plain
|
|
Content-Transfer-Encoding: 7bit
|
|
Content-Disposition: attachment; filename=\"message.txt.crypt\"
|
|
|
|
$ATTACHMENT
|
|
|
|
--${BOUNDARY}
|
|
|
|
" > $MAILTMP
|
|
cat $MAILTMP | "$SENDMAILPATH" -t;
|
|
rm $MAILTMP;
|
|
}
|
|
|
|
function notesdelete { # $1=$USER
|
|
USER="$1"
|
|
# Extract Mail part | exclude LEAD and TAIL | delete user
|
|
sed -n "/$LEAD/,/$TAIL/ p" $NOTES | grep -v "$LEAD\|$TAIL" | sed "/User:[ \t]*$USER$/,+2d" > $NOTESTMP
|
|
# Put edited part in between $LEAD and $TAIL again
|
|
sed -i "/$LEAD/,/$TAIL/{ /$LEAD/{p; r $NOTESTMP
|
|
}; /$TAIL/p; d }" $NOTES
|
|
rm $NOTESTMP
|
|
}
|
|
|
|
function notesinsert {
|
|
# Update datasheet (add new entry in Email section)
|
|
USER=$1
|
|
PASSFILE=$2
|
|
|
|
# Create temporary file from $PASSFILE
|
|
sed -E 's/(.*)/User: $USER\nPass: \1/' $PASSFILE > .$PASSFILE.tmp
|
|
|
|
# Insert this edited file into the datasheet
|
|
sed -i "/$TAIL/ {
|
|
h
|
|
r .$PASSFILE.tmp
|
|
g
|
|
N
|
|
}" $NOTES
|
|
|
|
#rm .$PASSFILE.tmp
|
|
|
|
# sed -i "/$TAIL/i User: $USER\nPass: $PASS\n" $NOTES
|
|
}
|
|
|
|
## 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" "$PASSFILE"
|
|
|
|
if [ $? == 0 ]; then
|
|
# Send infomail
|
|
$MAILTYPE "New Email account created" \
|
|
"A new Email account has been created." \
|
|
"User: $USER | Password: $PASS"
|
|
|
|
LEAD='## > EMAIL'
|
|
TAIL='## < EMAIL'
|
|
notesinsert "$USER" "$PASSFILE"
|
|
|
|
fi
|
|
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" "$PASSFILE"
|
|
|
|
if [ $? == 0 ]; then
|
|
# Send infomail
|
|
$MAILTYPE "Email password changed" \
|
|
"An Email account password has been changed." \
|
|
"User: $USER | New Password: $PASS"
|
|
|
|
# Update datasheet (delete entry in Email section and add a new one with the new password)
|
|
# In fact a combination of deluser and adduser
|
|
LEAD='## > EMAIL'
|
|
TAIL='## < EMAIL'
|
|
|
|
notesdelete "$USER"
|
|
notesinsert "$USER" "$PASSFILE"
|
|
|
|
fi
|
|
fi # /changepw
|
|
|
|
# # # # #
|
|
# LIST USERS
|
|
# # # # #
|
|
if [ "$ACTION" == "listusers" ]; then
|
|
echo "Listing all Email accounts..."
|
|
echo
|
|
|
|
listvdomain | column -s $' ' -t
|
|
|
|
fi # /listusers
|
|
|
|
# # # # #
|
|
# SIZE ALL USERS
|
|
# # # # #
|
|
if [ "$ACTION" == "sizeall" ]; then
|
|
echo "Calculate total size of all Email accounts..."
|
|
echo
|
|
|
|
du -sBM ~/users/* | sed -e "s:/home/$SYSUSER/users/::g"
|
|
|
|
fi # /sizeall
|
|
|
|
# # # # #
|
|
# VIEW DATASHEET
|
|
# # # # #
|
|
if [ "$ACTION" == "viewdata" ]; then
|
|
echo "Extracting data sheet..."
|
|
echo
|
|
|
|
cat $NOTES
|
|
|
|
fi # /viewdata
|
|
|
|
# # # # #
|
|
# SIZE USER
|
|
# # # # #
|
|
if [ "$ACTION" == "sizeuser" ]; then
|
|
echo "Calculate size of all folders of an Email account..."
|
|
echo
|
|
|
|
# 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
|
|
|
|
fi # /sizeuser
|
|
|
|
# # # # #
|
|
# 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"
|
|
|
|
if [ $? == 0 ]; then
|
|
# Send infomail
|
|
$MAILTYPE "Email account deleted" \
|
|
"An Email account has been deleted." \
|
|
"User: $USER"
|
|
|
|
# Update datasheet (delete entry in Email section)
|
|
LEAD='## > EMAIL'
|
|
TAIL='## < EMAIL'
|
|
|
|
notesdelete "$USER"
|
|
|
|
fi
|
|
fi # /deluser
|