# ------------------------------------------------------------------------------------ # Author: Dean Stringer # Description/Purpose: # greps the apache config file for various things like servername or directory path # Revisions: # 29/Nov/2005 - DeanS # [2] added support for /etc/groups 'find' and new modes 'group' and 'groups' # [1] added support for smb.conf 'find' and new modes 'share' and 'shares' # ------------------------------------------------------------------------------------ use strict; my $httpdConfFile = '/path/to/httpd.conf'; my $smbConfFile = '/path/to/smb.conf'; # [1] my $groupsConfFile = '/etc/group'; # [2] my $confFile = ''; my ($mode, $pattern, $outputMode) = @ARGV; if ($mode eq 'cat') { $mode = 'cat'; } if ($mode =~ /cat|hosts|host|dirs|dir|redir/) { $confFile = $httpdConfFile; } elsif ($mode =~ /shares|share/) { $confFile = $smbConfFile; } elsif ($mode =~ /groups|group/) { $confFile = $groupsConfFile; } elsif ($mode =~ /help/) { dieSyntax(''); } elsif ($mode !~ /find/) { dieSyntax('invalid mode'); } # modes that require a search pattern if (($mode =~ /dir$|find|host$|group$|share$/) && ($pattern eq '')) { dieSyntax('missing search pattern'); } if ($mode =~ /find/) { # need to process both files in a 'find' print "---find results from httpd.conf\n"; processCMD($httpdConfFile, 'find'); print "---find results from smb.conf\n"; processCMD($smbConfFile, 'find'); } else { processCMD($confFile, $mode); } exit; sub processCMD { my ($confFile, $mode) = @_; open (INFILE, '<' . $confFile); my ($showingDir, $dirCount, $showingHost, $matchingHost, $hostCount, $shareCount, $showingShare, $groupsCount, $groupsMax); $showingDir=0; $dirCount=0; $showingHost=0; $matchingHost=0; $hostCount=0; $shareCount=0; $showingShare = 0; $groupsMax = 0; my @hostCache = (); # strore virtual host data in an array until we know if servername or alias match while (my $line = ) { if (($mode eq 'find') && ($line =~ /$pattern/i)) { print $line; } if ($mode eq 'cat') { print $line if $line =~ /\S/; } if (($mode eq 'redir') && ($line =~ /^\s*Redirect(Match|Permanent|)\s+.*$pattern/i)) { print $line; } if (($mode eq 'hosts') && ($line =~ /^\s*Server(Name|Alias)\s+\.*$pattern/)) { print $line; $hostCount++; } if ( ($mode eq 'groups') && # [2] ( ($line =~ /^([^:]+)$pattern:/i) || ($line =~ /$pattern/i) ) ) { my ($groupName, $pwd, $groupID) = ($line =~ /^([^:]+):([^:]+):([^:]+)/); print "$groupName ($groupID)\n"; if ($groupName !~ /^#/) { $groupsCount++; } if ($groupID > $groupsMax) { $groupsMax = $groupID }; } if ( ($mode eq 'group') && ($line =~ /^$pattern:/i) ) { # [2] print $line . "\n"; } if ($mode eq 'host') { if ($line =~ /^\s*\n\n"; } $matchingHost = 0; } if ($showingHost) { push @hostCache, $line if $line =~ /\S/; # dont bother with blank lines if ($line =~ /^\s*Server(Name|Alias)\s+$pattern$/) { $matchingHost = 1; } # found it !!! } } if (($mode eq 'dirs') && ($line =~ /^\s*/) { # must be an exact match $showingDir = 1; } if (($line =~ /^\s*<\/Directory/) && $showingDir) { $showingDir = 0; print $line . "\n"; } if ($showingDir) { print $line if $line =~ /\S/; # dont bother with blank lines } } if (($mode eq 'shares') && ($line =~ /^\s*\[.*$pattern.*\]/i)) { # [1] print $line; $shareCount++; } if ($mode eq 'share') { # [1] if ($line =~ /^\s*\[.*$pattern.*\]/i) { $showingShare = 1; } if (($line =~ /^\s*\[.*\]/) && $showingShare && !( $line =~ /$pattern/i) ) { $showingShare = 0; } if ($showingShare) { print $line if $line =~ /\S/; # dont bother with blank lines } } } if ($outputMode ne 'quiet') { if (($dirCount > 0) || ($mode eq 'dirs')) { print "\nFound $dirCount matching directories"; } if (($hostCount > 0) || ($mode eq 'hosts')) { print "\nFound $hostCount matching hosts"; } if (($shareCount > 0) || ($mode eq 'shares')) { print "\nFound $shareCount matching shares"; } if (($groupsCount > 0) || ($mode eq 'groups')) { print "\nFound $groupsCount matching active groups"; } } } sub dieSyntax { my $msg = shift; if ($msg) { print "Error. $msg\n"; } print "Syntax is... webconf Where mode and pattern options are... find - grep for hosts - list all ServerName and Alias's host - show config for given dirs - list all stanzas dirs - list all stanzas containing dir - show config for given groups - list all groups containing group - show members for given redir - list all redirect entries containing share - show config for given [Share] shares - list all samba [Shares] containing and is optionally quiet which supresses summary text "; exit; }