Wednesday, July 20, 2011

Using systemd to start IndiMail

systemd is a system and service manager for Linux, compatible with SysV and LSB init scripts. systemd provides aggressive parallelization capabilities, uses socket and D-Bus activation for starting services, offers on-demand starting of daemons, keeps track of processes using Linux cgroups, supports snapshots and restoring of the system state, maintains mount and automount points and implements an elaborate transactional dependency-based service control logic. It can work as a drop-in replacement for sysvinit.

The first step is to write the service configuration file for IndiMail as /lib/systemd/system/indimail.service


[Unit]
Description=IndiMail Messaging Platform
After=local-fs.target network.target

[Service]
ExecStart=/var/indimail/bin/svscanboot /service
ExecStop=/etc/init.d/indimail stop
Restart=on-failure
Type=simple

[Install]
WantedBy=multi-user.target


In Fedora 15, upstart has been replaced by a service called systemd. Due to improper rpm package upgrade scripts, some system services previously enabled in Fedora 14, may not be enabled after upgrading to Fedora 15. To determine if a service is impacted, run the systemctl status command as shown below.

# systemctl is-enabled indimail.service && echo "Enabled on boot" || echo "Disabled on boot"


To enable indimail service on boot, run the following systemctl command


# systemctl enable indimail.service

Now to start IndiMail you can use the usual service command

# service indimail start    (to start indimail)

# service indimail stop     (to stop indimail)

You can automate the above service creation for systemd by running the initsvc(1) command


# /var/indimail/bin/initsvc -on  (to enable indimail service)
# /var/indimail/bin/initsvc -off   (to disable indimail service)

You can now also query the status of the running IndiMail service by using the systemctl command


