#!/bin/bash # Generate virtual file for postfix # # Each user should have space-separated hints like "mailname1@ mailname2@gid1 mailname3@gid2" # stored in the "Other" field (using chfn). # # The user of each mailgroup should have hints like "@domain1 @domain2" for each hosted domain # stored in the "Other" field (using chfn). #TODO: reuse getent requests (drastically improves speed with multiple domains) function get_fullname_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $1}'; } function get_roomnumber_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $2}'; } function get_other_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $5}'; } function get_domain() { echo $1 | egrep "^@[\.[:alnum:]]+$" | sed -e 's/@//'; } function get_account() { echo $1 | egrep "^[\.[:alnum:]]+@($gid)?$" | sed -e 's/@.*//'; } loop="" for gid in $@; do for maildomainchunk in `get_other_field $gid`; do for maildomain in `get_domain $maildomainchunk`; do if [ $loop ]; then echo echo "##################################################################" echo fi loop=true echo "$maildomain VIRTUAL" echo "postmaster@$maildomain root" echo mailusers="" for mailgroup in `get_roomnumber_field $gid`; do mailusers="$mailusers `members $mailgroup`" done # for uid in `members $gid | sort`; do for uid in `echo $mailusers | tsort | uniq | sort`; do echo "# `get_fullname_field $uid` (`get_roomnumber_field $uid`)" uid_seen="" for mailaccountchunk in `get_other_field $uid`; do for mailaccount in `get_account $mailaccountchunk`; do echo "$mailaccount@$maildomain $uid" uid_seen=true done done if [ ! $uid_seen ]; then echo "#WARNING: No addresses for $uid" fi echo done done done done