#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004. 
# See http://www.eu-egee.org/partners/ for details on the copyright 
# holders.  
#
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at 
#
#    http://www.apache.org/licenses/LICENSE-2.0 
#
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS 
# OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License.
##############################################################################
#
# NAME :        config_vomsdir
#
# DESCRIPTION : This function configures the vomsdir according to what it is
#               written in the voms-core user's guide
#               https://edms.cern.ch/file/571991/1/voms-guide.pdf.
#
# AUTHORS :     yaim-contact@cern.ch
#
# NOTES :       This function exists because of bug #29906.
#
# YAIM MODULE:  glite-yaim-core
#                 
##############################################################################


config_vomsdir_check () {
ret=0
   requires $1 VOS VO__VOMS_CA_DN VO__VOMSES
   let "ret |= $?"

   for VO in $VOS; do
    vo_low=`echo ${VO} | tr '[:upper:]' '[:lower:]'`
    voms_ca_dn=`get_vo_param ${VO} VOMS_CA_DN`
    if [ -n "${voms_ca_dn}" ]; then
      vomses=`get_vo_param ${VO} VOMSES`
      i=0
      while read vo1 server port rest; do
        i=`expr $i + 1`
      done < <(split_quoted_variable $vomses)
      vomses_count=$i
 #    yaimlog DEBUG "VOMSES_COUNT = $vomses_count "
      voms_ca_dn=`get_vo_param ${VO} VOMS_CA_DN`
      j=0
      while read line; do
        if [ -z "$line" ]; then break; fi
        j=`expr $j + 1`
      done < <(split_quoted_variable $voms_ca_dn)
      voms_ca_dn_count=$j
  #    yaimlog DEBUG "VOMS_CA_DN_COUNT = $voms_ca_dn_count "
 
      if [ $vomses_count -ne $voms_ca_dn_count ]; then
        yaimlog ERROR "ERROR for VO '$VO': VOMSES has $vomses_count values, while"
        yaimlog ERROR "ERROR for VO '$VO': VOMS_CA_DN has $voms_ca_dn_count values"
        yestr ${YEX_CONFIG}
        yaimlog ERROR "${YERRORSTR}"
        let "ret |= 1"
      fi
    fi
done
 
 return $ret

}

config_vomsdir_setenv () {

yaimlog DEBUG "This function currently doesn't set any environment variables."

}


config_vomsdir () {

X509_VOMS_DIR=${X509_VOMS_DIR:-/etc/grid-security/vomsdir/}

if [ ! -d ${X509_VOMS_DIR} ]; then
  ####@ Create the X509_VOMS_DIR directory 
  yaimlog INFO "Create the ${X509_VOMS_DIR} directory"
  mkdir -p ${X509_VOMS_DIR}
  chmod 755 ${X509_VOMS_DIR}
fi


cd ${X509_VOMS_DIR}
 
for VO in $VOS; do
  vo_low=`echo ${VO} | tr '[:upper:]' '[:lower:]'`
  voms_ca_dn=`get_vo_param ${VO} VOMS_CA_DN`
  if [ -n "${voms_ca_dn}" ]; then
    yaimlog DEBUG ".lsc file configuration for VO ${VO}..." 
    if [ ! -d ${vo_low} ] ; then
      ####@ Create the X509_VOMS_DIR/<vo_name> directory
      yaimlog DEBUG "Create the ${X509_VOMS_DIR}${vo_low} directory"
      mkdir -p ${vo_low}
      chmod 755 ${vo_low}
    fi
    cd ${vo_low}
    vomses=`get_vo_param ${VO} VOMSES`
    i=0
    while read vo1 server port rest; do
      dn=${rest% *}
      rfc=${rest##* }
      my_host[$i]=$server
      if [ $rfc != $vo1 ]; then     # globus version specified
        dn=${dn% *}
      fi
      if [ ! -f ${my_host[i]}.lsc ] ; then
        rm -rf ${my_host[i]}.lsc
      fi
      ####@ Create the lsc file
      yaimlog DEBUG "Create the ${X509_VOMS_DIR}${VO}/${my_host[i]}.lsc"
      touch ${my_host[i]}.lsc
      chmod 644 ${my_host[i]}.lsc  
      ####@ Copying the VOMS server my_host certificate DN in the lsc file
      yaimlog DEBUG "Copying ${dn} in ${X509_VOMS_DIR}${VO}/${my_host[i]}.lsc"
      echo ${dn} > ${my_host[i]}.lsc 
      i=`expr $i + 1`
    done < <(split_quoted_variable $vomses)

    voms_ca_dn=`get_vo_param ${VO} VOMS_CA_DN`
    i=0
    while read line; do
      if [ -z "$line" ]; then break; fi
      set `echo $line`
      unset ca_dn
      while [ $# -ge 1 ]; do
        ca_dn="$ca_dn $1"
        shift
      done
      ####@ Copying the VOMS server issuer CA DN in the lsc file
      yaimlog DEBUG "Copying the ${my_host[i]} CA DN ${ca_dn} in ${X509_VOMS_DIR}${VO}/${my_host[i]}.lsc"
      echo ${ca_dn} >> ${my_host[i]}.lsc
      i=`expr $i + 1`
    done < <(split_quoted_variable $voms_ca_dn)
    cd ..
  fi
done

return 0

}
