#!/usr/bin/env bash ############################################################ ### ### ### Someguy Scripts - (C) 2019 github.com/Someguy123 ### ### Released as open source under the GNU AGPL v3 ### ### Someguy Scripts Version: 2.0.0 ### ### ### ### github.com/Someguy123/someguy-scripts ### ### ### ### This minified script was compiled at: ### ### Sat Nov 16 06:02:18 UTC 2019 ### ### ### ############################################################ ############################################################# # # # Privex's Shell Core (Version v0.2.0) # # Cross-platform / Cross-shell helper functions # # # # Released under the GNU GPLv3 # # # # Official Repo: github.com/Privex/shell-core # # # # This minified script was compiled at: # # Sat Nov 16 06:02:18 UTC 2019 # # # ############################################################# ### -------------------------------------- ### Privex/shell-core/init.sh ### -------------------------------------- export S_CORE_VER="0.2.0" # Used by sourcing scripts to identify the current version of Privex's Shell Core. ! [ -z ${ZSH_VERSION+x} ] && _SDIR=${(%):-%N} || _SDIR="${BASH_SOURCE[0]}" DIR="$( cd "$( dirname "${_SDIR}" )" && pwd )" : ${SG_DIR="$DIR"} : ${SG_LAST_UPDATE_FILE="${SG_DIR}/.last_update"} : ${SG_DEBUG=0} # If set to 1, will enable debugging output to stderr : ${DEBUGLOG="${SG_DIR}/logs/debug.log"} : ${SG_LOCALDIR="${HOME}/.pv-shcore"} # Folder to install Privex Shell Core for local installs : ${SG_GLOBALDIR="/usr/local/share/pv-shcore"} # Folder to install Privex Shell Core for global installs : ${SG_UPDATE_SECS=604800} SG_LAST_UPDATE=0 export SG_DIR SG_LOCALDIR SG_GLOBALDIR DEBUGLOG SG_DEBUG last_update_shellcore() { if [[ -f "${SG_DIR}/.last_update" ]]; then __sg_lst=$(cat "$SG_LAST_UPDATE_FILE") export SG_LAST_UPDATE=$(($__sg_lst)) fi } last_update_shellcore update_shellcore() { bash "${SG_DIR}/run.sh" update; } autoupdate_shellcore() { last_update_shellcore local _unix_now=$(date +'%s') unix_now=$(($_unix_now)) next_update=$((SG_LAST_UPDATE+SG_UPDATE_SECS)) local last_rel=$((unix_now-SG_LAST_UPDATE)) if (($next_update<$unix_now)); then _debug green "Last update was $last_rel seconds ago. Auto-updating Privex ShellCore." update_shellcore else _debug yellow "Auto-update requested, but last update was $last_rel seconds ago (next update due after ${SG_UPDATE_SECS} seconds)" fi } DEBUGLOG_DIR=$(dirname "$DEBUGLOG") [[ ! -d "$DEBUGLOG_DIR" ]] && mkdir -p "$DEBUGLOG_DIR" && touch "$DEBUGLOG" _debug() { msg ts "$@" >> "$DEBUGLOG"; (($SG_DEBUG!=1)) && return msgerr ts "$@" } ### -------------------------------------- ### Privex/shell-core/base/identify.sh ### -------------------------------------- export SRCED_IDENT_SH=1 ident_shell() { if ! [ -z ${ZSH_VERSION+x} ]; then export CURRENT_SHELL="zsh" elif ! [ -z ${BASH_VERSION+x} ]; then export CURRENT_SHELL="bash" else export CURRENT_SHELL="unknown" return 1 fi echo "$CURRENT_SHELL" return 0 } ### -------------------------------------- ### Privex/shell-core/base/colors.sh ### -------------------------------------- export SRCED_COLORS=1 ! [ -z ${ZSH_VERSION+x} ] && _SDIR=${(%):-%N} || _SDIR="${BASH_SOURCE[0]}" DIR="$( cd "$( dirname "${_SDIR}" )" && pwd )" if [ -t 1 ]; then BOLD="$(tput bold)" RED="$(tput setaf 1)" GREEN="$(tput setaf 2)" YELLOW="$(tput setaf 3)" BLUE="$(tput setaf 4)" MAGENTA="$(tput setaf 5)" CYAN="$(tput setaf 6)" WHITE="$(tput setaf 7)" RESET="$(tput sgr0)" else BOLD="" RED="" GREEN="" YELLOW="" BLUE="" MAGENTA="" CYAN="" WHITE="" RESET="" fi function msg () { if [[ "$#" -eq 0 ]]; then echo ""; return; fi; if [[ "$#" -eq 1 ]]; then echo -e "$1" return fi [[ "$1" == "ts" ]] && shift && _msg="[$(date +'%Y-%m-%d %H:%M:%S %Z')] " || _msg="" if [[ "$#" -gt 2 ]] && [[ "$1" == "bold" ]]; then echo -n "${BOLD}" shift fi (($#==1)) && _msg+="$@" || _msg+="${@:2}" case "$1" in bold) echo -e "${BOLD}${_msg}${RESET}";; BLUE|blue) echo -e "${BLUE}${_msg}${RESET}";; YELLOW|yellow) echo -e "${YELLOW}${_msg}${RESET}";; RED|red) echo -e "${RED}${_msg}${RESET}";; GREEN|green) echo -e "${GREEN}${_msg}${RESET}";; CYAN|cyan) echo -e "${CYAN}${_msg}${RESET}";; MAGENTA|magenta|PURPLE|purple) echo -e "${MAGENTA}${_msg}${RESET}";; * ) echo -e "${_msg}";; esac } function msgts () { msg ts "${@:1}" } function msgerr () { >&2 msg "$@" } if [[ $(ident_shell) == "bash" ]]; then export -f msg msgts >/dev/null elif [[ $(ident_shell) == "zsh" ]]; then export msg msgts >/dev/null else >&2 echo "${RED}${BOLD}WARNING: Could not identify your shell. Attempting to export msg and msgts with plain export..." export msg msgts fi export RED GREEN YELLOW BLUE BOLD NORMAL RESET ### -------------------------------------- ### Privex/shell-core/base/permission.sh ### -------------------------------------- ! [ -z ${ZSH_VERSION+x} ] && _SDIR=${(%):-%N} || _SDIR="${BASH_SOURCE[0]}" DIR="$( cd "$( dirname "${_SDIR}" )" && pwd )" path_permission() { local pm_file="$1" pm_path="" part parts [[ "$pm_file" != /* ]] && pm_path="." parts=($(dirname "$pm_file" | tr '/' $'\n')) for part in "${parts[@]}"; do pm_path="$pm_path/$part" if ! [[ -x "$pm_path" ]] ; then msgerr red "Cannot access file/folder '$pm_file' because '$pm_path' isn't +x - please run 'sudo chmod +x \"$pm_path\"' to resolve this." return 1 fi done return 0 } can_read() { path_permission "$1" && [ -r "$1" ] && return 0 || return 1 } can_write() { path_permission "$1" && [ -w "$1" ] && return 0 || return 1 } ### -------------------------------------- ### Privex/shell-core/lib/000_gnusafe.sh ### -------------------------------------- if [ -t 1 ]; then BOLD="$(tput bold)" RED="$(tput setaf 1)" GREEN="$(tput setaf 2)" YELLOW="$(tput setaf 3)" BLUE="$(tput setaf 4)" MAGENTA="$(tput setaf 5)" CYAN="$(tput setaf 6)" WHITE="$(tput setaf 7)" RESET="$(tput sgr0)" else BOLD="" RED="" GREEN="" YELLOW="" BLUE="" MAGENTA="" CYAN="" WHITE="" RESET="" fi : ${HAS_GGREP=0} : ${HAS_GSED=0} : ${HAS_GAWK=0} function gnusafe () { if ! [ -z ${ZSH_VERSION+x} ]; then setopt aliases elif ! [ -z ${BASH_VERSION+x} ]; then shopt -s expand_aliases else >&2 echo "${RED} We can't figure out what shell you're running as neither BASH_VERSION nor ZSH_VERSION are set. This is important as we need to figure out which grep/sed/awk that we should use, and alias appropriately. Different shells have different ways of enabling alias's in scripts such as this, but since you don't seem to be using zsh or bash, we can't continue...${RESET} " return 3 fi if [[ $(uname -s) != 'Linux' ]] && [ -z ${FORCE_UNIX+x} ]; then if [[ $(command -v ggrep) ]]; then HAS_GGREP=1 alias grep="ggrep" alias egrep="ggrep -E" fi if [[ $(command -v gsed) ]]; then HAS_GSED=1 alias sed=gsed fi if [[ $(command -v gawk) ]]; then HAS_GAWK=1 alias awk=gawk fi if [[ $HAS_GGREP -eq 0 || $HAS_GSED -eq 0 || $HAS_GAWK -eq 0 ]]; then >&2 echo "${RED} --- ERROR: Non-Linux detected. Missing GNU sed, awk or grep --- The program could not find ggrep, gawk, or gsed as a fallback. Due to differences between BSD and GNU Utils the program will now exit Please install GNU grep and GNU sed, and make sure they work On BSD systems, including OSX, they should be available as 'ggrep' and 'gsed' For OSX, you can install ggrep/gsed/gawk via brew: brew install gnu-sed brew install grep brew install gawk If you are certain that both 'sed' and 'grep' are the GNU versions, you can bypass this and use the default grep/sed with FORCE_UNIX=1${RESET} " return 4 else trap gnusafe-cleanup EXIT return 0 fi else alias ggrep="grep" alias gawk="awk" alias gsed="sed" if [[ $(command -v egrep) ]]; then alias egrep="grep -E" fi trap gnusafe-cleanup EXIT return 0 fi } function is_alias () { command -V "$1" 2> /dev/null | grep -qi "alias" } function gnusafe-cleanup () { is_alias ggrep && unalias ggrep 2>/dev/null is_alias gawk && unalias gawk 2>/dev/null is_alias gsed && unalias gsed 2>/dev/null is_alias grep && unalias grep 2>/dev/null is_alias awk && unalias awk 2>/dev/null is_alias sed && unalias sed 2>/dev/null } ### -------------------------------------- ### Privex/shell-core/lib/010_helpers.sh ### -------------------------------------- export SRCED_010HLP=1 len() { wc -c <<< "${@:1}"; } containsElement () { local e match="$1" shift for e; do [[ "$e" == "$match" ]] && return 0; done return 1 } has_command() { command -v "$1" > /dev/null } has_binary() { /usr/bin/env which "$1" > /dev/null } yesno() { local MSG invert=0 retcode=3 defact="none" defacts defacts=('defno' 'defyes' 'deferr' 'fail') MSG="Do you want to continue? (y/n) > " (( $# > 0 )) && MSG="$1" && shift while (( $# > 0 )); do containsElement "$1" "${defacts[@]}" && defact="$1" [[ "$1" == "invert" ]] && invert=1 shift done local YES=0 NO=1 (( $invert == 1 )) && YES=1 NO=0 unset answer while true; do read -p "$MSG" answer if [ -z "$answer" ]; then case "$defact" in defno) retcode=$NO break ;; defyes) retcode=$YES (( $invert == 0 )) && retcode=0 || retcode=1 break ;; fail) exit 2 break ;; *) ;; esac fi case "$answer" in y|Y|yes|YES) retcode=$YES break ;; n|N|no|NO|nope|NOPE|exit) retcode=$NO break ;; *) msg red " (!!) Please answer by typing yes or no - or the characters y or n - then press enter." msg red " (!!) If you want to exit this program, press CTRL-C (hold CTRL and tap the C button on your keyboard)." msg ;; esac done return $retcode } : ${PKG_MGR_UPDATE="apt-get update -qy"} : ${PKG_MGR_INSTALL="apt-get install -y"} PKG_MGR_UPDATED="n" pkg_not_found() { if (($#<2)); then msg red "ERR: pkg_not_found requires 2 arguments (cmd) (package)" exit fi local cmd=$1 local pkg=$2 if ! has_binary "$cmd"; then msg yellow "WARNING: Command $cmd was not found. installing now..." if [[ "$PKG_MGR_UPDATED" == "n" ]]; then sudo sh -c "${PKG_MGR_UPDATE}" > /dev/null PKG_MGR_UPDATED="y" fi sudo sh -c "${PKG_MGR_INSTALL} ${pkg}" >/dev/null fi } sudo() { if [ "$EUID" -ne 0 ]; then if ! has_binary sudo; then msg bold red "ERROR: You are not root, and you don't have sudo installed. Cannot run command '${@:1}'" msg red "Please either install sudo and add your user to the sudoers group, or run this script as root." sleep 5 return 3 fi /usr/bin/env sudo "${@:1}" return $? fi /usr/bin/env "${@:1}" } if [[ $(ident_shell) == "bash" ]]; then export -f sudo containsElement yesno pkg_not_found >/dev/null elif [[ $(ident_shell) == "zsh" ]]; then export sudo containsElement yesno pkg_not_found >/dev/null else msgerr bold red "WARNING: Could not identify your shell. Attempting to export with plain export..." export sudo containsElement yesno pkg_not_found >/dev/null fi ############################################################# # # # Privex's Shell Core (Version v0.2.0) # # Cross-platform / Cross-shell helper functions # # # # Released under the GNU GPLv3 # # # # Official Repo: github.com/Privex/shell-core # # # # This minified script was compiled at: # # Sat Nov 16 06:02:18 UTC 2019 # # # ############################################################# ############################################################ ### ### ### Someguy Scripts - (C) 2019 github.com/Someguy123 ### ### Released as open source under the GNU AGPL v3 ### ### Someguy Scripts Version: 2.0.0 ### ### ### ### github.com/Someguy123/someguy-scripts ### ### ### ### This minified script was compiled at: ### ### Sat Nov 16 06:02:18 UTC 2019 ### ### ### ############################################################ ### -------------------------------------- ### Someguy123/someguy-scripts/scripts/base.sh ### -------------------------------------- sgs_var_defaults() { : ${SKIP_CLONE='n'} # Do not git clone. Trust that LIB_DIR contains someguy-scripts already. : ${SKIP_LOCALE='n'} # Do not configure/generate locales : ${SKIP_INSTALL='n'} # Do not install packages listed in INSTALL_PKGS : ${SKIP_GLOBAL='n'} # Do not install configurations and zsh files globally : ${SKIP_HARDEN='n'} # Do not run server hardening : ${LIB_DIR="$(mktemp -d)"} # Destination to clone someguy-scripts into (or existing location of someguy-scripts if SKIP_CLONE) } SGBASE_LOADED='y' has_binary() { /usr/bin/env which "$1" > /dev/null; } if ! has_binary sudo && [ "$EUID" -eq 0 ]; then sudo() { env "$@"; } has_sudo() { return 0; } else has_sudo() { sudo -n ls > /dev/null; } fi echo if ! has_binary curl; then if ! has_sudo; then echo echo " !!! CRITICAL ERROR: The package 'curl' is not installed, and you do not have passwordless sudo available." echo " !!! Due to a lack of passwordless sudo, we cannot auto-install curl for you." echo " !!! To be able to run lite.sh you must first run 'sudo apt install -y curl' and enter your password." echo exit 1 else echo " >>> Missing 'curl' application. Attempting to install curl for you now." echo " > Updating apt..." apt-get update -qy &> /dev/null APT_UPDATED="y" echo " > Installing curl..." apt-get -o Dpkg::Options::='--force-confold' --force-yes install -qy curl &> /dev/null echo -e " [+++] Installed curl." echo -e "${_LN}" fi fi if [ -z ${S_CORE_VER+x} ]; then echo "Checking if Privex ShellCore is installed / Downloading it..." _sc_fail() { >&2 echo "Failed to load or install Privex ShellCore..." && exit 1; } # Error handling function for Privex ShellCore [[ -f "${HOME}/.pv-shcore/load.sh" ]] || [[ -f "/usr/local/share/pv-shcore/load.sh" ]] || \ { curl -fsS https://cdn.privex.io/github/shell-core/install.sh | bash >/dev/null; } || _sc_fail echo "Loading Privex ShellCore..." [[ -d "${HOME}/.pv-shcore" ]] && . "${HOME}/.pv-shcore/load.sh" || . "/usr/local/share/pv-shcore/load.sh" || _sc_fail fi sg_copyright() { msg "${_LN}" msg bold green " ############################################################" msg bold green " ### ###" msg bold green " ### Someguy Scripts - (C) 2019 github.com/Someguy123 ###" msg bold green " ### Released as open source under the GNU AGPL v3 ###" msg bold green " ### ###" msg bold green " ############################################################" msg "${_LN}" sleep 2 } sgs_clone() { if [[ "$SKIP_CLONE" != "y" ]]; then msg cyan " >>> Cloning 'github.com/Someguy123/someguy-scripts.git' into temp folder: $LIB_DIR" git clone --recurse-submodules -q https://github.com/Someguy123/someguy-scripts.git "$LIB_DIR" msg green " [+] Done.\n" else if [[ ! -d "${LIB_DIR}" ]]; then msg bold red " [!!!] Critical Error. SKIP_CLONE is 'y' but the LIB_DIR folder doesn't exist: $LIB_DIR " msg bold red " [!!!] Cannot continue without LIB_DIR pointing to a valid someguy-scripts installation\n" return 1 elif [[ ! -f "${LIB_DIR}/lib.sh" ]]; then msg bold red " [!!!] Critical Error. SKIP_CLONE is 'y' but the file '${LIB_DIR}/lib.sh' does not exist." msg bold red " [!!!] Cannot continue without LIB_DIR pointing to a valid someguy-scripts installation\n" return 1 fi msg yellow " [-] Skipping clone as SKIP_CLONE is 'y'. Using someguy-scripts in folder: $LIB_DIR" fi return 0 } [ -z ${ZSH_USERS+x} ] && ZSH_USERS=(root ubuntu chris privex user) sgs_provision() { local u uh sgs_var_defaults msg msg cyan " >>> Checking both 'git' and 'curl' are available, otherwise installing them..." pkg_not_found git git pkg_not_found curl curl msg green " [+] Done.\n" if [ -z ${SGLIB_LOADED+x} ]; then sgs_clone msg "${_LN}" msg cyan " >>> Importing library file: ${LIB_DIR}/lib.sh" cd "$LIB_DIR" . "lib.sh" || { >&2 echo "!!! ERROR !!! Could not load lib.sh. Cannot continue."; exit 1; } msg green " [+] Done.\n" fi msg "${_LN}" [[ "$SKIP_LOCALE" == "y" ]] || { msg cyan " >>> Preventing locale issues..."; fix_locale; msg green " [+] Done.\n"; } msg "${_LN}" [[ "$SKIP_INSTALL" == "y" ]] || { install_essential; msg green " [+] Done.\n"; } msg "${_LN}" [[ "$SKIP_GLOBAL" == "y" ]] || { msg cyan " >>> Installing global configs and zsh files..."; install_global; msg green " [+] Done.\n"; } msg "${_LN}" [[ "$SKIP_HARDEN" == "y" ]] || { harden; msg green " [+] Done.\n"; } msg "${_LN}" msg cyan " >>> Setting zsh as the default shell for ZSH_USERS: ${ZSH_USERS[@]}" for u in "${ZSH_USERS[@]}"; do if ! getent passwd "$u" > /dev/null; then msg yellow " [!] User '$u' doesn't exist. Skipping.\n" continue fi msg green " [+] Setting ZSH as the default shell for user '$u'" change_shell "$u" > /dev/null msg green " [+] Checking if user '$u' has a .zshrc file" local uh=$(get_home "$u") if (($(len "$uh")<3)); then msg yellow " [!] User home folder too short (< 3 chars)... Skipping for safety. Folder was: ${BOLD}$uh" continue fi _copy_zshrc "$uh" > /dev/null && msg green " [+] User '$u' has .zshrc file, or one was installed successfully.\n" && \ OMZSH_INSTALLED='y' || msg red " [!] Error while attempting to install .zshrc file into '$uh'...\n" done msg green " [+] Done.\n"; msg bold green " [+++] Finished running Someguy-Scripts lite auto-provisioner.\n" sg_copyright } ### -------------------------------------- ### Someguy123/someguy-scripts/scripts/lite.sh ### -------------------------------------- _LN="\n==========================================================================\n" echo -e "\n${_LN}" echo " ############################################################" echo " ### ###" echo " ### Someguy Scripts - (C) 2019 github.com/Someguy123 ###" echo " ### Released as open source under the GNU AGPL v3 ###" echo " ### ###" echo " ############################################################" echo -e "${_LN}" sleep 1 SGLITE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" : ${SKIP_HARDEN='y'} # Do not run server hardening sg_copyright if [ -z ${INSTALL_PKGS+x} ]; then msg cyan " >>> Using default lite.sh INSTALL_PKGS as wasn't set in environment." INSTALL_PKGS=( git curl wget tmux screen mtr-tiny iputils-ping netcat dnsutils net-tools vim-tiny nano-tiny zsh htop zip unzip xz-utils thin-provisioning-tools ) else msg cyan " >>> Using custom INSTALL_PKGS which was set in environment..." msg cyan " >>> Content of INSTALL_PKGS: ${INSTALL_PKGS[@]}\n" fi IS_FRESH='y' # We set IS_FRESH to true to skip any prompts requiring user input. sgs_provision