diff --git a/init-linux-harden.sh b/init-linux-harden.sh index d940adf..e2da3e5 100644 --- a/init-linux-harden.sh +++ b/init-linux-harden.sh @@ -1,18 +1,5 @@ #!/etc/bin/env bash -# Something important fails - # Revert everything back to how it was -# Ask the user to NOT logout yet and login as normal user - # If he can - great - # Remove the SSH-only login and ask the user to login using password - # Report - # Root password - # User Password - # User SSH-Private Key - # User SSH-Public key - # If not - # Ask him to report back if he can login using the new user -with the ssh-private key - # - tell him to talk to the server provider's support to get help regarding SSH-only access # What to do if making .bkp file fails? # Add timestamp` to all backup files filename.071218_171731_bak #Test @@ -100,6 +87,7 @@ fi AUTO_GEN_USERNAME="y" RESET_ROOT_PWD="n" DEFAULT_SOURCE_LIST="n" +QUIET="n" while [[ "$#" -gt 0 ]]; do case $1 in @@ -128,6 +116,10 @@ while [[ "$#" -gt 0 ]]; do DEFAULT_SOURCE_LIST="y" shift ;; + -q|--quiet|--nowait|--noprompt) + QUIET="y" + shift + ;; -h|--help) usage shift @@ -147,16 +139,29 @@ done ############################################################## clear -cat </dev/null @@ -343,11 +345,10 @@ function revert_secure_authorized_key(){ fi if [[ $success -eq 0 ]]; then - op_log "Reverting - SSH Key Authorizations" "SUCCESSFUL" - file_log "Reverting SSH Key Authorizations - Completed" + op_log "Reverting - SSH Key Authorization" "SUCCESSFUL" + file_log "Reverting SSH Key Authorization - Completed" else - op_log "Reverting - SSH Key Authorizations" "FAILED" - file_log "Reverting SSH Key Authorizations - Failed" + error_restoring "Reverting - SSH Key Authorization" fi } @@ -367,8 +368,7 @@ function revert_ssh_only_login(){ op_log "Reverting - SSH-only Login" "SUCCESSFUL" file_log "Reverting SSH-only Login - Completed" else - op_log "Reverting - SSH-only Login" "FAILED" - file_log "Reverting SSH-only Login - Failed" + error_restoring "Reverting - SSH-only Login" fi } @@ -395,8 +395,7 @@ function revert_source_list_changes(){ op_log "Reverting - Source_list Changes" "SUCCESSFUL" file_log "Reverting Source_list Changes - Completed" else - op_log "Reverting - Source_list Changes" "FAILED" - file_log "Reverting Source_list Changesn - Failed" + error_restoring "Reverting - Source_list Changes" fi } @@ -406,7 +405,25 @@ function revert_root_pass_change(){ center_err_text "Your earlier root password remains VALID" } +revert_software_installs(){ + echo + center_err_text "Installing software failed..." + center_err_text "This is NOT a catastrophic error" +} + function finally(){ + if [[ $CreateNonRootUser -eq 2 ]] && + [[ $CreateSSHKey -eq 2 ]] && + [[ $SecureAuthkeysfile -eq 2 ]] && + [[ $EnableSSHOnly -eq 2 ]] && + [[ $ChangeSourceList -eq 2 ]] && + [[ $InstallReqSoftwares -eq 2 ]]; then + echo + line_fill "$CHORIZONTAL" "$CLINESIZE" + line_fill "$CHORIZONTAL" "$CLINESIZE" + center_reg_text "ALL OPERATIONS COMPLETED SUCCESSFULLY" + fi + # If something failed - try to revert things back if [[ "$#" -gt 0 ]]; then echo @@ -426,7 +443,7 @@ function finally(){ [[ $EnableSSHOnly -eq 3 ]]; then return 1 else - line_fill "$CHORIZONTAL" $CLINESIZE + line_fill "$CHORIZONTAL" "$CLINESIZE" recap "User Name" "$CreateNonRootUser" "$NORM_USER_NAME" recap "User's Password" "$CreateNonRootUser" "$USER_PASS" recap "SSH Private Key File" "$CreateSSHKey" "$SSH_DIR"/"$NORM_USER_NAME".pem @@ -435,7 +452,19 @@ function finally(){ if [[ "$RESET_ROOT_PWD" == "y" ]]; then recap "New root Password" "$ChangeRootPwd" "$PASS_ROOT" fi - line_fill "$CHORIZONTAL" $CLINESIZE + line_fill "$CHORIZONTAL" "$CLINESIZE" + + recap_file_content "SSH Private Key" "$SSH_DIR"/"$NORM_USER_NAME".pem + recap_file_content "SSH Public Key" "$SSH_DIR"/"$NORM_USER_NAME".pem.pub + + line_fill "$CHORIZONTAL" "$CLINESIZE" + center_reg_text "!!! DO NOT LOG OUT JUST YET !!!" + center_reg_text "Use another window to test out the above credentials" + center_reg_text "If you face issue logging in look at the log file to see what went wrong" + center_reg_text "Log file at ${LOGFILE}" + + line_fill "$CHORIZONTAL" "$CLINESIZE" + echo fi if [[ $ChangeSourceList -eq 3 ]] || @@ -458,7 +487,7 @@ function finally(){ CVERTICAL="|" CHORIZONTAL="_" -CLINESIZE=64 +CLINESIZE=72 function center_text(){ textsize=${#1} @@ -469,7 +498,7 @@ function center_text(){ function center_err_text(){ printf "${CRED}" - center_text "$1" $CLINESIZE + center_text "$1" "$CLINESIZE" printf "${CEND}\\n" } @@ -520,18 +549,35 @@ function recap (){ local value=$3 if [[ $status -eq 0 ]]; then + file_log "${purpose}: Did not start this operation. See log above." value="[${CGREEN}--NO_OP--${CEND}]" elif [[ $status -eq 2 ]]; then + file_log "${purpose}: ${value}" value="[${CGREEN}${value}${CEND}]" elif [[ $status -eq 1 ]] || [[ $status -eq 3 ]]; then + file_log "${purpose}: ERROR. See log above." value="${CRED}--ERROR--${CEND}" fi horizontal_fill "$CVERTICAL" 1 - printf "%20s:%3s%-49s" "$purpose" " " "$(echo -e "$value")" + printf "%23s:%3s%-54s" "$purpose" " " "$(echo -e "$value")" line_fill "$CVERTICAL" 1 } +function recap_file_content(){ + local file_type=$1 + local file_location=$2 + echo + + center_reg_text "$file_type" + file_log "$file_type" + echo + printf "${CGREEN}" + cat "$file_location" + cat "$file_location" 2>> "$LOGFILE" >&2 + printf "${CEND}" +} + ############################################################## # Create non-root user @@ -867,6 +913,7 @@ if [[ $OP_CODE -eq 0 ]]; then else update_event_status "Installing required softwares" 3 op_log "Installing required softwares" "FAILED" + revert_software_installs fi @@ -887,8 +934,7 @@ if [[ $RESET_ROOT_PWD == 'y' ]]; then file_log "Generated Root Password - ${PASS_ROOT}" # Change root's password - false - #echo -e "${PASS_ROOT}\\n${PASS_ROOT}" | passwd + echo -e "${PASS_ROOT}\\n${PASS_ROOT}" | passwd set_op_code $? } 2>> "$LOGFILE" >&2