#!/bin/sh
#
# Copyright C 2002-2014 by Isilon Systems, LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

# PROVIDE: isi_first_update
# REQUIRE: isi_first_boot isi_hal_init

if [ -f /etc/ifs/isi_first_update_already_ran ]; then
    exit
fi

isi_upgrade_post_ifs_log -l "Inform" "====== Started Executing script: $0 ======"

touch /etc/ifs/isi_first_update_already_ran
chmod 640 /etc/ifs/isi_first_update_already_ran

# Create a .ifsvar file in /ifs root filesystem, so no programs can
# accidently create a local .ifsvar directly and populate it.
touch /ifs/.ifsvar

# Recover any newer saved journal metadata from the old root
LKGMOUNT=/usr/update/root

if ! [ -d $LKGMOUNT ]; then
  /bin/mkdir -p $LKGMOUNT
fi

dataormissing() {
    if [ -f "$*" ]; then
        retvalue=`cat "$*"`
    else
        retvalue="No file"
    fi
}

bootdisk_type=`isi_bootdisk_type`
if [ ${bootdisk_type} != "gmirror" ]; then
  backup="/dev/imdd3a"
else
  backup="/dev/mirror/journal-backup"
fi
if /sbin/mount $backup $LKGMOUNT; then
  backupvalid=`/usr/bin/python -c "import isi.fs.journal as jlib; print jlib.FILES['backup_valid']"`
  backupserial=`/usr/bin/python -c "import isi.fs.journal as jlib; print jlib.FILES['backup_serial']"`
  if [ "X${backupvalid}X" = "XX" ]; then
      backupvalid=/etc/ifs/journal_backup_valid
  fi
  if [ "X${backupserial}X" = "XX" ]; then
      backupserial=/etc/ifs/journal_backup_serial
  fi
  # 
  # This is only important if there's a hardware fault requiring the
  # journal backup.  However, it's very dangerous to get this wrong.
  # As sanity checks to make sure we're doing this properly, we make
  # sure that:
  #   * the files have the same contents
  #   * the *valid file was written before the *serial file 
  #   * neither of the files on the current root are newer than the
  #     ones on the LKG root.
  #
  /usr/bin/cmp -s $LKGMOUNT/$backupserial $LKGMOUNT/$backupvalid
  # Another sanity check: *valid should have been written before
  # the journal backup, and *serial written after.
  if [ $? -eq 0 ] && \
     ! [ $LKGMOUNT/$backupserial -nt $LKGMOUNT/$backupvalid ] && \
     [ \( ! -f $backupserial \) -o \( $backupserial -ot $LKGMOUNT/$backupserial \) ] && \
     [ \( ! -f $backupvalid \) -o \( $backupvalid -ot $LKGMOUNT/$backupvalid \) ]; then
     dataormissing $backupvalid
     backupvaliddata=$retvalue
     dataormissing $backupserial
     backupserialdata=$retvalue
     # Somewhat redundant; we've already tested their existence
     dataormissing $LKGMOUNT/$backupvalid
     lkgbackupvaliddata=$retvalue
     dataormissing $LKGMOUNT/$backupserial
     lkgbackupserialdata=$retvalue
     /usr/bin/logger -i -p daemon.notice <<LOGGING
Recovering journal metadata from LKG
$backupvalid contents: $backupvaliddata
$backupserial contents: $backupserialdata
LKG $backupvalid contents: $lkgbackupvaliddata
LKG $backupserial contents: $lkgbackupserialdata
LOGGING
     /bin/cp -f $LKGMOUNT/$backupserial $backupserial
     /bin/cp -f $LKGMOUNT/$backupvalid $backupvalid
  fi
  /sbin/umount $backup
fi

if [ -f /etc/mcp/override/crontab.advquotas ]; then
    /bin/rm -f /etc/mcp/override/crontab.advquotas
fi

if [ -f /usr/sbin/isi_ssh_keys_cleanup ]; then
    /usr/sbin/isi_ssh_keys_cleanup
fi

# Update sshd_config to accomodate OpenSSH 7.0+ upgrade
if [ -f /usr/libdata/patchmgr/sshd-upgrade.sh ]; then
    isi_upgrade_post_ifs_log -l "Inform" "Start Executing: /usr/libdata/patchmgr/sshd-upgrade.sh"
    /usr/libdata/patchmgr/sshd-upgrade.sh
    if [ $? -eq 0 ]; then
        isi_upgrade_post_ifs_log -l "Inform" "Successfully Executed /usr/libdata/patchmgr/sshd-upgrade.sh"
    else
        isi_upgrade_post_ifs_log -l "Error" "Error while Executing /usr/libdata/patchmgr/sshd-upgrade.sh"
    fi
fi


# Regenerate /etc/nsswitch.conf and /usr/local/etc/ldap.conf based on
# preserved LDAP settings.
/etc/mcp/scripts/generic_override.py /etc/mcp/sys/ldap.xml /etc/mcp/override/ldap.xml /usr/local/etc/ldap.conf
/etc/mcp/scripts/ldap.py /etc/mcp/sys/ldap.xml /etc/mcp/override/ldap.xml

# cleanup patch meta data
rm -f /+INSTALL
rm -f /+POST-INSTALL
rm -f /+REQUIRE
rm -f /+CONTENTS
rm -f /+COMMENT
rm -f /+DESC

if [ -f /etc/ifs/array.xml ]; then
    /usr/bin/python -c '
