/usr/share/perl5/Mail/Milter/Chain.pm is in libmail-milter-perl 0.07-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | # $Id: Chain.pm,v 1.10 2004/04/23 15:51:39 tvierling Exp $
#
# Copyright (c) 2002-2004 Todd Vierling <tv@pobox.com> <tv@duh.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the author nor the names of contributors may be used
# to endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
package Mail::Milter::Chain;
use 5.006;
use strict;
use warnings;
use Carp;
use Mail::Milter;
use Sendmail::Milter 0.18; # get needed constants
use UNIVERSAL;
our $VERSION = '0.03';
=pod
=head1 NAME
Mail::Milter::Chain - Perl extension for chaining milter callbacks
=head1 SYNOPSIS
use Mail::Milter::Chain;
my $chain = new Mail::Milter::Chain({ connect => \&foo, ... }, ...);
$chain->register({ connect => \&bar, ... });
$chain->register({ connect => \&baz, ... });
$chain->accept_break(1);
use Sendmail::Milter;
...
Sendmail::Milter::register('foo', $chain, SMFI_CURR_ACTS);
=head1 DESCRIPTION
Mail::Milter::Chain allows multiple milter callback sets to be registered
in a single milter server instance, simulating multiple milters running in
separate servers. This is typically much less resource intensive than
running each milter in its own server process.
Any contained milter returning SMFIS_REJECT, SMFIS_TEMPFAIL, or
SMFIS_DISCARD will terminate the entire chain and return the respective
code up to the containing chain or milter server.
Normally, a milter returning SMFIS_ACCEPT will remove only that milter
from the chain, allowing others to continue processing the message.
Alternatively, SMFIS_ACCEPT can be made to terminate the entire chain as
is done for error results; see C<accept_break()> below.
A C<Mail::Milter::Chain> is itself a milter callback hash reference, and
can thus be passed directly to C<Sendmail::Milter::register()> or another
Mail::Milter::Chain container. IMPORTANT CAVEAT: Once this object has
been registered with a parent container (a milter or another chain), DO
NOT call C<register()> on this object any longer. This will result in
difficult to diagnose problems at callback time.
=head1 METHODS
=over 4
=item new([HASHREF, ...])
Creates a Mail::Milter::Chain object. For convenience, accepts one or
more hash references corresponding to individual callback sets that will
be registered with this chain.
=cut
sub new ($) {
my $this = bless {}, shift;
$this->{_acceptbreak} = 0;
$this->{_chain} = [];
# "connect" and "helo" use the global chain, and whittle out
# callbacks to be ignored for the rest of the connection.
$this->{connect} = sub {
$this->{_curchain} = [ @{$this->{_chain}} ];
$this->dispatch('connect', @_);
};
$this->{helo} = sub {
my $rc = $this->dispatch('helo', @_);
$this->{_connchain} = [ @{$this->{_curchain}} ];
$rc;
};
# "envfrom" uses the chain whittled by "connect" and "helo"
# each pass through.
$this->{envfrom} = sub {
$this->{_curchain} = [ @{$this->{_connchain}} ];
$this->dispatch('envfrom', @_);
};
# "close" must use the global chain always, and must also
# clean up any internal state. Every callback must be called;
# there are no shortcuts.
$this->{close} = sub {
my $ctx = shift;
my $chain = $this->{_chain};
for (my $i = 0; $i < scalar @$chain; $i++) {
my $cb = $chain->[$i];
$ctx->setpriv($cb->{_priv});
&{$cb->{close}}($ctx, @_) if defined($cb->{close});
}
$ctx->setpriv(undef);
SMFIS_CONTINUE;
};
foreach my $callbacks (@_) {
$this->register($callbacks);
}
$this;
}
=pod
=item accept_break(FLAG)
If FLAG is 0 (the default), SMFIS_ACCEPT will only remove the current
milter from the list of callbacks, thus simulating a completely
independent milter server.
If FLAG is 1, SMFIS_ACCEPT will terminate the entire chain and propagate
SMFIS_ACCEPT up to the parent chain or milter server. This allows a
milter to provide a sort of "whitelist" effect, where SMFIS_ACCEPT speaks
for the entire chain rather than just one milter callback set.
This method returns a reference to the object itself, allowing this
method call to be chained.
=cut
sub accept_break ($$) {
my $this = shift;
my $flag = shift;
croak 'accept_break: flag argument is undef' unless defined($flag);
$this->{_acceptbreak} = $flag;
$this;
}
# internal method to add dispatch closure hook as a callback
sub create_callback ($$) {
my $this = shift;
my $cbname = shift;
return 0 if defined($this->{$cbname});
$this->{$cbname} = sub {
$this->dispatch($cbname, @_);
};
1;
}
# internal method to dispatch a callback
sub dispatch ($$;@) {
my $this = shift;
my $cbname = shift;
my $ctx = shift;
# @_ is remaining args
my $chain = $this->{_curchain};
my $rc = SMFIS_CONTINUE;
for (my $i = 0; $i < scalar @$chain; $i++) {
my $cb = $chain->[$i];
$ctx->setpriv($cb->{_priv});
my $newrc = defined($cb->{$cbname}) ?
&{$cb->{$cbname}}($ctx, @_) :
$rc;
if ($newrc == SMFIS_TEMPFAIL || $newrc == SMFIS_REJECT) {
# If "envrcpt", these are special and don't nuke.
$rc = $newrc;
@$chain = () unless $cbname eq 'envrcpt';
} elsif ($newrc == SMFIS_DISCARD) {
$rc = $newrc;
@$chain = ();
} elsif ($newrc == SMFIS_ACCEPT) {
if ($this->{_acceptbreak}) {
@$chain = ();
} else {
splice(@$chain, $i, 1);
$i--;
}
} elsif ($newrc != SMFIS_CONTINUE) {
warn "chain element returned invalid result $newrc\n";
$rc = SMFIS_TEMPFAIL;
@$chain = ();
}
$cb->{_priv} = $ctx->getpriv();
}
# If we're still at SMFIS_CONTINUE and the chain is empty,
# convert to a SMFIS_ACCEPT to bubble up to the parent.
$rc = SMFIS_ACCEPT if ($rc == SMFIS_CONTINUE && !scalar @$chain);
$ctx->setpriv(undef);
$rc;
}
=pod
=item register(HASHREF)
Registers a callback set with this chain. Do not call after this chain
has itself been registered with a parent container (chain or milter
server).
=cut
sub register ($$) {
my $this = shift;
my $callbacks = shift;
my $pkg = caller;
croak 'register: callbacks is undef' unless defined($callbacks);
croak 'register: callbacks not hash ref' unless UNIVERSAL::isa($callbacks, 'HASH');
# make internal copy, and convert to code references
my $ncallbacks = {};
foreach my $cbname (keys %Sendmail::Milter::DEFAULT_CALLBACKS) {
my $cb = $callbacks->{$cbname};
next unless defined($cb);
$ncallbacks->{$cbname} = Mail::Milter::resolve_callback($cb, $pkg);
$this->create_callback($cbname);
}
# add to chain
push(@{$this->{_chain}}, $ncallbacks);
1;
}
1;
__END__
=back
=head1 AUTHOR
Todd Vierling, E<lt>tv@duh.orgE<gt> E<lt>tv@pobox.comE<gt>
=head1 SEE ALSO
L<Mail::Milter>, L<Sendmail::Milter>
=cut
|