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

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