Article Listing 1 Listing 2 oct2005.tar

Listing 1 assert_attributes_1

# Listing 1: assert_attributes_1
#!/bin/ksh

function list_attributes
{
    if [[ -n $1 ]]
    then
        path=$1
        if (( $(file -h $path | grep -c directory) > 0 ))
        then
            ls -ld $path | awk '{print $1" "$3":"$4}'
        else
            ls -l $path | awk '{print $1" "$3":"$4}'
        fi
    fi
}

function list_files
{
    packages=$@
    
    if [[ -z $packages ]]
    then
        while read package
        do
            packages="$packages $package"
        done
    fi

    for package in $packages
    do
        grep " ${package}$" /var/sadm/install/contents | \
        cut -d' ' -f1,2,4,5,6
    done
}

function assert_attributes
{
    local file=$1
    local perms=$2
    local owner=$3
    local group=$4

    local attributes_old=$(list_attributes $file)
    chown ${owner}:${group} $file
    chmod $perms $file
    local attributes_new=$(list_attributes $file)

    if [[ "$attributes_old" != "$attributes_new" ]]
    then
        sudo -u monitor /opt/monitor/bin/create_alert_file -p low \
        -c $(basename $0) -s "$(uname -n):$file has changed!" << EOF
    Old: $attributes_old
    New: $attributes_new
EOF
        if [[ TRUE = $verbose ]]
        then
            echo "$file - CHANGED!"
        fi
    else
        if [[ TRUE = $verbose ]]
        then
            echo "$file - OK"
        fi
    fi
}

#
# MAIN PROGRAM
#

verbose=FALSE

while getopts v optarg
do
    case $optarg in
    v)
        verbose=TRUE
        ;;
    esac
done

shift $(($OPTIND-1))

list_files $@ | \
while read file_equals type perms owner group
do
    file=${file_equals%=*}
    case $type in
    f|v)
        if [[ -f $file ]]
        then
            assert_attributes $file $perms $owner $group
        fi
        ;;
    d)
        if [[ -d $file ]]
        then
            assert_attributes $file $perms $owner $group
        fi
        ;;
    esac
done
# End Listing 1