Debian Codegraph @

debian/ppp-udeb.postinst

raw
1 #!/bin/sh
2 set -e
3
4 . /usr/share/debconf/confmodule
5
6 INTERFACES=$(grep : /proc/net/dev | cut -d':' -f1 | sort | sed -e 's/ *//' | grep -E '^(en|eth|nas|tap|nic-)')
7 db_get ppp/interface
8 PPPOE="$RET"
9 db_capb backup
10
11 log() {
12 logger -t DEBUG:ppp-udeb $*
13 }
14
15 fail() {
16 logger -t FAIL:ppp-udeb $*
17 }
18
19 ppp_concentrator_on() {
20 local R P
21 db_subst ppp/detect_progress IFACE "${1}" || true
22 if [ -z "$(ip link show $1 up)" ]; then
23 log "$1 was not configured. ppp-udeb will bring it up"
24 PPPUP=yes
25 ip link set $1 up
26 else
27 log "$1 was already up"
28 PPPUP=no
29 fi
30 R=0
31 db_progress START 0 2 ppp/detect_progress
32 for P in "" "-U" ; do
33 if pppoe-discovery -Q -t 1 -a 3 $P -I $1 2> /dev/null; then
34 R=1
35 break
36 fi
37 db_progress STEP 1
38 done
39 db_progress STOP
40 if [ "$R" = 1 ]; then
41 return 0
42 else
43 return 1
44 fi
45 }
46
47 reset_if_needed() {
48 # Bring down an earlier pppoe connection, if there is one
49 if [ -e $PIDF ]
50 then
51 PID=$(cat $PIDF)
52 log "found PID file $PIDF which refers to process $PID; searching for the pppd process"
53 if [ -z "$(ps | grep "^\s*$PID" | sed "s/^\s*$PID\s.*$/$PID/")" ]
54 then
55 log "$PID not found; removing pid file"
56 else
57 log "$PID found; killing it and removing pid file"
58 kill $PID || true
59 fi
60 rm -f $PIDF
61 fi
62
63 # Bring down previously raised interface
64 if [ "$PPPOE" != "_" ]; then
65 ip link set "$PPPOE" down
66 db_set ppp/interface "_"
67 fi
68 }
69
70 valid_hostname() {
71 if [ $(echo -n "$1" | wc -c) -lt 2 ] ||
72 [ $(echo -n "$1" | wc -c) -gt 63 ] ||
73 [ "$(echo -n "$1" | sed 's/[^-\.[:alnum:]]//g')" != "$1" ] ||
74 [ "$(echo -n "$1" | grep "\(^-\|-$\)")" ]; then
75 return 1
76 fi
77 return 0
78 }
79
80 # Sanity check: we rely on some netcfg functionality but cannot depend on it;
81 # netcfg should always be present, but bail out if it is not
82 if [ ! -e /bin/netcfg ]; then
83 fail "required package netcfg is not installed"
84 exit 1
85 fi
86
87 # Bring up the loopback interface
88 if [ -z "$(ip link show lo up)" ]; then
89 ip link set lo up
90 ip addr flush dev lo
91 ip addr add 127.0.0.1/8 dev lo
92 fi
93
94 if [ -z "$INTERFACES" ]; then
95 fail "no Ethernet interfaces detected"
96 db_input critical ppp/detect_failed || true
97 db_go || true
98 exit 1
99 fi
100
101 PIDF=/var/run/ppp-udeb.pid
102 reset_if_needed
103
104 # Test each of the interfaces for a concentrator; stop when one is found
105 for IFACE in $INTERFACES; do
106 if ppp_concentrator_on $IFACE; then
107 log "setting pppoe connection on $IFACE"
108 db_set ppp/interface $IFACE
109 break
110 else
111 if [ "$PPPUP" = yes ]; then
112 log "no concentrator found on $IFACE; will bring the interface back down"
113 ip link set $IFACE down
114 else
115 log "ppp-udeb no concentrator found on $IFACE"
116 fi
117 IFACE=''
118 fi
119 done
120
121 if [ -z "$IFACE" ]; then
122 fail "no concentrators detected"
123 db_input critical ppp/no_concentrators || true
124 db_go || true
125 exit 1
126 fi
127
128 db_input high ppp/username || true
129 db_go || exit 10
130 db_get ppp/username
131 USERNAME="$RET"
132
133 db_input high ppp/password || true
134 db_go || exit 10
135 db_get ppp/password
136 PASSWORD="$RET"
137
138 # Clear answers in case the script is run a second time
139 db_unregister ppp/password
140 db_unregister ppp/username
141
142
143 # Ask for the hostname and domainname to use for the system
144 # (using the netcfg templates!)
145 while true; do
146 db_input high netcfg/get_hostname || [ $? -eq 30 ]
147 db_input high netcfg/get_domain || [ $? -eq 30 ]
148 db_go || exit 10
149 db_get netcfg/get_hostname
150 HOSTNAME="$RET"
151 if valid_hostname "$HOSTNAME"; then
152 db_get netcfg/get_domain
153 DOMAINNAME="$RET"
154 break
155 else
156 db_input high netcfg/invalid_hostname || true
157 db_fset netcfg/get_hostname seen false
158 fi
159 done
160
161
162 # FIXME: lo snippet should not be ppp-udeb's job
163 cat > /etc/network/interfaces <<EOF
164 # This file describes the network interfaces available on your system
165 # and how to activate them. For more information, see interfaces(5).
166
167 source /etc/network/interfaces.d/*
168
169 # The loopback network interface
170 auto lo
171 iface lo inet loopback
172
173 # This configuration stanza prevents ifupdown-aware network daemons
174 # like NetworkManager from managing the interface which is used for
175 # the PPPoE connection
176 iface $IFACE inet manual
177
178 # PPPoE connection
179 auto provider
180 iface provider inet ppp
181 pre-up /bin/ip link set $IFACE up
182 provider provider
183 EOF
184
185 # Set hostname and create a basic /etc/hosts file
186 echo "127.0.0.1 localhost" > /etc/hosts
187
188 if [ "$HOSTNAME" ]; then
189 echo "$HOSTNAME" >/etc/hostname
190
191 if [ "$DOMAINNAME" ]; then
192 printf "127.0.1.1\t$HOSTNAME.$DOMAINNAME\t$HOSTNAME" >> /etc/hosts
193 else
194 printf "127.0.1.1\t$HOSTNAME" >> /etc/hosts
195 fi
196 fi
197
198 cat >> /etc/hosts <<EOF
199
200 # The following lines are desirable for IPv6 capable hosts
201 ::1 ip6-localhost ip6-loopback
202 fe00::0 ip6-localnet
203 ff00::0 ip6-mcastprefix
204 ff02::1 ip6-allnodes
205 ff02::2 ip6-allrouters
206 EOF
207
208
209 cat <<EOF > /etc/ppp/peers/provider
210 # kernel space PPPoE driver configuration
211 #
212 # See the manual page pppd(8) for information on all the options.
213
214 # MUST CHANGE: Uncomment the following line, replacing the [email protected]
215 # by the DSL user name given to your by your DSL provider.
216 # There should be a matching entry with the password in /etc/ppp/pap-secrets
217 # and/or /etc/ppp/chap-secrets.
218 #user "[email protected]"
219 user "$USERNAME"
220
221 #linkname provider
222 #debug
223
224 # Load the pppoe plugin. Change the ethernet interface name if needed.
225 plugin pppoe.so
226 $IFACE
227
228 # Assumes that your IP address is allocated dynamically by the ISP.
229 noipdefault
230 # Try to get the name server addresses from the ISP.
231 usepeerdns
232 # Use this connection as the default route.
233 # Comment out if you already have the correct default route installed.
234 defaultroute
235
236 # Make sure that sensitive data does not get into the logs
237 hide-password
238
239 # Peer should be alive
240 lcp-echo-interval 20
241 lcp-echo-failure 3
242
243 # Makes pppd "dial again" when the connection is lost.
244 persist
245
246 # Do not ask the remote to authenticate.
247 noauth
248
249 # RFC 2516, paragraph 7 mandates that the following options MUST NOT be
250 # requested and MUST be rejected if requested by the peer:
251 # Address-and-Control-Field-Compression (ACFC)
252 noaccomp
253 # Asynchronous-Control-Character-Map (ACCM)
254 default-asyncmap
255
256 # Do not try to negotiate other kinds of compression.
257 nopcomp
258 noccp
259 novj
260 EOF
261
262 : > /etc/ppp/pap-secrets
263 chmod 600 /etc/ppp/pap-secrets
264 cat <<EOF > /etc/ppp/pap-secrets
265 #GENERATED-BY-DEBIAN-INSTALLER#
266 "$USERNAME" * "$PASSWORD"
267 EOF
268 cp /etc/ppp/pap-secrets /etc/ppp/chap-secrets
269
270 log-output -t depmod
271 log-output -t ppp-udeb modprobe pppoe
272
273 log-output -t ppp-udeb pppd call provider
274 sleep 1 # allow forking
275 PID=`ps | grep 'call provider' | grep pppd | sed 's:\s*\([0-9]*\).*$:\1:g'`
276 echo $PID > $PIDF
277
278 STEP=0
279 db_progress START 0 31 ppp/please_wait
280 log "Waiting for message from pppd[$PID]"
281 while [ "$STEP" -lt 30 ]
282 do
283 ANSWER=$(grep -E "pppd\[$PID\]:.(PAP|CHAP)\ authentication\ (failed|succeeded)" /var/log/syslog | tail -n 1 | sed -e 's:^.*\(failed\).*$:\1:g' -e 's:^.*\(succeeded\).*$:\1:g')
284 if [ "$ANSWER" = "succeeded" ] || [ "$ANSWER" = "failed" ]
285 then
286 STEP=30
287 log "got result: authentication $ANSWER"
288 else
289 log "Answer unknown"
290 fi
291 sleep 1
292 STEP=$(expr $STEP + 1)
293 db_progress STEP 1
294 done
295 db_progress STOP
296
297 if [ "$ANSWER" = "failed" ]; then
298 fail "authentication failed"
299 (rm -f $PIDF && kill $PID) || true 2> /dev/null
300 db_input critical ppp/wrong_login || true
301 db_go || true
302 exit 1
303 elif [ -z "$ANSWER" ] && [ "$STEP" -ge 30 ]; then
304 fail "unhandled error detected"
305 (rm -f $PIDF && kill $PID) || true 2>/dev/null
306 db_input critical ppp/unhandled || true
307 db_go || true
308 exit 1
309 else
310 # Handle a fallback DNS
311 if ! [ -s /etc/resolv.conf ] && db_get netcfg/get_nameservers; then
312 for nameserver in $RET; do
313 echo "nameserver $nameserver" >>/etc/resolv.conf
314 done
315 fi
316 fi
317
318 exit 0