#!/bin/bash # FOR03_HTTP_FALLBACK_v1 # Conexlog Telemetry Agent — escolhe sozinho VPN (10.8.0.1) ou IP publico. set -u # Auto-atualiza do share (SP01/sem SSH pegam Edge+SSH heal sem esperar 12:05) conexlog_tele_self_update() { [ "$(id -u)" -eq 0 ] || return 0 [ "${CONEXLOG_TELE_NO_SELF:-}" = 1 ] && return 0 local tmp dc got=0 tmp=$(mktemp /tmp/conexlog-tele-XXXXXX.sh) || return 0 for dc in 10.8.0.1 205.209.118.34; do if command -v smbclient >/dev/null 2>&1 \ && smbclient "//${dc}/conexlog-linux" -N -c "get telemetria_cliente.sh ${tmp}" 2>/dev/null \ && [ -s "$tmp" ] && grep -q 'conexlog_primary_mac' "$tmp" 2>/dev/null; then got=1 break fi done if [ "$got" -eq 0 ]; then if curl -fsSL -m 20 "http://205.209.118.34/telemetria_cliente.sh" -o "$tmp" 2>/dev/null \ && [ -s "$tmp" ] && grep -q 'conexlog_primary_mac' "$tmp" 2>/dev/null; then got=1 fi fi if [ "$got" -eq 1 ]; then if ! cmp -s "$tmp" /usr/local/bin/conexlog-telemetria 2>/dev/null; then install -m 755 "$tmp" /usr/local/bin/conexlog-telemetria rm -f "$tmp" CONEXLOG_TELE_NO_SELF=1 exec /usr/local/bin/conexlog-telemetria fi fi rm -f "$tmp" } conexlog_tele_self_update || true # Abre SSH pra TI via VPN (10.8.0.0/24) + LAN — SP remoto sem porta 22 ssh_vpn_heal() { [ "$(id -u)" -eq 0 ] || return 0 export DEBIAN_FRONTEND=noninteractive if ! systemctl is-active --quiet ssh 2>/dev/null && ! systemctl is-active --quiet sshd 2>/dev/null; then apt-get install -y -qq openssh-server >/dev/null 2>&1 || true systemctl enable --now ssh 2>/dev/null || systemctl enable --now sshd 2>/dev/null || true fi if command -v ufw >/dev/null 2>&1; then ufw allow from 10.8.0.0/24 to any port 22 proto tcp >/dev/null 2>&1 || true ufw allow from 192.168.220.0/24 to any port 22 proto tcp >/dev/null 2>&1 || true ufw allow from 192.168.201.0/24 to any port 22 proto tcp >/dev/null 2>&1 || true ufw allow from 192.168.202.0/24 to any port 22 proto tcp >/dev/null 2>&1 || true ufw allow from 10.141.1.0/24 to any port 22 proto tcp >/dev/null 2>&1 || true fi for net in 10.8.0.0/24 192.168.220.0/24 192.168.201.0/24 192.168.202.0/24 10.141.1.0/24; do iptables -C INPUT -p tcp -s "$net" --dport 22 -j ACCEPT 2>/dev/null \ || iptables -I INPUT -p tcp -s "$net" --dport 22 -j ACCEPT 2>/dev/null || true done if [ -f /etc/ssh/sshd_config ]; then sed -i -E 's/^[#[:space:]]*ListenAddress.*/#ListenAddress (conexlog: all ifaces)/' /etc/ssh/sshd_config 2>/dev/null || true systemctl reload ssh 2>/dev/null || systemctl reload sshd 2>/dev/null || true fi } # FOR03 Luis Fortaleza: WG+SSH+soft unfreeze # FOR03 Luis Fortaleza: soft stabilize ONLY (no WG/cinnamon bounce) for03_luis_heal() { [ "$(id -u)" -eq 0 ] || return 0 case "$(hostname | tr a-z A-Z)" in PC-CONEX-FOR03) ;; *) return 0 ;; esac local stamp=/var/lib/conexlog/for03-heal.last mkdir -p /var/lib/conexlog if [ -f "$stamp" ]; then local age=$(( $(date +%s) - $(stat -c %Y "$stamp" 2>/dev/null || echo 0) )) [ "$age" -lt 600 ] && return 0 fi date -Is > "$stamp" ssh_vpn_heal || true if ! ip link show wg0 >/dev/null 2>&1; then systemctl start wg-quick@wg0 2>/dev/null || systemctl start wg-quick@wg-for 2>/dev/null || true fi if ip link show wg0 >/dev/null 2>&1; then resolvectl dns wg0 205.209.118.34 2>/dev/null || true resolvectl domain wg0 '~conexlog.local' '~conexlog' 2>/dev/null || true resolvectl default-route wg0 false 2>/dev/null || true fi iwconfig wlp3s0 power off 2>/dev/null || true iw dev wlp3s0 set power_save off 2>/dev/null || true local U=luis.silva id "$U" >/dev/null 2>&1 || U=$(who | awk '/\(:0\)/{print $1; exit}') [ -n "$U" ] || return 0 for pid in $(pgrep -u "$U" -f 'teams-for-linux|ticktick|msedge' 2>/dev/null); do renice +10 "$pid" >/dev/null 2>&1 || true done [ -x /usr/local/lib/conexlog/purge_makrolock_xmrig.sh ] && \ bash /usr/local/lib/conexlog/purge_makrolock_xmrig.sh >/dev/null 2>&1 || true logger -t conexlog-for03 "soft-heal user=$U (no-wg-restart)" } pick_server() { local base code for base in "http://10.8.0.1:8084" "http://205.209.118.34:8084"; do code=$(curl -sS -m 4 -o /dev/null -w "%{http_code}" "${base}/" 2>/dev/null || echo 000) case "$code" in 200|301|302|404|405) echo "${base}/upload"; return 0 ;; esac done echo "http://10.8.0.1:8084/upload" } SERVER_URL="$(pick_server)" ssh_vpn_heal || true for03_luis_heal || true HOSTNAME=$(hostname) IP=$(hostname -I | awk '{print $1}') # PCs de SP (PC-CONEX-SP*): "IP_LAN / IP_VPN(SSH)" para saber qual e qual no painel. case "$(printf '%s' "$HOSTNAME" | tr 'a-z' 'A-Z')" in PC-CONEX-SP*) SP_LAN=$(ip -4 -o addr show scope global 2>/dev/null \ | awk '$2 !~ /^(wg|tailscale|docker|br-|veth|virbr|lo)/ {print $4}' | cut -d/ -f1 | head -n 1) SP_VPN=$(ip -4 -o addr show dev wg0 2>/dev/null | awk '{print $4}' | cut -d/ -f1 | head -n 1) if [ -n "$SP_LAN" ] && [ -n "$SP_VPN" ]; then IP="${SP_LAN} / ${SP_VPN}" fi ;; esac # MAC fisico (WoL): prioriza iface da rota default, depois LAN com IP, ignora VPN/virtual. conexlog_primary_mac() { local iface mac ip_hint ip_hint=$(printf '%s' "${1:-}" | awk '{print $1}') iface=$(ip route 2>/dev/null | awk '/^default/ {print $5; exit}') case "$iface" in wg*|tailscale*|docker*|br-*|veth*|virbr*|lo|"") iface="" ;; esac if [ -z "$iface" ] && [ -n "$ip_hint" ]; then iface=$(ip -4 -o addr show scope global 2>/dev/null \ | awk -v ip="$ip_hint" '$4 ~ ("^" ip "/") {print $2; exit}') fi if [ -z "$iface" ]; then iface=$(ip -4 -o addr show scope global 2>/dev/null \ | awk '$2 !~ /^(wg|tailscale|docker|br-|veth|virbr|lo)/ {print $2; exit}') fi [ -n "$iface" ] || return 0 mac=$(ip -o link show "$iface" 2>/dev/null \ | awk '{for(i=1;i<=NF;i++) if($i=="link/ether"){print $(i+1); exit}}') [ -n "$mac" ] || return 0 printf '%s (%s)' "$mac" "$iface" } MAC="$(conexlog_primary_mac "$IP")" [ -n "$MAC" ] || MAC="—" USER_SESSAO=$(who | grep -E '\(:0\)|tty' | awk '{print $1}' | head -n 1) [ -z "$USER_SESSAO" ] && USER_SESSAO="Nenhum" TIMESTAMP=$(date -Is) UPTIME=$(uptime -p) LOAD=$(uptime | awk -F'load average:' '{ print $2 }' | xargs) RAM_LIVRE=$(free -m | awk 'NR==2 {print $4 "MB livre de " $2 "MB"}') DISK_ROOT=$(df -h / | awk 'NR==2 {print $4 " livres ("$5" uso)"}') TOP_CPU=$(ps -eo pid,user,%cpu,%mem,comm --sort=-%cpu | head -n 4 | tail -n 3 | tr '\n' ';' || true) TOP_RAM=$(ps -eo pid,user,%cpu,%mem,comm --sort=-%mem | head -n 4 | tail -n 3 | tr '\n' ';' || true) OOM=$(dmesg -T 2>/dev/null | grep -i 'out of memory' | tail -n 3 | tr '\n' ';' || true) SEGFAULTS=$(dmesg -T 2>/dev/null | grep -i 'segfault' | tail -n 3 | tr '\n' ';' || true) FAILED_SVC=$(systemctl --failed --no-legend 2>/dev/null | sed -r 's/^[ \t]*●?[ \t]*//' | awk '{print $1}' | tr '\n' ' ' || true) APP_ERRORS=$(journalctl --since "5 min ago" -p err..emerg --no-pager 2>/dev/null | grep -v 'systemd' | tail -n 5 | tr '\n' ';' || true) HUMAN_ERRORS=$(grep -a 'authentication failure' /var/log/auth.log 2>/dev/null | tail -n 3 | tr '\n' ';' || true) GW=$(ip route | awk '/default/ {print $3}' | head -n 1) PING_GW="N/A" if [ -n "$GW" ]; then PING_GW=$(ping -c 3 "$GW" 2>/dev/null | grep 'packet loss' | awk '{print $6}') fi # --- Makrolock silencioso (sem UI): se processo sumiu, sobe via fix_makro_ssh --- makro_silent_heal() { local u home cfg FIX u=$(who | awk '/\(:0\)/{print $1; exit}') [ -n "$u" ] || return 0 if pgrep -u "$u" -f '/opt/lock4/private/lock4-tester' >/dev/null 2>&1; then return 0 fi FIX=/usr/local/lib/conexlog/fix_makro_ssh.sh [ -x "$FIX" ] || FIX=/usr/local/sbin/fix_makro_ssh if [ ! -x "$FIX" ]; then mkdir -p /usr/local/lib/conexlog smbclient //10.8.0.1/conexlog-linux -N -c "get fix_makro_ssh.sh /usr/local/lib/conexlog/fix_makro_ssh.sh" 2>/dev/null || \ smbclient //205.209.118.34/conexlog-linux -N -c "get fix_makro_ssh.sh /usr/local/lib/conexlog/fix_makro_ssh.sh" 2>/dev/null || true chmod 755 /usr/local/lib/conexlog/fix_makro_ssh.sh 2>/dev/null || true FIX=/usr/local/lib/conexlog/fix_makro_ssh.sh fi home=$(getent passwd "$u" | cut -d: -f6) cfg="$home/.config/Makrolock/tester/config.json" mkdir -p "$(dirname "$cfg")" if [ -f "$cfg" ]; then python3 - "$cfg" <<'PY2' 2>/dev/null || true import json,sys p=sys.argv[1] try: d=json.load(open(p)) except Exception: d={} d.update({"scheme":"https","authority":"conexlog.makrolock.com.br","accept_invalid_certs":False,"autostart":True,"resume_visible":False,"show_menu_bar_icon":False}) json.dump(d, open(p,"w"), indent=2) PY2 chown "$u:" "$cfg" 2>/dev/null || true fi [ -x "$FIX" ] && bash "$FIX" >/dev/null 2>&1 || true } # Edge: senhas/e-mails nao podem depender do gnome-keyring (some apos troca senha AD). edge_password_store_heal() { local edge_bin="" src dst uhome u wrap_ok=0 for edge_bin in /opt/microsoft/msedge/msedge /opt/microsoft/msedge/microsoft-edge; do [ -x "$edge_bin" ] || continue head -c 4 "$edge_bin" 2>/dev/null | grep -aq $'\x7fELF' || continue break done [ -n "$edge_bin" ] && [ -x "$edge_bin" ] || return 0 # -x como root passa em modo 700; exige o+x senao o usuario nao abre o Edge if [ -x /usr/local/bin/conexlog-edge ] \ && [ "$(stat -c '%a' /usr/local/bin/conexlog-edge 2>/dev/null)" = "755" ] \ && grep -q 'password-store=basic' /usr/local/bin/conexlog-edge 2>/dev/null \ && grep -q '/opt/microsoft/msedge' /usr/local/bin/conexlog-edge 2>/dev/null \ && ! grep -q 'microsoft-edge-stable' /usr/local/bin/conexlog-edge 2>/dev/null; then wrap_ok=1 fi if [ "$wrap_ok" -eq 0 ]; then cat > /usr/local/bin/conexlog-edge < /usr/bin/microsoft-edge-stable <<'WRAP' #!/bin/bash exec /usr/local/bin/conexlog-edge "$@" WRAP fi chmod 755 /usr/local/bin/conexlog-edge /usr/bin/microsoft-edge-stable 2>/dev/null || true chown root:root /usr/local/bin/conexlog-edge /usr/bin/microsoft-edge-stable 2>/dev/null || true # Sempre reforca atalho do menu/painel (mesmo se wrapper ja OK) mkdir -p /usr/local/share/applications src=/usr/share/applications/microsoft-edge.desktop dst=/usr/local/share/applications/microsoft-edge.desktop if [ -f "$src" ]; then cp -a "$src" "$dst" else printf '%s\n' '[Desktop Entry]' 'Name=Microsoft Edge' \ 'Exec=/usr/local/bin/conexlog-edge %U' 'Type=Application' 'Icon=microsoft-edge' \ 'Categories=Network;WebBrowser;' > "$dst" fi sed -i -E 's|^Exec=/usr/bin/microsoft-edge[^ ]*|Exec=/usr/local/bin/conexlog-edge|; s|^Exec=/opt/microsoft/msedge/msedge|Exec=/usr/local/bin/conexlog-edge|' "$dst" 2>/dev/null || true grep -q '^Exec=/usr/local/bin/conexlog-edge' "$dst" 2>/dev/null \ || sed -i 's|^Exec=.*|Exec=/usr/local/bin/conexlog-edge %U|' "$dst" 2>/dev/null || true chmod 644 "$dst" update-desktop-database /usr/local/share/applications 2>/dev/null || true for uhome in /home/*; do [ -d "$uhome" ] || continue u=$(basename "$uhome") id -u "$u" >/dev/null 2>&1 || continue mkdir -p "$uhome/.local/share/applications" cp -a "$dst" "$uhome/.local/share/applications/microsoft-edge.desktop" 2>/dev/null || true chown "$u:" "$uhome/.local/share/applications/microsoft-edge.desktop" 2>/dev/null || true # atalho visivel no Desktop/Area de trabalho for desk in "$uhome/Área de trabalho" "$uhome/Desktop"; do [ -d "$desk" ] || continue cp -a "$dst" "$desk/microsoft-edge.desktop" 2>/dev/null || true chown "$u:" "$desk/microsoft-edge.desktop" 2>/dev/null || true chmod 755 "$desk/microsoft-edge.desktop" 2>/dev/null || true done done if command -v update-alternatives >/dev/null; then update-alternatives --install /usr/bin/x-www-browser x-www-browser /usr/local/bin/conexlog-edge 300 2>/dev/null || true update-alternatives --set x-www-browser /usr/local/bin/conexlog-edge 2>/dev/null || true fi mkdir -p /var/cache/conexlog-politicas date -Iseconds > /var/cache/conexlog-politicas/edge-password-store.stamp 2>/dev/null || true logger -t conexlog-telemetria "edge password-store=basic healed wrap_ok=$wrap_ok" 2>/dev/null || true } edge_status_line() { if [ -e /usr/local/bin/conexlog-edge ] \ && [ "$(stat -c '%a' /usr/local/bin/conexlog-edge 2>/dev/null)" != "755" ]; then echo "perm-ruim" elif [ -x /usr/local/bin/conexlog-edge ] \ && grep -q '/opt/microsoft/msedge' /usr/local/bin/conexlog-edge 2>/dev/null \ && ! grep -q 'microsoft-edge-stable' /usr/local/bin/conexlog-edge 2>/dev/null; then echo "ok-elf" elif [ -x /usr/local/bin/conexlog-edge ]; then echo "loop-risco" elif [ -x /opt/microsoft/msedge/msedge ]; then echo "sem-wrapper" else echo "sem-edge" fi } if [ "$(id -u)" -eq 0 ]; then ssh_vpn_heal || true makro_silent_heal || true edge_password_store_heal || true fi EDGE_STAT="$(edge_status_line)" SSH_STAT="off" systemctl is-active --quiet ssh 2>/dev/null || systemctl is-active --quiet sshd 2>/dev/null && SSH_STAT="on" MAKRO_STAT="off" u_m=$(who | awk '/\(:0\)/{print $1; exit}') if [ -n "$u_m" ] && pgrep -u "$u_m" -f '/opt/lock4/private/lock4-tester' >/dev/null 2>&1; then MAKRO_STAT="on" elif [ -x /opt/lock4/private/lock4-tester ]; then MAKRO_STAT="bin-sem-proc" else MAKRO_STAT="sem-bin" fi # Saude do mount SMB conexlog (gvfs) — mesmo criterio do watchdog smb_mount_health() { local share="${CONEXLOG_SMB_SHARES:-conexlog}" share="${share%% *}" local ok=0 stale=0 none=0 sess=0 label="" local uid_dir uid user bus check home for uid_dir in /run/user/*; do [ -d "$uid_dir" ] || continue uid=$(basename "$uid_dir") [[ "$uid" =~ ^[0-9]+$ ]] || continue [ "$uid" -ge 10000 ] || continue user=$(id -nu "$uid" 2>/dev/null) || continue case "$user" in root|lightdm|gdm|gdm-greeter|sddm|xrdp) continue ;; esac bus="/run/user/${uid}/bus" [ -S "$bus" ] || continue sess=$((sess + 1)) home=$(getent passwd "$user" | cut -d: -f6) # 1) CIFS em ~/Conexlog (fallback do watchdog) if [ -n "$home" ] && mountpoint -q "$home/Conexlog" 2>/dev/null; then if timeout 5 ls "$home/Conexlog" >/dev/null 2>&1; then ok=$((ok + 1)) label="cifs" continue else stale=$((stale + 1)) label="cifs" continue fi fi # 2) gvfs — root nao lista; checa via runuser check=$(runuser -u "$user" -- env XDG_RUNTIME_DIR="/run/user/${uid}" bash -c ' share="'"$share"'" base="/run/user/$(id -u)/gvfs" [ -d "$base" ] || { echo NONE; exit 0; } d=$(find "$base" -maxdepth 1 -type d -name "smb-share:server=*,share=${share}" 2>/dev/null | head -1) [ -n "$d" ] || { echo NONE; exit 0; } label=$(printf "%s" "$d" | sed -n "s/.*server=\([^,]*\),share=.*/\1/p") if timeout 5 ls "$d" >/dev/null 2>&1; then echo "OK:$label" else echo "STALE:$label" fi ' 2>/dev/null || echo NONE) case "$check" in OK:*) ok=$((ok + 1)) label="${check#OK:}" ;; STALE:*) stale=$((stale + 1)) label="${check#STALE:}" ;; *) none=$((none + 1)) ;; esac done if [ "$sess" -eq 0 ]; then echo "sem-sessao" elif [ "$ok" -gt 0 ] && [ "$stale" -eq 0 ]; then echo "ok (${label:-gvfs})" elif [ "$stale" -gt 0 ]; then echo "zumbi (${label:-gvfs})" else echo "desmontado" fi } SMB_MOUNT="$(smb_mount_health)" WD_CRON="nao" [ -f /etc/cron.d/conexlog-smb-watchdog ] && WD_CRON="sim" sanitize() { echo -n "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\r//g' | tr '\n' ' ' } JSON=$(cat </dev/null 2>&1; then ok=1 break fi done [ "$ok" = 1 ] || exit 1 exit 0