Subversion Repositories DevTools

Rev

Rev 5813 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/perl -w
# Copyright (c) VIX TECHNOLOGY (AUST) LTD
###############################################################################
#
#   Ensure that package directories in dpkg_archive are:
#       chmod 0775      - Not world writable
#       chown pkgadm  - Owned by pkgadm
#
#   Script should be run as root - so that it can fix file attributes
#

use File::stat;

$DIR="/export/devl/dpkg_archive";
chdir($DIR) or die "$0: Could not change to \"$DIR\": $!\n";

($name, $passwd, $pkgadm, $buildgid) = getpwnam ('pkgadm');
$name = '';
$passwd = '';

die "$0: Could not determine uid of builadadm\n"
    unless ($pkgadm && $buildgid);

die "$0: Not run as root\n"
        if ( $> );

#   Proccess all directories in dpkg_archive
#   Are only expecting directories at the top level
#   
opendir(DPKGARCHIVE, ".") or die "$0: Could not open \"$DIR\": $!\n";
while (defined($pkg=readdir(DPKGARCHIVE))) {
    next if ( $pkg eq ".." );
    next if ( $pkg eq "." );

    # Ignore special (symlinks) to files
    next if ( $pkg eq "dpkg_archive_list.txt" );
    next if ( $pkg eq "dpkg_archive_pkg.txt" );

    $sb=lstat($pkg);
    $rwx=$sb->mode & 00777;
    $uid=$sb->uid;

    # Delete empty subdirs (packages)
    if ( -d $pkg && isEmptyDir($pkg) == 1 )
    {
        system("/usr/bin/logger", "-p", "local0.notice", "-t", $0,
               sprintf("%s/%s: is empty", $DIR, $pkg, $rwx));
        printf STDERR ("%s: %s/%s: is empty\n", $0, $DIR, $pkg, $rwx);
        unless (rmdir($pkg)) {
            printf STDERR "%s: Could not delete empty directiry %s/%s: %s\n", $0, $rwx, $DIR, $pkg, $!;
        }
        next;
    }

    # Check that (directory) is NOT world writable, but is writable by user/group
    #   Change it only if it's wrong
    if ($rwx != 00775) {
        system("/usr/bin/logger", "-p", "local0.notice", "-t", $0,
               sprintf("%s/%s: was chmod %04o", $DIR, $pkg, $rwx));
        printf STDERR ("%s: %s/%s: was chmod %04o\n", $0, $DIR, $pkg, $rwx);
        $rwx=($sb->mode & 07000) | 00775;
        unless (chmod($rwx, $pkg) == 1) {
            printf STDERR "%s: Could not chmod %04o %s/%s: %s\n", $0, $rwx, $DIR, $pkg, $!;
        }
    }

    # Check that (directory) is owned by buildamd
    #   Chnage if wrong
    if ($uid != $pkgadm ) {
        system("/usr/bin/logger", "-p", "local0.notice", "-t", $0,
               sprintf("%s/%s: was uid %d", $DIR, $pkg, $uid));
        printf STDERR ("%s: %s/%s: was uid %d\n", $0, $DIR, $pkg, $uid);
        unless (chown($pkgadm, $buildgid, $pkg) == 1) {
            printf STDERR "%s: Could not chown pkgadm (%d) %s/%s: %s\n", $0,$uid, $DIR, $pkg, $!;
        }
    }
}
closedir(DPKGARCHIVE);

#-------------------------------------------------------------------------------
# Function        : isEmptyDir 
#
# Description     : Test for empty directiry
#
# Inputs          : $dir        - Path to directory
#
# Returns         : 0           - Contains stuff
#                   1           - Does not contain stuff
#                  -1           - Does not exist
#
sub isEmptyDir
{
    my ($dir) = @_;
    my $rv = -1;

    if (opendir (my $dh, $dir ))
    {
        $rv = 1;
        while (readdir $dh)
        {
            next if ($_ eq '.');
            next if ($_ eq '..');
            $rv = 0;
            last;
        }
        closedir $dh;
    }
    return $rv;
}