#!/usr/bin/eperl -w
###
#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
# Exit codes (on failure error message goes to stderr):
#  0 - success
#  1 - failure
# 
# Boolean arguments on the command line are given as 0 or 1. All the
# command line arguments are encoded to avoid problems with escapes.
#
# All functions defined here either return an error message if an
# error occured and "" if everything went well or allways return a
# valid value, but exit (with code 1) printing an error message if an
# error occurs. This second type of functions have _e appended to
# their name. Functions are allowed to print results onto stdout, but
# errors are printed only in the main program.
# ----------------------------------------------------------------------
# Usage: remove_access <rejected>
# ----------------------------------------------------------------------
# This programs removes from the access database the entry that caused
# a certain email address to be rejected.


push @INC, ($ENV{'OCW_SVCPATH'} || "/usr/lib/opcenter")."/sendmail";
push @INC, ($ENV{'OCW_SVCPATH'} || "/usr/lib/opcenter")."/cmdline_common";

use strict;
require sendmail;
require CmdLineCoder;
require Locking;
sub unlock_die($);

use lib ($ENV{OCW_SVCPATH} or "/usr/lib/opcenter") . "/sendmail";
use lib ($ENV{OCW_SVCPATH} or "/usr/lib/opcenter") . "/cmdline_common";

use ERRORS;
use Carper;

&CmdLineCoder::decode_args();

my( $sendmailcf );    # The 'sendmail.cf' config file in a perl data structure

die "Usage: remove_access <rejected>" unless scalar(@ARGV) == 1;
my $reject = shift;

&Locking::PushLock ($sendmailconf::config{'sendmail_cf'});
$sendmailcf = &sendmail::get_sendmailcf();

my( $afile, $adbm, $adbmtype, @lines, %option);

$afile = &sendmail::access_file($sendmailcf);
($adbm, $adbmtype) = &sendmail::access_dbm($sendmailcf);
if (!$adbm) {
  # No Kaccess directive in sendmail.cf
  $cerr += $E_UNSUPPORTEDSPAM ;
  print STDERR "Spam filter not supported by sendmail.\n";
  &Locking::ReleasePushedLocks;
  exit 1;
}
$cerr += $E_SPAMDATAUNREADABLE and 
    unlock_die("Spam file datafile not readable") unless -r $afile;

@lines = &sendmail::list_access($afile);
@lines = grep { ($_->{action} eq "REJECT") and ($_->{from} eq $reject) } @lines;

if ( defined $lines[0] ) {
    # delete the old reject line from the file
    my(@conffile);
    open (ACCESS,"$afile") || ($cerr += $E_UNABLEREADSPAMFILTER) && unlock_die( "Cannot read spam filter file" );
    @conffile = <ACCESS>;
    close (ACCESS);

    splice (@conffile,$lines[0]->{line},1);
    open (ACCESS,">$afile") || ($cerr += $E_UNABLEWRITESPAMFILTER) && unlock_die( "Cannot write spam filter file" );
    print ACCESS @conffile;
    close (ACCESS);

    # rebuild the database
    system ("$sendmailconf::config{'makemap'} hash /etc/mail/access.db< /etc/mail/access >/dev/null 2>/dev/null");
}

&Locking::ReleasePushedLocks;
exit 0;

sub unlock_die($) {
    my $msg = shift;
    &Locking::ReleasePushedLocks;
    die( $msg );
}

