Backup konfiguracijskih datoteka u /var/backups/xinetd-cn/.
[xinetd-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
11 PKG=xinetd-cn
12 CONF=/etc/xinetd.conf
13 INETDCONF=/etc/inetd.conf
14 DEFAULT=/etc/default/xinetd
15 BACKUPDIR=/var/backups/xinetd-cn
16
17 need_restart=0
18 temp_files=
19 xinetd_conf_did=0
20
21 # cleanup()
22 #
23 #   Cleanup all temp files.
24 #
25 cleanup () {
26
27         local item
28         if [ -n "$temp_files" ]; then
29                 for item in $temp_files; do
30                         if [ -e "$item" ]; then
31                                 rm -f $item
32                         fi
33                 done
34         fi
35 }
36
37
38 # Set trap for deleting all temp files.
39 #
40 trap cleanup 0 1 2 15
41
42 # First, we do backup (inetd, xinetd)
43 #
44 if cp_backup_conffile -r -d $BACKUPDIR -p $INETDCONF; then
45         cp_echo "CN: Old $INETDCONF saved as $BACKUPDIR/`basename $INETDCONF`.bak."
46 fi
47 if cp_backup_conffile -r -d $BACKUPDIR -p $CONF; then
48         cp_echo "CN: Old $CONF saved as $BACKUPDIR/`basename $CONF`.bak."
49 fi
50
51 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
52 temp_files="$CONFTMP"
53
54 if [ -f "$INETDCONF" ]; then
55
56         # Convert inetd.conf to temporary xinetd.conf file using xconv.pl tool
57         /usr/sbin/xconv.pl < $INETDCONF > $CONFTMP
58 fi
59
60 # Parse /etc/xinetd.conf file and convert services' configuration to
61 # separated configurations in /etc/xinetd.d/ directory
62 #
63 conffile_list="$CONFTMP"
64 if [ -f "$CONF" ]; then
65         if egrep -q "^service[[:space:]]+" "$CONF"; then
66                 conffile_list="$CONF $conffile_list"
67                 xinetd_conf_did=1
68         fi
69 fi
70 services_list="`cat $conffile_list | egrep "^service[[:space:]]+" | sed -r 's/service[[:space:]]+//g' | uniq`" || true
71
72 if [ -n "$services_list" ]; then
73
74     for service in $services_list; do
75
76         if [ -f "/etc/xinetd.d/$service" ]; then
77
78                 if cp_backup_conffile -r -d $BACKUPDIR -p /etc/xinetd.d/$service; then
79                         cp_echo "CN: Old /etc/xinetd.d/$service saved as $BACKUPDIR/`basename /etc/xinetd.d/$service`.bak."
80                 fi
81
82                 rm -f /etc/xinetd.d/$service
83         fi
84
85         touch /etc/xinetd.d/$service || true
86         # cat "$CONF" "$CONFTMP" | sed -n "/^service $service/,/^}/p" | cp-update "$PKG" "/etc/xinetd.d/$service"
87         cat $conffile_list | sed -rn "/^service[[:space:]]+$service/,/^}/p" >> "/etc/xinetd.d/$service"
88
89         need_restart=1
90     done
91
92     if egrep -q "^service[[:space:]]+" "$CONFTMP"; then
93         cp_echo "CN: All services were converted from $INETDCONF file to separated"
94         cp_echo "CN: configuration files located in /etc/xinetd.d/ directory."
95     fi
96
97     if [ $xinetd_conf_did -eq 1 ]; then
98         cp_echo "CN: All services were converted from $CONF file to separated"
99         cp_echo "CN: configuration files located in /etc/xinetd.d/ directory."
100     fi
101 fi
102
103 rm -f $CONFTMP
104
105 # Configuration file /etc/xinetd.conf does not exist?
106 #
107 if [ ! -f "$CONF" ]; then
108         touch "$CONF"
109 fi
110
111 # Remove services from /etc/xinetd.conf file
112 #
113 cp_check_and_sed "^service[[:space:]]+" \
114         "/^service[[:space:]]/,/^}/d" \
115         "$CONF" && need_restart=1 || true
116
117 # Check if there is no defaults block in /etc/xinetd.conf
118 #
119 if ! egrep -q '^defaults' "$CONF"; then
120
121         echo -e "defaults\n{\n\n}" | cp-update -m "$PKG" "$CONF"
122         cp_echo "CN: Added defaults block to $CONF"
123         
124         need_restart=1
125 fi
126
127 # Check if /etc/xinetd.d/ directory is included or not
128 #
129 if ! egrep -q '^includedir /etc/xinetd.d' "$CONF"; then
130
131         echo "includedir /etc/xinetd.d" | cp-update -m "$PKG" "$CONF"
132         cp_echo "CN: Added \"includedir /etc/xinetd.d\" line to $CONF"
133         
134         need_restart=1
135 fi
136
137 # Remove -inetd_compat option and set INETD_COMPAT to 'No' in /etc/default/xinetd
138 #
139 if [ -f "$DEFAULT" ]; then
140         
141         # File exists, set INETD_COMPAT to 'No'
142         cp_check_and_sed "^[[:space:]]*INETD_COMPAT.*[Yy][Ee][Ss]" \
143                 "s/^[[:space:]]*INETD_COMPAT.*[Yy][Ee][Ss]/INETD_COMPAT=No/g" \
144                 "$DEFAULT" && need_restart=1 || true
145
146         # File exists, remove -inetd_compat option
147         cp_check_and_sed "^[[:space:]]*XINETD_OPTS.*\-inetd_compat" \
148                 "s/\-inetd_compat//g" \
149                 "$DEFAULT" && need_restart=1 || true
150
151         # No INETD_COMPAT line?
152         if ! egrep -q '^[[:space:]]*INETD_COMPAT' "$DEFAULT"; then
153                 echo 'INETD_COMPAT=No' | cp-update -m "$PKG" "$DEFAULT"
154                 cp_echo "CN: Added 'INETD_COMPAT=No' to $DEFAULT."
155                 need_restart=1
156         fi
157
158         # Just in case there is no active XINETD_OPTS line
159         if ! egrep -q '^[[:space:]]*XINETD_OPTS' "$DEFAULT"; then
160                 echo 'XINETD_OPTS="-stayalive"' | cp-update -m "$PKG" "$DEFAULT"
161                 cp_echo "CN: Added -stayalive option to $DEFAULT."
162                 need_restart=1
163         fi
164 else
165         # File wasn't there at all?
166         touch $DEFAULT
167         echo -e 'INETD_COMPAT=No\nXINETD_OPTS="-stayalive"' | cp-update "$PKG" "$DEFAULT"
168         cp_echo "CN: Created $DEFAULT file with -stayalive enabled and -inetd_compat disabled."
169
170         need_restart=1
171 fi
172
173 # Restart xinetd if needed
174 #
175 if [ $need_restart -eq 1 ]; then
176     do=restart
177 fi
178 pgrep -u root -f /usr/sbin/xinetd > /dev/null || do=start
179
180 if [ -n "$do" ]; then
181
182         # Restart xinetd.
183         if [ -x "/etc/init.d/xinetd" ]; then
184                 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
185                     invoke-rc.d xinetd "$do" || exit $?
186                 else
187                     /etc/init.d/xinetd "$do" || exit $?
188                 fi
189         fi
190 fi
191
192 # Mail root
193 #
194 cp_mail "$PKG"
195
196 exit 0