Magellan Linux

Contents of /trunk/magellan-initscripts/etc/rc.d/init.d/network

Parent Directory Parent Directory | Revision Log Revision Log


Revision 270 - (show annotations) (download)
Tue Oct 11 11:19:58 2005 UTC (18 years, 6 months ago) by niro
File size: 7826 byte(s)
fixed wpa logic

1 #!/bin/bash
2 # $Header: /home/cvsd/magellan-cvs/magellan-src/magellan-initscripts/etc/rc.d/init.d/network,v 1.10 2005-10-11 11:19:58 niro Exp $
3
4 #%rlevels: 0:k 1:k 2:k 3:s 4:s 5:s 6:k
5 #%start: 20
6 #%stop: 80
7
8 #deps
9 #%needs:
10 #%before:
11 #%after:
12
13 source /etc/sysconfig/rc
14 source ${rc_functions}
15
16 # read values from files
17 read_value()
18 {
19 local var="$1"
20 local file="$2"
21 local value
22
23 # local all possible vars
24 # global
25 local ONBOOT
26 local NETWORKING
27
28 # static
29 local IP
30 local NETMASK
31 local BROADCAST
32 local NETWORKING
33 local FORCE_MAC_TO
34
35 # dhcp
36 local DHCP_PROG
37 local DHCP_START
38 local DHCP_STOP
39
40 # default gw
41 local GATEWAY
42 local GATEWAY_IF
43
44 # wireless extensions
45 local WIRELESS_AP
46 local WIRELESS_AUTH_MODE
47 local WIRELESS_BITRATE
48 local WIRELESS_CHANNEL
49 local WIRELESS_DEFAULT_KEY
50 local WIRELESS_ESSID
51 local WIRELESS_FREQUENCY
52 local WIRELESS_KEY
53 local WIRELESS_KEY_ASCII
54 local WIRELESS_KEY_0
55 local WIRELESS_KEY_1
56 local WIRELESS_KEY_2
57 local WIRELESS_KEY_3
58 local WIRELESS_KEY_LENGTH
59 local WIRELESS_MODE
60 local WIRELESS_NICK
61 local WIRELESS_NWID
62 local WIRELESS_POWER
63
64 source ${file}
65 eval value=\$$(echo ${var})
66 echo "${value}"
67 }
68
69 checkconfig()
70 {
71 if [ -z "${NETWORKING}" ]
72 then
73 echo "NETWORKING missing in net.${interface}, aborted"
74 exit 1
75 fi
76
77 case "${NETWORKING}" in
78 static)
79 if [ -z "${IP}" ]
80 then
81 echo "IP missing in net.${interface}, aborted"
82 exit 1
83 fi
84
85 if [ -z "${NETMASK}" ]
86 then
87 echo -n "NETMASK missing in net.${interface}, "
88 echo "using 255.255.255.0"
89 NETMASK=255.255.255.0
90 fi
91
92 if [ -z "${BROADCAST}" ]
93 then
94 echo -n "BROADCAST missing in net.${interface}, "
95 echo "using default address"
96 fi
97 ;;
98
99 dhcp)
100 if [ -z "${DHCP_PROG}" ]
101 then
102 echo "DHCP_PROG missing in net.${interface}, aborted"
103 exit 1
104 fi
105 ;;
106
107 esac
108 }
109
110 # onboot_interface_list /path/to/files*
111 onboot_interface_list()
112 {
113 local file
114 local devices
115 local iface
116
117 # get list of all devices
118 for file in $@
119 do
120 if [[ $(read_value ONBOOT ${file}) = yes ]]
121 then
122 iface="$(basename ${file} | sed s/net.//)"
123 # exclude backup files
124 case "${iface}" in
125 *~) ;;
126 *) devices="${devices} $(basename ${file} | sed s/net.//)" ;;
127 esac
128 fi
129 done
130
131 echo "${devices}"
132 }
133
134 config_wireless_wep()
135 {
136 local iface="$1"
137
138 if [[ -z ${iface} ]]
139 then
140 echo "WEP: no \$iface given. Aborting setup."
141 return 1
142 fi
143
144 ${CURS_UP}
145 ${SET_WWCOL}
146 echo "[AUTH: WEP]"
147
148 iwconfig "${iface}" enc on
149 [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}"
150 [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}"
151 [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}"
152 }
153
154 config_wireless_wpa()
155 {
156 local iface="$1"
157
158 if [[ -z ${iface} ]]
159 then
160 echo "WPA: no \$iface given. Aborting setup."
161 return 1
162 fi
163
164 if [ ! -x /sbin/wpa_supplicant ]
165 then
166 echo "WPA: wpa_supplicant not installed. Aborting setup."
167 return 1
168 fi
169
170 ${CURS_UP}
171 ${SET_WWCOL}
172 echo "[AUTH: WPA]"
173
174 # get default settings
175 [[ -f /etc/conf.d/wpa_supplicant ]] && source /etc/conf.d/wpa_supplicant
176
177 # check the configuration
178 [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant.auto
179 [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel
180 if [[ -z ${WIRELESS_WPA_DRIVER} ]]
181 then
182 echo "WPA: WIRELESS_WPA_DRIVER given. Aborting setup."
183 return 1
184 fi
185
186 # write a config with the settings from net.${iface}
187 # only wpa-psk ! all other needs manual setup
188 if [[ ${WIRELESS_WPA_AUTOCONF} = true ]]
189 then
190 # write default cfg from skeleton
191 cat ${WIRELESS_WPA_SKEL} > ${WIRELESS_WPA_CONFIG}
192
193 # setup the network entry
194 sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
195 -e "s:@WIRELESS_KEY@:${WIRELESS_KEY}:g" \
196 ${WIRELESS_WPA_CONFIG}
197 fi
198
199 # now run the wpa_supplicant dameon
200 wpa_supplicant -B \
201 -D"${WIRELESS_WPA_DRIVER}" \
202 -c"${WIRELESS_WPA_CONFIG}" \
203 -i"${iface}" \
204 ${WIRELESS_WPA_OPTS}
205
206 # echo wait 5 seconds
207 echo " Waiting 5 seconds to retrieve authentification reply ... "
208 sleep 5
209 }
210
211 setup_wireless_extensions()
212 {
213 local iface="$1"
214
215 if [[ -z ${iface} ]]
216 then
217 echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup."
218 return 1
219 fi
220
221 echo -e ${COLOREDSTAR}"Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... "
222
223 [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}"
224 [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}"
225 [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}"
226 [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}"
227 [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}"
228 [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}"
229
230 case "${WIRELESS_AUTH_MODE}" in
231 wpa) config_wireless_wpa "${iface}" ;;
232 wep|on) config_wireless_wep "${iface}" ;;
233 off) iwconfig "${iface}" enc off ;;
234 esac
235 }
236
237 networking_start()
238 {
239 local iface dns
240
241 # get list of all devices
242 for iface in $(onboot_interface_list ${network_settings}/net.*)
243 do
244 # checkconfig
245 source ${network_settings}/net.${iface} || exit 1
246 checkconfig
247
248 # setup mac
249 if [ -n "${FORCE_MAC_TO}" ]
250 then
251 echo -e ${COLOREDSTAR}"Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "
252 ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
253 evaluate_retval
254 fi
255
256 # activate the interface
257 ifconfig "${iface}" up
258
259 # now configure wireless_extensions
260 [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
261
262 echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
263
264 # setup static or dhcp
265 case ${NETWORKING} in
266 dhcp|DHCP)
267 ${CURS_UP}
268 ${SET_WWCOL}
269 echo "[DHCP]"
270 loadproc ${DHCP_PROG} ${DHCP_START}
271 ;;
272 static|STATIC)
273 ${CURS_UP}
274 ${SET_WWCOL}
275 echo "[STATIC]"
276 ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}"
277 evaluate_retval
278 ;;
279 esac
280
281 # setup def gw
282 if [[ -n ${GATEWAY} ]]
283 then
284 echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
285 route add default gateway ${GATEWAY} metric 1 dev ${iface}
286 evaluate_retval
287 fi
288
289 # setup /etc/resolv.conf
290 if [[ -n ${NAMESERVER} ]]
291 then
292 echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
293
294 # whipe out the old one
295 echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
296 for dns in ${NAMESERVER}
297 do
298 echo "nameserver ${dns}" >> /etc/resolv.conf
299 done
300 fi
301 done
302 }
303
304 networking_stop()
305 {
306 # get list of all devices
307 for iface in $(onboot_interface_list ${network_settings}/net.*)
308 do
309 source ${network_settings}/net.${iface} || exit 1
310 checkconfig
311
312 if [[ -n ${GATEWAY} ]]
313 then
314 echo -e ${COLOREDSTAR}"Removing default gateway ..."
315 route del -net default
316 evaluate_retval
317 fi
318
319 echo -e ${COLOREDSTAR}"Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..."
320 ifconfig ${iface} down
321 evaluate_retval
322
323 # shutdown dhcp-daemon
324 if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]
325 then
326 echo -e ${COLOREDSTAR}"Stopping the dhcp-daemon ..."
327 ${CURS_UP}
328 ${SET_WWCOL}
329 echo "[$(basename ${DHCP_PROG})]"
330 ${DHCP_PROG} ${DHCP_STOP} "${iface}"
331 evaluate_retval
332 fi
333
334 # shutdown wpa_supplicant daemon
335 if [[ -n $(pidof wpa_supplicant) ]]
336 then
337 killall wpa_supplicant
338 fi
339 done
340
341 # remove state dir
342 if [ -d /var/run/wpa_supplicant ]
343 then
344 rm -rf /var/run/wpa_supplicant
345 fi
346 }
347
348 case $1 in
349 start)
350 networking_start
351 update_svcstatus $1
352 splash svc_started "$(basename $0)" 0
353 ;;
354
355 stop)
356 networking_stop
357 update_svcstatus $1
358 splash svc_stopped "$(basename $0)" 0
359 ;;
360
361 restart)
362 $0 stop
363 sleep 1
364 $0 start
365 ;;
366
367 *)
368 echo "Usage: $0 {start|stop|restart}"
369 exit 1
370 ;;
371 esac

Properties

Name Value
svn:executable *