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