#!/bin/sh
#
#     tiger - A UN*X security checking system
#     Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 1, or (at your option)
#    any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#     Please see the file `COPYING' for the complete copyright notice.
#
# SunOS/gen_cron - 06/14/93
#
# SunOS/gen_cron - 08/12/2002 - jfs - Fixed so that it works standalone
# SunOS/gen_cron  - 02/10/2018 - jfs - Exit if the directory does not exist
#                   or is not readable
#
#-----------------------------------------------------------------------------
#
[ -z "$LS" ] && LS=`which ls`
[ -z "$SED" ] && SED=`which sed`
[ -z "$CRONSPOOL" ] && CRONSPOOL=/var/spool/cron/crontabs

outfile=$1

#if [ -z "$outfile" ] 
#then
#	echo "Usage: $0 file"
#	exit 1
#fi


[ -n "$outfile" ] && > $outfile

[ ! -n "$GETUSERHOME" ] && GETUSERHOME=echo

if [ ! -d "$CRONSPOOL" ] ; then
    echo "--ERROR-- Cron spool directory $CRONSPOOL does not exist."
    exit 1
fi
if [ ! -r "$CRONSPOOL" ] ; then
    echo "--ERROR-- Cannot read contents of spool directory $CRONSPOOL."
    exit 1
fi

(
  cd $CRONSPOOL
  $LS -l $LSGROUP *
) |
while read perm y owner group size date1 date2 file
do
  [ "$owner" != "root" ] && {
    echo "--WARN-- CRON file \`$file' is owned by $owner."
  }
  case $file in
    *[!a-zA-Z0-9]*)
      echo "--WARN-- Unusual cron file \`$file' found."
      ;;
    *)
      $GETUSERHOME "$file" >/dev/null 2>/dev/null
      
      if [ $? = 0 ]; then
	$SED -e 's/#.*$//' -e '/^$/d' $CRONSPOOL/$file |
	while read a b c d e command
	do
	  echo "$file $command"
	done >> $outfile
      else
	echo "--WARN-- Found cron file for unknown user $file."
      fi
      ;;
    esac
done
  
