diff options
| author | fukachan <fukachan> | 2002-06-27 08:24:08 +0000 |
|---|---|---|
| committer | fukachan <fukachan> | 2002-06-27 08:24:08 +0000 |
| commit | 3341c7ba22542b5ecd716bba5812c327e8c92987 (patch) | |
| tree | ee65eac6b2cbe426ac4175cab873a85dc16a4447 /fml | |
| parent | a415e320a51ecd8c3b88312d12994fe87ba8002f (diff) | |
| download | fml8-3341c7ba22542b5ecd716bba5812c327e8c92987.tar.gz fml8-3341c7ba22542b5ecd716bba5812c327e8c92987.tar.bz2 fml8-3341c7ba22542b5ecd716bba5812c327e8c92987.zip | |
major overhaul
o reconstruct architecture
o bug fix virtual domain support
o sync w/ FML::Process overhaul to bug fix virtual domain support
Diffstat (limited to 'fml')
| -rw-r--r-- | fml/lib/FML/Command/Admin/newml.pm | 245 | ||||
| -rw-r--r-- | fml/lib/FML/Command/Admin/rmml.pm | 141 | ||||
| -rw-r--r-- | fml/lib/FML/MTAControl.pm | 144 | ||||
| -rw-r--r-- | fml/lib/FML/MTAControl/Postfix.pm | 248 | ||||
| -rw-r--r-- | fml/lib/FML/MTAControl/Qmail.pm | 110 |
5 files changed, 536 insertions, 352 deletions
diff --git a/fml/lib/FML/Command/Admin/newml.pm b/fml/lib/FML/Command/Admin/newml.pm index de97b31a..d4febe5c 100644 --- a/fml/lib/FML/Command/Admin/newml.pm +++ b/fml/lib/FML/Command/Admin/newml.pm @@ -4,7 +4,7 @@ # All rights reserved. This program is free software; you can # redistribute it and/or modify it under the same terms as Perl itself. # -# $FML: newml.pm,v 1.44 2002/06/23 08:15:43 fukachan Exp $ +# $FML: newml.pm,v 1.45 2002/06/25 12:35:31 fukachan Exp $ # package FML::Command::Admin::newml; @@ -85,10 +85,11 @@ sub process croak("\$ml_name is not specified") unless $ml_name; croak("\$ml_home_dir is not specified") unless $ml_home_dir; - # update $ml_home_prefix and expand variables again. - $config->set( 'ml_home_prefix' , $ml_home_prefix ); + # update $ml_home_prefix and $ml_home_dir to expand variables again. + $config->set( 'ml_home_prefix', $ml_home_prefix ); + $config->set( 'ml_home_dir', $ml_home_dir ); - # "makefml --force newml elena" makes elena ML even if elena + # "makefml --force newml elena" creates elena ML even if elena # already exists. unless (defined $options->{ force } ) { if (-d $ml_home_dir) { @@ -97,30 +98,14 @@ sub process } } - # check alias key defined in MTA aliases - if ($self->_mta_alias_has_ml_entry($curproc, $ml_name)) { + # check the duplication of alias keys in MTA aliases + # Example: search among all entries in postfix $alias_maps + if ($self->_is_mta_alias_maps_has_ml_entry($curproc, $params, $ml_name)) { warn("$ml_name already exists (somewhere in MTA aliases)"); return ; } - # o.k. here we go ! - eval q{ - use File::Utils qw(mkdirhier); - use File::Spec; - }; - croak($@) if $@; - - mkdirhier( $ml_home_dir, $config->{ default_dir_mode } || 0755 ); - - # $ml_home_dir/etc/mail - my $dirlist = $config->get_as_array_ref('init_config_dir_list'); - for my $_dir (@$dirlist) { - unless (-d $_dir) { - print STDERR "creating $_dir\n"; - mkdirhier( $_dir, $config->{ default_dir_mode } || 0755 ); - } - } - + # 0. creat $ml_home_dir # 1. install $ml_home_dir/{config.cf,include,include-ctl} # 2. set up aliases # 3. prepare mail archive at ~fml/public_html/$domain/$ml/ ? @@ -129,6 +114,7 @@ sub process # prepare thread cgi interface at ? # ~fml/public_html/cgi-bin/fml/$domain/threadview.cgi ? # 5. prepare listinfo url + $self->_init_ml_home_dir($curproc, $command_args, $params); $self->_install_template_files($curproc, $command_args, $params); $self->_update_aliases($curproc, $command_args, $params); $self->_setup_mail_archive_dir($curproc, $command_args, $params); @@ -137,6 +123,38 @@ sub process } +# Descriptions: create $ml_home_dir if needed +# Arguments: OBJ($self) +# OBJ($curproc) +# HASH_REF($command_args) +# HASH_REF($params) +# Side Effects: create $ml_home_dir dirctory if needed +# Return Value: none +sub _init_ml_home_dir +{ + my ($self, $curproc, $command_args, $params) = @_; + my $config = $curproc->{ 'config' }; + my $ml_home_dir = $config->{ ml_home_dir }; + + eval q{ + use File::Utils qw(mkdirhier); + unless (-d $ml_home_dir) { + mkdirhier( $ml_home_dir, $config->{ default_dir_mode } || 0755 ); + } + }; + croak($@) if $@; + + # $ml_home_dir/etc/mail + my $dirlist = $config->get_as_array_ref('init_config_dir_list'); + for my $_dir (@$dirlist) { + unless (-d $_dir) { + print STDERR "creating $_dir\n"; + mkdirhier( $_dir, $config->{ default_dir_mode } || 0755 ); + } + } +} + + # Descriptions: install config.cf, include, include-ctl et. al. # Arguments: OBJ($self) # OBJ($curproc) @@ -153,6 +171,8 @@ sub _install_template_files my $templ_files = $config->get_as_array_ref('newml_command_template_files'); + # 1. set up fml specific files e.g. config.cf + use File::Spec; for my $file (@$templ_files) { my $src = File::Spec->catfile($template_dir, $file); my $dst = File::Spec->catfile($ml_home_dir, $file); @@ -161,13 +181,14 @@ sub _install_template_files _install($src, $dst, $params); } + # 2. set up MTA specific files e.g. include, .qmail-* use FML::MTAControl; - # setup include include-ctl ... + # 2.1 setup include include-ctl ... (postfix/sendmail style) my $postfix = new FML::MTAControl { mta_type => 'postfix' }; $postfix->setup($curproc, $params); - # setup ~fml/.qmail-* + # 2.2 setup ~fml/.qmail-* (qmail style) my $qmail = new FML::MTAControl { mta_type => 'qmail' }; $qmail->setup($curproc, $params); } @@ -183,148 +204,59 @@ sub _install_template_files sub _update_aliases { my ($self, $curproc, $command_args, $params) = @_; - my $template_dir = $curproc->template_files_dir_for_newml(); - my $config = $curproc->{ config }; - my $ml_name = $config->{ ml_name }; - my $ml_domain = $config->{ ml_domain }; - - use File::Spec; - my $alias = $config->{ mail_aliases_file }; - my $src = File::Spec->catfile($template_dir, 'aliases'); - my $dst = $alias . "." . $$; - - # update params - use FML::MTAControl; - my $postfix = new FML::MTAControl; - my $xparams = {}; - for my $k (keys %$params) { $xparams->{ $k } = $params->{ $k };} - $postfix->virtual_params($curproc, $xparams); - _install($src, $dst, $xparams); + my $config = $curproc->{ config }; + my $ml_name = $config->{ ml_name }; + my $ml_domain = $config->{ ml_domain }; + my $alias = $config->{ mail_aliases_file }; # append - if ($self->_alias_has_ml_entry($curproc, $alias, $ml_name)) { + if ($self->_is_mta_alias_maps_has_ml_entry($curproc, $params, $ml_name)) { print STDERR "warning: $ml_name already defined!\n"; print STDERR " ignore aliases updating.\n"; } else { - print STDERR "updating $alias as $ml_name is a new ml\n"; eval q{ - use File::Utils qw(append); - append($dst, $alias); - unlink $dst; + for my $mta (qw(postfix qmail)) { + my $optargs = { mta_type => $mta }; - unless ($curproc->is_default_domain($ml_domain)) { - # we need to use the original $params here - $self->_install_postfix_virtual_map($curproc, $params); + use FML::MTAControl; + my $obj = new FML::MTAControl; + # we need to use the original $params here # update templates for qmail/control/virtualdomains - $self->_install_qmail_virtual_map($curproc, $params); - } + unless ($curproc->is_default_domain($ml_domain)) { + $obj->install_virtual_map($curproc, $params, $optargs); + $obj->update_virtual_map($curproc, $params, $optargs); + } - use FML::MTAControl; - my $postfix = new FML::MTAControl; - $postfix->update_alias($curproc, { - mta_type => 'postfix', - alias_maps => [ $alias ], - }); + $obj->install_alias($curproc, $params, $optargs); + $obj->update_alias($curproc, $params, $optargs); + } }; croak($@) if $@; } } -# Descriptions: install postfix virtual_maps -# Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) -# Side Effects: install/udpate postfix virtual_maps and the .db -# Return Value: none -sub _install_postfix_virtual_map -{ - my ($self, $curproc, $params) = @_; - my $template_dir = $curproc->template_files_dir_for_newml(); - my $config = $curproc->{ config }; - my $ml_name = $config->{ ml_name }; - my $ml_domain = $config->{ ml_domain }; - my $postmap = $config->{ path_postmap }; - - my $virtual = $config->{ postfix_virtual_map_file }; - my $src = File::Spec->catfile($template_dir, 'postfix_virtual'); - my $dst = $virtual . "." . $$; - print STDERR "updating $virtual\n"; - - # at the first time - unless( -f $virtual) { - my $fh = new FileHandle ">> $virtual"; - if (defined $fh) { - print $fh "# $ml_domain is one of \$mydestination\n"; - print $fh "# CAUTION: DO NOT REMOVE THE FOLLOWING LINE.\n"; - print $fh "$ml_domain\t$ml_domain\n\n"; - $fh->close(); - } - } - - _install($src, $dst, $params); - append($dst, $virtual); - unlink $dst; - system "$postmap $virtual"; -} - - -# Descriptions: prepare a template for qmail/control/virtualdomains -# for example: rule of a virtual domain for "nuinui.net" -# nuinui.net:fml-.nuinui.net -# Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) -# bugs: we cannot update /var/qmail/control/virtualdomains -# since it needs root priviledge. -# So, we can only update our templates for qmail. -# Side Effects: update template not /var/qmail/control/virtualdomains -# Return Value: none -sub _install_qmail_virtual_map -{ - my ($self, $curproc, $params) = @_; - my $fmlowner = $curproc->fml_owner(); - my $config = $curproc->{ config }; - my $ml_domain = $config->{ ml_domain }; - my $virtual = $config->{ qmail_virtual_map_file }; - - # 1. check the current temlate file firstly - my $found = 0; - my $fh = new FileHandle $virtual; - if (defined $fh) { - while (<$fh>) { - $found = 1 if /^$ml_domain:/i; - } - $fh->close(); - } - - # 2. if not found - unless ($found) { - print STDERR "updating $virtual\n"; - - my $fh = new FileHandle ">> $virtual"; - if (defined $fh) { - print $fh "$ml_domain:$fmlowner-$ml_domain\n"; - $fh->close(); - } - } -} - - # Descriptions: $alias file has an $ml_name entry or not # Arguments: OBJ($self) OBJ($curproc) STR($ml_name) # Side Effects: none # Return Value: NUM( 1 or 0 ) -sub _mta_alias_has_ml_entry +sub _is_mta_alias_maps_has_ml_entry { - my ($self, $curproc, $ml_name) = @_; + my ($self, $curproc, $params, $ml_name) = @_; my $found = 0; eval q{ use FML::MTAControl; - my $postfix = new FML::MTAControl; - $found = $postfix->find_key_in_alias($curproc, { - mta_type => 'postfix', - key => $ml_name, - }); + + for my $mta (qw(postfix qmail)) { + my $obj = new FML::MTAControl; + $found = $obj->find_key_in_alias_maps($curproc, $params, { + mta_type => $mta, + key => $ml_name, + }); + } }; croak($@) if $@; @@ -332,34 +264,6 @@ sub _mta_alias_has_ml_entry } -# Descriptions: $alias file has an $ml_name entry or not -# Arguments: OBJ($self) OBJ($curproc) STR($alias) STR($ml_name) -# Side Effects: none -# Return Value: NUM( 1 or 0 ) -sub _alias_has_ml_entry -{ - my ($self, $curproc, $alias, $ml_name) = @_; - my $found = 0; - - # search all entries defined in MTA - $found = $self->_mta_alias_has_ml_entry($curproc, $ml_name); - return 1 if $found; - - use FileHandle; - my $fh = new FileHandle $alias; - if (defined $fh) { - while (<$fh>) { - if (/ALIASES $ml_name\@/) { - return 1; - } - } - $fh->close; - } - - return 0; -} - - # Descriptions: set up ~fml/public_html/ for this mailing list # Arguments: OBJ($self) # OBJ($curproc) @@ -451,6 +355,7 @@ sub _setup_cgi_interface }; }; + use File::Spec; for my $dst ( File::Spec->catfile($admin_cgi_dir, 'menu.cgi'), File::Spec->catfile($admin_cgi_dir, 'config.cgi'), diff --git a/fml/lib/FML/Command/Admin/rmml.pm b/fml/lib/FML/Command/Admin/rmml.pm index c0acd74a..16171ebe 100644 --- a/fml/lib/FML/Command/Admin/rmml.pm +++ b/fml/lib/FML/Command/Admin/rmml.pm @@ -4,7 +4,7 @@ # All rights reserved. This program is free software; you can # redistribute it and/or modify it under the same terms as Perl itself. # -# $FML: rmml.pm,v 1.4 2002/06/25 09:38:09 fukachan Exp $ +# $FML: rmml.pm,v 1.5 2002/06/25 12:36:51 fukachan Exp $ # package FML::Command::Admin::rmml; @@ -88,33 +88,45 @@ sub process $config->set( 'ml_home_prefix' , $ml_home_prefix ); # check if $ml_name already exists. - my $found = 0; - - # --force: ignore the existence of $ml_home_dir - unless (defined $options->{ force }) { - if (-d $ml_home_dir) { - $found = 1; - } - } - - unless ($found) { - warn("no such ml"); + unless (-d $ml_home_dir) { + warn("no such ml ($ml_home_dir)"); return; } + # o.k. here we go! + $self->_remove_ml_home_dir($curproc, $command_args, $params); + $self->_remove_aliases($curproc, $command_args, $params); +} + + +# Descriptions: remove $ml_home_dir and update aliases if needed +# Arguments: OBJ($self) +# OBJ($curproc) +# HASH_REF($command_args) +# HASH_REF($params) +# Side Effects: remove ml_home_dir, update aliases entry +# Return Value: none +sub _remove_ml_home_dir +{ + my ($self, $curproc, $command_args, $params) = @_; + my $ml_name = $params->{ ml_name }; + my $ml_domain = $params->{ ml_domain }; + my $ml_home_prefix = $params->{ ml_home_prefix }; + my $ml_home_dir = $params->{ ml_home_dir }; + + print STDERR "removing ml_home_dir for $ml_name\n"; + # /var/spool/ml/elena -> /var/spool/ml/@elena use File::Spec; my $removed_dir = File::Spec->catfile($ml_home_prefix, '@'.$ml_name); rename($ml_home_dir, $removed_dir); if (-d $removed_dir && (! -d $ml_home_dir)) { - print STDERR "$ml_name home_dir removed.\n"; + print STDERR "\tremoved.\n"; } else { - print STDERR "cannot rmdir $ml_name home_dir.\n"; + print STDERR "\tfailed.\n"; } - - $self->_update_aliases($curproc, $command_args, $params); } @@ -125,94 +137,25 @@ sub process # HASH_REF($params) # Side Effects: update aliases entry # Return Value: none -sub _update_aliases +sub _remove_aliases { my ($self, $curproc, $command_args, $params) = @_; my $config = $curproc->{ config }; - my $ml_name = $self->{ _ml_name }; - my $alias = $config->{ mail_aliases_file }; - - # append - if ($self->_alias_has_ml_entry($alias, $ml_name)) { - $self->_remove_alias_entry($alias, $ml_name); - eval q{ - use FML::MTAControl; - my $postfix = new FML::MTAControl; - $postfix->update_alias($curproc, { - mta_type => 'postfix', - alias_maps => [ $alias ], - }); - }; - croak($@) if $@; - } - else { - warn("no such ml in aliases"); - } -} - - -# Descriptions: $alias file has an $ml_name entry or not -# Arguments: OBJ($self) STR($alias) STR($ml_name) -# Side Effects: none -# Return Value: NUM( 1 or 0 ) -sub _alias_has_ml_entry -{ - my ($self, $alias, $ml_name) = @_; - - use FileHandle; - my $fh = new FileHandle $alias; - if (defined $fh) { - while (<$fh>) { - if (/ALIASES $ml_name\@/) { - return 1; - } - } - $fh->close; - } - - return 0; -} + my $ml_name = $params->{ ml_name }; - -# Descriptions: $alias file has an $ml_name entry or not -# Arguments: OBJ($self) STR($alias) STR($ml_name) -# Side Effects: none -# Return Value: NUM( 1 or 0 ) -sub _remove_alias_entry -{ - my ($self, $alias, $ml_name) = @_; - my $alias_new = $alias."new.$$"; - my $removed = 0; - - use FileHandle; - my $rh = new FileHandle $alias; - my $wh = new FileHandle "> $alias_new"; - if (defined $rh && defined $wh) { - LINE: - while (<$rh>) { - if (/\<ALIASES\s+$ml_name\@/ .. /\<\/ALIASES\s+$ml_name\@/) { - $removed++; - next LINE; - } - - print $wh $_; - } - $wh->close; - $rh->close; - - if ($removed > 3) { - if (rename($alias_new, $alias)) { - print STDERR "$ml_name aliases removed.\n"; - } - else { - print STDERR "warning: fail to rename alias files.\n"; - } + eval q{ + use FML::MTAControl; + + for my $mta (qw(postfix qmail)) { + my $optargs = { mta_type => $mta }; + my $obj = new FML::MTAControl; + $obj->remove_alias($curproc, $params, $optargs); + $obj->update_alias($curproc, $params, $optargs); + $obj->remove_virtual_map($curproc, $params, $optargs); + $obj->update_virtual_map($curproc, $params, $optargs); } - } - else { - warn("cannot open $alias") unless defined $rh; - warn("cannot open $alias_new") unless defined $wh; - } + }; + croak($@) if $@; } diff --git a/fml/lib/FML/MTAControl.pm b/fml/lib/FML/MTAControl.pm index 032c14fc..ed643697 100644 --- a/fml/lib/FML/MTAControl.pm +++ b/fml/lib/FML/MTAControl.pm @@ -4,7 +4,7 @@ # All rights reserved. This program is free software; you can # redistribute it and/or modify it under the same terms as Perl itself. # -# $FML: MTAControl.pm,v 1.7 2002/06/01 05:02:31 fukachan Exp $ +# $FML: MTAControl.pm,v 1.8 2002/06/01 05:09:23 fukachan Exp $ # package FML::MTAControl; @@ -12,6 +12,11 @@ use strict; use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD); use Carp; +use FML::MTAControl::Postfix; +use FML::MTAControl::Qmail; +@ISA = qw(FML::MTAControl::Postfix + FML::MTAControl::Qmail); + my $debug = 0; @@ -52,40 +57,10 @@ sub new $me->{ mta_type } = defined $args->{ mta_type } ? $args->{ mta_type } : 'postfix'; - _update_isa($me); - return bless $me, $type; } -# Descriptions: load a sub module and update @ISA -# Arguments: OBJ($self) HASH_REF($optargs) -# Side Effects: load FML::Control::$MTA and update @ISA -# Return Value: none -sub _update_isa -{ - my ($self, $optargs) = @_; - my $mta_type = - defined $optargs->{ mta_type } ? $optargs->{ mta_type } : - $self->{ mta_type }; - - if ($mta_type eq 'postfix') { - eval q{ - use FML::MTAControl::Postfix; - push(@ISA, qw(FML::MTAControl::Postfix)); - }; - croak($@) if $@; - } - elsif ($mta_type eq 'qmail') { - eval q{ - use FML::MTAControl::Qmail; - push(@ISA, qw(FML::MTAControl::Qmail)); - }; - croak($@) if $@; - } -} - - # Descriptions: install configuration temaplate alias # Arguments: OBJ($self) # HASH_REF($curproc) HASH_REF($params) HASH_REF($optargs) @@ -114,14 +89,14 @@ sub setup # Return Value: none sub update_alias { - my ($self, $curproc, $optargs) = @_; + my ($self, $curproc, $params, $optargs) = @_; my $mta_type = defined $optargs->{ mta_type } ? $optargs->{ mta_type } : $self->{ mta_type }; if ($mta_type eq 'postfix' || $mta_type eq 'qmail') { my $method = "${mta_type}_update_alias"; - $self->$method($curproc, $optargs); + $self->$method($curproc, $params, $optargs); } else { croak("unknown MTA"); @@ -133,17 +108,17 @@ sub update_alias # Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) # Side Effects: none # Return Value: none -sub find_key_in_alias +sub find_key_in_alias_maps { - my ($self, $curproc, $optargs) = @_; + my ($self, $curproc, $params, $optargs) = @_; my $mta_type = defined $optargs->{ mta_type } ? $optargs->{ mta_type } : $self->{ mta_type }; my $key = $optargs->{ key }; if ($mta_type eq 'postfix' || $mta_type eq 'qmail') { - my $method = "${mta_type}_find_key_in_alias"; - $self->$method($curproc, $optargs); + my $method = "${mta_type}_find_key_in_alias_maps"; + $self->$method($curproc, $params, $optargs); } else { croak("unknown MTA"); @@ -157,14 +132,58 @@ sub find_key_in_alias # Return Value: none sub get_aliases_as_hash_ref { - my ($self, $curproc, $optargs) = @_; + my ($self, $curproc, $params, $optargs) = @_; my $mta_type = defined $optargs->{ mta_type } ? $optargs->{ mta_type } : $self->{ mta_type }; if ($mta_type eq 'postfix' || $mta_type eq 'qmail') { my $method = "${mta_type}_get_aliases_as_hash_ref"; - $self->$method($curproc, $optargs); + $self->$method($curproc, $params, $optargs); + } + else { + croak("unknown MTA"); + } +} + + +# Descriptions: install configuration temaplate alias +# Arguments: OBJ($self) +# HASH_REF($curproc) HASH_REF($params) HASH_REF($optargs) +# Side Effects: update aliases +# Return Value: none +sub install_alias +{ + my ($self, $curproc, $params, $optargs) = @_; + my $mta_type = + defined $optargs->{ mta_type } ? $optargs->{ mta_type } : + $self->{ mta_type }; + + if ($mta_type eq 'postfix' || $mta_type eq 'qmail') { + my $method = "${mta_type}_install_alias"; + $self->$method($curproc, $params, $optargs); + } + else { + croak("unknown MTA"); + } +} + + +# Descriptions: remove configuration temaplate alias +# Arguments: OBJ($self) +# HASH_REF($curproc) HASH_REF($params) HASH_REF($optargs) +# Side Effects: update aliases +# Return Value: none +sub remove_alias +{ + my ($self, $curproc, $params, $optargs) = @_; + my $mta_type = + defined $optargs->{ mta_type } ? $optargs->{ mta_type } : + $self->{ mta_type }; + + if ($mta_type eq 'postfix' || $mta_type eq 'qmail') { + my $method = "${mta_type}_remove_alias"; + $self->$method($curproc, $params, $optargs); } else { croak("unknown MTA"); @@ -177,7 +196,50 @@ sub get_aliases_as_hash_ref # HASH_REF($curproc) HASH_REF($params) HASH_REF($optargs) # Side Effects: update aliases # Return Value: none -sub virtual_params +sub install_virtual_map +{ + my ($self, $curproc, $params, $optargs) = @_; + my $mta_type = + defined $optargs->{ mta_type } ? $optargs->{ mta_type } : + $self->{ mta_type }; + + if ($mta_type eq 'postfix' || $mta_type eq 'qmail') { + my $method = "${mta_type}_install_virtual_map"; + $self->$method($curproc, $params, $optargs); + } + else { + croak("unknown MTA"); + } +} + + +# Descriptions: remove configuration temaplate alias +# Arguments: OBJ($self) +# HASH_REF($curproc) HASH_REF($params) HASH_REF($optargs) +# Side Effects: update aliases +# Return Value: none +sub remove_virtual_map +{ + my ($self, $curproc, $params, $optargs) = @_; + my $mta_type = + defined $optargs->{ mta_type } ? $optargs->{ mta_type } : + $self->{ mta_type }; + + if ($mta_type eq 'postfix' || $mta_type eq 'qmail') { + my $method = "${mta_type}_remove_virtual_map"; + $self->$method($curproc, $params, $optargs); + } + else { + croak("unknown MTA"); + } +} + + +# Descriptions: update virtual_map +# Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) +# Side Effects: update virtual_map +# Return Value: none +sub update_virtual_map { my ($self, $curproc, $params, $optargs) = @_; my $mta_type = @@ -185,7 +247,7 @@ sub virtual_params $self->{ mta_type }; if ($mta_type eq 'postfix' || $mta_type eq 'qmail') { - my $method = "${mta_type}_virtual_params"; + my $method = "${mta_type}_update_virtual_map"; $self->$method($curproc, $params, $optargs); } else { diff --git a/fml/lib/FML/MTAControl/Postfix.pm b/fml/lib/FML/MTAControl/Postfix.pm index b0ad53d5..5b3784f8 100644 --- a/fml/lib/FML/MTAControl/Postfix.pm +++ b/fml/lib/FML/MTAControl/Postfix.pm @@ -4,7 +4,7 @@ # All rights reserved. This program is free software; you can # redistribute it and/or modify it under the same terms as Perl itself. # -# $FML: Postfix.pm,v 1.3 2002/06/01 05:02:32 fukachan Exp $ +# $FML: Postfix.pm,v 1.4 2002/06/24 16:25:33 tmu Exp $ # package FML::MTAControl::Postfix; @@ -27,41 +27,122 @@ FML::MTAControl - postfix utilities =head1 METHODS -=head2 new($args) +=head2 new() - $args = { - mta_type => 'postfix', - }; +=cut -C<postfix> as C<mta_type> is available now. -=cut +# Descriptions: install new alias entries +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) +# Side Effects: update aliases +# Return Value: none +sub postfix_install_alias +{ + my ($self, $curproc, $params, $optargs) = @_; + my $config = $curproc->{ config }; + my $template_dir = $curproc->template_files_dir_for_newml(); + + use File::Spec; + my $alias = $config->{ mail_aliases_file }; + my $src = File::Spec->catfile($template_dir, 'aliases'); + my $dst = $alias . "." . $$; + + # update params + my $xparams = {}; + for my $k (keys %$params) { $xparams->{ $k } = $params->{ $k };} + $self->_postfix_rewrite_virtual_params($curproc, $xparams); + $self->_install($src, $dst, $xparams); + + print STDERR "updating $alias\n"; + + use File::Utils qw(append); + append($dst, $alias); + unlink $dst; +} + + +# Descriptions: remove alias +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) +# Side Effects: update aliases +# Return Value: none +sub postfix_remove_alias +{ + my ($self, $curproc, $params, $optargs) = @_; + my $config = $curproc->{ config }; + my $alias = $config->{ mail_aliases_file }; + my $alias_new = $alias."new.$$"; + my $ml_name = $params->{ ml_name }; + my $ml_domain = $params->{ ml_domain }; + my $removed = 0; + + # update params + my $xparams = {}; + for my $k (keys %$params) { $xparams->{ $k } = $params->{ $k };} + $self->_postfix_rewrite_virtual_params($curproc, $xparams); + + my $key = $xparams->{ ml_name }; + + print STDERR "removing $key in $alias\n"; + + use FileHandle; + my $rh = new FileHandle $alias; + my $wh = new FileHandle "> $alias_new"; + if (defined $rh && defined $wh) { + LINE: + while (<$rh>) { + if (/\<ALIASES\s+$key\@/ .. /\<\/ALIASES\s+$key\@/) { + $removed++; + next LINE; + } + + print $wh $_; + } + $wh->close; + $rh->close; + + if ($removed > 3) { + if (rename($alias_new, $alias)) { + print STDERR "\tremoved.\n"; + } + else { + print STDERR "\twarning: fail to rename alias files.\n"; + } + } + } + else { + warn("cannot open $alias") unless defined $rh; + warn("cannot open $alias_new") unless defined $wh; + } +} -# Descriptions: update alias -# Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) +# Descriptions: regenerate aliases.db +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) # Side Effects: update aliases # Return Value: none sub postfix_update_alias { - my ($self, $curproc, $optargs) = @_; + my ($self, $curproc, $params, $optargs) = @_; my $config = $curproc->{ config }; my $prog = $config->{ path_postalias }; - my $maps = $optargs->{ alias_maps }; + my $alias = $config->{ mail_aliases_file }; - for my $alias (@$maps) { - system "$prog $alias"; - } + print STDERR "updating $alias database\n"; + system "$prog $alias"; } # Descriptions: find key in aliases -# Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) # Side Effects: none # Return Value: NUM(1 or 0) -sub postfix_find_key_in_alias +sub postfix_find_key_in_alias_maps { - my ($self, $curproc, $optargs) = @_; + my ($self, $curproc, $params, $optargs) = @_; my $key = $optargs->{ key }; my $maps = $self->postfix_alias_maps($curproc, $optargs); @@ -86,12 +167,13 @@ sub postfix_find_key_in_alias # Descriptions: get { key => value } in aliases -# Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) # Side Effects: none # Return Value: HASH_REF sub postfix_get_aliases_as_hash_ref { - my ($self, $curproc, $optargs) = @_; + my ($self, $curproc, $params, $optargs) = @_; my $config = $curproc->{ config }; my $alias_file = $config->{ mail_aliases_file }; my $key = $optargs->{ key }; @@ -135,12 +217,13 @@ sub postfix_get_aliases_as_hash_ref # Descriptions: return alias_maps as ARRAY_REF -# Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) # Side Effects: none # Return Value: ARRAY_REF sub postfix_alias_maps { - my ($self, $curproc, $optargs) = @_; + my ($self, $curproc, $params, $optargs) = @_; my $config = $curproc->{ config }; my $prog = $config->{ path_postconf }; @@ -157,12 +240,13 @@ sub postfix_alias_maps # Descriptions: install configuratin templates -# Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) # Side Effects: create include* # Return Value: none sub postfix_setup { - my ($self, $curproc, $params) = @_; + my ($self, $curproc, $params, $optargs) = @_; my $config = $curproc->{ config }; my $template_dir = $curproc->template_files_dir_for_newml(); my $ml_home_dir = $params->{ ml_home_dir }; @@ -182,12 +266,13 @@ sub postfix_setup # Descriptions: rewrite $params -# Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($params) +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) # Side Effects: update $params # Return Value: none -sub postfix_virtual_params +sub _postfix_rewrite_virtual_params { - my ($self, $curproc, $params) = @_; + my ($self, $curproc, $params, $optargs) = @_; my $config = $curproc->{ config }; my $ml_name = $config->{ ml_name }; my $ml_domain = $config->{ ml_domain }; @@ -198,6 +283,117 @@ sub postfix_virtual_params } +# Descriptions: install postfix virtual_maps +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) +# Side Effects: install/udpate postfix virtual_maps and the .db +# Return Value: none +sub postfix_install_virtual_map +{ + my ($self, $curproc, $params, $optargs) = @_; + my $template_dir = $curproc->template_files_dir_for_newml(); + my $config = $curproc->{ config }; + my $ml_name = $config->{ ml_name }; + my $ml_domain = $config->{ ml_domain }; + my $postmap = $config->{ path_postmap }; + + use File::Spec; + my $virtual = $config->{ postfix_virtual_map_file }; + my $src = File::Spec->catfile($template_dir, 'postfix_virtual'); + my $dst = $virtual . "." . $$; + print STDERR "updating $virtual\n"; + + # at the first time + unless( -f $virtual) { + use FileHandle; + my $fh = new FileHandle ">> $virtual"; + if (defined $fh) { + print $fh "# $ml_domain is one of \$mydestination\n"; + print $fh "# CAUTION: DO NOT REMOVE THE FOLLOWING LINE.\n"; + print $fh "$ml_domain\t$ml_domain\n\n"; + $fh->close(); + } + } + + $self->_install($src, $dst, $params); + + use File::Utils qw(append); + append($dst, $virtual); + unlink $dst; + system "$postmap $virtual"; +} + + + +# Descriptions: remove postfix virtual_maps +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) +# Side Effects: remove/udpate postfix virtual_maps and the .db +# Return Value: none +sub postfix_remove_virtual_map +{ + my ($self, $curproc, $params, $optargs) = @_; + my $config = $curproc->{ config }; + my $postmap = $config->{ path_postmap }; + my $key = $params->{ ml_name }; + my $removed = 0; + + use File::Spec; + my $virtual = $config->{ postfix_virtual_map_file }; + my $virtual_new = $virtual . 'new'. $$; + + print STDERR "removing $key in $virtual\n"; + + use FileHandle; + my $rh = new FileHandle $virtual; + my $wh = new FileHandle "> $virtual_new"; + if (defined $rh && defined $wh) { + LINE: + while (<$rh>) { + if (/\<VIRTUAL\s+$key\@/ .. /\<\/VIRTUAL\s+$key\@/) { + $removed++; + next LINE; + } + + print $wh $_; + } + $wh->close; + $rh->close; + + if ($removed > 3) { + if (rename($virtual_new, $virtual)) { + print STDERR "\tremoved.\n"; + } + else { + print STDERR "\twarning: fail to rename virtual files.\n"; + } + } + } + else { + warn("cannot open $virtual") unless defined $rh; + warn("cannot open $virtual_new") unless defined $wh; + } + +} + + +# Descriptions: regenerate virtual.db +# Arguments: OBJ($self) +# OBJ($curproc) HASH_REF($params) HASH_REF($optargs) +# Side Effects: update aliases +# Return Value: none +sub postfix_update_virtual_map +{ + my ($self, $curproc, $params, $optargs) = @_; + my $config = $curproc->{ config }; + my $postmap = $config->{ path_postmap }; + my $virtual = $config->{ postfix_virtual_map_file }; + + print STDERR "updating $virtual database\n"; + system "$postmap $virtual"; +} + + =head1 AUTHOR Ken'ichi Fukamachi diff --git a/fml/lib/FML/MTAControl/Qmail.pm b/fml/lib/FML/MTAControl/Qmail.pm index e5225925..95f6c466 100644 --- a/fml/lib/FML/MTAControl/Qmail.pm +++ b/fml/lib/FML/MTAControl/Qmail.pm @@ -4,7 +4,7 @@ # All rights reserved. This program is free software; you can # redistribute it and/or modify it under the same terms as Perl itself. # -# $FML: Qmail.pm,v 1.5 2002/06/01 05:09:24 fukachan Exp $ +# $FML: Qmail.pm,v 1.6 2002/06/01 14:53:39 fukachan Exp $ # package FML::MTAControl::Qmail; @@ -42,9 +42,35 @@ C<qmail> as C<mta_type> is available now. # Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) # Side Effects: update aliases # Return Value: none +sub qmail_install_alias +{ + my ($self, $curproc, $params, $optargs) = @_; + + 0; +} + + +# Descriptions: update alias +# Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) +# Side Effects: update aliases +# Return Value: none +sub qmail_remove_alias +{ + my ($self, $curproc, $params, $optargs) = @_; + + 0; +} + + +# Descriptions: update alias +# Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) +# Side Effects: update aliases +# Return Value: none sub qmail_update_alias { - my ($self, $curproc, $optargs) = @_; + my ($self, $curproc, $params, $optargs) = @_; + + 0; } @@ -52,9 +78,11 @@ sub qmail_update_alias # Arguments: OBJ($self) HASH_REF($curproc) HASH_REF($optargs) # Side Effects: none # Return Value: NUM(1 or 0) -sub qmail_find_key_in_alias +sub qmail_find_key_in_alias_maps { - my ($self, $curproc, $optargs) = @_; + my ($self, $curproc, $params, $optargs) = @_; + + 0; } @@ -64,7 +92,9 @@ sub qmail_find_key_in_alias # Return Value: HASH_REF sub qmail_get_aliases_as_hash_ref { - my ($self, $curproc, $optargs) = @_; + my ($self, $curproc, $params, $optargs) = @_; + + 0; } @@ -74,9 +104,10 @@ sub qmail_get_aliases_as_hash_ref # Return Value: ARRAY_REF sub qmail_alias_maps { - my ($self, $curproc, $optargs) = @_; -} + my ($self, $curproc, $params, $optargs) = @_; + 0; +} # Descriptions: install configuratin templates @@ -85,7 +116,7 @@ sub qmail_alias_maps # Return Value: none sub qmail_setup { - my ($self, $curproc, $params) = @_; + my ($self, $curproc, $params, $optargs) = @_; my $config = $curproc->{ config }; my $template_dir = $curproc->template_files_dir_for_newml(); my $ml_home_dir = $params->{ ml_home_dir }; @@ -112,21 +143,68 @@ sub qmail_setup my $virtual_domain_conf = $config->{ qmail_virtualdomains_file }; unless (-f $virtual_domain_conf) { - print STDERR " XXX We assume $ml_domain:fml-$ml_domain\n"; - print STDERR " XXX in $virtual_domain_conf\n"; - print STDERR " XXX\n"; + if (0) { + print STDERR " XXX We assume $ml_domain:fml-$ml_domain\n"; + print STDERR " XXX in $virtual_domain_conf\n"; + print STDERR " XXX\n"; + } } } -# Descriptions: dummy -# Arguments: none -# Side Effects: none +# Descriptions: prepare a template for qmail/control/virtualdomains +# for example: rule of a virtual domain for "nuinui.net" +# nuinui.net:fml-.nuinui.net +# Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) +# bugs: we cannot update /var/qmail/control/virtualdomains +# since it needs root priviledge. +# So, we can only update our templates for qmail. +# Side Effects: update template not /var/qmail/control/virtualdomains # Return Value: none -sub qmail_virtual_params +sub qmail_install_virtual_map +{ + my ($self, $curproc, $params) = @_; + my $fmlowner = $curproc->fml_owner(); + my $config = $curproc->{ config }; + my $ml_domain = $config->{ ml_domain }; + my $virtual = $config->{ qmail_virtual_map_file }; + + # 1. check the current temlate file firstly + my $found = 0; + my $fh = new FileHandle $virtual; + if (defined $fh) { + while (<$fh>) { + $found = 1 if /^$ml_domain:/i; + } + $fh->close(); + } + + # 2. if not found + unless ($found) { + print STDERR "updating $virtual\n"; + + my $fh = new FileHandle ">> $virtual"; + if (defined $fh) { + print $fh "$ml_domain:$fmlowner-$ml_domain\n"; + $fh->close(); + } + } + else { + print STDERR "skipping $virtual\n"; + } +} + + +sub qmail_remove_virtual_map +{ + 0; +} + + +sub qmail_update_virtual_map { - ; + 0; } |
