1package Bastille::API::Miscellaneous;
2use strict;
3
4use File::Path;
5use Bastille::API;
6use Bastille::API::HPSpecific;
7use Bastille::API::FileContent;
8
9require Exporter;
10our @ISA = qw(Exporter);
11our @EXPORT_OK = qw(
12PrepareToRun
13B_is_package_installed
14);
15our @EXPORT = @EXPORT_OK;
16
17
18###########################################################################
19#
20# PrepareToRun sets up Bastille to run.  It checks the ARGV array for
21# special options and runs ConfigureForDistro to set necessary file
22# locations and other global variables.
23#
24###########################################################################
25
26sub PrepareToRun {
27
28    # Make sure we're root!
29    if ( $> != 0 ) {
30	&B_log("ERROR","Bastille must run as root!\n");
31        exit(1);
32    }
33
34
35    # Make any directories that don't exist...
36    foreach my $dir (keys %GLOBAL_BDIR) {
37        my $BdirPath = $GLOBAL_BDIR{$dir};
38        if ( $BdirPath =~ /^\s*\// ) { #Don't make relative directories
39            mkpath ($BdirPath,0,0700);
40        }
41    }
42
43    if(&GetDistro =~ "^HP-UX") {
44	&B_check_system;
45    }
46
47    &B_log("ACTION","\n########################################################\n" .
48	       "#  Begin Bastille Run                                  #\n" .
49	       "########################################################\n\n");
50
51    #read sum file if it exists.
52    &B_read_sums;
53
54
55# No longer necessary as flags are no longer in sum file, and sums are
56# are now checked "real time"
57
58    # check the integrity of the files listed
59#    for my $file (sort keys %GLOBAL_SUM) {
60#	&B_check_sum($file);
61#    }
62    # write out the newly flagged sums
63#    &B_write_sums;
64
65
66}
67
68
69
70###########################################################################
71# &B_is_package_installed($package);
72#
73# This function checks for the existence of the package named.
74#
75# TODO: Allow $package to be an expression.
76# TODO: Allow optional $version, $release, $epoch arguments so we can
77#       make sure that the given package is at least as recent as some
78#       given version number.
79#
80# scalar return values:
81# 0:    $package is not installed
82# 1:    $package is installed
83###########################################################################
84
85sub B_is_package_installed($) {
86    no strict;
87    my $package = $_[0];
88# Create a "global" variable with values scoped to this function
89# We do this to avoid having to repeatedly swlist/rpm
90# when we run B_is_package_installed
91local %INSTALLED_PACKAGE_LIST;
92
93    my $distro = &GetDistro;
94    if ($distro =~ /^HP-UX/) {
95        if (&checkProcsForService('swagent','ignore_warning') == SECURE_CANT_CHANGE()) {
96            &B_log("WARNING","Software Distributor Agent(swagent) is not running.  Can not tell ".
97                   "if package: $package is installed or not.  Bastille will assume not.  ".
98                   "If the package is actually installed, Bastille may report or configure incorrectly.".
99                   "To use Bastille-results as-is, please check to ensure $package is not installed, ".
100                   "or re-run with the swagent running to get correct results.");
101            return 0; #FALSE
102        }
103	my $swlist=&getGlobal('BIN','swlist');
104        if (%INSTALLED_PACKAGE_LIST == () ) { # re-use prior results
105          if (open(SWLIST, "$swlist -a state -l fileset |")) {
106            while (my $line = <SWLIST>){
107              if ($line =~ /^ {2}\S+\.(\S+)\s*(\w+)/) {
108                $INSTALLED_PACKAGE_LIST{$1} = $2;
109              }
110            }
111          close SWLIST;
112          } else {
113            &B_log("ERROR","B_is_package_installed was unable to run the swlist command: $swlist,\n");
114            return FALSE;
115          }
116        }
117        # Now find the entry
118        if ($INSTALLED_PACKAGE_LIST{$package} == 'configured') {
119            return TRUE;
120        } else {
121            return FALSE;
122        }
123    } #End HP-UX Section
124    # This routine only works on RPM-based distros: Red Hat, Fedora, Mandrake and SuSE
125    elsif ( ($distro !~ /^RH/) and ($distro !~ /^MN/) and($distro !~ /^SE/) ) {
126        return 0;
127    } else { #This is a RPM-based distro
128        # Run an rpm command -- librpm is extremely messy, dynamic and not
129        # so much a perl thing.  It's actually barely a C/C++ thing...
130        if (open RPM,"rpm -q $package") {
131            # We should get only one line back, but let's parse a few
132            # just in case.
133            my @lines = <RPM>;
134            close RPM;
135            #
136            # This is what we're trying to parse:
137            # $ rpm -q jay
138            # package jay is not installed
139            # $ rpm -q bash
140            # bash-2.05b-305.1
141            #
142
143            foreach $line (@lines) {
144                if ($line =~ /^package\s$package\sis\snot\sinstalled/) {
145            	return 0;
146                }
147                elsif ($line =~ /^$package\-/) {
148            	return 1;
149                }
150            }
151
152            # If we've read every line without finding one of these, then
153            # our parsing is broken
154            &B_log("ERROR","B_is_package_installed was unable to find a definitive RPM present or not present line.\n");
155            return 0;
156        } else {
157            &B_log("ERROR","B_is_package_installed was unable to run the RPM command,\n");
158            return 0;
159        }
160    }
161}
162
163
164
1651;
166
167