Rewrite token formatter engine

This commit is contained in:
Lucas 2022-04-19 03:41:36 +00:00
parent e7aeadfa32
commit cfd97ff74d

View File

@ -45,53 +45,50 @@ get_ca_sk_comment_from_pk()
} }
} }
_template_fmt() format()
{ {
_allowed_chars=$1 _s=$1
_char=$2 shift
if [ "X$_char" = X% ]; then
echo %
return $?
fi
case $_char in _cleanup=unset
[$_allowed_chars]) while [ $# -ge 2 ]; do
;; _k=$1 _v=$2
*) shift 2
return 1 case $_k in
;; [A-Za-z])
esac ;;
*)
_v=$(eval echo '${_template_fmt_'"$_char"':-}') return 1
if [ -z "$_v" ]; then ;;
esac
eval "_token_${_k}=\$_v"
_cleanup=$_cleanup" _token_${_k}"
done
if [ $# -ne 0 ]; then
return 1 return 1
fi fi
echo "$_v"
}
template()
{
_allowed=$1
_s=$2
_out= _out=
while [ "${_s#*%}" != "$_s" ]; do while [ "${_s#*%}" != "$_s" ]; do
_t=${_s#*%} _t=${_s#*%}
_out=$_out${_s%"%"$_t} _out=$_out${_s%"%"$_t}
_s=$_t _s=$_t
_c=${_s%${_s#?}} _c=${_s%${_s#?}}
_t=$(_template_fmt "$_allowed" "$_c") if [ -z "${_c:-}" ]; then
if [ $? -ne 0 ]; then
return 1 return 1
elif [ X"${_c}" = X% ]; then
_out=$_out%
else
eval "_out=$_out\$_token_${_c}" || return 1
fi fi
_out=$_out$_t
_s=${_s#$_c} _s=${_s#$_c}
done done
_out=$_out$_s _out=$_out$_s
eval "$_cleanup"
echo "$_out" echo "$_out"
} }
@ -136,20 +133,20 @@ main_issue()
read -r serial <"$PATH_CA_SERIAL" read -r serial <"$PATH_CA_SERIAL"
ca_comment=$(get_ca_sk_comment_from_pk "$PATH_CA_PUB") ca_comment=$(get_ca_sk_comment_from_pk "$PATH_CA_PUB")
_template_fmt_C=${ca_comment:-cassh} : ${ca_comment:=cassh}
find "$PATH_PUBKEYS_DIR/" -type f -name '*.pub' ! -name '*-cert.pub' | { find "$PATH_PUBKEYS_DIR/" -type f -name '*.pub' ! -name '*-cert.pub' | {
while read -r pk; do while read -r pk; do
pkname=${pk%.pub} pkname=${pk%.pub}
pkname=${pkname#$PATH_PUBKEYS_DIR/} pkname=${pkname#$PATH_PUBKEYS_DIR/}
_template_fmt_f=$pkname
id=$(template Cf "$key_id_fmt") id=$(format "$key_id_fmt" C "$ca_comment" f "$pkname")
set -- -I "$id" -Us "$PATH_CA_PUB" \ set -- -I "$id" -Us "$PATH_CA_PUB" \
$hflag $qflag $vflag \ $hflag $qflag $vflag \
-V "$validity_interval" -z "$serial" -V "$validity_interval" -z "$serial"
if $nflag; then if $nflag; then
principals=$(template f "$principals_fmt") principals=$(format "$principals_fmt" \
f "$pkname")
ssh-keygen "$@" -n "$principals" "$pk" ssh-keygen "$@" -n "$principals" "$pk"
else else
ssh-keygen "$@" "$pk" ssh-keygen "$@" "$pk"