Listing 3 RCCFG script
#!/usr/bin/perl
#############################################################
##### file name: rccfg
##### purpose: Runlevel tool used my sys admin to manage runlevel rc scripts
##### secured version 1.3
#####
##### author: BJM 07/25/03
#############################################################
use strict; use File::Basename;
##################Global Vars #########################
################# Config File #########################
my $gscripts; my $lscripts; my $stlink; my $stplink; my $rclog; my $secaccess;
read_cfg(); ### Go read config file and get parms
my $command=basename($0); chomp($command);
my $secfile="$secaccess/$command.access";
################# Config File #########################
my $host=`hostname -s ` ; chomp($host);
my $ldate=`date +%m_%y`; chomp($ldate);
my $rccfglog="$rclog/rccfglog.$ldate"; ## command log file
my @srchlist=(); ## Global link name
my $parms=join("",@ARGV);
my $comn; my $appn;
my $valid="start stop list all status build noauto";
my $appaccess; ## for use with secured ver filled from /etc/<basename>.access
################## End Global Vars #########################
######################### MAIN
chk_input(); ### chk input parms
if ( $comn=~/build/ix ) { bld_links();} ### Create links
elsif ($comn=~/noauto/ix) {
noauto("$stlink");
noauto("$stplink"); } ### remove links
elsif ($comn eq "list") {list_apps();} ### List All Applications
else { rc_std(); } ### start,stop,status calls
############################ MAIN
############################################################
sub rc_std {
### Mimic Standard rc
### local Vars
my $comm; my $item;
### local Vars
## load list of scripts into array of srchlist
if ($appn eq "All") { mappn("All") ; ## Load all scripts
} else { mappn("$appn"); } ## Load specific script
####################################### send task to script list
while(@srchlist) {
$item=shift(@srchlist);
$comm="$stlink\/$item $comn";
#print "$comm echo \n";
system("$comm");
}
}
############################################################
sub rm_links {
### To Remove lnks
### local Vars
my @line=(); my $comm; my $file;
my $target=$_[0];
### local Vars
if (!($target=~/\//)) { die "you did not submit a removal directory ";}
## remove all links prior to rebuild
opendir( lstdir, $target);
while( defined ($file = readdir lstdir)) {
if (($file=~/^S/) || ($file=~/^K/)){
$comm=" rm $target\/$file ";
print " Removing $comm \n";
system("$comm");
}
}
close(lstdir);
}
############################################################
sub list_apps {
### To list all Application Names
### local Vars
my @applist; my @appname; my $temp;
### local Vars
mappn("All"); ## go get all link names
@applist=@srchlist; # filled by mappn
print "Runlevel Application Listing for Node $host \n";
print "\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-";
print "\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\n";
while(@applist){
$temp=pop(@applist);
@appname=split(/\./,$temp);
print " Application \-\> $appname[1] \n";
}
print "\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-";
print "\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\n";
}
############################################################
sub bld_links {
### remove and recreate links in $stlink and $stplink
### local Vars
my $startlnk; my $stoplnk;
my @line=(); my $comm; my $file;
my $torder; my @korder; my @tmplist; my $inc=1; my $pinc;
### local Vars
## remove all links prior to rebuild
rm_links("$stlink");
rm_links("$stplink");
## remove all links prior to rebuild
opendir(lstdir,$lscripts) || die " cant open local directory ";
while(defined ($file = readdir lstdir)) {
if ($file=~/^S/){
push(@tmplist,$file);
@line=split(/\./,$file);
$startlnk="$stlink\/$line[0].$line[1].$line[2]";
print " making Start Link ln -s $lscripts\/$file $startlnk\n";
system("ln -s $lscripts\/$file $startlnk");
}
}
@korder= sort @tmplist; ## sort for the kill order and create $inc
close(lstdir);
opendir(lstdir,$lscripts) || die " cant open local directory ";
while(@korder) {
if ($inc < 10 ) {
$pinc="0$inc";
} else { $pinc=$inc; }
$torder=pop(@korder);
$stoplnk="$stplink\/K$pinc.$torder";
print " making Stop Link ln -s $lscripts\/$torder $stoplnk \n";
system("ln -s $lscripts\/$torder $stoplnk");
++$inc;
}
close(lstdir);
print " Finished Removing/Restoring runlevel Links \n";
}
############################################################
sub noauto {
### remove start link
### local Vars
my $comm; my $file;
my $lnkdir=$_[0];
### local Vars
if (!($lnkdir =~ /\//)) { die " sent invalid dir to noauto " }
## Removing one or all links for an application
opendir(lstdir,$lnkdir);
while( defined ($file = readdir lstdir)) {
if(($appn=~/All/m) && (($file=~/^K/)||($file=~/^S/))) {
## removing all links ..
$comm="$lnkdir\/$file";
print " Removing all Startup links $comm \n";
system("rm $comm");
} else {
#starting search
if (($file=~/$lnkdir\/S/ix ) || ($file=~/\.$appn\./m )) {
### checking that its the start script
$comm="$lnkdir\/$file";
print "Removing Startup link $comm \n";
system("rm $comm");
}
}
}
close(lstdir);
}
############################################################
sub mappn {
### Match App Name called to push filelist on Array from Search strings
### local Vars
my $comm; my $match=$_[0]; my $file; my $patt;
my @tmplist=();
### local Vars
opendir( lstdir, $stlink ) || die " Failed to lstdir $stlink ";
while( defined ($file = readdir lstdir)) {
chomp;
if (( $file=~/^S/x) && ( $file=~/\.$match\./x) ){
push(@tmplist,$file);
} elsif (( $match eq "All" ) && ( $file=~/^S/) ){
push(@tmplist,$file);
}
}
## Check to make sure we found a start script
$patt=join('',@tmplist);
if (!($patt=~/S/)) {print "\n\n Failed to Match an Application Name\n"; die Usage();}
## create ordered srchlist for return
@srchlist= sort @tmplist ;
}
############################################################
sub chk_input {
### local Vars ###########################3
my $date=`date +%D-%T`; chomp($date);
my $host=`hostname -s ` ; chomp($host);
my $report="$date $0 @ARGV";
### local Vars ###########################
## If list is not in $parms then normal parm check
if ( $parms !~/list/i) {
## Check for command parms #'s then validity
if ((!($ARGV[0])) || (!($ARGV[1])) || ($parms=~/\-h/x) || \ ($parms=~/all/x) || ($parms=~/\?/x )) { die Usage(); }
## Check for command parms
if ( (!($valid =~/$ARGV[0]/x)) && (!($valid =~/$ARGV[1]/x)) ){ die Usage(); }
## set the command name and app name parm ##
if ( $valid =~/$ARGV[0]/x ) { $comn =$ARGV[0]; $appn=$ARGV[1];}
else { $comn=$ARGV[1] ; $appn=$ARGV[0]; }
if(($comn=~/build/m) && ( $appn !~/All/)) { die Usage(); } ## make sure they want to rebuild
} else { $comn="list"; $appn="All"; }
########### Security Access Check #######################################
if (($0 !~/rccfg/) && ( $parms !~/list/i)) {
### Read in security file if neccesary
rsecfile("$secfile");
if( ($appaccess !~/$appn/) && ($comn !~/status/) ) {
$report="$date $0 @ARGV FAILED FOR ACCESS AND LOGGED " ;
record("$report");
die &Usage("F"); ## send die access msg
}
}
########### Passed Access Check #######################################
## Passed check log the command send to rccfg in /stats/hostname
record("$report");
}
############################################################
sub rsecfile {
## Security sub used for secured version only
##local Var
my $fname="$_[0]"; chomp($fname);
my @temp;
##local Var
open(fin,$fname) || die " Secured Application Authorization file $fname does not exits \n";
while (<fin>) {
if (!(/^\#/)) {
@temp=split;
$appaccess="$appaccess $temp[0] ";
}
}
close(fin);
}
############################################################
sub Usage {
### generic Usage for secured and unsecured version
###localVar
my $secure;
my $access;
if ($_[0]) {
$access="$_[0]"; ## should be "F"
} else { $access="T";}
###localVar
##Check if this is a secured version or not
if ($0 !~ /rccfg/ ) {
$secure="T"
}
system("clear");
print <<EOF ;
*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
EOF
if (($secure eq "T") && ($access eq "F" )) {
print "UNAUTHORIZED APPLICATION ACCESS --\n";
}
print <<EOF ;
$0 <app name > < command >
$0 <command> <appname>
Valid commands:
start <appname> = Call link for appname startup
stop <appname> = Call link for appname stop
status <appname> = Call link for appname status
noauto <appname> = Remove startup link for an application
noauto <appname> = Remove startup link for an application
build All = Refresh/Recreate all Runlevel links from local dir
The "All" appname is req and case sensitive
list = Lists all local Runlevel Application Names
Valid appnames
EOF
if ($secure eq "T" ) {
print "$appaccess ";
} else {
print <<EOF ;
<appname> = Pattern to match <full field will be matched, case sensitive >
All = Run the command for all, case sensitive
EOF
}
if (($secure) && ($access eq "F" )) {
print "\n\n---UNAUTHORIZED APPLICATION ACCESS WAS RECORDED----\n";
}
print <<EOF ;
*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
EOF
exit;
}
###########################################################
sub record {
### record valid commands to log file
### local Vars ###########################3
my $msg=$_[0];
#$rclog is our log file
if (!( -d ("$rclog"))) { die "$rclog does not exist ABORT";
exit :}
open(record,">>$rccfglog");
print record "$msg \n";
close(record);
}
############################################################
############################################################33
sub read_cfg {
# Read in Global variables
#my $gscripts; my $stlink; my $stplink; my $rclog; my $lscripts; my $secaccess;
##########################Local Vars
my $match="gscripts lscripts stlink stplink rclog secaccess "; ## required parm list
my @mlist=split(" ",$match);
my @line; my %rparm; my $key; my $parm;
my $config_file="/etc/rccfg.config" ;
###########################################3
open(fin,"cat $config_file \| grep -vE \"^\\#\" |") || die " Config file $config_file is not present -- \n";
## Read config into hash
while (<fin>) {
chomp;
if ((/[a-z]/i)) { # Allow for blanks
@line=split(/ /);
if ($match =~/$line[0]/) {
$rparm{$line[0]}=$line[1];
}
}
}
close(fin);
####Setting Parms
for $key (keys %rparm) {
#print " $key -> $rparm{$key} \n";
if ($key eq "gscripts") {$gscripts=$rparm{$key}; }
elsif ($key eq "lscripts") {$lscripts=$rparm{$key}; }
elsif ($key eq "secaccess") {$secaccess=$rparm{$key}; }
elsif ($key eq "stlink") {$stlink=$rparm{$key}; }
elsif ($key eq "stplink") {$stplink=$rparm{$key}; }
elsif ($key eq "rclog") {$rclog=$rparm{$key}; }
else { print " Invalid parm in config file $key ignored \n"; }
}
#####checking for all config variables
foreach $parm (@mlist) {
if ($rparm{$parm} =~/[a-z]/i) {
#print " $parm -> $rparm{$parm} is valid \n ";
} else { die "ERROR $parm is not valid and is required in rccfg.config file ERROR";}
}
}
### end sub read_cfg
############################################################
|