#!/bin/sh

CP=`which cp`
CHMOD=`which chmod`
CHOWN=`which chown`
MKDIR=`which mkdir`
SYSTEM=`uname -sr`
SED=`which sed`
LN=`which ln`
RM=`which rm`

USER=osiris
GROUP=${USER}

# ---------------------------------------------------------------------------
# in case this script is not run from make.
# ---------------------------------------------------------------------------

if [ -f ../osirismd/osirismd ]; then
    ${CP} ../osirismd/osirismd .
fi

if [ -f ../osirisd/osirisd ]; then
    ${CP} ../osirisd/osirisd .
fi

if [ -f ../cli/osiris ]; then
    ${CP} ../cli/osiris .
fi

if [ -d ../configs ]; then
    ${CP} -r ../configs .
fi


# ---------------------------------------------------------------------------
# some default install directory.
# ---------------------------------------------------------------------------


INSTALL_DIR="/usr/sbin/"
OSIRIS_DIR="/usr/local/osiris"

echo "Osiris Scanning Daemon Version 2.0.0-release for $SYSTEM"
echo "Copyright (c) 2003 The Shmoo Group. All Rights Reserved."
echo ""
echo "By installing this product you agree that you have read the"
echo "LICENSE file and will comply with its terms. "
echo ""
echo "-------------------------------------------------------------------------"
echo ""

trap 'echo; echo " ! installation aborted! "; exit 1' 1 2 3 15

# ---------------------------------------------------------------------------
# take a peek at the environment before asking user about installation.
# ---------------------------------------------------------------------------

case `uname -s`  in
Linux | OpenBSD)

    EXISTS=0
    ENTRY=`cat /etc/passwd | grep osiris`

    if [ "${ENTRY}" != "" ]; then
        EXISTS=1
    fi

    if [ ${EXISTS} = 0 ]; then
        echo "No Osiris user exists."
    else
        echo "Found existing Osiris user."
    fi

    ;;
FreeBSD)

    EXISTS=0

    if pw user show "${USER}" 2>/dev/null; then
        EXISTS=1
    fi

    if [ ${EXISTS} = 0 ]; then
        echo "No Osiris user exists."
    else
        echo "Found existing Osiris user."
    fi

    ;;
Darwin)

  EXISTS=`nireport / /users name | grep osiris`
  LAST_ID=`nireport / /users uid | grep "5[0123456789][0123456789]" | tail -n 1`
  NEXT_ID=`expr ${LAST_ID} + 1`

  if [ "$EXISTS" = "" ]; then
    echo "No Osiris user or group exists, determined uid/gid is: ${NEXT_ID}"
  else
    echo "Found existing Osiris user."
  fi

    ;;

*)
    ;;
esac

# ---------------------------------------------------------------------------
# prompt user to continue or not.
# ---------------------------------------------------------------------------

prompt="Continue with installation? (y/n)"
printf "%s" "$prompt "
read answer
case "$answer" in
  y* | Y*)
    ;;
  *)
    echo "Osiris installation aborted."
    exit 0
    ;;
esac

# ---------------------------------------------------------------------------
# prompt user for installation directory.
# ---------------------------------------------------------------------------

prompt="Installation directory for binaries: [$INSTALL_DIR]"
printf "%s" "$prompt "

# read and remove any trailing slash.

read answer
INSTALL_DIR=`expr "${INSTALL_DIR}" : '\(.*[^/]\)/*$'`


if [ "$answer" != "" ]; then
    INSTALL_DIR=$answer
fi

if [ ! -d ${INSTALL_DIR} ]; then
    echo "Installation directory doesn't exist, creating."
    ${MKDIR} ${INSTALL_DIR}
fi

if [ ! -d ${OSIRIS_DIR} ]; then
    echo "Osiris root directory doesn't exist, creating."
    ${MKDIR} ${OSIRIS_DIR}
fi

# ---------------------------------------------------------------------------
# copy binaries.
# ---------------------------------------------------------------------------

# CLI management app.

if [ -f ./osiris ]; then
${CP} ./osiris ${INSTALL_DIR}
echo "==> installed osiris CLI: ${INSTALL_DIR}/osiris"
fi

# scanning daemon.

if [ -f ./osirisd ]; then
${CP} ./osirisd ${INSTALL_DIR}
echo "==> installed scanning daemon: ${INSTALL_DIR}/osirisd"
fi

# management daemon.

