#!/usr/bin/perl -w

=head1 NAME

arc-config-check - checks the arc.conf for inconsistencies, known problems
or (in a future development) just general bad taste.

=head1 SYNOPSIS

arc-config-check --printall

=head1 DESCRIPTION

The motivation behind this little script was to have a repository for
automated tests on issues that came up on the NorduGrid developers
mailing list. As such this script indicates directories that are not
present, checks host certificates, CA certificates and CRLs, validates
the sanity of ARC configuration and tests for clock skew.

BECAUSE EVERY INSTALLATION OF ARC IS DIFFERENT THIS UTILITY ONLY SUGGESTS
WHAT COULD BE WRONG. SOMETIMES IT IS OVERRESTRICTIVE. AND SOMETIMES IT
CAN MISS SOME MISCONFIGURATION. NEVER TREAT RESULTS PRODUCED BY IT AS
ULTIMATE TRUTH.

=head1 OPTIONS

=over 4

=item --quiet

Do not print any output

=item --verbose

Print detailed output

=item --debug

Print very detailed output

=item --config <string>

Specifies the location of the config file, by default it is /etc/arc.conf

=item --printall

Lists all variable names of the config file together with their values.

=item --timeserver <server>

Allows the specification of a server against which to test the local
system's time.

=item --skip-warnings

Do not show warnings.

=item --help

Quick summary of options,

=item --man

Detailed man page.

=item --openssl-path <filename>

Path to openssl binary, will be found from PATH if not set.

=back

=cut


#################### P R E A M B E L  and options parsing ####################

use strict;
use warnings;
use Getopt::Long;
use POSIX;

my ($conffile,$printall,$skip_warnings,$help,$man)=("/etc/arc.conf",0,0,0,0);

#Please make sure this reference server is not one you administer yourself.\n";
my $timeserver="europe.pool.ntp.org";

my $globusloc = $ENV{"GLOBUS_LOCATION"};

my $arcloc = undef;
if (defined $ENV{"ARC_LOCATION"}) {
	$arcloc = $ENV{"ARC_LOCATION"};
} else {
	$arcloc = "/usr";
}
my $OS = `uname`;
chomp $OS;

my $usercert;
my $hostcert;
my $CApath;
my $opensslpath;
my $verbose=0;
my $debug=0;
my $quiet=0;

GetOptions(
	"config:s" => \$conffile,
	"printall" => \$printall,
	"skip-warnings" => \$skip_warnings,
	"timeserver:s" => \$timeserver,
	"openssl-path:s" => \$opensslpath,
	"verbose" => \$verbose,
	"debug" => \$debug,
    "quiet" => \$quiet,
	"help" => \$help,
	"man" => \$man
	) or die "Could not parse options.\n";

if ( $man or $help ) {
        # Load Pod::Usage only if needed.
	require "Pod/Usage.pm";
	import Pod::Usage;
	pod2usage(1) if $help;
	pod2usage(VERBOSE => 2) if $man;
}


# key counters

my $warnings=0;
my $errors=0;

$verbose=1 if $debug;


########## S E E D   A S S U M P T I O N S   F O R   R U L E S ############

# Some bits of the configuration are dynamic and may cross-reference other
# bits of the configuration. This hash shall keep track of these.

my %introducedSections = (
	"userlist" => [],
	"authgroup" => [],
	"arex/jura/sgas" => [],
	"arex/jura/apel" => [],
	"queue" => [],
	"custom" => []
);

print STDERR "The following sections have configurable subgroups: "
		. join(", ",keys %introducedSections)."\n" if $verbose;

################ U T I L  R O U T I N E S #################################

# Prints message with required format
sub p($) {
	my $s = shift;
	my $t = strftime "%F %T", localtime;
	print STDERR "[$t] [Arc.validator] $s" if !$quiet;
}

# prints and counts a warning
sub w($) {
	my $s = shift;
	if (!$skip_warnings) {
		p("[WARNING] $s");
		$warnings++;
	}
}

