diff --git a/snippets/create_basic-auth_password.sh b/snippets/create_basic-auth_password.sh new file mode 100755 index 0000000..5751023 --- /dev/null +++ b/snippets/create_basic-auth_password.sh @@ -0,0 +1,216 @@ +#!/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/$(basename $0).$$.LOCK" +log_file="${LOCK_DIR}/${script_name%%.*}.log" + + +# ---------- +# Base Function(s) +# ---------- + +usage() { + + + [[ -n "$1" ]] && error "$1" + + + [[ $terminal ]] && echo -e " +\033[1mUsage:\033[m + + $(basename $0) [user] + +\033[1mDescription\033[m + + Script creates a passord for use as basic auth authentificatiion at webservers. if + 'user' is also given, script prints auth a password line for use in htpasswd files. + +\033[1mOptions\033[m + + No further Options needed. The clear password is given at the commandline. + +\033[1mFiles\033[m + + No configuration files needed + +\033[1mExample:\033[m + + Create a saltet SHA512 password hash + + $(basename $0) test100 + +" + + clean_up 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 +} + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + exit $1 +} + +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 "" +} + +echo_ok() { + if $terminal ; then + echo -e "\033[55G[ \033[32mok\033[m ]" + fi +} +echo_failed(){ + if $terminal ; then + echo -e "\033[55G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal ; then + echo -e "\033[55G[ \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 + fatal "Script must run in a terminal." +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 [[ $# < 1 ]] +then + usage +fi + + +passwd=$1 +[[ -n "$2" ]] && _user="$2" + +echo "" +echo "" +echononl "\033[1mopenssl passwd -crypt $passwd\033[m" +passwd_hash="$(openssl passwd -crypt $passwd 2> $log_file)" +if [[ $? -ne 0 ]] ; then + echo_failed + error "$(cat "$log_file")" +else + echo_ok + echo "" + echo " password: $passwd" + echo " password hash: $passwd_hash" + if [[ -n "$2" ]]; then + echo "" + echo " ${2}:$passwd_hash" + fi + echo "" +fi + +echo "" +echo "" +echononl "\033[1mopenssl passwd -apr1 $passwd\033[m" +passwd_hash="$(openssl passwd -apr1 $passwd 2> $log_file)" +if [[ $? -ne 0 ]] ; then + echo_failed + error "$(cat "$log_file")" +else + echo_ok + echo "" + echo " password: $passwd" + echo " password hash: $passwd_hash" + if [[ -n "$2" ]]; then + echo "" + echo " ${2}:$passwd_hash" + fi + echo "" +fi + +clean_up 0 diff --git a/snippets/detect_os.sh b/snippets/detect_os.sh index a8c4fa2..70b091b 100755 --- a/snippets/detect_os.sh +++ b/snippets/detect_os.sh @@ -64,12 +64,13 @@ detect_os_2 () fi else - unknown_os + os_dist="unknown" + os="unknown" fi fi if [ -z "$os_dist" ]; then - unknown_os + os_dist="unknown" fi # remove whitespace from OS and os_dist name @@ -90,6 +91,7 @@ if [[ -n "$os_codename" ]]; then echo "Codename: ${os_codename}" fi +os="" os_dist="" os_version="" os_codename="" diff --git a/snippets/genereate_md5_hash_from_string.sh b/snippets/genereate_md5_hash_from_string.sh new file mode 100755 index 0000000..3f03a71 --- /dev/null +++ b/snippets/genereate_md5_hash_from_string.sh @@ -0,0 +1,171 @@ +#"/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/$(basename $0).$$.LOCK" +log_file="${LOCK_DIR}/${script_name%%.*}.log" + + +# ---------- +# Base Function(s) +# ---------- + +usage() { + + + [[ -n "$1" ]] && error "$1" + + + [[ $terminal ]] && echo -e " +\033[1mUsage:\033[m + + $(basename $0) + +\033[1mDescription\033[m + + Script generates MD5 hash from given String. + +\033[1mOptions\033[m + + No further Options needed. The clear password is given at the commandline. + +\033[1mFiles\033[m + + No configuration files needed + +\033[1mExample:\033[m + + Create a saltet SHA512 password hash + + $(basename $0) testString + +" + + clean_up 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 +} + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + exit $1 +} + +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 +} + +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 +} + +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 + fatal "Script must run in a terminal." +fi +# - Print help? +# - +if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then + usage +fi + + +if [[ $# < 1 ]] +then + usage +fi + + + +string=$1 + +echo "" +echo "" +echononl "\033[1mecho -n "$string" | md5sum | cut -d ' ' -f1\033[m" +md5_hash="$(echo -n "$string" 2> $log_file | md5sum 2> $log_file | cut -d ' ' -f1 2> $log_file)" +if [[ $? -ne 0 ]] ; then + echo_failed + error "$(cat "$log_file")" +else + echo_ok + echo "" + echo " String: $string" + echo " md5 hash: $md5_hash" + echo "" +fi + +clean_up 0