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