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