ec3ab097fc7c0fc53ea803ef21d9846945f0a879
[postfix-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$1" = "configure" ] || exit 0
6 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
7
8 # Load CARNET Tools
9 . /usr/share/carnet-tools/functions.sh
10
11 # Load Debconf
12 . /usr/share/debconf/confmodule
13
14 db_get postfix-cn/rbl || true
15 rbl="$RET"
16
17 # Tue, 19 Dec 2006 10:31:01 +0100
18 # relays.ordb.org se gasi 2006-12-31
19 if echo $rbl | grep -q relays.ordb.org; then
20   rbl="`echo $rbl | sed 's/relays.ordb.org, //g'`"
21   # za svaki slucaj, ako je na kraju
22   rbl="`echo $rbl | sed 's/relays.ordb.org//g'`"
23   db_set postfix-cn/rbl "$rbl" || true
24 fi
25
26 # Thu, 21 Dec 2006 09:03:50 +0100
27 # opm.blitzed.org je ugasen 2006-05
28 if echo $rbl | grep -q opm.blitzed.org; then
29   rbl="`echo $rbl | sed 's/opm.blitzed.org, //g'`"
30   # za svaki slucaj, ako je na kraju
31   rbl="`echo $rbl | sed 's/opm.blitzed.org//g'`"
32   db_set postfix-cn/rbl "$rbl" || true
33 fi
34
35 # Wed, 12 Jun 2013 15:15:28 +0200
36 # dnsbl.njabl.org je ugasen 2013-03
37 if echo $rbl | grep -q dnsbl.njabl.org; then
38   rbl="`echo $rbl | sed 's/dnsbl.njabl.org, //g'`"
39   # za svaki slucaj, ako je na kraju
40   rbl="`echo $rbl | sed 's/dnsbl.njabl.org//g'`"
41   db_set postfix-cn/rbl "$rbl" || true
42 fi
43
44 # zelja, 2021-04-29
45 # ako je spamhaus.org ovdje, postavi eta.cert.hr, a sam
46 # spamhaus je nepotreban, jer imamo zen.dnsbl-sh.carnet.hr
47 if echo $rbl | grep -q zen.spamhaus.org; then
48   rbl="`echo $rbl | sed 's/zen.spamhaus.org, /eta.cert.hr, /g'`"
49   # za svaki slucaj, ako je na kraju
50   rbl="`echo $rbl | sed 's/zen.spamhaus.org/eta.cert.hr/g'`"
51   db_set postfix-cn/rbl "$rbl" || true
52 fi
53
54 db_get postfix-cn/matchgecos || true
55 matchgecos="$RET"
56
57 hostname="`hostname`"
58 fqdn="`hostname --fqdn`"
59 domain="`dnsdomainname`"
60 mailname="`cat /etc/mailname 2> /dev/null || hostname --fqdn`"
61 cp_get_netaddr || true
62 netaddr="$RET"
63
64 # ako ne postoje konfiguracijske datoteke, napravi ih
65 [ -f /etc/postfix/master.cf ] || touch /etc/postfix/master.cf
66 [ -f /etc/postfix/main.cf ] || touch /etc/postfix/main.cf
67
68 # spremi pocetnu verziju
69 cp -pf /etc/postfix/master.cf /etc/postfix/master.cf.dpkg-tmp.$$
70 cp -pf /etc/postfix/main.cf /etc/postfix/main.cf.dpkg-tmp.$$
71
72 # srediti master.cf za TLS & submission & retry
73 if [ -f /etc/postfix/master.cf ]; then
74   cp_check_and_sed '^#tlsmgr' \
75                    's/^#tlsmgr/tlsmgr/g' \
76                    /etc/postfix/master.cf || true
77
78   cp_check_and_sed '^#submission' \
79                    's/^#submission/submission/g' \
80                    /etc/postfix/master.cf || true
81
82   cp_check_and_sed '^#retry' \
83                    's/^#retry/retry/g' \
84                    /etc/postfix/master.cf || true
85
86   cp_check_and_sed '^#smtps' \
87                    's/^#smtps/smtps/g' \
88                    /etc/postfix/master.cf || true
89
90   cp_check_and_sed '^#  -o' \
91                    's/^#  -o/  -o/g' \
92                    /etc/postfix/master.cf || true
93
94   for option in  "-o milter_macro_daemon_name=ORIGINATING" \
95                  "-o smtpd_recipient_restrictions=" \
96                  "-o smtpd_reject_unlisted_recipient=no" \
97                  "-o smtpd_relay_restrictions=permit_sasl_authenticated,reject" \
98                  "-o smtpd_sasl_auth_enable=yes" \
99                  "-o smtpd_tls_security_level=encrypt" \
100                  "-o smtpd_tls_wrappermode=yes" \
101                  "-o syslog_name=postfix/smtps" \
102                  "-o syslog_name=postfix/submission"; do
103   cp_check_and_sed "^#   $option" \
104                    "s/#   $option/$option/g" \
105                    /etc/postfix/master.cf || true
106   done
107
108   for option in  "o smtpd_client_restrictions=" \
109                  "o smtpd_helo_restrictions=" \
110                  "o smtpd_sender_restrictions="; do
111   cp_check_and_sed "$option" \
112                    "s/.*$option/# -$option/g" \
113                    /etc/postfix/master.cf || true
114   done
115 fi
116
117 # ovo manje/vise uzima kao default, ali u slucaju da je multihomed stroj,
118 # i eventualno je sprckan, mozda nesto nece raditi kako spada pa cemo konfigurirati
119 postconf -e myhostname="$fqdn"
120 postconf -e mydomain="$domain"
121 postconf -e myorigin="$mailname"
122
123 # zbog problema sa squirrelmailom
124 postconf -e append_dot_mydomain="yes"
125 postconf -e append_at_myorigin="yes"
126
127 # dodaj 127.0.0.0/8, netaddr i ipv6-localhost
128 # dodaje izlazne servere za webmail --zelja
129 mynetworks="`postconf -h mynetworks`"
130 for i in "127.0.0.0/8" "\[::1\]/128" "$netaddr" "193.198.233.95" "193.198.233.96"; do
131   if ! echo $mynetworks | grep -q $i; then
132    mynetworks="$mynetworks, $i"
133   fi
134 done
135 postconf -e mynetworks="$mynetworks"
136
137 # obrisati $mydomain, ako je upgrade sa starije verzije
138 # te srediti mydestination
139 if dpkg --compare-versions "$2" lt 2.3.8-2; then
140   mydestination="`postconf -h mydestination`"
141   if echo $mydestination | grep -q ", \$mydomain"; then
142     mydestination="`echo $mydestination | sed 's/, \$mydomain//g'`"
143   fi
144   if ! echo $mydestination | grep -q "localhost.\$mydomain"; then
145     mydestination="$mydestination, localhost.\$mydomain"
146   fi
147   if ! echo $mydestination | grep -q "\$myhostname"; then
148     mydestination="\$myhostname, $mydestination"
149   fi
150   postconf -e mydestination="$mydestination"
151 fi
152
153 # default je: $myhostname, localhost.$mydomain, localhost
154 # treba dodati jos ", $mydomain", ako je host mx
155 cp_get_mx_domain || true
156 mx_domain="$RET"
157 if [ "$mx_domain" != "$fqdn" ]; then
158   mydestination="`postconf -h mydestination`"
159   if ! echo $mydestination | grep -q ", \$mydomain"; then
160     postconf -e mydestination="$mydestination, \$mydomain"
161   fi
162   postconf -e relay_domains="\$mydestination"
163   if [ "$mx_domain" != "$domain" ]; then
164     if dpkg --compare-versions "$2" lt 2.1.5-8; then
165       cp_echo "CN: This host is MX for more than one domain! postfix-cn package will"
166       cp_echo "CN: only set up $mx_domain, you HAVE TO add the other domains manually"
167       cp_echo "CN: to mydestination variable in /etc/postfix/main.cf, if you haven't"
168       cp_echo "CN: done that already or you may lose mail."
169     fi
170   fi
171 fi
172
173 # sredi $alias_maps
174 if [ "$matchgecos" = "true" ]; then
175   aliasmaps="hash:/etc/aliases hash:/var/lib/postfix-cn/aliases_gecos"
176 elif [ "$matchgecos" = "false" ]; then
177   aliasmaps="hash:/etc/aliases"
178   alias_maps="`postconf -h alias_maps | \
179                sed -e 's#hash:/var/lib/postfix-cn/aliases_gecos,*##g' -e 's/,*[[:space:]]*$//g'`"
180   postconf -e alias_maps="$alias_maps"
181 fi
182
183 for i in $aliasmaps; do
184   alias_maps="`postconf -h alias_maps`"
185   if ! echo $alias_maps | grep -q "$i"; then
186     postconf -e alias_maps="$alias_maps, $i"
187   fi
188 done
189
190 postconf -e alias_database="hash:/etc/aliases"
191 postconf -e local_recipient_maps="proxy:unix:passwd.byname \$alias_maps"
192
193 # jos malo
194 postconf -e recipient_delimiter="+"
195 postconf -e smtpd_helo_required="yes"
196 postconf -e smtpd_helo_restrictions="permit_mynetworks"
197 postconf -e in_flow_delay="2s"
198 postconf -e initial_destination_concurrency="10"
199 postconf -e default_destination_concurrency_limit="30"
200 postconf -e smtpd_recipient_limit="400"
201 postconf -e hash_queue_depth="3"
202
203 if dpkg --compare-versions "$2" lt 2.1.5-6; then
204   postconf -e bounce_queue_lifetime="5d"
205   postconf -e maximal_queue_lifetime="5d"
206 fi
207
208 # ako netko zeli maknuti maskiranje, da mu ga nova verzija ne doda
209 if dpkg --compare-versions "$2" lt 2.1.5-2; then
210   postconf -e masquerade_domains="\$mydomain"
211 fi
212
213 # sredi smtpd_recipient_restrictions
214 smtpd_recipient_restrictions="reject_invalid_hostname, \
215                               reject_unknown_sender_domain, \
216                               reject_unknown_recipient_domain, \
217                               reject_unauth_pipelining, \
218                               permit_sasl_authenticated, \
219                               permit_mynetworks, \
220                               reject_unauth_destination, \
221                               check_client_access hash:/etc/postfix/cn-client-access, \
222                               check_helo_access hash:/etc/postfix/cn-helo-access, \
223                               check_sender_access hash:/etc/postfix/cn-sender-access, \
224                               check_recipient_access hash:/etc/postfix/cn-recipient-access"
225
226 # dodaj samo rbl-ove iz debconfa
227 if [ "$rbl" ]; then
228   for i in `echo $rbl | tr -d ,`; do
229     smtpd_recipient_restrictions="$smtpd_recipient_restrictions, reject_rbl_client $i"
230   done
231 fi
232
233 # dodaj permit na kraj
234 smtpd_recipient_restrictions="$smtpd_recipient_restrictions, permit"
235 postconf -e smtpd_recipient_restrictions="$smtpd_recipient_restrictions"
236
237 # Header & body checks
238
239 postconf -e header_checks="pcre:/etc/postfix/cn-header-checks"
240 postconf -e mime_header_checks="pcre:/etc/postfix/cn-mime-header-checks"
241 postconf -e nested_header_checks="pcre:/etc/postfix/cn-nested-header-checks"
242 postconf -e body_checks="pcre:/etc/postfix/cn-body-checks"
243
244
245 # SASL stuff
246 postconf -e smtpd_sasl_auth_enable="yes"
247 postconf -e smtpd_sasl_security_options="noanonymous"
248 postconf -e smtpd_sasl_local_domain="\$myhostname"
249 postconf -e broken_sasl_auth_clients="yes"
250
251 [ -d /etc/postfix/sasl/ ] || mkdir -p /etc/postfix/sasl/
252 if [ ! -f /etc/postfix/sasl/smtpd.conf ]; then
253   cat - > /etc/postfix/sasl/smtpd.conf <<EOF
254 pwcheck_method: saslauthd
255 mech_list: plain login
256 EOF
257   restart_postfix="yes"
258 fi
259
260 if [ -f /etc/default/saslauthd ]; then
261   cp_check_and_sed "^START=no" \
262                    "s/^START=no/START=yes/g" \
263                    /etc/default/saslauthd && restart_saslauthd="yes"
264
265   cp_check_and_sed "^#[[:blank:]]*START=" \
266                    "s/^#[[:blank:]]*START=.*$/START=yes/g" \
267                    /etc/default/saslauthd && restart_saslauthd="yes"
268
269   if ! grep -q "^START.*yes" /etc/default/saslauthd; then
270     echo "START=yes" >> /etc/default/saslauthd
271     restart_saslauthd="yes"
272   fi
273
274   if ! grep -q "^OPTIONS.*/var/spool/postfix/var/run/saslauthd" /etc/default/saslauthd; then
275     echo "OPTIONS=\"-c -m /var/spool/postfix/var/run/saslauthd\"" >> /etc/default/saslauthd
276     restart_saslauthd="yes"
277   fi
278
279   if ! grep -q "&& ln -s" /etc/default/saslauthd; then
280     echo "[ ! -L /var/run/saslauthd -a -d /var/spool/postfix/var/run/saslauthd -a ! -d /var/run/saslauthd ] && ln -s /var/spool/postfix/var/run/saslauthd /var/run/saslauthd || true" >> /etc/default/saslauthd
281     restart_saslauthd="yes"
282   fi
283
284   if [ ! -L /var/run/saslauthd ]; then
285     mkdir -p /var/spool/postfix/var/run/
286     if [ -d /var/run/saslauthd -a ! -d /var/spool/postfix/var/run/saslauthd ]; then
287       mv /var/run/saslauthd /var/spool/postfix/var/run/
288     elif [ -d /var/run/saslauthd -a -d /var/spool/postfix/var/run/saslauthd ]; then
289       rm -rf /var/run/saslauthd/
290     fi
291     # XXX jos kad bi ln -s u /var/run/ radio (bootclean.sh)
292     if [ -d /var/spool/postfix/var/run/saslauthd -a ! -d /var/run/saslauthd ]; then
293       ln -s /var/spool/postfix/var/run/saslauthd /var/run/saslauthd
294     fi
295     restart_saslauthd="yes"
296   fi
297
298   if [ ! -d /var/spool/postfix/var/run/saslauthd ]; then
299     mkdir -p /var/spool/postfix/var/run/saslauthd
300     restart_saslauthd="yes"
301   fi
302
303   if ! dpkg-statoverride --list /var/spool/postfix/var/run/saslauthd > /dev/null; then
304     dpkg-statoverride --update --add root sasl 710 /var/spool/postfix/var/run/saslauthd
305   fi
306 fi
307
308 # restart saslauthd
309 if [ "$restart_saslauthd" = "yes" ]; then
310   service saslauthd restart
311 fi
312
313 # TLS stuff
314 postconf -e smtp_tls_security_level="may"
315 postconf -e smtp_tls_session_cache_database="btree:/var/lib/postfix/smtp_scache"
316 postconf -e smtp_tls_session_cache_timeout="3600s"
317 postconf -e smtpd_tls_security_level="may"
318 postconf -e smtpd_tls_session_cache_database="btree:/var/lib/postfix/smtpd_scache"
319 postconf -e smtpd_tls_session_cache_timeout="3600s"
320 postconf -e tls_random_source="dev:/dev/urandom"
321 postconf -e smtpd_tls_exclude_ciphers="aNULL"
322 postconf -e smtpd_tls_mandatory_exclude_ciphers="aNULL"
323
324 # nije potrebno generiranje certifikata jer postfix koristi paket ssl-cert
325 # ako je postavljen snakeoil certifikat za smtpd_tls_* postavi isti za smtp_tls_*
326 smtpd_tls_cert_file="`postconf -h smtpd_tls_cert_file`"
327 if [ "$smtpd_tls_cert_file" = "/etc/ssl/certs/ssl-cert-snakeoil.pem" ]; then
328   postconf -e smtp_tls_cert_file="/etc/ssl/certs/ssl-cert-snakeoil.pem"
329   postconf -e smtp_tls_key_file="/etc/ssl/private/ssl-cert-snakeoil.key"
330 fi
331
332 # ako je prazan, postavi na defaultni iz paketa ssl-cert
333 smtpd_tls_cert_file="`postconf -h smtpd_tls_cert_file`"
334 if [ -z "$smtpd_tls_cert_file" ]; then
335   postconf -e smtpd_tls_cert_file="/etc/ssl/certs/ssl-cert-snakeoil.pem"
336   postconf -e smtpd_tls_key_file="/etc/ssl/private/ssl-cert-snakeoil.key"
337 fi
338 smtp_tls_cert_file="`postconf -h smtp_tls_cert_file`"
339 if [ -z "$smtp_tls_cert_file" ]; then
340   postconf -e smtp_tls_cert_file="/etc/ssl/certs/ssl-cert-snakeoil.pem"
341   postconf -e smtp_tls_key_file="/etc/ssl/private/ssl-cert-snakeoil.key"
342 fi
343
344 # prije bilo u /etc/postfix/, od verzije 2.1.5-2 je u /var/spool/postfix
345 [ -f /etc/postfix/smtp_scache.dir ] && rm -f /etc/postfix/smtp_scache.dir
346 [ -f /etc/postfix/smtp_scache.pag ] && rm -f /etc/postfix/smtp_scache.pag
347 [ -f /etc/postfix/smtpd_scache.dir ] && rm -f /etc/postfix/smtpd_scache.dir
348 [ -f /etc/postfix/smtpd_scache.pag ] && rm -f /etc/postfix/smtpd_scache.pag
349 # prije bilo u /var/spool/postfix/, od verzije 2.5.5 je u /var/lib/postfix
350 [ -f /var/spool/postfix/smtp_scache.dir ] && rm -f /var/spool/postfix/smtp_scache.dir
351 [ -f /var/spool/postfix/smtp_scache.pag ] && rm -f /var/spool/postfix/smtp_scache.pag
352 [ -f /var/spool/postfix/smtpd_scache.dir ] && rm -f /var/spool/postfix/smtpd_scache.dir
353 [ -f /var/spool/postfix/smtpd_scache.pag ] && rm -f /var/spool/postfix/smtpd_scache.pag
354
355 # debconf stop
356 db_go || true
357 db_stop || true
358
359 # ako postoji amavis, sredi
360 if [ -x /usr/share/amavisd-cn/postfixize.sh ]; then
361   /usr/share/amavisd-cn/postfixize.sh
362 fi
363
364 # provjeri da li postoji sendmail-base
365 if ! dpkg -l sendmail-base | grep -q '^.n'; then
366   cp_echo ""
367   cp_echo "CN: Package sendmail-base detected."
368   cp_echo "CN: Please, purge this package with command:"
369   cp_echo "CN: # dpkg --purge sendmail-base"
370   cp_echo ""
371 fi
372
373 # sendmailov /etc/mail/access
374 if [ -f /etc/mail/access -a /etc/mail/access.db ]; then
375   cp_echo ""
376   cp_echo "CN: Sendmail's access file (/etc/mail/access) detected."
377   cp_echo "CN: Please, upgrade your's /etc/postfix/access."
378   cp_echo "CN: http://www.postfix.org/SMTPD_ACCESS_README.html"
379   cp_echo ""
380 fi
381
382 if ! id postfix | grep -q sasl; then
383   adduser postfix sasl > /dev/null
384   restart_postfix="yes"
385 fi
386
387 # ako ima razlika, backupiraj stari, ako ne, obrisi privremeni backup
388 if ! cmp -s /etc/postfix/master.cf /etc/postfix/master.cf.dpkg-tmp.$$; then
389   cp_echo "CN: Backing up old version of /etc/postfix/master.cf in /var/backups/..."
390   cp_backup_conffile /etc/postfix/master.cf.dpkg-tmp.$$ master.cf
391   restart_postfix="yes"
392 fi
393 rm -f /etc/postfix/master.cf.dpkg-tmp.$$
394
395 if ! cmp -s /etc/postfix/main.cf /etc/postfix/main.cf.dpkg-tmp.$$; then
396   cp_echo "CN: Backing up old version of /etc/postfix/main.cf in /var/backups/..."
397   cp_backup_conffile /etc/postfix/main.cf.dpkg-tmp.$$ main.cf
398   restart_postfix="yes"
399 fi
400 rm -f /etc/postfix/main.cf.dpkg-tmp.$$
401
402 #if ! grep -q "retry     unix  -       -       y       -       -       error" /etc/postfix/master.cf; then
403 #  echo "CN: Please, add this line in /etc/postfix/master.cf and restart Postfix."
404 #  echo "retry     unix  -       -       y       -       -       error"
405 #  echo
406 #fi
407
408 # kreiraj aliases_gecos
409 if [ -x /usr/sbin/newaliases-gecos ]; then
410   if [ "$matchgecos" = "true" ]; then
411     echo "CN: Creating GECOS alias map..."
412     /usr/sbin/newaliases-gecos
413   fi
414 fi
415
416 # rjesava gresku using backwards-compatible default setting chroot=y
417
418 for service in smtps/inet smtp/inet smtp/unix submission/inet pickup/fifo cleanup/unix qmgr/fifo rewrite/unix bounce/unix defer/unix trace/unix verify/unix flush/unix relay/unix showq/unix error/unix scache/unix discard/unix retry/unix
419 do
420         postconf -F $service/chroot=y
421 done    
422 restart_postfix="yes"
423
424
425 # obavijest za webmail
426
427 cp-update -t postfix-cn /etc/postfix/main.cf <<EOF
428
429 # Posluzitelji "193.198.233.95" "193.198.233.96" su odlazni posluzitelji za webmail.carnet.hr
430 # Ukoliko koristite uslugu CARNET-ovog Webmaila, nemojte ih brisati iz "mynetworks"!
431
432 EOF
433
434 test -f /etc/postfix/access_client_cn && /usr/sbin/postmap /etc/postfix/access_client_cn
435 test -f /etc/postfix/access_recipient_cn && /usr/sbin/postmap /etc/postfix/access_recipient_cn
436 test -f /etc/postfix/access_sender_cn && /usr/sbin/postmap /etc/postfix/access_sender_cn
437 test -f /etc/postfix/access_helo_cn && /usr/sbin/postmap /etc/postfix/access_helo_cn
438
439
440 # restart 
441 if [ "$restart_postfix" = "yes" ]; then
442   service postfix restart
443 fi
444 cp_mail postfix-cn
445
446 #DEBHELPER#