# systemctl status indimail.service
indimail.service - IndiMail Messaging Platform
          Loaded: loaded (/lib/systemd/system/indimail.service)
          Active: active (running) since Wed, 20 Jul 2011 18:18:35 +0530; 10min ago
        Main PID: 4353 (svscanboot)
          CGroup: name=systemd:/system/indimail.service
                  ├ 4353 /bin/sh /var/indimail/bin/svscanboot
                  ├ 4356 /var/indimail/bin/svscan /service
                  ├ 4357 supervise log
                  ├ 4358 supervise clamd
                  ├ 4359 supervise log
                  ├ 4360 supervise freshclam
                  ├ 4361 supervise log
                  ├ 4362 supervise qmail-send.25
                  ├ 4363 supervise log
                  ├ 4364 supervise qmail-pop3d-ssl.995
                  ├ 4365 supervise log
                  ├ 4366 supervise proxy-imapd-ssl.9143
                  ├ 4367 supervise log
                  ├ 4368 supervise qmail-poppass.106
                  ├ 4369 supervise log
                  ├ 4370 supervise proxy-imapd.4143
                  ├ 4371 supervise log
                  ├ 4372 supervise proxy-pop3d-ssl.9110
                  ├ 4373 supervise log
                  ├ 4374 supervise qmail-smtpd.465
                  ├ 4375 supervise log
                  ├ 4376 supervise inlookup.infifo
                  ├ 4377 supervise log
                  ├ 4378 supervise qmail-smtpd.366
                  ├ 4379 supervise log
                  ├ 4380 supervise proxy-pop3d.4110
                  ├ 4381 supervise log
                  ├ 4382 supervise slapd.389
                  ├ 4383 supervise log
                  ├ 4384 supervise qmail-smtpd.587
                  ├ 4385 supervise log
                  ├ 4386 supervise pwdlookup
                  ├ 4387 supervise log
                  ├ 4388 supervise qmail-pop3d.110
                  ├ 4389 supervise log
                  ├ 4390 supervise qmail-qmtpd.209
                  ├ 4391 supervise log
                  ├ 4392 supervise fetchmail
                  ├ 4393 supervise log
                  ├ 4394 supervise qscanq
                  ├ 4395 supervise log
                  ├ 4396 supervise qmail-smtpd.25
                  ├ 4397 supervise log
                  ├ 4398 supervise mysql.3306
                  ├ 4399 supervise log
                  ├ 4400 supervise qmail-spamlog
                  ├ 4401 supervise log
                  ├ 4402 supervise qmail-qmqpd.628
                  ├ 4403 supervise log
                  ├ 4404 supervise qmail-imapd-ssl.993
                  ├ 4405 supervise log
                  ├ 4406 supervise indisrvr.4000
                  ├ 4407 supervise log
                  ├ 4408 supervise qmail-imapd.143
                  ├ 4409 supervise log
                  ├ 4410 supervise greylist.1999
                  ├ 4411 supervise log
                  ├ 4412 /var/indimail/bin/multilog t /var/log/indimail/fre...
                  ├ 4413 qmail-daemon ./Maildir/
                  ├ 4414 /var/indimail/bin/tcpserver -v -H -R -l 0 -x /var/...
                  ├ 4415 /var/indimail/bin/tcpserver -v -c /service/qmail-p...
                  ├ 4416 /var/indimail/bin/multilog t /var/log/indimail/pro...
                  ├ 4417 /var/indimail/bin/multilog t /var/log/indimail/pop...
                  ├ 4418 /var/indimail/bin/multilog t /var/log/indimail/pro...
                  ├ 4419 /var/indimail/bin/multilog t /var/log/indimail/pro...
                  ├ 4420 /var/indimail/bin/multilog t /var/log/indimail/pop...
                  ├ 4421 /var/indimail/bin/tcpserver -v -c /service/qmail-p...
                  ├ 4422 /var/indimail/bin/multilog t /var/log/indimail/pro...
                  ├ 4424 /var/indimail/bin/tcpserver -v -c /service/proxy-p...
                  ├ 4425 /var/indimail/bin/multilog t -* cleanq starting -*...
                  ├ 4426 /var/indimail/bin/tcpserver -v -h -R -l 0 -x /var/...
                  ├ 4427 /var/indimail/bin/tcpserver -v -c /service/proxy-i...
                  ├ 4428 /var/indimail/sbin/inlookup -i 5
                  ├ 4429 /var/indimail/bin/multilog t /var/log/indimail/mys...
                  ├ 4430 /var/indimail/bin/multilog t /var/log/indimail/smt...
                  ├ 4431 /var/indimail/sbin/nssd -d notice
                  ├ 4432 /var/indimail/bin/tcpserver -v -c /service/qmail-i...
                  ├ 4433 /var/indimail/bin/multilog t /var/log/indimail/pwd...
                  ├ 4434 /var/indimail/bin/multilog t /var/log/indimail/smt...
                  ├ 4435 /var/indimail/bin/multilog t /var/log/indimail/pop...
                  ├ 4436 /var/indimail/bin/tcpserver -v -c /service/proxy-i...
                  ├ 4437 /var/indimail/bin/multilog t /var/log/indimail/inl...
                  ├ 4438 /usr/bin/perl /var/indimail/bin/greydaemon -w /var...
                  ├ 4439 /var/indimail/bin/tcpserver -v -c /service/qmail-i...
                  ├ 4440 /var/indimail/bin/tcpserver -v -h -R -l 0 -x /var/...
                  ├ 4441 /var/indimail/bin/multilog t /var/log/indimail/ima...
                  ├ 4442 /var/indimail/bin/tcpserver -v -H -R -l 0 -x /var/...
                  ├ 4443 /var/indimail/bin/multilog t /var/log/indimail/gre...
                  ├ 4444 /usr/local/mysql/libexec/mysqld --defaults-file=/v...
                  ├ 4445 /var/indimail/bin/tcpserver -v -H -R -l 0 -x /var/...
                  ├ 4446 /var/indimail/bin/qmail-cat /tmp/spamfifo
                  ├ 4447 /var/indimail/bin/multilog t /var/log/indimail/fet...
                  ├ 4448 /var/indimail/bin/multilog t /var/log/indimail/qmq...
                  ├ 4449 /var/indimail/bin/multilog t /var/log/indimail/smt...
                  ├ 4450 /var/indimail/bin/multilog t /var/log/indimail/qmt...
                  ├ 4451 /var/indimail/bin/tcpserver -v -H -R -l 0 -x /var/...
                  ├ 4452 /var/indimail/sbin/indisrvr -i 0 -p 4000 -b 40 -n ...
                  ├ 4453 /var/indimail/bin/multilog t /var/log/indimail/ind...
                  ├ 4454 /var/indimail/bin/multilog t /var/log/indimail/smt...
                  ├ 4455 /var/indimail/bin/multilog t /var/log/indimail/cla...
                  ├ 4456 /var/indimail/bin/multilog t /var/log/indimail/svs...
                  ├ 4457 /var/indimail/sbin/clamd
                  ├ 4458 /var/indimail/bin/multilog t /var/log/indimail/del...
                  ├ 4459 /var/indimail/bin/tcpserver -v -c /service/proxy-p...
                  ├ 4460 /var/indimail/bin/multilog t /var/log/indimail/spa...
                  ├ 4461 /var/indimail/bin/multilog t /var/log/indimail/ima...
                  ├ 4462 /bin/sh ./run
                  ├ 4477 /var/indimail/bin/multilog t /var/log/indimail/sla...
                  ├ 4509 /var/indimail/bin/freshclam -v --stdout --datadir=...
                  ├ 4519 /var/indimail/sbin/inlookup -i 5
                  ├ 4520 /var/indimail/sbin/inlookup -i 5
                  ├ 4521 /var/indimail/sbin/inlookup -i 5
                  ├ 4522 /var/indimail/sbin/inlookup -i 5
                  ├ 4523 /var/indimail/sbin/inlookup -i 5
                  ├ 4526 qmail-send
                  ├ 4527 qmail-send
                  ├ 4528 qmail-send
                  ├ 4529 qmail-send
                  ├ 4530 qmail-send
                  ├ 4531 qmail-lspawn ./Maildir/
                  ├ 4532 qmail-rspawn
                  ├ 4533 qmail-clean
                  ├ 4534 qmail-todo
                  ├ 4535 qmail-clean
                  ├ 4536 qmail-lspawn ./Maildir/
                  ├ 4537 qmail-rspawn
                  ├ 4538 qmail-clean
                  ├ 4539 qmail-todo
                  ├ 4540 qmail-clean
                  ├ 4541 qmail-lspawn ./Maildir/
                  ├ 4542 qmail-rspawn
                  ├ 4543 qmail-clean
                  ├ 4544 qmail-todo
                  ├ 4545 qmail-clean
                  ├ 4546 qmail-lspawn ./Maildir/
                  ├ 4547 qmail-rspawn
                  ├ 4548 qmail-clean
                  ├ 4549 qmail-todo
                  ├ 4550 qmail-clean
                  ├ 4551 qmail-lspawn ./Maildir/
                  ├ 4552 qmail-rspawn
                  ├ 4553 qmail-clean
                  ├ 4554 qmail-todo
                  ├ 4555 qmail-clean
                  ├ 4751 /bin/sh ./run
                  └ 4752 sleep 200

