#!/bin/sh
#
################################################################
#       ITEM: CNTL(USSICOLB)                                   #
#    CREATED: 07/07/00 DCM                                     #
#   FUNCTION: Shell script for OS/390 UNIX System Services     #
#             data collection.                                 #
#     NOTES: - This member contains UNIX commands.             #
#              - It MUST stay in mixed case.                   #
#              - It MUST NOT be numbered.                      #
#            - Any find commands that are continued must have  #
#              the continuation character (i.e., \) in column  #
#              80. This is required becuase the OCOPY command  #
#              will be inserting  new line characters that     #
#              MUST follow.                                    #
#              --> ANY CONTINUED FIND COMMANDS MUST HAVE \ in  #
#                  column 80.                                  #
#   CHANGES:                                                   #
#   07/12/01 - DCM - Added _BPX_SHAREAS for performance        #
#                  - Changed "if" statements using bracket     #
#                    characters to use test statement          #
#   05/21/15 - CLF - Removed unneeded commands, STS-008182.    #
#   12/07/15 - CLF - Changed information specified in PATH.    #
#                    Improved performance by collecting all    #
#                    file and storing them in filelist file.   #
#                    This filelist is use to find specific     #
#                    files for WAS.  Remove duplicate process. #
#   04/27/16 - CLF - Changes in how sticky bit is evaluated    #
#                    in usscmds_rpt_ls function, STS-014368.   #
#   08/11/16 - CLF - Changed how information processing is     #
#                    displayed in output and changed value of  #
#                    the PATH variable issue, STS-015326.      #
################################################################
#
################################################################
#  Set Variables                                               #
################################################################
export _BPX_SHAREAS=YES
RPT0HOST="System `/bin/uname -nm` at `/bin/uname -sv`.`/bin/uname -r`"
RPT1DIR=$HOME"/fso_srrmvs/usscmds_rpt"
RPT1DATE=`/bin/date "+%Y-%m-%d %T %Z"`
RPT1HOST="System `/bin/uname -n`"
PGMNAME="USSICOLB 08/11/16"
#
################################################################
#  Define functions                                            #
#   - usscmds_rpt_cat    - print permission bits and contents  #
#   - usscmds_rpt_ls     - print permission bits               #
#   - usscmds_rpt_hdr    - print report header                 #
#   - usscmds_perm_trans - translate permission bits           #
################################################################
function usscmds_rpt_cat
{
echo '         usscmds_rpt_cat:' ${1}
if test ! -e ${1}
  then
    echo "    File does not exist -" ${1} >> ${RPT1FILE}
    return 0
fi
#
LSDATA=`ls -aldELW ${1}`
FPERM=0000
usscmds_perm_trans ${LSDATA}
echo " ${FPERM}   ${LSDATA}"                      >> ${RPT1FILE}
echo " ........ Contents Start -" ${1} "........" >> ${RPT1FILE}
cat ${1}                                          >> ${RPT1FILE}
echo " ........ Contents Stop  -" ${1} "........" >> ${RPT1FILE}
#
return 0
}
#
function usscmds_rpt_ls
{
echo '         usscmds_rpt_ls:' ${1} ${2} ${3}
AUD=${3}
if test ${1} = ${AUTOMNT}
  then
    AUD="---"
  fi
if test -e ${1}
  then
    LSDATA=`ls -aldELW ${1}`
    FPERM=0000
    usscmds_perm_trans ${LSDATA}
    T1=`echo ${FPERM} | /bin/tr 01234567 01452367`
    T2=`echo ${2}     | /bin/tr 01234567 01452367`
#   echo ${FPERM} 'to' ${T1} 'and' ${2} 'to' ${T2}
    T11=`/bin/expr substr ${T1} 1 1`
    T12=`/bin/expr substr ${T1} 2 1`
    T13=`/bin/expr substr ${T1} 3 1`
    T14=`/bin/expr substr ${T1} 4 1`
    T21=`/bin/expr substr ${T2} 1 1`
    T22=`/bin/expr substr ${T2} 2 1`
    T23=`/bin/expr substr ${T2} 3 1`
    T24=`/bin/expr substr ${T2} 4 1`
    XTST=0
#   echo ${1} ${T11} ${T21} "act:"${FPERM} "rec:"${2} "start"
    if test ${T11} -ne ${T21}                                                  \
         -o ${T12} -gt ${T22}                                                  \
         -o ${T13} -gt ${T23}                                                  \
         -o ${T14} -gt ${T24}
      then
#       echo ${DIR} ${1} "first if"
        if test ${T11} -eq 1                                                   \
             -a ${T21} -eq 0                                                   \
             -a ${DIR} = 'd'                                                   \
             -a ${T12} -le ${T22}                                              \
             -a ${T13} -le ${T23}                                              \
             -a ${T14} -le ${T24}
          then
#           echo "second if"
            XTST=0
          else
#           echo "second if else"
            XTST=1
          fi
        else
#         echo "first if else"
          XTST=0
      fi
#   echo ${1} ${T11} ${T21} "act:"${FPERM} "rec:"${2} "X:"${XTST} "D:"${DIR}
    XAUD=`/bin/expr substr "${LSDATA}" 13 3`
    XAUD1=`/bin/expr substr "${LSDATA}" 13 11`
    XOWN=`/bin/expr substr "${LSDATA}" 28 17`
    if test ${XAUD} != ${AUD}
      then
        XTST=$((${XTST} + 2))
    fi
    echo "${XTST}   ${FPERM}   ${XAUD1} ${XOWN} ${1}"     >> ${RPT1FILE}
  else
    echo "    File does not exist -" ${1}    >> ${RPT1FILE}
fi
#
return 0
}
#
function usscmds_rpt_hdr
{
if test ! -e ${1}
  then
    RPT1DATE=`/bin/date "+%Y-%m-%d %T %Z"`
    echo " ************************" >  ${1}
    echo " " $1 " Report created on" $RPT1DATE "for" $RPT1HOST >> ${1}
    echo " ************************" >> ${1}
  else
    echo " "                        >> ${1}
fi
#
return 0
}
#
function usscmds_perm_trans
{
#
if test ${#1} -lt 10
  then
    return 1
fi
#
DIR=`/bin/expr substr "${1}" 1 1`
ALPPERM=`/bin/expr substr "${1}" 2 9`
NUMPERM=`echo ${ALPPERM} | /bin/tr rwstxST- 42111000`
#
i=0
a=0
if test "`/bin/expr substr "${ALPPERM}" 3 1`" = "s"                            \
  -o "`/bin/expr substr "${ALPPERM}" 3 1`" = "S"
  then
    a=4
fi
if test "`/bin/expr substr "${ALPPERM}" 6 1`" = "s"                            \
  -o "`/bin/expr substr "${ALPPERM}" 6 1`" = "S"
  then
    a=$((${a} + 2))
fi
if test "`/bin/expr substr "${ALPPERM}" 9 1`" = "t"                            \
  -o "`/bin/expr substr "${ALPPERM}" 9 1`" = "T"
  then
    a=$((${a} + 1))
fi
for x in  1 4 7
  do
    FPERMX=`/bin/expr substr ${NUMPERM} ${x} 3`
    APERMX=`/bin/expr substr ${ALPPERM} ${x} 3`
    for y in 1 2 3
      do
        i=$(($i + $((`/bin/expr substr ${FPERMX} ${y} 1`))))
      done
    a=${a}${i}
    i=
  done
#
FPERM=${a}
#
return 0
}
#
#
################################################################
#  Create collection directories in /tmp                       #
################################################################
#cd /tmp
#
export PATH=/bin:.:${HOME}
#
rm -r fso_srrmvs
umask 0027
mkdir -p fso_srrmvs/usscmds_rpt
cd fso_srrmvs/usscmds_rpt
testgid=`id -g`
#
################################################################
#  Data collection start                                       #
################################################################
RPT1DATE=`/bin/date "+%Y-%m-%d %T %Z"`
echo " "
echo ${PGMNAME} "Data Collection started" ${RPT1DATE} "for" ${RPT0HOST}
echo "         Using PATH" ${PATH}
find / >>filelist
#
################################################################
#  Report: ihshfsob  - http file/dir permission bits           #
################################################################
echo " "
RPT1NAME=ihshfsob
echo ${PGMNAME} "Starting report" ${RPT1NAME} `/bin/date "+%Y-%m-%d %T %Z"`
RPT1FILE=${RPT1DIR}/${RPT1NAME}
usscmds_rpt_hdr ${RPT1FILE}
################################################################
# find and list httpd.conf files                               #
################################################################
httpd_file=`grep /httpd.conf$ filelist`
for MNFNAME in ${httpd_file}
  do
#   echo ${MNFNAME} `/bin/date "+%Y-%m-%d %T %Z"`
    usscmds_rpt_ls ${MNFNAME}               0460    faf
  done
echo " "
################################################################
# find and list httpd.envvars files                            #
################################################################
#for MNFNAME in `find / -name httpd.envvars`
#  do
#    usscmds_rpt_ls ${MNFNAME}               0460    faf
#  done
for xxxNAME in ${httpd_file}
  do
    MNFNAME=`dirname "${xxxNAME}"`/httpd.envvars
#   echo ${xxxNAME} ${MNFNAME} `/bin/date "+%Y-%m-%d %T %Z"`
    usscmds_rpt_ls ${MNFNAME}               0460    faf
  done
#exit
################################################################
# find and list mvsds.conf files                               #
################################################################
for MNFNAME in `grep /mvsds.conf$ filelist`
  do
    usscmds_rpt_ls ${MNFNAME}               0460    faf
  done
echo " " >> ${RPT1FILE}
################################################################
# find in each httpd.conf files for specific directives        #
################################################################
for MNFNAME in ${httpd_file}
  do
    echo " "
    echo "         Processing contents of" ${MNFNAME}"."
    InstallPath=`grep "InstallPath " ${MNFNAME}|grep -v "^#"|awk               \
      '{print $2}'|/bin/tr -d '"'|/bin/tr -d "'"`
    if test "${InstallPath}" != ""
      then
      echo "InstallPath" ${InstallPath} >> temp0
      echo "         InstallPath" ${InstallPath}
    fi
    ServerRoot=`grep "ServerRoot " ${MNFNAME}|grep -v "^#"|awk                 \
      '{print $2}'|/bin/tr -d '"'|/bin/tr -d "'"`
    if test "${ServerRoot}" != ""
      then
      if test "`/bin/expr substr "${ServerRoot}" 1 1`" != "/"
        then
        ServerRoot=${InstallPath}/${ServerRoot}
      fi
      echo "ServerRoot" ${ServerRoot} >> temp0
      echo "         ServerRoot" ${ServerRoot}
    fi
    Log=`grep "Log " ${MNFNAME}|grep -v "^#"|grep -v "NoLog "|awk              \
      '{print $2}'|/bin/tr -d '"'|/bin/tr -d "'"|/bin/tr -d "|"`
    for Logname in ${Log}
      do
        if test "`/bin/expr substr "${Logname}" 1 1`" != "/"
          then
          Logname=${ServerRoot}/${Logname}
        fi
        echo "Log" ${Logname} >> temp0
        echo "         Log" ${Logname}
      done
  done
sort -u -o temp00 temp0
################################################################
# select InstallPath entries for processing                    #
################################################################
InstallPaths=`grep "InstallPath " temp00|awk '{print $2}'`
for MNFNAME in ${InstallPaths}
  do
    usscmds_rpt_ls ${MNFNAME}               0755    fff
    usscmds_rpt_ls ${MNFNAME}/bin           0755    fff
    usscmds_rpt_ls ${MNFNAME}/sbin          0750    fff
  done
################################################################
# select ServerRoot entries for processing                     #
################################################################
ServerRoots=`grep "ServerRoot " temp00|awk '{print $2}'`
for MNFNAME in ${ServerRoots}
  do
    usscmds_rpt_ls ${MNFNAME}               0555    fff
    usscmds_rpt_ls ${MNFNAME}/Admin         0550    fff
    usscmds_rpt_ls ${MNFNAME}/admin-bin     0550    fff
    usscmds_rpt_ls ${MNFNAME}/cgi-bin       0551    fff
    usscmds_rpt_ls ${MNFNAME}/fcgi-bin      0550    fff
    usscmds_rpt_ls ${MNFNAME}/pub           0555    fff
    usscmds_rpt_ls ${MNFNAME}/logs          0750    fff
  done
################################################################
# select *Log entries for processing                           #
################################################################
echo " "
Logs=`grep "Log " temp00|awk '{print $2}'`
for MNFNAME in ${Logs}
  do
    echo "         Processing prefix" ${MNFNAME} `/bin/date "+%Y-%m-%d %T %Z"`
    file=`basename "${MNFNAME}"`*
    dir=`dirname "${MNFNAME}"`/
#   echo "Find" ${file} "in" ${dir}"."
    find "${dir}" -name "${file}" ! \( -perm -0750 -a -audit rwx=f \) |        \
      head -5 >> temp1
  done
sort -u -o temp00 temp1
for MNFNAME in `awk '{print $1}' temp00`
  do
#   echo "usscmds_rpt_ls" ${MNFNAME} "            0750    fff"
    usscmds_rpt_ls ${MNFNAME}              0750    fff
  done
RPT1DATE=`/bin/date "+%Y-%m-%d %T %Z"`
echo " ........ End Report ...." >> ${RPT1FILE}
#
################################################################
#  Report: washfsob  - http file/dir permission bits           #
################################################################
echo " "
RPT1NAME=washfsob
echo ${PGMNAME} "Starting report" ${RPT1NAME} `/bin/date "+%Y-%m-%d %T %Z"`
RPT1FILE=${RPT1DIR}/${RPT1NAME}
usscmds_rpt_hdr ${RPT1FILE}
################################################################
# find ServerInit directive in httpd.conf files                #
################################################################
for MNFNAME in ${httpd_file}
  do
    grep "ServerInit " ${MNFNAME}|grep -v "^#"|tr ":," "  "|                   \
      awk                                                                      \
      '{print $0}' >> temp2
  done
sort -u -o temp00 temp2
#
################################################################
# select ServerInit directives files and directories           #
################################################################
for MNFNAME in `awk '{print $5}' temp00`
  do
    echo ${MNFNAME}  >> temp3
    DMNFNAME=`dirname ${MNFNAME}`
    echo ${DMNFNAME}  >> temp3
  done
for MNFNAME in `awk '{print $2}' temp00`
  do
    DMNFNAME=`dirname ${MNFNAME}`
    echo ${DMNFNAME}  >> temp3
  done
sort -u -o temp00 temp3
#
################################################################
# process ServerInit directives files and directories          #
################################################################
for MNFNAME in `awk '{print $1}' temp00`
  do
    usscmds_rpt_ls ${MNFNAME}                   0555    fff
  done
echo " " >> ${RPT1FILE}
################################################################
# find and list ics_pics.conf files                            #
################################################################
for MNFNAME in `grep /ics_pics.conf$ filelist`
  do
    usscmds_rpt_ls ${MNFNAME}               0640    fff
  done
################################################################
# find and list javelin.conf files                             #
################################################################
for MNFNAME in `grep /javelin.conf$ filelist`
  do
    usscmds_rpt_ls ${MNFNAME}               0640    fff
  done
################################################################
# find and list socks.conf files                               #
################################################################
for MNFNAME in `grep /socks.conf$ filelist`
  do
    usscmds_rpt_ls ${MNFNAME}               0640    fff
  done
################################################################
# find and list lgw_fcgi.conf files                            #
################################################################
for MNFNAME in `grep /lgw_fcgi.conf$ filelist`
  do
    usscmds_rpt_ls ${MNFNAME}               0640    fff
  done
################################################################
# find and list IMWSendMail.cfg files                          #
################################################################
for MNFNAME in `grep /IMWSendMail.cfg$ filelist`
  do
    usscmds_rpt_ls ${MNFNAME}               0640    fff
  done
echo " " >> ${RPT1FILE}
################################################################
# find and list admin.config files                             #
################################################################
for MNFNAME in `grep /admin.config$ filelist`
  do
    usscmds_rpt_ls ${MNFNAME}               0640    fff
  done
################################################################
# find and list sas.server.props files                         #
################################################################
for MNFNAME in `grep /sas.server.props$ filelist`
  do
    usscmds_rpt_ls ${MNFNAME}               0640    fff
  done
################################################################
# find and list sas.client.props files                         #
################################################################
for MNFNAME in `grep /sas.client.props$ filelist`
  do
    usscmds_rpt_ls ${MNFNAME}               0640    fff
  done
echo " " >> ${RPT1FILE}
echo " ........ End Report ...." >> ${RPT1FILE}
#
################################################################
#  Report: ahttpd    - report on all httpd.conf files          #
################################################################
echo " "
RPT1NAME=ahttpd
echo ${PGMNAME} "Starting report" ${RPT1NAME} `/bin/date "+%Y-%m-%d %T %Z"`
RPT1FILE=${RPT1DIR}/${RPT1NAME}
usscmds_rpt_hdr ${RPT1FILE}
for MNFNAME in ${httpd_file}
  do
    usscmds_rpt_cat ${MNFNAME}
  done
echo " ........ End Report ...." >> ${RPT1FILE}
#
################################################################
# Remove temporary scripts                                     #
################################################################
#chown -R ${LOGNAME}:${testgid} /tmp/fso_srrmvs
#/bin/cd
###rm ./usscmds_rpt_find
#
################################################################
#  Data collection end                                         #
################################################################
RPT1DATE=`/bin/date "+%Y-%m-%d %T %Z"`
echo " "
echo ${PGMNAME} "Data Collection ended " ${RPT1DATE} "for" ${RPT0HOST}
echo "         ........................"
echo "         ........................"
 
