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

init: fix mnt() for loram (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Dec 25 20:07:48 2017 +0100 (9 months ago)
parents 354d476130a4
children
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 [ "$1" != 'netapplet' ] && 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: %s' "$(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 ps aux 2>/dev/null | grep -q [X]org || return
62 [ -n "$DISPLAY" ] || return
63 which notify-send >/dev/null || return
65 icon=$(echo $1 | awk -vw=$WIFI '{printf("notification-network-%s%s\n",
66 w=="yes"?"wireless":"wired", /start/?"":"-disconnected");}')
68 # FIXME: this valid only for lxde-session
69 local user="$(ps aux 2>/dev/null | grep [l]xde-session | awk 'END{print $2}')"
70 local rpid=''
71 [ -s "$npid" ] && rpid="-r $(cat $npid)"
72 su -c "notify-send $rpid -p -i $icon 'Network' \"$2\"" - $user | tr -c 0-9 '\n' | tail -n1 > $npid
73 }
76 # Change LXPanel Network applet interface
78 ch_netapplet() {
79 for user in $(awk -F: '$6 ~ "/home/" {print $1}' /etc/passwd); do
80 # need to be executed as user, due to different XDG variables
81 su -l -c "$0 netapplet" - "$user"
82 done
83 # restart if LXPanel running
84 if [ -n "$DISPLAY" -a -n "$(which lxpanelctl)" ]; then
85 lxpanelctl restart
86 fi
87 }
90 # Use ethernet
92 eth() {
93 if [ "$WIFI" != 'yes' ]; then
94 notification start "$(_ 'Starting Ethernet interface %s...' "$INTERFACE")"
95 ifconfig $INTERFACE up
96 sleep 5
97 fi
98 }
101 # Start wpa_supplicant with prepared settings in wpa.conf
103 start_wpa_supplicant() {
104 echo "Starting wpa_supplicant for $1..."
105 wpa_supplicant -B -W -c$WPA_CONF -D$WIFI_WPA_DRIVER -i$WIFI_INTERFACE
106 }
109 # Reconnect to the given network
111 reconnect_wifi_network() {
112 if [ "$WIFI" == 'yes' ]; then
113 # Wpa_supplicant will auto-connect to the first network
114 # notwithstanding to priority when scan_ssid=1
115 current_ssid="$(wpa_cli list_networks 2>/dev/null | fgrep '[CURRENT]' | cut -f2)"
116 if [ "$current_ssid" != "$WIFI_ESSID" ]; then
117 notification start "$(_ 'Connecting to %s...' "$WIFI_ESSID")"
118 action 'Connecting to %s...' "$WIFI_ESSID"
119 for i in $(seq 5); do
120 index=$(wpa_cli list_networks 2>/dev/null | \
121 grep -m1 -F $'\t'$WIFI_ESSID$'\t' | head -n1 | cut -f1)
122 [ -z "$index" ] && echo -n '.' && sleep 1
123 done
124 wpa_cli select_network $index 2>/dev/null; status
125 fi
126 fi
127 }
130 # Remove selected network settings from wpa.conf
132 remove_network() {
133 mv -f $WPA_CONF $WPA_CONF.old
134 cat $WPA_CONF.old | tr '\n' '\a' | sed 's|[^#]\(network={\)|\n\1|g' | \
135 fgrep -v "ssid=\"$1\"" | tr '\a' '\n' > $WPA_CONF
136 }
139 # For Wi-Fi. Users just have to enable it through WIFI="yes" and usually
140 # ESSID="any" will work and the interface is autodetected.
142 wifi() {
143 if [ "$WIFI" == 'yes' ]; then
144 ifconfig $INTERFACE down
146 # Confirm if $WIFI_INTERFACE is the Wi-Fi interface
147 if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
148 echo "$WIFI_INTERFACE is not a Wi-Fi interface, changing it."
149 WIFI_INTERFACE=$(iwconfig 2>/dev/null | awk 'NR==1{print $1}')
150 [ -n "$WIFI_INTERFACE" ] && sed -i \
151 "s|^WIFI_INTERFACE=.*|WIFI_INTERFACE=\"$WIFI_INTERFACE\"|" \
152 /etc/network.conf
153 fi
155 notification start "$(_ 'Starting Wi-Fi interface %s...' "$WIFI_INTERFACE")"
156 action 'Configuring Wi-Fi interface %s...' "$WIFI_INTERFACE"
157 ifconfig $WIFI_INTERFACE up 2>/dev/null
158 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
159 iwconfig $WIFI_INTERFACE txpower on
160 fi
161 status
163 IWCONFIG_ARGS=''
164 [ -n "$WIFI_WPA_DRIVER" ] || WIFI_WPA_DRIVER='wext'
165 [ -n "$WIFI_MODE" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS mode $WIFI_MODE"
166 [ -n "$WIFI_CHANNEL" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS channel $WIFI_CHANNEL"
167 [ -n "$WIFI_AP" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS ap $WIFI_AP"
169 # Use "any" network only when it is needed
170 [ "$WIFI_ESSID" != 'any' ] && remove_network 'any'
172 # Clean all / add / change stored networks settings
173 if [ "$WIFI_BLANK_NETWORKS" == 'yes' ]; then
174 echo "Creating new $WPA_CONF"
175 cat /etc/wpa/wpa_empty.conf > $WPA_CONF
176 else
177 if fgrep -q "ssid=\"$WIFI_ESSID\"" $WPA_CONF; then
178 echo "Change network settings in $WPA_CONF"
179 # Remove given existing network (it's to be appended later)
180 remove_network "$WIFI_ESSID"
181 else
182 echo "Append existing $WPA_CONF"
183 fi
184 fi
186 # Each new network has a higher priority than the existing
187 MAX_PRIORITY=$(sed -n 's|[\t ]*priority=\([0-9]*\)|\1|p' $WPA_CONF | sort -g | tail -n1)
188 PRIORITY=$(( ${MAX_PRIORITY:-0} + 1 ))
190 # Begin network description
191 cat >> $WPA_CONF <<EOT
192 network={
193 ssid="$WIFI_ESSID"
194 EOT
196 # For networks with hidden SSID: write its BSSID
197 [ -n "$WIFI_BSSID" ] && cat >> $WPA_CONF <<EOT
198 bssid=$WIFI_BSSID
199 EOT
200 # Allow probe requests (for all networks)
201 cat >> $WPA_CONF <<EOT
202 scan_ssid=1
203 EOT
205 case x$(echo -n $WIFI_KEY_TYPE | tr a-z A-Z) in
206 x|xNONE) # Open network
207 cat >> $WPA_CONF <<EOT
208 key_mgmt=NONE
209 priority=$PRIORITY
210 }
211 EOT
212 # start_wpa_supplicant NONE
213 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
214 ;;
216 xWEP) # WEP security
217 # Encryption key length: 64 bit (5 ASCII or 10 HEX)
218 # Encryption key length: 128 bit (13 ASCII or 26 HEX)
219 # ASCII key in "quotes", HEX key without quotes
220 case "${#WIFI_KEY}" in
221 10|26) Q='' ;;
222 *) Q='"' ;;
223 esac
224 cat >> $WPA_CONF <<EOT
225 key_mgmt=NONE
226 auth_alg=OPEN SHARED
227 wep_key0=$Q$WIFI_KEY$Q
228 priority=$PRIORITY
229 }
230 EOT
231 start_wpa_supplicant WEP ;;
233 xWPA) # WPA/WPA2-PSK security
234 cat >> $WPA_CONF <<EOT
235 psk="$WIFI_KEY"
236 key_mgmt=WPA-PSK
237 priority=$PRIORITY
238 }
239 EOT
240 start_wpa_supplicant WPA/WPA2-PSK ;;
242 xEAP) # 802.1x EAP security
243 {
244 cat <<EOT
245 key_mgmt=WPA-EAP IEEE8021X
246 eap=$WIFI_EAP_METHOD
247 EOT
248 if [ "$WIFI_EAP_METHOD" == 'PWD' ]; then
249 WIFI_PHASE2=''; WIFI_CA_CERT=''; WIFI_USER_CERT=''; WIFI_ANONYMOUS_IDENTITY=''
250 fi
251 [ -n "$WIFI_CA_CERT" ] && echo -e "\tca_cert=\"$WIFI_CA_CERT\""
252 [ -n "$WIFI_CLIENT_CERT" ] && echo -e "\tclient_cert=\"$WIFI_CLIENT_CERT\""
253 [ -n "$WIFI_IDENTITY" ] && echo -e "\tidentity=\"$WIFI_IDENTITY\""
254 [ -n "$WIFI_ANONYMOUS_IDENTITY" ] && echo -e "\tanonymous_identity=\"$WIFI_ANONYMOUS_IDENTITY\""
255 [ -n "$WIFI_KEY" ] && echo -e "\tpassword=\"$WIFI-KEY\""
256 [ -n "$WIFI_PHASE2" ] && echo -e "\tphase2=\"auth=$WIFI_PHASE2\""
257 echo }
258 } >> $WPA_CONF
259 start_wpa_supplicant '802.1x EAP' ;;
261 xANY)
262 cat >> $WPA_CONF <<EOT
263 key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
264 group=CCMP TKIP WEP104 WEP40
265 pairwise=CCMP TKIP
266 psk="$WIFI_KEY"
267 password="$WIFI_KEY"
268 priority=$PRIORITY
269 }
270 EOT
271 start_wpa_supplicant 'any key type' ;;
273 esac
274 INTERFACE=$WIFI_INTERFACE
275 fi
276 }
279 # WPA DHCP script
281 wpa() {
282 wpa_cli -a"/etc/init.d/wpa_action.sh" -B
283 }
286 # For a dynamic IP with DHCP
288 dhcp() {
289 if [ "$DHCP" == 'yes' ]; then
290 echo "Starting udhcpc client on: $INTERFACE..."
291 # Is wpa wireless && wpa_ctrl_open interface up?
292 if [ -d /var/run/wpa_supplicant ] && [ "$WIFI" == 'yes' ]; then
293 wpa
294 else
295 # fallback on udhcpc: wep, eth
296 /sbin/udhcpc -b -T 1 -A 12 -i $INTERFACE -p \
297 /var/run/udhcpc.$INTERFACE.pid
298 fi
299 fi
300 }
303 # For a static IP
305 static_ip() {
306 if [ "$STATIC" == 'yes' ]; then
307 echo "Configuring static IP on $INTERFACE: $IP..."
308 if [ -n "$BROADCAST" ]; then
309 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK broadcast $BROADCAST up
310 else
311 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK up
312 fi
314 # Use ip to set gateways if iproute.conf exists
315 if [ -f /etc/iproute.conf ]; then
316 while read line; do
317 ip route add $line
318 done < /etc/iproute.conf
319 else
320 /sbin/route add default gateway $GATEWAY
321 fi
323 # wpa_supplicant waits for wpa_cli
324 [ -d /var/run/wpa_supplicant ] && wpa_cli -a"/etc/init.d/wpa_cli_static.sh" -B
326 # Multi-DNS server in $DNS_SERVER
327 /bin/mv /etc/resolv.conf /tmp/resolv.conf.$$
328 {
329 printf 'nameserver %s\n' $DNS_SERVER # Multiple allowed
330 [ -n "$DOMAIN" ] && echo "search $DOMAIN"
331 } >> /etc/resolv.conf
332 for HELPER in /etc/ipup.d/*; do
333 [ -x $HELPER ] && $HELPER $INTERFACE $DNS_SERVER
334 done
335 fi
336 }
339 # Stopping everything
341 stop() {
342 ch_netapplet
343 notification stop "$(_ 'Stopping all interfaces')"
344 echo 'Stopping all interfaces'
345 for iface in $(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d'); do
346 ifconfig $iface down
347 done
348 ifconfig $WIFI_INTERFACE down
350 echo 'Killing all daemons'
351 killall udhcpc
352 killall wpa_supplicant 2>/dev/null
354 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
355 echo 'Shutting down Wi-Fi card'
356 iwconfig $WIFI_INTERFACE txpower off
357 fi
358 }
361 start() {
362 ch_netapplet
363 # stopping only unspecified interfaces
364 interfaces="$(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d')"
365 case $WIFI in
366 # don't stop Wi-Fi Interface if Wi-Fi selected
367 yes) interfaces="$(echo "$interfaces" | sed -e "/^$WIFI_INTERFACE$/d")";;
368 esac
369 for iface in $interfaces; do
370 ifconfig $iface down
371 done
373 eth; wifi
374 dhcp; static_ip
375 reconnect_wifi_network
377 # change default LXPanel panel iface
378 if [ -f /etc/lxpanel/default/panels/panel ]; then
379 sed -i "s/iface=.*/iface=$INTERFACE/" /etc/lxpanel/default/panels/panel
380 fi
381 }
384 # Looking for arguments:
386 case "$1" in
387 '')
388 boot; start ;;
389 start)
390 start ;;
391 stop)
392 stop ;;
393 restart)
394 stop; sleep 2; start ;;
395 *)
396 cat <<EOT
398 $(boldify 'Usage:') /etc/init.d/$(basename $0) [start|stop|restart]
400 Default configuration file is $(boldify '/etc/network.conf')
401 You can specify another configuration file in the second argument:
402 /etc/init.d/$(basename $0) [start|stop|restart] file.conf
404 EOT
405 ;;
406 esac
408 [ -f "$npid" ] && rm "$npid"