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

network.sh: missing } in wpa.conf
author Lucas Levrel <llevrel@yahoo.fr>
date Wed Jan 06 21:13:16 2016 +0100 (2016-01-06)
parents 29ec74a6d359
children d477099ba12c
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
7 . /etc/init.d/rc.functions
9 CONF="${2:-/etc/network.conf}"
10 echo "Loading network settings from $CONF"
11 . "$CONF"
13 WPA_CONF='/etc/wpa/wpa.conf'
14 [ ! -e "$WPA_CONF" ] && cp /etc/wpa/wpa_empty.conf $WPA_CONF 2> /dev/null
16 # Migrate existing settings to a new format file
18 . /usr/share/slitaz/network.conf_migration
21 # Actions executing on boot time (running network.sh without parameters)
23 boot() {
24 # Set hostname
25 action "Setting hostname to: $(cat /etc/hostname)"
26 /bin/hostname -F /etc/hostname
27 status
29 # Configure loopback interface
30 action 'Configuring loopback...'
31 /sbin/ifconfig lo 127.0.0.1 up
32 /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
33 status
35 [ -s /etc/sysctl.conf ] && sysctl -p /etc/sysctl.conf
36 }
39 # Use ethernet
41 eth() {
42 [ "$WIFI" != 'yes' ] && ifconfig $INTERFACE up && sleep 5
43 }
46 # Start wpa_supplicant with prepared settings in wpa.conf
48 start_wpa_supplicant() {
49 echo "Starting wpa_supplicant for $1..."
50 wpa_supplicant -B -W -c$WPA_CONF -D$WIFI_WPA_DRIVER -i$WIFI_INTERFACE
51 }
54 # Reconnect to the given network
56 reconnect_wifi_network() {
57 if [ "$WIFI" == 'yes' ]; then
58 # Wpa_supplicant will auto-connect to the first network
59 # notwithstanding to priority when scan_ssid=1
60 current_ssid="$(wpa_cli list_networks 2>/dev/null | fgrep '[CURRENT]' | cut -f2)"
61 if [ "$current_ssid" != "$WIFI_ESSID" ]; then
62 action 'Connecting to $WIFI_ESSID...'
63 for i in $(seq 5); do
64 index=$(wpa_cli list_networks 2>/dev/null | \
65 grep -m1 -F $'\t'$WIFI_ESSID$'\t' | head -n1 | cut -f1)
66 [ -z "$index" ] && echo -n '.' && sleep 1
67 done
68 wpa_cli select_network $index >/dev/null; status
69 fi
70 fi
71 }
74 # Remove selected network settings from wpa.conf
76 remove_network() {
77 mv -f $WPA_CONF $WPA_CONF.old
78 cat $WPA_CONF.old | tr '\n' '\a' | sed 's|[^#]\(network={\)|\n\1|g' | \
79 fgrep -v "ssid=\"$1\"" | tr '\a' '\n' > $WPA_CONF
80 }
83 # For Wi-Fi. Users just have to enable it through WIFI="yes" and usually
84 # ESSID="any" will work and the interface is autodetected.
86 wifi() {
87 if [ "$WIFI" == 'yes' ]; then
88 ifconfig $INTERFACE down
90 # Confirm if $WIFI_INTERFACE is the Wi-Fi interface
91 if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
92 echo "$WIFI_INTERFACE is not a Wi-Fi interface, changing it."
93 WIFI_INTERFACE=$(iwconfig 2>/dev/null | awk 'NR==1{print $1}')
94 [ -n "$WIFI_INTERFACE" ] && sed -i \
95 "s|^WIFI_INTERFACE=.*|WIFI_INTERFACE=\"$WIFI_INTERFACE\"|" \
96 /etc/network.conf
97 fi
99 action 'Configuring Wi-Fi interface $WIFI_INTERFACE...'
100 ifconfig $WIFI_INTERFACE up 2>/dev/null
101 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
102 iwconfig $WIFI_INTERFACE txpower on
103 fi
104 status
106 IWCONFIG_ARGS=''
107 [ -n "$WIFI_WPA_DRIVER" ] || WIFI_WPA_DRIVER='wext'
108 [ -n "$WIFI_MODE" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS mode $WIFI_MODE"
109 [ -n "$WIFI_CHANNEL" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS channel $WIFI_CHANNEL"
110 [ -n "$WIFI_AP" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS ap $WIFI_AP"
112 # Use "any" network only when it is needed
113 [ "$WIFI_ESSID" != 'any' ] && remove_network 'any'
115 # Clean all / add / change stored networks settings
116 if [ "$WIFI_BLANK_NETWORKS" == 'yes' ]; then
117 echo "Creating new $WPA_CONF"
118 cat /etc/wpa/wpa_empty.conf > $WPA_CONF
119 else
120 if fgrep -q ssid=\"$WIFI_ESSID\" $WPA_CONF; then
121 echo "Change network settings in $WPA_CONF"
122 # Remove given existing network (it's to be appended later)
123 remove_network "$WIFI_ESSID"
124 else
125 echo "Append existing $WPA_CONF"
126 fi
127 fi
129 # Each new network has a higher priority than the existing
130 MAX_PRIORITY=$(sed -n 's|[\t ]*priority=\([0-9]*\)|\1|p' $WPA_CONF | sort -g | tail -n1)
131 PRIORITY=$(( ${MAX_PRIORITY:-0} + 1 ))
133 # Begin network description
134 cat >> $WPA_CONF <<EOT
135 network={
136 ssid="$WIFI_ESSID"
137 EOT
139 # For networks with hidden SSID: write its BSSID
140 [ -n "$WIFI_BSSID" ] && cat >> $WPA_CONF <<EOT
141 bssid=$WIFI_BSSID
142 EOT
143 # Allow probe requests (for all networks)
144 cat >> $WPA_CONF <<EOT
145 scan_ssid=1
146 EOT
148 case x$(echo -n $WIFI_KEY_TYPE | tr a-z A-Z) in
149 x|xNONE) # Open network
150 cat >> $WPA_CONF <<EOT
151 key_mgmt=NONE
152 priority=$PRIORITY
153 }
154 EOT
155 # start_wpa_supplicant NONE
156 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
157 ;;
159 xWEP) # WEP security
160 # Encryption key length: 64 bit (5 ASCII or 10 HEX)
161 # Encryption key length: 128 bit (13 ASCII or 26 HEX)
162 # ASCII key in "quotes", HEX key without quotes
163 case "${#WIFI_KEY}" in
164 10|26) Q='' ;;
165 *) Q='"' ;;
166 esac
167 cat >> $WPA_CONF <<EOT
168 key_mgmt=NONE
169 auth_alg=OPEN SHARED
170 wep_key0=$Q$WIFI_KEY$Q
171 priority=$PRIORITY
172 }
173 EOT
174 start_wpa_supplicant WEP ;;
176 xWPA) # WPA/WPA2-PSK security
177 cat >> $WPA_CONF <<EOT
178 psk="$WIFI_KEY"
179 key_mgmt=WPA-PSK
180 priority=$PRIORITY
181 }
182 EOT
183 start_wpa_supplicant WPA/WPA2-PSK ;;
185 xEAP) # 802.1x EAP security
186 {
187 cat <<EOT
188 key_mgmt=WPA-EAP IEEE8021X
189 eap=$WIFI_EAP_METHOD
190 EOT
191 if [ "$WIFI_EAP_METHOD" == 'PWD' ]; then
192 WIFI_PHASE2=''; WIFI_CA_CERT=''; WIFI_USER_CERT=''; WIFI_ANONYMOUS_IDENTITY=''
193 fi
194 [ -n "$WIFI_CA_CERT" ] && echo -e "\tca_cert=\"$WIFI_CA_CERT\""
195 [ -n "$WIFI_CLIENT_CERT" ] && echo -e "\tclient_cert=\"$WIFI_CLIENT_CERT\""
196 [ -n "$WIFI_IDENTITY" ] && echo -e "\tidentity=\"$WIFI_IDENTITY\""
197 [ -n "$WIFI_ANONYMOUS_IDENTITY" ] && echo -e "\tanonymous_identity=\"$WIFI_ANONYMOUS_IDENTITY\""
198 [ -n "$WIFI_KEY" ] && echo -e "\tpassword=\"$WIFI-KEY\""
199 [ -n "$WIFI_PHASE2" ] && echo -e "\tphase2=\"auth=$WIFI_PHASE2\""
200 echo }
201 } >> $WPA_CONF
202 start_wpa_supplicant '802.1x EAP' ;;
204 xANY)
205 cat >> $WPA_CONF <<EOT
206 key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
207 group=CCMP TKIP WEP104 WEP40
208 pairwise=CCMP TKIP
209 psk="$WIFI_KEY"
210 password="$WIFI_KEY"
211 priority=$PRIORITY
212 }
213 EOT
214 start_wpa_supplicant 'any key type' ;;
216 esac
217 INTERFACE=$WIFI_INTERFACE
218 fi
219 }
222 # WPA DHCP script
224 wpa() {
225 wpa_cli -a"/etc/init.d/wpa_action.sh" -B
226 }
229 # For a dynamic IP with DHCP
231 dhcp() {
232 if [ "$DHCP" == 'yes' ]; then
233 echo "Starting udhcpc client on: $INTERFACE..."
234 # Is wpa wireless && wpa_ctrl_open interface up?
235 if [ -d /var/run/wpa_supplicant ] && [ "$WIFI" == 'yes' ]; then
236 wpa
237 else
238 # fallback on udhcpc: wep, eth
239 /sbin/udhcpc -b -T 1 -A 12 -i $INTERFACE -p \
240 /var/run/udhcpc.$INTERFACE.pid
241 fi
242 fi
243 }
246 # For a static IP
248 static_ip() {
249 if [ "$STATIC" == 'yes' ]; then
250 echo "Configuring static IP on $INTERFACE: $IP..."
251 if [ -n "$BROADCAST" ]; then
252 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK broadcast $BROADCAST up
253 else
254 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK up
255 fi
257 # Use ip to set gateways if iproute.conf exists
258 if [ -f /etc/iproute.conf ]; then
259 while read line; do
260 ip route add $line
261 done < /etc/iproute.conf
262 else
263 /sbin/route add default gateway $GATEWAY
264 fi
266 # wpa_supplicant waits for wpa_cli
267 [ -d /var/run/wpa_supplicant ] && wpa_cli -B
269 # Multi-DNS server in $DNS_SERVER
270 /bin/mv /etc/resolv.conf /tmp/resolv.conf.$$
271 {
272 printf 'nameserver %s\n' $DNS_SERVER # Multiple allowed
273 [ -n "$DOMAIN" ] && echo "search $DOMAIN"
274 } >> /etc/resolv.conf
275 for HELPER in /etc/ipup.d/*; do
276 [ -x $HELPER ] && $HELPER $INTERFACE $DNS_SERVER
277 done
278 fi
279 }
282 # Stopping everything
284 stop() {
285 echo 'Stopping all interfaces'
286 for iface in $(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d'); do
287 ifconfig $iface down
288 done
289 ifconfig $WIFI_INTERFACE down
291 echo 'Killing all daemons'
292 killall udhcpc
293 killall wpa_supplicant 2>/dev/null
295 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
296 echo 'Shutting down Wi-Fi card'
297 iwconfig $WIFI_INTERFACE txpower off
298 fi
299 }
302 start() {
303 # stopping only unspecified interfaces
304 interfaces="$(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d')"
305 case $WIFI in
306 # don't stop Wi-Fi Interface if Wi-Fi selected
307 yes) interfaces="$(echo "$interfaces" | sed -e "/^$WIFI_INTERFACE$/d")";;
308 esac
309 for iface in $interfaces; do
310 ifconfig $iface down
311 done
313 eth; wifi
314 dhcp; static_ip
315 reconnect_wifi_network
317 # change default LXPanel panel iface
318 if [ -f /etc/lxpanel/default/panels/panel ]; then
319 sed -i "s/iface=.*/iface=$INTERFACE/" /etc/lxpanel/default/panels/panel
320 fi
321 }
324 # Looking for arguments:
326 case "$1" in
327 '')
328 boot; start ;;
329 start)
330 start ;;
331 stop)
332 stop ;;
333 restart)
334 stop; sleep 2; start ;;
335 *)
336 cat <<EOT
338 $(boldify 'Usage:') /etc/init.d/$(basename $0) [start|stop|restart]
340 Default configuration file is $(boldify '/etc/network.conf')
341 You can specify another configuration file in the second argument:
342 /etc/init.d/$(basename $0) [start|stop|restart] file.conf
344 EOT
345 ;;
346 esac