#============================================================================# # vHost - one-step solution for all virtual hosting needs # # V20sendmail - sendmail 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 ################################################## ### sendmail configuration file. # $sendmailCf = "/etc/mail/sendmail.cf"; $sendmailCf = "/etc/mail/sendmail.cf"; ### sendmail reload command sequence. separate multiple commands with shell # meta characters such as "|", ";", "||", "&&", etc. leave blank if invoked # by inetd. # $sendmailRc = "/etc/rc.d/init.d/sendmail reload"; $sendmailRc = "/etc/rc.d/init.d/sendmail 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 "makemap". # $makemap = "/usr/bin/makemap"; $makemap = "/usr/bin/makemap"; ### access db (text version). # $accessdb = "/etc/mail/access"; $accessdb = "/etc/mail/access"; ### DO NOT TOUCH ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!! ### SanityChecking ########################################################### $ERR = 1, Error "$MOD: \$sendmailCf = '$sendmailCf'" if !-f $sendmailCf; $ERR = 1, Error "$MOD: \$sendmailRc = '$sendmailRc'" if $sendmailRc && ($sendmailRc !~ /^\// || !-x (split /[\s|&;]+/, $sendmailRc)[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: \$makemap = '$makemap'" if $makemap !~ /^\// || !-x (split /[\s|&;]+/, $makemap)[0]; $ERR = 1, Error "$MOD: \$accessdb = '$accessdb'" if !-f $accessdb; $sendmailCd = ($sendmailCf =~ /(.*)\/[^\/]+$/)[0]; }; $X eq "initialization" && do { ############################################### if ($ONCFG) { `ln -s /etc/vhostz $sendmailCd/vhostz $NERR` if !-e "$sendmailCd/vhostz"; `ln -s /etc/vuserz $sendmailCd/vuserz $NERR` if !-e "$sendmailCd/vuserz"; `ln /etc/conf.linuxconf /etc/vdeliver $NERR` if !-e "/etc/vdeliver"; `chown 0:$GHID /etc/vdeliver $NERR` if !-o "/etc/vdeliver"; `chmod 0600 /etc/vdeliver $NERR` if ((stat "/etc/vdeliver")[2] & 07777) != 0600 && !-e "/etc/conf.linuxconf"; my $ok, @cv; Flock $sendmailCf, "+<", 0 if !grep /^R\$.*\$=V.*\bvhost\b/, Cat $sendmailCf; BackupCf $sendmailCf; /^S[^\s\d]*0\s*$/ && ($_ .= "\n".<<'#'."\n", $ok = 1) && last for @FILE; # special ruleset for virtual domain routing - by vhost R$+<@$=V> $: $1<@$(vhost$2$)> R$+<@$=V.> $: $1<@$(vhost$2$).> R$+<@$=V> $: vhost.:$1@$2 R$+<@$=V.> $: vhost.:$1@$2 Rvhost$-:$+@$+ $: $(vuser$2@$3$:vhost$).:$2@$3 Rvhost$-:$++$*@$+ $: $(vuser$2@$4$:vhost$),:$2+$3@$4 Rvhost$-:$+@$+ $: $(vuser@$3$:vhost$).:$2@$3 Rvhost$-:$+@$+ $# error $@ 5.1.1 $: "550 User Unknown" Rvuser.:$+@$+ $# vhost $@ $2 $: $1 Rvuser,:$++$*@$+ $# vhost $@ $3 $: $1 # $FILE[-1] .= "\n".<<'#'."\n" if $ok; # special mailer for virtual domain routing - by vhost Mvhost, P=/usr/sbin/vhost, F=lsDFMShP, S=10, R=20/40, A=vhost --vdeliver $u $h # $FILE[-1] .= "\n".<<"#"."\n" if $ok; FV$sendmailCd/vhostz Kvhost hash $sendmailCd/vhostz Kvuser hash $sendmailCd/vuserz # print FILE @FILE; Flock 0; Flock $sendmailCf, "+<", 0 if grep /^O\s*DaemonPortOptions\s*=.*Addr\s*=\s*127\.[\d.]+/i, Cat $sendmailCf; s/^(O\s*DaemonPortOptions\s*=.*Addr)\s*=\s*127\.[\d.]+/$1=0.0.0.0/i for @FILE; print FILE @FILE; Flock 0; Flock $sendmailCf, "+<", 0 if !grep (/^R\$\+ < \@ \$=V >\t/, Cat $sendmailCf) && grep /^R\$\+ < \@ \$=w >\t/, Cat $sendmailCf; s/^((R\$\+ < \@ \$=)w( >\t.*))/$1$2V$3/s 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 "/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; `$makemap hash $sendmailCd/vhostz < $sendmailCd/vhostz $NERR`; `$makemap hash $sendmailCd/vuserz < $sendmailCd/vuserz $NERR`; ReloadRc 0; }; $X eq "delhost" && do { ###################################################### if ($_d) { Flock "/etc/vdeliver", "+<", 0; s/^\s*vdomain(\.index\s+|_\w+\.)\Q$H\E(\s|$).*//s for @FILE; print FILE @FILE; Flock 0; } `$makemap hash $sendmailCd/vhostz < $sendmailCd/vhostz $NERR` if $_d; `$makemap hash $sendmailCd/vuserz < $sendmailCd/vuserz $NERR` if $_d; ReloadRc 0 if $_d; }; $X eq "addhali" && do { ###################################################### my ($i, $j, $k); 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; `$makemap hash $sendmailCd/vhostz < $sendmailCd/vhostz $NERR`; ReloadRc 0; }; $X eq "delhali" && do { ###################################################### Flock "/etc/vdeliver", "+<", 0; s/^\s*vdomain_other\.\Q$H\E\s+\Q$A\E\s*$//s for @FILE; print FILE @FILE; Flock 0; `$makemap hash $sendmailCd/vhostz < $sendmailCd/vhostz $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; `$makemap hash $sendmailCd/vuserz < $sendmailCd/vuserz $NERR`; }; $X eq "deluser" && do { ###################################################### `$makemap hash $sendmailCd/vuserz < $sendmailCd/vuserz $NERR`; }; $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; } `$makemap hash $sendmailCd/vuserz < $sendmailCd/vuserz $NERR`; `$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; } `$makemap hash $sendmailCd/vuserz < $sendmailCd/vuserz $NERR`; `$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 $sendmailRc; }; $X eq "vdeliver" && do { ##################################################### my ($u, $d, $b, $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", $b = $d =~ /(.*)\/[^\/]+$/ && $1, $d = -d $d && $d || $O[7], $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 $u $ARGV[-1] $i $b) $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) { Flock "/tmp/.vhost/.out.$$", ">", 0; print FILE for Cat $accessdb; s/^\s*(\S+)\s+\d+\s*$/$1\tRELAY\n/s && print FILE for Cat "/etc/poprelay"; Flock 0; `($makemap hash $accessdb < /tmp/.vhost/.out.$$; rm /tmp/.vhost/.out.$$) $NERR`; } ReloadRc $sendmailRc if !$_q && $ARGV[0] =~ /^(addhost|delhost|addhali|delhali)$/; }; 1;