#!/bin/bash
# Kategorie: mailaccounts
# Script zum Setzen von individuellen Quotas von Kopano-Konten
# und Objektklassen
# (C) 2009,2011,2019,2020 invis-server.org
# Author: Stefan Schäfer <stefan@invis-server.org>

# License: GPLv3
# Dieses Programm ist freie Software. Sie können es unter den Bedingungen der 
# GNU General Public License, wie von der Free Software Foundation veröffentlicht,
# weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder
# (nach Ihrer Option) jeder späteren Version.

# Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen
# von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite 
# Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. 
# Details finden Sie in der GNU General Public License.

# Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem 
# Programm erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>. 


#Konfigurationsdaten
conffile="/etc/invis/invis.conf"
passfile="/etc/invis/invis-pws.conf"

# Funktionen
# Werte aus Konfigurationsdatendatei extrahieren
# $1 = Konfigurationsdatei, $2 = Parameter, $3 Wert (Feld)
getconfdata() {
    cat $1 |grep "$2" | cut -d ":" -f $3
}

# Konfigurationsparameter tauschen
changevalues() {
    # Arg1 = Pfad, Arg2 = Datei, Arg3 = sed String
    cat $1/$2|sed "s%$3%g" > $1/$2.new
    mv $1/$2.new $1/$2
}
# Aufrufparameter
if [[ -n $1 && -n $2 && $2 =~ [0-9]{4,} ]]; then
    uid=$1
    hardquota=$2
else
    echo -e "Usage: extzu username hardquota"
    echo -e "Für \"hardquota\" werden nur mindestens 4-stellige ganze Zahlen (Größe in Megabyte) gestattet."
    exit
fi

# softquota und qutawarn berechnen
(( softquota=$hardquota - 512 ))
(( quotawarn=$softquota - 512 ))

echo "$hardquota/$softquota/$quotawarn"

# Basis-Variablen
basedn=`getconfdata $conffile "baseDN" "2"`
localdomain=`getconfdata $conffile "intDomain" "2"`
binddn="ldap.admin@$localdomain"

bindpw=`getconfdata $passfile "LDAPAdminPW" "2"`
ldaphost=`getconfdata $conffile "ldapHost" "2"`

function lds { 
    ldapsearch -LLL -x -Z -h $ldaphost -D $binddn -w $bindpw -b $1 $2
}

searchbase="cn=Users,$basedn"
filter="(samAccountName=$uid)"

# Ist das Konto bereits fuer Zarafa bekannt
iszu=`lds $searchbase $filter|grep "zarafaAccount: 1"|cut -d " " -f2`
#echo $iszu

# DN des zu aendernden Benutzerknotens ermitteln
udnraw=`lds $searchbase $filter|grep "^dn:"| cut -d " " -f 2-`

# In Klartext umwandeln, falls DN in base64 codiert ist.
if [[ $udnraw =~ ^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$ ]]; then
    udn=`echo $udnraw |base64 -d`
else
    udn=$udnraw
fi

# Wenn die Eintraege noch nicht vorhanden sind, jetzt anlegen
if [[ $iszu != "" ]]; then

    # Sind bereits Quotas gesetzt
    quotaexist=`lds $searchbase $filter|grep "zarafaQuotaOverride: 1"|cut -d " " -f2`

    if [[ $quotaexist == "" ]]; then
	# Quotas setzen
	line1="dn: $udn\n"
	line2="changetype: modify\n"
	line3="add: zarafaQuotaHard\n"
	line4="zarafaQuotaHard: $hardquota\n"
	line5="\n"
	echo -e "$line1$line2$line3$line4$line5" |ldapmodify -x -Z -h $ldaphost -D $binddn -w $bindpw 
	
	line1="dn: $udn\n"
	line2="changetype: modify\n"
	line3="add: zarafaQuotaSoft\n"
	line4="zarafaQuotaSoft: $softquota\n"
	line5="\n"
	echo -e "$line1$line2$line3$line4$line5" |ldapmodify -x -Z -h $ldaphost -D $binddn -w $bindpw 
	
	line1="dn: $udn\n"
	line2="changetype: modify\n"
	line3="add: zarafaQuotaWarn\n"
	line4="zarafaQuotaWarn: $quotawarn\n"
	line5="\n"
	echo -e "$line1$line2$line3$line4$line5" |ldapmodify -x -Z -h $ldaphost -D $binddn -w $bindpw 

	line1="dn: $udn\n"
	line2="changetype: modify\n"
	line3="add: zarafaQuotaOverride\n"
	line4="zarafaQuotaOverride: 1\n"
	line5="\n"
	echo -e "$line1$line2$line3$line4$line5" |ldapmodify -x -Z -h $ldaphost -D $binddn -w $bindpw 
    else
	# Quotas ersetzen
	line1="dn: $udn\n"
	line2="changetype: modify\n"
	line3="replace: zarafaQuotaHard\n"
	line4="zarafaQuotaHard: $hardquota\n"
	line5="\n"
	echo -e "$line1$line2$line3$line4$line5" |ldapmodify -x -Z -h $ldaphost -D $binddn -w $bindpw 
	
	line1="dn: $udn\n"
	line2="changetype: modify\n"
	line3="replace: zarafaQuotaSoft\n"
	line4="zarafaQuotaSoft: $softquota\n"
	line5="\n"
	echo -e "$line1$line2$line3$line4$line5" |ldapmodify -x -Z -h $ldaphost -D $binddn -w $bindpw 
	
	line1="dn: $udn\n"
	line2="changetype: modify\n"
	line3="replace: zarafaQuotaWarn\n"
	line4="zarafaQuotaWarn: $quotawarn\n"
	line5="\n"
	echo -e "$line1$line2$line3$line4$line5" |ldapmodify -x -Z -h $ldaphost -D $binddn -w $bindpw 
    fi

fi
