#!/bin/sh
#
#
ARGV="$@"

Number="$#"
OS=`uname`
Root=@@ServerRoot@@
ADMIN_PASSWD_FILE="$Root/conf/admin.passwd"

#Set Path for "groupadd" and "adduser" in Linux, HP-UX.
PATH=/usr/sbin:$PATH
export PATH

RC_GROUPEXISTS=50
RC_NOGROUP=54

#Set Path for SunOS in order to use "grep" command
if [ $OS = "SunOS" ]; then
        PATH=/usr/xpg4/bin:$PATH
        export PATH
        if   echo "$1" | grep -iE  "help|-h|-help" >/dev/null    ; then
                HELPDISPLAY=1
        else HELPDISPLAY=0
        fi
elif [ $OS = "AIX" ]||[ $OS = "Linux" ]||[ $OS = "HP-UX" ]; then
        if   echo "$1" | grep -iE  "help|-h|-help" >/dev/null    ; then
                HELPDISPLAY=1
        else HELPDISPLAY=0
        fi
fi
        
##HELP

helpdisplay() {

## Display help only for  non silent execution 

if [ $silent -eq 0 ] ; then
        echo "usage: $0 (no arguments)/n"
        cat <<EOF | more


EXECUTION MODE:

        COMMAND LINE Arguments:
                Run this script with command line arguments:
                -usr <userName> -grp <groupname> -cfg <IHS web server configfileName>  
                -adm <IHS admin server configfilename> -plg <plugin-cfg.xml>(optional)
ARGUMENTS:


        -silent:
                This allows this script to run without any messsage text.  
        -create:
                This  parameter  indicates that we will attempt to create 
                a User and Group. You must be running as Root.
        -usr: (Required)
                This UserID will be used to run the Administration Server.
                This UserID will be updated in the <User> directive of the 
                Administration Server configuration file (admin.conf).

        -grp: (Required)
                This Group Name will be used to run the Administration Server.
                The Group Name will be used to change the file Permissions for
                the configuration files and User or Group Authentication files.
                This Group Name  will be updated in the <Group> directive 
                of the Administration Server configuration file (admin.conf). 
                We recommend a unique Group Name for Administration Server.

         -cfg: (Required)
                Fully qualified File name of the IHS webserver configuration
                file.  This is the file name for which the permission and 
                 group will be changed. The Administration Server requires read 
                and write access to IHS configuration files.

         -plg: (Optional)
                This is the fully qualifed file name for the plugin-cfg.xml
                for which file permissions will be changed.
                
         -adm: (Required)
                Fully qualified filename for the 
                Administration server configuration file. If no parameter 
                is supplied a default administration configuration file 
                will be used based on the installRoot of the webserver and
                default path for the admin.conf (<InstallRoot>conf/admin.conf)

        PROCESS
                -Create a UserID and Group Name using the UserID and
                     Group Name input.
                -Change the Group Name associated with the configuration file 
                        to the Group Name specified on the input. 
                -Update the Administration Server Configuration file
                        (admin.conf). The Administration server configuration
                        file is expected to be found in <ServerRoot/conf>.
                        A backup file will be created for the admin.conf each 
                        time you execute this script.

EOF
fi
        return

}

