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