#!/bin/bash
# RHCSA EX200 (RHEL 10) Practice Exam - checker. Run as root on server1:
#   bash check.sh            (run it before AND after a reboot; the score after the reboot counts)
PASS=0; TOTAL=20; MASK=0
G=$'\e[1;32m'; R=$'\e[1;31m'; Y=$'\e[1;33m'; N=$'\e[0m'
res(){ if [ "$2" = ok ]; then PASS=$((PASS+1)); MASK=$((MASK | (1 << ($1 - 1)))); printf "%sPASS%s  Task %2d  %s\n" "$G" "$N" "$1" "$3"; else printf "%sFAIL%s  Task %2d  %s  %s(%s)%s\n" "$R" "$N" "$1" "$3" "$Y" "$2" "$N"; fi; }
pwok(){ # user password
  local h; h=$(getent shadow "$1" | cut -d: -f2); [ -n "$h" ] || return 1
  python3 - "$h" "$2" <<'EOF'
import sys, ctypes, ctypes.util
lib = ctypes.CDLL(ctypes.util.find_library("crypt")); lib.crypt.restype = ctypes.c_char_p
h, p = sys.argv[1], sys.argv[2]
sys.exit(0 if lib.crypt(p.encode(), h.encode()).decode() == h else 1)
EOF
}

