Skip to content

Commit e0e8749

Browse files
add_whitelists.sh
1 parent 216dd8c commit e0e8749

File tree

1 file changed

+5
-104
lines changed

1 file changed

+5
-104
lines changed

add_whitelists.sh

Lines changed: 5 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ POSTGREY_FILE="/etc/postgrey/whitelist_clients.local"
66
BACKUP_DATE="$(date +%F_%H%M%S)"
77

88
usage() {
9-
cat <<EOF
9+
cat <<'EOF'
1010
Usage:
11-
$0 [-n] <domain-or-ip>
12-
$0 [-n] -f <file_with_entries>
11+
./add_whitelists.sh [-n] <domain-or-ip>
12+
./add_whitelists.sh [-n] -f <file_with_entries>
1313
1414
Options:
1515
-f FILE File with entries (one per line, empty lines and #comments ignored)
@@ -29,7 +29,7 @@ while getopts ":f:nh" opt; do
2929
*) usage ;;
3030
esac
3131
done
32-
shift $((OPTIND -1))
32+
shift $((OPTIND - 1))
3333

3434
SINGLE_TARGET="${1:-}"
3535

@@ -70,103 +70,4 @@ backup_if_exists() {
7070
}
7171

7272
is_domain() {
73-
local s="$1"
74-
[[ "$s" =~ ^([A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,}$ ]]
75-
}
76-
77-
is_ipv4() {
78-
local s="$1"
79-
[[ "$s" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]
80-
}
81-
82-
is_cidr() {
83-
local s="$1"
84-
[[ "$s" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}$ ]]
85-
}
86-
87-
already_in_file() {
88-
local needle="$1"
89-
local file="$2"
90-
grep -qE -- "^${needle//./\\.}([[:space:]]|$)" "$file"
91-
}
92-
93-
add_postfix() {
94-
local v="$1"
95-
if already_in_file "$v" "$POSTFIX_FILE"; then
96-
msg "ℹ️ Already in Postfix: $v"
97-
return 1
98-
fi
99-
msg "➕ Adding to Postfix: $v OK"
100-
[[ $DRY -eq 0 ]] && echo "$v OK" >> "$POSTFIX_FILE"
101-
return 0
102-
}
103-
104-
add_postgrey() {
105-
local v="$1"
106-
if already_in_file "$v" "$POSTGREY_FILE"; then
107-
msg "ℹ️ Already in Postgrey: $v"
108-
return 1
109-
fi
110-
msg "➕ Adding to Postgrey: $v"
111-
[[ $DRY -eq 0 ]] && echo "$v" >> "$POSTGREY_FILE"
112-
return 0
113-
}
114-
115-
require_root
116-
msg "🔧 Dry-run: $DRY"
117-
118-
ensure_file "$POSTFIX_FILE"
119-
ensure_file "$POSTGREY_FILE"
120-
backup_if_exists "$POSTFIX_FILE"
121-
backup_if_exists "$POSTGREY_FILE"
122-
123-
CHANGED_POSTFIX=0
124-
CHANGED_POSTGREY=0
125-
ERRORS=0
126-
127-
process_entry() {
128-
local raw="$1"
129-
local entry
130-
entry="$(echo "$raw" | tr '[:upper:]' '[:lower:]' | xargs)"
131-
[[ -z "$entry" ]] && return 0
132-
[[ "$entry" =~ ^# ]] && return 0
133-
134-
if is_cidr "$entry"; then
135-
msg "⚠️ CIDR '$entry' not supported in hash map."
136-
return 0
137-
elif is_ipv4 "$entry"; then
138-
add_postfix "$entry" && CHANGED_POSTFIX=1 || true
139-
elif is_domain "$entry"; then
140-
add_postfix "$entry" && CHANGED_POSTFIX=1 || true
141-
add_postgrey "$entry" && CHANGED_POSTGREY=1 || true
142-
else
143-
msg "❌ Invalid entry: $entry"
144-
ERRORS=$((ERRORS+1))
145-
return 1
146-
fi
147-
}
148-
149-
if [[ -n "$LIST_FILE" ]]; then
150-
[[ -f "$LIST_FILE" ]] || die "File not found: $LIST_FILE"
151-
while IFS= read -r line || [[ -n "$line" ]]; do
152-
process_entry "$line" || true
153-
done < "$LIST_FILE"
154-
else
155-
process_entry "$SINGLE_TARGET" || true
156-
fi
157-
158-
if [[ $DRY -eq 0 ]]; then
159-
if [[ $CHANGED_POSTFIX -eq 1 ]]; then
160-
msg "🧰 postmap $POSTFIX_FILE"
161-
postmap "$POSTFIX_FILE"
162-
msg "🔄 Restarting Postfix"
163-
systemctl restart postfix
164-
fi
165-
if [[ $CHANGED_POSTGREY -eq 1 ]]; then
166-
msg "🔄 Restarting Postgrey"
167-
systemctl restart postgrey || true
168-
fi
169-
msg "✅ Done. Changes: Postfix=${CHANGED_POSTFIX}, Postgrey=${CHANGED_POSTGREY}, Errors=${ERRORS}"
170-
else
171-
msg "🔎 Dry-run complete. Would change: Postfix=${CHANGED_POSTFIX}, Postgrey=${CHANGED_POSTGREY}, Errors=${ERRORS}"
172-
fi
73+
local s

0 commit comments

Comments
 (0)