summaryrefslogtreecommitdiffstats
path: root/pki/base/setup/pkiremove
blob: 6ec3752b5e37d40bfc28233fd4af7bdc1d889a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
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;