Saturday, July 26, 2014

BASH: running script with argument equal value ( ./script.sh VAR="value" VAR2="value")

This is an example:

#Parse argument
while [ $# -gt 0 ]; do
 case ${1%%=*} in
  MESSAGE|message)
   MESSAGE=${1#*=}
   ;;
  SPANS|spans)
   SPANS=${1#*=}
   ;;
  NUMBERS|numbers)
   NUMBERS=${1#*=}
   ;;
  ATTEMPT|attempt)
   ATTEMPT=${1#*=}
   ;;
  VERBOSE|verbose)
   VERBOSE=${1#*=}
   ;;
  REPEAT|repeat)
   REPEAT=${1#*=}
   ;;
   *)
   usage
   exit 1
   ;;
 esac
 shift
done


Now you run your script as follow:

./script.sh  MESSAGE="message" SPANS="spans" NUMBERS="numbers" ....

How this works? That's easy!

while [ $# -gt 0 ] - Do, till we are running out of arguments. If we invoke ./script.sh one two three, then our arguments will be:
$0 = name of script ('./script.sh')
$1 = 'one'
$2 = 'two'
$3 = 'three'

On the end of each iteration we cast shift, which removes the first element from arguments array, but not the element's number. Argument numbers will be shifted. So at first cycle $1 will be equal 'one', but at the second iteration $1 will be equal 'two'. Got it?

case ${1%%=*} - In case if the first part (before '=' ) of our argument $1 is equal to (below). If we invoke ./script.sh MESSAGE="sometext", then $1 will be equal 'MESSAGE="sometext"'. Then respectively:

$1 = 'MESSAGE="sometext"'
${1} = 'MESSAGE="sometext"'
${1%%=*}  =  'MESSAGE'
${1#*=} = "sometext"



P.S.

Example of usage function:
usage()
{
        echo "Usage:"
        echo
        echo "$0 MESSAGE=\"message\" SPANS=\"spans\" NUMBERS=\"numbers\" [ATTEMPT=\"attempt\"] [VERBOSE=\"verbose\"] [REPEAT=\"repeat\"]"
        echo "Defaults: ATTEMPT=always VERBOSE=3 REPEAT=1"
        echo "Example: $APP_NAME MESSAGE=\"hello\" SPANS=auto NUMBERS=\"135xxxxxxxx,136xxxxxxxx\""
        echo
}

Look as well at: http://tldp.org/LDP/abs/html/refcards.html#AEN22664

Friday, July 25, 2014

Postfix: controlling attachment size limits

One of the famous problems, if you're building up a corporate email server, might be an email attachment size, that is allowed for your users to send as emails. On newbie's questions on forums most of gurus are pointing to policyd. Obviously, this plugin is more than amazing, works more than perfect and pretty fast as well. But.. It wont control the attachment size. It's plugin "Quotas" is only to control the amount of sent messages and consumed bandwidth, but not to control a single message size.

NOTE: MessageCumulativeSize will never block your first message with attachment, even it's size is exceeding the limits of hundred times! The first message will always go through. Read this: https://www.mail-archive.com/users@lists.policyd.org/msg01962.html

So this is not what I expected. I'd like to have a simple plugin, that BLOCKS the message sending, if it's size is larger, than I would allow. And I came up with this simple path.

Go to /usr/lib64/policyd-2.0/cbp/modules/ or wherever your cbpolicyd files stored and modify  Quotas.pm or make a patch like this (name it Quotas.pm.patch):

[root@server modules]# cat Quotas.pm.patch

--- Quotas.pm.orig      2014-07-25 21:01:38.332494503 -0400
+++ Quotas.pm   2014-07-25 22:24:08.297785096 -0400
@@ -101,7 +101,6 @@
        #   stage
        #
        if ($sessionData->{'ProtocolState'} eq "RCPT") {
-
                # Key tracking list, if quotaExceeded is not undef, it will contain the msg
                my %newCounters;  # Indexed by QuotaLimitsID
                my @trackingList;
@@ -414,7 +413,24 @@
                                                        if (lc($limit->{'Type'}) eq "messagecumulativesize") {
                                                                # Bump up counter
                                                                my $currentCounter = $qtrack->{'Counter'} + $sessionData->{'Size'};
-
+
+#-------------------------------# Added by SHIRKER
+                                                              # $server->maillog("DEBUG Quota TESTTTTT START!! Quota=%s MessageSize=%s",
+                                                              #                $limit->{'CounterLimit'},
+                                                              #                $sessionData->{'Size'});
+
+                                                               if ($sessionData->{'Size'} > $limit->{'CounterLimit'}){
+                                                               # $server->maillog("DEBUG Quota TESTTTTT DONE!! Quota=%s MessageSize=%s" Verdict=%s",
+                                                               # $limit->{'CounterLimit'},
+                                                               # $sessionData->{'Size'},
+                                                              # $quota->{'Verdict'});
+
+                                                                       # Set verdict
+                                                                       $verdict = $quota->{'Verdict'};
+                                                               }
+#-------------------------------# END added by Shirker
+#
+#
                                                                # Update database
                                                                my $sth = DBDo("
                                                                        UPDATE

Then apply it:

patch Quotas.pm < Quotas.pm.patch