diff --git a/setup_database.sh b/setup_database.sh index 0e78906..72f4aff 100755 --- a/setup_database.sh +++ b/setup_database.sh @@ -1,80 +1,72 @@ -#!/usr/bin/env bash +#!/bin/bash set -e DB_NAME="mitm_db" DB_USER="mitm_user" -DB_PASS="mitm_pass" +DB_PASS="mitm_password" +LAN_SUBNET="192.168.178.0/24" -echo "[+] Installing PostgreSQL..." -sudo apt-get update -y -sudo apt-get install -y postgresql postgresql-contrib +echo "[1] Installing PostgreSQL…" +sudo apt update -y +sudo apt install -y postgresql postgresql-contrib -echo "[+] Ensuring PostgreSQL is running..." -sudo systemctl enable postgresql -sudo systemctl start postgresql +echo "[2] Configuring PostgreSQL to listen on all addresses…" +PG_CONF="/etc/postgresql/$(ls /etc/postgresql)/main/postgresql.conf" +sudo sed -i "s/^#listen_addresses =.*/listen_addresses = '*'/" "$PG_CONF" -echo "[+] Creating user and database (if missing)..." +echo "[3] Updating pg_hba.conf for LAN + localhost access…" +PG_HBA="/etc/postgresql/$(ls /etc/postgresql)/main/pg_hba.conf" + +# Ensure localhost entry exists +if ! grep -q "host *all *all *127.0.0.1/32" "$PG_HBA"; then + echo "host all all 127.0.0.1/32 md5" | sudo tee -a "$PG_HBA" +fi + +# Ensure LAN entry exists +if ! grep -q "$LAN_SUBNET" "$PG_HBA"; then + echo "host all all $LAN_SUBNET md5" | sudo tee -a "$PG_HBA" +fi + +echo "[4] Restarting PostgreSQL…" +sudo systemctl restart postgresql + +echo "[5] Creating database and user (if not existing)…" sudo -u postgres psql <