Subject: Re: Program execution via procmail

From: David Meyer <[email-masked]>
Date: Wed, 11 Aug 1993 09:31:03 -0400 (EDT)

>> I have set up a user on our network called - server, what I need to do is to
>> send a mail message to the user <server>, with the following in the message
>> body
>> 
>> PROGRAM : <program name> <paramter1> <paramter2>

Here's my solution to this.  In .procmailrc, I have

:0:						# execute
* ^subject: execute$
| $MAILDIR/bin/execute.pl

which pipes the message to a perl script called execute.pl.
Execute.pl looks like

#!/usr/local/bin/perl
$outfile="/tmp/execute.out.$$";
$tmpinp="/tmp/execute.inp.$$";
open(SAVEINP,">$tmpinp") || die "Can't open $tmpinp" ;
print SAVEINP <<'DONE';
PATH=/usr/local/bin:/bin:/usr/ucb:/usr/bin
set -x
DONE

header: while (<>) {
    chop;
# Find the return-path.  Probably should look at From line if no
#  return-path, but I haven't done it yet.
    if (/^Return-Path:.*\<(.*)\>.*$/) {
        $retpath=$1;
        next header;
    }
    if ($_ eq "") {last header};
}

main: while (<>) {
# look for password: entry
    if ((!defined($password)) && (/^(pass.*:\s*)(\S*)(.*)/i)) {
        $password=$2;
	$passecho=$1.'*' x length($2).$3."\n";
        next main;
    }
# assume -- means beginning of .sig - ignore everything after it
    if (substr($_, 0, 2) eq '--') {
        last main;
    }
# otherwise send to script SAVEINP
    print SAVEINP $_ ;
}
close(SAVEINP);
if ($password eq "mypass") {
    open(SAVEOUT,">&STDOUT");
    open(SAVEERR,">&STDERR");
    open(STDOUT,">$outfile") || die "Can't redirect stdout";
    open(STDERR,">&STDOUT") || die "Can't redirect stdout";
    select(STDERR); $| = 1;
    select(STDOUT); $| = 1;

    print STDOUT $passecho;
    system "sh $tmpinp";
    
    close(STDOUT);
    close(STDERR);
    open(STDOUT,">&SAVEOUT");
    open(STDERR,">&SAVEERR");

} else {
    open(SAVEOUT,">$outfile");
    if ((defined($password)) && (length($password) gt 0)) {
        print SAVEOUT "bad password - cannot execute command\n";
    } else {
        print SAVEOUT "no password given - cannot execute command\n";
    }
    close(SAVEOUT);
}
chop($date=`date`);
$header="\"execute output from $date\"";
system "cat $outfile | /usr/ucb/Mail -s $header $retpath ";
unlink $tmpinp;
unlink $outfile;

Basically, you send a message with the subject "execute", and the body
of the message has shell commands.  You also need to have the line
"password: mypass" in order for this to work (you should change mypass
to something more secure).

Hope this helps,
		Dave

-- 
David M. Meyer                             Mechanical & Aerospace Engineering
[email-masked]          University of Virginia
NeXTmail ok