#!/usr/bin/perl5
#
# +---------------------------------------------------------------------+
# | dbm_add                                                             |
# | Written By   : Trans-Euro I.T Ltd                                   |
# | Written On   : May 3rd 1998                                         |
# |                                                                     |
# | Purpose        adds an entry to the authorised                      |
# |                telnet database                                      |
# +---------------------------------------------------------------------+

$PROGRAM="dbm_add";
$VERSION="1.00";

$config  ="/usr/local/phalanx/phalanx.conf";
$dbmdir  ="/usr/local/phalanx/database/";
$contact ="";
$report  ="";
$database="";
$sendmail="";
$d_rec;

$user    =$ARGV[0];
$domain  =$ARGV[1];

if ( "ZXSYS$user" eq "ZXSYS" or "XZYSY$domain" eq "XZYSY") {
     print "usage $PROGRAM <authorised_username> <domain_pattern_to add>\n";
     exit 1;} 

read_config();
update_dbm($user,$domain);
mail_support();

exit 0;

# +---------------------------------------------------------------------+

sub read_config {
    my $line;

    open(CONFIG,"$config") || die "Cannot open configuration $config!\n";
     while($line=<CONFIG>) {
       $line=~s/\012//g;
       $line=~s/\ {1,}//g;
         SWITCH: {
          if ($line =~ /^CONTACT=/){(undef,$contact)=split(/=/,$line)};
          if ($line =~ /^REPORTFILE=/){(undef,$report)=split(/=/,$line)};
          if ($line =~ /^DATABASE=/){(undef,$database)=split(/=/,$line)};
          if ($line =~ /^SENDMAIL=/){(undef,$sendmail)=split(/=/,$line)};}}
    close(CONFIG);    
}

# +---------------------------------------------------------------------+

sub update_dbm {
    my ($user) =@_;
    shift;
    my ($domain)=@_;
       $domain =~ s/\012//g;
    my $dbmfile="$dbmdir$database";
    my $db_rec="";
    my $dbe   =";";

    print "Adding User\t\t\t$user\nRecognition pattern\t$domain\n";
    
    dbmopen(%DB,"$dbmfile",0600)||die "Cannot open $dbmfile\n";
            $db_rec=$DB{"$user"};
            if ( $db_rec  eq "" ) { $db_rec="$domain$dbe";} else {
                                    $db_rec="$db_rec$domain$dbe";}
            $d_rec=$db_rec;
            $DB{"$user"}=$db_rec;
    dbmclose(%DB);

}

# +---------------------------------------------------------------------+

sub mail_support {

     my ($d_name) = @_;

     open (MAIL, "| $sendmail $contact") || die "Can't open $sendmail!\n";
     print MAIL "To: $contact\n";
     print MAIL "From:$PROGRAM\n";
     print MAIL "Subject:$PROGRAM : Telnet Database updated\n";
     print MAIL
"------------------------------------------------------\n\n";
     print MAIL "$user now permitted from the following domains\n";
     print MAIL "$d_rec\n";
     close (MAIL);
}                                                                       

