Cover V11, I05

Article
Listing 1
Listing 2
Listing 3
Listing 4
Listing 5

may2002.tar

Listing 3 emctl.pl

001: #!/usr/local/bin/perl -w
002: #
003: use strict;
004: use Mail::Mailer;
005: use Parse::RecDescent;
006: #
007: my $pf = "/myconfigs/emctl.dat";
008: my $gf = "/myconfigs/grammar.dat";
009: my $uf = "/myconfigs/users.dat";
010: my %validcmd;
011: my %validuser;
012: my $me = "me\@acompany.com";
013: my $from = "noone"; 
014: my @rslts;
015: my $rval;
016: my $indicator = 0;
017: #
018: process_config_file($pf,\%validcmd);
019: process_config_file($uf,\%validuser);
020: #
021: open GF, "< $gf" or die "Cannot open $gf\n";
022: my @stuff = (<GF>);
023: close GF;
024: my $grammar = qq { @stuff };
025: #
026: my $parse = new Parse::RecDescent ($grammar);
027: #
028: while (<>) {
029:     next if /^\s*$/;
030:     chomp;
031:     my @a = split;
032:     if ($a[0] =~ /^[F|f]rom\:/) {
033:         s/[\<|\>|\"|\:]//g;
034:         my @line = split;
035:         foreach my $item (@line) {
036:             if ($item =~ /\@/) {
037:                 $from = $item;
038:                 if (!(defined $validuser{$from})) {
039:                     my @msg = "Attempt to run command by $from";
040:                     mr_mailman($me,"Unauthorized email command attempt!",\@msg);
041:                     exit;
042:                 } 
043:             }
044:         }
045:     }
046:     else {
047:         if ($a[0] =~ /^Command\:/) {
048:             $indicator++;
049:             shift(@a);
050:             my $cmd = join " ",@a;
051:             $rval = $parse->Command($cmd);
052:             if ($rval) {
053:                 my @parsertn = split / /,$rval;
054:                 $parsertn[0] = $validcmd{$parsertn[0]};
055:                 my $cstring = join " ",@parsertn;
056:                 my @rslts = do_cmd($cstring);
057:                 my $sbj = "Results of your request to execute $cmd";
058:                 mr_mailman($from,$sbj,\@rslts);
059:             }
060:             else {                # $rval contains a 0 from the parser
061:                 my $sbj = "Problems executing your command!";
062:                 my @rslts = "A syntax error was encountered whilst processing the following command:\n\n$_\n";
063:                 mr_mailman($from,$sbj,\@rslts);
064:             }
065:         }
066:     }
067: }
068: if (!($indicator)) {
069:     my $sbj = "Problems executing your command!";
070:     my @rslts = "I found no commands to execute";
071:     mr_mailman($from,$sbj,\@rslts);
072: }
073: #
074: sub do_cmd {
075:     my $cmd = qq($_[0]);
076:     my @results;
077:     open CMD, "$cmd |";
078:     while (<CMD>) { push @results,$_; }
079:     close CMD;
080:     return @results;
081: }
082: #
083: sub mr_mailman {
084:     my ($to, $subj, $msg) = @_;
085:     my $ecmd = qq(\|mailx -s "$subj" $to);
086:     open (CMD2, $ecmd) or die "Error opening CMD2 for >$ecmd< $!\n";
087:     print CMD2 @$msg;
088:     close CMD2;
089: }
090: #
091: sub process_config_file {
092:     my ($fyle,$hashpointer) = @_;
093:     open CF, "< $fyle" or die "Cannot open $fyle\n";
094:     while (<CF>) {
095:         next if /^\s+$/;
096:         next if /^\s*#/;
097:         chomp;
098:         my @configline = split /,/;
099:         if (defined $configline[1]) { $$hashpointer{$configline[0]} = $configline[1]; }
100:         else { $$hashpointer{$_}++; }
101:     }
102:     close CF;
103: }