Provjera postojanosti datoteka prije njihovog backupa.
[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 XCONV_TOOL=/usr/sbin/xconv.pl
17
18 need_restart=0
19 temp_files=
20 xinetd_conf_did=0
21
22 # cleanup()
23 #
24 #   Cleanup all temp files.
25 #
26 cleanup () {
27     local item
28
29     if [ -n "$temp_files" ]; then
30         for item in $temp_files; do
31             if [ -e "$item" ]; then
32                 rm -f $item
33             fi
34         done
35     fi
36 }
37
38 # backup_and_disable_service()
39 #
40 #   Backup and disable service with invalid configuration.
41 #   Arguments:  service, services_file
42 #
43 backup_and_disable_service () {
44     local serv servfile out
45     serv="$1"
46     servfile="$2"
47
48     if cp_backup_conffile -r -d $BACKUPDIR -p /etc/xinetd.d/$servfile; then
49         cp_echo "CN: Old /etc/xinetd.d/$servfile saved as $BACKUPDIR/`basename /etc/xinetd.d/$servfile`.bak."
50     fi
51
52     cp_echo "CN: Disabling service '$serv' in configuration file /etc/xinetd.d/$servfile."
53
54     out=`mktemp /etc/xinetd.d/$servfile.tmp.XXXXXX`
55     temp_files="$temp_files $out"
56
57     sed -r "/^[[:space:]]*service[[:space:]]+$serv[[:space:]]*$/,/^}/ s/^(.*)/#\1/" \
58         /etc/xinetd.d/$servfile > $out
59     rm -f /etc/xinetd.d/$servfile
60     mv "$out" "/etc/xinetd.d/$servfile"
61     chmod 644 "/etc/xinetd.d/$servfile"
62 }
63
64 # Set trap for deleting all temp files.
65 #
66 trap cleanup 0 1 2 15
67
68 # First, we do backup (inetd, xinetd)
69 #
70 if [ -f "$INETDCONF" ]; then
71     if cp_backup_conffile -r -d $BACKUPDIR -p $INETDCONF; then
72         cp_echo "CN: Old $INETDCONF saved as $BACKUPDIR/`basename $INETDCONF`.bak."
73     fi
74 fi
75 if [ -f "$CONF" ]; then
76     if cp_backup_conffile -r -d $BACKUPDIR -p $CONF; then
77         cp_echo "CN: Old $CONF saved as $BACKUPDIR/`basename $CONF`.bak."
78     fi
79 fi
80
81 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
82 temp_files="$CONFTMP"
83
84 if [ -f "$INETDCONF" ]; then
85     # Convert inetd.conf to temporary xinetd.conf file using xconv.pl tool
86     egrep -v "^#<off>#" $INETDCONF | $XCONV_TOOL > $CONFTMP || true
87 fi
88
89 # Parse /etc/xinetd.conf file and convert services' configuration to
90 # separated configurations in /etc/xinetd.d/ directory
91 #
92 conffile_list="$CONFTMP"
93 if [ -f "$CONF" ]; then
94     if egrep -q "^[[:space:]]*service[[:space:]]+" "$CONF"; then
95         conffile_list="$CONF $conffile_list"
96         xinetd_conf_did=1
97     fi
98 fi
99 services_list="`sed -nr 's/^[[:space:]]*service[[:space:]]+//p' $conffile_list | uniq`"
100
101 if [ -n "$services_list" ]; then
102
103     for service in $services_list; do
104
105         if [ -f "/etc/xinetd.d/$service" ]; then
106             if cp_backup_conffile -r -d $BACKUPDIR -p /etc/xinetd.d/$service; then
107                 cp_echo "CN: Old /etc/xinetd.d/$service saved as $BACKUPDIR/`basename /etc/xinetd.d/$service`.bak."
108             fi
109             rm -f /etc/xinetd.d/$service
110         fi
111
112         touch /etc/xinetd.d/$service || true
113         # cat "$CONF" "$CONFTMP" | sed -n "/^service $service/,/^}/p" | cp-update "$PKG" "/etc/xinetd.d/$service"
114         sed -rn "/^[[:space:]]*service[[:space:]]+$service[[:space:]]*$/,/^}/p" \
115             $conffile_list >> "/etc/xinetd.d/$service"
116
117         need_restart=1
118     done
119
120     if egrep -q "^[[:space:]]*service[[:space:]]+" "$CONFTMP"; then
121         cp_echo "CN: All services were converted from $INETDCONF file to separated"
122         cp_echo "CN: configuration files located in /etc/xinetd.d/ directory."
123     fi
124
125     if [ $xinetd_conf_did -eq 1 ]; then
126         cp_echo "CN: All services were converted from $CONF file to separated"
127         cp_echo "CN: configuration files located in /etc/xinetd.d/ directory."
128     fi
129 fi
130
131 rm -f $CONFTMP
132
133 # Configuration file /etc/xinetd.conf does not exist?
134 #
135 if [ ! -f "$CONF" ]; then
136     touch "$CONF"
137 fi
138 CONF_CONTENT=
139
140 # Remove services from /etc/xinetd.conf file
141 #
142 cp_check_and_sed "^[[:space:]]*service[[:space:]]+" \
143     "/^[[:space:]]*service[[:space:]]/,/^}/d" \
144     "$CONF" && need_restart=1 || true
145
146 # Check if there is no defaults block in /etc/xinetd.conf
147 #
148 if ! egrep -q '^defaults' "$CONF"; then
149     cp_echo "CN: Adding defaults block to $CONF"
150     CONF_CONTENT="defaults
151 {
152
153 }"
154 fi
155
156 # Check if /etc/xinetd.d/ directory is included or not
157 #
158 if ! egrep -q '^includedir /etc/xinetd.d' "$CONF"; then
159     cp_echo "CN: Adding \"includedir /etc/xinetd.d\" line to $CONF"
160     CONF_CONTENT="$CONF_CONTENT
161 includedir /etc/xinetd.d"
162 fi
163
164 if [ -n "$CONF_CONTENT" ]; then
165     echo "$CONF_CONTENT" | cp-update -m "$PKG" "$CONF"
166     need_restart=1
167 fi
168
169 # Validate services' configuration.
170 #
171 services_file_list="`ls -1 /etc/xinetd.d/`"
172 if [ -n "$services_file_list" ]; then
173
174     for services_file in $services_file_list; do
175         # Get services list from $services_file
176         services_list="`sed -rn "s/^[[:space:]]*service[[:space:]]+(.*)[[:space:]]*$/\1/p" /etc/xinetd.d/$services_file`"
177
178         if [ -n "$services_list" ]; then
179
180             for service in $services_list; do
181                 service_disable=0
182                 service_block="`sed -rn "/^[[:space:]]*service[[:space:]]+$service[[:space:]]*$/,/^}/p" /etc/xinetd.d/$services_file`"
183
184                 # Check service's user
185                 service_user="`echo "$service_block" | sed -nr "s/^[[:space:]]*user[[:space:]]*=[[:space:]]*(.*)[[:space:]]*$/\1/p"`"
186                 if [ -n "$service_user" ]; then
187                     for service_user_x in $service_user; do
188                         service_user_chk="`getent passwd $service_user_x`" || true
189                         if [ -z "$service_user_chk" ]; then
190                             cp_echo "CN: Error in /etc/xinetd.d/$services_file for service '$service' - user '$service_user_x' does not exist."
191                             service_disable=1
192                             break
193                         fi
194                     done
195                 fi
196
197                 # Check service's group
198                 service_group="`echo "$service_block" | sed -nr "s/^[[:space:]]*group[[:space:]]*=[[:space:]]*(.*)[[:space:]]*$/\1/p"`"
199                 if [ -n "$service_group" ]; then
200                     for service_group_x in $service_group; do
201                         service_group_chk="`getent passwd $service_group_x`" || true
202                         if [ -z "$service_group_chk" ]; then
203                             cp_echo "CN: Error in /etc/xinetd.d/$services_file for service '$service' - group '$service_group_x' does not exist."
204                             service_disable=1
205                             break
206                         fi
207                     done
208                 fi
209
210                 # Check service's binary
211                 service_server="`echo "$service_block" | sed -nr "s/^[[:space:]]*server[[:space:]]*=[[:space:]]*(.*)[[:space:]]*$/\1/p"`"
212                 if [ -n "$service_server" ]; then
213                     for service_server_x in $service_server; do
214                         if [ ! -x "$service_server_x" ]; then
215                             cp_echo "CN: Error in /etc/xinetd.d/$services_file for service '$service' - server '$service_server_x' does not exist."
216                             service_disable=1
217                             break
218                         fi
219                     done
220                 fi
221
222                 if [ $service_disable -eq 1 ]; then
223                     backup_and_disable_service "$service" "$services_file"
224                     need_restart=1
225                 fi
226             done
227         fi
228     done
229 fi
230
231 # Remove -inetd_compat option and set INETD_COMPAT to 'No' in /etc/default/xinetd
232 #
233 if [ -f "$DEFAULT" ]; then
234     # File exists, set INETD_COMPAT to 'No'
235     cp_check_and_sed "^[[:space:]]*INETD_COMPAT.*[Yy][Ee][Ss]" \
236         "s/^[[:space:]]*INETD_COMPAT.*[Yy][Ee][Ss]/INETD_COMPAT=No/g" \
237         "$DEFAULT" && need_restart=1 || true
238
239     # File exists, remove -inetd_compat option
240     cp_check_and_sed "^[[:space:]]*XINETD_OPTS.*\-inetd_compat" \
241         "s/\-inetd_compat//g" \
242         "$DEFAULT" && need_restart=1 || true
243
244     # No INETD_COMPAT line?
245     if ! egrep -q '^[[:space:]]*INETD_COMPAT' "$DEFAULT"; then
246         echo 'INETD_COMPAT=No' | cp-update -m "$PKG" "$DEFAULT"
247         cp_echo "CN: Added 'INETD_COMPAT=No' to $DEFAULT."
248         need_restart=1
249     fi
250
251     # Just in case there is no active XINETD_OPTS line
252     if ! egrep -q '^[[:space:]]*XINETD_OPTS' "$DEFAULT"; then
253         echo 'XINETD_OPTS="-stayalive"' | cp-update -m "$PKG" "$DEFAULT"
254         cp_echo "CN: Added -stayalive option to $DEFAULT."
255         need_restart=1
256     fi
257 else
258     # File wasn't there at all?
259     touch "$DEFAULT"
260     cp-update "$PKG" "$DEFAULT" <<EOF
261 INETD_COMPAT=No
262 XINETD_OPTS="-stayalive"
263 EOF
264     cp_echo "CN: Created $DEFAULT file with -stayalive enabled and -inetd_compat disabled."
265     need_restart=1
266 fi
267
268 # Restart xinetd if needed
269 #
270 if [ $need_restart -eq 1 ]; then
271     if [ -x "/etc/init.d/xinetd" ]; then
272         if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
273             invoke-rc.d xinetd restart || exit $?
274         else
275             /etc/init.d/xinetd restart || exit $?
276         fi
277     fi
278 fi
279
280 # Mail root
281 #
282 cp_mail "$PKG"
283
284 # dh_installdeb will replace this with shell code automatically
285 # generated by other debhelper scripts.
286
287 #DEBHELPER#
288
289 exit 0