Jack Disu schrieb:

>     I also confifure sendmail.It is working.
>      But in CRIN mail , when I say send mail to New user
>      counter doesn't stop , saying sending mail to
>      user1 ,2 ,3........

Fixed in CVS already.

Michael
-- 
-------------------------------------------------------------------
Michael Bell                   Email (private): [EMAIL PROTECTED]
Rechenzentrum - Datacenter     Email:  [EMAIL PROTECTED]
Humboldt-University of Berlin  Tel.: +49 (0)30-2093 2482
Unter den Linden 6             Fax:  +49 (0)30-2093 2959
10099 Berlin
Germany                                       http://www.openca.org
## RA Server Management Utility 
## (c) 1999 by Massimiliano Pala
## All Rights Reserved
##
## Project Information:
##
##      Current Version ..................... $VER
##      Project Started on .................. 17/12/1998
##      Last Modified on .................... 09/09/2000
##      Project Closed on ................... n/a
##
## Program currently tested with OpenLDAP v.1.2 on Linux, Solaris
## and Sleepycat DB.
##
## DISC CLAIMER: THIS SOFTWARE IS GIVEN AS IS WITHOUT ANY WARRANTIES
## ABOUT ANY DAMAGE DERIVED BY THE USE ( CORRECT OR NOT ) OF THIS
## SOFTWARE. THE AUTHOR IS THEREFORE NOT RESPONSABLE IN ANY WAY OF
## DAMAGES RELATED IN ANY WAY TO THIS OR SUPPORTED SOFTWARE AS WELL.
##
## If you want to contact me (the author) please use the e-mail
## addresses listed below. Do not esitate in reporting bugs, enhancement
## or anything seems useful in developing this software:
##
##      [EMAIL PROTECTED]
##      [EMAIL PROTECTED]
##      [EMAIL PROTECTED]
##

## librarie's errorcode is 45
##
## warnUsers            10
## sendMail             20
## sendPreparedMails    30
## sendCRINMail         40

## only for testing the library
#use strict;
#my $errno;
#my $query;
#my $VER;
#my $db;
#my $config;

sub warnUsers {
        my @keys = @_;
        my ( $filename, $certDir, $ID, $mail, $user, 
             $tmp );
 
        $certDir = $keys[0];
        return unless $certDir;

        $filename = "$certDir/lastImport.txt";

        ## Let's open the lastImport.txt
        if( not -e "$filename" ) {
                configError( "File $filename not found!");
        }

        $tmp = $query->getFile( "$filename");

        if( $tmp eq "" ) {
                success( "Last Import file was empty.");
        }

        my @certsList = split( "\n", $tmp );
        
        foreach $ID ( @certsList ) {
                my ($user, $userCert, $serID);

                ( $serID ) = ( $ID =~ /([a-f0-9]+)/i );

                ## Let's be sure it is in the right format
                $serID = uc( $serID );
                $serID = "0$serID" if( length($serID) % 2 );

                print "Sending Mail [$serID] : <B>Certificate Serial " .
                      "$serID (0x0" . hex( $serID ) . ")</B><BR>";

                my $cert = $db->getItem ( DATATYPE => "VALID_CERTIFICATE",
                                 KEY => $serID );
                if( not $cert ) {
                        print "<FONT COLOR=\"Red\">";
                        print "Sending Mail [$serID] : can't get certificate" .
                              " from dB!<BR><BR>\n";
                        print "</FONT>";
                        next;
                }

                $userCert = $cert->getParsed();
 
                print "Sending Mail [" . $cert->getSerial() . "] : " .
                       $userCert->{DN_HASH}->{CN}[0];

                print " ( " . $userCert->{EMAILADDRESS} . " )<BR>\n";

                if( $userCert->{EMAILADDRESS} !~ /.*?\@.*?/ ) {
                        print "<FONT COLOR=\"Red\">";
                        print "Sending Mail [$serID] : non valid e-mail" .
                              " address!";
                        print "</FONT><BR><BR>\n";
                        next;
                }

                if( sendMail( $userCert->{EMAILADDRESS}, $cert->getSerial(),
                                           $userCert->{DN_HASH}->{CN}[0] ) ne 
"Success") {
                        print "<FONT COLOR=\"Red\">";
                        print "Sending Mail [$serID] : Can't Send Mail ( $! )";
                        print "</FONT><BR><BR>\n";
                        next;
                };
                
                print "Sending Mail [" . $cert->getSerial() . "] : " .
                      "<B>Mail sent succesfully</B>.<BR><BR>\n";
        }
}