# prints and counts an error
sub e($) {
	my $s = shift;
	p("[ERROR] $s");
	$errors++;
}

sub v($) {
	return unless $verbose or $debug;
	my $s = shift;
	p("[VERBOSE] $s");
}

####################### C H E C K S  #######################################

=head1 PERFORMED TESTS

=over 4

=item timecheck

The current time is compared with an external time server. A clock
shift higher than a maximally allowed maxtimediff results in an error.

=cut

sub timecheck($$$) {
	my ($timeserver, $maxtimediff, $maxtimediffwarn) = @_;
	my $timeoffset = undef;

	my $ntpdate = "/usr/sbin/ntpdate";
	unless ( -x $ntpdate )  {
		w("Could not find location of 'ntpdate'.\n");
		return;
	}

	unless (open(NTPDATE, "$ntpdate -p 1 -q $timeserver |")) {
		w("Could not properly invoke 'ntpdate'.\n");
		return;
	}
	while (<NTPDATE>) {
		next unless m/^server/;
		if (m/offset *[-+]?([0-9]*\.[0-9]*)/) {
			$timeoffset = $1;
		}
	}
	close NTPDATE;

	if (defined $timeoffset) {
		if (abs($timeoffset)>=$maxtimediff) {
			e("Timecheck: Your time differs by more than " .
				"$maxtimediff seconds ($timeoffset seconds) from the " .
				"public time server '$timeserver'\n");
		} elsif (abs($timeoffset)>=$maxtimediffwarn) {
			w("Timecheck: Your time differs slightly " .
				"($timeoffset seconds) from the public time " .
				"server '$timeserver'.\n");
		}
	} else {
		w("Timecheck: Can't check the time\n");
	}
}

=item check of permissions

The permission to access several different directories are checked.
The first argument is the file to be checked. The second is the permission
that is should have. The third is a binary mask that selects the bits
that are to be inspected.

=cut

sub permcheck($$$) {
	my ($filename, $p, $mask) = @_;
	my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
	       $atime,$mtime,$ctime,$blksize,$blocks)
	                  = stat($filename);
	$mode &= 07777;
	printf "$filename: mode %04o to be compared with %04o at mask %04o\n",$mode,$p,$mask if $debug;
	$mode &= $mask;
	return ($p == $mode);
}


