slitaz-boot-scripts view etc/init.d/network.sh @ rev 430

init: add serial live console (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat May 21 17:20:19 2016 +0200 (2016-05-21)
parents 90c9aecd15b4
children 2a9569d6dbc7
line source
1 #!/bin/sh
2 #
3 # /etc/init.d/network.sh : Network initialization boot script
4 # /etc/network.conf : Main SliTaz network configuration file
5 # /etc/wpa/wpa.conf : Wi-Fi networks configuration file
8 . /etc/init.d/rc.functions
10 CONF="${2:-/etc/network.conf}"
11 echo "Loading network settings from $CONF"
12 . "$CONF"
15 # Change LXPanel Network applet settings
17 if [ "$1" == 'netapplet' ]; then
18 if [ "$WIFI" == 'yes' ]; then
19 interface="$WIFI_INTERFACE"
20 else
21 interface="$INTERFACE"
22 fi
24 for i in $(find ${XDG_CONFIG_HOME:-$HOME/.config}/lxpanel -name panel 2> /dev/null); do
25 fgrep -q netstatus "$i" || continue
26 sed -i '/iface/s|=.*$|='$interface'|' "$i"
27 done
28 exit 0
29 fi
31 WPA_CONF='/etc/wpa/wpa.conf'
32 [ ! -e "$WPA_CONF" ] && cp /etc/wpa/wpa_empty.conf $WPA_CONF 2>/dev/null
33 npid='/tmp/notify_pid'
35 # Migrate existing settings to a new format file
37 . /usr/share/slitaz/network.conf_migration
40 # Actions executing on boot time (running network.sh without parameters)
42 boot() {
43 # Set hostname
44 action "Setting hostname to: $(cat /etc/hostname)"
45 /bin/hostname -F /etc/hostname
46 status
48 # Configure loopback interface
49 action 'Configuring loopback...'
50 /sbin/ifconfig lo 127.0.0.1 up
51 /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
52 status
54 [ -s /etc/sysctl.conf ] && sysctl -p /etc/sysctl.conf
55 }
58 # Freedesktop notification
60 notification() {
61 # FIXME: this valid only for lxde-session
62 local user="$(ps aux | grep [l]xde-session | awk 'END{print $2}')"
63 local icon="$1" rpid=''
64 [ -s "$npid" ] && rpid="-r $(cat $npid)"
65 which notify-send > /dev/null &&
66 su -c "notify-send $rpid -p -i $icon 'Network' \"$2\"" - $user | tail -n1 > $npid
67 }
70 # Change LXPanel Network applet interface
72 ch_netapplet() {
73 for user in $(awk -F: '$6 ~ "/home/" {print $1}' /etc/passwd); do
74 # need to be executed as user, due to different XDG variables
75 su -l -c "$0 netapplet" - "$user"
76 done
77 # restart if LXPanel running
78 which lxpanelctl > /dev/null && lxpanelctl restart
79 }
82 # Use ethernet
84 eth() {
85 if [ "$WIFI" != 'yes' ]; then
86 notification network-wired "$(_ 'Starting Ethernet interface %s...' "$INTERFACE")"
87 ifconfig $INTERFACE up
88 sleep 5
89 fi
90 }
93 # Start wpa_supplicant with prepared settings in wpa.conf
95 start_wpa_supplicant() {
96 echo "Starting wpa_supplicant for $1..."
97 wpa_supplicant -B -W -c$WPA_CONF -D$WIFI_WPA_DRIVER -i$WIFI_INTERFACE
98 }
101 # Reconnect to the given network
103 reconnect_wifi_network() {
104 if [ "$WIFI" == 'yes' ]; then
105 # Wpa_supplicant will auto-connect to the first network
106 # notwithstanding to priority when scan_ssid=1
107 current_ssid="$(wpa_cli list_networks 2>/dev/null | fgrep '[CURRENT]' | cut -f2)"
108 if [ "$current_ssid" != "$WIFI_ESSID" ]; then
109 notification network-wireless "$(_ 'Connecting to %s...' "$WIFI_ESSID")"
110 action 'Connecting to $WIFI_ESSID...'
111 for i in $(seq 5); do
112 index=$(wpa_cli list_networks 2>/dev/null | \
113 grep -m1 -F $'\t'$WIFI_ESSID$'\t' | head -n1 | cut -f1)
114 [ -z "$index" ] && echo -n '.' && sleep 1
115 done
116 wpa_cli select_network $index >/dev/null; status
117 fi
118 fi
119 }
122 # Remove selected network settings from wpa.conf
124 remove_network() {
125 mv -f $WPA_CONF $WPA_CONF.old
126 cat $WPA_CONF.old | tr '\n' '\a' | sed 's|[^#]\(network={\)|\n\1|g' | \
127 fgrep -v "ssid=\"$1\"" | tr '\a' '\n' > $WPA_CONF
128 }
131 # For Wi-Fi. Users just have to enable it through WIFI="yes" and usually
132 # ESSID="any" will work and the interface is autodetected.
134 wifi() {
135 if [ "$WIFI" == 'yes' ]; then
136 ifconfig $INTERFACE down
138 # Confirm if $WIFI_INTERFACE is the Wi-Fi interface
139 if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
140 echo "$WIFI_INTERFACE is not a Wi-Fi interface, changing it."
141 WIFI_INTERFACE=$(iwconfig 2>/dev/null | awk 'NR==1{print $1}')
142 [ -n "$WIFI_INTERFACE" ] && sed -i \
143 "s|^WIFI_INTERFACE=.*|WIFI_INTERFACE=\"$WIFI_INTERFACE\"|" \
144 /etc/network.conf
145 fi
147 notification network-wireless "$(_ 'Starting Wi-Fi interface %s...' "$WIFI_INTERFACE")"
148 action 'Configuring Wi-Fi interface $WIFI_INTERFACE...'
149 ifconfig $WIFI_INTERFACE up 2>/dev/null
150 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
151 iwconfig $WIFI_INTERFACE txpower on
152 fi
153 status
155 IWCONFIG_ARGS=''
156 [ -n "$WIFI_WPA_DRIVER" ] || WIFI_WPA_DRIVER='wext'
157 [ -n "$WIFI_MODE" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS mode $WIFI_MODE"
158 [ -n "$WIFI_CHANNEL" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS channel $WIFI_CHANNEL"
159 [ -n "$WIFI_AP" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS ap $WIFI_AP"
161 # Use "any" network only when it is needed
162 [ "$WIFI_ESSID" != 'any' ] && remove_network 'any'
164 # Clean all / add / change stored networks settings
165 if [ "$WIFI_BLANK_NETWORKS" == 'yes' ]; then
166 echo "Creating new $WPA_CONF"
167 cat /etc/wpa/wpa_empty.conf > $WPA_CONF
168 else
169 if fgrep -q ssid=\"$WIFI_ESSID\" $WPA_CONF; then
170 echo "Change network settings in $WPA_CONF"
171 # Remove given existing network (it's to be appended later)
172 remove_network "$WIFI_ESSID"
173 else
174 echo "Append existing $WPA_CONF"
175 fi
176 fi
178 # Each new network has a higher priority than the existing
179 MAX_PRIORITY=$(sed -n 's|[\t ]*priority=\([0-9]*\)|\1|p' $WPA_CONF | sort -g | tail -n1)
180 PRIORITY=$(( ${MAX_PRIORITY:-0} + 1 ))
182 # Begin network description
183 cat >> $WPA_CONF <<EOT
184 network={
185 ssid="$WIFI_ESSID"
186 EOT
188 # For networks with hidden SSID: write its BSSID
189 [ -n "$WIFI_BSSID" ] && cat >> $WPA_CONF <<EOT
190 bssid=$WIFI_BSSID
191 EOT
192 # Allow probe requests (for all networks)
193 cat >> $WPA_CONF <<EOT
194 scan_ssid=1
195 EOT
197 case x$(echo -n $WIFI_KEY_TYPE | tr a-z A-Z) in
198 x|xNONE) # Open network
199 cat >> $WPA_CONF <<EOT
200 key_mgmt=NONE
201 priority=$PRIORITY
202 }
203 EOT
204 # start_wpa_supplicant NONE
205 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
206 ;;
208 xWEP) # WEP security
209 # Encryption key length: 64 bit (5 ASCII or 10 HEX)
210 # Encryption key length: 128 bit (13 ASCII or 26 HEX)
211 # ASCII key in "quotes", HEX key without quotes
212 case "${#WIFI_KEY}" in
213 10|26) Q='' ;;
214 *) Q='"' ;;
215 esac
216 cat >> $WPA_CONF <<EOT
217 key_mgmt=NONE
218 auth_alg=OPEN SHARED
219 wep_key0=$Q$WIFI_KEY$Q
220 priority=$PRIORITY
221 }
222 EOT
223 start_wpa_supplicant WEP ;;
225 xWPA) # WPA/WPA2-PSK security
226 cat >> $WPA_CONF <<EOT
227 psk="$WIFI_KEY"
228 key_mgmt=WPA-PSK
229 priority=$PRIORITY
230 }
231 EOT
232 start_wpa_supplicant WPA/WPA2-PSK ;;
234 xEAP) # 802.1x EAP security
235 {
236 cat <<EOT
237 key_mgmt=WPA-EAP IEEE8021X
238 eap=$WIFI_EAP_METHOD
239 EOT
240 if [ "$WIFI_EAP_METHOD" == 'PWD' ]; then
241 WIFI_PHASE2=''; WIFI_CA_CERT=''; WIFI_USER_CERT=''; WIFI_ANONYMOUS_IDENTITY=''
242 fi
243 [ -n "$WIFI_CA_CERT" ] && echo -e "\tca_cert=\"$WIFI_CA_CERT\""
244 [ -n "$WIFI_CLIENT_CERT" ] && echo -e "\tclient_cert=\"$WIFI_CLIENT_CERT\""
245 [ -n "$WIFI_IDENTITY" ] && echo -e "\tidentity=\"$WIFI_IDENTITY\""
246 [ -n "$WIFI_ANONYMOUS_IDENTITY" ] && echo -e "\tanonymous_identity=\"$WIFI_ANONYMOUS_IDENTITY\""
247 [ -n "$WIFI_KEY" ] && echo -e "\tpassword=\"$WIFI-KEY\""
248 [ -n "$WIFI_PHASE2" ] && echo -e "\tphase2=\"auth=$WIFI_PHASE2\""
249 echo }
250 } >> $WPA_CONF
251 start_wpa_supplicant '802.1x EAP' ;;
253 xANY)
254 cat >> $WPA_CONF <<EOT
255 key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
256 group=CCMP TKIP WEP104 WEP40
257 pairwise=CCMP TKIP
258 psk="$WIFI_KEY"
259 password="$WIFI_KEY"
260 priority=$PRIORITY
261 }
262 EOT
263 start_wpa_supplicant 'any key type' ;;
265 esac
266 INTERFACE=$WIFI_INTERFACE
267 fi
268 }
271 # WPA DHCP script
273 wpa() {
274 wpa_cli -a"/etc/init.d/wpa_action.sh" -B
275 }
278 # For a dynamic IP with DHCP
280 dhcp() {
281 if [ "$DHCP" == 'yes' ]; then
282 echo "Starting udhcpc client on: $INTERFACE..."
283 # Is wpa wireless && wpa_ctrl_open interface up?
284 if [ -d /var/run/wpa_supplicant ] && [ "$WIFI" == 'yes' ]; then
285 wpa
286 else
287 # fallback on udhcpc: wep, eth
288 /sbin/udhcpc -b -T 1 -A 12 -i $INTERFACE -p \
289 /var/run/udhcpc.$INTERFACE.pid
290 fi
291 fi
292 }
295 # For a static IP
297 static_ip() {
298 if [ "$STATIC" == 'yes' ]; then
299 echo "Configuring static IP on $INTERFACE: $IP..."
300 if [ -n "$BROADCAST" ]; then
301 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK broadcast $BROADCAST up
302 else
303 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK up
304 fi
306 # Use ip to set gateways if iproute.conf exists
307 if [ -f /etc/iproute.conf ]; then
308 while read line; do
309 ip route add $line
310 done < /etc/iproute.conf
311 else
312 /sbin/route add default gateway $GATEWAY
313 fi
315 # wpa_supplicant waits for wpa_cli
316 [ -d /var/run/wpa_supplicant ] && wpa_cli -B
318 # Multi-DNS server in $DNS_SERVER
319 /bin/mv /etc/resolv.conf /tmp/resolv.conf.$$
320 {
321 printf 'nameserver %s\n' $DNS_SERVER # Multiple allowed
322 [ -n "$DOMAIN" ] && echo "search $DOMAIN"
323 } >> /etc/resolv.conf
324 for HELPER in /etc/ipup.d/*; do
325 [ -x $HELPER ] && $HELPER $INTERFACE $DNS_SERVER
326 done
327 fi
328 }
331 # Stopping everything
333 stop() {
334 ch_netapplet
335 notification network-offline "$(_ 'Stopping all interfaces')"
336 echo 'Stopping all interfaces'
337 for iface in $(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d'); do
338 ifconfig $iface down
339 done
340 ifconfig $WIFI_INTERFACE down
342 echo 'Killing all daemons'
343 killall udhcpc
344 killall wpa_supplicant 2>/dev/null
346 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
347 echo 'Shutting down Wi-Fi card'
348 iwconfig $WIFI_INTERFACE txpower off
349 fi
350 }
353 start() {
354 ch_netapplet
355 # stopping only unspecified interfaces
356 interfaces="$(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d')"
357 case $WIFI in
358 # don't stop Wi-Fi Interface if Wi-Fi selected
359 yes) interfaces="$(echo "$interfaces" | sed -e "/^$WIFI_INTERFACE$/d")";;
360 esac
361 for iface in $interfaces; do
362 ifconfig $iface down
363 done
365 eth; wifi
366 dhcp; static_ip
367 reconnect_wifi_network
369 # change default LXPanel panel iface
370 if [ -f /etc/lxpanel/default/panels/panel ]; then
371 sed -i "s/iface=.*/iface=$INTERFACE/" /etc/lxpanel/default/panels/panel
372 fi
373 }
376 # Looking for arguments:
378 case "$1" in
379 '')
380 boot; start ;;
381 start)
382 start ;;
383 stop)
384 stop ;;
385 restart)
386 stop; sleep 2; start ;;
387 *)
388 cat <<EOT
390 $(boldify 'Usage:') /etc/init.d/$(basename $0) [start|stop|restart]
392 Default configuration file is $(boldify '/etc/network.conf')
393 You can specify another configuration file in the second argument:
394 /etc/init.d/$(basename $0) [start|stop|restart] file.conf
396 EOT
397 ;;
398 esac
400 [ -f "$npid" ] && rm "$npid"