#!/bin/sh
########################################################################
# 
#   Copyright (C) 2006 Novell, Inc. All Rights Reserved.
# 
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; version 2.1
#   of the License.
# 
#   This library 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
#   Library Lesser General Public License for more details.
# 
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, Novell, Inc.
#  
#   To contact Novell about this file by physical or electronic mail, 
#   you may find current contact information at www.novell.com.
#  
#   Author: Juan Carlos Luciani <jluciani@novell.com>
#   
########################################################################

#############################################################
#                                                           #
# CASA Authentication Token Script for refreshing the       #
# trusted ATS keystore.                                     #
#                                                           #
# This script sets up the certificate associated with the   #
# keys used by the ATS to sign authentication tokens in the #
# keystore utilized by token validating clients.            #
#                                                           #
#############################################################

if [ -d /usr/lib64/jvm/jre ]; then
   LIB=lib64
else
   LIB=lib
fi

JAVA_HOME=/usr/$LIB/jvm/jre
KEYTOOL_PATH=$JAVA_HOME/bin/keytool
KEYSTORE_PATH=/etc/CASA/authtoken/keys/trusted-ats-jks-store
LOCAL_ATS_SIGNING_CERT_PATH=/etc/CASA/authtoken/keys/localSigningCert
NON_LOCAL_ATS_SIGNING_CERT_DIR_PATH=/etc/CASA/authtoken/keys/trustedATSCerts

# Remove the keystore if present
rm -f $KEYSTORE_PATH

// Import the SigningCert from the local ATS if present
if [ -f $LOCAL_ATS_SIGNING_CERT_PATH ]; then
   echo "Importing local ATS cert"
   $KEYTOOL_PATH -import -noprompt -keystore $KEYSTORE_PATH -alias local_signingCert -storepass secret -keypass secret -file $LOCAL_ATS_SIGNING_CERT_PATH
fi

// Import the SigningCert of the non-local trusted ATSs
if [ -d $NON_LOCAL_ATS_SIGNING_CERT_DIR_PATH ]; then
   for ATSCert in `ls $NON_LOCAL_ATS_SIGNING_CERT_DIR_PATH`
   do
      echo "Importing $ATSCert cert"
      $KEYTOOL_PATH -import -noprompt -keystore $KEYSTORE_PATH -alias $ATSCert -storepass secret -keypass secret -file $NON_LOCAL_ATS_SIGNING_CERT_DIR_PATH/$ATSCert
   done
fi

# List the content's of the trusted ATS keystore
#$KEYTOOL_PATH -list -rfc -keystore $KEYSTORE_PATH -storepass secret

