#!/bin/bash # # /usr/local/sbin/localmkpostfixvirtual # Copyright 2001-2002 Jonas Smedegaard # # $Id: localmkpostfixvirtual,v 1.5 2002-03-07 16:22:51 jonas Exp $ # # Generate virtual file for postfix # # Hints are stored in the "Other" field (using chfn). # # Each user should have space-separated hints like "mailname1@ mailname2@gid1 mailname3@gid2". # # The user of each mailgroup should have hints like "@domain1 @domain2" for each hosted domain. # # Optional: root can have hints like "postmaster@ hostmaster@ support@" (default: "postmaster@"). # # TODO: reuse getent requests (drastically improves speed) # 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_groups() { groups $1 | sed -e 's/^.*: //' -e "s/\( \+\|^\)$1\( \+\|$\)/\1/"; } 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" uid_seen="" for mailaccountchunk in `get_other_field root`; do for mailaccount in `get_account $mailaccountchunk`; do echo "$mailaccount@$maildomain root" uid_seen=true done done if [ ! $uid_seen ]; then echo "postmaster@$maildomain root" fi 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_groups $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