Exporting a list of email addresses from Kerio Mail Server

Kerio Mail Server (KMS, now Kerio Connect) is a good mail/groupware server with support for a staggering amount of clients and devices, including the ubiquitous Microsoft Outlook. Administration via the web/Mac/Win32 administration interface is a breeze and the server has modest hardware requirements.

Adminstration via the backend isn’t so easy though. If you want a report on who’s about to exhaust their quota - or already have - you’re out of luck. Making a list of all valid email addresses on the server is equally difficult, which means you have to do a lot of keyboard work to maintain user lists for e.g. an SMTP gateway or an antispam/antivirus appliance. KMS’ builtin LDAP does not return anything useful in this regard since it’s only meant to facilitate recipient autocompletion. I.e. you can’t ask Kerio’s LDAP for a list of email addresses.

Luckily though, the Kerio guys have put (almost) all configuration in flat files which are easy-ish to parse. The file containing KMS’ core config is stored in two well defined XML files which can easily be parsed with the open source program xmlstarlet. Yes, there is also a Windows version of xmlstarlet in case you wondered, but if you run KMS on Windows you’ll have to come up with your own wrapper script.

The following script simply dumps all the mail addresses from users.cfg and mailserver.cfg, including disabled accounts. If you want the output sorted by domain, pipe the script output through sort: kmsexpadr | sort -t”@” -k2.

Note: This script will only export internal accounts. Accounts mapped to Open Directory or Active Directory should be trivial to export with e.g. ldapsearch on OS X or your favourite Linux flavour, or with a suitable VBScript if you’re on Windows.

#!/bin/sh
# kmsexpadr
# Dumps a list of valid email addresses in KMS to stdout
# Handles:
# - Primary and all secondary addresses
# - Aliases
# - Mailing lists
#
# Tested with KMS 6.7.2
# Requires xmlstarlet (and dependencies) to work (http://xmlstar.sourceforge.net)
# 20100211/twa/kabj

KMSDIR=/opt/kerio/mailserver
USERS=users.cfg
MAILSERVER=mailserver.cfg

# List primaries, secondaries and aliases
xmlstarlet sel -t -m "/config/list[@name = 'User']/listitem" -v "variable[@name = 'Name']" -o "@" -v "variable[@name = 'Domain']" -n \
                -t -m "/config/list[@name='Alias']/listitem" -v "variable[@name='Lhs']" -o "@" -v "variable[@name='Domain']" -n \
                $KMSDIR/$USERS

# List mailing lists
xmlstarlet sel -t -m "/config/list[@name='MailingList']/listitem" \
                -v "variable[@name='Name']" -o "@" -v "variable[@name='Domain']" -n \
                $KMSDIR/$MAILSERVER

What you do with the generated list is up to you. I export it with scp to the SMTP gateway every half hour so Exim can reject mail to unknown addresses and do some spamtrap handling.

Please test the script on a copy of your config files! The script is provided “as is”, without any guarantees, promises of support or acceptance of liability. If you break something, I’m not buying it. If you have questions or feedback, leave a comment here or contact me on the Kerio Support forums. I’m “TorW”.

One Response to “Exporting a list of email addresses from Kerio Mail Server”

  1. rafha Says:

    nice… thx very much!!!

Leave a Reply