#!/bin/bash


. /usr/share/preupgrade/common.sh
check_applies_to ""  "grep,tail,cut,sort"
switch_to_content
#END GENERATED SECTION

[ ! -r systemids ] && log_error "Generic part of the module is missing." && \
  exit $RESULT_ERROR

founderror=0
logindefs=0
rm solution.txt
echo \
"In Red Hat Enterprise Linux 6, the range reserved for system account IDs is 0 - 499, while in Red Hat Enterprise Linux 7 it is 0 - 999. This difference might cause troubles during the upgrade. In addition, the range 0 - 199 is prohibited from the use without a static ID reservation in the setup package. The IDs that are in this range might be reserved and used later by a package, and using them might cause the malfunction of the package.

The following problems were found on your system:
" >solution.txt

# Check for invalid range user ids
#for i in `cat $VALUE_PASSWD | cut -d':' -f1-4`
while read -r i ; do
  # Is id greater than 999? Nothing to do
  [ `echo $i | awk -F':' '{ if ( $3 ~ /^[0-9]+$/ ) { print $3 } else {print $4} }'` -gt 999 ] && continue

  myname=`echo $i | cut -d':' -f1`
  # RHEL 6 uid user range 500-999 - we need to keep RHEL 6 defaults
  if [ `echo $i | awk -F':' '{ if ( $3 ~ /^[0-9]+$/ ) { print $3 } else {print $4} }'` -gt 499 ]
  then
  logindefs=1
  continue
  fi

  # Reserved system user ID range use
  if [ `echo $i | awk -F':' '{ if ( $3 ~ /^[0-9]+$/ ) { print $3 } else {print $4} }'` -lt 200 ]
  then
    grep " $myname " systemids >/dev/null 2>/dev/null && continue
    echo " System account \"$myname\" uses ID `echo $i | awk -F':' '{ if ( $3 ~ /^[0-9]+$/ ) { print $3 } else {print $4} }'` without reservation. This usage is prohibited and might cause migration problems." >>solution.txt
    log_slight_risk "System account \"$myname\" uses ID `echo $i | awk -F':' '{ if ( $3 ~ /^[0-9]+$/ ) { print $3 } else {print $4} }'` without reservation. This usage is prohibited and might cause migration problems."
    founderror=1
  fi
done < "$VALUE_PASSWD"

[ $founderror -eq 1 ] && echo >>solution.txt
# Check for invalid range group ids
#for i in `cat $VALUE_GROUP | cut -d':' -f1-4`
while read -r i ; do
  # Is id greater than 999? Nothing to do
  [ `echo $i | awk -F':' '{ if ( $3 ~ /^[0-9]+$/ ) { print $3 } else {print $4} }'` -gt 999 ] && continue

  myname=`echo $i | cut -d':' -f1`
  # rhel-6 gid user range 500-999 - we need to keep the RHEL 6 defaults
  if [ `echo $i | awk -F':' '{ if ( $3 ~ /^[0-9]+$/ ) { print $3 } else {print $4} }'` -gt 499 ]
  then
  logindefs=1
  continue
  fi

  # Reserved system group ID range use
  if [ `echo $i | awk -F':' '{ if ( $3 ~ /^[0-9]+$/ ) { print $3 } else {print $4} }'` -lt 200 ]
  then
    grep " $myname " systemids >/dev/null 2>/dev/null && continue
    echo " System group \"$myname\" uses ID `echo $i | awk -F':' '{ if ( $3 ~ /^[0-9]+$/ ) { print $3 } else {print $4} }'` without reservation. This ID is prohibited from the use and might cause migration problems." >>solution.txt
    log_slight_risk "System group \"$myname\" uses ID `echo $i | awk -F':' '{ if ( $3 ~ /^[0-9]+$/ ) { print $3 } else {print $4} }'` without reservation. This ID is prohibited from the use and might cause migration problems."
    founderror=1
  fi
done < "$VALUE_GROUP"

[ $logindefs -eq 1 ] &&
echo \
"
Your system contains UIDs or GIDs in the range 500 - 999. Therefore, the Red Hat Enterprise Linux 6 defaults (system accounts limit on ID 499) will be kept to prevent mixing up the system and user accounts. If you can migrate your user accounts above 1000 easily, do so, and adjust the /etc/login.defs file to the values used in Red Hat Enterprise Linux 7.
" >>solution.txt

result=0
[ $founderror -eq 1 ] && result=$RESULT_FAIL
rhelup_preupgrade_hookdir="$VALUE_TMP_PREUPGRADE/preupgrade-scripts"
[ $logindefs -eq 1 ] &&
 $(grep "/etc/login.defs" $VALUE_CONFIGCHANGED >/dev/null || $(mkdir -p "$rhelup_preupgrade_hookdir" && install -m755 fixlogindefs.sh "$rhelup_preupgrade_hookdir"/ )) &&
 result=$RESULT_FIXED
[ $founderror -eq 1 ] && result=$RESULT_FAIL
[ $result -gt 0 ] && exit $result

#no issues found, so remove solution text
rm solution.txt && touch solution.txt
exit $RESULT_PASS