if [ -f ./osirismd ]; then
${CP} ./osirismd ${INSTALL_DIR}
echo "==> installed management daemon ${INSTALL_DIR}/osirismd"

  # if the configs directory is here, we make sure the
  # osiris root direcotry and default-configs dir exists
  # and then copy over the default configs.
 
  if [ -d ./configs ]; then
      ${MKDIR} -p ${OSIRIS_DIR}/default-configs
      ${CP} -rf ./configs/*.conf ${OSIRIS_DIR}/default-configs 
      echo "==> installed default scan configs."
  fi
fi

# ---------------------------------------------------------------------------
# platform specific copy operations.
# ---------------------------------------------------------------------------

case `uname -s`  in
Linux)

    # first create an osiris user and group if they don't exist.

    if [ ${EXISTS} = 1 ]; then
        echo "==> using existing Osiris user."

    else
        echo "==> creating Osiris user and group with uid/gid ${USERID}."

        # add group, must be first.

        if groupadd ${GROUP}; then
            echo "==> osiris group added."
        else
            echo "==> error: unable to add osiris group!"
            exit 1
        fi

        # add user

        if useradd -g ${GROUP} -d ${OSIRIS_DIR} \
                   -s /sbin/nologin -c "Osiris Daemon" ${USER}
        then
            echo "==> osiris user added."
        else
            echo "==> error: unable to add osiris user!"
            groupdel ${GROUP}
            exit 1
        fi
    fi

    # now install startup stuff. 

    echo "==> installing rc startup for daemon(s)."
    ${SED} "s#@INSTALLDIR@#${INSTALL_DIR}#" < ./linux/osirisd.in > ./linux/osirisd
    ${SED} "s#@INSTALLDIR@#${INSTALL_DIR}#" < ./linux/osirismd.in > ./linux/osirismd

    if [ -d /etc/init.d ]; then
        INIT_D="/etc/init.d"
    elif [ -d /etc/rc.d/init.d ]; then
        INIT_D="/etc/rc.d/init.d"
    elif [ -d /etc/rc.d ]; then
        INIT_D="/etc/rc.d"
    else
        INIT_D=""
    fi

    if [ -d /etc/rc3.d ]; then
        RC_D="/etc"
    elif [ -d /etc/rc.d/rc3.d ]; then
        RC_D="/etc/rc.d"
    elif [ -f "/etc/rc.d/rc.sysvinit" ]; then
        RC_D="/etc/rc.d"

        for runlevel in 3 4 5 ; do
            dir="${RC_D}/rc${runlevel}.d"
            if [ ! -d "$dir" ]; then
                mkdir "$dir"
                chmod 0755 "$dir"
            fi
        done

    else
        RC_D=""
    fi

    if [ -f ./osirisd ]; then
        ${CP} ./linux/osirisd  ${INIT_D}
        ${CHMOD} 555 ${INIT_D}/osirisd

       	${RM} -rf ${RC_D}/rc3.d/S80osirisd
        ${RM} -rf ${RC_D}/rc4.d/S80osirisd
        ${RM} -rf ${RC_D}/rc5.d/S80osirisd

        ${LN} -s ${INIT_D}/osirisd ${RC_D}/rc3.d/S80osirisd
        ${LN} -s ${INIT_D}/osirisd ${RC_D}/rc4.d/S80osirisd
        ${LN} -s ${INIT_D}/osirisd ${RC_D}/rc5.d/S80osirisd
    fi

    if [ -f ./osirismd ]; then
        ${CP} ./linux/osirismd ${INIT_D}
        ${CHMOD} 555 ${INIT_D}/osirismd

        ${RM} -rf ${RC_D}/rc3.d/S80osirismd
        ${RM} -rf ${RC_D}/rc4.d/S80osirismd
        ${RM} -rf ${RC_D}/rc5.d/S80osirismd

        ${LN} -s ${INIT_D}/osirismd ${RC_D}/rc3.d/S80osirismd
        ${LN} -s ${INIT_D}/osirismd ${RC_D}/rc4.d/S80osirismd
        ${LN} -s ${INIT_D}/osirismd ${RC_D}/rc5.d/S80osirismd
    fi

    ;;

OpenBSD)

    # first create an osiris user and group if they don't exist.

    if [ ${EXISTS} = 1 ]; then
        echo "==> using existing Osiris user."

    else
        echo "==> creating Osiris user and group with uid/gid ${USERID}."

        # add group, must be first.

        if groupadd ${GROUP}; then
            echo "==> osiris group added."
        else
            echo "==> error: unable to add osiris group!"
            exit 1
        fi

        # add user

        if useradd -g ${GROUP} -d ${OSIRIS_DIR} \
                   -s /sbin/nologin -c "Osiris Daemon" ${USER}
        then
            echo "==> osiris user added."
        else
            echo "==> error: unable to add osiris user!"
            groupdel ${GROUP}
            exit 1
        fi
    fi

    OSIRISD_CONF_ENTRY="# Osiris scanning daemon\nif [ X\"\${osirisd}\" == X\"YES\" -a -x ${INSTALL_DIR}/osirisd ]; then\n    echo -n \" osirisd\"; ${INSTALL_DIR}/osirisd\nfi"

    OSIRISMD_CONF_ENTRY="# Osiris management daemon\nif [ X\"\${osirismd}\" == X\"YES\" -a -x ${INSTALL_DIR}/osirismd ]; then\n    echo -n \" osirismd\"; ${INSTALL_DIR}/osirismd\nfi"


    if [ ! -f /etc/rc.conf.local ]; then
        echo "==> creating /etc/rc.conf.local"
        touch /etc/rc.conf.local
        chmod 644 /etc/rc.conf.local
    fi

    if [ ! -f /etc/rc.conf.local ]; then
        echo "unable to create /etc/rc.conf.local!!"
        exit 1
    fi

    if [ -f ./osirisd ]; then

        OSIRISD_ENTRY=`cat /etc/rc.conf.local | grep osirisd`

        if [ "${OSIRISD_ENTRY}" = "" ]; then
            echo "==> adding osirisd to /etc/rc.conf.local"
            echo "osirisd=YES" >> /etc/rc.conf.local
        else
            echo "::osirismd already in /etc/rc.conf.local"
        fi

        OSIRISD_ENTRY=`cat /etc/rc.local | grep osirisd`

        if [ "${OSIRISD_ENTRY}" = "" ]; then
            echo "==> adding osirisd to /etc/rc.local"
            echo "${OSIRISD_CONF_ENTRY}" >> /etc/rc.local
        else
            echo "==> osirisd already in rc.local"
        fi
    fi

    if [ -f ./osirismd ]; then

        OSIRISMD_ENTRY=`cat /etc/rc.conf.local | grep osirismd`

        if [ "${OSIRISMD_ENTRY}" = "" ]; then
            echo "==> adding osirismd to /etc/rc.conf.local"
            echo "osirismd=YES" >> /etc/rc.conf.local
        else
            echo "::osirismd already in /etc/rc.conf.local"
        fi

        OSIRISMD_ENTRY=`cat /etc/rc.local | grep osirismd`

        if [ "${OSIRISMD_ENTRY}" = "" ]; then
            echo "==> adding osirismd to /etc/rc.local"
            echo "${OSIRISMD_CONF_ENTRY}" >> /etc/rc.local
        else
            echo "::osirismd already in rc.local"
        fi
    fi

    ;;
FreeBSD)

    # first create an osiris user and group if they don't exist.

    if [ ${EXISTS} = 1 ]; then
        echo "==> using existing Osiris user."

    else
        echo "==> creating Osiris user and group with uid/gid ${UID}."

        # add group, must be first.

        if pw groupadd ${GROUP}; then
            echo "==> osiris group added."
        else
            echo "error: unable to add osiris group!"
            exit 1
        fi

        # add user.

        if pw useradd ${USER} -g ${GROUP} -h - \
            -d ${OSIRIS_DIR} -s /sbin/nologin -c "Osiris Daemon"
        then
            echo "==> osiris user added."
        else
            echo "error: unable to add osiris user!"
            groupdel ${GROUP}
            exit 1 
        fi
    fi

    # now install startup stuff.

    echo "==> installing rc startup for daemon(s)."

    ${SED} "s#@INSTALLDIR@#${INSTALL_DIR}#" < ./freebsd/osirisd.in > ./freebsd/osirisd.sh
    ${SED} "s#@INSTALLDIR@#${INSTALL_DIR}#" < ./freebsd/osirismd.in > ./freebsd/osirismd.sh

    if [ -f ./osirisd ]; then
        ${CP} ./freebsd/osirisd.sh /usr/local/etc/rc.d/
        ${CHMOD} 555 /usr/local/etc/rc.d/osirisd.sh
        echo "==> installed rc startup script: /usr/local/etc/rc.d/osirisd.sh"

        EXISTS=`grep osirisd /etc/rc.conf`        
        if [ "${EXISTS}" = "" ]; then
            echo "osirisd_enable=\"YES\"" >> /etc/rc.conf   
            echo "==> updated: /etc/rc.conf --> osirisd_enable=YES"
        fi
    fi

    if [ -f ./osirismd ]; then
        ${CP} ./freebsd/osirismd.sh /usr/local/etc/rc.d/
        ${CHMOD} 555 /usr/local/etc/rc.d/osirismd.sh 
        echo "==> installed rc startup script: /usr/local/etc/rc.d/osirismd.sh"

        EXISTS=`grep osirismd /etc/rc.conf`
        if [ "${EXISTS}" = "" ]; then
            echo "osirismd_enable=\"YES\"" >> /etc/rc.conf
            echo "==> updated: /etc/rc.conf --> osirismd_enable=YES"
        fi
    fi

    ;;
Darwin)

    # first create an osiris user and group if they don't exist.

    if [ "$EXISTS" != "" ]; then
        echo "==> using existing Osiris user."
    else
        echo "==> creating Osiris user and group with uid/gid ${NEXT_ID}."

    niutil -create / /users/osiris
    niutil -createprop / /users/osiris uid ${NEXT_ID}
    niutil -createprop / /users/osiris realname "Osiris IDS User"
    niutil -createprop / /users/osiris shell "/dev/null"
    niutil -createprop / /users/osiris gid ${NEXT_ID}
    niutil -createprop / /users/osiris passwd "*" 
    niutil -createprop / /users/osiris _writers_passwd osiris
    niutil -createprop / /users/osiris home /dev/null
    niutil -createprop / /users/osiris expire 0
    niutil -createprop / /users/osiris change 0

    niutil -create / /groups/osiris
    niutil -createprop / /groups/osiris gid ${NEXT_ID}

    fi  # if osiris user doesn't exist.

    # now add to hostconfig file if not there.

    EXISTS=`grep OSIRISSERVER /etc/hostconfig`
    if [ "${EXISTS}" = "" ]; then
        echo "OSIRISSERVER=-YES-" >> /etc/hostconfig
        echo "==> updated: /etc/hostconfig --> OSIRISSERVER=-YES-"
    fi

    # now create the startup item and install it.

    echo "==> installing StartupItem for the osiris scanning daemon."

    ${SED} "s#@INSTALLDIR@#${INSTALL_DIR}#" < ./darwin/Osiris.in > ./darwin/Osiris/Osiris
    ${CP} -rf ./darwin/Osiris /System/Library/StartupItems/
    ${CHMOD} 755 /System/Library/StartupItems/Osiris/Osiris
    echo "==> installed /System/Library/StartupItems/Osiris/Osiris script."

    ;;
*)
    echo "unrecognized host type, no startup options installed for "
    echo "the osiris scanning daemon, you will need to set this up "
    echo "by hand."
    ;;
esac

# ---------------------------------------------------------------------------
# verify permissions
# ---------------------------------------------------------------------------

${CHOWN} -R ${USER}:${GROUP} ${OSIRIS_DIR}

if [ -f ${INSTALL_DIR}/osiris ]; then
    ${CHOWN} root:wheel ${INSTALL_DIR}/osiris
    ${CHMOD} 755 ${INSTALL_DIR}/osiris
    echo "==> change owner and  permissions on ${INSTALL_DIR}/osiris"
    ls -la ${INSTALL_DIR}/osiris
fi

if [ -f ${INSTALL_DIR}/osirisd ]; then
    ${CHOWN} root:wheel ${INSTALL_DIR}/osirisd
    ${CHMOD} 755 ${INSTALL_DIR}/osirisd
    echo "==> change owner and permissions on ${INSTALL_DIR}/osirisd"
    ls -la ${INSTALL_DIR}/osirisd
fi

if [ -f ${INSTALL_DIR}/osirismd ]; then
    ${CHOWN} osiris:osiris ${INSTALL_DIR}/osirismd
    ${CHMOD} 4755 ${INSTALL_DIR}/osirismd
    echo "==> change owner permissions on ${INSTALL_DIR}/osirismd"
    ls -la ${INSTALL_DIR}/osirismd
fi

# now give some instructions on how to start daemons for those that need it.

echo ""
echo "=================================================================="
echo "Osiris has been installed, but is not currently running but will"
echo "be started upon boot, or using the following command(s):"
echo ""

if [ -f ${INSTALL_DIR}/osirismd ]; then
    echo "To start the management daemon:"
    echo "     ${INSTALL_DIR}/osirismd "
    echo ""
fi

echo "To start the scanning daemon:"
echo "    sudo ${INSTALL_DIR}/osirisd"
echo ""

echo "Documentation is included with this source and available online at:"
echo "    http://osiris.shmoo.com/docs"
echo ""
echo ""
echo "(c) 2003 - The Shmoo Group"
echo ""

exit 0
