Compare commits

2 Commits

Author SHA1 Message Date
868d131972 fix quota 2021-12-18 13:03:21 +01:00
81fc921e3f fix quota calc 2021-12-18 12:01:20 +01:00
3 changed files with 102 additions and 93 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
.htaccess .htaccess
.htpasswd .htpasswd
config.cfg config.cfg
quota

View File

@@ -25,9 +25,17 @@ It's recommended to install UAAP automatically by using another script of mine,
### Manual method (not recommended, needs more work) ### Manual method (not recommended, needs more work)
Download the git repository by using Download the git repository by using
`git clone http://src.mehl.mx/git/uberspace-setup.git` `git clone http://src.mehl.mx/git/uberspace-webadmin.git`
I assume it's best to place these files in a subdomain. I used `panel.example.com` for that. I assume it's best to place these files in a subdomain. I used `panel.example.com` for that.
Now open the files `config.cfg.sample`, `.htaccess.sample`, and `.htpasswd.sample` and change all necessary settings. Be sure to remove the `.sample` in the file names of these files afterwards. Now open the files `config.cfg.sample`, `.htaccess.sample`, and `.htpasswd.sample` and change all necessary settings. Be sure to remove the `.sample` in the file names of these files afterwards.
### Other steps
On Uberspace 7, you'll need to create a cronjob to provide quota information because of SELinux limitations. To do so, edit your crontab with `crontab -e` and insert:
```
* * * * * quota -gsl > /var/www/virtual/MYUSERNAME/panel.example.com/quota
```

182
action.sh
View File

