# check if run from script [[ "${BASH_SOURCE[0]}" == "${0}" ]] && exit 1 #------------------------------------------------------------------------------ # read default param init_default(){ DEFAULT_CONF="${ROOT_DIR}/config/default.conf" [[ -f "$DEFAULT_CONF" ]] && source "$DEFAULT_CONF" } #------------------------------------------------------------------------------ # read param from config file init_env() { # read config file CONFIG_FILE="/etc/own-pki/own-pki.conf" [[ -f "$CONFIG_FILE" ]] && source "$CONFIG_FILE" || msg_error "File ${ORANGE}$CONFIG_FILE${RED} missing. Run 'sudo bin/install.sh'${NC}" # debug mode if [[ "${DEBUG:-false}" == "true" ]]; then set -x fi out_tmp=$(mktemp) err_tmp=$(mktemp) } #------------------------------------------------------------------------------ # read credential from /root/.cred file init_cred(){ CRED_FILE=/root/.cred [[ -f "$CRED_FILE" ]] && source "$CRED_FILE"|| msg_error "File ${ORANGE}$CRED_FILE${RED} missing.${NC}" } #------------------------------------------------------------------------------ # clean string # input: string to clean # output: string cleaned clean_string() { echo "$1" | \ # translate special chars to closest ASCII (e.g., 'é' -> 'e') iconv -f utf-8 -t ascii//TRANSLIT | \ # convert to lowercase tr '[:upper:]' '[:lower:]' | \ # replace any non-alphanumeric character with an underscore sed -E 's/[^a-z0-9]+/_/g' | \ # replace multiple underscores into one sed -E 's/(_)+/_/g' | \ # remove underscores at the beginning or end sed -E 's/^_|_$//g' } #------------------------------------------------------------------------------ # check format fqdn # input: fqdn to check check_format_fqdn(){ if [[ ! "$1" =~ ^([a-z0-9]+(-[a-z0-9]+)*\.){2,}[a-z]{2,}$ ]]; then msg_error "\n$1 is not a fqdn valid.\n" exit 1 fi } #------------------------------------------------------------------------------ # check format ip address # input: ip address to check check_format_ip(){ if [[ ! "$1" =~ ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]; then msg_error "\n$1 is not an address IP valid.\n" exit 1 fi } #------------------------------------------------------------------------------ # check if the user has sudo privileges and ensure that the script runs with sudo. check_sudo(){ # check if user has sudo rigth if sudo ! -n true 2>/dev/null; then msg_error "\nAccess denied: user $USER does not have sudo privileges or a password is required.\n" exit 1 fi # check if the effective user ID is 0 (root) if [[ $EUID -ne 0 ]]; then msg_error "\nThis script must be run as root or with sudo.\n" exit 1 fi } #------------------------------------------------------------------------------ # check the return code check_rc(){ if [ "$1" != "0" ]; then msg_error "Error (RC:$rc)" exit 1 else msg_ok "OK" fi } #----------------------------------------------------------- # set colors # RED Error # ORANGE Attention or color for parameters when # confirmation # CYAN Ask to user or display a data # GREEN OK set_color(){ if [[ "$ENABLE_COLOR" == "true" ]]; then RED='\e[0;31m' ORANGE='\e[0;33m' CYAN='\e[0;36m' GREEN='\e[0;32m' NC='\e[0m' else RED='' ORANGE='' CYAN='' GREEN='' NC='' fi } #----------------------------------------------------------- # ask question yes or no # input: prompt yes_no(){ if [ "$ASSUME_YES" == "0" ]; then echo -n -e "${CYAN}$1 [y/N]? ${NC}" unset answer read answer if [ "${answer}" != "y" ]; then echo -e "${ORANGE}Canceled!${NC}" exit 1 fi fi }