prva verzija za stretch
[proftpd-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 # created:     2002-11-15 Bozo Juretic <bjuretic@srce.hr>
9 # last update: 2007-05-14 Zoran Dzelajlija <zoran.dzelajlija@carnet.hr>
10 # last update: 2011-04-24 Dinko Korunic <kreator@carnet.hr>
11
12 # Load CARNet Tools
13 . /usr/share/carnet-tools/functions.sh
14
15 # Load Debconf
16 #. /usr/share/debconf/confmodule
17
18 FTP_CONF=/etc/proftpd/proftpd.conf
19 FTP_TMP=`mktemp /etc/proftpd/proftpd.conf.XXXXXX`
20 FTP_OLD=/var/backups/proftpd.conf.bak
21 SSL_CERT=/etc/ssl/certs/ftpd.pem
22 SSL_KEY=/etc/ssl/private/ftpd.pem
23
24 # Backup stare konfiguracije
25 cp_backup_conffile $FTP_CONF
26 cp -p $FTP_CONF $FTP_TMP
27
28 # Onemogucavanje Anonymous ftp pristupa
29 disable_anonymous() {
30   if grep -qi "^<Anonymous" $FTP_TMP; then
31     echo "CN: Anonymous access has been disabled in $FTP_CONF." 
32     # Brisanje Anonymous linija
33     sed -n -i -e '/<Anonymous /,/\/Anon/!p' $FTP_TMP
34   fi
35 }
36
37 # Popravi razne stvari u confu
38 fix_conf() {
39   if [ -f $FTP_TMP ]; then
40     # Stare list opcije
41     sed -i -e 's/lsdefaultoptions/ListOptions/i' $FTP_TMP 
42
43     # Stari tcpwin
44     sed -i -e "s/tcpreceivewindow/SocketOptions rcvbuf/i" \
45            -e "s/tcpsendwindow/SocketOptions sndbuf/i" $FTP_TMP
46
47     # Scoreboard
48     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
49
50     # Ubaci pravi hostname
51     CARNET_HOSTNAME=`hostname`
52     CARNET_DOMAINNAME=`hostname --domain`
53     sed -i -e "s/^ServerName.*\"Debian\"/ServerName \"$CARNET_HOSTNAME.$CARNET_DOMAINNAME\"/i" $FTP_TMP 
54
55     # Upali DelayEngine
56     sed -i -e 's/^#.*DelayEngine.*/DelayEngine on/i' $FTP_TMP
57
58     # Omoguci da bude standalone servis
59     sed -i -e 's/^\(ServerType.*\)inetd/\1standalone/' $FTP_TMP
60     update-inetd --disable ftp || true
61     #db_set shared/proftpd/inetd_or_standalone "standalone"
62     #db_go || true
63     #db_stop
64   fi
65 }
66
67 # Dodaj TLS konfiguraciju ako je potrebna
68 add_tls() {
69   if [ -f $FTP_TMP ]; then
70     cp-update proftpd-cn $FTP_TMP <<EOF
71 <IfModule mod_tls.c>
72   TLSEngine on
73   TLSLog /var/log/proftpd/tls.log
74   TLSProtocol SSLv23
75
76   # Are clients required to use FTP over TLS when talking to this server?
77   TLSRequired off
78
79   # Server's certificate
80   TLSRSACertificateFile $SSL_CERT
81   TLSRSACertificateKeyFile $SSL_KEY
82
83   # CA the server trusts
84   #TLSCACertificateFile /etc/ftpd/root.cert.pem
85
86   # Authenticate clients that want to use FTP over TLS?
87   TLSVerifyClient off
88
89   # Allow SSL/TLS renegotiations when the client requests them, but
90   # do not force the renegotations.  Some clients do not support
91   # SSL/TLS renegotiations; when mod_tls forces a renegotiation, these
92   # clients will close the data connection, or there will be a timeout
93   # on an idle data connection.
94   TLSRenegotiate required off
95 </IfModule>
96 EOF
97   fi
98 }
99
100 # include za slucaj da sistemac nije prihvatio izmjene od Debiana
101 include_modules() {
102   if [ -f $FTP_TMP ] ; then
103     if ! egrep -qi "^[[:space:]]*Include.*/etc/proftpd/modules.conf" $FTP_TMP ; then
104       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.$$
105       cat $FTP_TMP >>$FTP_TMP.tmp.$$
106       cp_mv $FTP_TMP.tmp.$$ $FTP_TMP
107     fi
108   fi
109 }
110
111 # Generiranje SSL certifikata
112 make_ssl_cert() {
113   if [ \( ! -f $SSL_CERT \) -o \( ! -f $SSL_KEY \) ] ; then
114     echo "CN: Generating SSL certificate... "
115     HOSTNAME=`hostname -s`
116     FQDN=`hostname -f`
117     MAILNAME=`cat /etc/mailname 2> /dev/null || hostname -f`
118     (openssl req -new -x509 -days 365 -nodes -out $SSL_CERT -keyout $SSL_KEY >/dev/null 2>&1 <<EOF
119 .
120 .
121 .
122 FTP server
123 $hostname.$domainname
124 $fqdn
125 root@$mailname
126 EOF
127 )
128     echo "CN: Self-signed SSL certificate generated in $SSL_CERT."
129     echo "CN: Please note that the certificate will expire in one year."
130   fi
131 }
132
133 # purge starog proftpd-common paketa bi napravio rusvaj
134 defuse_old_postrm() {
135   if [ -f /var/lib/dpkg/info/proftpd-common.postrm ]; then
136     cp_check_and_sed '^[^#]*(update-rc.d|update-inetd|/var/run/proftpd)' \
137                      '/update-rc.d/d; /update-inetd/d; /var\/run\/proftpd/d' \
138                      /var/lib/dpkg/info/proftpd-common.postrm || true
139   fi
140 }
141
142 defuse_old_postrm
143 disable_anonymous
144 make_ssl_cert
145 fix_conf
146 add_tls
147 include_modules
148
149 if [ -z "$2" ]; then
150   echo "CN: Proftpd-cn is configured with disabled anonymous FTP access,"
151   echo "CN: for the security reasons."
152 fi
153
154 if ! cmp -s $FTP_TMP $FTP_CONF; then
155   echo "CN: Original configuration file is saved in $FTP_OLD."
156   cp_mv $FTP_TMP $FTP_CONF
157 else
158   rm -f $FTP_TMP
159 fi
160
161 echo "CN: Restarting proftpd..."
162 service proftpd restart
163
164 # dh_installdeb will replace this with shell code automatically
165 # generated by other debhelper scripts.
166
167 #DEBHELPER#
168
169 exit 0