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