r2: - fix debconf issue with openssl interactive
[proftpd-cn.git] / debian / postinst
1 #!/bin/sh
2 # postinst script for proftpd-cn
3 #
4 # see: dh_installdeb(1)
5
6 set -e
7
8 # summary of how this script can be called:
9 #        * <postinst> `configure' <most-recently-configured-version>
10 #        * <old-postinst> `abort-upgrade' <new version>
11 #        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
12 #          <new-version>
13 #        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
14 #          <failed-install-package> <version> `removing'
15 #          <conflicting-package> <version>
16 # for details, see http://www.debian.org/doc/debian-policy/ or
17 # the debian-policy package
18 #
19
20 case "$1" in
21     configure|reconfigure)
22       # continue below
23     ;;
24
25     *)
26         exit 0
27     ;;
28 esac
29
30 # created:     2002-11-15 Bozo Juretic <bjuretic@srce.hr>
31 # last update: 2007-05-14 Zoran Dzelajlija <zoran.dzelajlija@carnet.hr>
32 # last update: 2007-10-27 Dinko Korunic <kreator@carnet.hr>
33
34 # Source debconf library.
35 . /usr/share/debconf/confmodule
36
37 # Import CN toolsa
38 . /usr/share/carnet-tools/functions.sh
39
40 FTP_CONF=/etc/proftpd/proftpd.conf
41 FTP_TMP=`mktemp /etc/proftpd/proftpd.conf.XXXXXX`
42 FTP_OLD=/var/backups/proftpd.conf.bak
43 SSL_CERT=/etc/ssl/certs/ftpd-rsa.pem
44 SSL_KEY=/etc/ssl/certs/ftpd-rsa-key.pem
45
46 # Backup stare konfiguracije
47 cp_backup_conffile $FTP_CONF
48 cp -p $FTP_CONF $FTP_TMP
49
50 # Onemogucavanje Anonymous ftp pristupa
51 disable_anonymous()
52 {
53     if grep -qi "^<Anonymous" $FTP_TMP; then
54         echo "CN: Anonymous access has been disabled in $FTP_CONF." 
55         # Brisanje Anonymous linija
56         sed -n -i -e '/<Anonymous /,/\/Anon/!p' $FTP_TMP
57     fi
58 }
59
60 # Popravi razne stvari u confu
61 fix_conf()
62 {
63     if [ -f $FTP_TMP ]; then
64         # Stare list opcije
65         sed -i -e 's/lsdefaultoptions/ListOptions/i' $FTP_TMP 
66
67         # Stari tcpwin
68         sed -i -e "s/tcpreceivewindow/SocketOptions rcvbuf/i" \
69             -e "s/tcpsendwindow/SocketOptions sndbuf/i" $FTP_TMP
70
71         # Scoreboard
72         sed -i -e "s/\(scoreboardpath.*\)/#\n#ScoreboardPath is deprecated in 1.2.9, use ScoreboardFile instead\n#\1\n#\n#ScoreboardFile\t\/var\/run\/proftpd\/proftpd.scoreboard\n#/i" $FTP_TMP
73
74         # Ubaci pravi hostname
75         CARNET_HOSTNAME=`hostname`
76         CARNET_DOMAINNAME=`hostname --domain`
77             sed -i -e "s/^ServerName.*\"Debian\"/ServerName \"$CARNET_HOSTNAME.$CARNET_DOMAINNAME\"/i" $FTP_TMP 
78
79         # Upali DelayEngine
80         sed -i -e 's/^#.*DelayEngine.*/DelayEngine on/i' $FTP_TMP
81
82         # Omoguci da bude standalone servis
83         sed -i -e 's/^\(ServerType.*\)inetd/\1standalone/' $FTP_TMP
84         update-inetd --disable ftp || true
85         db_set shared/proftpd/inetd_or_standalone "standalone"
86         db_go || true
87         db_stop
88     fi
89 }
90
91 # Dodaj TLS konfiguraciju ako je potrebna
92 add_tls()
93 {
94     if [ -f $FTP_TMP ]; then
95         cp-update proftpd-cn $FTP_TMP <<EOF
96 <IfModule mod_tls.c>
97   TLSEngine on
98
99   # Are clients required to use FTP over TLS when talking to this server?
100   TLSRequired off
101
102   # Server's certificate
103   TLSRSACertificateFile $SSL_CERT
104   TLSRSACertificateKeyFile $SSL_KEY
105
106   # CA the server trusts
107   #TLSCACertificateFile /etc/ftpd/root.cert.pem
108
109   # Authenticate clients that want to use FTP over TLS?
110   TLSVerifyClient off
111
112   # Allow SSL/TLS renegotiations when the client requests them, but
113   # do not force the renegotations.  Some clients do not support
114   # SSL/TLS renegotiations; when mod_tls forces a renegotiation, these
115   # clients will close the data connection, or there will be a timeout
116   # on an idle data connection.
117   TLSRenegotiate required off
118 </IfModule>
119 EOF
120     fi
121 }
122
123 # include za slucaj da sistemac nije prihvatio izmjene od Debiana
124 include_modules()
125 {
126     if [ -f $FTP_TMP ] ; then
127         if ! egrep -qi "^[[:space:]]*Include.*/etc/proftpd/modules.conf" $FTP_TMP ; then
128             printf "#\n# Includes required DSO modules. This is mandatory in proftpd 1.3\n#\nInclude\t/etc/proftpd/modules.conf\n\n" >$FTP_TMP.tmp.$$
129             cat $FTP_TMP >>$FTP_TMP.tmp.$$
130             mv -f $FTP_TMP.tmp.$$ $FTP_TMP
131         fi
132     fi
133 }
134
135 # Generiranje SSL certifikata
136 make_ssl_cert()
137 {
138     if [ ! -f $SSL_CERT ] ; then
139         cd $(dirname $SSL_CERT)
140         echo "CN: Generating SSL certificate ... "
141         openssl req -new -x509 -days 365 -nodes -out $(basename $SSL_CERT) -keyout $(basename $SSL_KEY)
142         echo "CN: Self-signed SSL certificate generated in $SSL_CERT."
143         echo "CN: Please note that the certificate will expire in one year."
144     fi
145 }
146
147 # purge starog proftpd-common paketa bi napravio rusvaj
148 defuse_old_postrm()
149 {
150     if [ -f /var/lib/dpkg/info/proftpd-common.postrm ]; then
151         cp_check_and_sed '^[^#]*(update-rc.d|update-inetd|/var/run/proftpd)' \
152             '/update-rc.d/d; /update-inetd/d; /var\/run\/proftpd/d' \
153             /var/lib/dpkg/info/proftpd-common.postrm || true
154     fi
155 }
156
157 defuse_old_postrm
158 disable_anonymous
159 make_ssl_cert
160 fix_conf
161 add_tls
162 include_modules
163
164 if [ -z "$2" ]; then
165         echo "CN: Proftpd-cn is configured with disabled anonymous FTP access,"
166         echo "CN: for the security reasons."
167 fi
168
169 if ! cmp -s $FTP_TMP $FTP_CONF; then
170         echo "CN: Original configuration file is saved in $FTP_OLD."
171         cp_mv $FTP_TMP $FTP_CONF
172 else
173         rm -f $FTP_TMP
174 fi
175
176 echo "CN: Restarting proftpd ..."
177
178 if [ -x /usr/sbin/invoke-rc.d ]; then
179         invoke-rc.d proftpd restart
180 else
181         /etc/init.d/proftpd restart
182 fi
183
184 # dh_installdeb will replace this with shell code automatically
185 # generated by other debhelper scripts.
186
187 #DEBHELPER#
188
189 exit 0