Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | trap "postfix stop" EXIT |
| 4 | |
| 5 | [[ ! -d /opt/postfix/conf/sql/ ]] && mkdir -p /opt/postfix/conf/sql/ |
| 6 | |
| 7 | # Wait for MySQL to warm-up |
| 8 | while ! mysqladmin status --socket=/var/run/mysqld/mysqld.sock -u${DBUSER} -p${DBPASS} --silent; do |
| 9 | echo "Waiting for database to come up..." |
| 10 | sleep 2 |
| 11 | done |
| 12 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 13 | until dig +short mailcow.email > /dev/null; do |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 14 | echo "Waiting for DNS..." |
| 15 | sleep 1 |
| 16 | done |
| 17 | |
| 18 | cat <<EOF > /etc/aliases |
| 19 | # Autogenerated by mailcow |
| 20 | null: /dev/null |
| 21 | watchdog: /dev/null |
| 22 | ham: "|/usr/local/bin/rspamd-pipe-ham" |
| 23 | spam: "|/usr/local/bin/rspamd-pipe-spam" |
| 24 | EOF |
| 25 | newaliases; |
| 26 | |
| 27 | # create sni configuration |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 28 | if [[ "${SKIP_LETS_ENCRYPT}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then |
| 29 | echo -n "" > /opt/postfix/conf/sni.map |
| 30 | else |
| 31 | echo -n "" > /opt/postfix/conf/sni.map; |
| 32 | for cert_dir in /etc/ssl/mail/*/ ; do |
| 33 | if [[ ! -f ${cert_dir}domains ]] || [[ ! -f ${cert_dir}cert.pem ]] || [[ ! -f ${cert_dir}key.pem ]]; then |
| 34 | continue; |
| 35 | fi |
| 36 | IFS=" " read -r -a domains <<< "$(cat "${cert_dir}domains")" |
| 37 | for domain in "${domains[@]}"; do |
| 38 | echo -n "${domain} ${cert_dir}key.pem ${cert_dir}cert.pem" >> /opt/postfix/conf/sni.map; |
| 39 | echo "" >> /opt/postfix/conf/sni.map; |
| 40 | done |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 41 | done |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 42 | fi |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 43 | postmap -F hash:/opt/postfix/conf/sni.map; |
| 44 | |
| 45 | cat <<EOF > /opt/postfix/conf/sql/mysql_relay_ne.cf |
| 46 | # Autogenerated by mailcow |
| 47 | user = ${DBUSER} |
| 48 | password = ${DBPASS} |
| 49 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 50 | dbname = ${DBNAME} |
| 51 | query = SELECT IF(EXISTS(SELECT address, domain FROM alias |
| 52 | WHERE address = '%s' |
| 53 | AND domain IN ( |
| 54 | SELECT domain FROM domain |
| 55 | WHERE backupmx = '1' |
| 56 | AND relay_all_recipients = '1' |
| 57 | AND relay_unknown_only = '1') |
| 58 | |
| 59 | ), 'lmtp:inet:dovecot:24', NULL) AS 'transport' |
| 60 | EOF |
| 61 | |
| 62 | cat <<EOF > /opt/postfix/conf/sql/mysql_relay_recipient_maps.cf |
| 63 | # Autogenerated by mailcow |
| 64 | user = ${DBUSER} |
| 65 | password = ${DBPASS} |
| 66 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 67 | dbname = ${DBNAME} |
| 68 | query = SELECT DISTINCT |
| 69 | CASE WHEN '%d' IN ( |
| 70 | SELECT domain FROM domain |
| 71 | WHERE relay_all_recipients=1 |
| 72 | AND domain='%d' |
| 73 | AND backupmx=1 |
| 74 | ) |
| 75 | THEN '%s' ELSE ( |
| 76 | SELECT goto FROM alias WHERE address='%s' AND active='1' |
| 77 | ) |
| 78 | END AS result; |
| 79 | EOF |
| 80 | |
| 81 | cat <<EOF > /opt/postfix/conf/sql/mysql_tls_policy_override_maps.cf |
| 82 | # Autogenerated by mailcow |
| 83 | user = ${DBUSER} |
| 84 | password = ${DBPASS} |
| 85 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 86 | dbname = ${DBNAME} |
| 87 | query = SELECT CONCAT(policy, ' ', parameters) AS tls_policy FROM tls_policy_override WHERE active = '1' AND dest = '%s' |
| 88 | EOF |
| 89 | |
| 90 | cat <<EOF > /opt/postfix/conf/sql/mysql_tls_enforce_in_policy.cf |
| 91 | # Autogenerated by mailcow |
| 92 | user = ${DBUSER} |
| 93 | password = ${DBPASS} |
| 94 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 95 | dbname = ${DBNAME} |
| 96 | query = SELECT IF(EXISTS( |
| 97 | SELECT 'TLS_ACTIVE' FROM alias |
| 98 | LEFT OUTER JOIN mailbox ON mailbox.username = alias.goto |
| 99 | WHERE (address='%s' |
| 100 | OR address IN ( |
| 101 | SELECT CONCAT('%u', '@', target_domain) FROM alias_domain |
| 102 | WHERE alias_domain='%d' |
| 103 | ) |
| 104 | ) AND JSON_UNQUOTE(JSON_VALUE(attributes, '$.tls_enforce_in')) = '1' AND mailbox.active = '1' |
| 105 | ), 'reject_plaintext_session', NULL) AS 'tls_enforce_in'; |
| 106 | EOF |
| 107 | |
| 108 | cat <<EOF > /opt/postfix/conf/sql/mysql_sender_dependent_default_transport_maps.cf |
| 109 | # Autogenerated by mailcow |
| 110 | user = ${DBUSER} |
| 111 | password = ${DBPASS} |
| 112 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 113 | dbname = ${DBNAME} |
| 114 | query = SELECT GROUP_CONCAT(transport SEPARATOR '') AS transport_maps |
| 115 | FROM ( |
| 116 | SELECT IF(EXISTS(SELECT 'smtp_type' FROM alias |
| 117 | LEFT OUTER JOIN mailbox ON mailbox.username = alias.goto |
| 118 | WHERE (address = '%s' |
| 119 | OR address IN ( |
| 120 | SELECT CONCAT('%u', '@', target_domain) FROM alias_domain |
| 121 | WHERE alias_domain = '%d' |
| 122 | ) |
| 123 | ) |
| 124 | AND JSON_UNQUOTE(JSON_VALUE(attributes, '$.tls_enforce_out')) = '1' |
| 125 | AND mailbox.active = '1' |
| 126 | ), 'smtp_enforced_tls:', 'smtp:') AS 'transport' |
| 127 | UNION ALL |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 128 | SELECT COALESCE( |
| 129 | (SELECT hostname FROM relayhosts |
| 130 | LEFT OUTER JOIN mailbox ON JSON_UNQUOTE(JSON_VALUE(mailbox.attributes, '$.relayhost')) = relayhosts.id |
| 131 | WHERE relayhosts.active = '1' |
| 132 | AND ( |
| 133 | mailbox.username IN (SELECT alias.goto from alias |
| 134 | JOIN mailbox ON mailbox.username = alias.goto |
| 135 | WHERE alias.active = '1' |
| 136 | AND alias.address = '%s' |
| 137 | AND alias.address NOT LIKE '@%%' |
| 138 | ) |
| 139 | ) |
| 140 | ), |
| 141 | (SELECT hostname FROM relayhosts |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 142 | LEFT OUTER JOIN domain ON domain.relayhost = relayhosts.id |
| 143 | WHERE relayhosts.active = '1' |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 144 | AND (domain.domain = '%d' |
| 145 | OR domain.domain IN ( |
| 146 | SELECT target_domain FROM alias_domain |
| 147 | WHERE alias_domain = '%d' |
| 148 | ) |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 149 | ) |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 150 | ) |
| 151 | ) |
| 152 | ) AS transport_view; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 153 | EOF |
| 154 | |
| 155 | cat <<EOF > /opt/postfix/conf/sql/mysql_transport_maps.cf |
| 156 | # Autogenerated by mailcow |
| 157 | user = ${DBUSER} |
| 158 | password = ${DBPASS} |
| 159 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 160 | dbname = ${DBNAME} |
| 161 | query = SELECT CONCAT('smtp_via_transport_maps:', nexthop) AS transport FROM transports |
| 162 | WHERE active = '1' |
| 163 | AND destination = '%s'; |
| 164 | EOF |
| 165 | |
| 166 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_resource_maps.cf |
| 167 | # Autogenerated by mailcow |
| 168 | user = ${DBUSER} |
| 169 | password = ${DBPASS} |
| 170 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 171 | dbname = ${DBNAME} |
| 172 | query = SELECT 'null@localhost' FROM mailbox |
| 173 | WHERE kind REGEXP 'location|thing|group' AND username = '%s'; |
| 174 | EOF |
| 175 | |
| 176 | cat <<EOF > /opt/postfix/conf/sql/mysql_sasl_passwd_maps_sender_dependent.cf |
| 177 | # Autogenerated by mailcow |
| 178 | user = ${DBUSER} |
| 179 | password = ${DBPASS} |
| 180 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 181 | dbname = ${DBNAME} |
| 182 | query = SELECT CONCAT_WS(':', username, password) AS auth_data FROM relayhosts |
| 183 | WHERE id IN ( |
Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame] | 184 | SELECT COALESCE( |
| 185 | (SELECT id FROM relayhosts |
| 186 | LEFT OUTER JOIN domain ON domain.relayhost = relayhosts.id |
| 187 | WHERE relayhosts.active = '1' |
| 188 | AND (domain.domain = '%d' |
| 189 | OR domain.domain IN ( |
| 190 | SELECT target_domain FROM alias_domain |
| 191 | WHERE alias_domain = '%d' |
| 192 | ) |
| 193 | ) |
| 194 | ), |
| 195 | (SELECT id FROM relayhosts |
| 196 | LEFT OUTER JOIN mailbox ON JSON_UNQUOTE(JSON_VALUE(mailbox.attributes, '$.relayhost')) = relayhosts.id |
| 197 | WHERE relayhosts.active = '1' |
| 198 | AND ( |
| 199 | mailbox.username IN ( |
| 200 | SELECT alias.goto from alias |
| 201 | JOIN mailbox ON mailbox.username = alias.goto |
| 202 | WHERE alias.active = '1' |
| 203 | AND alias.address = '%s' |
| 204 | AND alias.address NOT LIKE '@%%' |
| 205 | ) |
| 206 | ) |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 207 | ) |
Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame] | 208 | ) |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 209 | ) |
| 210 | AND active = '1' |
| 211 | AND username != ''; |
| 212 | EOF |
| 213 | |
| 214 | cat <<EOF > /opt/postfix/conf/sql/mysql_sasl_passwd_maps_transport_maps.cf |
| 215 | # Autogenerated by mailcow |
| 216 | user = ${DBUSER} |
| 217 | password = ${DBPASS} |
| 218 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 219 | dbname = ${DBNAME} |
| 220 | query = SELECT CONCAT_WS(':', username, password) AS auth_data FROM transports |
| 221 | WHERE nexthop = '%s' |
| 222 | AND active = '1' |
| 223 | AND username != '' |
| 224 | LIMIT 1; |
| 225 | EOF |
| 226 | |
| 227 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_alias_domain_maps.cf |
| 228 | # Autogenerated by mailcow |
| 229 | user = ${DBUSER} |
| 230 | password = ${DBPASS} |
| 231 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 232 | dbname = ${DBNAME} |
| 233 | query = SELECT username FROM mailbox, alias_domain |
| 234 | WHERE alias_domain.alias_domain = '%d' |
| 235 | AND mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) |
| 236 | AND (mailbox.active = '1' OR mailbox.active = '2') |
| 237 | AND alias_domain.active='1' |
| 238 | EOF |
| 239 | |
| 240 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_alias_maps.cf |
| 241 | # Autogenerated by mailcow |
| 242 | user = ${DBUSER} |
| 243 | password = ${DBPASS} |
| 244 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 245 | dbname = ${DBNAME} |
| 246 | query = SELECT goto FROM alias |
| 247 | WHERE address='%s' |
| 248 | AND (active='1' OR active='2'); |
| 249 | EOF |
| 250 | |
| 251 | cat <<EOF > /opt/postfix/conf/sql/mysql_recipient_bcc_maps.cf |
| 252 | # Autogenerated by mailcow |
| 253 | user = ${DBUSER} |
| 254 | password = ${DBPASS} |
| 255 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 256 | dbname = ${DBNAME} |
| 257 | query = SELECT bcc_dest FROM bcc_maps |
| 258 | WHERE local_dest='%s' |
| 259 | AND type='rcpt' |
| 260 | AND active='1'; |
| 261 | EOF |
| 262 | |
| 263 | cat <<EOF > /opt/postfix/conf/sql/mysql_sender_bcc_maps.cf |
| 264 | # Autogenerated by mailcow |
| 265 | user = ${DBUSER} |
| 266 | password = ${DBPASS} |
| 267 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 268 | dbname = ${DBNAME} |
| 269 | query = SELECT bcc_dest FROM bcc_maps |
| 270 | WHERE local_dest='%s' |
| 271 | AND type='sender' |
| 272 | AND active='1'; |
| 273 | EOF |
| 274 | |
| 275 | cat <<EOF > /opt/postfix/conf/sql/mysql_recipient_canonical_maps.cf |
| 276 | # Autogenerated by mailcow |
| 277 | user = ${DBUSER} |
| 278 | password = ${DBPASS} |
| 279 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 280 | dbname = ${DBNAME} |
| 281 | query = SELECT new_dest FROM recipient_maps |
| 282 | WHERE old_dest='%s' |
| 283 | AND active='1'; |
| 284 | EOF |
| 285 | |
| 286 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_domains_maps.cf |
| 287 | # Autogenerated by mailcow |
| 288 | user = ${DBUSER} |
| 289 | password = ${DBPASS} |
| 290 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 291 | dbname = ${DBNAME} |
| 292 | query = SELECT alias_domain from alias_domain WHERE alias_domain='%s' AND active='1' |
| 293 | UNION |
| 294 | SELECT domain FROM domain |
| 295 | WHERE domain='%s' |
| 296 | AND active = '1' |
| 297 | AND backupmx = '0' |
| 298 | EOF |
| 299 | |
| 300 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_mailbox_maps.cf |
| 301 | # Autogenerated by mailcow |
| 302 | user = ${DBUSER} |
| 303 | password = ${DBPASS} |
| 304 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 305 | dbname = ${DBNAME} |
| 306 | query = SELECT CONCAT(JSON_UNQUOTE(JSON_VALUE(attributes, '$.mailbox_format')), mailbox_path_prefix, '%d/%u/') FROM mailbox WHERE username='%s' AND (active = '1' OR active = '2') |
| 307 | EOF |
| 308 | |
| 309 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_relay_domain_maps.cf |
| 310 | # Autogenerated by mailcow |
| 311 | user = ${DBUSER} |
| 312 | password = ${DBPASS} |
| 313 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 314 | dbname = ${DBNAME} |
| 315 | query = SELECT domain FROM domain WHERE domain='%s' AND backupmx = '1' AND active = '1' |
| 316 | EOF |
| 317 | |
| 318 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_sender_acl.cf |
| 319 | # Autogenerated by mailcow |
| 320 | user = ${DBUSER} |
| 321 | password = ${DBPASS} |
| 322 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 323 | dbname = ${DBNAME} |
| 324 | # First select queries domain and alias_domain to determine if domains are active. |
| 325 | query = SELECT goto FROM alias |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 326 | WHERE id IN ( |
| 327 | SELECT COALESCE ( |
| 328 | ( |
| 329 | SELECT id FROM alias |
| 330 | WHERE address='%s' |
| 331 | AND (active='1' OR active='2') |
| 332 | ), ( |
| 333 | SELECT id FROM alias |
| 334 | WHERE address='@%d' |
| 335 | AND (active='1' OR active='2') |
| 336 | ) |
| 337 | ) |
| 338 | ) |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 339 | AND active='1' |
| 340 | AND (domain IN |
| 341 | (SELECT domain FROM domain |
| 342 | WHERE domain='%d' |
| 343 | AND active='1') |
| 344 | OR domain in ( |
| 345 | SELECT alias_domain FROM alias_domain |
| 346 | WHERE alias_domain='%d' |
| 347 | AND active='1' |
| 348 | ) |
| 349 | ) |
| 350 | UNION |
| 351 | SELECT logged_in_as FROM sender_acl |
| 352 | WHERE send_as='@%d' |
| 353 | OR send_as='%s' |
| 354 | OR send_as='*' |
| 355 | OR send_as IN ( |
| 356 | SELECT CONCAT('@',target_domain) FROM alias_domain |
| 357 | WHERE alias_domain = '%d') |
| 358 | OR send_as IN ( |
| 359 | SELECT CONCAT('%u','@',target_domain) FROM alias_domain |
| 360 | WHERE alias_domain = '%d') |
| 361 | AND logged_in_as NOT IN ( |
| 362 | SELECT goto FROM alias |
| 363 | WHERE address='%s') |
| 364 | UNION |
| 365 | SELECT username FROM mailbox, alias_domain |
| 366 | WHERE alias_domain.alias_domain = '%d' |
| 367 | AND mailbox.username = CONCAT('%u','@',alias_domain.target_domain) |
| 368 | AND (mailbox.active = '1' OR mailbox.active ='2') |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 369 | AND alias_domain.active='1'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 370 | EOF |
| 371 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 372 | # MX based routing |
| 373 | cat <<EOF > /opt/postfix/conf/sql/mysql_mbr_access_maps.cf |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 374 | # Autogenerated by mailcow |
| 375 | user = ${DBUSER} |
| 376 | password = ${DBPASS} |
| 377 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 378 | dbname = ${DBNAME} |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 379 | query = SELECT CONCAT('FILTER smtp_via_transport_maps:', nexthop) as transport FROM transports |
| 380 | WHERE '%s' REGEXP destination |
| 381 | AND active='1' |
| 382 | AND is_mx_based='1'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 383 | EOF |
| 384 | |
| 385 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_spamalias_maps.cf |
| 386 | # Autogenerated by mailcow |
| 387 | user = ${DBUSER} |
| 388 | password = ${DBPASS} |
| 389 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 390 | dbname = ${DBNAME} |
| 391 | query = SELECT goto FROM spamalias |
| 392 | WHERE address='%s' |
| 393 | AND validity >= UNIX_TIMESTAMP() |
| 394 | EOF |
| 395 | |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 396 | if [ ! -f /opt/postfix/conf/dns_blocklists.cf ]; then |
| 397 | cat <<EOF > /opt/postfix/conf/dns_blocklists.cf |
| 398 | # This file can be edited. |
| 399 | # Delete this file and restart postfix container to revert any changes. |
| 400 | postscreen_dnsbl_sites = wl.mailspike.net=127.0.0.[18;19;20]*-2 |
| 401 | hostkarma.junkemailfilter.com=127.0.0.1*-2 |
| 402 | list.dnswl.org=127.0.[0..255].0*-2 |
| 403 | list.dnswl.org=127.0.[0..255].1*-4 |
| 404 | list.dnswl.org=127.0.[0..255].2*-6 |
| 405 | list.dnswl.org=127.0.[0..255].3*-8 |
| 406 | ix.dnsbl.manitu.net*2 |
| 407 | bl.spamcop.net*2 |
| 408 | bl.suomispam.net*2 |
| 409 | hostkarma.junkemailfilter.com=127.0.0.2*3 |
| 410 | hostkarma.junkemailfilter.com=127.0.0.4*2 |
| 411 | hostkarma.junkemailfilter.com=127.0.1.2*1 |
| 412 | backscatter.spameatingmonkey.net*2 |
| 413 | bl.ipv6.spameatingmonkey.net*2 |
| 414 | bl.spameatingmonkey.net*2 |
| 415 | b.barracudacentral.org=127.0.0.2*7 |
| 416 | bl.mailspike.net=127.0.0.2*5 |
| 417 | bl.mailspike.net=127.0.0.[10;11;12]*4 |
| 418 | dnsbl.sorbs.net=127.0.0.10*8 |
| 419 | dnsbl.sorbs.net=127.0.0.5*6 |
| 420 | dnsbl.sorbs.net=127.0.0.7*3 |
| 421 | dnsbl.sorbs.net=127.0.0.8*2 |
| 422 | dnsbl.sorbs.net=127.0.0.6*2 |
| 423 | dnsbl.sorbs.net=127.0.0.9*2 |
| 424 | EOF |
| 425 | fi |
| 426 | DNSBL_CONFIG=$(grep -v '^#' /opt/postfix/conf/dns_blocklists.cf | grep '\S') |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 427 | |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 428 | if [ ! -z "$DNSBL_CONFIG" ]; then |
| 429 | echo -e "\e[33mChecking if ASN for your IP is listed for Spamhaus Bad ASN List...\e[0m" |
| 430 | if [ -n "$SPAMHAUS_DQS_KEY" ]; then |
| 431 | echo -e "\e[32mDetected SPAMHAUS_DQS_KEY variable from mailcow.conf...\e[0m" |
| 432 | echo -e "\e[33mUsing DQS Blocklists from Spamhaus!\e[0m" |
| 433 | SPAMHAUS_DNSBL_CONFIG=$(cat <<EOF |
| 434 | ${SPAMHAUS_DQS_KEY}.zen.dq.spamhaus.net=127.0.0.[4..7]*6 |
| 435 | ${SPAMHAUS_DQS_KEY}.zen.dq.spamhaus.net=127.0.0.[10;11]*8 |
| 436 | ${SPAMHAUS_DQS_KEY}.zen.dq.spamhaus.net=127.0.0.3*4 |
| 437 | ${SPAMHAUS_DQS_KEY}.zen.dq.spamhaus.net=127.0.0.2*3 |
| 438 | postscreen_dnsbl_reply_map = texthash:/opt/postfix/conf/dnsbl_reply.map |
| 439 | EOF |
| 440 | |
| 441 | cat <<EOF > /opt/postfix/conf/dnsbl_reply.map |
| 442 | # Autogenerated by mailcow, using Spamhaus DQS reply domains |
| 443 | ${SPAMHAUS_DQS_KEY}.sbl.dq.spamhaus.net sbl.spamhaus.org |
| 444 | ${SPAMHAUS_DQS_KEY}.xbl.dq.spamhaus.net xbl.spamhaus.org |
| 445 | ${SPAMHAUS_DQS_KEY}.pbl.dq.spamhaus.net pbl.spamhaus.org |
| 446 | ${SPAMHAUS_DQS_KEY}.zen.dq.spamhaus.net zen.spamhaus.org |
| 447 | ${SPAMHAUS_DQS_KEY}.dbl.dq.spamhaus.net dbl.spamhaus.org |
| 448 | ${SPAMHAUS_DQS_KEY}.zrd.dq.spamhaus.net zrd.spamhaus.org |
| 449 | EOF |
| 450 | ) |
| 451 | else |
| 452 | if [ -f "/opt/postfix/conf/dnsbl_reply.map" ]; then |
| 453 | rm /opt/postfix/conf/dnsbl_reply.map |
| 454 | fi |
| 455 | response=$(curl --connect-timeout 15 --max-time 30 -s -o /dev/null -w "%{http_code}" "https://asn-check.mailcow.email") |
| 456 | if [ "$response" -eq 503 ]; then |
| 457 | echo -e "\e[31mThe AS of your IP is listed as a banned AS from Spamhaus!\e[0m" |
| 458 | echo -e "\e[33mNo SPAMHAUS_DQS_KEY found... Skipping Spamhaus blocklists entirely!\e[0m" |
| 459 | SPAMHAUS_DNSBL_CONFIG="" |
| 460 | elif [ "$response" -eq 200 ]; then |
| 461 | echo -e "\e[32mThe AS of your IP is NOT listed as a banned AS from Spamhaus!\e[0m" |
| 462 | echo -e "\e[33mUsing the open Spamhaus blocklists.\e[0m" |
| 463 | SPAMHAUS_DNSBL_CONFIG=$(cat <<EOF |
| 464 | zen.spamhaus.org=127.0.0.[10;11]*8 |
| 465 | zen.spamhaus.org=127.0.0.[4..7]*6 |
| 466 | zen.spamhaus.org=127.0.0.3*4 |
| 467 | zen.spamhaus.org=127.0.0.2*3 |
| 468 | EOF |
| 469 | ) |
| 470 | |
| 471 | else |
| 472 | echo -e "\e[31mWe couldn't determine your AS... (maybe DNS/Network issue?) Response Code: $response\e[0m" |
| 473 | echo -e "\e[33mDeactivating Spamhaus DNS Blocklists to be on the safe site!\e[0m" |
| 474 | SPAMHAUS_DNSBL_CONFIG="" |
| 475 | fi |
| 476 | fi |
| 477 | fi |
| 478 | |
| 479 | # Reset main.cf |
| 480 | sed -i '/Overrides/q' /opt/postfix/conf/main.cf |
| 481 | echo >> /opt/postfix/conf/main.cf |
| 482 | # Append postscreen dnsbl sites to main.cf |
| 483 | if [ ! -z "$DNSBL_CONFIG" ]; then |
| 484 | echo -e "${DNSBL_CONFIG}\n${SPAMHAUS_DNSBL_CONFIG}" >> /opt/postfix/conf/main.cf |
| 485 | fi |
| 486 | # Append user overrides |
| 487 | echo -e "\n# User Overrides" >> /opt/postfix/conf/main.cf |
| 488 | touch /opt/postfix/conf/extra.cf |
| 489 | sed -i '/\$myhostname/! { /myhostname/d }' /opt/postfix/conf/extra.cf |
| 490 | echo -e "myhostname = ${MAILCOW_HOSTNAME}\n$(cat /opt/postfix/conf/extra.cf)" > /opt/postfix/conf/extra.cf |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 491 | cat /opt/postfix/conf/extra.cf >> /opt/postfix/conf/main.cf |
| 492 | |
| 493 | if [ ! -f /opt/postfix/conf/custom_transport.pcre ]; then |
| 494 | echo "Creating dummy custom_transport.pcre" |
| 495 | touch /opt/postfix/conf/custom_transport.pcre |
| 496 | fi |
| 497 | |
| 498 | if [[ ! -f /opt/postfix/conf/custom_postscreen_whitelist.cidr ]]; then |
| 499 | echo "Creating dummy custom_postscreen_whitelist.cidr" |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 500 | cat <<EOF > /opt/postfix/conf/custom_postscreen_whitelist.cidr |
| 501 | # Autogenerated by mailcow |
| 502 | # Rules are evaluated in the order as specified. |
| 503 | # Blacklist 192.168.* except 192.168.0.1. |
| 504 | # 192.168.0.1 permit |
| 505 | # 192.168.0.0/16 reject |
| 506 | EOF |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 507 | fi |
| 508 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 509 | # Fix Postfix permissions |
| 510 | chown -R root:postfix /opt/postfix/conf/sql/ /opt/postfix/conf/custom_transport.pcre |
| 511 | chmod 640 /opt/postfix/conf/sql/*.cf /opt/postfix/conf/custom_transport.pcre |
| 512 | chgrp -R postdrop /var/spool/postfix/public |
| 513 | chgrp -R postdrop /var/spool/postfix/maildrop |
| 514 | postfix set-permissions |
| 515 | |
| 516 | # Check Postfix configuration |
| 517 | postconf -c /opt/postfix/conf > /dev/null |
| 518 | |
| 519 | if [[ $? != 0 ]]; then |
| 520 | echo "Postfix configuration error, refusing to start." |
| 521 | exit 1 |
| 522 | else |
| 523 | postfix -c /opt/postfix/conf start |
| 524 | sleep 126144000 |
| 525 | fi |