Add squid add user script

This commit is contained in:
Sebastian Cabrera 2025-04-21 20:13:58 -04:00
parent 8cf6e7612a
commit bc7ecfe3c1
No known key found for this signature in database

48
scripts/adduser.sh Executable file
View file

@ -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."