#!/usr/bin/env bash # review: Show detailed information about a CWP account # Nathan P. # 0.1d set -euo pipefail # This script is meant to be used by root or a user with sudo access to it # The silent exit is intentional current_user="$(whoami)" if [[ "${current_user}" != 'root' ]]; then exit 1 fi # Version information VERSION='0.1d' # Nice text formatting options TEXT_BOLD='\e[1m' TEXT_UNSET='\e[0m' # Help message show_help() { cat << END_OF_HELP $(echo -e "${TEXT_BOLD}")review:$(echo -e "${TEXT_UNSET}") Show detailed information about a CWP account USAGE: review USER --help -h Show this message --version -v Show version information END_OF_HELP } # Newer versions of MariaDB complain about using 'mysql' if command -v mariadb >/dev/null 2>&1; then MYSQL_COMMAND='mariadb' else MYSQL_COMMAND='mysql' fi # We do an initial check if the user was found in the CWP database. If it # was, we continue on and fetch the bits of info we need for each field. review_user() { result="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT username FROM user WHERE username = '${user}';")" if [[ -z "${result}" ]]; then echo "${user} wasn't found in the CWP database." >&2 return 1 fi # Username (duh) echo -e "🧔 ${TEXT_BOLD}Username:${TEXT_UNSET} ${user}" # Domain domain="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT domain FROM user WHERE username = '${user}';")" echo -e "🌎 ${TEXT_BOLD}Domain:${TEXT_UNSET} ${domain}" # Package package_id="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT package FROM user WHERE username = '${user}';")" package="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT package_name FROM packages WHERE id = '${package_id}';")" echo -e "📦 ${TEXT_BOLD}Package:${TEXT_UNSET} ${package}" # Creation date date="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT setup_date FROM user WHERE username = '${user}';")" echo -e "📅 ${TEXT_BOLD}Created:${TEXT_UNSET} ${date}" # Contact email email="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT email FROM user WHERE username = '${user}';")" echo -e "📧 ${TEXT_BOLD}Email:${TEXT_UNSET} ${email}" # We do a simple du of the homedir. CWP doesn't offer a better method for # this and there's nothing reliable enough for other account data. size="$(du -hs /home/"${user}" | awk '{print $1}')" echo -e "💾 ${TEXT_BOLD}Size:${TEXT_UNSET} ${size}" # IP address ip_address="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT ip_address FROM user WHERE username = '${user}';")" echo -e "🌐 ${TEXT_BOLD}IP:${TEXT_UNSET} ${ip_address}" # The username in the database is always the Linux user, and the docroot is # fixed to public_html on CWP. At least as far as CWP knows. echo -e "📂 ${TEXT_BOLD}Doc. Root:${TEXT_UNSET} /home/${user}/public_html" } # Command options while [[ "${#}" -gt 0 ]]; do case "${1}" in --help | -h) show_help exit 0 ;; --version | -v) echo -e "${TEXT_BOLD}review${TEXT_UNSET} ${VERSION}" exit 0 ;; -*) echo -e "Not sure what ${1} is supposed to mean..." >&2 echo show_help exit 1 ;; *) break ;; esac done # Show help message and exit if no arguments (like a username) were passed if [[ "${#}" -eq 0 ]]; then show_help exit 0 fi # First convert the user's input into lowercase (usernames are always # lowercase) and capture it user="$(echo "${1}" | tr '[:upper:]' '[:lower:]')" # And then check if it's valid for a Linux username if [[ ! "${user}" =~ ^[a-z_][a-z0-9_-]*$ ]]; then echo "That doesn't look like a valid username." >&2 exit 1 fi # And then pass the valid input to review_user if it's valid. Valid. review_user "${user}"