Skip to content

Commit 8dd3811

Browse files
add_whitelists.sh
1 parent 04ebcfd commit 8dd3811

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

add_whitelists.sh

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,91 @@ is_ipv4() {
9090

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

0 commit comments

Comments
 (0)