#!/usr/bin/perl
###############################################################################
## @file   harden
# @brief   Manages CS1000 Linux Base hardening items
# @details This utility provides the command line interface in order to manage
#          the Enhanced Hardening options. It uses the Hardening Manager in
#          order to get information about accessible modules and passes control
#          to the requested module to perform the corresponding action.
#
#          High level privileges are needed to have the ability to manage the
#          Enhanced Hardening options.
#
#          It supports the following usage:
#          \li harden
#          \li harden \em module [ \em action [ \em extra \em option ]]
#          \li harden \em --modules
#          \li harden \em --help
# @author Alexander Kolotov
# @author Petr Kascheev
# @version 1.00 - Initial version
# @version 1.01 - Two colons were removed before shared functions and variables.
# @version 1.02 - check for user has been changed for group.
# @version 1.03 - fixed a bug with "--modules" printing.
# @version 1.04 - added signals_ignore() in order to ignore SIGTERM, SIGHUP etc.

=pod

=head1 NAME

=over 4

=item B<harden> - command to manage CS1000 hardening items.

=back

=head1 SYNOPSIS

=over 10

=item B<harden> I<module> I<action> 

=item B<harden> I<--help>  - prints a help message and exit

=item B<harden> I<help> - prints a help page for every available hardening module

=item B<harden> I<status> - prints statuses for every available hardening module.

=item B<harden> I<--modules> - prints list of the avaliable modules

=back

=head1 DESCRIPTION

B<module> allows managing the CS1000 hardening items. All available modules can
be identified by B<harden> I<--module>. All available B<actions> can be identified
by using the command B<harden> I<hardening_option> I<help>.

=head1 OPTIONS

=over 4

=item B<status>

Outputs statuses for every available hardening module.

=item B<help>

prints a help page for every available hardening module).

=back

=head1 EXAMPLES

=over 4

=item B<$ harden --modules>

 The current available Hardening modules:
   audit
   banners
   basic
   coredumps
   ftp
   nettools
   passwd_days
   ssh_filter
   telnet
   tftp

=back

=head1 CAVEATS

B<harden> can be run only under Nortel authorized accounts.

=cut

use lib "/opt/nortel/base/lib";

use strict;
use warnings;

use common_variables;
use common_functions;
use baselog;
use plugin_mgr;
use hardening::hardening_common;
use base_semctl;

## @skipline
sub main();
## @skipline
sub print_list_of_modules();
## @skipline
sub print_help_of_each_module();
## @skipline
sub print_error_message($);

exit(main());

