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