From bc7ecfe3c15e43a35821c6e28a16519c3b296da4 Mon Sep 17 00:00:00 2001 From: Sebastian Cabrera Date: Mon, 21 Apr 2025 20:13:58 -0400 Subject: [PATCH] Add squid add user script --- scripts/adduser.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 scripts/adduser.sh diff --git a/scripts/adduser.sh b/scripts/adduser.sh new file mode 100755 index 0000000..e76d582 --- /dev/null +++ b/scripts/adduser.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +PASSWD_FILE="/etc/squid/passwd" + +# Ensure htpasswd is installed +if ! command -v htpasswd &>/dev/null; then + echo "❌ 'htpasswd' not found. Please install apache2-utils (Debian/Ubuntu) or httpd-tools (RHEL/CentOS)." + exit 1 +fi + +# Ensure script is run as root +if [[ $EUID -ne 0 ]]; then + echo "❌ This script must be run as root." + exit 1 +fi + +# Prompt for username +read -rp "👤 Enter Squid proxy username: " username +if [[ -z "$username" ]]; then + echo "❌ Username cannot be empty." + exit 1 +fi + +# Create the password file only if it doesn't exist +if [[ ! -f "$PASSWD_FILE" ]]; then + echo "🔧 Creating new password file at $PASSWD_FILE" + htpasswd -c "$PASSWD_FILE" "$username" +else + # Check if user exists + if grep -q "^$username:" "$PASSWD_FILE"; then + echo "⚠️ User '$username' already exists." + read -rp "🔄 Update password? (y/N): " answer + if [[ "$answer" =~ ^[Yy]$ ]]; then + htpasswd "$PASSWD_FILE" "$username" + else + echo "❌ Aborted." + exit 1 + fi + else + htpasswd "$PASSWD_FILE" "$username" + fi +fi + +# Fix permissions +chmod 640 "$PASSWD_FILE" +chown proxy:proxy "$PASSWD_FILE" 2>/dev/null || chown squid:squid "$PASSWD_FILE" 2>/dev/null + +echo "✅ User '$username' is set up for Squid."