20e4b9db92ff46fdc003bdfa3feb37ca8cfc074a
[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
16 need_restart=0
17 temp_files=
18
19 # cleanup()
20 #
21 #   Cleanup all temp files.
22 #
23 cleanup () {
24
25         local item
26         if [ -n "$temp_files" ]; then
27                 for item in $temp_files; do
28                         if [ -e "$item" ]; then
29                                 rm -f $item
30                         fi
31                 done
32         fi
33 }
34
35
36 # Set trap for deleting all temp files.
37 #
38 trap cleanup 0 1 2 15
39
40 # First, we do backup (inetd, xinetd)
41 #
42 if cp_check_and_backup "$INETDCONF"; then
43         cp_echo "CN: Old $INETDCONF saved as /var/backups/`basename $INETDCONF`.bak."
44 fi
45 if cp_check_and_backup "$CONF"; then
46         cp_echo "CN: Old $CONF saved as /var/backups/`basename $CONF`.bak."
47 fi
48
49 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
50 temp_files="$CONFTMP"
51
52 if [ -f "$INETDCONF" ]; then
53
54         # Convert inetd.conf to temporary xinetd.conf file using xconv.pl tool
55         /usr/sbin/xconv.pl < $INETDCONF > $CONFTMP
56 fi
57
58 # Parse /etc/xinetd.conf file and convert services' configuration to
59 # separated configurations in /etc/xinetd.d/ directory
60 #
61 conffile_list="$CONFTMP"
62 if [ -f "$CONF" ]; then
63         conffile_list="$CONF $conffile_list"
64 fi
65 services_list="`cat $conffile_list | grep "^service " | uniq | sed 's/service //g'`" || true
66
67 if [ -n "$services_list" ]; then
68
69     for service in $services_list; do
70
71         if [ -f "/etc/xinetd.d/$service" ]; then
72
73                 if cp_check_and_backup "/etc/xinetd.d/$service"; then
74                         cp_echo "CN: Old /etc/xinetd.d/$service saved as /var/backups/`basename /etc/xinetd.d/$service`.bak."
75                 fi
76
77                 rm -f /etc/xinetd.d/$service
78         fi
79
80         touch /etc/xinetd.d/$service || true
81         # cat "$CONF" "$CONFTMP" | sed -n "/^service $service/,/^}/p" | cp-update "$PKG" "/etc/xinetd.d/$service"
82         cat "$CONF" "$CONFTMP" | sed -n "/^service $service/,/^}/p" >> "/etc/xinetd.d/$service"
83
84         need_restart=1
85     done
86
87     if egrep -q "service " "$CONFTMP"; then
88         cp_echo "CN: All services were converted from $INETDCONF file to separated"
89         cp_echo "CN: configuration files located in /etc/xinetd.d/ directory."
90     fi
91
92     if [ -f "$CONF" ]; then
93         cp_echo "CN: All services were converted from $CONF file to separated"
94         cp_echo "CN: configuration files located in /etc/xinetd.d/ directory."
95     fi
96 fi
97
98 rm -f $CONFTMP
99
100 # Configuration file /etc/xinetd.conf does not exist?
101 #
102 if [ ! -f "$CONF" ]; then
103         touch "$CONF"
104 fi
105
106 # Remove services from /etc/xinetd.conf file
107 #
108 cp_check_and_sed "^service " \
109         "/^service/,/^}/d" \
110         "$CONF" && need_restart=1 || true
111
112 # Check if there is no defaults block in /etc/xinetd.conf
113 #
114 if ! egrep -q '^defaults' "$CONF"; then
115
116         echo -e "defaults\n{\n\n}" | cp-update -m "$PKG" "$CONF"
117         cp_echo "CN: Added defaults block to $CONF"
118         
119         need_restart=1
120 fi
121
122 # Check if /etc/xinetd.d/ directory is included or not
123 #
124 if ! egrep -q '^includedir /etc/xinetd.d' "$CONF"; then
125
126         echo "includedir /etc/xinetd.d" | cp-update -m "$PKG" "$CONF"
127         cp_echo "CN: Added \"includedir /etc/xinetd.d\" line to $CONF"
128         
129         need_restart=1
130 fi
131
132 # Remove -inetd_compat option and set INETD_COMPAT to 'No' in /etc/default/xinetd
133 #
134 if [ -f "$DEFAULT" ]; then
135         
136         # File exists, set INETD_COMPAT to 'No'
137         cp_check_and_sed "^[[:space:]]*INETD_COMPAT.*[Yy][Ee][Ss]" \
138                 "s/^[[:space:]]*INETD_COMPAT.*[Yy][Ee][Ss]/INETD_COMPAT=No/g" \
139                 "$DEFAULT" && need_restart=1 || true
140
141         # File exists, remove -inetd_compat option
142         cp_check_and_sed "^[[:space:]]*XINETD_OPTS.*\-inetd_compat" \
143                 "s/\-inetd_compat//g" \
144                 "$DEFAULT" && need_restart=1 || true
145
146         # No INETD_COMPAT line?
147         if ! egrep -q '^[[:space:]]*INETD_COMPAT' "$DEFAULT"; then
148                 echo 'INETD_COMPAT=No' | cp-update -m "$PKG" "$DEFAULT"
149                 cp_echo "CN: Added 'INETD_COMPAT=No' to $DEFAULT."
150                 need_restart=1
151         fi
152
153         # Just in case there is no active XINETD_OPTS line
154         if ! egrep -q '^[[:space:]]*XINETD_OPTS' "$DEFAULT"; then
155                 echo 'XINETD_OPTS="-stayalive"' | cp-update -m "$PKG" "$DEFAULT"
156                 cp_echo "CN: Added -stayalive option to $DEFAULT."
157                 need_restart=1
158         fi
159 else
160         # File wasn't there at all?
161         touch $DEFAULT
162         echo -e 'INETD_COMPAT=No\nXINETD_OPTS="-stayalive"' | cp-update "$PKG" "$DEFAULT"
163         cp_echo "CN: Created $DEFAULT file with -stayalive enabled and -inetd_compat disabled."
164
165         need_restart=1
166 fi
167
168 # Restart xinetd if needed
169 #
170 if [ $need_restart -eq 1 ]; then
171     do=restart
172 fi
173 pgrep -u root -f /usr/sbin/xinetd > /dev/null || do=start
174
175 if [ -n "$do" ]; then
176
177         # Restart xinetd.
178         if [ -x "/etc/init.d/xinetd" ]; then
179                 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
180                     invoke-rc.d xinetd "$do" || exit $?
181                 else
182                     /etc/init.d/xinetd "$do" || exit $?
183                 fi
184         fi
185 fi
186
187 # Mail root
188 #
189 cp_mail "$PKG"
190
191 exit 0