#!/bin/sh 
#
# Copyright 1999 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)install_UDH_keys.sh	3.7 09/30/99 Sun Microsystems, Inc.
#
# Script for setting up the UDH keys from either the installer
# (Web Start Wizard) or the script ss_install. 
#
# When executed from the wizard (with the -w flag), the script
# doesn't print the messages and doesn't expect input.  The admin
# certificate key ID is entered in the command line and the screen
# certificate key ID is echoed in the end.
#
# When executed from ss_install, the admin and screen certificate
# key IDs are stored in temporary files for ss_install to read.
#
# BugID 4300645: installer does not display SKIP key already installed
#
# Exit Codes:
#    0       success
#    1       no slot for screen key
#    2       screen key not found
#

#
# BugID 4210826: installer doesn't properly config remote admin
#
PATH="/usr/bin:/usr/sbin"
export PATH

#
# BugID 4272397: some messages for skiplocal must not be localized
#
LC_MESSAGES=C
export LC_MESSAGES

wizard=false
msg() {
  if [ "$wizard" = "false" ]
  then echo "$*"
  fi
}

set_num=136     # SI18N_INST_UDH_KEYS
LS()		# localize a string
{
	msg_num=$1; shift
        if [ -x ${SS_LIBDIR}/catgets ]
        then ${SS_LIBDIR}/catgets $set_num $msg_num "$*"
        else echo "$*"
	fi
}

#---Main---

ADMINCERTFILE=/tmp/admincert
SCREENCERTFILE=/tmp/screencert

if [ $# = "2" ] && [ $1 = "-w" ]
then
  wizard=true
  ADMIN_CERT="$2"
fi
#
# create a local UDH certificate
#
if [ "$wizard" = "false" ]
then
  fmt=`LS 10 "Generating Screen's key."`
  printf "\n$fmt\n"
fi
#
# BugID 4210826: installer doesn't properly config remote admin
#
skiplocal -i

SLOT=`skiplocal -k -f | nawk '{ if ($1 == "Added") print $5 }'`

#
# BugID 4300645: installer does not display SKIP key already installed
#
if [ "$SLOT" = "" ]
then
  fmt=`LS 40 "Local SKIP key not generated"`
  printf "$fmt\n"
  exit 1
fi

SCREEN_CERT=`skiplocal -l -V | sed -e 's/=/ /g' | nawk -v SLOT=$SLOT '{ if ($2 == SLOT) print $8}'`

#
# BugID 4300645: installer does not display SKIP key already installed
#
if [ "$SCREEN_CERT" = "" ]
then
  fmt=`LS 41 "Local SKIP certificate not found"`
  printf "$fmt\n"
  exit 2
fi

if [ "$wizard" = "false" ]
then
  #
  # display the screen's key name (nsid)
  #
  fmt=`LS 11 "The Screen's certificate ID is 0x%s"`
  printf "$fmt\n" $SCREEN_CERT

  #
  # load the admin station's key name (nsid)
  #
  fmt=`LS 20 "Enter the name/hash of the administration station's unsigned Diffie-Hellman"`
  printf "$fmt\n"
  fmt=`LS 21 "key certificate: (do not include leading 0x)"`
  printf "$fmt\n"

  read ADMIN_CERT

  fmt=`LS 22 "The Administration station's certificate ID is 0x%s"`
  printf "$fmt\n" $ADMIN_CERT
fi
  
# restart the skipd process to incorporate new keys, then sleep to
# to leave enough time for the restart.
skipd_restart

msg `LS 30 "Please wait while the SKIP key manager is restarted."`
sleep 15

if [ "$wizard" = "true" ]
then
  echo $SCREEN_CERT
else
  echo $ADMIN_CERT > $ADMINCERTFILE
  echo $SCREEN_CERT > $SCREENCERTFILE
fi

exit 0
