diff --git a/autoremakessh b/autoremakessh index b3cbe8e..37cfe95 100644 --- a/autoremakessh +++ b/autoremakessh @@ -4,7 +4,6 @@ FB="\e[1m" CG="\e[38;5;40m" CR="\e[38;5;9m" CY="\033[38;5;226m" -config="/opt/autoremakessh/client/defaultconfig" ### BEGIN FUNCTION BLOCK @@ -24,38 +23,45 @@ return 1 fi source $config if [ "$server" = "" ]; then - echo -e "${CR}'server' value cannot be empty! Exiting!" + echo -e "${CR}[!] 'server' value cannot be empty! Exiting!" return 1 fi if [ "$privkey" = "" ]; then - echo -e "${CR}'privkey' value cannot be empty! Exiting!" + echo -e "${CR}[!] 'privkey' value cannot be empty! Exiting!" return 1 fi if [ "$pubkey" = "" ]; then - echo -e "${CR}'pubkey' value cannot be empty! Exiting!" + echo -e "${CR}[!] 'pubkey' value cannot be empty! Exiting!" return 1 fi if [ "$checksumdir" = "" ]; then - echo -e "${CR}'checksumdir' value cannot be empty! Exiting!" + echo -e "${CR}[!] 'checksumdir' value cannot be empty! Exiting!" return 1 fi if [[ "$disablechecksums" != "0" && "$disablechecksums" != "1" ]]; then - echo -e "${CR}Invalid value for option 'disablechecksums'. Exiting!" + echo -e "${CR}[!] Invalid value for option 'disablechecksums'. Exiting!" return 1 fi if [[ "$insecure" != "0" && "$insecure" != "1" ]]; then - echo -e "${CR}Invalid value for option 'insecure'. Exiting!" + echo -e "${CR}[!] Invalid value for option 'insecure'. Exiting!" return 1 fi } guardband() { if [ $? -ne 0 ]; then +echo '' echo -e "${CR}Script Exited with an error.${NF}" exit 1 fi } +ipguardband() { +if [ $? -ne 0 ]; then +securitywarning +fi +} + verifyserver(){ if ! wget --spider "$server" 2>/dev/null; then echo -e "${CR}Error: Cannot reach provisioning server${NF}" @@ -79,48 +85,60 @@ fi } verifyconfigsecureIP(){ - local config_file="$1" - local server_line=$(grep "^server=" "$config_file") - local url=$(echo "$server_line" | sed 's/^server=//') - local ip="" - local host_part=$(echo "$url" | sed -E 's|^[a-zA-Z0-9]+://([^/]+).*|\1|') +if [ "$insecure" = "1" ]; then +return 0 +fi + local host_part=$(echo "$server" | sed -E 's|^[a-zA-Z0-9]+://([^/]+).*|\1|') if [[ $host_part =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then ip="$host_part" else + ip="" if command -v host >/dev/null 2>&1; then - ip=$(host "$host_part" 2>/dev/null | grep "has address" | awk '{print $4}' | head -1) + output=$(host "$host_part") + if [[ $output =~ has\ address ]]; then + ip=$(echo "$output" | awk '{print $4}') + fi elif command -v nslookup >/dev/null 2>&1; then - ip=$(nslookup "$host_part" 2>/dev/null | grep "Name:" | awk '{print $2}' | head -1) + output=$(nslookup "$host_part") + if [[ $output =~ Name: ]]; then + ip=$(echo "$output" | awk '{print $2}') + fi elif command -v dig >/dev/null 2>&1; then - ip=$(dig +short "$host_part" 2>/dev/null | head -1) + output=$(dig +short "$host_part") + ip="$output" else - ip=$(getent ahosts "$host_part" 2>/dev/null | awk '{print $1}' | head -1) + output=$(getent ahosts "$host_part") + if [[ $output =~ [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ ]]; then + ip=$(echo "$output" | awk '{print $1}') + fi + fi + + if [[ -z "$ip" ]]; then + echo -e ${CR}"[!] Unable to resolve IP address for host '$host_part'."${NF} + return 1 fi fi - if [[ -z "$ip" ]]; then - return 1 - fi - local oct1=$(echo "$ip" | cut -d. -f1) - local oct2=$(echo "$ip" | cut -d. -f2) +local oct1=$(echo "$ip" | cut -d. -f1) +local oct2=$(echo "$ip" | cut -d. -f2) +if [[ -n $oct1 && $oct1 =~ ^[0-9]+$ ]] && [[ -n $oct2 && $oct2 =~ ^[0-9]+$ ]]; then if [[ $oct1 -eq 10 ]]; then return 0 fi - if [[ $oct1 -eq 172 ]] && [[ $oct2 -ge 16 ]] && [[ $oct2 -le 31 ]]; then return 0 fi - if [[ $oct1 -eq 192 ]] && [[ $oct2 -eq 168 ]]; then return 0 fi - +fi return 1 } securitywarning(){ +echo '' echo -e "${CR}${FB}@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@${NF}" echo -e "${CR}${FB}@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@${NF}" echo -e "${CR}${FB}@@@ @@@${NF}" @@ -159,29 +177,72 @@ sleep 30 exit 1 } +usage() { + echo "" + echo "Usage:" + echo "autoremakessh [MODE] [OPTIONS]" + echo "" + echo "Possible modes:" + echo "Default - Client mode" + echo "-s - Server mode" + echo "" + echo "Possible Options:" + echo "-c [FILE] - Configuration file to use (Default /opt/autoremakessh/client/defaultconfig)" + echo "-h - Display this message" + exit 0 +} + ### END FUNCTION BLOCK +config="/opt/autoremakessh/client/defaultconfig" +srvmode=0 + echo "" echo "AutoremakeSSH Version 20260410-0" echo "© 2026 Franscobec - AGPLv3 License" echo "https://git.franscorack.com/Franscobec/AutoremakeSSH/src/branch/main/" echo "" -if [[ $1 = "--server" ]]; then - echo -e "${CR}Feature not yet implemented in this release${NF}" - elif [[ $1 = "-h" ]]; then - echo "" - echo "Usage:" - echo "autoremakessh [MODE] [OPTIONS]" - echo "" - echo "Possible modes:" - echo "Default (No Arg) - Client mode" - echo "--server - Server mode" - echo "" - echo "Possible Options:" - echo "-c [FILE] - Configuration file to use (Default /opt/autoremakessh/client/defaultconfig)" - echo "-h - Display this message" - else +while getopts ":c:sh" opt; do + case ${opt} in + c ) + config=$OPTARG + ;; + s ) + srvmode=1 + ;; + h ) + usage + ;; + \? ) + echo "Invalid option: -$OPTARG" 1>&2 + exit 1 + ;; + : ) + echo "Invalid option: -$OPTARG requires an argument" 1>&2 + exit 1 + ;; + esac +done + +shift $((OPTIND -1)) +if [ -z "$config" ]; then + echo "No custom config file specified. Using default: $config" +fi + +if [ "$srvmode" -eq 1 ]; then + echo "Server mode is not yet implemented" + exit 1 +fi + +verifydefaultconfigpresence +guardband +verifyconfigvalidity +guardband +verifyserver +guardband +verifyconfigsecureIP +ipguardband verifydefaultconfigpresence guardband verifyconfigvalidity