own-pki/lib/core.sh
2026-04-20 06:27:02 +02:00

52 lines
1.4 KiB
Bash

# check if run from script
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && exit 1
init_default(){
# read default conf file
DEFAULT_CONF="${ROOT_DIR}/config/default.conf"
[[ -f "$DEFAULT_CONF" ]] && source "$DEFAULT_CONF"
}
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)
}
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_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
}