#!/usr/bin/env bash script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/${script_name%%.*}.conf" LOCK_DIR="/tmp/${script_name%%.*}.$$.LOCK" log_file="${LOCK_DIR}/${script_name%%.*}.log" backup_date="$(date +%Y-%m-%d-%H%M)" # ---------- # Base Function(s) # ---------- usage() { [[ -n "$1" ]] && error "$1" [[ $terminal ]] && echo -e " \033[1mUsage:\033[m $(basename $0) \033[1mDescription\033[m Script changes/adds API Key for user schleuder to enable managing lists. \033[1mNote\033[m You can also run this script to initialise user 'schleuder'. \033[1mOptions\033[m No Options available \033[1mExample:\033[m The only usage is: $(basename $0) " clean_up 1 } clean_up() { # Perform program exit housekeeping rm -rf "$LOCK_DIR" blank_line exit $1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n " $*\\c" 1>&2 else echo -e -n " $*" 1>&2 fi rm /tmp/shprompt$$ fi } fatal(){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mFatal\033[m ] $*" else echo -e " [ Fatal ] $*" fi echo "" if $terminal ; then echo -e " \033[1mScript terminated\033[m.." else echo -e " Script terminated.." fi echo "" rm -rf $LOCK_DIR exit 1 } error (){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mError\033[m ] $*" else echo " [ Error ] $*" fi echo "" } warn (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[33m\033[1mWarn\033[m ] $*" else echo " [ Warn ] $*" fi echo "" fi } info (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[32m\033[1mInfo\033[m ] $*" else echo " [ Info ] $*" fi echo "" fi } ok (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[32m\033[1mOk\033[m ] $*" else echo " [ Ok ] $*" fi echo "" fi } echo_done() { if $terminal ; then echo -e "\033[75G[ \033[32mdone\033[m ]" fi } echo_ok() { if $terminal ; then echo -e "\033[75G[ \033[32mok\033[m ]" fi } echo_failed(){ if $terminal ; then echo -e "\033[75G[ \033[1;31mfailed\033[m ]" fi } echo_skipped() { if $terminal ; then echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" fi } trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo -n "$var" } blank_line() { if $terminal ; then echo "" fi } # ---------- # - Jobhandling # ---------- # - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM # - trap clean_up SIGHUP SIGINT SIGTERM # - Create lock directory '$LOCK_DIR" # mkdir "$LOCK_DIR" # ---------- # - Some checks .. # ---------- # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false fi # - Print help? # - if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then usage fi if [[ -z "$(which basename)" ]]; then fatal 'It seems "basename" is not installed, but needed!' fi if [[ -z "$(which realpath)" ]]; then fatal 'It seems "realpath" is not installed, but needed!' fi # ========== # - Begin Main Script # ========== # ---------- # - Headline # ---------- if $terminal ; then echo "" echo -e "\033[1m----------\033[m" echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" echo -e "\033[1m----------\033[m" fi # ---------- # Read Configurations from $conf_file # ---------- # - Give your default values here # - LOGGING=false SCHLEUDER_USER="schleuder" SCHLEUDER_MAIN_CONFIG="/etc/schleuder/schleuder.yml" if [[ -f "$conf_file" ]]; then source "$conf_file" else warn "No configuration file '$conf_file' present.\n Loading default values.." fi # ---------- # - Some pre-script tasks .. # ---------- if $terminal ; then echo "" echo "" echo -e " \033[1mDoing some pre-script tasks ..\033[m" echo "" fi if [[ ! -f "$SCHLEUDER_MAIN_CONFIG" ]]; then fatal "Schleuder main configuration file not found!" fi if grep -q -E "^${SCHLEUDER_USER}" /etc/passwd 2> /dev/null ; then # - This variable connot be set userdefined. Its almost the 'official' home directory. # - schleuder_home="$(cat /etc/passwd | grep -E "^${SCHLEUDER_USER}" | cut -d ':' -f6)" else fatal "User '$SCHLEUDER_USER' not present!" fi if [[ ! -d "$schleuder_home" ]]; then fatal "Home Directory '$schleuder_home' for user '$SCHLEUDER_USER' not found!" fi # - This variables cannot be set userdefined. # - schleuder_user_dir="${schleuder_home}/.schleuder-cli" schleuder_user_config="${schleuder_user_dir}/schleuder-cli.yml" echononl "All is fine" echo_ok # ---------- # - Main part of script # ---------- if $terminal ; then echo "" echo "" echo -e " \033[1mMain part of script ..\033[m" echo "" fi # - Create API Key for user schleuder # - > "$log_file" echononl "Generate new API Key .." api_key="$(schleuder new_api_key 2> "$log_file")" if [[ -s "$log_file" ]] ; then echo_failed error "$(cat $log_file)" > $log_file else echo_ok info "generated api key: $api_key" fi # - Add the generated API Key to the list of valid api keys at # - configuration file $SCHLEUDER_MAIN_CONFIG # - echononl "Add the generated API Key to '$SCHLEUDER_MAIN_CONFIG'.." if ! grep -q "$api_key" 2> /dev/null $SCHLEUDER_MAIN_CONFIG ; then perl -i.$backup_date -n -p \ -e "s/(^(\s*)valid_api_keys:.*)/\1\n\2 - ${api_key}/" \ $SCHLEUDER_MAIN_CONFIG > "$log_file" 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat "$log_file")" fi fi # - Make LOCK_DIR writable for user SCHLEUDER_USER # - chown -R ${SCHLEUDER_USER}:$SCHLEUDER_USER "$LOCK_DIR" # - Add generated API Key to schleuder's user configuration file # - # - If no configuration file present, create a new default one. # - have_dot_schleuder_cli_yml=true if [[ ! -d "$schleuder_user_dir" ]] ; then warn "Directory '$schleuder_user_dir' does not exist." have_dot_schleuder_cli_yml=false elif [[ ! -f "$schleuder_user_config" ]] ; then warn "Directory '$schleuder_user_dir' exists, but no file '$(basename "$schleuder_user_config")' inside" # - If the directory is present, no default configuration file (see below) # - will be written # - echononl "Move (backup) directory '$schleuder_user_dir' .." mv "$schleuder_user_dir" "${schleuder_user_dir}.${backup_date}" > "$log_file" 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat "$log_file")" fi have_dot_schleuder_cli_yml=false fi if ! $have_dot_schleuder_cli_yml ; then # Creates a default configuration file '${schleuder_user_config}' # echononl "Create default user configuration for user '$SCHLEUDER_USER' .." su - $SCHLEUDER_USER -s /bin/bash -c "/usr//bin/schleuder-cli lists list > \"$log_file\" 2>&1" if [[ -f "$schleuder_user_config" ]]; then echo_ok else echo_failed error "$(cat "$log_file")" fi fi # - Now, add the API Key.. # - echononl "Add API Key to user configuration '$(basename "$schleuder_user_config")' .." perl -i.$backup_date -n -p \ -e "s/^(\s*api_key:).*/\1 ${api_key}/" \ ${schleuder_user_config} > "$log_file" 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat "$log_file")" fi # - Get tls fingerprint of configured certificate # - echononl "Get tls fingerprint of configured certificate .." > "$log_file" cert_fingerprint="$(schleuder cert fingerprint 2> "$log_file" | awk '{print$4}')" if [[ -s "$log_file" ]] ; then echo_failed error "$(cat $log_file)" > $log_file else echo_ok info "TLS fingerprint of configured certificate: $cert_fingerprint" fi # - Add the fingerprint to schleuder users private configuration file # - echononl "Add the fingerprint to user configuration '$(basename "$schleuder_user_config")' .." if ! grep -q "$cert_fingerprint" 2> /dev/null ${schleuder_user_config} ; then perl -i.$backup_date -n -p \ -e "s/^(\s*tls_fingerprint:).*/\1 ${cert_fingerprint}/" \ ${schleuder_user_config} > "$log_file" 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat "$log_file")" fi else echo_skipped fi # ---------- # - Some post-script tasks .. # ---------- if $terminal ; then echo "" echo "" echo -e " \033[1mDoing some post-script tasks ..\033[m" echo "" fi # - Restart 'schleuder-api-daemon' # - echononl "Restart service 'schleuder-api-daemon' .." systemctl restart schleuder-api-daemon > "$log_file" 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat "$log_file")" fi clean_up 0