5 [ "$1" = "configure" ] || exit 0
6 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
9 . /usr/share/carnet-tools/functions.sh
13 INETDCONF=/etc/inetd.conf
14 DEFAULT=/etc/default/xinetd
15 BACKUPDIR=/var/backups/xinetd-cn
16 XCONV_TOOL=/usr/sbin/xconv.pl
24 # Cleanup all temp files.
29 if [ -n "$temp_files" ]; then
30 for item in $temp_files; do
31 if [ -e "$item" ]; then
38 # backup_and_disable_service()
40 # Backup and disable service with invalid configuration.
41 # Arguments: service, services_file
43 backup_and_disable_service () {
44 local serv servfile out
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."
52 cp_echo "CN: Disabling service '$serv' in configuration file /etc/xinetd.d/$servfile."
54 out=`mktemp /etc/xinetd.d/$servfile.tmp.XXXXXX`
55 temp_files="$temp_files $out"
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"
64 # Set trap for deleting all temp files.
68 # First, we do backup (inetd, xinetd)
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."
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."
81 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
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
89 # Parse /etc/xinetd.conf file and convert services' configuration to
90 # separated configurations in /etc/xinetd.d/ directory
92 conffile_list="$CONFTMP"
93 if [ -f "$CONF" ]; then
94 if egrep -q "^[[:space:]]*service[[:space:]]+" "$CONF"; then
95 conffile_list="$CONF $conffile_list"
99 services_list="`sed -nr 's/^[[:space:]]*service[[:space:]]+//p' $conffile_list | uniq`"
101 if [ -n "$services_list" ]; then
103 for service in $services_list; do
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."
109 rm -f /etc/xinetd.d/$service
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"
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."
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."
133 # Configuration file /etc/xinetd.conf does not exist?
135 if [ ! -f "$CONF" ]; then
140 # Remove services from /etc/xinetd.conf file
142 cp_check_and_sed "^[[:space:]]*service[[:space:]]+" \
143 "/^[[:space:]]*service[[:space:]]/,/^}/d" \
144 "$CONF" && need_restart=1 || true
146 # Check if there is no defaults block in /etc/xinetd.conf
148 if ! egrep -q '^defaults' "$CONF"; then
149 cp_echo "CN: Adding defaults block to $CONF"
150 CONF_CONTENT="defaults
156 # Check if /etc/xinetd.d/ directory is included or not
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"
164 if [ -n "$CONF_CONTENT" ]; then
165 echo "$CONF_CONTENT" | cp-update -m "$PKG" "$CONF"
169 # Validate services' configuration.
171 services_file_list="`ls -1 /etc/xinetd.d/`"
172 if [ -n "$services_file_list" ]; then
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`"
178 if [ -n "$services_list" ]; then
180 for service in $services_list; do
182 service_block="`sed -rn "/^[[:space:]]*service[[:space:]]+$service[[:space:]]*$/,/^}/p" /etc/xinetd.d/$services_file`"
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."
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."
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."
222 if [ $service_disable -eq 1 ]; then
223 backup_and_disable_service "$service" "$services_file"
231 # Remove -inetd_compat option and set INETD_COMPAT to 'No' in /etc/default/xinetd
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
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
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."
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."
258 # File wasn't there at all?
260 cp-update "$PKG" "$DEFAULT" <<EOF
262 XINETD_OPTS="-stayalive"
264 cp_echo "CN: Created $DEFAULT file with -stayalive enabled and -inetd_compat disabled."
268 # Restart xinetd if needed
270 if [ $need_restart -eq 1 ]; then
271 service xinetd restart || exit $?