diff options
Diffstat (limited to 'pki/base/setup/pkiremove')
-rwxr-xr-x | pki/base/setup/pkiremove | 419 |
1 files changed, 419 insertions, 0 deletions
diff --git a/pki/base/setup/pkiremove b/pki/base/setup/pkiremove new file mode 100755 index 000000000..6ec3752b5 --- /dev/null +++ b/pki/base/setup/pkiremove @@ -0,0 +1,419 @@ +#!/usr/bin/perl +# +# --- BEGIN COPYRIGHT BLOCK --- +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Copyright (C) 2007 Red Hat, Inc. +# All rights reserved. +# --- END COPYRIGHT BLOCK --- +# + +############################################################## +# This script is used to remove an existing PKI instance. +# +# To execute: +# +# ./pkiremove -pki_instance_root=<pki_instance_root> # Instance root +# # directory destination +# +# -pki_instance_name=<pki_instance_id> # Unique PKI subsystem +# # instance name +# # (e. g. - pki-pki1) +# +# [-force] # Don't ask any +# # questions +# +############################################################## + + +############################################################## +# Perl Version +############################################################## + +my $MINIMUM_PERL_VERSION = "5.006001"; + +my $perl_version_error_message = "ERROR: Using Perl version $] ...\n" + . " Must use Perl version " + . "$MINIMUM_PERL_VERSION or later to " + . "run this script!\n"; + +die "$perl_version_error_message" if $] < $MINIMUM_PERL_VERSION; + + +############################################################## +# Execution Check +############################################################## + +# Check to insure that this script's original +# invocation directory has not been deleted! +my $cwd = `/bin/pwd`; +chomp $cwd; +if( "$cwd" eq "" ) { + print( STDERR "Cannot invoke '$0' from non-existent directory!\n" ); + print( STDOUT "\n" ); + exit 255; +} + + +############################################################## +# Environment Variables +############################################################## + +# untaint called subroutines +if( ( $^O ne 'Windows_NT' ) && ( $^O ne 'MSWin32' ) ) { + $> = $<; # set effective user ID to real UID + $) = $(; # set effective group ID to real GID + $ENV{ 'PATH' } = '/bin:/usr/bin'; + $ENV{ 'ENV' } = '' if $ENV{ 'ENV' } ne ''; +} + + +############################################################## +# Command-Line Variables +############################################################## + +my $ARGS = ( $#ARGV + 1 ); + + +############################################################## +# Shared Common Perl Data and Subroutines +############################################################## + +# Compute "flavor" of Operating System +my $pki_flavor = ""; +if( $^O eq "linux" ) { + $pki_flavor = `pkiflavor`; +} elsif( $^O eq "solaris" ) { + $pki_flavor = `pkiflavor`; +} else { + print( STDERR + "ERROR: Unsupported platform '$^O'!\n" ); + print( STDOUT "\n" ); + exit 255; +} + +$pki_flavor =~ s/\s+$//g; + +# Establish path to scripts +my $common_path = "/usr/share/pki/scripts"; + +if( ! -d "$common_path" ) { + print( STDERR + "ERROR: The path '$common_path' does not exist!\n" + . " Unable to load shared Common Perl Data " + . "and Subroutines!\n" ); + print( STDOUT "\n" ); + exit 255; +} + +if( ! -e "$common_path/pkicommon" ) { + print( STDERR + "ERROR: The file '$common_path/pkicommon' does not exist!\n" + . " Unable to load shared Common Perl Data " + . "and Subroutines!\n" ); + print( STDOUT "\n" ); + exit 255; +} + +eval( "use lib '" . $common_path . "'" ); +require( 'pkicommon' ); + + +############################################################## +# Local Constants +############################################################## + +my $saved_cleanup_file_name = ".cleanup.dat"; +my $saved_file_marker = "[files]"; +my $saved_directory_marker = "[directories]"; + + +############################################################## +# Local Data Structures +############################################################## + + +############################################################## +# Local Variables +############################################################## + +my $pki_instance_root = ""; +my $pki_instance_name = ""; +my $force = 0; + +my $pki_instance_path = ""; + + +############################################################## +# Platform-Dependent Data Initialization +############################################################## + + +############################################################## +# Local Data Initialization +############################################################## + + +############################################################## +# PKI Instance Removal Subroutines +############################################################## + +# no args +# no return value +sub usage() +{ + print( STDOUT + "Usage: pkiremove -pki_instance_root=<pki_instance_root> " + . "# Instance root\n" + . " " + . "# directory\n" + . " " + . "# destination\n\n" + . " -pki_instance_name=<pki_instance_id> " + . "# Unique PKI\n" + . " " + . "# subsystem\n" + . " " + . "# instance name\n" + . " " + . "# (e. g. - pki-pki1)\n\n" + . " [-force] " + . "# Don't ask\n" + . " " + . "# any questions\n\n" ); + + print( STDOUT + "Example: pkiremove -pki_instance_root=/var/lib " + . "-pki_instance_name=$pki_flavor-ca1\n\n" ); + + print( STDOUT + "IMPORTANT: Must be run as root!\n\n" ); + + return; +} + + +# no args +# return 1 - success, or +# return 0 - failure +sub remove_instance() +{ + my $command = ""; + + print( STDOUT + "PKI instance Deletion Utility " + . "cleaning up instance ...\n\n" ); + + my $result = 0; + my $cleanup = new FileHandle; + my $source_file_path = $pki_instance_path + . "/" . $saved_cleanup_file_name; + my @files; + my @directories; + my $pki_start_stop_script_instance_file_path = ""; + my $confirm = "Y"; + +ASK_AGAIN: + if( !$force ) { + $confirm = prompt( "You have elected to remove the instance " + . "installed in " + . "$pki_instance_path.\n" + . "Are you sure (Y/N)? " ); + } + + if( $confirm eq "N" || $confirm eq "n" ) { + return 1; + } elsif( $confirm ne "Y" && $confirm ne "y" ) { + goto ASK_AGAIN; + } + + if( !file_exists( "$source_file_path" ) ) { + print( STDERR + "ERROR: Can't remove instance, " + . "cleanup file does not exist!\n" ); + return $result; + } + + $cleanup->open( "<$source_file_path" ) or die "Could not open file!\n"; + + my $file_mode = "file"; + my @file_split; + + while( <$cleanup> ) + { + my $line = $_; + chomp( $line ); + + if( $line eq $saved_file_marker ) { + $file_mode = "file"; + next; + } + + if( $line eq $saved_directory_marker ) { + $file_mode = "directory"; + next; + } + + if( $file_mode eq "file" ) { + push( @files, $line ); + + @file_split = split( '/', $line ); + my $last = @file_split; + + if( $file_split[$last -1] eq $pki_instance_name ) { + $pki_start_stop_script_instance_file_path = $line; + } + } + + if( $file_mode eq "directory" ) { + push( @directories, $line ); + } + } + + $cleanup->close(); + + if( $pki_start_stop_script_instance_file_path eq "" ) { + print( STDERR + "ERROR: Can't locate start script of " + . "instance to be cleaned up!\n" ); + return $result; + } + + $command = "$pki_start_stop_script_instance_file_path stop"; + + system( "$command" ); + + my $size = @directories; + + print( STDOUT "\n" ); + + if( $size ) { + my $i = 0; + for( $i = 0; $i < $size; $i ++ ) { + print( STDOUT + "Removing dir $directories[$i]\n" ); + remove_directory( $directories[$i] ); + } + } + + $size = @files; + + if( $size ) { + my $i = 0; + for( $i = 0; $i < $size; $i++ ) { + print( STDOUT + "Removing file $files[$i]\n" ); + remove_file( $files[$i] ); + } + } + + print( STDOUT "\n" ); + + $result = 1; + return $result; +} + + +############################################################## +# Main Program +############################################################## + +# no args +# return 1 - success, or +# return 0 - failure +sub main() +{ + chdir( "/tmp" ); + + my $result = 0; + + print( STDOUT + "PKI instance Deletion Utility ...\n\n" ); + + # On Linux/UNIX, insure that this script is being run as "root". + $result = check_for_root_UID(); + if( !$result ) { + usage(); + exit 255; + } + + # Check for a valid number of command-line arguments. + if( $ARGS < 2 ) { + print( STDERR + "$0: Insufficient arguments!\n\n" ); + usage(); + exit 255; + } + + # Parse command-line arguments. + $result = GetOptions( "pki_instance_root=s" => \$pki_instance_root, + "pki_instance_name=s" => \$pki_instance_name, + "force" => \$force ); + + # Always disallow root to be the pki_instance_root. + if( $pki_instance_root eq "/" ) { + print( STDERR + "$0: Don't even think about making root " + . "the pki_instance_root!\n\n" ); + usage(); + exit 255; + } + + # Remove all trailing directory separators ('/') + $pki_instance_root =~ s/\/+$//; + + # Check for valid content of command-line arguments. + if( $pki_instance_root eq "" ) { + print( STDERR + "$0: Must have value for -pki_instance_root!\n\n" ); + usage(); + exit 255; + } + + if( $pki_instance_name eq "" ) { + print( STDERR + "$0: The instance ID of the PKI instance " + . "to be removed is required!\n\n" ); + usage(); + exit 255; + } + + $pki_instance_path = $pki_instance_root . "/" . $pki_instance_name; + + if( !directory_exists( "$pki_instance_path" ) ) { + print( STDERR + "$0: Target directory $pki_instance_path " + . "is not a legal directory.\n\n" ); + usage(); + exit 255; + } + + # Remove the specified instance + $result = remove_instance(); + if( $result != 1 ) { + exit 255; + } + + return $result; +} + + +############################################################## +# PKI Instance Removal +############################################################## + +main(); + +exit 0; + |