You are on page 1of 9

#!

/usr/bin/sh
#
# @(#) $Revision: 1.3 $
#
#######################################################################
# script:
lp_sanity.sh
# function:
verifies PrinterMgmt, HPNP, HPNPL products
#
checks permissions and ownership in /var/spool/lp,
#
/etc/lp and /var/adm/lp directories
#
checks for possible printer status file corruption
# date created: Mar 13 2002
# last modified: Aug 21 2002
#
# This script is not guaranteed by HP. Use at your own risk.
#
# Exit codes:
# exit 0
success
# exit 2
lp_permissions file cannot be created
# exit 5
not logged in as user root
# exit 6
lpstat command fails
#######################################################################
PATH=/sbin:/usr/sbin:/usr/bin
integer
integer
integer
integer
integer
integer
integer

verify=0
config_check=0
status_file_check=0
root_id
lpstat_return=0
hpnp_installed=1
hpnpl_installed=1

permissions=/tmp/lp_permissions
log=/tmp/lp_sanity.log
lpstat_v=/tmp/lpstat_v
lpstat_p=/tmp/lpstat_p
lpstat_a=/tmp/lpstat_a
orig_IFS="$IFS"
IFS=:
# remove any previous log file
rm $log 2>/dev/null
clear
print -n "
LP Spooler configuration check script
=====================================
This script checks your LP Spooler configuration in three steps:
Step 1
Runs swverify to check the existence and integrity of the
LP Spooler files included in the PrinterMgmt product as well
as HPNP (jetadmin) and HPNPL (hp printer installer) if installed.
Step 2
Checks the ownership and permissions of the LP Spooler

configuration files under /var/spool/lp, /etc/lp and /var/adm/lp.


Step 3
Checks for possible printer status file corruption using
the lpstat command.
You will be asked to confirm before any of these steps is executed.
Please run the script in the current directory as user root.
"
root_id=$(id -u)
if (( $root_id != 0 ))
then
user_name=$(id -un)
print "\nError: Please log in as user root to run this script."
print "You are currently logged in as user: $user_name."
exit 5
fi
print "\nChecking if JetAdmin(HPNP), Printer Installer(HPNPL) are installed..."
swlist -l product HPNP 2>/dev/null | grep -q HPNP
hpnp_installed=$?
swlist -l product HPNPL 2>/dev/null | grep -q HPNPL
hpnpl_installed=$?
while true
do
print "\nStep 1"
print -n "Would you like to run swverify for the printer products (y/n): "
read ANSWER
case $ANSWER in
y|Y|yes) print "Running swverify ...\n"
verify=1
break
;;
n|N|no)

print "Skipping swverify.\n"


verify=0
break

;;
q|Q|quit) print "Exiting LP Spooler configuration check script.\n"
exit 0
;;
*)

print "Invalid answer. please reply with y or n.\n"


continue

;;
esac
done
if (( $verify == 1 ))
then
if (( $hpnp_installed == 0 ))
then
swverify -v -x autoselect_dependencies=false \

-x mount_all_filesystems=false HPNP 1>/dev/null


if (( $? == 0 ))
then
print "\nVerification of HPNP succeeded.\n"
else
print "\nVerification of HPNP failed."
print "See /var/adm/sw/swagent.log for more information.\n"
fi
fi
if (( $hpnpl_installed == 0 ))
then
swverify -v -x autoselect_dependencies=false \
-x mount_all_filesystems=false HPNPL 1>/dev/null
if (( $? == 0 ))
then
print "\nVerification of HPNPL succeeded.\n"
else
print "\nVerification of HPNPL failed."
print "See /var/adm/sw/swagent.log for more information.\n"
fi
fi
swverify -v -x autoselect_dependencies=false \
-x mount_all_filesystems=false PrinterMgmt 1>/dev/null
if (( $? == 0 ))
then
print "\nVerification of PrinterMgmt succeeded.\n"
else
print "\nVerification of PrinterMgmt failed."
print "See /var/adm/sw/swagent.log for more information.\n"
fi
fi
while true
do
print "Step 2"
print -n "Would you like to check printer configuration file ownership and
permissions in /var/spool/lp, /etc/lp and /var/adm/lp (y/n): "
read ANSWER
case $ANSWER in
y|Y|yes) print "Checking printer configuration files ...\n"
config_check=1
break
;;
n|N|no)

print "Skipping configuration file check.\n"


config_check=0
break

;;
q|Q|quit) print "Exiting LP Spooler configuration check script.\n"
exit 0
;;
*)
;;

print "Invalid answer. Please reply with y or n.\n"


continue