sub sendMail {
        my @keys = @_;
        my ($line, $mailTo, $serialID);
        my $txt = "";
 
        $mailTo   = $keys[0];
        $serialID = $keys[1];
        my $user     = $keys[2];

        my $DEBUG    = 0;
 
       if( ("$mailTo" eq "") || ("$serialID" eq "") || ("$user" eq "") ) {
               configError("Error Sending Mail 434");
               exit 95; 
        };

        my $mailCommand = ($config->getParam('mailcommand'))->{VALUES}->[0];
        my $mailSenderName = 
                 ($config->getParam('mailsendername'))->{VALUES}->[0];
        my $mailSenderAddress = 
                 ($config->getParam('mailsenderaddress'))->{VALUES}->[0];
        my $baseMailFile = ($config->getParam('basemailfile'))->{VALUES}->[0];

        if( not ( $txt = $query->getFile( "$baseMailFile" ))) {
               configError ("Can not open baseMailFile ($baseMailFile)!");
        }

        $txt = $query->subVar( $txt, '@SERIAL@', $serialID );
        $txt = $query->subVar( $txt, '@USER@', $user );
        $txt = $query->subVar( $txt, '@EMAIL@', $mailTo );
 
if( not $DEBUG ) {
        open( FD, "| $mailCommand >/dev/null" ) || return;
        print FD "From: $mailSenderName \n";
        print FD "To: $user <$mailTo>\n";
        print FD "Subject: New Issued Certificate (Serial: $serialID)";
        print FD "\n";
        print FD "$txt";
        print FD "\n";
        print FD "=====================================================\n";
        print FD "Message generated by OpenCA (RAManager) -V.  $VER\n";
        print FD "(c) 1998 by Massimiliano Pala and the OpenCA Team.\n";
        print FD "=====================================================\n";
        print FD "\n.\n";
        close(FD);
} else {
        ## Debug Version
        ## print "<PRE>:::> $mailCommand >/dev/null";
        ## print ":::> From: $mailSenderName \n";
        ## print ":::> To: $user <$mailTo>\n";
        ## print ":::> Subject: New Issued Certificate (Serial: $serialID)";
        ## print ":::> \n";
        ## print ":::> ...";
        ## print ":::> \n";
        ## print ":::> =====================================================\n";
        ## print ":::> Message generated by OpenCA (RAManager) -V.  $VER\n";
        ## print ":::> (c) 1998 by Massimiliano Pala and the OpenCA Team.\n";
        ## print ":::> =====================================================\n";
        ## print "\n.\n</PRE>";
}
        if( $? == 0 ) {
                return "Success";
        } else {
                return "$!";
        }
}

sub sendPreparedMails {

  my $sentMails  = getRequired ('SENT_MAIL_CRIN_COUNTER');
  my $mail_dir   = getRequired ('CRIN_MAIL_DIR');
  my $mailCommand = getRequired ('mailcommand');
  my $counter;

  print addLogSection ("Sending the PIN-Mails ...");

  ## load the counter
  if (not open FD, $sentMails) {
    print addErrorLog("Cannot read mailcounter $sentMails!");
    print closeLogSection ();
    return 0;
  }
  read FD, $counter, 100;
  close FD;
  print addPreLogLine ("Loaded the mailcounter.");
  $counter =~ s/\n//;

  ## mails has the format 123.msg
  print addPreLogLine ("Mailcounter before sending mails is ".$counter.".");
  while (1) {

    $errno = 0;
    sendCRINMail ($counter);

    if ($errno) {
      if ($errno == 4540005) {
        ## how many mails exist?
        opendir (DIR, $mail_dir) or last;
        my @files = grep (/\.msg/i, readdir (DIR));
        closedir (DIR);
        ## compare with counter and decide what to do
        last if ((scalar @files) <= ($counter+1));
      }
      print addPreLogLine ("<FONT COLOR=#FF0000>".
                           $errval.
                           "</FONT>");

    } else {
      print addPreLogLine ("send mail $mail_dir/$counter.msg successful");
    }

    $counter++;

  }
  print addPreLogLine ("Mailcounter after sending mails is ".$counter.".");

  if (not open FD, ">".$sentMails) {
    print addErrorLog("Cannot write to mailcounter $sentMails!");
    print closeLogSection ();
    return 0;
  }
  print FD $counter;
  close FD;
  print addPreLogLine( "Wrote the mailcounter." );

  print closeLogSection ();

  return 1;
}

sub sendCRINMail {
  my $mail_dir   = getRequired ('CRIN_MAIL_DIR');
  my $mailCommand = getRequired ('mailcommand');

  my $counter = $_[0];

  if (not -e $mail_dir."/".$counter.".msg") {
    $errval = "Mail ".$mail_dir."/".$counter.".msg"." does not exist.";
    $errno = 4540005;
    return undef;
  }

  if (not open FD, $mail_dir."/".$counter.".msg") {
    $errval = "Mail ".$mail_dir."/".$counter.".msg"." is not readable.";
    $errno = 4540010;
    return undef;
  }
  my $msg;
  my $line;
  until ( not ($line = <FD>) ) {
    $msg .= $line;
  }
  close FD;
  if (not $msg) {
    $errval = "Mail ".$mail_dir."/".$counter.".msg"." is empty.";
    $errno = 4540020;
    return undef;
  }
  open( FD, "| $mailCommand >/dev/null" );
  print FD "$msg";
  close FD;
  if ( $? != 0 ) {
    $errval = "Mail ".$mail_dir."/".$counter.".msg"." could not be send.";
    $errno = 4540030;
    return undef;
  } else {
    return 1;
  }
}

1;

Reply via email to