quota on maildirs :)

Discussion in 'Tips/Tricks/Mods' started by jmroth, Oct 9, 2006.

  1. jmroth

    jmroth New Member

    Hi there,
    I have now developed another procmail script to be used for processing quotas on mail delivery (works with my MailDIRS)

    Code:
    DROPPRIVS=YES
    
    ## using repquota will not work because normal users must not use it
    PATH="/usr/bin:$PATH:/usr/local/bin"
    SHELL=/bin/sh
    EMAIL=`formail -zxTo:`
    
    QUOTA=`mailquotacheck.sh`
    :0
    * > ${QUOTA}
    {
            LOG="THIS USER/DOMAIN IS FULL. PLEASE TRY AGAIN AT A LATER TIME!"
            EXITCODE=69
            HOST
    }
    #end
    
    I put the mailquotacheck.sh script into /usr/local/bin and this is it:
    Code:
    #!/bin/bash
    
    QUOTA=10000000000 #10GB
    
    GROUP_QUOTA_ENABLED=0
    quota -g | grep -q ": none" || GROUP_QUOTA_ENABLED=1
    if [ $GROUP_QUOTA_ENABLED -eq 1 ]; then
            FS_GROUP_QUOTA=`quota -g | sed -n 3p | awk '{print $3}'`*1024
            FS_GROUP_QUOTA=$(($FS_GROUP_QUOTA))
            FS_GROUP_QUOTA_USED=`quota -g | sed -n 3p | awk '{print $2}' | sed -n 's/^\([0-9]*\).*$/\1/g p'`*1024
            FS_GROUP_QUOTA_USED=$(($FS_GROUP_QUOTA_USED))
            FS_GROUP_QUOTA_FREE=$(($FS_GROUP_QUOTA-$FS_GROUP_QUOTA_USED))
    else
            FS_GROUP_QUOTA=0
    fi
    
    USER_QUOTA_ENABLED=0
    quota -u | grep -q ": none" || USER_QUOTA_ENABLED=1
    if [ $USER_QUOTA_ENABLED -eq 1 ]; then
            FS_USER_QUOTA=`quota -u | sed -n 3p | awk '{print $2}'`*1024
            FS_USER_QUOTA=$(($FS_USER_QUOTA))
            FS_USER_QUOTA_USED=`quota -u | awk '{print $2}' | sed -n 's/^\([0-9]*\).*$/\1/g p'`*1024
            FS_USER_QUOTA_USED=$(($FS_USER_QUOTA_USED))
            FS_USER_QUOTA_FREE=$((FS_USER_QUOTA-$FS_USER_QUOTA_USED)) 
    else
            FS_USER_QUOTA=0
    fi
    
    if [ $FS_USER_QUOTA == 0 ]; then
            if [ $FS_GROUP_QUOTA != 0 ]; then
            # we have unlimtied user filesystem quota but the group is limited
                    if [ $FS_GROUP_QUOTA_FREE -lt $QUOTA ]; then
                            QUOTA=$FS_GROUP_QUOTA_FREE
                    else
                            QUOTA=$QUOTA
                    fi
            else
            # unlimited user and group quota
                    QUOTA=$QUOTA
            fi
    else
    # the minimum of user and group quotas counts
            if [ $FS_USER_QUOTA_FREE -lt $FS_GROUP_QUOTA_FREE ]; then
                    QUOTA=$FS_USER_QUOTA_FREE
            else
                    QUOTA=$FS_GROUP_QUOTA_FREE
            fi
    fi
    
    echo $QUOTA
    
    Have phun. Regards,
    Marc
     
  2. falko

    falko Super Moderator Howtoforge Staff

    Does it check if the current mailbox + the new message is greater than the quota value, or just if the new message is greater than the quota value?
     
  3. jmroth

    jmroth New Member

    Actually the shell script will output how much space is _available_ to either user or group the user is a member of, whatever is the least. (Large group quota will help nothing if the user is f.e. already over quota)
    Then, the procmail script will compare the message size to that value.
    The problem I saw many times in the forums was that *1024-error. Mainly that is due to the "quota" command appending an * to the value it outputs in case the user is over quota. The regexp in the script removes this superfluous *, so the math can be done correctly.
    The main reason I tried to implement this is so that the senders get more verbose messages back than "can't create user output file", which to most people means nothing. Here you can define the LOG="..." string in the procmail recipe as you like. (From what I have seen this only works if logging is not enabled in the procmail file.)
    Regards,
    Marc
     
  4. till

    till Super Moderator Staff Member ISPConfig Developer

    Looks promising. I will try to implement this in the dev tree.

    Maybe we can provide the QUOTA value to the shell script as commandline parameter, so we dont have to make a bash script for every user with different quota values.
     
  5. jmroth

    jmroth New Member

    Why make a script for every user with different quota values?
    The QUOTA=10GB at the beginning is only there as a fallback, so noone can ever be sent a message larger than 10 GB. I took this somewhere from the old scripts. One could just remove that altogether. The actual quota of the user/group is of course given to the script via the quota command. (FS_GROUP_QUOTA=`quota -g | sed -n 3p | awk '{print $3}'`*1024 ...)
    Regards,
    Marc
     

Share This Page