esac
done
if (( $config_check == 1 ))
#
# the script does not change any permissions or ownership on purpose
#
then
error=FALSE
# create /tmp/lp_permissions file
touch $permissions
if (( $? != 0 ))
then
print "Error: $permissions could not be created.\n"
exit 2
else
print "/etc:d:r-xr-xr-x:555:bin:bin
/etc/lp:d:rwxr-xr-x:755:lp:bin
/etc/lp/cinterface:d:rwxr-xr-x:755:lp:bin
/etc/lp/class:d:rwxr-xr-x:755:lp:bin
/etc/lp/info:d:rwxr-xr-x:755:lp:bin
/etc/lp/interface:d:rwxr-xr-x:755:lp:bin
/etc/lp/member:d:rwxr-xr-x:755:lp:bin
/etc/lp/sinterface:d:rwxr-xr-x:755:lp:bin
/etc/lp/cinterface:l:rwxr-xr-x:755:lp:lp
/etc/lp/class:l:rw-r--r--:644:lp:lp
/etc/lp/interface:l:rwxr-xr-x:755:lp:lp
/etc/lp/member:l:rw-r--r--:644:lp:lp
/etc/lp/sinterface:l:rwxr-xr-x:755:lp:lp
/tmp:d:rwxrwxrwx:777:bin:bin
/var:d:r-xr-xr-x:555:bin:bin
/var/tmp:d:rwxrwxrwx:777:bin:bin
/var/spool:d:r-xr-xr-x:555:bin:bin
/var/spool/lp:d:rwxr-xr-x:755:lp:bin
/var/spool/lp/default:f:rw-r--r--:644:lp:lp
/var/spool/lp/outputq:f:rw-r--r--:644:lp:lp
/var/spool/lp/pstatus:f:rw-r--r--:644:lp:bin
/var/spool/lp/qstatus:f:rw-r--r--:644:lp:bin
/var/spool/lp/receive:d:rwxr-xr-x:755:lp:bin
/var/spool/lp/request:d:rwxr-xr-x:755:lp:bin
/var/spool/lp/seqfile:f:rw-r--r--:644:lp:lp
/var/spool/lp/request:l:rwxr-xr-x:755:lp:lp
/var/adm:d:rwxr-xr-x:755:adm:adm
/var/adm/lp:d:rwxr-xr-x:755:lp:bin
/var/adm/lp/log:f:rw-r--r--:644:lp:lp" > $permissions
if (( $hpnp_installed == 0 ))
then
print "/etc/lp/interface/model.orig:d:rwxr-xr-x:755:lp:bin
/etc/lp/interface/model.orig:l:rwxr-xr-x:755:lp:bin
/opt/hpnp/tmp:d:rwxr-xr-x:755:lp:bin" >> $permissions
fi
if (( $hpnpl_installed == 0 ))
then
if (( $hpnp_installed == 1 ))
then

print "/etc/lp/interface/model.orig:d:rwxr-xr-x:755:lp:bin
/etc/lp/interface/model.orig:l:rwxr-xr-x:755:lp:bin" >> $permissions
fi
print "/opt/hpnpl/tmp:d:rwxr-xr-x:755:lp:bin" >> $permissions
fi
fi
cat $permissions | while read name type perm num_perm uid gid
do
case "$type" in
# if the entry is a directory or regular file
d|f) if [[ "$type" = "d" ]]
then
test -d $name
result=$?
elif [[ "$type" = "f" ]]
then
test -f $name
result=$?
fi
if (( $result == 0 ))
then
# read the current permissions, owner and group ID
current_perm=$(ll -d $name | tr -s " " ":" \
| cut -d ":" -f 1 | cut -c 2-10 2>/dev/null)
current_uid=$(ll -d $name | tr -s " " ":" \
| cut -d ":" -f 3 2>/dev/null)
current_gid=$(ll -d $name | tr -s " " ":" \
| cut -d ":" -f 4 2>/dev/null)
if [[ "$perm" != "$current_perm" ]]
then
print "---" >>$log
print "Permissions of $name should be: $perm" >>$log
print "Currenly they are: $current_perm" >>$log
print "To correct them execute: chmod $num_perm $name" >>$log
error=TRUE
fi
if [[ "$uid" != "$current_uid" ]]
then
print "---" >>$log
print "The user owning $name should be: $uid" >>$log
print "Currenly the owner is: $current_uid" >>$log
print "To correct this execute: chown $uid $name" >>$log
error=TRUE
fi
if [[ "$gid" != "$current_gid" ]]
then
print "---" >>$log
print "The group owning $name should be: $gid" >>$log
print "Currenly the group is: $current_gid" >>$log
print "To correct this execute: chgrp $gid $name" >>$log
error=TRUE
fi