##Functions for update of the admin.conf file 
endProc()  {  
    if [ $silent -eq 1 ] ; then
      exit $retcde  
    else
      case $retcde  in
        60) 
          echo "Using existing UserID: '$UserID' RC(60) "
        ;;
        62) 
                echo "You did NOT supply required parameter <-usr> UserID RC(62)"
        ;;
        64) 
                echo "UserID '$UserID' does NOT exist and  option <-create> was not specified RC(64)"
        ;;
        68) 
                echo "Error Invalid UserID: '$UserID' RC(68)"
        ;;
        $RC_GROUPEXISTS) 
          echo "Using existing Group: '$GroupN' RC($RC_GROUPEXISTS) "
        ;;
        52) 
                echo "You did NOT supply required parameter <-grp> Group Name RC(52)"
        ;;
        $RC_NOGROUP) 
                echo "Group '$GroupN' does NOT exist and  option <-create> was not specified RC($RC_NOGROUP)"
        ;;
        58)
                echo "Error Invalid Group Name: '$GroupN' RC(58) "
        ;;
        70) echo "Last complete return code RC(70)" 
        ;;
        72) 
                echo "You did NOT supply  required parameter <-cfg> configuration file RC(72)"
        ;;
        78) 
                echo "Parameter <-cfg>, file name '$cfgfile' does not exist RC(78)"
        ;;
        10) 
              echo "Command: 'chgrp $GroupN $cfgfile' failed RC(10)"
        ;;
        11) 
              echo "Directory conf - '$conf' does not exist failed RC(11)"
        ;;
        12) 
              echo "Command: 'chmod or chgrp for $conf' failed RC(12)"
        ;;
        13) 
                 echo "Command: 'chmod g+rw $cfgfile' failed RC(13)"
        ;;
        80) echo  "Last completed return code RC(80)"
        ;;
        82) 
##              Not an error condition
##              echo "You did NOT supply a pluign-cfg.xml filename"
        ;;
        88) 
                echo "Parameter <-plg>, file name '$plgfile' does not exist RC(88)"
        ;;
        14) 
              echo "Command: 'chgrp $GroupN $plgfile' failed RC(14)"
        ;;
        15) 
                 echo "Command: 'chmod g+rw $plgfile' failed RC(15)"
        ;;
        90) echo  "Script Completed RC(90)"
        ;;
        92) 
                echo "You did NOT supply required parameter <-adm> Administration Server Configuration filename RC(92)"
        ;;
        98) 
                echo "Parameter <-adm>, file name '$admfile' does not exist RC(98)"
        ;;
        16) 
                echo "Admin File '$admfile' is not writable RC(16)"
        ;;
        17)
                echo "Update failed for Administration Server Configuration file directive 'User' and 'Group' RC(17)"   
        ;;
        99) 
                echo  "Comand '$cmd' "
                echo  "Error on Option  '$option'"
                echo  "Syntax error: '$ARGV'"
                echo
                helpdisplay
        ;;
        98) 
                echo  "End of Help"
        ;;

        *) echo "return code '$retcde'" 
        ;;
      esac

    fi
    exit $retcde  
   
}

##Functions for update of the admin.conf file 
updadmfile()  {  
          if [ ! -f $admfile ] ; then
                retcde=98
                return 98
          elif [  ! -w $admfile ] ; then
                retcde=16
                return 16
          fi 

          tempfile1=$admfile"_1".`date -u +"%T_%j"`
          tempfile2=$admfile"_2".`date -u +"%T_%j"`
         

          if sed "s/User .*$/User $UserID/" $admfile > $tempfile1 ; then
              if sed "s/Group .*$/Group $GroupN/" $tempfile1 > $tempfile2 ; then
                rm -f $admfile
                cp $tempfile2  $admfile
                rm $tempfile1 
                rm $tempfile2
                retcde=90
                return 90
              else      
                retcde=17
                return 17
              fi 
          else  
                retcde=17
                return 17
          fi 

 } 

updadmfiledefault() { 
    admfile_save=$admfile
    admfile=${admfile}.default
    updadmfile
    admfile=$admfile_save  
}
## function for  changing file permission
chgcfgfile()  {  

          conf=$Root"/conf"
          if [ ! -f $cfgfile ] ; then
                retcde=78
                return 78
          fi 

          if chgrp  $GroupN $cfgfile ; then
               if chmod g+rw $cfgfile ; then
                  if [ -d $conf ]; then
                     if chgrp  $GroupN $conf ; then
                        if chmod g+rw $conf ; then
                           retcde=70
                           return 70
                        else
                           retcde=12
                           return 12
                        fi   
                     else 
                        retcde=12
                        return 12
                     fi 
                  else            
                     retcde=11
                     return 11
                  fi 
               else
                 retcde=13
                 return 13
               fi
          else 
              retcde=10
              return 10
          fi
}

