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 | |
| 13 | until dig +short mailcow.email @unbound > /dev/null; do |
| 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 |
| 28 | echo -n "" > /opt/postfix/conf/sni.map; |
| 29 | for cert_dir in /etc/ssl/mail/*/ ; do |
| 30 | if [[ ! -f ${cert_dir}domains ]] || [[ ! -f ${cert_dir}cert.pem ]] || [[ ! -f ${cert_dir}key.pem ]]; then |
| 31 | continue; |
| 32 | fi |
| 33 | IFS=" " read -r -a domains <<< "$(cat "${cert_dir}domains")" |
| 34 | for domain in "${domains[@]}"; do |
| 35 | echo -n "${domain} ${cert_dir}key.pem ${cert_dir}cert.pem" >> /opt/postfix/conf/sni.map; |
| 36 | echo "" >> /opt/postfix/conf/sni.map; |
| 37 | done |
| 38 | done |
| 39 | postmap -F hash:/opt/postfix/conf/sni.map; |
| 40 | |
| 41 | cat <<EOF > /opt/postfix/conf/sql/mysql_relay_ne.cf |
| 42 | # Autogenerated by mailcow |
| 43 | user = ${DBUSER} |
| 44 | password = ${DBPASS} |
| 45 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 46 | dbname = ${DBNAME} |
| 47 | query = SELECT IF(EXISTS(SELECT address, domain FROM alias |
| 48 | WHERE address = '%s' |
| 49 | AND domain IN ( |
| 50 | SELECT domain FROM domain |
| 51 | WHERE backupmx = '1' |
| 52 | AND relay_all_recipients = '1' |
| 53 | AND relay_unknown_only = '1') |
| 54 | |
| 55 | ), 'lmtp:inet:dovecot:24', NULL) AS 'transport' |
| 56 | EOF |
| 57 | |
| 58 | cat <<EOF > /opt/postfix/conf/sql/mysql_relay_recipient_maps.cf |
| 59 | # Autogenerated by mailcow |
| 60 | user = ${DBUSER} |
| 61 | password = ${DBPASS} |
| 62 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 63 | dbname = ${DBNAME} |
| 64 | query = SELECT DISTINCT |
| 65 | CASE WHEN '%d' IN ( |
| 66 | SELECT domain FROM domain |
| 67 | WHERE relay_all_recipients=1 |
| 68 | AND domain='%d' |
| 69 | AND backupmx=1 |
| 70 | ) |
| 71 | THEN '%s' ELSE ( |
| 72 | SELECT goto FROM alias WHERE address='%s' AND active='1' |
| 73 | ) |
| 74 | END AS result; |
| 75 | EOF |
| 76 | |
| 77 | cat <<EOF > /opt/postfix/conf/sql/mysql_tls_policy_override_maps.cf |
| 78 | # Autogenerated by mailcow |
| 79 | user = ${DBUSER} |
| 80 | password = ${DBPASS} |
| 81 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 82 | dbname = ${DBNAME} |
| 83 | query = SELECT CONCAT(policy, ' ', parameters) AS tls_policy FROM tls_policy_override WHERE active = '1' AND dest = '%s' |
| 84 | EOF |
| 85 | |
| 86 | cat <<EOF > /opt/postfix/conf/sql/mysql_tls_enforce_in_policy.cf |
| 87 | # Autogenerated by mailcow |
| 88 | user = ${DBUSER} |
| 89 | password = ${DBPASS} |
| 90 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 91 | dbname = ${DBNAME} |
| 92 | query = SELECT IF(EXISTS( |
| 93 | SELECT 'TLS_ACTIVE' FROM alias |
| 94 | LEFT OUTER JOIN mailbox ON mailbox.username = alias.goto |
| 95 | WHERE (address='%s' |
| 96 | OR address IN ( |
| 97 | SELECT CONCAT('%u', '@', target_domain) FROM alias_domain |
| 98 | WHERE alias_domain='%d' |
| 99 | ) |
| 100 | ) AND JSON_UNQUOTE(JSON_VALUE(attributes, '$.tls_enforce_in')) = '1' AND mailbox.active = '1' |
| 101 | ), 'reject_plaintext_session', NULL) AS 'tls_enforce_in'; |
| 102 | EOF |
| 103 | |
| 104 | cat <<EOF > /opt/postfix/conf/sql/mysql_sender_dependent_default_transport_maps.cf |
| 105 | # Autogenerated by mailcow |
| 106 | user = ${DBUSER} |
| 107 | password = ${DBPASS} |
| 108 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 109 | dbname = ${DBNAME} |
| 110 | query = SELECT GROUP_CONCAT(transport SEPARATOR '') AS transport_maps |
| 111 | FROM ( |
| 112 | SELECT IF(EXISTS(SELECT 'smtp_type' FROM alias |
| 113 | LEFT OUTER JOIN mailbox ON mailbox.username = alias.goto |
| 114 | WHERE (address = '%s' |
| 115 | OR address IN ( |
| 116 | SELECT CONCAT('%u', '@', target_domain) FROM alias_domain |
| 117 | WHERE alias_domain = '%d' |
| 118 | ) |
| 119 | ) |
| 120 | AND JSON_UNQUOTE(JSON_VALUE(attributes, '$.tls_enforce_out')) = '1' |
| 121 | AND mailbox.active = '1' |
| 122 | ), 'smtp_enforced_tls:', 'smtp:') AS 'transport' |
| 123 | UNION ALL |
| 124 | SELECT hostname AS transport FROM relayhosts |
| 125 | LEFT OUTER JOIN domain ON domain.relayhost = relayhosts.id |
| 126 | WHERE relayhosts.active = '1' |
| 127 | AND domain = '%d' |
| 128 | OR domain IN ( |
| 129 | SELECT target_domain FROM alias_domain |
| 130 | WHERE alias_domain = '%d' |
| 131 | ) |
| 132 | ) |
| 133 | AS transport_view; |
| 134 | EOF |
| 135 | |
| 136 | cat <<EOF > /opt/postfix/conf/sql/mysql_transport_maps.cf |
| 137 | # Autogenerated by mailcow |
| 138 | user = ${DBUSER} |
| 139 | password = ${DBPASS} |
| 140 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 141 | dbname = ${DBNAME} |
| 142 | query = SELECT CONCAT('smtp_via_transport_maps:', nexthop) AS transport FROM transports |
| 143 | WHERE active = '1' |
| 144 | AND destination = '%s'; |
| 145 | EOF |
| 146 | |
| 147 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_resource_maps.cf |
| 148 | # Autogenerated by mailcow |
| 149 | user = ${DBUSER} |
| 150 | password = ${DBPASS} |
| 151 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 152 | dbname = ${DBNAME} |
| 153 | query = SELECT 'null@localhost' FROM mailbox |
| 154 | WHERE kind REGEXP 'location|thing|group' AND username = '%s'; |
| 155 | EOF |
| 156 | |
| 157 | cat <<EOF > /opt/postfix/conf/sql/mysql_sasl_passwd_maps_sender_dependent.cf |
| 158 | # Autogenerated by mailcow |
| 159 | user = ${DBUSER} |
| 160 | password = ${DBPASS} |
| 161 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 162 | dbname = ${DBNAME} |
| 163 | query = SELECT CONCAT_WS(':', username, password) AS auth_data FROM relayhosts |
| 164 | WHERE id IN ( |
| 165 | SELECT relayhost FROM domain |
| 166 | WHERE CONCAT('@', domain) = '%s' |
| 167 | OR domain IN ( |
| 168 | SELECT target_domain FROM alias_domain WHERE CONCAT('@', alias_domain) = '%s' |
| 169 | ) |
| 170 | ) |
| 171 | AND active = '1' |
| 172 | AND username != ''; |
| 173 | EOF |
| 174 | |
| 175 | cat <<EOF > /opt/postfix/conf/sql/mysql_sasl_passwd_maps_transport_maps.cf |
| 176 | # Autogenerated by mailcow |
| 177 | user = ${DBUSER} |
| 178 | password = ${DBPASS} |
| 179 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 180 | dbname = ${DBNAME} |
| 181 | query = SELECT CONCAT_WS(':', username, password) AS auth_data FROM transports |
| 182 | WHERE nexthop = '%s' |
| 183 | AND active = '1' |
| 184 | AND username != '' |
| 185 | LIMIT 1; |
| 186 | EOF |
| 187 | |
| 188 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_alias_domain_maps.cf |
| 189 | # Autogenerated by mailcow |
| 190 | user = ${DBUSER} |
| 191 | password = ${DBPASS} |
| 192 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 193 | dbname = ${DBNAME} |
| 194 | query = SELECT username FROM mailbox, alias_domain |
| 195 | WHERE alias_domain.alias_domain = '%d' |
| 196 | AND mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) |
| 197 | AND (mailbox.active = '1' OR mailbox.active = '2') |
| 198 | AND alias_domain.active='1' |
| 199 | EOF |
| 200 | |
| 201 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_alias_maps.cf |
| 202 | # Autogenerated by mailcow |
| 203 | user = ${DBUSER} |
| 204 | password = ${DBPASS} |
| 205 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 206 | dbname = ${DBNAME} |
| 207 | query = SELECT goto FROM alias |
| 208 | WHERE address='%s' |
| 209 | AND (active='1' OR active='2'); |
| 210 | EOF |
| 211 | |
| 212 | cat <<EOF > /opt/postfix/conf/sql/mysql_recipient_bcc_maps.cf |
| 213 | # Autogenerated by mailcow |
| 214 | user = ${DBUSER} |
| 215 | password = ${DBPASS} |
| 216 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 217 | dbname = ${DBNAME} |
| 218 | query = SELECT bcc_dest FROM bcc_maps |
| 219 | WHERE local_dest='%s' |
| 220 | AND type='rcpt' |
| 221 | AND active='1'; |
| 222 | EOF |
| 223 | |
| 224 | cat <<EOF > /opt/postfix/conf/sql/mysql_sender_bcc_maps.cf |
| 225 | # Autogenerated by mailcow |
| 226 | user = ${DBUSER} |
| 227 | password = ${DBPASS} |
| 228 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 229 | dbname = ${DBNAME} |
| 230 | query = SELECT bcc_dest FROM bcc_maps |
| 231 | WHERE local_dest='%s' |
| 232 | AND type='sender' |
| 233 | AND active='1'; |
| 234 | EOF |
| 235 | |
| 236 | cat <<EOF > /opt/postfix/conf/sql/mysql_recipient_canonical_maps.cf |
| 237 | # Autogenerated by mailcow |
| 238 | user = ${DBUSER} |
| 239 | password = ${DBPASS} |
| 240 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 241 | dbname = ${DBNAME} |
| 242 | query = SELECT new_dest FROM recipient_maps |
| 243 | WHERE old_dest='%s' |
| 244 | AND active='1'; |
| 245 | EOF |
| 246 | |
| 247 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_domains_maps.cf |
| 248 | # Autogenerated by mailcow |
| 249 | user = ${DBUSER} |
| 250 | password = ${DBPASS} |
| 251 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 252 | dbname = ${DBNAME} |
| 253 | query = SELECT alias_domain from alias_domain WHERE alias_domain='%s' AND active='1' |
| 254 | UNION |
| 255 | SELECT domain FROM domain |
| 256 | WHERE domain='%s' |
| 257 | AND active = '1' |
| 258 | AND backupmx = '0' |
| 259 | EOF |
| 260 | |
| 261 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_mailbox_maps.cf |
| 262 | # Autogenerated by mailcow |
| 263 | user = ${DBUSER} |
| 264 | password = ${DBPASS} |
| 265 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 266 | dbname = ${DBNAME} |
| 267 | 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') |
| 268 | EOF |
| 269 | |
| 270 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_relay_domain_maps.cf |
| 271 | # Autogenerated by mailcow |
| 272 | user = ${DBUSER} |
| 273 | password = ${DBPASS} |
| 274 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 275 | dbname = ${DBNAME} |
| 276 | query = SELECT domain FROM domain WHERE domain='%s' AND backupmx = '1' AND active = '1' |
| 277 | EOF |
| 278 | |
| 279 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_sender_acl.cf |
| 280 | # Autogenerated by mailcow |
| 281 | user = ${DBUSER} |
| 282 | password = ${DBPASS} |
| 283 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 284 | dbname = ${DBNAME} |
| 285 | # First select queries domain and alias_domain to determine if domains are active. |
| 286 | query = SELECT goto FROM alias |
| 287 | WHERE address='%s' |
| 288 | AND active='1' |
| 289 | AND (domain IN |
| 290 | (SELECT domain FROM domain |
| 291 | WHERE domain='%d' |
| 292 | AND active='1') |
| 293 | OR domain in ( |
| 294 | SELECT alias_domain FROM alias_domain |
| 295 | WHERE alias_domain='%d' |
| 296 | AND active='1' |
| 297 | ) |
| 298 | ) |
| 299 | UNION |
| 300 | SELECT logged_in_as FROM sender_acl |
| 301 | WHERE send_as='@%d' |
| 302 | OR send_as='%s' |
| 303 | OR send_as='*' |
| 304 | OR send_as IN ( |
| 305 | SELECT CONCAT('@',target_domain) FROM alias_domain |
| 306 | WHERE alias_domain = '%d') |
| 307 | OR send_as IN ( |
| 308 | SELECT CONCAT('%u','@',target_domain) FROM alias_domain |
| 309 | WHERE alias_domain = '%d') |
| 310 | AND logged_in_as NOT IN ( |
| 311 | SELECT goto FROM alias |
| 312 | WHERE address='%s') |
| 313 | UNION |
| 314 | SELECT username FROM mailbox, alias_domain |
| 315 | WHERE alias_domain.alias_domain = '%d' |
| 316 | AND mailbox.username = CONCAT('%u','@',alias_domain.target_domain) |
| 317 | AND (mailbox.active = '1' OR mailbox.active ='2') |
| 318 | AND alias_domain.active='1' |
| 319 | EOF |
| 320 | |
| 321 | # Reject sasl usernames with smtp disabled |
| 322 | cat <<EOF > /opt/postfix/conf/sql/mysql_sasl_access_maps.cf |
| 323 | # Autogenerated by mailcow |
| 324 | user = ${DBUSER} |
| 325 | password = ${DBPASS} |
| 326 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 327 | dbname = ${DBNAME} |
| 328 | query = SELECT 'REJECT' FROM mailbox WHERE username = '%u' AND JSON_UNQUOTE(JSON_VALUE(attributes, '$.smtp_access')) = '0'; |
| 329 | EOF |
| 330 | |
| 331 | cat <<EOF > /opt/postfix/conf/sql/mysql_virtual_spamalias_maps.cf |
| 332 | # Autogenerated by mailcow |
| 333 | user = ${DBUSER} |
| 334 | password = ${DBPASS} |
| 335 | hosts = unix:/var/run/mysqld/mysqld.sock |
| 336 | dbname = ${DBNAME} |
| 337 | query = SELECT goto FROM spamalias |
| 338 | WHERE address='%s' |
| 339 | AND validity >= UNIX_TIMESTAMP() |
| 340 | EOF |
| 341 | |
| 342 | sed -i '/User overrides/q' /opt/postfix/conf/main.cf |
| 343 | echo >> /opt/postfix/conf/main.cf |
| 344 | touch /opt/postfix/conf/extra.cf |
| 345 | sed -i '/myhostname/d' /opt/postfix/conf/extra.cf |
| 346 | echo -e "myhostname = ${MAILCOW_HOSTNAME}\n$(cat /opt/postfix/conf/extra.cf)" > /opt/postfix/conf/extra.cf |
| 347 | |
| 348 | cat /opt/postfix/conf/extra.cf >> /opt/postfix/conf/main.cf |
| 349 | |
| 350 | if [ ! -f /opt/postfix/conf/custom_transport.pcre ]; then |
| 351 | echo "Creating dummy custom_transport.pcre" |
| 352 | touch /opt/postfix/conf/custom_transport.pcre |
| 353 | fi |
| 354 | |
| 355 | if [[ ! -f /opt/postfix/conf/custom_postscreen_whitelist.cidr ]]; then |
| 356 | echo "Creating dummy custom_postscreen_whitelist.cidr" |
| 357 | echo '# Autogenerated by mailcow' > /opt/postfix/conf/custom_postscreen_whitelist.cidr |
| 358 | fi |
| 359 | |
| 360 | # Fix SMTP last login on slaves |
| 361 | sed -i "s/__REDIS_SLAVEOF_IP__/${REDIS_SLAVEOF_IP}/g" /usr/local/bin/smtpd_last_login.sh |
| 362 | |
| 363 | # Fix Postfix permissions |
| 364 | chown -R root:postfix /opt/postfix/conf/sql/ /opt/postfix/conf/custom_transport.pcre |
| 365 | chmod 640 /opt/postfix/conf/sql/*.cf /opt/postfix/conf/custom_transport.pcre |
| 366 | chgrp -R postdrop /var/spool/postfix/public |
| 367 | chgrp -R postdrop /var/spool/postfix/maildrop |
| 368 | postfix set-permissions |
| 369 | |
| 370 | # Check Postfix configuration |
| 371 | postconf -c /opt/postfix/conf > /dev/null |
| 372 | |
| 373 | if [[ $? != 0 ]]; then |
| 374 | echo "Postfix configuration error, refusing to start." |
| 375 | exit 1 |
| 376 | else |
| 377 | postfix -c /opt/postfix/conf start |
| 378 | sleep 126144000 |
| 379 | fi |