@@ -3,28 +3,28 @@
######################################################################## ########################################################################
# Copyright (C) 2016 Max Mehl <mail [at] mehl [dot] mx> # Copyright (C) 2016 Max Mehl <mail [at] mehl [dot] mx>
######################################################################## ########################################################################
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the # published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. # License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details. # GNU Affero General Public License for more details.
# #
# You should have received a copy of the GNU Affero General Public # You should have received a copy of the GNU Affero General Public
# License along with this program. If not, see # License along with this program. If not, see
# <http://www.gnu.org/licenses/>. # <http://www.gnu.org/licenses/>.
# #
######################################################################## ########################################################################
# #
# This script handles calls from submit.php. # This script handles calls from submit.php.
# It checks the validity of usernames, executes basic command # It checks the validity of usernames, executes basic command
# When password entries are required, it starts the respective python # When password entries are required, it starts the respective python
# scripts # scripts
# #
######################################################################## ########################################################################
# Test if config.cfg exists and set needed variables # Test if config.cfg exists and set needed variables
@@ -40,8 +40,8 @@ PATH=$PATH:$HOME/bin
## FUNCTIONS ## FUNCTIONS
function checkaction { function checkaction {
REGEX="^adduser$|^changepw$|^listusers$|^userdetail$|^deluser$|^sizeall$|^sizeuser$|^viewdata$|^addalias$|^quota$|^installwp$|^uninstallwp$" REGEX="^adduser$|^changepw$|^listusers$|^userdetail$|^deluser$|^sizeall$|^sizeuser$|^viewdata$|^addalias$|^quota$|^installwp$|^uninstallwp$"
if [[ $1 =~ $REGEX ]]; then if [[ $1 =~ $REGEX ]]; then
echo "true" echo "true"
else else
echo "false" echo "false"
fi fi
@@ -49,8 +49,8 @@ function checkaction {
function checkuser { function checkuser {
REGEX="^[A-Za-z0-9._+-]+$" # Allowed symbols REGEX="^[A-Za-z0-9._+-]+$" # Allowed symbols
if [[ $1 =~ $REGEX ]]; then if [[ $1 =~ $REGEX ]]; then
echo "true" echo "true"
else else
echo "false" echo "false"
fi fi
@@ -58,8 +58,8 @@ function checkuser {
function checkpass { function checkpass {
REGEX="[ '\\]" # Not allowed symbols REGEX="[ '\\]" # Not allowed symbols
if [[ $(grep -E "$REGEX" $1 ; echo $?) == 1 ]]; then if [[ $(grep -E "$REGEX" $1 ; echo $?) == 1 ]]; then
echo "true" echo "true"
else else
echo "false" echo "false"
fi fi
@@ -74,7 +74,7 @@ function userexists {
fi fi
} }
function mailsend { function mailsend {
TOEMAIL="$TOEMAIL"; TOEMAIL="$TOEMAIL";
FREMAIL="$FREMAIL"; FREMAIL="$FREMAIL";
SUBJECT="[$DOMAIN] $1"; SUBJECT="[$DOMAIN] $1";
@@ -95,14 +95,14 @@ $MSGBODY2
} }
function mailsendenc { function mailsendenc {
if [ ! -e $SSLKEY ]; then if [ ! -e $SSLKEY ]; then
#echo "Encryption key \"$SSLKEY\" is not available. Aborting." #echo "Encryption key \"$SSLKEY\" is not available. Aborting."
#exit 1 #exit 1
openssl genrsa -out $SSLKEY 2048 openssl genrsa -out $SSLKEY 2048
fi fi
TOEMAIL="$TOEMAIL"; TOEMAIL="$TOEMAIL";
FREMAIL="$FREMAIL"; FREMAIL="$FREMAIL";
SUBJECT="[$DOMAIN] $1"; SUBJECT="[$DOMAIN] $1";
@@ -124,8 +124,8 @@ Content-Disposition: inline
$MSGBODY1 $MSGBODY1
Upload the attached encrypted file to your Account Administration Panel Upload the attached encrypted file to your Account Administration Panel
in order to see sensitive details. Please visit the section \"Decrypt in order to see sensitive details. Please visit the section \"Decrypt
system email\" for more details. system email\" for more details.
--${BOUNDARY} --${BOUNDARY}
@@ -142,7 +142,7 @@ $ATTACHMENT
rm $MAILTMP; rm $MAILTMP;
} }
function notesdelete { function notesdelete {
USER="$1" USER="$1"
# Extract Mail part | exclude LEAD and TAIL | delete user # 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 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) # Update datasheet (add new entry in Email section)
USER=$1 USER=$1
PASSFILE=$2 PASSFILE=$2
# Create temporary file from $PASSFILE # Create temporary file from $PASSFILE
sed -E "s/(.*)/User: $USER\nPass: \1\n\n/" $PASSFILE > .$PASSFILE.tmp sed -E "s/(.*)/User: $USER\nPass: \1\n\n/" $PASSFILE > .$PASSFILE.tmp
# Insert this edited file into the datasheet # Insert this edited file into the datasheet
sed -i "/$TAIL/ { sed -i "/$TAIL/ {
h h
@@ -184,34 +184,34 @@ fi
if [ "$ACTION" == "adduser" ]; then if [ "$ACTION" == "adduser" ]; then
echo "Adding new Email user..." echo "Adding new Email user..."
echo echo
if ! $(checkuser "$USER"); then if ! $(checkuser "$USER"); then
echo "Username \"$USER\" invalid" echo "Username \"$USER\" invalid"
exit 1 exit 1
fi fi
if $(userexists "$USER"); then if $(userexists "$USER"); then
echo "User \"$USER\" does already exist!" echo "User \"$USER\" does already exist!"
exit 1 exit 1
fi fi
if ! $(checkpass "$PASSFILE"); then if ! $(checkpass "$PASSFILE"); then
echo "Password \""$(cat $PASSFILE)"\" invalid" echo "Password \""$(cat $PASSFILE)"\" invalid"
exit 1 exit 1
fi fi
python adduser.py "$USER" "$PASSFILE" python adduser.py "$USER" "$PASSFILE"
if [ $? == 0 ]; then if [ $? == 0 ]; then
# Send infomail # Send infomail
$MAILTYPE "New Email account created" \ $MAILTYPE "New Email account created" \
"A new Email account has been created." \ "A new Email account has been created." \
"User: $USER" "User: $USER"
LEAD='## > EMAIL' LEAD='## > EMAIL'
TAIL='## < EMAIL' TAIL='## < EMAIL'
notesinsert "$USER" "$PASSFILE" notesinsert "$USER" "$PASSFILE"
fi fi
fi # /adduser fi # /adduser
@@ -221,26 +221,26 @@ fi # /adduser
if [ "$ACTION" == "addalias" ]; then if [ "$ACTION" == "addalias" ]; then
echo "Extracting details of Email account..." echo "Extracting details of Email account..."
echo echo
DEST=$PASS DEST=$PASS
if ! $(checkuser "$USER"); then if ! $(checkuser "$USER"); then
echo "Username \"$USER\" invalid" echo "Username \"$USER\" invalid"
exit 1 exit 1
fi fi
if $(userexists "$USER"); then if $(userexists "$USER"); then
echo "User \"$USER\" does already exist!" echo "User \"$USER\" does already exist!"
exit 1 exit 1
fi fi
if ! $(userexists "$DEST"); then if ! $(userexists "$DEST"); then
echo "Destination account \"$DEST\" does not exist!" echo "Destination account \"$DEST\" does not exist!"
exit 1 exit 1
fi fi
vaddalias $USER $DEST vaddalias $USER $DEST
fi # /addalias fi # /addalias
# # # # # # # # # #
@@ -249,19 +249,19 @@ fi # /addalias
if [ "$ACTION" == "changepw" ]; then if [ "$ACTION" == "changepw" ]; then
echo "Changing password of Email account..." echo "Changing password of Email account..."
echo echo
if ! $(userexists "$USER"); then if ! $(userexists "$USER"); then
echo "User \"$USER\" does not exist!" echo "User \"$USER\" does not exist!"
exit 1 exit 1
fi fi
if ! $(checkpass "$PASSFILE"); then if ! $(checkpass "$PASSFILE"); then
echo "Password \""$(cat $PASSFILE)"\" invalid" echo "Password \""$(cat $PASSFILE)"\" invalid"
exit 1 exit 1
fi fi
python changepw.py "$USER" "$PASSFILE" python changepw.py "$USER" "$PASSFILE"
if [ $? == 0 ]; then if [ $? == 0 ]; then
# Send infomail # Send infomail
$MAILTYPE "Email password changed" \ $MAILTYPE "Email password changed" \
@@ -272,10 +272,10 @@ if [ "$ACTION" == "changepw" ]; then
# In fact a combination of deluser and adduser # In fact a combination of deluser and adduser
LEAD='## > EMAIL' LEAD='## > EMAIL'
TAIL='## < EMAIL' TAIL='## < EMAIL'
notesdelete "$USER" notesdelete "$USER"
notesinsert "$USER" "$PASSFILE" notesinsert "$USER" "$PASSFILE"
fi fi
fi # /changepw fi # /changepw
@@ -285,9 +285,9 @@ fi # /changepw
if [ "$ACTION" == "listusers" ]; then if [ "$ACTION" == "listusers" ]; then
echo "Listing all Email accounts..." echo "Listing all Email accounts..."
echo echo
listvdomain | column -s $' ' -t listvdomain | column -s $' ' -t
fi # /listusers fi # /listusers
# # # # # # # # # #
@@ -296,9 +296,9 @@ fi # /listusers
if [ "$ACTION" == "sizeall" ]; then if [ "$ACTION" == "sizeall" ]; then
echo "Calculate total size of all Email accounts..." echo "Calculate total size of all Email accounts..."
echo echo
du -sBM ~/users/* | sed -e "s:/home/$SYSUSER/users/::g" du -sBM ~/users/* | sed -e "s:/home/$SYSUSER/users/::g"
fi # /sizeall fi # /sizeall
# # # # # # # # # #
@@ -307,9 +307,9 @@ fi # /sizeall
if [ "$ACTION" == "viewdata" ]; then if [ "$ACTION" == "viewdata" ]; then
echo "Extracting data sheet..." echo "Extracting data sheet..."
echo echo
cat $NOTES cat $NOTES
fi # /viewdata fi # /viewdata
# # # # # # # # # #
@@ -318,10 +318,10 @@ fi # /viewdata
if [ "$ACTION" == "sizeuser" ]; then if [ "$ACTION" == "sizeuser" ]; then
echo "Calculate size of all folders of an Email account..." echo "Calculate size of all folders of an Email account..."
echo echo
# Show size in MB, strip long paths, strip tmp and new folders, rename .INBOX cur-folder # 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 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 fi # /sizeuser
# # # # # # # # # #
@@ -330,14 +330,14 @@ fi # /sizeuser
if [ "$ACTION" == "userdetail" ]; then if [ "$ACTION" == "userdetail" ]; then
echo "Extracting details of Email account..." echo "Extracting details of Email account..."
echo echo
if ! $(userexists "$USER"); then if ! $(userexists "$USER"); then
echo "User \"$USER\" does not exist!" echo "User \"$USER\" does not exist!"
exit 1 exit 1
fi fi
dumpvuser "$USER" | column -s $' ' -t dumpvuser "$USER" | column -s $' ' -t
fi # /userdetail fi # /userdetail
# # # # # # # # # #
@@ -346,26 +346,26 @@ fi # /userdetail
if [ "$ACTION" == "quota" ]; then if [ "$ACTION" == "quota" ]; then
echo "Calculating account disk usage..." echo "Calculating account disk usage..."
echo echo
USAGE=$(quota -gsl | tail -n 1 | awk -F" " '{ print $2 }') USAGE=$(cat quota | tail -n 1 | awk -F" " '{ print $2 }')
QUOTA=$(quota -gsl | tail -n 1 | awk -F" " '{ print $3 }' | sed 's/[A-Za-z]//g') QUOTA=$(cat quota | 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 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" USAGE="1"
else else
USAGE=$(echo $USAGE | sed 's/[A-Za-z]//g') USAGE=$(echo $USAGE | sed 's/[A-Za-z]//g')
fi fi
PERC=$(echo "scale=2; $USAGE/$QUOTA" | bc) PERC=$(echo "scale=2; $USAGE/$QUOTA" | bc)
PERC=$(echo "scale=2; $PERC*100" | bc) PERC=$(echo "scale=2; $PERC*100" | bc)
echo "$USAGE MB of $QUOTA MB are used ($PERC %)." echo "$USAGE MB of $QUOTA MB are used ($PERC %)."
PERC=$(echo "scale=2; 100 - $PERC" | bc) PERC=$(echo "scale=2; 100 - $PERC" | bc)
echo "You have "$[$QUOTA - $USAGE]" MB free ($PERC %)." echo "You have "$(($QUOTA - $USAGE))" MB free ($PERC %)."
fi # /quota fi # /quota
# # # # # # # # # #
@@ -374,26 +374,26 @@ fi # /quota
if [ "$ACTION" == "deluser" ]; then if [ "$ACTION" == "deluser" ]; then
echo "Extracting details of Email account..." echo "Extracting details of Email account..."
echo echo
if ! $(userexists "$USER"); then if ! $(userexists "$USER"); then
echo "User \"$USER\" does not exist!" echo "User \"$USER\" does not exist!"
exit 1 exit 1
fi fi
vdeluser "$USER" vdeluser "$USER"
if [ $? == 0 ]; then if [ $? == 0 ]; then
# Send infomail # Send infomail
$MAILTYPE "Email account deleted" \ $MAILTYPE "Email account deleted" \
"An Email account has been deleted." \ "An Email account has been deleted." \
"User: $USER" "User: $USER"
# Update datasheet (delete entry in Email section) # Update datasheet (delete entry in Email section)
LEAD='## > EMAIL' LEAD='## > EMAIL'
TAIL='## < EMAIL' TAIL='## < EMAIL'
notesdelete "$USER" notesdelete "$USER"
fi fi
fi # /deluser fi # /deluser
@@ -403,15 +403,15 @@ fi # /deluser
if [ "$ACTION" == "installwp" ]; then if [ "$ACTION" == "installwp" ]; then
echo "Installing WordPress..." echo "Installing WordPress..."
echo echo
# $USER: Username for Wordpress # $USER: Username for Wordpress
# $PASS: Email address for Wordpress user # $PASS: Email address for Wordpress user
if ! $(checkuser "$USER"); then if ! $(checkuser "$USER"); then
echo "Username \"$USER\" invalid" echo "Username \"$USER\" invalid"
exit 1 exit 1
fi fi
WEBDIR=$HOME/html WEBDIR=$HOME/html
WPUSER=$USER WPUSER=$USER
WPPASS=$(apg -n 1 -M NCL -m 14) WPPASS=$(apg -n 1 -M NCL -m 14)
@@ -434,7 +434,7 @@ if [ "$ACTION" == "installwp" ]; then
echo "The default database \"${USER}_wp\" already exists." echo "The default database \"${USER}_wp\" already exists."
exit 1 exit 1
fi fi
# Downloading wordpress # Downloading wordpress
wget $WPDL wget $WPDL
unzip $(basename $WPDL) unzip $(basename $WPDL)
@@ -448,7 +448,7 @@ if [ "$ACTION" == "installwp" ]; then
# Create database # Create database
mysql -e "CREATE DATABASE $MYSQLDB;" mysql -e "CREATE DATABASE $MYSQLDB;"
# Use wp-cli to create config.php and install WP # 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 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} 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 # Update datasheet
TAIL='## < WORDPRESS' TAIL='## < WORDPRESS'
sed -i "/$TAIL/i User: $WPUSER\nPass: $WPPASS\n" $NOTES sed -i "/$TAIL/i User: $WPUSER\nPass: $WPPASS\n" $NOTES
echo echo
echo "Wordpress successfully installed to $WPDOMAIN" echo "Wordpress successfully installed to $WPDOMAIN"
echo "You can login on $WPDOMAIN/wp-login.php" echo "You can login on $WPDOMAIN/wp-login.php"
echo "Please take a look into your data sheet for the login data." echo "Please take a look into your data sheet for the login data."
fi # /installwp fi # /installwp
# # # # # # # # # #
@@ -470,15 +470,15 @@ fi # /installwp
if [ "$ACTION" == "uninstallwp" ]; then if [ "$ACTION" == "uninstallwp" ]; then
echo "Uninstalling WordPress..." echo "Uninstalling WordPress..."
echo echo
# $USER: Username for Wordpress # $USER: Username for Wordpress
# $PASS: Email address for Wordpress user # $PASS: Email address for Wordpress user
WEBDIR=$HOME/html WEBDIR=$HOME/html
MYSQLUSER=$SYSUSER MYSQLUSER=$SYSUSER
MYSQLDB=${MYSQLUSER}_wp MYSQLDB=${MYSQLUSER}_wp
# Delete database and content of ~/htmp # Delete database and content of ~/htmp
mysql -e "DROP DATABASE ${MYSQLDB};" mysql -e "DROP DATABASE ${MYSQLDB};"
rm -rf $WEBDIR/* rm -rf $WEBDIR/*
@@ -487,10 +487,10 @@ if [ "$ACTION" == "uninstallwp" ]; then
# Update datasheet # Update datasheet
LEAD='## > WORDPRESS' LEAD='## > WORDPRESS'
TAIL='## < WORDPRESS' TAIL='## < WORDPRESS'
notesdelete ".*" notesdelete ".*"
echo echo
echo "Wordpress successfully uninstalled." echo "Wordpress successfully uninstalled."
fi # /uninstallwp fi # /uninstallwp