else
if [[ "$name" != "/var/spool/lp/default" ]]
then
print "$name is either not a directory/file or does not exist.">>$log
error=TRUE
fi
fi
;;
# if the entry is a list of files
l) if [[ -d $name ]]
then
if [[ "$name" = "/var/spool/lp/request" ]]
then
files=$(ls $name | tr -s "\n" ":" 2>/dev/null)
else
files=$(lsf $name | grep -v '/$' | tr -d "*" \
| tr -s "\n" ":" 2>/dev/null)
fi
for file in $files
do
# read the current permissions, owner and group ID
current_perm=$(ll -d $name/$file | tr -s " " ":" \
| cut -d ":" -f 1 \
| cut -c 2-10 2>/dev/null)
current_uid=$(ll -d $name/$file | tr -s " " ":" \
| cut -d ":" -f 3 2>/dev/null)
current_gid=$(ll -d $name/$file | tr -s " " ":" \
| cut -d ":" -f 4 2>/dev/null)
if [[ "$perm" != "$current_perm" ]]
then
print "---" >>$log
print "Permissions of $name/$file should be: $perm" >>$log
print "Currenly they are: $current_perm" >>$log
print "To correct them execute: chmod $num_perm $name/$file" >>$log
error=TRUE
fi
if [[ "$uid" != "$current_uid" ]]
then
print "---" >>$log
print "The user owning $name/$file should be: $uid" >>$log
print "Currenly the owner is: $current_uid" >>$log
print "To correct this execute: chown $uid $name/$file" >>$log
error=TRUE
fi
if [[ "$gid" != "$current_gid" ]]
then
print "---" >>$log
print "The group owning $name/$file should be: $gid" >>$log
print "Currenly the group is: $current_gid" >>$log
print "To correct this execute: chgrp $gid $name/$file" >>$log
error=TRUE
fi
done

else
print "$name is not a directory or does not exist." >>$log
error=TRUE
fi
;;
esac
done
if [[ "$error" = "FALSE" ]]
then
print "The files and directories have correct permissions and"
print "ownership.\n\n"
else
print "Verification failed. See $log for more information.\n\n"
fi
rm $permissions
fi
while true
do
print "Step 3"
print -n "Would you like to check for possible printer status file
corruption (y/n): "
read ANSWER
case $ANSWER in
y|Y|yes) print "Checking for printer status file corruption ...\n"
status_file_check=1
break
;;
n|N|no)

print "Skipping printer status file corruption check.\n"


status_file_check=0
break

;;
q|Q|quit) print "Exiting LP Spooler configuration check script.\n"
exit 0
;;
*)

print "Invalid answer. please reply with y or n.\n"


continue

;;
esac
done
if (( $status_file_check == 1 ))
then
num_v=$(lpstat -v | grep -c ^device)
if (( $? != 0 ))
then
lpstat_return=1
fi
num_p=$(lpstat -p | grep -c ^printer)
if (( $? != 0 ))

then
lpstat_return=1
fi
num_a=$(lpstat -a | grep -c "accepting requests")
if (( $? != 0 ))
then
lpstat_return=1
fi
if (( $lpstat_return == 1 ))
then
print "\nThe lpstat command failed. The script cannot continue."
print "Possibly there are no printers configured on your system."
print "Please correct any permission/ownership problems first."
print "See above error message for details."
exit 6
fi
num_class=$(ll /etc/lp/class | grep -c -v ^total)
(( num_q = $num_a - $num_class ))
if [[ $num_v != $num_p || $num_p != $num_q ]]
then
print "The number of configured printer device files, print queues"
print "and their acceptance status as listed by lpstat does not match."
print "Printer status file corruption is likely."
lpstat -v | grep ^device | cut -d " " -f 3 | tr -d ":" \
| sort > $lpstat_v 2>/dev/null
lpstat -p | grep ^printer | cut -d " " -f 2 | sort > $lpstat_p 2>/dev/null
lpstat -a | grep "accepting requests" | cut -d " " -f 1 | sort \
> $lpstat_a 2>/dev/null
diff_vp=$(diff $lpstat_v $lpstat_p | grep -e "^>" -e "^<" \
| cut -c 3- )
diff_pa=$(diff $lpstat_p $lpstat_a | grep -e "^>" -e "^<" \
| cut -c 3- )
diff=$(print $diff_vp $diff_pa)
diff_number=$(print $diff | wc -l)
if (( $diff_number != 0 ))
then
if (( $num_class != 0 ))
then
list_class=$(ls /etc/lp/class | tr -s "\n" ":")
for class in $list_class
do
diff=$(print $diff | grep -v "^${class}$")
done
fi
print "\n\nThis is the list of print queues which most likely are"
print "corrupt:"
print "$diff\n"
print
print
print
print
fi

"Please remove these print queues and add them again"


"using SAM, HP Printer Installer or the lpadmin command."
"Correct any permissions problems first before removing or"
"adding print queues.\n"

rm $lpstat_v $lpstat_p $lpstat_a


else
print "Printer status file check succeeded.\n"
fi
fi
print "Finished LP Spooler configuration check."
exit 0

You might also like