chgplgfile()  {  
        ## Change Group and permissions for the plugin-cfg.xml
          if [ ! -f $plgfile ] ; then
             plgfile=`dirname $plgfile`
             chmod g+s $plgfile
          fi 
          if chgrp  $GroupN $plgfile ; then
               if chmod g+rw $plgfile ; then
                 retcde=80
                 return 80
               else
                 retcde=15
                 return 15
               fi
          else 
              retcde=14
              return 14
          fi
}


##Functions used for group processing

##Check User exists
chkUser() {
    if [ -x "/usr/bin/getent" ]; then
        if /usr/bin/getent passwd $UserID ; then
            retcde=60
            return 60
        else
            retcde=64
            return 64
        fi
    else 
        temp=`awk " /^$UserID:/ " /etc/passwd`
        if  [ "x$temp" != x"" ]  || [  "x$temp" != "x" ] ; then
            retcde=60
            return 60
        else
            ## User does NOT exist    
            retcde=64
            return 64
        fi
    fi
}

##Create User PROCESSING
addUser() {
  if [ $OS = "AIX" ]; then
        if mkuser login=false rlogin=false home=none groups=$GroupN $UserID 2>/dev/error ; then
                retcde=60
                return 60
        else    
                retcde=68
                return 68
        fi

  elif [ $OS = "SunOS" ]; then
        if useradd -s /bin/false -g $GroupN $UserID    2>/dev/null ; then
                retcde=60
                return 60
        else
                retcde=68
                return 68
        fi
  elif [ $OS = "Linux" ]; then
        useradd -s /bin/false -g $GroupN $UserID
        local rc=$?
        if [ $rc -eq 0 ]; then
            retcde=60
            return 60
        fi
        if [ $rc -eq 9 ]; then
            # XXX: Why did chkUser fail?
            retcde=60
            return 60
        fi
        retcde=68
        return 68
  elif [ $OS = "HP-UX" ]; then
        if useradd -s /bin/false -g $GroupN $UserID    2>/dev/null ; then
                retcde=60
                return 60
        else
                retcde=68
                return 68
        fi
  fi
}

##Check GROUP exists
chkGroup() {
         ## check to see if the Group Exists 
         if [ -x "/usr/bin/getent" ]; then
           if /usr/bin/getent group $GroupN ; then
             retcde=$RC_GROUPEXISTS
             return $RC_GROUPEXISTS
           else
             retcde=$RC_NOGROUP
             return $RC_NOGROUP
           fi   
         else 
           temp=`awk " /^$GroupN:/ " /etc/group`
           if  [ "x$temp" != x"" ]  || [  "x$temp" != "x" ] ; then
                  ## Group already exist
                  retcde=$RC_GROUPEXISTS
                  return $RC_GROUPEXISTS
           else
                  ## Group does NOT exist    
                  retcde=$RC_NOGROUP
                  return $RC_NOGROUP
           fi
         fi
}

##Create GROUP PROCESSING
addGroup() {

                   if [ $OS = "AIX" ]; then
                        addGroupAIX
                   elif [ $OS = "SunOS" ]; then
                        addGroupSOLARIS
                   elif [ $OS = "Linux" ]; then
                        addGroupLINUX
                   elif [ $OS = "HP-UX" ]; then
                        addGroupHPUX
        
                   fi

}

##
addGroupAIX() {
        if mkgroup $GroupN ; then
                retcde=$RC_GROUPEXISTS
                return $RC_GROUPEXISTS
        else
                retcde=58
                return 58
        fi
}
addGroupSOLARIS() {
        if groupadd $GroupN ; then
                retcde=$RC_GROUPEXISTS
                return $RC_GROUPEXISTS
        else
                retcde=58
                return 58
        fi
}
addGroupLINUX() {
        if groupadd $GroupN ; then
                retcde=$RC_GROUPEXISTS
                return $RC_GROUPEXISTS
        else
                retcde=58
                return 58
        fi
}
addGroupHPUX() {
        if groupadd $GroupN ; then
                retcde=$RC_GROUPEXISTS
                return $RC_GROUPEXISTS
        else
                retcde=58
                return 58
        fi
}

