#!/bin/bash


#Preupgrade Assistant performs system upgradability assessment
#and gathers information required for successful operating system upgrade.
#Copyright (C) 2013 Red Hat Inc.
#Ondrej Vasik <ovasik@redhat.com>
#
#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 3 of the License, 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.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
. /usr/share/preupgrade/common.sh
check_rpm_to "" ""
#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 5, the range reserved for system account IDs is 0-499, while in Red Hat Enterprise Linux 7 it is 0-999. This situation might cause troubles during the migration process. In addition, the 0-100 range 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 a 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`
do
  # Is id greater than 999? Nothing to do
  [ `echo $i | cut -d':' -f3` -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 | cut -d':' -f3` -gt 499 ]
  then
  logindefs=1
  continue
  fi

  # Reserved system user ID range use
  if [ `echo $i | cut -d':' -f3` -lt 101 ]
  then
    grep " $myname " systemids >/dev/null 2>/dev/null && continue
    echo " System account \"$myname\" uses ID `echo $i | cut -d':' -f3` without reservation - the usage is prohibited and might cause migration issues." >>solution.txt
    log_slight_risk "System account \"$myname\" uses ID `echo $i | cut -d':' -f3` without reservation - the usage is prohibited and might cause migration issues."
    founderror=1
  fi
done

[ $founderror -eq 1 ] && echo >>solution.txt
# Check for invalid range group ids
for i in `cat $VALUE_GROUP | cut -d':' -f1-4`
do
  # Is id greater than 999? Nothing to do
  [ `echo $i | cut -d':' -f3` -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 | cut -d':' -f3` -gt 499 ]
  then
  logindefs=1
  continue
  fi

  # Reserved system group ID range use
  if [ `echo $i | cut -d':' -f3` -lt 101 ]
  then
    grep " $myname " systemids >/dev/null 2>/dev/null && continue
    echo " System group \"$myname\" uses ID `echo $i | cut -d':' -f3` without reservation - this ID is prohibited from the use and might cause migration issues." >>solution.txt
    log_slight_risk "System group \"$myname\" uses ID `echo $i | cut -d':' -f3` without reservation - this ID is prohibited from the use and might cause migration issues."
    founderror=1
  fi
done

[ $logindefs -eq 1 ] &&
echo \
"
Your system contains user or group IDs in the range between 500 and 1000. Therefore, we will keep the Red Hat Enterprise Linux 5 defaults (system accounts limit on ID 499) to prevent mixing up system and user accounts. If you can easily migrate your user accounts above 1000, do so, and adjust the /etc/login.defs file to the values used in Red Hat Enterprise Linux 7. It might be a non-trivial task for some system setups.
" >>solution.txt

result=0
[ $founderror -eq 1 ] && result=$RESULT_FAILED
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_FAILED
[ $result -gt 0 ] && exit $result

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