# 1
pwok root 'RedHatLab26!' && res 1 ok "root password recovered" || res 1 "root password does not match" "root password recovered"
# 2
e=""; [ "$(hostnamectl hostname)" = server1.lab.example ] || e="hostname"
nmcli -g ipv4.addresses con show labnet 2>/dev/null | grep -q '192.168.50.10/24' || e="$e ipv4"
nmcli -g ipv6.addresses con show labnet 2>/dev/null | tr -d '\\' | grep -q 'fd00:50::10/64' || e="$e ipv6"
[ "$(getent hosts repo.lab.example | awk '{print $1}')" = 192.168.50.1 ] || e="$e repo.lab.example"
[ -z "$e" ] && res 2 ok "network and names" || res 2 "$e" "network and names"
# 3
e=""; for r in BaseOS AppStream; do grep -rqs "http://repo.lab.example/$r" /etc/yum.repos.d/ || e="$e $r"; done
dnf -q makecache >/dev/null 2>&1 || e="$e makecache-fails"
[ -z "$e" ] && res 3 ok "RPM repositories" || res 3 "$e" "RPM repositories"
# 4
e=""; rpm -q tmux >/dev/null || e="tmux"; rpm -q labtools >/dev/null || e="$e labtools"; rpm -q labjunk >/dev/null && e="$e labjunk-still-installed"
[ -z "$e" ] && res 4 ok "packages" || res 4 "$e" "packages"
# 5
e=""; flatpak remotes --system 2>/dev/null | grep -q '^labhub' || e="no system remote labhub"
flatpak info --system org.lab.Notes >/dev/null 2>&1 || e="$e org.lab.Notes"
[ -z "$e" ] && res 5 ok "Flatpak" || res 5 "$e" "Flatpak"
# 6
e=""; [ "$(getent group sysops | cut -d: -f3)" = 3000 ] || e="sysops-gid"
for u in ana ben; do id -nG $u 2>/dev/null | grep -qw sysops || e="$e $u-not-in-sysops"; done
case "$(getent passwd carl | cut -d: -f7)" in */nologin|*/false) ;; *) e="$e carl-shell";; esac
for u in ana ben carl; do pwok $u 'Lab-pass26' || e="$e $u-password"; done
[ -z "$e" ] && res 6 ok "users and groups" || res 6 "$e" "users and groups"
# 7
e=""; chage -l ben 2>/dev/null | grep -q 'Maximum number of days between password change.*: 60' || e="ben-max"
chage -l ben 2>/dev/null | grep -q 'Number of days of warning before password expires.*: 7' || e="$e ben-warn"
grep -Eq '^PASS_MAX_DAYS[[:space:]]+90$' /etc/login.defs || e="$e login.defs"
[ -z "$e" ] && res 7 ok "password aging" || res 7 "$e" "password aging"
# 8
e=""; sudo -l -U ana 2>/dev/null | grep -q 'NOPASSWD: ALL' || e="ana-no-nopasswd"
sudo -l -U carl 2>/dev/null | grep -q 'may run' && e="$e carl-has-sudo"
visudo -cq || e="$e sudoers-syntax"
[ -z "$e" ] && res 8 ok "privileged access" || res 8 "$e" "privileged access"
# 9
e=""; st=$(stat -c '%G %a' /srv/projects 2>/dev/null)
[ "${st%% *}" = sysops ] || e="group"; m=${st##* }
[ ${#m} -eq 4 ] && [ $(( ${m:0:1} & 2 )) -ne 0 ] || e="$e no-setgid"
[ "${m: -2:1}" = 7 ] || e="$e group-not-rwx"; [ "${m: -1}" = 0 ] || e="$e others-have-access"
[ "$(su - ana -c umask 2>/dev/null)" = 0027 ] || e="$e ana-umask"
[ -z "$e" ] && res 9 ok "shared directory and umask" || res 9 "$e" "shared directory and umask"
# 10
e=""; su - ana -c /srv/reports/run.sh 2>/dev/null | grep -q 'Report OK' || e="ana-cannot-run"
[ $(( 0$(stat -c %a /srv/reports/run.sh) & 2 )) -eq 0 ] || e="$e world-writable"
[ -z "$e" ] && res 10 ok "permission problem fixed" || res 10 "$e" "permission problem fixed"
# 11
su - ana -c 'ssh -o BatchMode=yes -o StrictHostKeyChecking=accept-new ana@localhost true' >/dev/null 2>&1 && res 11 ok "SSH keys" || res 11 "key login fails" "SSH keys"
# 12
e=""; f=/root/nologin.txt
if [ -s $f ] && ! grep -qv 'nologin$' $f && grep -q '^carl:' $f && [ -z "$(grep -vxFf /etc/passwd $f)" ]; then :; else e="nologin.txt"; fi
tar -tjf /root/etc-backup.tar.bz2 2>/dev/null | grep -q '^etc/passwd$' || e="$e backup"
[ -L /root/latest-backup ] && [ "$(readlink -f /root/latest-backup)" = /root/etc-backup.tar.bz2 ] || e="$e symlink"
[ -z "$e" ] && res 12 ok "text and archives" || res 12 "$e" "text and archives"
# 13
e=""; S=/usr/local/bin/bigfiles; t=$(mktemp -d)
if [ -x $S ]; then
  head -c 2000000 /dev/zero > $t/big.bin; echo hi > $t/small.txt
  o=$($S $t 2>&1); echo "$o" | grep -q big.bin || e="misses-big"; echo "$o" | grep -q small.txt && e="$e lists-small"
  o=$($S 2>&1); rc=$?; [ $rc -eq 1 ] || e="$e exit-$rc"; echo "$o" | grep -q 'Usage: bigfiles DIR' || e="$e usage-text"
else e="not-executable"; fi; rm -rf $t
[ -z "$e" ] && res 13 ok "script" || res 13 "$e" "script"
# 14
e=""; crontab -l -u ana 2>/dev/null | grep -Eq '^23[[:space:]]+14[[:space:]]+\*[[:space:]]+\*[[:space:]]+\*[[:space:]].*logger' || e="cron"
grep -Eq '^(server|pool)[[:space:]]+repo\.lab\.example' /etc/chrony.conf || e="$e chrony.conf"
systemctl is-enabled -q chronyd || e="$e chronyd-disabled"
[ -z "$e" ] && res 14 ok "schedule and time" || res 14 "$e" "schedule and time"
# 15
e=""; [ "$(findmnt -n -o FSTYPE /data 2>/dev/null)" = xfs ] || e="not-mounted-xfs"
findmnt -n -o SOURCE /data 2>/dev/null | grep -q 'vgexam-lvdata' || e="$e wrong-device"
grep -Eq '^UUID=[^[:space:]]+[[:space:]]+/data[[:space:]]' /etc/fstab || e="$e fstab-not-uuid"
[ -z "$e" ] && res 15 ok "LVM and file system" || res 15 "$e" "LVM and file system"
# 16
e=""; swapon --show=NAME,SIZE --noheadings --bytes 2>/dev/null | awk '$1 ~ /(vd|sd)b/ && $2 >= 500000000 && $2 <= 540000000 {f=1} END{exit !f}' || e="no-512M-swap-on-vdb"
grep -Eq '[[:space:]]swap[[:space:]]' /etc/fstab || e="$e fstab"
[ -z "$e" ] && res 16 ok "swap" || res 16 "$e" "swap"
# 17
sz=$(lvs --noheadings --units m -o lv_size vgexam/lvdata 2>/dev/null | tr -d ' m' | cut -d. -f1)
fs=$(df -BM --output=size /data 2>/dev/null | tail -1 | tr -d ' M')
[ "${sz:-0}" -ge 1200 ] && [ "${fs:-0}" -ge 1100 ] && res 17 ok "grow lvdata" || res 17 "lv=${sz}M fs=${fs}M" "grow lvdata"
# 18
e=""; ls /remote/docs >/dev/null 2>&1 || e="cannot-access"
findmnt -n -o FSTYPE,OPTIONS /remote/docs 2>/dev/null | grep -q '^nfs.*\bro\b' || e="$e not-nfs-ro"
systemctl is-enabled -q autofs || e="$e autofs-disabled"
[ -z "$e" ] && res 18 ok "network storage (autofs)" || res 18 "$e" "network storage (autofs)"
# 19
e=""; curl -fs http://server1:82 2>/dev/null | grep -q 'it works' || e="page"
[ "$(getenforce)" = Enforcing ] || e="$e not-enforcing"
semanage port -l 2>/dev/null | grep -E '^http_port_t' | grep -qw 82 || e="$e port-label"
firewall-cmd --permanent --list-ports 2>/dev/null | grep -qw 82/tcp || e="$e firewall"
semanage boolean -l 2>/dev/null | grep -E '^httpd_can_network_connect ' | grep -q '(on *, *on)' || e="$e boolean"
systemctl is-enabled -q httpd || e="$e httpd-disabled"
[ -z "$e" ] && res 19 ok "web server on port 82" || res 19 "$e" "web server on port 82"
# 20
e=""; [ "$(systemctl get-default)" = multi-user.target ] || e="target"
[ "$(tuned-adm active 2>/dev/null | awk -F': ' '{print $2}')" = "$(tuned-adm recommend 2>/dev/null)" ] || e="$e tuned"
[ -d /var/log/journal ] || grep -Eqrs "^Storage=persistent" /etc/systemd/journald.conf /etc/systemd/journald.conf.d || e="$e journal"
pgrep -x labburn >/dev/null && e="$e labburn-running"
systemctl is-enabled -q labburn 2>/dev/null && e="$e labburn-enabled"
[ -z "$e" ] && res 20 ok "running system" || res 20 "$e" "running system"

echo; [ $PASS -ge 15 ] && c=$G || c=$R
printf "%sScore: %d / %d%s   (pass mark 15)\n" "$c" "$PASS" "$TOTAL" "$N"
SUM=$(( (MASK * 7 + 13) % 251 ))
printf "\nResult code: %sRHCSA-PE1-%05X-%02X%s\n" "$Y" "$MASK" "$SUM" "$N"
echo "Paste it at https://rhcsa-practice-exam.pages.dev for your score report and study plan."
