OpenPKG CVS Repository http://cvs.openpkg.org/ ____________________________________________________________________________
Server: cvs.openpkg.org Name: Ralf S. Engelschall Root: /v/openpkg/cvs Email: r...@openpkg.org Module: openpkg-src Date: 05-Jul-2009 18:56:31 Branch: HEAD Handle: 2009070517563100 Added files: openpkg-src/milter-header milter-header.cfg milter-header.pl milter-header.spec rc.milter-header Log: place my milter-header under version control now Summary: Revision Changes Path 1.1 +17 -0 openpkg-src/milter-header/milter-header.cfg 1.1 +143 -0 openpkg-src/milter-header/milter-header.pl 1.1 +117 -0 openpkg-src/milter-header/milter-header.spec 1.1 +63 -0 openpkg-src/milter-header/rc.milter-header ____________________________________________________________________________ patch -p0 <<'@@ .' Index: openpkg-src/milter-header/milter-header.cfg ============================================================================ $ cvs diff -u -r0 -r1.1 milter-header.cfg --- /dev/null 2009-07-05 18:56:17 +0200 +++ milter-header.cfg 2009-07-05 18:56:31 +0200 @@ -0,0 +1,17 @@ +## +## milter-header.cfg -- Header Insertion MILTER Configuration +## + +milter { + socket @l_prefix@/var/milter/socket/milter-header; + logfile @l_prefix@/var/milter-header/milter-header.log; + max_backlog 10; + max_requests 10; + max_childs 5; +}; + +action { + require "From" "^...@example\.com.*$"; + insert "Organization" "Example Inc."; +}; + @@ . patch -p0 <<'@@ .' Index: openpkg-src/milter-header/milter-header.pl ============================================================================ $ cvs diff -u -r0 -r1.1 milter-header.pl --- /dev/null 2009-07-05 18:56:17 +0200 +++ milter-header.pl 2009-07-05 18:56:31 +0200 @@ -0,0 +1,143 @@ +...@l_prefix@/bin/perl +## +## milter-header -- Header Insertion MILTER Program +## Copyright (c) 2007-2009 Ralf S. Engelschall <r...@engelschall.com> +## + +# requirements +require 5.008; +use warnings; +use strict; +use IO::All; +use IO::File; +use OSSP::cfg; +use Date::Format; +use Data::Dumper; +use Sendmail::PMilter qw(:all); + +# command line handling +my $cfgfile = $ARGV[0] || "@l_prefix@/etc/milter-header/milter-header.cfg"; + +# parse configuration file +my $txt < io($cfgfile); +my $cfg = new OSSP::cfg::simple; +$cfg->parse($txt); +my $tree = $cfg->unpack(); +undef $cfg; + +# determine milter parameters +my ($socket) = map { $_->[1] } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'socket' } + map { @{$_->[1]} } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'milter' } @{$tree}; +my ($logfile) = map { $_->[1] } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'logfile' } + map { @{$_->[1]} } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'milter' } @{$tree}; +my ($max_backlog) = map { $_->[1] } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'max_backlog' } + map { @{$_->[1]} } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'milter' } @{$tree}; +my ($max_childs) = map { $_->[1] } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'max_childs' } + map { @{$_->[1]} } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'milter' } @{$tree}; +my ($max_requests) = map { $_->[1] } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'max_requests' } + map { @{$_->[1]} } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'milter' } @{$tree}; + +# determine header actions +my @action = (); +foreach my $action (map { $_->[1] } grep { ref($_) eq 'ARRAY' and $_->[0] eq 'action' } @{$tree}) { + my @require = map { my $value = $_->[2]; $value =~ s/^\s+//s; $value =~ s/\s+$//; my $x = { -name => $_->[1], -value => $value }; $x } + grep { ref($_) eq 'ARRAY' and $_->[0] eq 'require' } @{$action}; + my @insert = map { my $value = $_->[2]; $value =~ s/^\s+//s; $value =~ s/\s+$//; my $x = { -name => $_->[1], -value => $value }; $x } + grep { ref($_) eq 'ARRAY' and $_->[0] eq 'insert' } @{$action}; + push(@action, { -require => [ @require ], -insert => [ @insert ] }); +} + +# helper function for logging +sub logbook ($;@) { + my ($fmt, @args) = @_; + my $log = new IO::File ">>$logfile"; + if (defined $log) { + $log->printf("%s %05d %s\n", time2str("%Y-%m-%d %H:%M:%S", time()), $$, sprintf($fmt, @args)); + $log->close(); + } +} + +# create milter +umask(002); +unlink($socket); +my $milter = new Sendmail::PMilter; +my $dispatcher = Sendmail::PMilter::prefork_dispatcher( + max_children => $max_childs, + max_requests_per_child => $max_requests, +); +$milter->set_dispatcher($dispatcher); +$milter->set_listen($max_backlog); +$milter->setconn("local:$socket"); +$milter->register( + "milter-header", { + 'connect' => \&milter_cb_connect, + 'header' => \&milter_cb_header, + 'eom' => \&milter_cb_eom, + }, + (SMFI_V2_ACTS|SMFIF_ADDHDRS) +); +logbook("started"); +$milter->main(); +logbook("terminated"); +exit(0); + +# milter hook: connect +sub milter_cb_connect { + my ($ctx, $hostname, $hostaddr) = @_; + my $status = { -header => [] }; + $ctx->setpriv($status); + return SMFIS_CONTINUE; +} + +# milter hook: header +sub milter_cb_header { + my ($ctx, $name, $value) = @_; + my $status = $ctx->getpriv(); + push(@{$status->{-header}}, { -name => $name, -value => $value }); + return SMFIS_CONTINUE; +}; + +# milter hook: end of message +sub milter_cb_eom ($;@) { + my ($ctx) = @_; + my $status = $ctx->getpriv(); + + # loop over all header actions + foreach my $action (@action) { + # check number of fulfilled requirements + my $fulfilled = 0; + foreach my $require (@{$action->{-require}}) { + #logbook("check requirement: header \"%s\" regex \"%s\"", $require->{-name}, $require->{-value}); + foreach my $header (@{$status->{-header}}) { + #logbook("compare with header \"%s\" value \"%s\"", $header->{-name}, $header->{-value}); + my $regex = $require->{-value}; + if ( uc($header->{-name}) eq uc($require->{-name}) + and $header->{-value} =~ m/$regex/) { + #logbook("requirement fulfilled by header \"%s\" value \"%s\"", $header->{-name}, $header->{-value}); + $fulfilled++; + } + } + } + if ($fulfilled == scalar(@{$action->{-require}})) { + # if _all_ requirements are fulfilled + foreach my $insert (@{$action->{-insert}}) { + my $value = $insert->{-value}; + $value =~ s/\s*\n+$//s; + $value =~ s/^\s+//s; + $value =~ s/\n\s+/\n\t/sg; + if (grep { $_->{-name} eq $insert->{-name} } @{$status->{-header}}) { + #logbook("change existing header \"%s\" value \"%s\"", $insert->{-name}, $insert->{-value}); + logbook("change existing header \"%s\"", $insert->{-name}); + $ctx->chgheader($insert->{-name}, 0, $value); + } + else { + #logbook("add new header \"%s\" value \"%s\"", $insert->{-name}, $insert->{-value}); + logbook("add new header \"%s\"", $insert->{-name}); + $ctx->addheader($insert->{-name}, $value); + } + } + } + } + return SMFIS_CONTINUE; +} + @@ . patch -p0 <<'@@ .' Index: openpkg-src/milter-header/milter-header.spec ============================================================================ $ cvs diff -u -r0 -r1.1 milter-header.spec --- /dev/null 2009-07-05 18:56:17 +0200 +++ milter-header.spec 2009-07-05 18:56:31 +0200 @@ -0,0 +1,117 @@ +## +## milter-header.spec -- OpenPKG RPM Package Specification +## Copyright (c) 2000-2009 OpenPKG Foundation e.V. <http://openpkg.net/> +## +## Permission to use, copy, modify, and distribute this software for +## any purpose with or without fee is hereby granted, provided that +## the above copyright notice and this permission notice appear in all +## copies. +## +## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR +## 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 information +Name: milter-header +Summary: Mail Filter for Header Insertion +URL: - +Vendor: Ralf S. Engelschall +Packager: OpenPKG Foundation e.V. +Distribution: OpenPKG Community +Class: EVAL +Group: Mail +License: GPL +Version: 20090705 +Release: 20090705 + +# list of sources +Source0: milter-header.pl +Source1: milter-header.cfg +Source2: rc.milter-header + +# build information +Prefix: %{l_prefix} +BuildRoot: %{l_buildroot} +BuildPreReq: OpenPKG, openpkg >= 20040130 +PreReq: OpenPKG, openpkg >= 20040130 +BuildPreReq: perl, perl-util, perl-sys, perl-time +PreReq: perl, perl-util, perl-sys, perl-time +BuildPreReq: cfg, cfg::with_perl = yes +PreReq: cfg, cfg::with_perl = yes +BuildPreReq: milter, milter::with_perl = yes +PreReq: milter, milter::with_perl = yes +AutoReq: no +AutoReqProv: no + +%description + milter-header is a small "milter" API based daemon that is able to + insert arbitrary headers into mail messages. + +%track + prog milter-header = { + disabled + comment = "rse: OpenPKG package is upstream source" + version = %{version} + url = - + regex = milter-header + } + +%prep + %setup -q -c -T + +%build + +%install + # create installation hierarchy + rm -rf $RPM_BUILD_ROOT + %{l_shtool} mkdir -f -p -m 755 \ + $RPM_BUILD_ROOT%{l_prefix}/bin \ + $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \ + $RPM_BUILD_ROOT%{l_prefix}/etc/milter-header \ + $RPM_BUILD_ROOT%{l_prefix}/var/milter-header + + # install program, manual page and default configuration + %{l_shtool} install -c -m 755 %{l_value -s -a} \ + %{SOURCE milter-header.pl} $RPM_BUILD_ROOT%{l_prefix}/bin/milter-header + %{l_shtool} install -c -m 644 %{l_value -s -a} \ + %{SOURCE milter-header.cfg} $RPM_BUILD_ROOT%{l_prefix}/etc/milter-header/ + + # install run-command script + %{l_shtool} install -c -m 755 %{l_value -s -a} \ + %{SOURCE rc.milter-header} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/ + + # determine installation files + %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \ + %{l_files_std} \ + '%config %{l_prefix}/etc/milter-header/*' \ + '%attr(755,%{l_rusr},%{l_mgrp}) %{l_prefix}/var/milter-header' + +%files -f files + +%clean + rm -rf $RPM_BUILD_ROOT + +%post + # after upgrade, restart service + [ $1 -eq 2 ] || exit 0 + eval `%{l_rc} milter-header status 2>/dev/null` + [ ".$milter_header_active" = .yes ] && %{l_rc} milter-header restart + exit 0 + +%preun + # before erase, stop service and remove log files + [ $1 -eq 0 ] || exit 0 + %{l_rc} milter-header stop 2>/dev/null + rm -f $RPM_INSTALL_PREFIX/var/milter-header/* >/dev/null 2>&1 || true + exit 0 + @@ . patch -p0 <<'@@ .' Index: openpkg-src/milter-header/rc.milter-header ============================================================================ $ cvs diff -u -r0 -r1.1 rc.milter-header --- /dev/null 2009-07-05 18:56:17 +0200 +++ rc.milter-header 2009-07-05 18:56:31 +0200 @@ -0,0 +1,63 @@ +...@l_prefix@/bin/openpkg rc +## +## rc.milter-header -- Run-Commands +## + +%config + milter_header_enable="$openpkg_rc_def" + milter_header_flags="" + milter_header_user="@l_rusr@" + milter_header_prolog="true" + milter_header_epilog="true" + milter_header_numfiles="10" + milter_header_minsize="1M" + milter_header_complevel="9" + +%common + milter_header_cfgfile="@l_prefix@/etc/milter-header/milter-header.cfg" + milter_header_pidfile="@l_prefix@/var/milter-header/milter-header.pid" + milter_header_logfile="@l_prefix@/var/milter-header/milter-header.log" + milter_header_socket="@l_prefix@/var/milter/socket/milter-header" + milter_header_signal () { + [ -f $milter_header_pidfile ] && kill -$1 `cat $milter_header_pidfile` + } + +%status -u @l_rusr@ -o + milter_header_usable="unknown" + milter_header_active="no" + rcService milter-header enable yes && \ + milter_header_signal 0 && milter_header_active="yes" + echo "milter_header_enable=\"$milter_header_enable\"" + echo "milter_header_usable=\"$milter_header_usable\"" + echo "milter_header_active=\"$milter_header_active\"" + +%start -u @l_rusr@ + rcService milter-header enable yes || exit 0 + rcService milter-header active yes && exit 0 + ( umask 002 + nohup @l_prefix@/bin/milter-header $milter_header_cfgfile </dev/null >/dev/null 2>&1 & + echo $! >$milter_header_pidfile + ) >/dev/null 2>&1 + +%stop -u @l_rusr@ + rcService milter-header enable yes || exit 0 + rcService milter-header active no && exit 0 + milter_header_signal TERM + sleep 2 + rm -f $milter_header_pidfile >/dev/null 2>&1 || true + rm -f $milter_header_socket >/dev/null 2>&1 || true + +%restart -u @l_rusr@ + rcService milter-header enable yes || exit 0 + rcService milter-header active no && exit 0 + rc milter-header stop start + +%daily -u @l_rusr@ + rcService milter-header enable yes || exit 0 + shtool rotate -f \ + -n $milter_header_numfiles -s $milter_header_minsize -d \ + -z $milter_header_complevel -m 644 -o @l_rusr@ -g @l_rgrp@ \ + -P "$milter_header_prolog" \ + -E "$milter_header_epilog; rc milter-header restart" \ + $milter_header_logfile + @@ . ______________________________________________________________________ OpenPKG http://openpkg.org CVS Repository Commit List openpkg-cvs@openpkg.org