#! /bin/bash # Usage: iron client # Summary: manage CSR for certificates creation # Help: This command groups commands used to setup config create delete a CA APPNAME="iron" CURRDIR=$(pwd) RCDIR=$HOME/.$APPNAME test -d $RCDIR || mkdir -p $RCDIR CURRENT_TSTAMP=$(date '+%Y%m%d%H%M') function client_create_csr() { DOMAIN=$1 client=$2 CLIENT_CONFIG_DIR="$RCDIR/${DOMAIN}/conf" CLIENT_CONFIG_FILE=${CLIENT_CONFIG_DIR}/client-${client}.conf test -f $CLIENT_CONFIG_FILE || (echo "missing client config file"; exit -1) CP=$CLIENT_CERTPATH/$client mkdir -p $CP openssl genrsa -out $CP/$client.key 2048 openssl req -new -key $CP/$client.key -out $CP/$client.csr -config $CLIENT_CONFIG_FILE } function client_home_setup_delete() { DOMAIN=$1 client=$2 echo "About to DELETE cert path: [$RCDIR/${DOMAIN}] are you sure? y/n" read confirmation if [ "$confirmation" = "y" ]; then (rm -fr $RCDIR/${DOMAIN}) && echo "CA DELETED" else echo "SKIPPING" exit -1 fi } function client_export_bundle() { DOMAIN=$1 client=$2 CAPATH=$RCDIR/CA CLIENT_CERTPATH=$RCDIR/${DOMAIN}/certs target_dir="cert-$client" mkdir -p $target_dir/CA mkdir -p $target_dir/$DOMAIN mkdir -p $target_dir/$DOMAIN/certs/$client cp $CAPATH/ca.crt $target_dir/CA cp $CLIENT_CERTPATH/$client/$client.crt $target_dir/$DOMAIN/certs/$client openssl rsa -aes256 -in $CLIENT_CERTPATH/$client/$client.key --out $target_dir/$DOMAIN/certs/$client/$client.key tar cvfz $target_dir.tar.gz $target_dir && rm -fr $target_dir } function client_home_setup_write() { DOMAIN=$1 client=$2 CAPATH=$RCDIR/CA CLIENT_CERTPATH=$RCDIR/${DOMAIN}/certs CLIENT_CONFIG_DIR="$RCDIR/${DOMAIN}/conf" test -d $CLIENT_CONFIG_DIR || mkdir -p $CLIENT_CONFIG_DIR test -d $CLIENT_CERTPATH/client/$client && ( echo "Dir exist, please delete before" && exit -1) test -d $CLIENT_CERTPATH/client/$client || mkdir -p $CLIENT_CERTPATH/client/$client echo "CLIENT_CONFIG_DIR=$CLIENT_CONFIG_DIR" >> ${RCFILE} echo "CLIENT_CERTPATH=$CLIENT_CERTPATH" >> ${RCFILE} echo "CLIENT_CONFIG_FILE=${CLIENT_CONFIG_DIR}/client-${client}.conf" >> ${RCFILE} echo "CAPATH=${RCDIR}/$DOMAIN/CA" >> ${RCFILE} test -d $CAPATH || mkdir -p $CAPATH test -d $CLIENT_CERTPATH || mkdir -p $CLIENT_CERTPATH cat<<__EOF__ >$RCDIR/${DOMAIN}/conf/client-${client}.conf RANDFILE = $ENV::HOME/.rnd [ req ] default_bits = 2048 default_keyfile = keyfile.pem distinguished_name = req_distinguished_name attributes = req_attributes prompt = no #output_password = bliblablu [ req_distinguished_name ] C = SE ST = Sweden L = Göteborg O = ${DOMAIN} OU = ${DOMAIN} CN = ${client} emailAddress = ${client} [ req_attributes ] #challengePassword = blablabla __EOF__ } if [ "$#" = 3 ];then operation=$1 service=$2 client=$3 RCFILE="$RCDIR/$service/client-$client"".env" if [ "$operation" = "csr" ]; then test -f ${RCFILE} || ( client_home_setup_write ; echo "Edit values in ${RCFILE}"; exit -1) source ${RCFILE} client_create_csr $service $client fi if [ "$operation" = "setup" ]; then test -f ${RCFILE} || ( client_home_setup_write $service $client; echo "Edit values in ${RCFILE}"; exit 0) fi if [ "$operation" = "export" ]; then client_export_bundle $service $client fi if [ "$operation" = "delete" ]; then source ${RCFILE} #client_home_setup_delete $service $client fi else echo "Usage: iron client " exit -1 fi