#!/usr/bin/perl
#
# Copyright (c) 2008--2017 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.

use strict;
use Spacewalk::Setup;

sub setup_sudoers {
  my $opts = shift;
  my $answers = shift;

  my ($sudoers_content_orig, $sudoers_content);
  my $sudoers_path = '/etc/sudoers';

  local *FILE;
  if (-e $sudoers_path) {
    # If /etc/sudoers exists, let's modify that
    open FILE, $sudoers_path or die "Error reading [$sudoers_path]: $!\n";
  } else {
    return;
  }
  {
  local $/ = undef;
  $sudoers_content = <FILE>;
  $sudoers_content_orig = $sudoers_content if not defined $sudoers_content_orig;
  }
  close FILE;

  # The sudoers.clear file has regular expressions for clearing
  # old configuration
  if (open FILE, Spacewalk::Setup::SHARED_DIR . '/sudoers.clear') {
    while (defined(my $regexp = <FILE>)) {
      chomp $regexp;
      $sudoers_content =~ s!$regexp!!igm;
    }
    close FILE;
  }

  if ($sudoers_content ne $sudoers_content_orig) {
    open FILE, "> $sudoers_path" or die "Error writing [$sudoers_path]: $!\n";
    chmod 0440, $sudoers_path;
    print FILE $sudoers_content;
    close FILE;
  }

  return;
}

setup_sudoers();

=head1 NAME

spacewalk-setup-sudoers - utility to clear sudo configuration from previous version of
Spacewalk / Satellite

=head1 SYNOPSIS

B<spacewalk-setup-sudoers>

=head1 DESCRIPTION

B<spacewalk-setup-sudoers> is a utility to clear sudo configuration from previous version of
Spacewalk / Satellite. New configuration is properly put into b</etc/sudoers.d/spacewalk>.

Ordinarily, spacewalk-setup-sudoers is called by spacewalk-setup(1) during
initial Spacewalk / Satellite configuration or upgrade.

=head1 SEE ALSO

B<spacewalk-setup>(1) - Spacewalk setup program

=cut
