#!/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 "" ""
COMPONENT="distribution"
#END GENERATED SECTION

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

founderror=0
rm solution.txt
echo \
"In Red Hat Enterprise Linux 6, the range reserved for system account IDs is 0-500 while in Red Hat Enterprise Linux 7 it is 0-999 which may cause troubles during migration. In addition, the 0-199 range is prohibited from use without a static ID reservation in the setup package. IDs which are in this range might be reserved and used later by a package and using them may cause 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
  if [ `echo $i | cut -d':' -f3` -gt 499 ]
  then
  echo " User \"$myname\" uses ID `echo $i | cut -d':' -f3` - this ID is in the range reserved for system accounts." >>solution.txt
  founderror=1
  continue
  fi

  # Reserved system user ID range use
  if [ `echo $i | cut -d':' -f3` -lt 200 ]
  then
    grep " $myname " systemids >/dev/null 2>/dev/null && continue
    echo " System account \"$myname\" uses ID `echo $i | cut -d':' -f3` without reservation - usage is prohibited and may cause migration issues!" >>solution.txt
    log_slight_risk "System account \"$myname\" uses ID `echo $i | cut -d':' -f3` without reservation - usage is prohibited and may 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
  if [ `echo $i | cut -d':' -f3` -gt 499 ]
  then
  echo " Group \"$myname\" uses ID `echo $i | cut -d':' -f3` - this ID is in the range reserved for system accounts." >>solution.txt
  founderror=1
  continue
  fi

  # Reserved system group ID range use
  if [ `echo $i | cut -d':' -f3` -lt 200 ]
  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 use and may 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 use and may cause migration issues!"
    founderror=1
  fi
done

echo \
"

If these accounts are not system accounts, migrate them to an ID number above 1000. Alternatively, if required by your system setup settings, you can change the default ranges in the /etc/login.defs file to the values used in Red Hat Enterprise Linux 6.
" >>solution.txt
#FIXME: Provide some solution scripts to change either /etc/login.defs or
#to migrate the users to "safe range"

[ $founderror -eq 1 ] && exit $RESULT_FAIL

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