Tuesday, July 5, 2011

IndiMail Installation for newbies in < 10 steps


Installing Indimail using YUM/APT Repository

  1. Install OS
    • OpenSUSE
      • openSUSE Leap 42.3
      • openSUSE Leap 42.2
      • openSUSE 13.2
      • openSUSE 13.1
      • SUSE Linux Enterprise 12 SP2
      • SUSE Linux Enterprise 12 SP1
      • SUSE Linux Enterprise 12
    • Red Hat
      • Feodra 27
      • Fedora 26
      • Red Hat Enterprise Linux 7
      • Red Hat Enterprise Linux 6
      • CentOS 7
      • CentOS 6
    • Debian
      • Debian 8.0
      • Debian 7.0
      • Ubuntu 17.04
      • Ubuntu 16.10
      • Ubuntu 16.04
      • Ubuntu 14.04
      • Ubuntu 12.04
  1. Click the below URL for Install Instructions
https://software.opensuse.org/download.html?project=home%3Aindimail&package=indimail
  1. Shutdown MySQL if already running and disable MySQL from being started up by the system % /etc/init.d/mysqld stop % sudo chkconfig mysqld off % /bin/rm -f /service/mysql.3306/down
  2. Start IndiMail
% sudo service indimail start
  1. Check Servicess
