|
1 | | -MIAB Whitelists |
| 1 | +# MIAB Whitelists |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +[](LICENSE) |
2 | 7 |
|
3 | 8 | Automate adding domains and IP addresses to Postfix and Postgrey whitelists on Mail-in-a-Box servers with a single, universal Bash script. |
4 | | -📝 Overview |
5 | | -The add_whitelists.sh script simplifies whitelisting domains and IP addresses for Postfix and Postgrey on Mail-in-a-Box servers. It supports: |
6 | 9 |
|
7 | | -Reading a text file (whitelist.txt) with one domain or IP per line. |
8 | | -Adding a single domain or IP directly via command-line argument. |
9 | | -Dry-run mode to preview changes without modifying files. |
10 | | -Creating timestamped backups of existing whitelist files. |
11 | | -Adding entries to Postfix (/etc/postfix/client_whitelist) with OK suffix. |
12 | | -Adding domain entries to Postgrey (/etc/postgrey/whitelist_clients.local). |
13 | | -Rebuilding the Postfix hash database and restarting services as needed. |
| 10 | +--- |
| 11 | + |
| 12 | +## 📖 Table of Contents |
| 13 | + |
| 14 | +* [Overview](#overview) |
| 15 | +* [Prerequisites](#prerequisites) |
| 16 | +* [Installation](#installation) |
| 17 | +* [Configuration](#configuration) |
| 18 | +* [Usage](#usage) |
| 19 | +* [Backup & Safety](#backup--safety) |
| 20 | +* [Examples](#examples) |
| 21 | +* [Contributing](#contributing) |
| 22 | +* [License](#license) |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## 📝 Overview |
| 27 | + |
| 28 | +`add_whitelists.sh` reads a simple text file (`whitelist.txt`) with one domain or IP (CIDR supported) per line, then: |
| 29 | + |
| 30 | +1. Creates timestamped backups of your existing whitelist files. |
| 31 | +2. Adds entries to Postfix (`/etc/postfix/client_whitelist`) suffixed with `OK`. |
| 32 | +3. Adds domain entries to Postgrey (`/etc/postgrey/whitelist_clients.local`). |
| 33 | +4. Rebuilds the Postfix hash database and restarts both services. |
| 34 | + |
| 35 | +Keeping your whitelist entries in a separate file lets you safely publish this script on GitHub without exposing private data. |
14 | 36 |
|
15 | | -The whitelist entries are stored in a separate file or provided as arguments, ensuring sensitive data is not hardcoded in the script. |
16 | | -⚙️ Prerequisites |
| 37 | +--- |
17 | 38 |
|
18 | | -OS: Debian / Ubuntu |
19 | | -Services: Postfix and Postgrey installed |
20 | | -Permissions: Root or sudo access to modify /etc/postfix and /etc/postgrey |
21 | | -Input Format: Domains (e.g., example.com, mail.partner-domain.org) or IPv4 addresses (e.g., 198.51.100.1). CIDR notation (e.g., 198.51.100.0/24) is not supported in Postfix hash maps and will be skipped with a warning. |
| 39 | +## ⚙️ Prerequisites |
22 | 40 |
|
23 | | -🚀 Installation |
| 41 | +* **OS:** Debian / Ubuntu |
| 42 | +* **Services:** Postfix & Postgrey installed |
| 43 | +* **Permissions:** Root or `sudo` to modify `/etc/postfix` and `/etc/postgrey` |
24 | 44 |
|
25 | | -Clone the repository: |
26 | | -git clone https://github.com/Anton-Babaskin/miab-whitelists.git |
27 | | -cd miab-whitelists |
| 45 | +--- |
28 | 46 |
|
| 47 | +## 🚀 Installation |
29 | 48 |
|
30 | | -Create a whitelist.txt file (optional, for bulk entries): |
31 | | -# whitelist.txt |
32 | | -example.com |
33 | | -198.51.100.1 |
34 | | -mail.partner-domain.org |
| 49 | +1. **Clone the repo** |
35 | 50 |
|
| 51 | + ```bash |
| 52 | + git clone https://github.com/Anton-Babaskin/miab-whitelists.git |
| 53 | + cd miab-whitelists |
| 54 | + ``` |
| 55 | +2. **Create your whitelist file** |
36 | 56 |
|
37 | | -Make the script executable: |
38 | | -chmod +x add_whitelists.sh |
| 57 | + ```text |
| 58 | + # whitelist.txt |
| 59 | + example.com |
| 60 | + 198.51.100.0/24 |
| 61 | + mail.partner-domain.org |
| 62 | + ``` |
| 63 | +3. **Make the script executable** |
39 | 64 |
|
| 65 | + ```bash |
| 66 | + chmod +x add_whitelists.sh |
| 67 | + ``` |
40 | 68 |
|
| 69 | +--- |
41 | 70 |
|
42 | | -🛠️ Configuration |
43 | | -Customize file paths in the script header if needed: |
| 71 | +## 🛠️ Configuration |
| 72 | + |
| 73 | +Customize paths in the script header if needed: |
| 74 | + |
| 75 | +```bash |
44 | 76 | # add_whitelists.sh |
45 | 77 | POSTFIX_FILE="/etc/postfix/client_whitelist" |
46 | 78 | POSTGREY_FILE="/etc/postgrey/whitelist_clients.local" |
| 79 | +``` |
| 80 | + |
| 81 | +--- |
47 | 82 |
|
48 | | -Ensure /etc/postfix/main.cf includes the Postfix whitelist in smtpd_client_restrictions, e.g.: |
49 | | -smtpd_client_restrictions = ..., check_client_access hash:/etc/postfix/client_whitelist, ... |
| 83 | +## ▶️ Usage |
50 | 84 |
|
51 | | -▶️ Usage |
52 | | -Add entries from a file |
53 | | -Run the script with a whitelist file: |
54 | | -sudo ./add_whitelists.sh -f whitelist.txt |
| 85 | +Run the script with your whitelist file: |
55 | 86 |
|
56 | | -Add a single entry |
57 | | -Add a single domain or IP directly: |
58 | | -sudo ./add_whitelists.sh example.com |
59 | | -sudo ./add_whitelists.sh 198.51.100.1 |
| 87 | +```bash |
| 88 | +sudo ./add_whitelists.sh whitelist.txt |
| 89 | +``` |
| 90 | +Quick add: add any single domain or IP with one command, no file needed: |
| 91 | +```bash |
| 92 | +sudo ./add_whitelists.sh YOURDOMAIN.com |
| 93 | +``` |
| 94 | +**What happens under the hood:** |
60 | 95 |
|
61 | | -Dry-run mode |
62 | | -Preview changes without applying them: |
63 | | -sudo ./add_whitelists.sh -n -f whitelist.txt |
64 | | -sudo ./add_whitelists.sh -n example.com |
| 96 | +1. Backups: |
65 | 97 |
|
66 | | -What happens |
| 98 | + ```bash |
| 99 | + /etc/postfix/client_whitelist.bak_YYYY-MM-DD_HH:MM:SS |
| 100 | + /etc/postgrey/whitelist_clients.local.bak_YYYY-MM-DD_HH:MM:SS |
| 101 | + ``` |
| 102 | +2. Reads each line: |
67 | 103 |
|
68 | | -Backups: Creates timestamped backups of whitelist files (e.g., client_whitelist.bak_YYYY-MM-DD_HHMMSS). |
69 | | -Processing: |
70 | | -Skips empty lines, comments (#), or invalid entries. |
71 | | -Warns about unsupported CIDR entries and skips them. |
72 | | -Adds valid IPs or domains to Postfix (ENTRY OK). |
73 | | -Adds valid domains (not IPs) to Postgrey (ENTRY). |
| 104 | + * If missing in Postfix: appends `ENTRY OK`. |
| 105 | + * If a domain (not IP/CIDR) and missing in Postgrey: appends `ENTRY`. |
| 106 | +3. Applies changes: |
74 | 107 |
|
| 108 | + ```bash |
| 109 | + postmap "$POSTFIX_FILE" |
| 110 | + systemctl restart postfix |
| 111 | + systemctl restart postgrey |
| 112 | + ``` |
75 | 113 |
|
76 | | -Application: |
77 | | -Runs postmap on the Postfix whitelist if modified. |
78 | | -Restarts postfix and postgrey services if changes are made. |
| 114 | +--- |
79 | 115 |
|
| 116 | +## 🛡️ Backup & Safety |
80 | 117 |
|
| 118 | +Before making changes, the script creates timestamped backups. To restore from backup: |
81 | 119 |
|
82 | | -🛡️ Backup & Safety |
83 | | -The script creates timestamped backups before modifying files. To restore: |
84 | | -sudo cp /etc/postfix/client_whitelist.bak_YYYY-MM-DD_HHMMSS /etc/postfix/client_whitelist |
85 | | -sudo cp /etc/postgrey/whitelist_clients.local.bak_YYYY-MM-DD_HHMMSS /etc/postgrey/whitelist_clients.local |
| 120 | +```bash |
| 121 | +sudo cp /etc/postfix/client_whitelist.bak_YYYY-MM-DD_HH:MM:SS /etc/postfix/client_whitelist |
| 122 | +sudo cp /etc/postgrey/whitelist_clients.local.bak_YYYY-MM-DD_HH:MM:SS /etc/postgrey/whitelist_clients.local |
86 | 123 | sudo postmap /etc/postfix/client_whitelist |
87 | 124 | sudo systemctl restart postfix postgrey |
| 125 | +``` |
88 | 126 |
|
89 | | -To automatically delete backups older than 30 days, add to the script or run separately: |
| 127 | +**Backup rotation:** |
| 128 | +To automatically delete backups older than 30 days, add this line at the end of your script or run it separately: |
| 129 | + |
| 130 | +```bash |
90 | 131 | find /etc/postfix -name "client_whitelist.bak_*" -mtime +30 -delete |
91 | | -find /etc/postgrey -name "whitelist_clients.local.bak_*" -mtime +30 -delete |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
92 | 135 |
|
93 | | -💡 Examples |
94 | | -Bulk add via file |
95 | | -echo -e "partner.com\n198.51.100.1" > whitelist.txt |
96 | | -sudo ./add_whitelists.sh -f whitelist.txt |
| 136 | +## 💡 Examples |
97 | 137 |
|
98 | | -Single entry |
99 | | -sudo ./add_whitelists.sh mail.example.org |
| 138 | +**One-liner:** |
100 | 139 |
|
101 | | -Automate via cron |
| 140 | +```bash |
| 141 | +echo -e "partner.com |
| 142 | +198.51.100.0/24" > whitelist.txt |
| 143 | +sudo ./add_whitelists.sh whitelist.txt |
| 144 | +``` |
| 145 | + |
| 146 | +**Automate via cron:** |
| 147 | + |
| 148 | +```cron |
102 | 149 | # /etc/cron.daily/miab-whitelist |
103 | 150 | #!/bin/bash |
104 | 151 | cd /opt/miab-whitelists |
105 | 152 | git pull --ff-only |
106 | | -/opt/miab-whitelists/add_whitelists.sh -f /opt/miab-whitelists/whitelist.txt |
| 153 | +/opt/miab-whitelists/add_whitelists.sh /opt/miab-whitelists/whitelist.txt |
| 154 | +``` |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +## 🤝 Contributing |
| 159 | + |
| 160 | +Pull requests and issues welcome! |
| 161 | +Please follow the standard fork → branch → PR workflow. |
107 | 162 |
|
108 | | -⚠️ Notes |
| 163 | +--- |
109 | 164 |
|
110 | | -CIDR Limitation: CIDR notation (e.g., 198.51.100.0/24) is not supported in Postfix hash maps. Use a separate CIDR table in /etc/postfix/main.cf (e.g., check_client_access cidr:/etc/postfix/client_whitelist.cidr). |
111 | | -Validation: The script validates domains and IPv4 addresses, skipping invalid entries with warnings. |
112 | | -Case Handling: Entries are converted to lowercase to ensure consistency. |
113 | | -Main.cf Check: The script checks if client_whitelist is referenced in /etc/postfix/main.cf and warns if missing. |
| 165 | +## 📜 License |
114 | 166 |
|
115 | | -🤝 Contributing |
116 | | -Contributions are welcome! Please follow the standard fork → branch → pull request workflow. Report issues or suggest improvements via GitHub Issues. |
117 | | -📜 License |
118 | | -MIT © Anton Babaskin. See LICENSE for details. |
| 167 | +MIT © Anton Babaskin. See [LICENSE](LICENSE) for details. |
| 168 | + |
0 commit comments