readability and whitespaces

This commit is contained in:
Jérémy Lecour 2017-10-18 00:43:33 +02:00
parent 232648a9b0
commit cd8ea40336
2 changed files with 87 additions and 81 deletions

View file

@ -25,10 +25,14 @@ EOT
} }
log() { log() {
[ "${QUIET}" != "1" ] && echo "${PROGNAME}: $1" if [ "${QUIET}" != "1" ]; then
echo "${PROGNAME}: $1"
fi
} }
debug() { debug() {
[ "${VERBOSE}" = "1" ] && [ "${QUIET}" != "1" ] && >&2 echo "${PROGNAME}: $1" if [ "${VERBOSE}" = "1" ] && [ "${QUIET}" != "1" ]; then
>&2 echo "${PROGNAME}: $1"
fi
} }
error() { error() {
>&2 echo "${PROGNAME}: $1" >&2 echo "${PROGNAME}: $1"

View file

@ -24,7 +24,9 @@ EOT
} }
debug() { debug() {
[ "${VERBOSE}" = 1 ] && >&2 echo "${PROGNAME}: $1" if [ "${VERBOSE}" = 1 ]; then
>&2 echo "${PROGNAME}: $1"
fi
} }
error() { error() {
>&2 echo "${PROGNAME}: $1" >&2 echo "${PROGNAME}: $1"
@ -32,113 +34,113 @@ error() {
} }
real_ip_for_domain() { real_ip_for_domain() {
dig +short "$1" | grep -oE "([0-9]+\.){3}[0-9]+" dig +short "$1" | grep -oE "([0-9]+\.){3}[0-9]+"
} }
local_ip() { local_ip() {
ip a | grep brd | cut -d'/' -f1 | grep -oE "([0-9]+\.){3}[0-9]+" ip a | grep brd | cut -d'/' -f1 | grep -oE "([0-9]+\.){3}[0-9]+"
} }
nginx_domains() { nginx_domains() {
local vhost_file="$1" local vhost_file="$1"
grep -oE "^( )*[^#]+" "${vhost_file}" \ grep -oE "^( )*[^#]+" "${vhost_file}" \
| grep -oE "[^\$]server_name.*;$" \ | grep -oE "[^\$]server_name.*;$" \
| sed 's/server_name//' \ | sed 's/server_name//' \
| tr -d ';' \ | tr -d ';' \
| sed 's/\s\{1,\}//' \ | sed 's/\s\{1,\}//' \
| sed 's/\s\{1,\}/\n/g' \ | sed 's/\s\{1,\}/\n/g' \
| sort \ | sort \
| uniq | uniq
} }
apache_domains() { apache_domains() {
local vhost_file="$1" local vhost_file="$1"
grep -oE "^( )*[^#]+" "${vhost_file}" \ grep -oE "^( )*[^#]+" "${vhost_file}" \
| grep -oE "(ServerName|ServerAlias).*" \ | grep -oE "(ServerName|ServerAlias).*" \
| sed 's/ServerName//' \ | sed 's/ServerName//' \
| sed 's/ServerAlias//' \ | sed 's/ServerAlias//' \
| sed 's/\s\{1,\}//' \ | sed 's/\s\{1,\}//' \
| sort \ | sort \
| uniq | uniq
} }
get_domains() { get_domains() {
local vhost_file="$1" local vhost_file="$1"
local ips="$2" local ips="$2"
local domains="" local domains=""
local valid_domains="" local valid_domains=""
local nb=0 local nb=0
if $(echo "${vhost_file}" | grep -q nginx); then if $(echo "${vhost_file}" | grep -q nginx); then
debug "Nginx vhost file used" debug "Nginx vhost file used"
domains=$(nginx_domains "${vhost_file}") domains=$(nginx_domains "${vhost_file}")
fi fi
if $(echo "${vhost_file}" | grep -q apache2); then if $(echo "${vhost_file}" | grep -q apache2); then
debug "Apache vhost file used" debug "Apache vhost file used"
domains=$(apache_domains "${vhost_file}") domains=$(apache_domains "${vhost_file}")
fi fi
debug "Valid(s) domain(s) in ${vhost_file} :" debug "Valid(s) domain(s) in ${vhost_file} :"
for domain in ${domains}; do
real_ip=$(real_ip_for_domain "${domain}")
for ip in $(echo "${ips}" | xargs -n1); do
if [ "${ip}" = "${real_ip}" ]; then
valid_domains="${valid_domains} ${domain}"
nb=$(( nb + 1 ))
debug "* ${domain} -> ${real_ip}"
fi
done
done
if [ "${nb}" -eq 0 ]; then
nb=$(echo "${domains}" | wc -l)
debug "* No valid domain found"
debug "All following(s) domain(s) will be used for CSR creation :"
for domain in ${domains}; do for domain in ${domains}; do
debug "* ${domain}" real_ip=$(real_ip_for_domain "${domain}")
for ip in $(echo "${ips}" | xargs -n1); do
if [ "${ip}" = "${real_ip}" ]; then
valid_domains="${valid_domains} ${domain}"
nb=$(( nb + 1 ))
debug "* ${domain} -> ${real_ip}"
fi
done
done done
else
domains="${valid_domains}"
fi
echo "${domains}" | xargs -n 1 if [ "${nb}" -eq 0 ]; then
nb=$(echo "${domains}" | wc -l)
debug "* No valid domain found"
debug "All following(s) domain(s) will be used for CSR creation :"
for domain in ${domains}; do
debug "* ${domain}"
done
else
domains="${valid_domains}"
fi
echo "${domains}" | xargs -n 1
} }
first_vhost_file_found() { first_vhost_file_found() {
local vhost_name="$1" local vhost_name="$1"
ls "/etc/nginx/sites-enabled/${vhost_name}" \ ls "/etc/nginx/sites-enabled/${vhost_name}" \
"/etc/nginx/sites-enabled/${vhost_name}.conf" \ "/etc/nginx/sites-enabled/${vhost_name}.conf" \
"/etc/apache2/sites-enabled/${vhost_name}.conf" \ "/etc/apache2/sites-enabled/${vhost_name}.conf" \
2>/dev/null \ 2>/dev/null \
| head -n 1 | head -n 1
} }
main() { main() {
if [ "$#" != 1 ]; then if [ "$#" != 1 ]; then
>&2 usage >&2 usage
exit 1 exit 1
fi fi
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage usage
exit 0 exit 0
fi fi
local vhost_name=$(basename "$1" .conf) local vhost_name=$(basename "$1" .conf)
local vhost_file=$(first_vhost_file_found "${vhost_name}") local vhost_file=$(first_vhost_file_found "${vhost_name}")
if [ ! -h "${vhost_file}" ]; then if [ ! -h "${vhost_file}" ]; then
>&2 echo "No virtualhost has been found for '${vhost_name}'." >&2 echo "No virtualhost has been found for '${vhost_name}'."
exit 1 exit 1
fi fi
local ips=$(local_ip) local ips=$(local_ip)
if [ -n "${SRV_IP}" ]; then if [ -n "${SRV_IP}" ]; then
ips="${ips} ${SRV_IP}" ips="${ips} ${SRV_IP}"
fi fi
get_domains "${vhost_file}" "${ips}" get_domains "${vhost_file}" "${ips}"
} }
readonly PROGNAME=$(basename "$0") readonly PROGNAME=$(basename "$0")