#!/usr/bin/perl -I/usr/lib/bs/bin -I/usr/lib/bs/uxmon

#=============================================================================
#
# Id	  : @(#) $Id: log_mail.pl,v 1.3 2002/01/17 19:37:42 aeby Exp $
# Package : swatch
# File	  : log_mail
# Author  : Tom Aeby 
# Date	  : 26/02/96
#
# Usage	: $prog
#
# Function :
#
#	send log messages through mail
#
# $Log: log_mail.pl,v $
# Revision 1.3  2002/01/17 19:37:42  aeby
# fixed security relevant bug: when running "$mailprog": use exec() instead
# of open() since open will start a subshell!
#
# Revision 1.8  2002/01/04 19:06:14  aeby
# moved log_mail and notify to the deprecated directory
#
# Revision 1.7  2001/08/16 15:52:25  aeby
# convert single line messages to multiline (|> -> \n) (Bug #451567)
#
# Revision 1.6  2001/06/30 17:55:33  aeby
# use "use lib" in place of -I
#
# Revision 1.5  2001/02/23 14:52:49  aeby
# PATH is centrally managed via common.pm and etc/resources & adm/resources
# files
#
# Revision 1.4  2000/06/20 21:33:34  jgooch
#
#
# Set QMAILUSER and QMAILNAME to some defaults.  If using qmail, it sets the
# outgoing fields in mail, if not, it's harmless anyway.
#
# Revision 1.3  1999/06/12 15:16:43  aeby
# tested on WinNT and fixed flaky implementations
#
# Revision 1.2  1999/05/22 09:44:02  aeby
# replaced require common by the new use ... proginit() construct
#
# Revision 1.1.1.1  1999/05/06 19:23:32  aeby
# release 0.31 imported into CVS
#
# Revision 1.3  1997/03/04 12:54:28  tom
# (outch) rather use a string for message code
#
# Revision 1.2  1997/03/04 12:48:00  tom
# added numerical message code, time stamp
#
# Revision 1.1  1996/02/26  12:43:13  tom
# Initial revision
#
#=============================================================================
$main::Usage = "[-D debug] [-m message] [-s severity] [-h host|-u user]";

@main::options = ( "H", "s=s", "S=i", "o=s", "m=s", "t=s", "c=s", "h=s", "M=s" );
use lib "."; use lib "uxmon"; use lib "platforms"; 		#inslib
use common;
proginit();

use strict;

my $mailprog = "/usr/ucb/mail";
unless( -f $mailprog ) {
    $mailprog = "/usr/bin/mail";
}
unless( -f $mailprog ) {
    $mailprog = "mail";
}

my( $host, @users );

my $default_user = "sys";
$host = $main::hostname;
chomp $host;

$ENV{'QMAILNAME'}="Big Sister";
$ENV{'QMAILUSER'}="bs";

my $dl = $main::dl;
my $extended_subject = $main::opt_H;
my $subject = $main::opt_s;
my $severity = $main::opt_S;
my $offender = $main::opt_o;
my $msgcode = $main::opt_c;
my $type = $main::opt_t."_";
if( open( IN, "</l/adm/log_mail_key" ) ) {
    $type .= <IN>;
    close IN;
}

my $message = $main::opt_m ? $main::opt_m:$subject;
($host = $main::opt_h) if( $main::opt_h );
my $Mailhost = $main::opt_M;

( @users = @ARGV ) || ( @users = ($default_user) );

if( $extended_subject ) {
    $subject = $host.": ".$subject;
}

my $text = "Type: $type\n";

$message =~ s/\|\>/\n         /g;
$text .="Message: $message\n";
$text .="Host: $host\n";
$text .="Code: $msgcode\n" if( $msgcode );
$text .="Offender: $offender\n" if( $offender );
$text .="Severity: $severity\n" if( $severity );
$text .="Time: ".time."\n";

print "text is: $text\n" if( $main::dl );

if( $Mailhost ) {
    require Net::SMTP;
    my $user;

    my $smtp = Net::SMTP->new($Mailhost); # connect to an SMTP server
    $smtp->mail( "bigsister" );     # use the sender's address here
    foreach $user (@users) {
	$smtp->to($user);        # recipient's address
    }
    $smtp->data();                      # Start the mail

    # Send the header.
    #
    $smtp->datasend("To: ".join(", ",@users)."\n");
    $smtp->datasend("From: Big Sister <bigsister>\n");
    $smtp->datasend("Subject: $subject\n");
    $smtp->datasend("\n");

    # Send the body.
    #
    $smtp->datasend($text);

    $smtp->dataend();                   # Finish sending the mail
    $smtp->quit;                       
}
else {
    $subject = untaint( $subject );
    my $pid = open( MAIL, "|-" );
    if( ! defined $pid ) {
	print STDERR "cannot invoke mail prog\n";
	exit 1;
    }

    unless( $pid ) {
	exec( $mailprog, "-s", "$subject", @users );
	exit 1;
    }

    print MAIL $text;
    close MAIL;
    if( $? ) {
	    print STDERR "mail program returned error $?\n";
	    exit 1;
    }

}



sub usage {
    exit 1;
}


sub untaint {
    my( $val ) = @_;
    $val =~ /^(.*)$/;
    return $1;
}


