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 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
81 # Convert inetd.conf to temporary xinetd.conf file using xconv.pl tool
82 egrep -v "^#<off>#" $INETDCONF | $XCONV_TOOL > $CONFTMP || true
85 # Parse /etc/xinetd.conf file and convert services' configuration to
86 # separated configurations in /etc/xinetd.d/ directory
88 conffile_list="$CONFTMP"
89 if [ -f "$CONF" ]; then
90 if egrep -q "^[[:space:]]*service[[:space:]]+" "$CONF"; then
91 conffile_list="$CONF $conffile_list"
95 services_list="`sed -nr 's/^[[:space:]]*service[[:space:]]+//p' $conffile_list | uniq`"
97 if [ -n "$services_list" ]; then
99 for service in $services_list; do
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."
105 rm -f /etc/xinetd.d/$service
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"
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."
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."
129 # Configuration file /etc/xinetd.conf does not exist?
131 if [ ! -f "$CONF" ]; then
136 # Remove services from /etc/xinetd.conf file
138 cp_check_and_sed "^[[:space:]]*service[[:space:]]+" \
139 "/^[[:space:]]*service[[:space:]]/,/^}/d" \
140 "$CONF" && need_restart=1 || true
142 # Check if there is no defaults block in /etc/xinetd.conf
144 if ! egrep -q '^defaults' "$CONF"; then
145 cp_echo "CN: Adding defaults block to $CONF"
146 CONF_CONTENT="defaults
152 # Check if /etc/xinetd.d/ directory is included or not
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"
160 if [ -n "$CONF_CONTENT" ]; then
161 echo "$CONF_CONTENT" | cp-update -m "$PKG" "$CONF"
165 # Validate services' configuration.
167 services_file_list="`ls -1 /etc/xinetd.d/`"
168 if [ -n "$services_file_list" ]; then
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`"
174 if [ -n "$services_list" ]; then
176 for service in $services_list; do
178 service_block="`sed -rn "/^[[:space:]]*service[[:space:]]+$service[[:space:]]*$/,/^}/p" /etc/xinetd.d/$services_file`"
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."
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."
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."
218 if [ $service_disable -eq 1 ]; then
219 backup_and_disable_service "$service" "$services_file"
227 # Remove -inetd_compat option and set INETD_COMPAT to 'No' in /etc/default/xinetd
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
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
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."
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."
254 # File wasn't there at all?
256 cp-update "$PKG" "$DEFAULT" <<EOF
258 XINETD_OPTS="-stayalive"
260 cp_echo "CN: Created $DEFAULT file with -stayalive enabled and -inetd_compat disabled."
264 # Restart xinetd if needed
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 $?
271 /etc/init.d/xinetd restart || exit $?
280 # dh_installdeb will replace this with shell code automatically
281 # generated by other debhelper scripts.