#!/bin/sh # # /usr/local/sbin/localmailrecipientsdump # Copyright 2004-2006 Jonas Smedegaard # # Dump a list of all local mail destinations # set -e pfdir="/etc/postfix" firstitem() { set -e file="$1" cat "$file" | grep -v '^#' | sed 's/#.*//' | head -n 1 } localdomains() { set -e postconf -h mydestination | tr ',' ' ' } aliasmaps() { set -e postconf -h alias_maps | tr ',' ' ' } virtualmaps() { set -e newmaps="$(postconf -h virtual_alias_maps)" if [ '$virtual_maps' = "$newmaps" ]; then perl -ne 's/^virtual_maps\s*=\s*// and print' < /etc/postfix/main.cf | tr ',' ' ' else echo "$newmaps" | tr ',' ' ' fi } accounts() { set -e ( getent passwd | perl -ne 's/:.*// and s/^([a-z]\S*).*/$1/ and print' for map in $(aliasmaps); do postalias -s $map | perl -ne 's/^([a-z]\S*):.*/$1/ and print' done ) | sort -u } mkrecipientsmap() { for domain in $(localdomains); do for account in $(accounts); do echo "$account@$domain OK" done done for map in $(virtualmaps); do case $map in regexp:/etc/mail/sympa_virtual_regexp_*) # FIXME: replace catch-all with actual accounts echo "$map" | perl -ne 's,regexp:/etc/mail/sympa_virtual_regexp_(\S+).*,\@$1 OK, and print' ;; hash:*) postmap -s $map | perl -ne 's/^([a-z]\S*\@\S+)\s.*/$1 OK/ and print' ;; *) echo >&2 "Unsupported map type: $map" exit 1 ;; esac done } redpill="$(firstitem "/etc/local-ORG/redpill")" realm="$(firstitem "/etc/local-ORG/realm")" mkrecipientsmap | LC_ALL=C sort -t@ -k2 -k1 > "/etc/local-$redpill/$realm/mailrecipients"