#!/usr/bin/perl -w

use strict;
use warnings;

my $vulnerable_count = 0;
my $print_progress = 1;
my $toggle_wrong_xbit = 1;

sub check_file {
    my $f = shift;

    if (-d $f) {
	opendir(DIRH, $f) || die("Couldn't open directory [$f]: $!");
	my @dirfiles = readdir(DIRH);
	closedir(DIRH);
	$f = '' if ($f eq '/');
	foreach (@dirfiles) {
	    next if (($_ eq ".") || ($_ eq ".."));
	    check_file($f . '/' . $_);
	}
        return;
    }

    return if (not -f $f);
    return if (not -x $f);

    return if ($f =~ /,v\Z/);  # stupid cvsroot.

    if ($toggle_wrong_xbit) {
	`chmod a-x "$f"`, return if ($f =~ /.mp3\Z/i);
	`chmod a-x "$f"`, return if ($f =~ /.mpg\Z/i);
	`chmod a-x "$f"`, return if ($f =~ /.pdf\Z/i);
	`chmod a-x "$f"`, return if ($f =~ /.jpg\Z/i);
    }

    print "checking $f ...\n" if $print_progress;

    if (`strings "$f" | grep 'invalid block type' 2>/dev/null`) {
	print STDERR "VULNERABLE: $f\n";
        $vulnerable_count++;
    }
}


# mainline.

print "WARNING: This is going to take a good goddamn long time.\n\n";

check_file("/");
exit(0);

# end of zlib-locate.pl ...