###############################################################################
## @fn sub main()
# @brief   Main entry point
# @details This subroutines uses the Hardening Manager in order to perform an
#          action for some Enhanced Hardening module. The semapore $SEM_HARD is
#          used to guarantee that no hardening code is not executed at the same
#          time.
#
#          The subroutine has no parameters but the module, action and extra
#          parameters are taken from \@ARGV array.
# @return  OK - the module and actions are handled sucessfully
# @return  ERROR - Some error happens
sub main()
{
   if (warning_if_not_root_uid())
   {
      return $ERROR;
   }

   my $retval = $OK;

   #harden script should parse the following options:
   #--help - prints help page
   #--modules - prints the list of avaliable modules

   my @command_line_params = @ARGV;

   if (scalar @command_line_params)
   {
      signals_ignore();
      
      my $semmngr = base_semctl::new("HARDENING");
      if (not defined($semmngr))
      {
         my $error_descr = $!;
         baselog($LOG_ERROR, "Cannot create semaphore manager object ($error_descr).");
         return $ERROR;
      } ## end if (not defined($semmngr...
      if (not $semmngr->sem_take($SEM_HARD, IPC_NOWAIT))
      {
         baselog($LOG_ERROR, "Cannot take semaphore.");
         baselog($LOG_ERROR, "Hardening operation aborted.\n", $TRUE);
         undef $semmngr;
         return $ERROR;
      } ## end if (not $semmngr->sem_take...

      if ($command_line_params[0] eq '--modules')
      {
         if (not print_list_of_modules())
         {
            $retval = $ERROR;
         }
      } ## end if ($command_line_params...
      elsif ($command_line_params[0] eq '--help')
      {
         print_pod_usage();
      }
      elsif ($command_line_params[0] eq 'help')
      {
         if (not print_help_of_each_module())
         {
            $retval = $ERROR;
         }
      } ## end elsif ($command_line_params...
      else
      {
         my $harden_mgr = plugin_mgr->new($PATH_TO_ENHANCED_HARDENING_MODULES);
         if (not defined $harden_mgr)
         {
            baselog($LOG_ERROR, "Cannot create Hardening manager instance.");
            baselog($LOG_ERROR, "Hardening operation aborted.\n", $TRUE);
            $retval = $ERROR;
         } ## end if (not defined $harden_mgr...
         else
         {
            my $hash_ref = prepare_action_hash(@command_line_params);

            if (not $harden_mgr->do_action($hash_ref))
            {
               print_error_message($hash_ref);
               $retval = $ERROR;
            }

            undef $harden_mgr;
         } ## end else [ if (not defined $harden_mgr...
      } ## end else [ if ($command_line_params...

      if (not $semmngr->sem_give($SEM_HARD))
      {
         baselog($LOG_ERROR, "Cannot release semaphores.");
         $retval = $ERROR;
      }
      undef $semmngr;

      signals_default();
   } ## end if (scalar @command_line_params...
   else
   {

      #print usage page if no params specified
      print_pod_usage();
   } ## end else [ if (scalar @command_line_params...

   return $retval;
} ## end sub main()

###############################################################################
## @fn sub print_list_of_modules()
# @brief   Prints list of available hardening modules
# @details This subroutine uses Hardening manager to get the list of all
#          accessible modules. It outputs the list on the terminal.
# @return  TRUE - no error
# @return  FALSE - error happens
sub print_list_of_modules()
{
   my $retval = $FALSE;

   my $harden_mgr = plugin_mgr->new($PATH_TO_ENHANCED_HARDENING_MODULES);
   if (not defined $harden_mgr)
   {
      baselog($LOG_ERROR, "Cannot create Hardening manager instance.");
      baselog($LOG_ERROR, "Hardening operation aborted.\n", $TRUE);
   }
   else
   {
      my @modules = $harden_mgr->get_modules();
      if ((scalar @modules == 0) or (not defined $modules[0]))
      {
         baselog($LOG_ERROR, "Cannot get the list of the avaliable modules.\n", $TRUE);
      }
      else
      {
         print "The current available Hardening modules:\n";
         foreach my $module (@modules)
         {
            print "  $module\n";
         }
         my $retval = $TRUE;
      } ## end else [ if ((scalar @modules ==...

      undef $harden_mgr;
   } ## end else [ if (not defined $harden_mgr...

   return $retval;
} ## end sub print_list_of_modules()

###############################################################################
## @fn sub print_help_of_each_module
# @brief   print help of each module
# @details Prints help of each avaliable hardening module
# @return  TRUE - no error
# @return  FALSE - error happens
# @todo    Remove this subroutine, so as a special plugin must be developed
sub print_help_of_each_module()
{

   #FIXME it is necessary to add help.pm module usage instead
   do_enhanced_hardening_action('help');

   return $TRUE;
} ## end sub print_help_of_each_module()

###############################################################################
## @fn sub print_error_message($hash_ref)
# @brief   Prints error message
# @details This subroutine outputs an error message provided by the Hardening
#          Manager after handling an action.
# @param   $hash_ref - hash reference to data returned by the Hardening Manager
# @return  always TRUE
sub print_error_message($)
{
   my $hash_ref = shift @_;

   print $hash_ref->{err_msg};
   print "\n";
   return $TRUE;
} ## end sub print_error_message($)
