#!/usr/bin/env bash # whoowns: Find the owner of a domain on a CWP server # 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}")whoowns:$(echo -e "${TEXT_UNSET}") Find the owner of a domain on a CWP server USAGE: whoowns DOMAIN --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 run our domain through all three tables to see if there's a match. If # there is, we're good and stop there. If not, we move on to the next one. # Yes, this is almost literally the docroot script with some tweaks. Guilty. find_owner() { # Primary domain result="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT username FROM user WHERE domain = '${domain}';")" if [[ -n "${result}" ]]; then echo "${result}" return 0 fi # Addon domain result="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT user FROM domains WHERE domain = '${domain}';")" if [[ -n "${result}" ]]; then echo "${result}" return 0 fi # Subdomain result="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT user FROM subdomains WHERE CONCAT(subdomain, '.', domain) = '${domain}';")" if [[ -n "${result}" ]]; then echo "${result}" return 0 fi # And if no results were found at all... echo "${domain} wasn't found in the CWP database." >&2 return 1 } # Command options while [[ "${#}" -gt 0 ]]; do case "${1}" in --help | -h) show_help exit 0 ;; --version | -v) echo -e "${TEXT_BOLD}whoowns${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 domain) were passed if [[ "${#}" -eq 0 ]]; then show_help exit 0 fi # First convert the user's input into lowercase (domains and subdomains are # lowercase according to Google) and capture it domain="$(echo "${1}" | tr '[:upper:]' '[:lower:]')" # And then check if it's valid (not strict) if [[ ! "${domain}" =~ ^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]+$ ]]; then echo "That doesn't look like a valid domain or subdomain." >&2 exit 1 fi # And then pass the valid input to find_owner if it's valid. Valid. find_owner "${domain}"