#============================================================================# # vHost - one-step solution for all virtual hosting needs # # V20postfix - postfix virtualization module # # # # Copyright(c) Chaogic Systems, LLC. http://chaogic.com # # Author: Jake Fan # # # # This is Free Software; permission to use, copy, modify, and distribute # # this software and its documentation for any purpose - with or without fee # # - is hereby granted, provided that the above copyright information and # # this permission notice appear in all copies and documentations. This # # software is provided "as is" without express or implied warranty. For # # more details, see GNU General Public License as published by the Free # # Software Foundation. # # # #============================================================================# $X eq "configuration" && do { ################################################ ### EDITABLE CONFIGURATIONS ################################################## ### postfix configuration directory which contains "main.cf" and "master.cf". # $postfixCf = "/etc/postfix"; $postfixCf = "/etc/postfix"; ### postfix reload command sequence. separate multiple commands with shell # meta characters such as "|", ";", "||", "&&", etc. leave blank if invoked # by inetd. # $postfixRc = "/etc/rc.d/init.d/postfix reload"; $postfixRc = "/etc/rc.d/init.d/postfix reload"; ### mailbox directory name under virtual users' home directories. # $mboxDir = "mail"; $mboxDir = "mail"; ### command "procmail". leave blank to disable procmail support. # $procmail = "/usr/bin/procmail"; $procmail = "/usr/bin/procmail"; ### command "vdeliver". # $vdeliver = "/usr/local/vhost/bin/vdeliver"; $vdeliver = "/usr/local/vhost/bin/vdeliver"; ### main host aliases.db update command. # $newaliases = "/usr/sbin/sendmail -bi"; $newaliases = "/usr/sbin/sendmail -bi"; ### command "postmap". # $postmap = "/usr/sbin/postmap"; $postmap = "/usr/sbin/postmap"; ### DO NOT TOUCH ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!! ### SanityChecking ########################################################### $ERR = 1, Error "$MOD: \$postfixCf = '$postfixCf'" if !-d $postfixCf; $ERR = 1, Error "$MOD: \$postfixRc = '$postfixRc'" if $postfixRc && ($postfixRc !~ /^\// || !-x (split /[\s|&;]+/, $postfixRc)[0]); $ERR = 1, Error "$MOD: \$procmail = '$procmail'" if $procmail && ($procmail !~ /^\// || !-x (split /[\s|&;]+/, $procmail)[0]); $ERR = 1, Error "$MOD: \$vdeliver = '$vdeliver'" if $vdeliver !~ /^\// || !-x (split /[\s|&;]+/, $vdeliver)[0]; $ERR = 1, Error "$MOD: \$newaliases = '$newaliases'" if $newaliases !~ /^\// || !-x (split /[\s|&;]+/, $newaliases)[0]; $ERR = 1, Error "$MOD: \$postmap = '$postmap'" if $postmap !~ /^\// || !-x (split /[\s|&;]+/, $postmap)[0]; $postfixCv = "$postfixCf/postfix.cV"; }; $X eq "initialization" && do { ############################################### if ($ONCFG) { `touch $postfixCv $NERR` if !-e $postfixCv; `ln /etc/conf.linuxconf /etc/vdeliver $NERR` if !-e "/etc/vdeliver"; `chown 0:$GHID $postfixCv $NERR` if !-o $postfixCv; `chown 0:$GHID /etc/vdeliver $NERR` if !-o "/etc/vdeliver"; `chmod 0600 $postfixCv $NERR` if ((stat $postfixCv)[2] & 07777) != 0600; `chmod 0600 /etc/vdeliver $NERR` if ((stat "/etc/vdeliver")[2] & 07777) != 0600 && !-e "/etc/conf.linuxconf"; `$postmap hash:$postfixCv $NERR` if !-e "$postfixCv.db"; `touch $postfixCf/poprelay $NERR`, `chmod 0600 $postfixCf/poprelay $NERR`, `$postmap hash:$postfixCf/poprelay $NERR`, `mv $postfixCf/poprelay.db /etc/poprelay.db $NERR`, `rm $postfixCf/poprelay $NERR` if !-e "/etc/poprelay.db"; my $ok; Flock "$postfixCf/main.cf", "+>>", 2 if !grep /^\s*transport_maps\s*=.*hash:\Q$postfixCv\E(,|\s|$)/, Cat "$postfixCf/main.cf"; BackupCf "$postfixCf/main.cf"; print FILE "\n$FOOT\n" if !grep /^\s*$FOOT/, @FILE; print FILE "inet_interfaces = all\n" if !grep /^\s*inet_interfaces\s*=\s*all\s*$/, @FILE; print FILE "transport_maps = hash:$postfixCv\n"; print FILE "relay_domains = hash:$postfixCv\n" if !grep /^\s*relay_domains\s*=.*hash:\Q$postfixCv\E(,|\s|$)/, @FILE; Flock 0; Flock "$postfixCf/main.cf", "+>>", 2 if !grep /^\s*mynetworks\s*=.*hash:\/etc\/poprelay(,|\s|$)/, Cat "$postfixCf/main.cf"; print FILE "mynetworks = 127.0.0.0/8 hash:/etc/poprelay\n"; Flock 0; Flock "$postfixCf/master.cf", "+>>", 2 if !grep /^[^#]*\suser\s*=.*\/vhost\s+--vdeliver\s/, Cat "$postfixCf/master.cf"; BackupCf "$postfixCf/master.cf"; print FILE "\n$FOOT\n" if !grep /^\s*$FOOT/, @FILE; print FILE "vhost unix - n n - - pipe\n"; print FILE " flags=BFR user=nobody argv=/usr/sbin/vhost --vdeliver \$user \$nexthop\n"; Flock 0; Flock "$postfixCf/master.cf", "+<", 0 if !grep /^[^#]*\suser\s*=\s*nobody\s.*--vdeliver\s/, Cat "$postfixCf/master.cf"; s/(\suser\s*=\s*)\S+(\s.*--vdeliver\s)/$1nobody$2/ for @FILE; print FILE @FILE; Flock 0; Flock "/etc/vdeliver", ">>", 2 if !grep /^\s*\[vdomain\]/, Cat "/etc/vdeliver"; print FILE "[vdomain]\n"; Flock 0; } }; $X eq "addhost" && do { ###################################################### `mkdir $VHOME/home/$O/$mboxDir $NERR`; `chown $O[2]:$GUID $VHOME/home/$O/$mboxDir $NERR`; `chmod 0700 $VHOME/home/$O/$mboxDir $NERR`; my ($i, $j); my $q = sprintf ("%.0f", GetNb ($QHOST[0]) / 1024) || ""; Flock $postfixCv, "+>>", 2; print FILE "$H vhost\n" if !grep /^\s*\Q$H\E\s+vhost\s*$/, @FILE; Flock 0; Flock "/etc/vdeliver", "+<", 0; s/^(\s*vdomain_quota\.\Q$H\E)(\s|$).*/$1 $q\n/s, /^\s*\[*vdomain[_.\]]/ && ($j = $i), $i++ for @FILE; $FILE[$j||$i] .= "vdomain.index $H\n" if !grep /^\s*vdomain\.index\s+\Q$H\E\s*$/, @FILE; $FILE[$j||$i] .= "vdomain_quota.$H $q\n" if !grep /^\s*vdomain_quota\.\Q$H\E\s/, @FILE; $FILE[$j||$i] .= "vdomain_fallback.$H\n" if !grep /^\s*vdomain_fallback\.\Q$H\E\s/, @FILE; print FILE @FILE; Flock 0; `$postmap hash:$postfixCv $NERR`; ReloadRc 0; }; $X eq "delhost" && do { ###################################################### if ($_d) { my $re = join "|", $H, GetAh $H; $re =~ s/\./\\./g; Flock $postfixCv, "+<", 0; s/^\s*($re)\s+vhost\s*$//s for @FILE; print FILE @FILE; Flock 0; Flock "/etc/vdeliver", "+<", 0; s/^\s*vdomain(\.index\s+|_\w+\.)\Q$H\E(\s|$).*//s for @FILE; print FILE @FILE; Flock 0; } `$postmap hash:$postfixCv $NERR`; ReloadRc 0 if $_d; }; $X eq "addhali" && do { ###################################################### my ($i, $j, $k); Flock $postfixCv, "+>>", 2; print FILE "$A vhost\n" if !grep /^\s*\Q$A\E\s+vhost\s*$/, @FILE; Flock 0; Flock "/etc/vdeliver", "+<", 0; /^\s*vdomain(\.index\s+|_\w+\.)\Q$H\E(\s|$)/ && ($k = $i), /^\s*\[*vdomain[_.\]]/ && ($j = $i), $i++ for @FILE; $FILE[$k||$j||$i] .= "vdomain_other.$H $A\n" if !grep /^\s*vdomain_other\.\Q$H\E\s+\Q$A\E\s*$/, @FILE; print FILE @FILE; Flock 0; `$postmap hash:$postfixCv $NERR`; ReloadRc 0; }; $X eq "delhali" && do { ###################################################### Flock $postfixCv, "+<", 0; s/^\s*\Q$A\E\s+vhost\s*$//s for @FILE; print FILE @FILE; Flock 0; Flock "/etc/vdeliver", "+<", 0; s/^\s*vdomain_other\.\Q$H\E\s+\Q$A\E\s*$//s for @FILE; print FILE @FILE; Flock 0; `$postmap hash:$postfixCv $NERR`; ReloadRc 0; }; $X eq "adduser" && do { ###################################################### `mkdir $homeDir/$U/$mboxDir $NERR` if $LOH; `chmod 0700 $homeDir/$U/$mboxDir $NERR` if $LOH; `mkdir $VHOME/$H/home/$U/$mboxDir $NERR` if !$LOH; `chmod 0700 $VHOME/$H/home/$U/$mboxDir $NERR` if !$LOH; }; $X eq "adduali" && do { ###################################################### if ($V eq "catch-all@") { my ($ok, $i, $j, $k); Flock "/etc/vdeliver", "+<", 0; s/^(\s*vdomain_fallback\.\Q$H\E)(\s|$).*/$1 $N[0]\n/s && ($ok = 1), /^\s*vdomain(\.index\s+|_\w+\.)\Q$H\E(\s|$)/ && ($k = $i), /^\s*\[*vdomain[_.\]]/ && ($j = $i), $i++ for @FILE; $FILE[$k||$j||$i] .= "vdomain_fallback.$H $N[0]\n" if !$ok; print FILE @FILE; Flock 0; } `$newaliases $NERR` if $LOH; }; $X eq "deluali" && do { ###################################################### if ($V eq "catch-all@") { Flock "/etc/vdeliver", "+<", 0; s/^(\s*vdomain_fallback\.\Q$H\E)(\s|$).*/$1\n/s for @FILE; print FILE @FILE; Flock 0; } `$newaliases $NERR` if $LOH; }; $X eq "qhost" && do { ######################################################## my ($ok, $i, $j, $k); my $q = sprintf ("%.0f", GetNb ($Q[0]) / 1024) || ""; Flock "/etc/vdeliver", "+<", 0; s/^(\s*vdomain_quota\.\Q$H\E)(\s|$).*/$1 $q\n/s && ($ok = 1), /^\s*vdomain(\.index\s+|_\w+\.)\Q$H\E(\s|$)/ && ($k = $i), /^\s*\[*vdomain[_.\]]/ && ($j = $i), $i++ for @FILE; $FILE[$k||$j||$i] .= "vdomain_quota.$H $q\n" if !$ok; print FILE @FILE; Flock 0; }; $X eq "reload" && do { ####################################################### ReloadRc 0 if $_f && $USER eq "root"; ReloadRc $postfixRc; }; $X eq "vdeliver" && do { ##################################################### my ($u, $d, $i); my $m = !$LOH && -d "$HOME{$PROF{$H}}/$H/tmp"? "$HOME{$PROF{$H}}/$H/tmp/.msg.$$" : "/tmp/.msg.$$"; my $o = !$LOH && -d "$HOME{$PROF{$H}}/$H/tmp"? "$HOME{$PROF{$H}}/$H/tmp/.out.$$" : "/tmp/.out.$$"; $#ARGV > 2 && `cat >$m $NERR`; -f $o && `rm $o $NERR`, $u = $LOH && (join ("", @VPWD) =~ /^\s*(\Q$_\E):/im)[0] || lc, $d = $LOH && (getpwnam $u)[7] || "$HOME{$PROF{$H}}/$H/home/$u", $i = (stat $d)[4] || $O[2], $< = $i, $( = 2000, $procmail? `(${\(-f $m && "cat $m | ")}$procmail HOME=$d DEFAULT=$o) $NERR` : `ln $m $o $NERR`, $< = 0, $( = 1999, ($procmail? -s $o : -s $o || !-f $m) && `(${\(-f $o && "cat $o | ")}$vdeliver $_ $ARGV[-1] $i $d) $NERR`, $ERR ||= $? >> 8 for @ARGV[1..$#ARGV-1]; -f $o && `rm $o $NERR`; $#ARGV > 2 && `rm $m $NERR`; }; $X eq "finalization" && do { ################################################# if ($ARGV[0] eq "poprelay" && $popReload) { `$postmap hash:/etc/poprelay $NERR`; `chcon -t postfix_etc_t /etc/poprelay.db $NERR` if -e "/selinux"; } ReloadRc $postfixRc if !$_q && $ARGV[0] =~ /^(addhost|delhost|addhali|delhali)$/; }; 1;