ffcbf7feee0caddc0d1701eb68c4edf1d60bc3bc
[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: 2011-04-24 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.pem
44 SSL_KEY=/etc/ssl/private/ftpd.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   TLSLog /var/log/proftpd/tls.log
99   TLSProtocol SSLv23
100
101   # Are clients required to use FTP over TLS when talking to this server?
102   TLSRequired off
103
104   # Server's certificate
105   TLSRSACertificateFile $SSL_CERT
106   TLSRSACertificateKeyFile $SSL_KEY
107
108   # CA the server trusts
109   #TLSCACertificateFile /etc/ftpd/root.cert.pem
110
111   # Authenticate clients that want to use FTP over TLS?
112   TLSVerifyClient off
113
114   # Allow SSL/TLS renegotiations when the client requests them, but
115   # do not force the renegotations.  Some clients do not support
116   # SSL/TLS renegotiations; when mod_tls forces a renegotiation, these
117   # clients will close the data connection, or there will be a timeout
118   # on an idle data connection.
119   TLSRenegotiate required off
120 </IfModule>
121 EOF
122     fi
123 }
124
125 # include za slucaj da sistemac nije prihvatio izmjene od Debiana
126 include_modules()
127 {
128     if [ -f $FTP_TMP ] ; then
129         if ! egrep -qi "^[[:space:]]*Include.*/etc/proftpd/modules.conf" $FTP_TMP ; then
130             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.$$
131             cat $FTP_TMP >>$FTP_TMP.tmp.$$
132             cp_mv $FTP_TMP.tmp.$$ $FTP_TMP
133         fi
134     fi
135 }
136
137 # Generiranje SSL certifikata
138 make_ssl_cert()
139 {
140     if [ \( ! -f $SSL_CERT \) -o \( ! -f $SSL_KEY \) ] ; then
141         echo "CN: Generating SSL certificate... "
142         HOSTNAME=`hostname -s`
143         FQDN=`hostname -f`
144         MAILNAME=`cat /etc/mailname 2> /dev/null || hostname -f`
145         (openssl req -new -x509 -days 365 -nodes -out $SSL_CERT -keyout $SSL_KEY >/dev/null 2>&1 <<EOF
146 .
147 .
148 .
149 FTP server
150 $hostname.$domainname
151 $fqdn
152 root@$mailname
153 EOF
154 )
155         echo "CN: Self-signed SSL certificate generated in $SSL_CERT."
156         echo "CN: Please note that the certificate will expire in one year."
157     fi
158 }
159
160 # purge starog proftpd-common paketa bi napravio rusvaj
161 defuse_old_postrm()
162 {
163     if [ -f /var/lib/dpkg/info/proftpd-common.postrm ]; then
164         cp_check_and_sed '^[^#]*(update-rc.d|update-inetd|/var/run/proftpd)' \
165             '/update-rc.d/d; /update-inetd/d; /var\/run\/proftpd/d' \
166             /var/lib/dpkg/info/proftpd-common.postrm || true
167     fi
168 }
169
170 defuse_old_postrm
171 disable_anonymous
172 make_ssl_cert
173 fix_conf
174 add_tls
175 include_modules
176
177 if [ -z "$2" ]; then
178         echo "CN: Proftpd-cn is configured with disabled anonymous FTP access,"
179         echo "CN: for the security reasons."
180 fi
181
182 if ! cmp -s $FTP_TMP $FTP_CONF; then
183         echo "CN: Original configuration file is saved in $FTP_OLD."
184         cp_mv $FTP_TMP $FTP_CONF
185 else
186         rm -f $FTP_TMP
187 fi
188
189 echo "CN: Restarting proftpd..."
190
191 if [ -x /usr/sbin/invoke-rc.d ]; then
192         invoke-rc.d proftpd restart
193 else
194         /etc/init.d/proftpd restart
195 fi
196
197 # dh_installdeb will replace this with shell code automatically
198 # generated by other debhelper scripts.
199
200 #DEBHELPER#
201
202 exit 0