import isi.sys.version as ver
ns = ver.Node_Status()
ns.update_versions()
'
    # Flag to communicate to init_smartpools that this cluster was upgraded
    # to Habanero (as opposed to being newly configured at Habanero)
    /usr/bin/touch /etc/ifs/upgraded_to_habanero
    chmod 640 /etc/ifs/upgraded_to_habanero

    if [ -f /etc/mcp/scripts/templated_override.py ]; then
        /etc/mcp/scripts/templated_override.py /etc/mcp/templates/crontab /etc/mcp/override/crontab.advquotas /etc/local/crontab.local /etc/crontab
    fi
else
    /usr/bin/python -c '
import isi.sys.version as ver
ns = ver.Node_Status()
ns.write_current_version()
'
fi

# Bug 43322: Remove any existing /var/log/httpd/ui-ssl_request_log* files
/bin/rm -f /var/log/httpd/ui-ssl_request_log*

# bug 45924/30001: update any conf files (including sysctl and syslog confs)
if [ -f /usr/sbin/isi_mcp ]; then
    /usr/sbin/isi_mcp -u
fi

# Remove the old adcache used pre-65.
if [ -f /var/lib/likewise/db/lsass-adcache.db ]; then
    sqlite3 /var/lib/likewise/db/lsass-adcache.db .schema | grep "lwiusers6"
    if [ $? != 0 ]; then
        rm -f /var/lib/likewise/db/lsass-adcache.db
    fi
fi

# Create IsilonSD User & GROUP only if Alcor Node
# This user is used by Software Defined OneFS Management Server to make 
# PAPI commands to virtual OneFS nodes.
# UGID 16 IS NOW RESERVED FOR IsilonSD Management Server SYS User
ISISD_USER=isdmgmt
ISISD_GROUP=isdmgmt
ISISD_UGID=16

if [ -f /etc/isilon_serial_number ]; then
    ALCOR_SERIAL=`cat /etc/isilon_serial_number`
    case "$ALCOR_SERIAL" in
    "SV100"*)
        if pw group show "${ISISD_GROUP}" >/dev/null 2>&1; then
            echo "Using existing group \"${ISISD_GROUP}\"."
        else
            echo "Creating group \"${ISISD_GROUP}\", (gid: ${ISISD_UGID})."
            pw group add ${ISISD_GROUP} -g ${ISISD_UGID}
            if [ $? != 0 ]; then
                /usr/bin/logger -i -p user.err "Failed to add IsilonSD group \"${ISISD_GROUP}\"."
            fi
        fi

        if pw user show "${ISISD_USER}" >/dev/null 2>&1; then
            echo "Using existing user \"${ISISD_USER}\"."
        else
            echo "Creating user \"${ISISD_USER}\", (uid: ${ISISD_UGID})."
            pw user add  -n ${ISISD_USER} -u ${ISISD_UGID} -g ${ISISD_GROUP} -h - -d /ifs/home/isdmgmt -s /sbin/nologin -c "IsilonSD User"
            if [ $? != 0 ]; then
                /usr/bin/logger -i -p user.err "Failed to add IsilonSD user \"${ISISD_USER}\"."
            fi
        fi
    ;;
    esac
fi

# Create user to run LLDPD
# This was added as a workaround for nodes upgrading from pre-FT to FT and
# can be removed as of the release when pre-FT upgrades are no longer supported
LLDPD_USER=_lldpd
LLDPD_GROUP=_lldpd
LLDPD_UGID=978

if pw group show "${LLDPD_GROUP}" >/dev/null 2>&1; then
    echo "Using existing group \"${LLDPD_GROUP}\"."
else
    echo "Creating group \"${LLDPD_GROUP}\", GID: ${LLDPD_UGID}."
    pw group add ${LLDPD_GROUP} -g ${LLDPD_UGID}
    if [ $? != 0 ]; then
        /usr/bin/logger -i -p user.err "Failed to add LLDPD group \"${LLDPD_GROUP}\"."
    fi
fi

if pw user show "${LLDPD_USER}" >/dev/null 2>&1; then
    echo "Using existing user \"${LLDPD_USER}\"."
else
    echo "Creating user \"${LLDPD_USER}\", UID: ${LLDPD_UGID}."
    pw user add ${LLDPD_USER} -g ${LLDPD_UGID} -d /nonexistent -s /usr/sbin/nologin
    if [ $? != 0 ]; then
        /usr/bin/logger -i -p user.err "Failed to add LLDPD user \"${LLDPD_USER}\"."
    fi
fi


# ConnectEMC requires these directories to function
mkdir -p /var/crash/connectemc/archive
mkdir -p /var/crash/connectemc/failed
mkdir -p /var/crash/connectemc/history
mkdir -p /var/crash/connectemc/output
mkdir -p /var/crash/connectemc/poll
mkdir -p /var/crash/connectemc/queue
mkdir -p /var/crash/connectemc/recycle
mkdir -p /var/crash/connectemc/tmp

# See Bug 171163
# Create cemc_recycle.txt to complete the symlink to /usr/local/connectemc/recycle.txt
touch /var/crash/connectemc/cemc_recycle.txt

# Setup the owner of the remote support shell
REMOTE_SUPPORT_UGID=12
REMOTE_SUPPORT_SHELL=/usr/bin/isi_remote_support_shell
chown ${REMOTE_SUPPORT_UGID} ${REMOTE_SUPPORT_SHELL}

# Upgrade /etc/ttys due to device rename in FreeBSD 8
sed -i '' -e 's/^ttyd/ttyu/' /etc/ttys

# Bug 181669: CVE-2015-5677 harden /etc/snmpd.config permissions.
chmod 0600 /etc/snmpd.config

isi_upgrade_post_ifs_log -l "Inform" "====== Successfully finished Executing script: $0 ========"
