#!/bin/sh 
# renew a request using only grid-proxy-init and ssleay
#
# There is one parameter, the Challenage 
# we sent you in the E-Mail
#

# NEED TO EDIT THE FOLLOWING
#
GRID_PROXY_INIT=/npac/home/globus/globus_1.0/tools/sparc-sun-solaris2.5.1/bin/globus-proxy-init
GRID_PROXY_DESTROY=/npac/home/globus/globus_1.0/tools/sparc-sun-solaris2.5.1/bin/globus-proxy-destroy
SSLEAY=/npac/home/globus/ssl/bin/ssleay
TMPPROXY=/tmp/tmpp.$$
RENEWALKEY=$HOME/.globus/renewalkey.pem
RENEWALREQ=$HOME/.globus/renewalrequest.pem
CERTFILE=$HOME/.globus/usercert.pem
KEYFILE=$HOME/.globus/userkey.pem
GATEKEEPER=no
# For a gatekeeper they should look something like this:
#
# DEPLOY=/opt/local/globus
# RENEWALKEY=$DEPLOY/etc/renewalkey-fork.pem
# RENEWALREQ=$DEPLOY/etc/renewalrequest-fork.pem
# CERTFILE=$DEPLOY/etc/globus-gatekeeper-fork.cert
# KEYFILE=$DEPLOY/etc/globus-gatekeeper-fork.key
# GATEKEEPER=yes
#
# END OF EDITING 

enckey=yes
if [ "$GATEKEEPER" = "yes" ] ; then
        enckey=no
        echo "creating new key ..."
        $GRID_PROXY_INIT -hours 1 -bits 1024 -out $TMPPROXY \
                -cert $CERTFILE -key $KEYFILE
        $SSLEAY rsa -in $TMPPROXY -out $RENEWALKEY
else

#       generate a proxy, and keypair from current cert and key

        echo "Using your current passphrase ..."
        $GRID_PROXY_INIT -hours 1 -bits 1024 -out $TMPPROXY \
                -cert $CERTFILE -key $KEYFILE

#       extract the private key, encrypt it in new passphrase
#       and save it as new user key

        echo "Using a new passphrase which needs to be entered twice ..."
        $SSLEAY rsa -des3 -in $TMPPROXY -out $RENEWALKEY

fi

#Save some info 
echo `pwd` > $RENEWALREQ

#copy proxy cert signed by user

$SSLEAY x509 -in $TMPPROXY >> $RENEWALREQ

# generate a digest which can not be copied 

$SSLEAY x509 -in $TMPPROXY > $TMPPROXY.d
echo X$1 >> $TMPPROXY.d
$SSLEAY dgst < $TMPPROXY.d >> $RENEWALREQ
rm $TMPPROXY.d

#generate a cert req signed by the new key. 

echo Using the new pass phrase again ...
$SSLEAY x509 -in $TMPPROXY -x509toreq \
         -signkey $RENEWALKEY  >> $RENEWALREQ

$GRID_PROXY_DESTROY $TMPPROXY

echo New key is in $RENEWALKEY
echo " " 
echo Mail $RENEWALREQ to ca@globus.org
echo from the same account you received this request. 

# mail the renewalrequest.pem to the CA. 

