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
23 # Cleanup all temp files.
28 if [ -n "$temp_files" ]; then
29 for item in $temp_files; do
30 if [ -e "$item" ]; then
37 # backup_and_disable_service()
39 # Backup and disable service with invalid configuration.
40 # Arguments: service, services_file
42 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 cp_backup_conffile -r -d $BACKUPDIR -p $INETDCONF; then
71 cp_echo "CN: Old $INETDCONF saved as $BACKUPDIR/`basename $INETDCONF`.bak."
73 if cp_backup_conffile -r -d $BACKUPDIR -p $CONF; then
74 cp_echo "CN: Old $CONF saved as $BACKUPDIR/`basename $CONF`.bak."
77 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
80 if [ -f "$INETDCONF" ]; then
82 # Convert inetd.conf to temporary xinetd.conf file using xconv.pl tool
83 egrep -v "^#<off>#" $INETDCONF | /usr/sbin/xconv.pl > $CONFTMP || true
86 # Parse /etc/xinetd.conf file and convert services' configuration to
87 # separated configurations in /etc/xinetd.d/ directory
89 conffile_list="$CONFTMP"
90 if [ -f "$CONF" ]; then
91 if egrep -q "^[[:space:]]*service[[:space:]]+" "$CONF"; then
92 conffile_list="$CONF $conffile_list"
96 services_list="`sed -nr 's/^[[:space:]]*service[[:space:]]+//p' $conffile_list | uniq`"
98 if [ -n "$services_list" ]; then
100 for service in $services_list; do
102 if [ -f "/etc/xinetd.d/$service" ]; then
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."
108 rm -f /etc/xinetd.d/$service
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"
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."
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."
132 # Configuration file /etc/xinetd.conf does not exist?
134 if [ ! -f "$CONF" ]; then
138 # Remove services from /etc/xinetd.conf file
140 cp_check_and_sed "^[[:space:]]*service[[:space:]]+" \
141 "/^[[:space:]]*service[[:space:]]/,/^}/d" \
142 "$CONF" && need_restart=1 || true
144 # Check if there is no defaults block in /etc/xinetd.conf
146 if ! egrep -q '^defaults' "$CONF"; then
148 echo -e "defaults\n{\n\n}" | cp-update -m "$PKG" "$CONF"
149 cp_echo "CN: Added defaults block to $CONF"
154 # Check if /etc/xinetd.d/ directory is included or not
156 if ! egrep -q '^includedir /etc/xinetd.d' "$CONF"; then
158 echo "includedir /etc/xinetd.d" | cp-update -m "$PKG" "$CONF"
159 cp_echo "CN: Added \"includedir /etc/xinetd.d\" line to $CONF"
164 # Validate services' configuration.
166 services_file_list="`ls -1 /etc/xinetd.d/`"
167 if [ -n "$services_file_list" ]; then
169 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`"
174 if [ -n "$services_list" ]; then
176 for service in $services_list; do
179 service_block="`sed -rn "/^[[:space:]]*service[[:space:]]+$service[[:space:]]*$/,/^}/p" /etc/xinetd.d/$services_file`"
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
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."
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
199 for service_group_x in $service_group; 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."
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" ]; 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
235 # File exists, set INETD_COMPAT to 'No'
236 cp_check_and_sed "^[[:space:]]*INETD_COMPAT.*[Yy][Ee][Ss]" \
237 "s/^[[:space:]]*INETD_COMPAT.*[Yy][Ee][Ss]/INETD_COMPAT=No/g" \
238 "$DEFAULT" && need_restart=1 || true
240 # File exists, remove -inetd_compat option
241 cp_check_and_sed "^[[:space:]]*XINETD_OPTS.*\-inetd_compat" \
242 "s/\-inetd_compat//g" \
243 "$DEFAULT" && need_restart=1 || true
245 # No INETD_COMPAT line?
246 if ! egrep -q '^[[:space:]]*INETD_COMPAT' "$DEFAULT"; then
247 echo 'INETD_COMPAT=No' | cp-update -m "$PKG" "$DEFAULT"
248 cp_echo "CN: Added 'INETD_COMPAT=No' to $DEFAULT."
252 # Just in case there is no active XINETD_OPTS line
253 if ! egrep -q '^[[:space:]]*XINETD_OPTS' "$DEFAULT"; then
254 echo 'XINETD_OPTS="-stayalive"' | cp-update -m "$PKG" "$DEFAULT"
255 cp_echo "CN: Added -stayalive option to $DEFAULT."
259 # File wasn't there at all?
261 echo -e 'INETD_COMPAT=No\nXINETD_OPTS="-stayalive"' | cp-update "$PKG" "$DEFAULT"
262 cp_echo "CN: Created $DEFAULT file with -stayalive enabled and -inetd_compat disabled."
267 # Restart xinetd if needed
269 if [ $need_restart -eq 1 ]; then
272 pgrep -u root -f /usr/sbin/xinetd > /dev/null || do=start
274 if [ -n "$do" ]; then
277 if [ -x "/etc/init.d/xinetd" ]; then
278 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
279 invoke-rc.d xinetd "$do" || exit $?
281 /etc/init.d/xinetd "$do" || exit $?