Dodaje servere za webmail u mynetwork, fix za dupliciranje IPv6 localhost adrese
[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 db_get postfix-cn/matchgecos || true
45 matchgecos="$RET"
46
47 hostname="`hostname`"
48 fqdn="`hostname --fqdn`"
49 domain="`dnsdomainname`"
50 mailname="`cat /etc/mailname 2> /dev/null || hostname --fqdn`"
51 cp_get_netaddr || true
52 netaddr="$RET"
53
54 # ako ne postoje konfiguracijske datoteke, napravi ih
55 [ -f /etc/postfix/master.cf ] || touch /etc/postfix/master.cf
56 [ -f /etc/postfix/main.cf ] || touch /etc/postfix/main.cf
57
58 # spremi pocetnu verziju
59 cp -pf /etc/postfix/master.cf /etc/postfix/master.cf.dpkg-tmp.$$
60 cp -pf /etc/postfix/main.cf /etc/postfix/main.cf.dpkg-tmp.$$
61
62 # srediti master.cf za TLS & submission & retry
63 if [ -f /etc/postfix/master.cf ]; then
64   cp_check_and_sed '^#tlsmgr' \
65                    's/^#tlsmgr/tlsmgr/g' \
66                    /etc/postfix/master.cf || true
67
68   cp_check_and_sed '^#submission' \
69                    's/^#submission/submission/g' \
70                    /etc/postfix/master.cf || true
71
72   cp_check_and_sed '^#retry' \
73                    's/^#retry/retry/g' \
74                    /etc/postfix/master.cf || true
75
76   cp_check_and_sed '^#smtps' \
77                    's/^#smtps/smtps/g' \
78                    /etc/postfix/master.cf || true
79
80   cp_check_and_sed '^#  -o' \
81                    's/^#  -o/  -o/g' \
82                    /etc/postfix/master.cf || true
83
84   for option in  "-o milter_macro_daemon_name=ORIGINATING" \
85                  "-o smtpd_recipient_restrictions=" \
86                  "-o smtpd_reject_unlisted_recipient=no" \
87                  "-o smtpd_relay_restrictions=permit_sasl_authenticated,reject" \
88                  "-o smtpd_sasl_auth_enable=yes" \
89                  "-o smtpd_tls_security_level=encrypt" \
90                  "-o smtpd_tls_wrappermode=yes" \
91                  "-o syslog_name=postfix/smtps" \
92                  "-o syslog_name=postfix/submission"; do
93   cp_check_and_sed "^#   $option" \
94                    "s/#   $option/$option/g" \
95                    /etc/postfix/master.cf || true
96   done
97 fi
98
99 # ovo manje/vise uzima kao default, ali u slucaju da je multihomed stroj,
100 # i eventualno je sprckan, mozda nesto nece raditi kako spada pa cemo konfigurirati
101 postconf -e myhostname="$fqdn"
102 postconf -e mydomain="$domain"
103 postconf -e myorigin="$mailname"
104
105 # zbog problema sa squirrelmailom
106 postconf -e append_dot_mydomain="yes"
107 postconf -e append_at_myorigin="yes"
108
109 # dodaj 127.0.0.0/8, netaddr i ipv6-localhost
110 # dodaje izlazne servere za webmail --zelja
111 mynetworks="`postconf -h mynetworks`"
112 for i in "127.0.0.0/8" "\[::1\]/128" "$netaddr" "193.198.233.95" "193.198.233.96"; do
113   if ! echo $mynetworks | grep -q $i; then
114    mynetworks="$mynetworks, $i"
115   fi
116 done
117 postconf -e mynetworks="$mynetworks"
118
119 # obrisati $mydomain, ako je upgrade sa starije verzije
120 # te srediti mydestination
121 if dpkg --compare-versions "$2" lt 2.3.8-2; then
122   mydestination="`postconf -h mydestination`"
123   if echo $mydestination | grep -q ", \$mydomain"; then
124     mydestination="`echo $mydestination | sed 's/, \$mydomain//g'`"
125   fi
126   if ! echo $mydestination | grep -q "localhost.\$mydomain"; then
127     mydestination="$mydestination, localhost.\$mydomain"
128   fi
129   if ! echo $mydestination | grep -q "\$myhostname"; then
130     mydestination="\$myhostname, $mydestination"
131   fi
132   postconf -e mydestination="$mydestination"
133 fi
134
135 # default je: $myhostname, localhost.$mydomain, localhost
136 # treba dodati jos ", $mydomain", ako je host mx
137 cp_get_mx_domain || true
138 mx_domain="$RET"
139 if [ "$mx_domain" != "$fqdn" ]; then
140   mydestination="`postconf -h mydestination`"
141   if ! echo $mydestination | grep -q ", \$mydomain"; then
142     postconf -e mydestination="$mydestination, \$mydomain"
143   fi
144   postconf -e relay_domains="\$mydestination"
145   if [ "$mx_domain" != "$domain" ]; then
146     if dpkg --compare-versions "$2" lt 2.1.5-8; then
147       cp_echo "CN: This host is MX for more than one domain! postfix-cn package will"
148       cp_echo "CN: only set up $mx_domain, you HAVE TO add the other domains manually"
149       cp_echo "CN: to mydestination variable in /etc/postfix/main.cf, if you haven't"
150       cp_echo "CN: done that already or you may lose mail."
151     fi
152   fi
153 fi
154
155 # sredi $alias_maps
156 if [ "$matchgecos" = "true" ]; then
157   aliasmaps="hash:/etc/aliases hash:/var/lib/postfix-cn/aliases_gecos"
158 elif [ "$matchgecos" = "false" ]; then
159   aliasmaps="hash:/etc/aliases"
160   alias_maps="`postconf -h alias_maps | \
161                sed -e 's#hash:/var/lib/postfix-cn/aliases_gecos,*##g' -e 's/,*[[:space:]]*$//g'`"
162   postconf -e alias_maps="$alias_maps"
163 fi
164
165 for i in $aliasmaps; do
166   alias_maps="`postconf -h alias_maps`"
167   if ! echo $alias_maps | grep -q "$i"; then
168     postconf -e alias_maps="$alias_maps, $i"
169   fi
170 done
171
172 postconf -e alias_database="hash:/etc/aliases"
173 postconf -e local_recipient_maps="proxy:unix:passwd.byname \$alias_maps"
174
175 # jos malo
176 postconf -e recipient_delimiter="+"
177 postconf -e smtpd_helo_required="yes"
178 postconf -e smtpd_helo_restrictions="permit_mynetworks"
179 postconf -e in_flow_delay="2s"
180 postconf -e initial_destination_concurrency="10"
181 postconf -e default_destination_concurrency_limit="30"
182 postconf -e smtpd_recipient_limit="400"
183 postconf -e hash_queue_depth="3"
184
185 if dpkg --compare-versions "$2" lt 2.1.5-6; then
186   postconf -e bounce_queue_lifetime="5d"
187   postconf -e maximal_queue_lifetime="5d"
188 fi
189
190 # ako netko zeli maknuti maskiranje, da mu ga nova verzija ne doda
191 if dpkg --compare-versions "$2" lt 2.1.5-2; then
192   postconf -e masquerade_domains="\$mydomain"
193 fi
194
195 # sredi smtpd_recipient_restrictions
196 smtpd_recipient_restrictions="reject_invalid_hostname, \
197                               reject_unknown_sender_domain, \
198                               reject_unknown_recipient_domain, \
199                               reject_unauth_pipelining, \
200                               permit_sasl_authenticated, \
201                               permit_mynetworks, \
202                               reject_unauth_destination"
203
204 # dodaj samo rbl-ove iz debconfa
205 if [ "$rbl" ]; then
206   for i in `echo $rbl | tr -d ,`; do
207     smtpd_recipient_restrictions="$smtpd_recipient_restrictions, reject_rbl_client $i"
208   done
209 fi
210
211 # dodaj permit na kraj
212 smtpd_recipient_restrictions="$smtpd_recipient_restrictions, permit"
213 postconf -e smtpd_recipient_restrictions="$smtpd_recipient_restrictions"
214
215 # SASL stuff
216 postconf -e smtpd_sasl_auth_enable="yes"
217 postconf -e smtpd_sasl_security_options="noanonymous"
218 postconf -e smtpd_sasl_local_domain="\$myhostname"
219 postconf -e broken_sasl_auth_clients="yes"
220
221 [ -d /etc/postfix/sasl/ ] || mkdir -p /etc/postfix/sasl/
222 if [ ! -f /etc/postfix/sasl/smtpd.conf ]; then
223   cat - > /etc/postfix/sasl/smtpd.conf <<EOF
224 pwcheck_method: saslauthd
225 mech_list: plain login
226 EOF
227   restart_postfix="yes"
228 fi
229
230 if [ -f /etc/default/saslauthd ]; then
231   cp_check_and_sed "^START=no" \
232                    "s/^START=no/START=yes/g" \
233                    /etc/default/saslauthd && restart_saslauthd="yes"
234
235   cp_check_and_sed "^#[[:blank:]]*START=" \
236                    "s/^#[[:blank:]]*START=.*$/START=yes/g" \
237                    /etc/default/saslauthd && restart_saslauthd="yes"
238
239   if ! grep -q "^START.*yes" /etc/default/saslauthd; then
240     echo "START=yes" >> /etc/default/saslauthd
241     restart_saslauthd="yes"
242   fi
243
244   if ! grep -q "^OPTIONS.*/var/spool/postfix/var/run/saslauthd" /etc/default/saslauthd; then
245     echo "OPTIONS=\"-c -m /var/spool/postfix/var/run/saslauthd\"" >> /etc/default/saslauthd
246     restart_saslauthd="yes"
247   fi
248
249   if ! grep -q "&& ln -s" /etc/default/saslauthd; then
250     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
251     restart_saslauthd="yes"
252   fi
253
254   if [ ! -L /var/run/saslauthd ]; then
255     mkdir -p /var/spool/postfix/var/run/
256     if [ -d /var/run/saslauthd -a ! -d /var/spool/postfix/var/run/saslauthd ]; then
257       mv /var/run/saslauthd /var/spool/postfix/var/run/
258     elif [ -d /var/run/saslauthd -a -d /var/spool/postfix/var/run/saslauthd ]; then
259       rm -rf /var/run/saslauthd/
260     fi
261     # XXX jos kad bi ln -s u /var/run/ radio (bootclean.sh)
262     if [ -d /var/spool/postfix/var/run/saslauthd -a ! -d /var/run/saslauthd ]; then
263       ln -s /var/spool/postfix/var/run/saslauthd /var/run/saslauthd
264     fi
265     restart_saslauthd="yes"
266   fi
267
268   if [ ! -d /var/spool/postfix/var/run/saslauthd ]; then
269     mkdir -p /var/spool/postfix/var/run/saslauthd
270     restart_saslauthd="yes"
271   fi
272
273   if ! dpkg-statoverride --list /var/spool/postfix/var/run/saslauthd > /dev/null; then
274     dpkg-statoverride --update --add root sasl 710 /var/spool/postfix/var/run/saslauthd
275   fi
276 fi
277
278 # restart saslauthd
279 if [ "$restart_saslauthd" = "yes" ]; then
280   service saslauthd restart
281 fi
282
283 # TLS stuff
284 postconf -e smtp_tls_security_level="may"
285 postconf -e smtp_tls_session_cache_database="btree:/var/lib/postfix/smtp_scache"
286 postconf -e smtp_tls_session_cache_timeout="3600s"
287 postconf -e smtpd_tls_security_level="may"
288 postconf -e smtpd_tls_session_cache_database="btree:/var/lib/postfix/smtpd_scache"
289 postconf -e smtpd_tls_session_cache_timeout="3600s"
290 postconf -e tls_random_source="dev:/dev/urandom"
291 postconf -e smtpd_tls_exclude_ciphers="aNULL"
292 postconf -e smtpd_tls_mandatory_exclude_ciphers="aNULL"
293
294 # nije potrebno generiranje certifikata jer postfix koristi paket ssl-cert
295 # ako je postavljen snakeoil certifikat za smtpd_tls_* postavi isti za smtp_tls_*
296 smtpd_tls_cert_file="`postconf -h smtpd_tls_cert_file`"
297 if [ "$smtpd_tls_cert_file" = "/etc/ssl/certs/ssl-cert-snakeoil.pem" ]; then
298   postconf -e smtp_tls_cert_file="/etc/ssl/certs/ssl-cert-snakeoil.pem"
299   postconf -e smtp_tls_key_file="/etc/ssl/private/ssl-cert-snakeoil.key"
300 fi
301
302 # ako je prazan, postavi na defaultni iz paketa ssl-cert
303 smtpd_tls_cert_file="`postconf -h smtpd_tls_cert_file`"
304 if [ -z "$smtpd_tls_cert_file" ]; then
305   postconf -e smtpd_tls_cert_file="/etc/ssl/certs/ssl-cert-snakeoil.pem"
306   postconf -e smtpd_tls_key_file="/etc/ssl/private/ssl-cert-snakeoil.key"
307 fi
308 smtp_tls_cert_file="`postconf -h smtp_tls_cert_file`"
309 if [ -z "$smtp_tls_cert_file" ]; then
310   postconf -e smtp_tls_cert_file="/etc/ssl/certs/ssl-cert-snakeoil.pem"
311   postconf -e smtp_tls_key_file="/etc/ssl/private/ssl-cert-snakeoil.key"
312 fi
313
314 # prije bilo u /etc/postfix/, od verzije 2.1.5-2 je u /var/spool/postfix
315 [ -f /etc/postfix/smtp_scache.dir ] && rm -f /etc/postfix/smtp_scache.dir
316 [ -f /etc/postfix/smtp_scache.pag ] && rm -f /etc/postfix/smtp_scache.pag
317 [ -f /etc/postfix/smtpd_scache.dir ] && rm -f /etc/postfix/smtpd_scache.dir
318 [ -f /etc/postfix/smtpd_scache.pag ] && rm -f /etc/postfix/smtpd_scache.pag
319 # prije bilo u /var/spool/postfix/, od verzije 2.5.5 je u /var/lib/postfix
320 [ -f /var/spool/postfix/smtp_scache.dir ] && rm -f /var/spool/postfix/smtp_scache.dir
321 [ -f /var/spool/postfix/smtp_scache.pag ] && rm -f /var/spool/postfix/smtp_scache.pag
322 [ -f /var/spool/postfix/smtpd_scache.dir ] && rm -f /var/spool/postfix/smtpd_scache.dir
323 [ -f /var/spool/postfix/smtpd_scache.pag ] && rm -f /var/spool/postfix/smtpd_scache.pag
324
325 # debconf stop
326 db_go || true
327 db_stop || true
328
329 # ako postoji amavis, sredi
330 if [ -x /usr/share/amavisd-cn/postfixize.sh ]; then
331   /usr/share/amavisd-cn/postfixize.sh
332 fi
333
334 # provjeri da li postoji sendmail-base
335 if ! dpkg -l sendmail-base | grep -q '^.n'; then
336   cp_echo ""
337   cp_echo "CN: Package sendmail-base detected."
338   cp_echo "CN: Please, purge this package with command:"
339   cp_echo "CN: # dpkg --purge sendmail-base"
340   cp_echo ""
341 fi
342
343 # sendmailov /etc/mail/access
344 if [ -f /etc/mail/access -a /etc/mail/access.db ]; then
345   cp_echo ""
346   cp_echo "CN: Sendmail's access file (/etc/mail/access) detected."
347   cp_echo "CN: Please, upgrade your's /etc/postfix/access."
348   cp_echo "CN: http://www.postfix.org/SMTPD_ACCESS_README.html"
349   cp_echo ""
350 fi
351
352 if ! id postfix | grep -q sasl; then
353   adduser postfix sasl > /dev/null
354   restart_postfix="yes"
355 fi
356
357 # ako ima razlika, backupiraj stari, ako ne, obrisi privremeni backup
358 if ! cmp -s /etc/postfix/master.cf /etc/postfix/master.cf.dpkg-tmp.$$; then
359   cp_echo "CN: Backing up old version of /etc/postfix/master.cf in /var/backups/..."
360   cp_backup_conffile /etc/postfix/master.cf.dpkg-tmp.$$ master.cf
361   restart_postfix="yes"
362 fi
363 rm -f /etc/postfix/master.cf.dpkg-tmp.$$
364
365 if ! cmp -s /etc/postfix/main.cf /etc/postfix/main.cf.dpkg-tmp.$$; then
366   cp_echo "CN: Backing up old version of /etc/postfix/main.cf in /var/backups/..."
367   cp_backup_conffile /etc/postfix/main.cf.dpkg-tmp.$$ main.cf
368   restart_postfix="yes"
369 fi
370 rm -f /etc/postfix/main.cf.dpkg-tmp.$$
371
372 #if ! grep -q "retry     unix  -       -       y       -       -       error" /etc/postfix/master.cf; then
373 #  echo "CN: Please, add this line in /etc/postfix/master.cf and restart Postfix."
374 #  echo "retry     unix  -       -       y       -       -       error"
375 #  echo
376 #fi
377
378 # kreiraj aliases_gecos
379 if [ -x /usr/share/postfix-cn/make-aliases-gecos.sh ]; then
380   if [ "$matchgecos" = "true" ]; then
381     echo "CN: Creating GECOS alias map..."
382     /usr/share/postfix-cn/make-aliases-gecos.sh
383   fi
384 fi
385
386 # rjesava gresku using backwards-compatible default setting chroot=y
387 postconf -F smtp/inet/chroot=y && restart_postfix="yes"
388
389 # restart 
390 if [ "$restart_postfix" = "yes" ]; then
391   service postfix restart
392 fi
393 cp_mail postfix-cn
394
395 #DEBHELPER#