#!/bin/sh
# COPYRIGHT 1997 Pentasecurity
# ALL RIGHTS RESERVED
#
# This software is the property of Pentasecurity and is furnished under license
# by Pentasecurity. This software may be used only in accordance with the terms
# of said license. This copyright notice may not be removed, modified or
# bliterated without the prior written permission of Pentasecurity.
#
# This software may not be copied, transmitted, provided to or otherwise made
# available to any other person, company, corporation or other entity except as
# specified in the terms of said license.
#
# No right, title, ownership or other interest in the software is hereby
# granted or transferred.
#
# The information contained herein is subject to change without notice and
# should not be construed as a commitment by Pentasecurity.

set -e

WEBCRYPTO_PATH="/usr/share/penta/isignpluswa"

LOCALSERVERD_COMMAND="./LocalServerd"

CONFIG_PATH="/tmp/.penta"
if [ ! -d "$CONFIG_PATH" ]; then
  mkdir "$CONFIG_PATH"
fi

STATUS_FILE="$CONFIG_PATH/isignpluswa.status"
if [ ! -r "$STATUS_FILE" ]; then
  echo "stop" > "$STATUS_FILE"
fi

WEBCRYPTO_PID_FILE="$CONFIG_PATH/isignpluswa.pid"
LOCALSERVERD_PID_FILE="$CONFIG_PATH/LocalServerd.pid"

do_start() {
  UID_MIN=$(grep ^UID_MIN /etc/login.defs | awk '{print $2}')
  UID_MAX=$(grep ^UID_MAX /etc/login.defs | awk '{print $2}')
  USER_LIST=$(cat /etc/passwd | awk -F ':' -v "min=$UID_MIN" -v "max=$UID_MAX" '{ if ( $3>=min && $3<=max ) print $1 }')
  cd $WEBCRYPTO_PATH
  for USER_NAME in $USER_LIST
  do
    USER_HOME=$(grep ${USER_NAME}: /etc/passwd | awk -F ':' '{print $6}')
    ./add_root_ca.sh $USER_HOME
  done
  cd -
  if [ -r "$WEBCRYPTO_PID_FILE" ]; then
    WEBCRYPTO_PID=$(cat "$WEBCRYPTO_PID_FILE")
    if [ -n $WEBCRYPTO_PID ]; then
      sleep 2
      if [ -n "$(ps -e | grep $WEBCRYPTO_PID)" ]; then
        exit 0
      fi
    fi
  fi

  echo $$ > "$WEBCRYPTO_PID_FILE"

  STATUS="start"
  echo $STATUS > "$STATUS_FILE"

  while [ "$STATUS" = "start" ]; do
    if [ -r "$LOCALSERVERD_PID_FILE" ]; then
      LOCALSERVERD_PID=$(cat "$LOCALSERVERD_PID_FILE")
      if [ -n "$LOCALSERVERD_PID" ] && [ -z "$(ps -e | grep $LOCALSERVERD_PID)" ]; then
        LOCALSERVERD_PID=""
      fi
    else
      LOCALSERVERD_PID=""
    fi

    if [ -z "$LOCALSERVERD_PID" ] && [ -d "$WEBCRYPTO_PATH" ]; then
      cd $WEBCRYPTO_PATH
# cis kcmvp for linux - bugfix (segmentation fault occured in SELFTEST_CMAC_ARIA128 if "/tmp/cis_cc" is not exist)
      touch /tmp/cis_cc
      $LOCALSERVERD_COMMAND &
      cd -
      sleep 1
      LOCALSERVERD_PID=$(ps -ef | grep "$LOCALSERVERD_COMMAND" | grep -v grep | awk '{print $2}')
      if [ -n "$LOCALSERVERD_PID" ]; then
        echo $LOCALSERVERD_PID > $LOCALSERVERD_PID_FILE
      fi
    fi

    sleep 2

    STATUS=$(cat "$STATUS_FILE")
  done

  rm -f "$WEBCRYPTO_PID_FILE"
}


do_stop() {
  echo "stop" > "$STATUS_FILE"
  do_kill_node
}

do_kill_node() {
  if [ -r "$LOCALSERVERD_PID_FILE" ]; then
    LOCALSERVERD_PID=$(cat "$LOCALSERVERD_PID_FILE")
    if [ -n "$LOCALSERVERD_PID" ] && [ -n "$(ps -e | grep $LOCALSERVERD_PID)" ]; then
      kill -9 $LOCALSERVERD_PID
      rm -f "$LOCALSERVERD_PID_FILE"
    fi
  fi
}

if [ -z "$1" ]; then
  do_start
  exit 0
else
  case "$1" in
    start)
      do_start
      exit 0
      ;;
    stop)
      do_stop
      exit 0
      ;;
    *)
      exit 3
      ;;
  esac
fi