##End of functions  used for Group Processing

## Start of function for  other processing
## start logic

create=0
silent=0
retcde=0

cmd=$0


##  No input show helpDisplay and END
if [ "$Number" = 0 ] ; then
   helpdisplay
   retcde=98
   endProc
fi

while [ "$1" != "" ]
do
  case $1
  in
  -h|-help|-hlp|/?)
        helpdisplay
        retcde=98
        endProc
        ;;
  -silent)
        silent=1
        option=-silent
        shift;;
  -create) 
        create=1
        option=-create
        shift;;
  -usr) 
        UserID=$2
        option=-usr
        shift 2 
        ;;
  -grp) 
        GroupN=$2
        option=-grp
        shift 2 ;;
  -cfg)
        cfgfile=$2
        option=-cfg
        shift 2 ;;
  -plg)
        plgfile=$2
        option=-plg
        shift 2 ;;
  -adm)
        admfile=$2
        option=-adm
        shift 2 ;;

      *) 
     option=$1 
     retcde=99
     endProc
  esac


done


## Check input options in case of NON-silent mode

if [ $silent -eq 0 ] ; then
        echo "$ARGV"
fi
        if  [ "x$UserID" = x"" ]  || [  "x$UserID" = "x" ] ; then
                retcde=62
                endProc
        fi

        if  [ "x$GroupN" = x"" ]  || [  "x$GroupN" = "x" ] ; then
                retcde=52
                endProc
        fi

        if  [ "x$cfgfile" = x"" ]  || [  "x$cfgfile" = "x" ] ; then
                retcde=72
                endProc
        elif [ ! -f $cfgfile ] ; then
                retcde=78
                endProc
        fi

##        if  [ "x$plgfile" = x"" ]  || [  "x$plgfile" = "x" ] ; then
##                continue
##                retcde=82
##              endProc
##        elif [ ! -f $plgfile ] ; then
##                retcde=88
##              endProc
##        fi

        if  [ "x$admfile" = x"" ]  || [  "x$admfile" = "x" ] ; then
                retcde=92
                endProc
        elif [ ! -f $admfile ] ; then
                retcde=98
                endProc
        fi

 
## Begining of logic 


        if  [  $create -eq 1  ] ; then
            ## check if Group exsists before creating group
            ## Add Group only if  -create option used 
            chkGroup 
            if [ $? -ne $RC_GROUPEXISTS ] ; then   
                addGroup 
                if [ $? -ne $RC_GROUPEXISTS ] ; then   
                        endProc
                fi
            fi
            ## check if User exists before creating User
            ## Add User only if  -create option used 
            chkUser 
            if [ $? -ne 60 ] ; then   
                addUser 
                if [ $? -ne 60 ] ; then   
                        endProc
                fi
                
            fi
        else
           ## check if Group exsists before file permission change
           ## Group MUST exist because the option  create is no specified 
           chkGroup 
           if [ $? -ne $RC_GROUPEXISTS ] ; then   
              endProc
           fi
           ## check if Group exsists before file permission change
           ## Group MUST exist because the option  create is no specified 
           chkUser 
           if [ $? -ne 60 ] ; then   
              endProc
           fi
        fi
        
        ## Continue process with User and Group created and verified
        chgcfgfile
        if [ $? -gt 70 ] ; then  
              endProc
        elif [ $? -eq 70 ] ; then
              endProc
        fi   

        if  [ "x$plgfile" != x"" ]  || [  "x$plgfile" != "x" ] ; then
                chgplgfile
                if [ $? -gt 80 ] ; then  
                        endProc
                elif [ $? -eq 80 ] ; then
                        endProc
                fi   
        fi

        updadmfiledefault
        updadmfile
        # The child process in the admin server needs to be able to read admin.passwd
        if [ -f "$ADMIN_PASSWD_FILE" ] ; then
            chgrp $GroupN "$ADMIN_PASSWD_FILE" 
            chmod 0640 "$ADMIN_PASSWD_FILE"
        fi        
        endProc




