######################################################################## # Copyright (C) 2015 Max Mehl ######################################################################## # # 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 . # ######################################################################## # # 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 USER="$2" PASS="$3" SYSUSER=$(whoami) NOTES="$HOME/$SYSUSER-Notes.txt" NOTESTMP=.Notes.txt.tmp ## FUNCTIONS function checkaction { REGEX="^adduser$|^changepw$|^listusers$|^userdetail$|^deluser$|^sizeall$|^sizeuser$" 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" if [ $? == 0 ]; then # Send infomail mailsend "New Email account created" \ "A new Email account has been created." \ "User: $USER | Password: $PASS" # Update datasheet (add new entry in Email section) TAIL='## < EMAIL' sed -i "/$TAIL/i User: $USER\nPass: $PASS\n" $NOTES 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" "$PASS" mailsend "Email password changed" \ "An Email account password has been changed." \ "User: $USER | New Password: $PASS" 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 # # # # # # 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" mailsend "Email account deleted" \ "An Email account has been deleted." \ "User: $USER" fi # /deluser