# this performs simple stateless checks of configuration entries
sub confchecktripel($$$) {

	my ($block, $name, $value) = @_;

	printf STDOUT "Checking in block %s, name %s, value %s\n",$block,$name,$value if ($verbose);

	# check the certificate
	if ($block eq "common" and $name eq "x509_host_cert") {
		if (! -e $value) {
			e("The host certificate '$value' does not exist or is unreadable.\n");
		} elsif (! -O $value) {
			e("The host certificate '$value' is not owned by this user.\n");
		} elsif (!permcheck($value,0644,0777) and !permcheck($value,0444,0777)) {
			e("Permission of '$value' must be 'rw-r--r--' or 'r--r--r--'\n");
		}
		else {
			$hostcert=$value;
		}
	}

	# check the key
	elsif ($block eq "common" and $name eq "x509_host_key") {
		if (! -e $value) {
			e("The host key '$value' does not exist or is unreadable.\n");
		} elsif (! -O $value) {
			e("The host key '$value' is not owned by this user.\n");
		} elsif (!permcheck($value,0400,0777) and !permcheck($value,0600,0777)) {
			e("Permission of '$value' must be 'r--------' or 'rw-------'\n");
		}
	}

	# check the certificate direcotry
	elsif ($block eq "common" and $name eq "x509_cert_dir") {
		if (! -d $value) {
			e("$name: The certificate directory $value does not exist.\n");
		} else {
			my @r0s=glob($value."/*.r0");
			if ($#r0s == -1) {
				w("$name: There are no certificate revocation lists.\n");
			} else {
				require File::stat;
				my $t=time();
				my $maxdiffsecs=60*60*24*2; # two days
				foreach my $r0 (@r0s) {
					my ($dev,$ino,$mode,$nlink,
						$uid,$gid,$rdev,$size,
						$atime,$mtime,$ctime,
						$blksize,$blocks)
						  = stat($r0);
					if ($t < $mtime ) {
						e("$r0: mtime in future\n");
					} elsif ($t > $mtime + $maxdiffsecs) {
						w("$r0: Older than $maxdiffsecs seconds. rerun fetch-crl\n");
					}
				}
			}
			$CApath=$value;
		}
	}

	# check the cache directory
	elsif ($block eq "arex/cache" and $name eq "cachedir") {
		my @dirs=split(/\s+/,$value);
		# If available, second dir is on workernode
		my $d = $dirs[0];
		if (! -d $d)  {
			w("cachedir: not existing at '$d'\n");
		}
	}

	# check the session directory
	elsif ($block eq "arex" and $name eq "sessiondir") {
		my @dirs=split(/\s+/,$value);
		my $dpath = $dirs[0];
		if (! -d $dpath)  {
			w("sessiondir: not existing at '$dpath'\n");
		} else {
			v("$name exists.");
		}
		if (scalar @dirs > 1) {
			# If multivalued second should be the drain instruction
       			my $drain = $dirs[1];
                	if ($drain ne "drain")  {
                        	e("Sessiondir unknown instruction: '$drain'\n");
                	} else {
                        	w("Sessiondir $dpath is set to drain. Jobs will not be accepted if this is the only sessiondir.\n");
                	}
                }
	}

	# check the controldir
	elsif ($block eq "arex" and $name eq "controldir") {
		if (! -d $value)  {
			w("$value: control directory ($name) does not exist\n");
		} elsif (!permcheck($value,0755,0777) and !permcheck($value,0700,0777)) {
			e("$value: directory ($name) should be 755 or 700\n");
		}
	}

	# check all remaining directory entries of the arex block for existence
	elsif ($block eq "arex" and $name =~ m/dir$/) {
		if (! -d $value)  {
			w("$value: directory ($name) does not exist\n");
		}
		else {
			v("$name exists.");
		}
		if ($name =~ /^(tmpdir)$/) {
			if (! permcheck($value,0777,0777)) {
				e("The tmpdir directory must be writable and have the sticky bit set (chmod +t \"$name\")\n");
			}
		}

	}
	
	# Check for logrotate paths
	if ($block =~ /^(arex|gridftpd|infosys)$/ and $name =~ /^(logfile|pidfile)$/) {
	    check_logrotate($block,$name,$value);
	}

	# check for any mount dirs
	if ($name eq "mount") {
		if (! -d "$value") {
			e("$value: directory ($name) does not exist\n");
		}
		else {
			v("$name exists.");
		}
	}
    
    # check few infosys commands
    if ($block eq "infosys/ldap") {
        if ($name eq "slapd_loglevel" and $value gt 0) {
            w("slapd_loglevel > 0 should only be used for development. DO NOT USE for production and testing.\nConfigure bdii_debug_level=\"DEBUG\" instead.\n");
        }
        if ($name ne "bdii_debug_level" and $name =~ /^bdii/ ) {
            w("Starting from release 13.02, BDII configuration commands are strongly deprecated. Use only for Development.\nThe only allowed command is bdii_debug_level\n");
        }
    }
    
    # check compliance of AdminDomain name as a LDAP DNs
    # TODO: add to startup script the capability of escaping special
    # chars.
    if ($block eq "infosys/glue2") {
        if ($name eq "admindomain_name") {
        w("Warning: $block $name not configured. Default AdminDomain name is UNDEFINEDVALUE.\n") if $value eq '';
        # bad symbols from RFC 4514
        my @syntaxerrors;
        push (@syntaxerrors, ("\n\tThe following unicode chars are not allowed:", $&)) while ( $value =~ /\x{0022}|\x{002B}|\x{002C}|\x{003B}|\x{003C}|\x{003E}|\x{005C}/g );
        push (@syntaxerrors, ("\n\tBlank space or \"#\" symbol at the beginning of name are not allowed")) if ( $value =~ /^#|^\s/ );
        push (@syntaxerrors, ("\n\tBlank space at the end of name is not allowed")) if ( $value =~ /\s$/ );
        push (@syntaxerrors, ("\n\tNull character unicode (U+0000) not allowed")) if ( $value =~ /\x{0000}/ );
        e("$block option $name contains the following errors: @syntaxerrors \n\tLDAP Infosys will not start. \n\tPlease change AdminDomain name accordingly *\n") if @syntaxerrors;
        
        }
   }
   	
}

# Checks log locations for standard location
sub check_logrotate($$$$) {
    my ($block, $name, $value, $defaultlocation) = @_;
    my $defaultlogpath = '/var/log/arc/';
    my $defaultpidpath = '/var/run/';
    my $defaultlocations = {
                            'arexlogfile' => $defaultlogpath.'arex.log',
                            'arexpidfile' => $defaultpidpath.'arched-arex.pid',
                            'gridftpdlogfile' => $defaultlogpath.'gridftpd.log',
                            'gridftpdpidfile' => $defaultpidpath.'gridftpd.pid',
                            'infosyslogfile' => $defaultlogpath.'infoprovider.log',
                           };
    $defaultlocation = $defaultlocations->{$block.$name} || $defaultlogpath;
    if ($value ne $defaultlocation ) {
        w("[$block] $name: $value is not in the default location ($defaultlocation). Logrotation will not work, check your system logrotation configuration files\n");
    }
}

# Get all valid configuration parameters and blocks from arc.parser.defaults.
# Dynamic blocks which match those in %introducedSections are not included.

sub validconfparams() {

	my @validparams;
	my @validblocks;
	# parameters allowed to have no value
	my @novalueparams = ("all", "mapped_unixid");
	unless (open (ARCREF, "<$arcloc/share/arc/arc.parser.defaults")) {
		e("Could not open arc.parser.defaults so cannot check arc.conf consistency\n");
		return ( \@validparams, \@validblocks, \@novalueparams );
	}
	while (my $line = <ARCREF>) {
		if ($line =~ m/^(\w*)=.*$/) { # match parameter=value
			my $name = $1;
			if (!grep(/^$name$/, @validparams)) {
				push(@validparams, $name);
			}
		} elsif ($line =~ m|^\[(.*)\]$|) { # match [section]
			my $blockname = $1;
			my $dynamic = 0;
			foreach my $k (keys %introducedSections) {
				if ($blockname =~ m/^$k.*/) {
					$dynamic = 1;
				}
			}
			push(@validblocks, $blockname) if !$dynamic;
		}
	}
    # allow removed in 6.8.0 (hardcode)
    push(@validparams, "urdelivery_keepfailed");
    push(@validparams, "legacy_fallback");
    push(@validparams, "use_ssl");
    push(@validparams, "benchmark_type");
    push(@validparams, "benchmark_value");
    push(@validparams, "benchmark_description");
    push(@validparams, "archivedir");
    push(@validparams, "archivettl");
    push(@validblocks, "arex/jura/archiving");
    return ( \@validparams, \@validblocks, \@novalueparams );
}
	
=item configuration check

General checks on the sensibility of the arc.conf

=cut

sub confcheck($) {
	my ($arcconf) = @_;
	my $config = {};

	unless (open (CONFIGFILE, "<$conffile")) {
		e("Could not open '$arcconf' for reading.\n");
		return;
	}

	# valid parameter names and blocks
	my ($validparams, $validblocks, $novalueparams) = validconfparams();
	my $blockname = undef;
	my $blockcontents = 0;
	my $c = 0;
	my $vo_counter = 0;
	while (my $line = <CONFIGFILE>) {
		if ($line =~ m/^<\?xml version=.*\?>$/) {
			w("XML configuration cannot be validated\n");
			return;
		}
		$c++;

		next if $line =~ m/^#/;
		next if $line =~ m/^\s*$/;

		# a new block?
		if ($line =~ m/^\s*\[(.+)\]\s*$/) {
			$blockname = $1;
			$blockcontents = 0;
			
			# No spaces allowed in block names
			if (($blockname !~ m/:/ && $blockname =~ m/\s/) || ($blockname =~ m/:\s*\w+\s+\w+\s*/)) {
			    e("No spaces allowed within block keywords or identifiers: [$blockname]\n");
			    next;
			}

			# blocknames must be unique
			# but there is a special case of userlist blocks...
			if ($blockname eq "userlist") {
				$blockname .= "|" . ++$vo_counter;
			}
			if (defined $config->{$blockname}) {
				w("$arcconf:$c: Block '"
				  . $blockname
				  ."' is defined multiple times\n");
				$warnings++;
			}

			$config->{$blockname}{">]|found|[<"} = $c;

			next;
		}

		my $name;
		my $value;

		# look out for crap
		unless ($line =~ m/^([^=]*)(=.*)?$/) {
			e("$arcconf: $c: Bad line!\n");
			next;
		}

		$name = $1;
		$value = $2;

		$name =~ s/^\s*//;
		$name =~ s/\s*$//;
		$name =~ s/^[-+!]//;
		
		if (!defined $value and !grep(/^$name$/, @$novalueparams)) {
		    e("$arcconf: $c: No value specified for $name\n");
		    next;
		} 
		
		if (defined $value) {
		    $value =~ s/^=//;
		    $value =~ s/^\s*//;
		    $value =~ s/\s*$//;

    		if ($value =~ m/^"/ and $value =~ m/"$/
	    	  or $value =~ m/^'/ and $value =~ m/'$/) {
		    		w("$arcconf: $c: quotes around attribute value\n");
			    	$warnings++;
		    }
		}

		# are we within a block?
		unless (defined $blockname) {
			e("$arcconf:$c: found value=name pair which is " .
				"not part of a block\n");
			next;
		}

		# check we have a valid parameter name, ignoring [custom:] blocks
		if (@$validparams != 0 and !grep(/^$name$/, @$validparams) and $blockname !~ m/^custom:/) {
			e("$arcconf:$c: found unknown parameter $name\n");
			next;
		}

		# check if we know more about this kind of tripel
		confchecktripel($blockname, $name, $value);

		#count
		$blockcontents++;

		if ($config->{$blockname}{$name}) {
			$config->{$blockname}{$name} .= ">]|sep|[<" . $value;
		} else {
			$config->{$blockname}{$name} = $value;
		}
	}

	close CONFIGFILE;

	check_completeness($config);

	if ($printall) {
		foreach my $key (sort { $config->{$a}{">]|found|[<"} <=> $config->{$b}{">]|found|[<"} }  keys %$config) {
			printf "\n# line: %d\n", $config->{$key}{">]|found|[<"};
			if ($key =~ m/^(.*)\|[0-9]+$/) {
				printf "[%s]\n", $1;
			} else {
				printf "[%s]\n", $key;
			}
			my $x = $config->{$key};
			foreach my $item (sort keys %$x) {
				next if $item eq ">]|found|[<";
				foreach my $val (split />\]\|sep\|\[</, $config->{$key}{$item}) {
					printf "%s=%s\n", $item, $val;
				}
			}
		}
	}
}

=item check for completeness

Return error if the presence of one value implies one of another

=cut

sub check_completeness() {
	my $config=shift;

	my @required=("common", "mapping", "arex", "infosys", "infosys/glue2", "lrms");

	my ($validparams, $validblocks, $novalueparams) = validconfparams();

	# testing for unknown
	foreach my $k (keys %$config) {
		next if grep(/^$k$/,@$validblocks) || !@$validblocks;
		my @secs;
		map { push(@secs,$_) if $k=~/^$_:/;} keys %introducedSections;
		print STDERR "$k -> " . join(":",@secs)."\n" if $debug;

		if (0 == @secs) {
			e("Unknown block identifier '[$k]'\n");
			next;
		}
		elsif (1 < @secs) {
			die "Programming error: found multiple sections "
			    .join(",",@secs)." to be prefixes of section '$k'.\n";
		}

		my $secs1 = $secs[0];
		unless (exists($introducedSections{$secs1})) {
			die "Programming error: section '$secs1' not amongs keys of hash %introducedSections: "
			    . join(",",keys %introducedSections)."\n";
		}
		my $listref=$introducedSections{$secs1};
		push @$listref,$k;
	}

	# testing for the missing
	foreach my $k (@required) {
		unless (exists($config->{$k})) {
			e("Missing mandatory block '$k'\n");
		}
	}
	# hostname
	my $hn=`hostname -f`;
	chomp($hn);
	my $hn_conf;
	if (exists($config->{common}{hostname})) {
		$hn_conf = $config->{common}{hostname};
	}
	elsif (exists($config->{'infosys/cluster'}{hostname})) {
		$hn_conf = $config->{'infosys/cluster'}{hostname};
	}
	if ($hn_conf) {
		if ($hn_conf ne "$hn") {
			w("The entry of the full hostname (".$hn_conf
			                      . ") is better\n"
		            ."   equal to `hostname -f` ($hn).\n"
		            ."   Also test reverse lookup of the hostname.\n");
		}
	}
	else {
		w("No hostname defined, the default $hn will be used.\n");
	}

	# Mapping cannot be empty if running as root
	if (getpwnam(getpwuid($<)) == 0 && keys %{ $config->{"mapping"} } == 1) {
		e("[mapping] block cannot be empty if running as root\n");
	}

    # Warn if no allow/denyaccess blocks are present in interface blocks
    if (exists($config->{"arex/ws/jobs"}) && !(exists($config->{"arex/ws/jobs"}{"allowaccess"}) || exists($config->{"arex/ws/jobs"}{"denyaccess"}))) {
        w("No allowaccess or denyaccess defined in [arex/ws/jobs]. Interface will be open for all mapped users\n");
    }
    if (exists($config->{"gridftpd/jobs"}) && !(exists($config->{"gridftpd/jobs"}{"allowaccess"}) || exists($config->{"gridftpd/jobs"}{"denyaccess"}))) {
        w("No allowaccess or denyaccess defined in [gridftpd/jobs]. Interface will be open for all mapped users\n");
    }
}

=item check of applications

Some applications are required for the server to be functional.

=cut

sub check_applications() {
    # placeholder for binary files check, e.g. previously
    #unless ( -x "/usr/bin/time" ) {
    #    e("Could not find GNU time utility (or a substitute) at /usr/bin/time.");
    #}
    return;
}


=item check of libraries

uses ldd to check if all libraries are installed

=cut

sub check_libraries() {

	my $prob=0;
	
	if (! defined($globusloc)) {
		v("check_libraries: interpreting undefined GLOBUS_LOCATION as /usr\n");
		$globusloc="/usr";
	}

	unless ( -e $globusloc) {
		e("Cannot find Globus at $globusloc: no such file or directory\n");
		$prob++;
	}

	if (defined($arcloc)) {
		unless (-e $arcloc) {
			e("Cannot find ARC at $arcloc: no such file or directory\n");

			if (defined $ENV{"ARC_LOCATION"}) {
				w("The location was retrieved from the environment variable 'ARC_LOCATION'. Maybe this needs an update.");
			}
			$prob++;
		}
	}

	return if ($prob);

	my @globmes = ( $arcloc . "/bin/arc*",
			$arcloc . "/libexec/arc/gm*",
			$arcloc . "/sbin/arc*",
			$arcloc . "/sbin/grid*",
	);

	push(@globmes, $arcloc . "/lib/*") if "/usr" ne "$arcloc"; # takes too long, little gain expected

	my @to_check;
	foreach my $d (@globmes) {
		@to_check = ( @to_check , glob $d );
	}

	if ($verbose) {
		print "Checking the following files for their dependencies:\n";
		print join("\n",@to_check);
		print "\n";
	}

	my %missing;
	foreach my $file ( @to_check ) {
		next unless -f $file;
		next if $file =~ m/\.a$/;
		next if $file =~ m/\.la$/;

		my $command = "LC_ALL=C ";
		$command .= "LD_LIBRARY_PATH=";
		$command .= "$arcloc/lib:" if "/usr" ne "$arcloc" and "/usr/" ne "$arcloc";
		$command .= "\$LD_LIBRARY_PATH ";
		$command .= "ldd $file 2>/dev/null |";

		my %libs;

		if (open LDD, $command) {
			while (<LDD>) {
				chomp;
				my $lib = $_;

				if ($lib =~ m/^\s*([^\s]+)\.so\.([^\s]+)\s*=>/) {
					my $a=$1;
					my $b=$2;
					my $index=$a;
					$index =~ s/_(gcc)(16|32|64|128|256)(dbg)?(pthr)?\././;
					if (defined $libs{$index}) {
						e("$file: uses multiple versions of lib " .
							"$a: ".$libs{$index}." and $b. This might not work\n");
					} else {
						$libs{$index} = $b;
					}
				}

				next unless /not found/;

				$lib =~ m/^\s*([^\s]+)\s*=>/;
				my $missing = $1;

				unless (defined $missing{$missing}) {
					$missing{$missing} = 1;
					e("$file: needs $missing. Not found.\n");
				}

			}
			close LDD;
		} else {
			w("Cannot check used libraries of $file.\n");
		}
	}
}

sub check_certificates() {

	# check if CAdir is present

	if (!defined($CApath)) {
		$CApath="/etc/grid-security/certificates";
	}
	if ( ! -d "$CApath") {
		e("CA directory does not exist at '$CApath");
		return;
	}
	
    # Find openssl command if not set in options
    unless (defined($opensslpath)) {
        foreach my $p (split /:/, $ENV{PATH}) {
            if (-x "$p/openssl") {
                $opensslpath = "$p/openssl";
                last;
            }
        }
    }

	unless (defined($opensslpath) and "" ne "$opensslpath") {
		w("openssl application not in path and not specified.\n");
		return;
	}

	unless ( -x "$opensslpath") {
		e("Cannot execute openssl application at '$opensslpath'");
	}

	# check of host certificate

	if (!defined($hostcert)) {
		$hostcert="/etc/grid-security/hostcert.pem";
	}
	if ( -f $hostcert) {
		if (system("$opensslpath verify -CApath $CApath $hostcert | grep 'OK' >/dev/null 2>&1")) {
			e("Verification of host cert at $hostcert with $opensslpath failed.\n");
		}
		if (!system("$opensslpath verify -CApath $CApath $hostcert | grep 'expired'")) {
			e("Host certificate $hostcert has expired.\n");
		}
	}
	else {
		w("Not verifying host cert which is not present at $hostcert (should already be reported).\n");
	}

}

# Add this warning when A-REX is enforcing new validator
# w("*** This tool is DEPRECATED and will be REMOVED in a future ARC release. Please use 'arcctl config verify' instead. ***\n");
timecheck($timeserver, 60, 0.01);
confcheck($conffile);
check_applications();
check_libraries();
check_certificates();

if (!$quiet) {
    if (0 == $errors) {
        p("Found no apparent failures.\n");
    } else {
        p(sprintf("Found %d failure%s.\n", $errors, ($errors > 1) ? "s" : ""));
    }
    if ($warnings) {
        p(sprintf("Found %d non-critical issue%s%s.\n",
            $warnings, ($warnings > 1) ? "s" : "",
            ($skip_warnings?" (not shown)":"")));
    }
}

exit $errors;

=back

=head1 SEE ALSO

http://www.nordugrid.org and our mailing lists.

=cut


# EOF
