diff --git a/action.sh b/action.sh index ad40a50..837c9f7 100755 --- a/action.sh +++ b/action.sh @@ -3,28 +3,28 @@ ######################################################################## # Copyright (C) 2016 Max Mehl ######################################################################## -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the +# +# This program is free software: you can redistribute it and/or modify +# 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 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# +# 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 Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public -# License along with this program. If not, see +# +# You should have received a copy of the GNU Affero 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 -# +# ######################################################################## # Test if config.cfg exists and set needed variables @@ -40,8 +40,8 @@ PATH=$PATH:$HOME/bin ## FUNCTIONS function checkaction { REGEX="^adduser$|^changepw$|^listusers$|^userdetail$|^deluser$|^sizeall$|^sizeuser$|^viewdata$|^addalias$|^quota$|^installwp$|^uninstallwp$" - if [[ $1 =~ $REGEX ]]; then - echo "true" + if [[ $1 =~ $REGEX ]]; then + echo "true" else echo "false" fi @@ -49,8 +49,8 @@ function checkaction { function checkuser { REGEX="^[A-Za-z0-9._+-]+$" # Allowed symbols - if [[ $1 =~ $REGEX ]]; then - echo "true" + if [[ $1 =~ $REGEX ]]; then + echo "true" else echo "false" fi @@ -58,8 +58,8 @@ function checkuser { function checkpass { REGEX="[ '\\]" # Not allowed symbols - if [[ $(grep -E "$REGEX" $1 ; echo $?) == 1 ]]; then - echo "true" + if [[ $(grep -E "$REGEX" $1 ; echo $?) == 1 ]]; then + echo "true" else echo "false" fi @@ -74,7 +74,7 @@ function userexists { fi } -function mailsend { +function mailsend { TOEMAIL="$TOEMAIL"; FREMAIL="$FREMAIL"; SUBJECT="[$DOMAIN] $1"; @@ -95,14 +95,14 @@ $MSGBODY2 } 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"; @@ -124,8 +124,8 @@ 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 +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} @@ -142,7 +142,7 @@ $ATTACHMENT rm $MAILTMP; } -function notesdelete { +function notesdelete { 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 @@ -156,10 +156,10 @@ 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\n\n/" $PASSFILE > .$PASSFILE.tmp - + # Insert this edited file into the datasheet sed -i "/$TAIL/ { h @@ -184,34 +184,34 @@ fi 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 "$PASSFILE"); then echo "Password \""$(cat $PASSFILE)"\" 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" - + LEAD='## > EMAIL' TAIL='## < EMAIL' notesinsert "$USER" "$PASSFILE" - + fi fi # /adduser @@ -221,26 +221,26 @@ fi # /adduser if [ "$ACTION" == "addalias" ]; then echo "Extracting details of Email account..." echo - + DEST=$PASS - + 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 ! $(userexists "$DEST"); then echo "Destination account \"$DEST\" does not exist!" exit 1 fi - + vaddalias $USER $DEST - + fi # /addalias # # # # # @@ -249,19 +249,19 @@ fi # /addalias 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 "$PASSFILE"); then echo "Password \""$(cat $PASSFILE)"\" invalid" exit 1 fi - + python changepw.py "$USER" "$PASSFILE" - + if [ $? == 0 ]; then # Send infomail $MAILTYPE "Email password changed" \ @@ -272,10 +272,10 @@ if [ "$ACTION" == "changepw" ]; then # In fact a combination of deluser and adduser LEAD='## > EMAIL' TAIL='## < EMAIL' - - notesdelete "$USER" + + notesdelete "$USER" notesinsert "$USER" "$PASSFILE" - + fi fi # /changepw @@ -285,9 +285,9 @@ fi # /changepw if [ "$ACTION" == "listusers" ]; then echo "Listing all Email accounts..." echo - + listvdomain | column -s $' ' -t - + fi # /listusers # # # # # @@ -296,9 +296,9 @@ fi # /listusers 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 # # # # # @@ -307,9 +307,9 @@ fi # /sizeall if [ "$ACTION" == "viewdata" ]; then echo "Extracting data sheet..." echo - + cat $NOTES - + fi # /viewdata # # # # # @@ -318,10 +318,10 @@ fi # /viewdata 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 # # # # # @@ -330,14 +330,14 @@ fi # /sizeuser 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 # # # # # @@ -346,26 +346,26 @@ fi # /userdetail if [ "$ACTION" == "quota" ]; then echo "Calculating account disk usage..." echo - - USAGE=$(quota -gsl | tail -n 1 | awk -F" " '{ print $2 }') + + USAGE=$(quota -gsl | tail -n 1 | awk -F" " '{ print $2 }' | sed 's/[A-Za-z]//g') QUOTA=$(quota -gsl | tail -n 1 | awk -F" " '{ print $3 }' | sed 's/[A-Za-z]//g') - + # If smaller than 1M, set usage to 1M to avoid miscalcuations - if [ $(echo $USAGE | grep -q "[A-Za-z]$" ; echo $?) != 0 ]; then + if ! echo $USAGE | grep -q "[A-Za-z]$"; then USAGE="1" else USAGE=$(echo $USAGE | sed 's/[A-Za-z]//g') fi - + PERC=$(echo "scale=2; $USAGE/$QUOTA" | bc) PERC=$(echo "scale=2; $PERC*100" | bc) - + echo "$USAGE MB of $QUOTA MB are used ($PERC %)." - + PERC=$(echo "scale=2; 100 - $PERC" | bc) - + echo "You have "$[$QUOTA - $USAGE]" MB free ($PERC %)." - + fi # /quota # # # # # @@ -374,26 +374,26 @@ fi # /quota 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" - + + notesdelete "$USER" + fi fi # /deluser @@ -403,15 +403,15 @@ fi # /deluser if [ "$ACTION" == "installwp" ]; then echo "Installing WordPress..." echo - + # $USER: Username for Wordpress # $PASS: Email address for Wordpress user - + if ! $(checkuser "$USER"); then echo "Username \"$USER\" invalid" exit 1 fi - + WEBDIR=$HOME/html WPUSER=$USER WPPASS=$(apg -n 1 -M NCL -m 14) @@ -434,7 +434,7 @@ if [ "$ACTION" == "installwp" ]; then echo "The default database \"${USER}_wp\" already exists." exit 1 fi - + # Downloading wordpress wget $WPDL unzip $(basename $WPDL) @@ -448,7 +448,7 @@ if [ "$ACTION" == "installwp" ]; then # Create database mysql -e "CREATE DATABASE $MYSQLDB;" - + # Use wp-cli to create config.php and install WP wp-cli core config --dbname=${MYSQLDB} --dbpass=${MYSQLPASS} --dbuser=${MYSQLUSER} --path=${WEBDIR} wp-cli core install --url="$WPDOMAIN" --title="CHANGE THIS TITLE" --admin_user=${WPUSER} --admin_password=${WPPASS} --admin_email=${WPMAIL} --path=${WEBDIR} @@ -456,12 +456,12 @@ if [ "$ACTION" == "installwp" ]; then # Update datasheet TAIL='## < WORDPRESS' sed -i "/$TAIL/i User: $WPUSER\nPass: $WPPASS\n" $NOTES - + echo echo "Wordpress successfully installed to $WPDOMAIN" echo "You can login on $WPDOMAIN/wp-login.php" echo "Please take a look into your data sheet for the login data." - + fi # /installwp # # # # # @@ -470,15 +470,15 @@ fi # /installwp if [ "$ACTION" == "uninstallwp" ]; then echo "Uninstalling WordPress..." echo - + # $USER: Username for Wordpress # $PASS: Email address for Wordpress user - + WEBDIR=$HOME/html MYSQLUSER=$SYSUSER MYSQLDB=${MYSQLUSER}_wp - + # Delete database and content of ~/htmp mysql -e "DROP DATABASE ${MYSQLDB};" rm -rf $WEBDIR/* @@ -487,10 +487,10 @@ if [ "$ACTION" == "uninstallwp" ]; then # Update datasheet LEAD='## > WORDPRESS' TAIL='## < WORDPRESS' - + notesdelete ".*" - + echo echo "Wordpress successfully uninstalled." - + fi # /uninstallwp