#!/bin/bash # info: list mail domain ssl certificate # options: USER DOMAIN [FORMAT] # # example: v-list-mail-domain-ssl user acme.com json # # This function of obtaining domain ssl files. #----------------------------------------------------------# # Variables & Functions # #----------------------------------------------------------# # Argument definition user=$1 domain=$2 format=${3-shell} # Includes # shellcheck source=/etc/hestiacp/hestia.conf source /etc/hestiacp/hestia.conf # shellcheck source=/usr/local/hestia/func/main.sh source $HESTIA/func/main.sh # load config file source_conf "$HESTIA/conf/hestia.conf" # Additional argument formatting format_domain format_domain_idn # JSON list function json_list() { echo '{' echo -e "\t\"$domain\": {" echo " \"CRT\": \"$crt\"," echo " \"KEY\": \"$key\"," echo " \"CA\": \"$ca\"," echo " \"SUBJECT\": \"$subj\"," echo " \"ALIASES\": \"$alt_dns\"," echo " \"NOT_BEFORE\": \"$before\"," echo " \"NOT_AFTER\": \"$after\"," echo " \"SIGNATURE\": \"$signature\"," echo " \"PUB_KEY\": \"$pub_key\"," echo " \"ISSUER\": \"$issuer\"" echo -e "\t}\n}" } # SHELL list function shell_list() { if [ -n "$crt" ]; then echo -e "$crt" fi if [ -n "$key" ]; then echo -e "\n$key" fi if [ -n "$ca" ]; then echo -e "\n$ca" fi if [ -n "$crt" ]; then echo echo echo "SUBJECT: $subj" if [ -n "$alt_dns" ]; then echo "ALIASES: ${alt_dns//,/ }" fi echo "VALID FROM: $before" echo "VALID TIL: $after" echo "SIGNATURE: $signature" echo "PUB_KEY: $pub_key" echo "ISSUER: $issuer" fi } # PLAIN list function plain_list() { if [ -n "$crt" ]; then echo -e "$crt" fi if [ -n "$key" ]; then echo -e "\n$key" fi if [ -n "$ca" ]; then echo -e "\n$ca" fi if [ -n "$crt" ]; then echo "$subj" echo "${alt_dns//,/ }" echo "$before" echo "$after" echo "$signature" echo "$pub_key" echo "$issuer" fi } # CSV list function csv_list() { echo -n "CRT,KEY,CA,SUBJECT,ALIASES,NOT_BEFORE,NOT_AFTER,SIGNATURE," echo "PUB_KEY,ISSUER" echo -n "\"$crt\",\"$key\",\"$ca\",\"$subj\",\"${alt_dns//,/ }\"," echo "\"$before\",\"$after\",\"$signature\",\"$pub_key\",\"$issuer\"" } #----------------------------------------------------------# # Verifications # #----------------------------------------------------------# check_args '2' "$#" 'USER DOMAIN [FORMAT]' is_format_valid 'user' 'domain' is_object_valid 'user' 'USER' "$user" is_object_valid 'mail' 'DOMAIN' "$domain" #----------------------------------------------------------# # Action # #----------------------------------------------------------# # Parsing domain SSL certificate if [ -e "$USER_DATA/ssl/mail.$domain.crt" ]; then crt=$(cat $USER_DATA/ssl/mail.$domain.crt | sed ':a;N;$!ba;s/\n/\\n/g') info=$(openssl x509 -text -in $USER_DATA/ssl/mail.$domain.crt) subj=$(echo "$info" | grep Subject: | sed -e "s/\"//g" -e "s/.*= //") before=$(echo "$info" | grep Before: | sed -e "s/.*Before: //") after=$(echo "$info" | grep "After :" | sed -e "s/.*After : //") signature=$(echo "$info" | grep "Algorithm:" | head -n1) signature=$(echo "$signature" | sed -e "s/.*Algorithm: //") pub_key=$(echo "$info" | grep Public-Key: | cut -f2 -d \( | tr -d \)) issuer=$(echo "$info" | grep Issuer: | sed -e "s/.*Issuer: //") alt_dns=$(echo "$info" | grep DNS | sed -e 's/DNS:/\n/g' | tr -d ',') alt_dns=$(echo "$alt_dns" | tr -d ' ' | sed -e "/^$/d") alt_dns=$(echo "$alt_dns" | sed -e ':a;N;$!ba;s/\n/,/g') fi if [ -e "$USER_DATA/ssl/mail.$domain.key" ]; then key=$(cat $USER_DATA/ssl/mail.$domain.key | sed ':a;N;$!ba;s/\n/\\n/g') fi if [ -e "$USER_DATA/ssl/mail.$domain.ca" ]; then ca=$(cat $USER_DATA/ssl/mail.$domain.ca | sed ':a;N;$!ba;s/\n/\\n/g') fi # Listing data case $format in json) json_list ;; plain) plain_list ;; csv) csv_list ;; shell) shell_list ;; esac #----------------------------------------------------------# # Hestia # #----------------------------------------------------------# exit