Zimbra: Create Distribution Lists from a File, 4 Easy Fixes

To create distribution lists from a file in Zimbra, you loop over the addresses and call zmprov cdl for each one that does not exist yet. The script in the original article does that, but when it was tested it skipped lists that were missing, tried to create a list with an empty name, and created names that ended with a hidden carriage return. This rewrite shows the test and a corrected script.

How this was tested: there is no Zimbra server in the author's lab. The script ran in a debian:13 container where zmprov is a small stand-in that remembers a list of existing distribution lists, prints them for gadl, and records the exact argument of every cdl call. So the bash behaviour is real, and the Zimbra commands are simulated. The syntax of cdl and gadl comes from the Zimbra Administrator Guide, which gives zmprov cdl needlepoint-list@domain.com as the example for creating a list and lists getAlldistributionLists (gadl) for reading them.

How the create distribution lists script works

The original script reads /tmp/list_distribute.txt line by line. For each name it asks Zimbra for all lists (zmprov gadl) and pipes the answer to grep. If grep finds the name, the script prints "Exists!"; otherwise it runs zmprov cdl through su - zimbra -c, because zmprov must run as the zimbra user and the script is started as root:

List_DL=/tmp/list_distribute.txt
while read count
do
Check_DL=$(su - zimbra -c "zmprov gadl | grep $count")
if [ $? == 0 ]
then
# To check distribution lists name exists or not!
echo "$count Exists!"
else
# To create distribution list
su - zimbra -c "zmprov cdl $count"
fi
done < $List_DL

Test the create distribution lists script

To check how the script decides when to create distribution lists, the lab started with three existing lists: presales@example.com, all-hr@example.com and it@example.com. The input file had five lines: sales, hr, it, an empty line, and dev without a final newline. The goal is that sales, hr and dev get created, and it is reported as existing:

$ bash orig.sh
sales@example.com Exists!
hr@example.com Exists!
it@example.com Exists!
Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.
created by the script (cdl calls):
CDL <>$

Three of the four problems that stop the script from creating distribution lists correctly show up in this one run:

  • Substring match. sales@example.com is "found" inside presales@example.com, and hr@example.com inside all-hr@example.com. Both lists that were needed are reported as existing and never created.
  • Empty line. The blank line makes the pattern empty, grep prints its usage message and exits with an error, and the script then calls cdl with an empty name (the line CDL <>).
  • Last line without newline. dev@example.com never appears in the output at all, because read returns a failure status when the final line has no newline, so the loop stops before processing it.
Create distribution lists script test: the original script reports existing lists wrongly and calls cdl with an empty name
The original script on the test file: wrong "Exists!" results and a cdl call with an empty name.

The fourth problem appears when the file was saved on Windows. Each line then ends with a carriage return, shown as ^M by cat -A, and the script uses it as part of the name:

$ bash orig.sh
created by the script (cdl calls):
CDL <sales@example.com^M>$
CDL <it@example.com^M>$
CDL <dev@example.com^M>$

Even it@example.com, which already existed, is created again, because the name with the extra character does not match the existing one.

A corrected create distribution lists script

The new version asks Zimbra for the existing lists only once, matches whole lines with grep -Fx (fixed string, whole line, ignoring case), removes a trailing carriage return and spaces, skips blank lines and comments, refuses names that do not look like an e-mail address, and keeps reading when the last line has no newline. The address check also protects you, because the name is placed inside a su -c string.

#!/bin/bash
# Create Zimbra distribution lists from a file (one address per line). Run as root.
LIST=${1:-/tmp/list_distribute.txt}
EXISTING=$(su - zimbra -c "zmprov gadl")          # ask Zimbra once, not once per line
while IFS= read -r name || [ -n "$name" ]; do      # also reads a last line without newline
  name=${name%$'\r'}                               # file saved on Windows (CRLF)
  name=${name//[[:space:]]/}
  case "$name" in ''|'#'*) continue ;; esac        # skip blank lines and comments
  if ! [[ "$name" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+$ ]]; then
    echo "INVALID $name"; continue
  fi
  if grep -Fxqi -- "$name" <<<"$EXISTING"; then    # whole-line match, not a substring
    echo "EXISTS  $name"
  else
    su - zimbra -c "zmprov cdl $name" && echo "CREATED $name"
    EXISTING+=$'\n'"$name"
  fi
done < "$LIST"

The same two input files, now with the corrected script:

$ bash new.sh /lab/lf.txt
CREATED sales@example.com
CREATED hr@example.com
EXISTS  it@example.com
CREATED dev@example.com
created by the script (cdl calls):
CDL <sales@example.com>$
CDL <hr@example.com>$
CDL <dev@example.com>$

$ bash new.sh /lab/crlf.txt
CREATED sales@example.com
EXISTS  it@example.com
CREATED dev@example.com
created by the script (cdl calls):
CDL <sales@example.com>$
CDL <dev@example.com>$

Now the corrected way to create distribution lists works: the lists that must be created are created, the existing one is only reported, and the Windows file works. In the CRLF test the input had no hr line, so only two lists are created. The read -r option and the || [ -n "$name" ] trick are documented in the Bash manual under the read builtin.

One more test covered the checks you need when you create distribution lists from a file written by someone else. The file had a comment line, a good name and a bad line, bad name; touch /tmp/pwned. The comment was skipped, the good name was created, and the bad line was refused as INVALID without running anything (the file /tmp/pwned does not exist afterwards):

$ bash new.sh /lab/bad.txt
CREATED ops@example.com
INVALID badname;touch/tmp/pwned
created by the script (cdl calls):
CDL <ops@example.com>$
ls: cannot access '/tmp/pwned': No such file or directory

Run it safely on a real server

To create distribution lists on a real server, run the script as root with the file as its argument, for example bash zimbra_Distribute.sh /tmp/list_distribute.txt. Check the first run with a file of two or three test names, then read the output: every name should be CREATED, EXISTS or INVALID. After creating lists you can add members with zmprov adlm and limit who may send to them; the guide on restricting sending to a distribution list shows the commands, and the zmprov command guide explains the syntax conventions.

Common mistakes when you create distribution lists

  • Using plain grep name to test for existence, which matches parts of other names.
  • Editing the name file on Windows and not removing the carriage returns.
  • Running zmprov as root instead of through su - zimbra -c.
  • Trying to create distribution lists from a big file without a check run on a small test file first.

FAQ

Can the script that creates distribution lists add members as well? Yes: the Administrator Guide lists addDistributionListMember (adlm) as zmprov adlm list@domain member@domain, and the same loop pattern works with a second file. The original post announced this as its next article.

Why not ask Zimbra for each name before you create distribution lists? It is slow: every su - zimbra -c "zmprov ..." starts a new login shell and a new zmprov session, so one request for the full list is faster.

Comments

Popular posts from this blog

How to Play Minecraft Bedrock Edition on Linux: A Comprehensive Guide for Tech Professionals

Linux desktop frozen? My 5 go-to tricks to try - before forcing a hard reboot

The Ultimate Guide: How to Set Up DXVK in Wine on Linux for Enhanced Gaming Performance