% sudo /usr/bin/svstat /service/*
/service/clamd: up (pid 1014) 2985 seconds
/service/dnscache: up (pid 1021) 2985 seconds
/service/fetchmail: down 2985 seconds
/service/freshclam: up (pid 1020) 2985 seconds
/service/greylist.1999: up (pid 1043) 2985 seconds
/service/indisrvr.4000: up (pid 1048) 2985 seconds
/service/inlookup.infifo: up (pid 1057) 2985 seconds
/service/mrtg: up (pid 1023) 2985 seconds
/service/mysql.3306: up (pid 1031) 2985 seconds
/service/proxy-imapd.4143: up (pid 1018) 2985 seconds
/service/proxy-imapd-ssl.9143: up (pid 1041) 2985 seconds
/service/proxy-pop3d.4110: up (pid 1017) 2985 seconds
/service/proxy-pop3d-ssl.9110: up (pid 1037) 2985 seconds
/service/pwdlookup: up (pid 1030) 2985 seconds
/service/qmail-imapd.143: up (pid 1046) 2985 seconds
/service/qmail-imapd-ssl.993: up (pid 1055) 2985 seconds
/service/qmail-logfifo: up (pid 1024) 2985 seconds
/service/qmail-pop3d.110: up (pid 1029) 2985 seconds
/service/qmail-pop3d-ssl.995: up (pid 1054) 2985 seconds
/service/qmail-poppass.106: up (pid 1022) 2985 seconds
/service/qmail-qmqpd.628: down 2985 seconds
/service/qmail-qmtpd.209: up (pid 1044) 2985 seconds
/service/qmail-send.25: up (pid 1026) 2985 seconds
/service/qmail-smtpd.25: up (pid 1050) 2985 seconds
/service/qmail-smtpd.366: up (pid 1019) 2985 seconds
/service/qmail-smtpd.465: up (pid 1027) 2985 seconds
/service/qmail-smtpd.587: up (pid 1012) 2985 seconds
/service/qscanq: up (pid 1036) 2985 seconds
/service/resolvconf: up (pid 1069) 2985 seconds
/service/udplogger.3000: up (pid 1061) 2985 seconds

  1. Play with the System
    1. Add A Domain
% sudo vadddomain example.com password1
    1. Add a User
% sudo vadduser user01@example.com password2
    1. Send email
swaks –to youremail@yourdomain –from user01@example.com –server localhost
  1. RTFM
% man indimail
  1. Buy Beer for Manvendra Bhangui



NOTE: The MySQL database gets installed with the user 'mysql' with all privileges and a user 'indimail' with limited privileges. You will find the passwords for these two users in the script /usr/sbin/svctool. If you modify these, please don't forget to modify /etc/indimail/control/host.mysql

Thursday, June 30, 2011

Eliminating Duplicate Emails during local delivery

Often you will find program like MS outlook, notorious for sending duplicate emails, flooding your inbox. IndiMail allows you to quickly deal with this proprietary nonsense by turning on duplicate eliminator in vdelivermail(8) - the default MDA. To turn on the duplicate eliminator in vdelivermail, you need to set ELIMINATE_DUPS and MAKE_SEEKABLE environment variables.

% su
# echo 1> /service/qmail-send.25/variables/ELIMINATE_DUPS
# echo 1> /service/qmail-send.25/variables/MAKE_SEEKABLE
# svc -d /service/qmail-send.25; svc -u /service/qmail-send.25
# exit

If you do not use vdelivermail and want to use your own delivery agent? Fear not by using ismaildup(1). ismaildup expects the email on standard input and is easily scriptable like the example below in a .qmail file.

   | ismaildup /var/indimail/bin/maildirdeliver /home/manny/Maildir

will deliver mails to /home/manny/Maildir while discarding duplicates.

If you are not happy with the 900 seconds (15 minutes) time interval for checking duplicates, you can change it by setting the DUPLICATE_INTERVAL environment variable. The following will not allow a single duplicate to be entertained within 24 hours

% su
# echo 86400 > /service/qmail-send.25/variables/DUPLICATE_INTERVAL
# svc -d /service/qmail-send.25 ; svc -u /service/qmail-send.25
# exit

Friday, June 24, 2011

Using procmail with Indimail


IndiMail follows the traditional UNIX philosophy.
"Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface"
This allows IndiMail to interface with many programs written by others. IndiMail uses a powerful filter mechanism called vfilter(8). You may already be familiar with procmail. procmail is a mail delivery agent (MDA) capable of sorting incoming mail into various directories and filtering out messages. There are three ways in which you can use procmail with IndiMail.
1. inside .qmail
Users can use
| preline procmail
from ~/.qmail. They will need to use a full path for procmail unless procmail is in the system's startup PATH.
2. Edit .qmail-default
| preline -f procmail -p -m /var/indimail/etc/procmailrc
3. Have an alias
You can use valias(1) to create an alias to call procmail. The following alias calls procmail to deliver the mail using /var/indimail/etc/procmailrc as a procmail recipe
valias -i "|/var/indimail/bin/preline -f /usr/bin/procmail -p -m /var/indimail/etc/procmailrc" testuser@example.com
The following procmailrc puts virus infected mails in /tmp/Maildir and calls maildirdeliver(1) to deliver the mail to /home/mail/T2Zsym/example.com/testuser01/Maildir.
SHELL=/bin/bash
VERBOSE="no"
unset DTLINE
unset RPLINE
:0w
*^X-Virus-Status: INFECTED
/tmp/Maildir/.Virus
:0w
| /var/indimail/bin/maildirdeliver /home/mail/T2Zsym/example.com/testuser01/Maildir
You can replace maildirdeliver in the last line with vdelivermail(8)
| /var/indimail/bin/vdelivermail '' bounce-no-mailbox



Saturday, June 18, 2011

Writing Filters for IndiMail

IndiMail provides multiple methods by which you can intercept an email in transit and modify the email headers or the email body. A filter is a simple program that expects the raw email on standard input and outputs the message text back on standard output. The program /bin/cat can be used as a filter which simply copies the standard input to standard output without modifying anything. Some methods can be used before the mail gets queued and some methods can be used before the execution of local / remote delivery.

It is not necessary for a filter to modify the email. You can have a filter just to extract the headers or body and use that information for some purpose. IndiMail also provides the following programs - 822addr(1), 822headerfilter(1), 822bodyfilter(1), 822field(1), 822fields(1), 822header(1), 822body(1), 822headerok(1), 822received(1), 822date(1), 822fields(1) to help in processing emails.

Let us say that we have written a script /usr/local/bin/myfilter. The myfilter program expects the raw email on stdin and outputs the email back (maybe modiying it) on stdout.

1.1 Filtering during SMTP (before mail gets queued)

1.1.1 Using FILTERARGS environment variable
The below configuration causes all inbound SMTP email to be fed through the filter /usr/local/bin/myfilter. You can use the programs 822header(1), 822body(1) inside myfilter to get and manipulate the headers and body (See 1.5.1).
% su root
# echo /usr/local/bin/myfilter > /service/qmail-smtpd.25/variables/FILTERARGS
# svc -d /service/qmail-smtpd.25
# svc -u /service/qmail-smtpd.25
NOTE: If the program myfilter returns 100, the message will be bounced. If it returns 2, the message will be discarded (blackholed).
1.1.2 Using QMAILQUEUE with qmail-qfilter
You can use qmail-qfilter(1). qmail-qfilter allows you to run multiple filters passed as command line arguments to qmail-qfilter. Since QMAILQUEUE doesn't allow you to specify multiple arguments, you can write a shell script which calls qmail-qfilter and have the shell script defined as QMAILQUEUE environment variable.
% su root
# echo /var/indimail/bin/qmail-qfilter /usr/local/bin/myfilter > /usr/local/bin/qfilter
# chmod +x /usr/local/bin/qfilter
# echo /usr/local/bin/qfilter > /service/qmail-smtpd.25/variables/QMAILQUEUE
# echo /var/indimail/bin/qmail-dk > /service/qmail-smtpd.25/variabels/QQF_QMAILQUEUE
# svc -d /service/qmail-smtpd.25
# svc -u /service/qmail-smtpd.25
NOTE: you can define QQF_MAILQUEUE to /var/indimail/bin/qmail-nullqueue to discard the mail (blackhole).
1.1.3 Using QMAILQUEUE with your own program
When you want to use your own program as QMAILQUEUE, then your program is responsible for queuing the email. It is trivial to queue the email by calling qmail-multi(8). You script can read the stdin for the raw message (headers + body) and pipe the output (maybe after modifications) to qmail-multi(8). If you are doing DK/DKIM signing, you can execute qmail-dk(8) instead of qmail-multi(8). You can have qmail-dk(8) call qmail-dkim(8) and qmail-dkim(8) calls qmail-multi(8). Assuming you want to do DK/DKIM signing, and myfilter calls qmail-dk(8), you can do the following
% su root
# echo /usr/local/bin/myfilter > /service/qmail-smtpd.25/variables/QMAILQUEUE
# echo /var/indimail/bin/qmail-dkim > /service/qmail-smtpd.25/variables/DKQUEUE
# echo /var/indimail/bin/qmail-multi > /service/qmail-smtpd.25/variables/DKIMQUEUE
# svc -d /service/qmail-smtpd.25
# svc -u /service/qmail-smtpd.25
NOTE: You can set the environment variable NULLQUEUE before calling qmail-multi to discard the mail completely (blackhole).
1.2 Filtering during local / remote delivery
1.2.1 Using FILTERARGS environment variable
The below configuration causes all local / remote deliveries to be fed through the filter /usr/local/bin/myfilter. You can use the programs 822header(1), 822body(1) inside myfilter to get and manipulate the headers and body.
% su root
# echo /usr/local/bin/myfilter > /service/qmail-send.25/variables/FILTERARGS
# svc -d /service/qmail-send.25
# svc -u /service/qmail-send.25
If you want to filter only for local delivery or only for remote delivery, you can use the environment variable QMAILLOCAL or QMAILREMOTE. QMAILLOCAL is defined only for local deliveries while QMAILREMOTE is defined only for remote deliveries.
NOTE: If the program myfilter returns 100, the message will be bounced. If it returns 2, the message will be discarded (blackholed).
e.g. the below script skips filtering for remote deliveries
#!/bin/sh
if [ -n “$QMAILREMOTE” ] ; then
    exec /bin/cat
fi
# rest of the script
...
...
exit 0
1.2.2 Using control file filterargs
The control file filterargs gives you control to run filters individually for local or remote deliveries. It also allows you to run your filter for both local and remote deliveries. See spawn-filter(8) for full description on this control file
e.g. The following entry in /var/indimail/control/filterargs causes all mails to yahoo.com be fed through the filter dk-filter(8) for DK/DKIM signing.
yahoo.com:remote:/var/indimail/bin/dk-filter
NOTE: If the program myfilter returns 100, the message will be bounced. If it returns 2, the message will be discarded (blackholed).
1.2.3 Using QMAILLOCAL or QMAILREMOTE environment variables
If you define QMAILLOCAL, indimail will execute the program/script defined by the QMAILLOCAL variable for all local deliveries. The arguments passed to this program/script will be the same as that for qmail-local(8).
Similarly, if you define QMAILREMOTE, indimail will execute the program/script defined by the QMAILREMOTE variable for all remote deliveries. The argument passed to this program/script are the same as that for qmail-remote(8).
The raw email (header + body) is available on stdin. You can use 822header(8), 822body(8) for getting the headers and body. After your program is through with filtering, the output should be piped to qmail-local(8) for local deliveries and qmail-remote(8) for remote deliveries. You need to also call qmail-local / qmail-remote with the same arguments. i.e
exec qmail-local  "$@"     #(for local deliveries)
exec qmail-remote "$@"     #(for remote deliveries)
NOTE: You can exit with value 0 instead of calling qmail-local / qmail-remote to discard the mail completely (blackhole)
1.3 Using dot-qmail(5) or valias(1)

Both .qmail files and valias mechanism allows you to execute your own programs for local deliveries. See the man pages for dot-qmail(5) and valias(1) for more details. After manipulating the original raw email on stdin, you can pipe the out to the program maildirdeliver(1) for the final delivery.
Assuming you write the program myscript to call maildirdeliver program, you can use the valias command to add the following alias
% valias -i "|/usr/local/bin/myfilter" testuser01@example.com
Now any mail sent to testuser01@example.com will be given to the program /usr/local/bin/myfilter as standard input.
NOTE: you can exit with value 0 instead of calling the maildirdeliver program to discard the mail completely (blackhole).
1.4 Using IndiMail rule based filter - vfilter
IndiMail's vfilter(8) mechanism allows you to create rule based filter based on any keyword in the message headers or message body. You can create a vfilter by calling the vcfilter(1) program.
% vcfilter -i -t myfilter -h 2 -c 0 -k "failure notice" -f /NoDeliver -b "2|/usr/local/bin/myfilter" testuser01@example.com
NOTE: you can exit with value 0 instead of putting anything on standard output to discard the mail completely (blackhole).
1.5 Examples Filters
e.g. the below filter looks for emails having "failure notice" in the subject line and inserts the line "sorry about that" in the first line of the message body and puts the line “sent by IndiMail Messaging platform” in the last line
1.5.1 FILTERARGS script
#bin/sh
# create a temporary file
tmp_file=`mktemp -p /var/tmp -t myfilter.XXXXXXXXXXXXXXX`
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file
    exit 111
fi
# Copy the stdin
/bin/cat > $tmp_file
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file
    exit 111
fi
subject=`/var/indimail/bin/822header -I Subject < $tmp_file`
echo $subject | grep "failure notice" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
    (
    /var/indimail/bin/822header < $tmp_file
    echo
    echo "sorry about that"
    /var/indimail/bin/822body < $tmp_file
    echo "sent by IndiMail Messaging platform"
    )
else
    /bin/cat $tmp_file
fi
/bin/rm -f $tmp_file
exit 0
1.5.2 QMAILQUEUE script
#!/bin/sh
# create a temporary file
inp_file=`mktemp -p /var/tmp -t myfilteri.XXXXXXXXXXXXXXX`
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file
    exit 111
fi
out_file=`mktemp -p /var/tmp -t myfiltero.XXXXXXXXXXXXXXX`
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file $out_file
    exit 111
fi
/bin/cat > $inp_file
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file $out_file
    exit 111
fi
subject=`/var/indimail/bin/822header -I Subject < $inp_file`
echo $subject | grep "failure notice" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
    (
    /var/indimail/bin/822header < $inp_file
    echo
    echo "sorry about that"
    /var/indimail/bin/822body < $inp_file
    echo "sent by IndiMail Messaging platform"
    ) > $out_file
    if [ $? -ne 0 ] ; then
        /bin/rm -f $inp_file $out_file
        exit 111
    fi
    exec 0<$out_file
else
    exec 0<$inp_file
fi
/bin/rm -f $inp_file $out_file
# queue the message
exec /var/indimail/bin/qmail-multi
exit 111
1.5.3 QMAILREMOTE script
#!/bin/sh
# This scripts expects qmail-remote arguments on command line
# argv0          - qmail-remote
# argv1          - host   (host)
# argv2          - sender (sender)
# argv3          - qqeh   (qmail queue extra header)
# argv4          - size
# argv5 .. argvn - recipients
# 
#
host=$1
sender=$2
qqeh=$3
size=$4
shift 4
#
# if needed you can modify host, sender, qqeh, size args above
#
if [ -z "$QMAILREMOTE" ] ; then # execute qmail-local
    # call spawn-filter so that features like
    # FILTERARGS, SPAMFILTER are not lost
    exec -a qmail-local /var/indimail/bin/spawn-filter "$@"
fi
if [ " $CONTROLDIR" = " " ] ; then
   FN=/var/indimail/control/filterargs
else
   FN=$CONTROLDIR/filterargs
fi
if [ -n "$SPAMFILTER" -o -n "$FILTERARGS" -o -f $FN ] ; then
   # execute spawn-filter if you have filters defined for remote/local deliveries
   PROG="bin/spawn-filter"
else
   PROG="bin/qmail-remote"
fi
# create a temporary file
inp_file=`mktemp -p /var/tmp -t myfilteri.XXXXXXXXXXXXXXX`
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file
    exit 111
fi
out_file=`mktemp -p /var/tmp -t myfiltero.XXXXXXXXXXXXXXX`
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file $out_file
    exit 111
fi
/bin/cat > $inp_file
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file $out_file
    exit 111
fi
subject=`/var/indimail/bin/822header -I Subject < $inp_file`
echo $subject | grep "failure notice" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
    (
    /var/indimail/bin/822header < $inp_file
    echo
    echo "sorry about that"
    /var/indimail/bin/822body < $inp_file
    echo "sent by IndiMail Messaging platform"
    ) > $out_file
    if [ $? -ne 0 ] ; then
        /bin/rm -f $inp_file $out_file
        exit 111
    fi
    exec 0<$out_file
else
    exec 0<$inp_file
fi
/bin/rm -f $inp_file $out_file
# $PROG points to spawn-filter if FILTERARGS or SPAMFILTER is set
# use $PROG so that features like FILTERARGS, SPAMFILTER are not lost
exec -a qmail-remote $PROG "$host" "$sender" "$qqeh" $size $*
exit 111
1.5.4 QMAILLOCAL script
#!/bin/sh
# This scripts expects qmail-local arguments on command line
# argv0          - qmail-local
# argv1          - user
# argv2          - homedir
# argv3          - local
# argv4          - dash
# argv5          - ext
# argv6          - domain
# argv7          - sender
# argv8          - defaultdelivery (mbox, Maildir)
# argv9          - qqeh
#
user=$1
homedir=$2
local=$3
dash=$4
ext=$5
domain=$6
sender=$7
defaultdel=$8
qqeh=$9

if [ -z "$QMAILLOCAL" ] ; then # execute qmail-remote
    # call spawn-filter so that features like
    # FILTERARGS, SPAMFILTER are not lost
    exec -a qmail-remote /var/indimail/bin/spawn-filter "$@"
fi
# create a temporary file
inp_file=`mktemp -p /var/tmp -t myfilteri.XXXXXXXXXXXXXXX`
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file
    exit 111
fi
out_file=`mktemp -p /var/tmp -t myfiltero.XXXXXXXXXXXXXXX`
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file $out_file
    exit 111
fi
/bin/cat > $inp_file
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file $out_file
    exit 111
fi
subject=`/var/indimail/bin/822header -I Subject < $inp_file`
echo $subject | grep "failure notice" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
    (
    /var/indimail/bin/822header < $inp_file
    echo
    echo "sorry about that"
    /var/indimail/bin/822body < $inp_file
    echo "sent by IndiMail Messaging platform"
    ) > $out_file
    if [ $? -ne 0 ] ; then
        /bin/rm -f $inp_file $out_file
        exit 111
    fi
    exec 0<$out_file
else
    exec 0<$inp_file
fi
/bin/rm -f $inp_file $out_file

# call spawn-filter so that features like
# FILTERARGS, SPAMFILTER are not lost
exec -a qmail-local /var/indimail/bin/spawn-filter "$@"
exit 111
1.5.5 valias / vfilter script
#!/bin/sh
# create a temporary file
inp_file=`mktemp -p /var/tmp -t myfilteri.XXXXXXXXXXXXXXX`
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file
    exit 111
fi
out_file=`mktemp -p /var/tmp -t myfiltero.XXXXXXXXXXXXXXX`
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file $out_file
    exit 111
fi
/bin/cat > $inp_file
if [ $? -ne 0 ] ; then
    /bin/rm -f $inp_file $out_file
    exit 111
fi
subject=`/var/indimail/bin/822header -I Subject < $inp_file`
echo $subject | grep "failure notice" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
    (
    /var/indimail/bin/822header < $inp_file
    echo
    echo "sorry about that"
    /var/indimail/bin/822body < $inp_file
    echo "sent by IndiMail Messaging platform"
    ) > $out_file
    if [ $? -ne 0 ] ; then
        /bin/rm -f $inp_file $out_file
        exit 111
    fi
    exec 0<$out_file
else
    exec 0<$inp_file
fi
/bin/rm -f $inp_file $out_file
#
# unset RPLINE so that maildirdeliver does not add a duplicate Return-Path line
# unset PWSTRUCT so that password structure cached is removed for vuserinfo to
# work correctly
#
unset RPLINE PWSTRUCT
dir=`/var/indimail/bin/vuserinfo -d testuser01@example.com | cut -d: -f2 |cut -c2-`

if [ $? -ne 0 -o " $dir" = " " ] ; then
    echo "unable to get user's homedir" 1>&1
    exit 111
fi
exec /var/indimail/bin/maildirdeliver "$dir"/Maildir
exit 111

Friday, June 3, 2011

Explanation of email delivery mechanism for local users

Any email that needs to be delivered needs to be put into a queue before it can be taken up for delivery. Email can be submitted to the queue using qmail-queue command or qmail_open() function. The following programs use the qmail_open() API -
condredirect, dot-forward, fastforward, filterto, forward, maildirserial, new-inject, ofmipd, qmail-inject, qmail-local, qmail-qmqpd, qmail-qmtpd, qmail-queue, qmail-send, qreceipt, replier, rrforward, qmail-smtpd.
Of these, qmail-smtpd and qmail-qmtpd accept an email for a domain only if the domain is listed in rcpthosts. Once an email is accepted into the queue, qmail-send(8) decides if the mail is to be delivered locally or to a remote address. If the email address corresponds to a domain listed in locals or virtualdomains control file, steps are taken to have the email delivered locally.

Delivery Mode
The delivery mode depends on the argument passed to qmail-daemon during startup. The script /service/qmail-send.25/run passes the content of the file /var/indimail/control/defaultdelivery as an argument to qmail-daemon.
See INSTALL.mboxINSTALL.maildir, and INSTALL.vsm for more information.
To select your default mailbox type, just enter the defaultdelivery value from the table into /var/indimail/control/defaultdelivery.
e.g., to select the standard qmail Mailbox delivery, do:

echo ./Maildir/ > /var/indimail/control/defaultdelivery

Addresses

Once you have decided the delivery mode above, one needs to have some mechanism to assign a local address for the delivery. qmail (which is what IndiMail uses) offers the following mechanism

locals 
Any email addressed to user@domain listed in the file /var/indimail/control/locals will be delivered to the local user user. If you have Maildir as the delivery mode and an email to user kanimoji@domain, with home directory /home/blackmoney, will be delivered to /home/blackmoney/Maildir/new

virtualdomains

The control file /var/indimail/control/virtualdomains allows you to have multiple domains configured on a single server. Entries in virtualdomains are of the form:

user@domain:prepend

qmail converts user@domain to prepend-user@domain and treats the result as if domain was local. The user@ part is optional. If it's omitted, the entry matches all @domain addresses.

When you run the command

vadddomain example.com some_password

It will add the following entry in virtualdomains control file

example.com:example.com

What this means is that any email addressed to user@example.com will be delivered to the address example.com-user@example.com.

IndiMail further uses qmail-users mechanism to deliver the email for users in a virtual domain. This is explained  below

qmail-users

The file /var/indimail/users/assign assigns addresses to users.


A simple assignment is a line of the form

=local:user:uid:gid:homedir:dash:ext:

Here local is an address; user, uid, and gid are the account name, uid, and gid o the user in charge of local; and messages to local will be controlled by

homedir/.qmaildashext.

If there are several assignments for the same local address, qmail-lspawn will use the first one. local is interpreted without regard to case.

A wildcard assignment is a line of the form

+loc:user:uid:gid:homedir:dash:pre:

This assignment applies to any address beginning with loc, including loc itself.  

It means the same as

=locext:user:uid:gid:homedir:dash:preext:

for every string ext.

When you add a virtualdomain using vadddomain, you will have the following entry

+example.com-:example.com:555:555:/var/indimail/domains/example.com:-::

As stated earlier,  any email addressed to user@example.com will be delivered to local user example.com-user@example.com because of virtualdomains control file. The above address can be looked as

=user@example.com:example.com:555:555:/var/indimail/domains/example.com:-:user:

So you can see that emails are controlled by .qmail-user in the directory /var/indimail/domains/example.com. if .qmail-user does not exist, then .qmail-default will be used

Adding the entry

+example.com-customer_care-:example.com:555:555:/var/indimail/domains/example.com/cc:-::

will cause emails to customer_care-delhi@example.com, customer_care-mumba@example.com, etc to be handled by /var/indimail/domains/cc/.qmail-default (if .qmail-customer_care-delhi does does not exist).

Extension Addresses

In the qmail system, you control all local addresses of the form user-anything, as well as the address user itself, where user is your account name. Delivery to user-anything is controlled by the file homedir/.qmail-anything. (These rules may be changed by editing the assign file as given above in qmail-users. 
The alias user controls all other addresses. Delivery to user is controlled by the file homedir/.qmail-user, where homedir is alias's home directory.
In the following description, qmail-local is handling a message addressed to local@domain, where local is controlled by .qmail-ext. Here is what it does.
If .qmail-ext is completely empty, qmail-local follows the defaultdelivery instructions set by your system administrator. 
If .qmail-ext doesn't exist, qmail-local will try some default .qmail files. For example, if ext is foo-bar, qmail-local will try first .qmail-foo-bar, then .qmail-foo-default, and finally .qmail-default. If none of these exist, qmail-local will bounce the message. (Exception: for the basic user address, qmail-local treats a nonexistent .qmail the same as an empty .qmail.) 
The vadddomain command creates the file .qmail-default in /var/domain/domains/domain_name. Hence any email addressed to user@example.com gets controlled by /var/indimail/domains/example.com/.qmail-default. 

WARNING: For security, qmail-local replaces any dots in ext with colons before checking .qmail-ext. For convenience, qmail-local converts any uppercase letters in ext to lowercase.





IndiMail Queue Mechanism

Indimail has the ability of configuring multiple local and remote queues. A queue is a location on your hard disk where email are deposited ...