r6:
[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.1.5-8; 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   postconf -e mydestination="$mydestination"
90 fi
91
92 # default je: $myhostname, localhost.$mydomain, localhost
93 # treba dodati jos ", $mydomain", ako je host mx
94 cp_get_mx_domain || true
95 mx_domain="$RET"
96 if [ "$mx_domain" != "$fqdn" ]; then
97   mydestination="`postconf -h mydestination`"
98   if ! echo $mydestination | grep -q ", \$mydomain"; then
99     postconf -e mydestination="$mydestination, \$mydomain"
100   fi
101   postconf -e relay_domains="\$mydestination"
102   if [ "$mx_domain" != "$domain" ]; then
103     if dpkg --compare-versions "$2" lt 2.1.5-8; then
104       cp_echo "CN: This host is MX for more than one domain!  postfix-cn package will"
105       cp_echo "CN  only set up $mx_domain, you HAVE TO add the other domains manually"
106       cp_echo "CN  to mydestination variable in /etc/postfix/main.cf, if you haven't"
107       cp_echo "CN  done that already or you may lose mail."
108     fi
109   fi
110 fi
111
112 # sredi $alias_maps
113 if [ "$matchgecos" = "true" ]; then
114   aliasmaps="hash:/etc/aliases hash:/var/lib/postfix-cn/aliases_gecos"
115 elif [ "$matchgecos" = "false" ]; then
116   aliasmaps="hash:/etc/aliases"
117   alias_maps="`postconf -h alias_maps | \
118                sed -e 's#hash:/var/lib/postfix-cn/aliases_gecos,*##g' -e 's/,*[[:space:]]*$//g'`"
119   postconf -e alias_maps="$alias_maps"
120 fi
121
122 for i in $aliasmaps; do
123   alias_maps="`postconf -h alias_maps`"
124   if ! echo $alias_maps | grep -q "$i"; then
125     postconf -e alias_maps="$alias_maps, $i"
126   fi
127 done
128
129 postconf -e alias_database="hash:/etc/aliases"
130 postconf -e local_recipient_maps="proxy:unix:passwd.byname \$alias_maps"
131
132 # jos malo
133 postconf -e recipient_delimiter="+"
134 postconf -e smtpd_helo_required="yes"
135 postconf -e smtpd_helo_restrictions="permit_mynetworks"
136 postconf -e in_flow_delay="2s"
137 postconf -e initial_destination_concurrency="10"
138 postconf -e default_destination_concurrency_limit="30"
139 postconf -e smtpd_recipient_limit="400"
140 postconf -e hash_queue_depth="3"
141
142 if dpkg --compare-versions "$2" lt 2.1.5-6; then
143   postconf -e bounce_queue_lifetime="5d"
144   postconf -e maximal_queue_lifetime="5d"
145 fi
146
147 # ako netko zeli maknuti maskiranje, da mu ga nova verzija ne doda
148 if dpkg --compare-versions "$2" lt 2.1.5-2; then
149   postconf -e masquerade_domains="\$mydomain"
150 fi
151
152 # sredi smtpd_recipient_restrictions
153 smtpd_recipient_restrictions="reject_invalid_hostname, \
154                               reject_unknown_sender_domain, \
155                               reject_unknown_recipient_domain, \
156                               reject_unauth_pipelining, \
157                               permit_sasl_authenticated, \
158                               permit_mynetworks, \
159                               reject_unauth_destination"
160
161 # dodaj samo rbl-ove iz debconfa
162 if [ "$rbl" ]; then
163   for i in `echo $rbl | tr -d ,`; do
164     smtpd_recipient_restrictions="$smtpd_recipient_restrictions, reject_rbl_client $i"
165   done
166 fi
167
168 # dodaj check_policy_service i permit na kraj
169 smtpd_recipient_restrictions="$smtpd_recipient_restrictions, check_policy_service inet:127.0.0.1:60000, permit"
170 postconf -e smtpd_recipient_restrictions="$smtpd_recipient_restrictions"
171
172 # SASL stuff
173 postconf -e smtpd_sasl_auth_enable="yes"
174 postconf -e smtpd_sasl_security_options="noanonymous"
175 postconf -e smtpd_sasl_local_domain="\$myhostname"
176 postconf -e broken_sasl_auth_clients="yes"
177
178 [ -d /etc/postfix/sasl/ ] || mkdir -p /etc/postfix/sasl/
179 if [ ! -f /etc/postfix/sasl/smtpd.conf ]; then
180   cat - > /etc/postfix/sasl/smtpd.conf <<EOF
181 pwcheck_method: saslauthd
182 mech_list: plain login
183 EOF
184   restart_postfix="yes"
185 fi
186
187 if [ -f /etc/default/saslauthd ]; then
188   cp_check_and_sed "^START=no" \
189                    "s/^START=no/START=yes/g" \
190                    /etc/default/saslauthd && restart_saslauthd="yes"
191
192   cp_check_and_sed "^#[[:blank:]]*START=" \
193                    "s/^#[[:blank:]]*START=.*$/START=yes/g" \
194                    /etc/default/saslauthd && restart_saslauthd="yes"
195
196   if ! grep -q "^START.*yes" /etc/default/saslauthd; then
197     echo "START=yes" >> /etc/default/saslauthd
198     restart_saslauthd="yes"
199   fi
200   if ! grep -q "^PARAMS.*/var/spool/postfix/var/run/saslauthd" /etc/default/saslauthd; then
201     echo "PARAMS=\"-m /var/spool/postfix/var/run/saslauthd\"" >> /etc/default/saslauthd
202     restart_saslauthd="yes"
203   fi
204   if ! grep -q "^PIDFILE.*/var/spool/postfix/var/run/saslauthd/saslauthd.pid" /etc/default/saslauthd; then
205     echo "PIDFILE=\"/var/spool/postfix/var/run/saslauthd/saslauthd.pid\"" >> /etc/default/saslauthd
206     restart_saslauthd="yes"
207   fi
208   if ! grep -q "^PWDIR.*/var/spool/postfix/var/run/saslauthd" /etc/default/saslauthd; then
209     echo "PWDIR=\"/var/spool/postfix/var/run/saslauthd\"" >> /etc/default/saslauthd
210     restart_saslauthd="yes"
211   fi
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   if [ ! -L /var/run/saslauthd ]; then
217     mkdir -p /var/spool/postfix/var/run/
218     if [ -d /var/run/saslauthd -a ! -d /var/spool/postfix/var/run/saslauthd ]; then
219       mv /var/run/saslauthd /var/spool/postfix/var/run/
220     elif [ -d /var/run/saslauthd -a -d /var/spool/postfix/var/run/saslauthd ]; then
221       rm -rf /var/run/saslauthd/
222     fi
223
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
229     restart_saslauthd="yes"
230   fi
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   if ! dpkg-statoverride --list /var/spool/postfix/var/run/saslauthd > /dev/null; then
236     dpkg-statoverride --update --add root sasl 710 /var/spool/postfix/var/run/saslauthd
237   fi
238 fi
239
240 # restart saslauthd
241 if [ "$restart_saslauthd" = "yes" ]; then
242   if [ -x /usr/sbin/invoke-rc.d ]; then
243     [ -x /etc/init.d/saslauthd ] && invoke-rc.d saslauthd restart
244   else
245     [ -x /etc/init.d/saslauthd ] && /etc/init.d/saslauthd restart
246   fi
247 fi
248
249 # izgenerirati certifikate
250 cert="postfix"   # basename of certificate
251 description="Postfix SMTP daemon"      # descriptive text
252 cd /etc/ssl/certs
253 PATH=$PATH:/usr/bin/ssl
254 if [ -f "$cert.pem" ]; then
255   echo "CN: You already have /etc/ssl/certs/$cert.pem"
256 else
257   echo "CN: Creating generic self-signed certificate: /etc/ssl/certs/$cert.pem"
258   echo "CN: (replace with hand-crafted or authorized one if needed)."
259   HOSTNAME=`hostname -s`
260   FQDN=`hostname -f`
261         openssl req -new -x509 -days 365 -nodes -out "$cert.pem" -keyout "$cert.pem" > /dev/null 2>&1 <<+
262 .
263 .
264 .
265 $description
266 $hostname
267 $fqdn
268 root@$mailname
269 +
270   ln -sf "$cert.pem" `openssl x509 -noout -hash < "$cert.pem"`.0
271   chown root.root "/etc/ssl/certs/$cert.pem"
272   chmod 0640 "/etc/ssl/certs/$cert.pem"
273 fi
274
275 # TLS stuff
276 postconf -e smtp_use_tls="yes"
277 postconf -e smtp_tls_key_file="\$smtp_tls_cert_file"
278 postconf -e smtp_tls_session_cache_database="sdbm:/var/spool/postfix/smtp_scache"
279 postconf -e smtp_tls_session_cache_timeout="3600s"
280 postconf -e smtpd_use_tls="yes"
281 postconf -e smtpd_tls_key_file="\$smtpd_tls_cert_file"
282 postconf -e smtpd_tls_session_cache_database="sdbm:/var/spool/postfix/smtpd_scache"
283 postconf -e smtpd_tls_session_cache_timeout="3600s"
284 postconf -e tls_random_source="dev:/dev/urandom"
285 # ako je postavljen neki drugi certifikat, ne diraj
286 smtp_tls_cert_file="`postconf -h smtp_tls_cert_file`"
287 if [ -z "$smtp_tls_cert_file" ]; then
288   postconf -e smtp_tls_cert_file="/etc/ssl/certs/$cert.pem"
289 fi
290 smtpd_tls_cert_file="`postconf -h smtpd_tls_cert_file`"
291 if [ -z "$smtpd_tls_cert_file" ]; then
292   postconf -e smtpd_tls_cert_file="/etc/ssl/certs/$cert.pem"
293 fi
294
295 # prije bilo u /etc/postfix/, od verzije 2.1.5-2 je u /var/spool/postfix
296 [ -f /etc/postfix/smtp_scache.dir ] && rm -f /etc/postfix/smtp_scache.dir
297 [ -f /etc/postfix/smtp_scache.pag ] && rm -f /etc/postfix/smtp_scache.pag
298 [ -f /etc/postfix/smtpd_scache.dir ] && rm -f /etc/postfix/smtpd_scache.dir
299 [ -f /etc/postfix/smtpd_scache.pag ] && rm -f /etc/postfix/smtpd_scache.pag
300
301 # debconf stop
302 db_go || true
303 db_stop || true
304
305 # ako postoji amavis, sredi
306 if [ -x /usr/share/amavisd-cn/postfixize.sh ]; then
307   /usr/share/amavisd-cn/postfixize.sh
308 fi
309
310 # provjeri da li postoji sendmail-base
311 if ! dpkg -l sendmail-base | grep -q '^.n'; then
312   cp_echo ""
313   cp_echo "CN: Package sendmail-base detected."
314   cp_echo "CN: Please, purge this package with command:"
315   cp_echo "CN: # dpkg --purge sendmail-base"
316   cp_echo ""
317 fi
318
319 # sendmailov /etc/mail/access
320 if [ -f /etc/mail/access -a /etc/mail/access.db ]; then
321   cp_echo ""
322   cp_echo "CN: Sendmail's access file (/etc/mail/access) detected."
323   cp_echo "CN: Please, upgrade your's /etc/postfix/access."
324   cp_echo "CN: http://www.postfix.org/SMTPD_ACCESS_README.html"
325   cp_echo ""
326 fi
327
328 if ! id postfix | grep -q sasl; then
329   adduser postfix sasl > /dev/null
330   restart_postfix="yes"
331 fi
332
333 # ako ima razlika, backupiraj stari, ako ne, obrisi privremeni backup
334 if ! cmp -s /etc/postfix/master.cf /etc/postfix/master.cf.dpkg-tmp.$$; then
335   cp_echo "CN: Backing up old version of /etc/postfix/master.cf in /var/backups/..."
336   cp_backup_conffile /etc/postfix/master.cf.dpkg-tmp.$$ master.cf
337   restart_postfix="yes"
338 fi
339 rm -f /etc/postfix/master.cf.dpkg-tmp.$$
340
341 if ! cmp -s /etc/postfix/main.cf /etc/postfix/main.cf.dpkg-tmp.$$; then
342   cp_echo "CN: Backing up old version of /etc/postfix/main.cf in /var/backups/..."
343   cp_backup_conffile /etc/postfix/main.cf.dpkg-tmp.$$ main.cf
344   restart_postfix="yes"
345 fi
346 rm -f /etc/postfix/main.cf.dpkg-tmp.$$
347
348 # kreiraj aliases_gecos
349 if [ -x /usr/share/postfix-cn/make-aliases-gecos.sh ]; then
350   if [ "$matchgecos" = "true" ]; then
351     echo "CN: Creating GECOS alias map..."
352     /usr/share/postfix-cn/make-aliases-gecos.sh
353   fi
354 fi
355
356 if dpkg --compare-versions "$2" lt 2.1.5-6 && \
357    [ -f /etc/postgrey/whitelist_clients ]; then
358   # dodaj CARNet MX u /etc/postgrey/whitelist_clients
359   cp-update postfix-cn /etc/postgrey/whitelist_clients < /usr/share/postfix-cn/carnet_whitelist_clients
360   restart_postgrey="yes"
361 fi
362
363 if [ -f /etc/default/postgrey ]; then
364   if ! grep -q "^POSTGREY_OPTS=.*--delay" /etc/default/postgrey; then
365     cp_check_and_sed 'POSTGREY_OPTS=' \
366                      's/^POSTGREY_OPTS="/POSTGREY_OPTS="--delay=58 /g' \
367                      /etc/default/postgrey || true
368   restart_postgrey="yes"
369   fi
370 fi
371
372 if [ "$restart_postgrey" = "yes" ]; then
373   if [ -x /usr/sbin/invoke-rc.d ]; then
374     [ -x /etc/init.d/postgrey ] && invoke-rc.d postgrey restart
375   else
376     [ -x /etc/init.d/postgrey ] && /etc/init.d/postgrey restart
377   fi
378 fi
379
380 # restart 
381 if [ "$restart_postfix" = "yes" ]; then
382   if [ -x /usr/sbin/invoke-rc.d ]; then
383     [ -x /etc/init.d/postfix ] && invoke-rc.d postfix restart
384   else
385     [ -x /etc/init.d/postfix ] && /etc/init.d/postfix restart
386   fi
387 fi
388 cp_mail postfix-cn