scripts/bash/realmjoin.sh
2025-04-14 09:22:15 +02:00

143 lines
4.9 KiB
Bash

#!/bin/sh -e
# ╭───────────────╮
# │ env functions │
# ╰───────────────╯
# ───────────────────────────────────< ANSI color codes >───────────────────────────────────
RED='\033[0;31m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
LIGHT_GREEN='\033[0;92m'
BOLD='\033[1m'
NC='\033[0m' # No Color
echo_error() {
printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2
}
echo_info() {
printf "${BOLD}${CYAN}INFO: ${NC}${CYAN}%s${NC}\n" "$1"
}
echo_warning() {
printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1"
}
echo_note() {
printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1"
}
# ────────────────< function to check if the given command exists silently >────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# ─────────────< Check if the user is root and set sudo variable if necessary >─────────────
check_root() {
if [ "$(id -u)" -ne 0 ]; then
if command_exists sudo; then
echo_info "User is not root. Using sudo for privileged operations."
_sudo="sudo"
else
echo_error "No sudo found and you're not root! Can't install packages."
return 1
fi
else
echo_info "Root access confirmed."
_sudo=""
fi
}
# ─< Distribution detection and installation >────────────────────────────────────────
get_packager() {
if [ -e /etc/os-release ]; then
echo_info "Detecting distribution..."
. /etc/os-release
ID=$(printf "%s" "$ID" | tr '[:upper:]' '[:lower:]')
ID_LIKE=$(printf "%s" "$ID_LIKE" | tr '[:upper:]' '[:lower:]')
case "$ID" in
ubuntu | pop) _install() { $_sudo apt-get install --assume-yes "$@"; } ;;
debian) _install() { $_sudo apt-get install --assume-yes "$@"; } ;;
fedora) _install() { $_sudo dnf install -y "$@"; } ;;
alpine) _install() { $_sudo apk add "$@"; } ;;
arch | manjaro | garuda | endeavour) _install() { $_sudo pacman -S --noconfirm "$@"; } ;;
opensuse*) _install() { $_sudo zypper in -y "$@"; } ;;
*)
if echo "$ID_LIKE" | grep -q "debian"; then
_install() { $_sudo apt-get install --assume-yes "$@"; }
elif echo "$ID_LIKE" | grep -q "ubuntu"; then
_install() { $_sudo apt-get install --assume-yes "$@"; }
elif echo "$ID_LIKE" | grep -q "arch"; then
_install() { $_sudo pacman -S --noconfirm "$@"; }
elif echo "$ID_LIKE" | grep -q "fedora"; then
_install() { $_sudo dnf install -y "$@"; }
elif echo "$ID_LIKE" | grep -q "suse"; then
_install() { $_sudo zypper in -y "$@"; }
else
echo_error "Unsupported distribution: $ID"
return 1
fi
;;
esac
else
echo_error "Unable to detect distribution. /etc/os-release not found."
return 1
fi
}
# ──────────────────────────< define your functions/script here >────────────────────────
dependencies() {
_deps="krb5-user realmd sssd-tools sssd libnss-sss libpam-sss adcli"
for dependency in $_deps; do
if ! command_exists "$dependency"; then
echo_info "Installing $dependency.."
sleep 0.3
if ! _install "$dependency"; then
echo_error "$dependency - could not be installed!"
else
echo_note "$dependency - was installed successfully!"
fi
else
echo_note "$dependency is already installed."
fi
done
}
_join() {
domain="swu.dom"
echo_note "You are trying to connect to $domain.."
sleep 1
echo_note "Please enter an administrator user like this: [example-user]"
printf "Admin User: " >&2
read -r _admin </dev/tty
if [ -n "$_admin" ]; then
if realm join -v -U "$_admin@$domain" "$domain"; then
echo_note "Successfully joined domain $domain."
else
echo_error "Failed to join domain $domain."
return 1
fi
else
echo_error "Administrator username cannot be empty."
return 1
fi
}
# ───────────────────────────────< define the main function >───────────────────────────────
main() {
if check_root; then
get_packager &&
dependencies &&
_join
else
echo_error "Root privileges are required. Exiting."
return 1
fi
}
# ──────────────────────────────< execute the main function >────────────────────────────
main