Dash:
[t2.git] / package / base / stone / stone_mod_network.sh
blobd04bb2e1e887f9a46c1cf76f6f1a096aecde886b
1 # --- T2-COPYRIGHT-NOTE-BEGIN ---
2 # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
3 #
4 # T2 SDE: package/.../stone/stone_mod_network.sh
5 # Copyright (C) 2004 - 2008 The T2 SDE Project
6 # Copyright (C) 1998 - 2003 ROCK Linux Project
7 #
8 # More information can be found in the files COPYING and README.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; version 2 of the License. A copy of the
13 # GNU General Public License can be found in the file COPYING.
14 # --- T2-COPYRIGHT-NOTE-END ---
16 # [MAIN] 20 network Network Configuration
18 ### DYNAMIC NEW-STYLE CONFIG ###
20 export rocknet_base="/lib/network" # export needed for subshells ...
21 export rocknet_config="/etc/conf/network"
23 edit() {
24 gui_edit "Edit file $1" "$1"
25 exec $STONE network
28 read_section() {
29 local globals=1
30 local readit=0
31 local i=0
33 unset tags
34 unset interfaces
37 [ "$1" = "" ] && readit=1
38 while read netcmd para
40 if [ -n "$netcmd" ]; then
41 netcmd="${netcmd//-/_}"
42 para="$( echo "$para" | sed 's,[\*\?],\\&,g' )"
43 if [ "$netcmd" = "interface" ] ; then
44 prof="$( echo "$para" | sed 's,[(),],_,g' )"
45 [ "$prof" = "$1" ] && readit=1 || readit=0
46 globals=0
47 interfaces="$interfaces $prof"
49 if [ $readit = 1 ] ; then
50 tags[$i]="$netcmd $para"
51 i=$((i+1))
54 done < <( sed 's,\(^\|[ \t]\)#.*$,,' < $rocknet_config )
57 write_tags() {
58 for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
59 local netcmd="${tags[$i]}"
60 [ "$netcmd" ] || continue
61 [ $1 = 0 ] && [[ "$netcmd" != interface* ]] && \
62 echo -en "\t"
63 echo "${tags[$i]}"
64 done
67 write_section() {
68 local globals=1
69 local passit=1
70 local dumped=0
72 [ "$1" = "" ] && passit=0
74 echo -n > $rocknet_config.new
76 while read netcmd para ; do
78 [ "$netcmd" ] || continue
79 netcmd="${netcmd//-/_}"
80 para="$( echo "$para" | sed 's,[\*\?],\\&,g' )"
82 # when we reached the matching section dump the
83 # mew tags ...
84 if [ $passit = 0 -a $dumped = 0 ] ; then
85 write_tags $globals >> $rocknet_config.new
86 dumped=1
89 # if we reached a new interface section maybe change
90 # the state
91 if [ "$netcmd" = "interface" ] ; then
92 prof="$( echo "$para" | sed 's,[(),],_,g' )"
93 [ "$prof" = "$1" ] && passit=0 || passit=1
95 # write out a separating newline
96 echo "" >> $rocknet_config.new
98 globals=0
101 # just pass the line thru?
102 if [ $passit = 1 ] ; then
103 [ $globals = 0 -a "$netcmd" != "interface" ] && \
104 echo -en "\t" >> $rocknet_config.new
105 echo "$netcmd $para" >> $rocknet_config.new
107 done < <( cat $rocknet_config )
109 # if the config file was empty, for an not yet present or last
110 # we had no change to match the existing position - so write them
111 # out now ...
112 [ $globals = 0 ] && echo "" >> $rocknet_config.new
113 [ "$1" ] && globals=0
114 [ $dumped = 0 ] && write_tags $globals >> $rocknet_config.new
116 mv $rocknet_config{.new,}
119 edit_tag() {
120 tag="${tags[$1]}"
121 name="$tag"
122 gui_input "Set new value for tag '$name'" \
123 "$tag" "tag"
125 tags[$1]="$tag"
128 edit_global_tag() {
129 edit_tag $@
130 write_section ""
133 add_tag() {
134 tta="$@"
135 if [ "$tta" = "" ] ; then
136 cmd="gui_menu add_tag 'Add tag of from module'"
138 while read module ; do
139 cmd="$cmd "$module" module='$module'"
140 done < <( cd $rocknet_base/ ; grep public_ * | sed -e \
141 's/\.sh//' -e 's/:.*//' | sort -u )
142 module=""
143 eval $cmd
145 cmd="gui_menu add_tag 'Add tag of type'"
146 while read tag ; do
147 cmd="$cmd "$tag" 'tta=$tag'"
148 done < <( cd $rocknet_base/ ; grep -h public_ $module.sh \
149 | sed -e 's/public_\([^(]*\).*/\1/' | sort )
150 eval "$cmd"
153 if [ "$tta" ] ; then
154 tagno=${#tags[@]}
155 tags[$tagno]="$tta"
156 edit_tag $tagno
160 add_global_tag() {
161 add_tag $@
162 write_section ""
165 edit_if() {
166 read_section "$1"
168 quit=0
169 while
170 cmd="gui_menu if_edit 'Configure interface ${1//_/ }'"
171 for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
172 [ "${tags[$i]}" ] || continue
173 cmd="$cmd '${tags[$i]}' 'edit_tag $i'"
174 done
176 cmd="$cmd '' '' 'Add new tag' 'add_tag'"
177 cmd="$cmd 'Delete this interface/profile' 'del_interface $1 && quit=1'"
179 # tiny hack since gui_menu return 0 or 1 depending on the exec
180 # status of e.g. dialog - and thus a del_interface && false
181 # cannot be used ...
183 eval "$cmd" || quit=1
184 [ $quit = 0 ]
185 do : ; done
186 write_section "$1"
189 add_interface() {
190 if="$1"
192 gui_input "The new interface name (and profile)" \
193 "$if" "if"
194 unset tags
195 tags[0]="interface $if"
197 if gui_yesno "Use DHCP to obtain the configuration?" ; then
198 add_tag "dhcp"
199 else
200 add_tag "ip 192.168.5.1/24"
201 add_tag "gw 192.168.5.1"
202 add_tag "nameserver 192.168.5.1"
205 write_section "$if"
208 del_interface() {
209 unset tags
210 write_section "$1"
213 ### STATIC OLD-STYLE CONFIG ###
215 set_name() {
216 old1="$HOSTNAME" old2="$HOSTNAME.$DOMAINNAME" old3="$DOMAINNAME"
217 if [ $1 = HOSTNAME ] ; then
218 gui_input "Set a new hostname (without domain part)" \
219 "${!1}" "$1"
220 else
221 gui_input "Set a new domainname (without host part)" \
222 "${!1}" "$1"
224 new="$HOSTNAME.$DOMAINNAME $HOSTNAME"
226 echo "$HOSTNAME" > /etc/HOSTNAME ; hostname "$HOSTNAME"
228 #ip="`echo $IPADDR | sed 's,[/ ].*,,'`"
229 #if grep -q "^$ip\\b" /etc/hosts ; then
230 # tmp="`mktemp`"
231 # sed -e "/^$ip\\b/ s,\\b$old2\\b[ ]*,,g" \
232 # -e "/^$ip\\b/ s,\\b$old1\\b[ ]*,,g" \
233 # -e "/^$ip\\b/ s,[ ]\\+,&$new ," < /etc/hosts > $tmp
234 # cat $tmp > /etc/hosts ; rm -f $tmp
235 #else
236 # echo -e "$ip\\t$new" >> /etc/hosts
239 if [ $1 = DOMAINNAME ] ; then
240 tmp="`mktemp`"
241 grep -vx "search $old3" /etc/resolv.conf > $tmp
242 [ -n "$DOMAINNAME" ] && echo "search $DOMAINNAME" >> $tmp
243 cat $tmp > /etc/resolv.conf
244 rm -f $tmp
248 set_dns() {
249 gui_input "Set a new (space seperated) list of DNS Servers" "$DNSSRV" "DNSSRV"
250 DNSSRV="`echo $DNSSRV`" ; [ -z "$DNSSRV" ] && DNSSRV="none"
252 tmp="`mktemp`" ; grep -v '^nameserver\b' /etc/resolv.conf > $tmp
253 for x in $DNSSRV ; do
254 [ "$x" != "none" ] && echo "nameserver $x" >> $tmp
255 done
256 cat $tmp > /etc/resolv.conf
257 rm -f $tmp
260 HOSTNAME="`hostname`"
261 DOMAINNAME="`hostname -d 2> /dev/null`"
263 tmp="`mktemp`"
264 grep '^nameserver ' /etc/resolv.conf | tr '\t' ' ' | tr -s ' ' | \
265 sed 's,^nameserver *\([^ ]*\),DNSSRV="$DNSSRV \1",' > $tmp
266 DNSSRV='' ; . $tmp ; DNSSRV="`echo $DNSSRV`"
267 [ -z "$DNSSRV" ] && DNSSRV="none" ; rm -f $tmp
269 main() {
270 first_run=1
271 while
273 # read global section and interface list ...
274 read_section ""
276 p_interfaces=$(ip -o link |
277 sed -n '/link\/ether/ s/[^:]*: \([^:]*\):.*/\1/p')
279 if [ $first_run = 1 ] ; then
280 first_run=0
281 # check if a section for the interface is already present
282 for x in $p_interfaces ; do
283 if [[ $interfaces != *$x* ]] ; then
284 if gui_yesno "Unconfigured interface $x detected. \
285 Do you want to create an interface section?" ; then
286 add_interface "$x"
289 done
290 read_section ""
293 cmd="gui_menu network 'Network Configuration - Select an item to
294 change the value
296 WARNING: This script tries to adapt /etc/conf/network and /etc/hosts
297 according to your changes. Changes only take affect the next time
298 rocknet is executed.'"
300 cmd="$cmd 'Static hostname: $HOSTNAME' 'set_name HOSTNAME'"
301 cmd="$cmd 'Static domainname: $DOMAINNAME' 'set_name DOMAINNAME'"
302 cmd="$cmd 'Static DNS-Server: $DNSSRV' 'set_dns' '' ''"
304 for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
305 cmd="$cmd '${tags[$i]}' 'edit_global_tag $i'"
306 done
307 cmd="$cmd 'Add new global tag' 'add_global_tag' '' ''"
309 for if in $interfaces ; do
310 cmd="$cmd 'Edit interface ${if//_/ }' 'edit_if $if'"
311 done
313 cmd="$cmd 'Add new interface/profile' 'add_interface' '' ''"
315 cmd="$cmd 'Configure runlevels for network service'"
316 cmd="$cmd '$STONE runlevel edit_srv network'"
317 cmd="$cmd '(Re-)Start network init script'"
318 cmd="$cmd '$STONE runlevel restart network'"
319 cmd="$cmd '' ''"
321 cmd="$cmd 'View/Edit /etc/resolv.conf file' 'edit /etc/resolv.conf'"
322 cmd="$cmd 'View/Edit /etc/hosts file' 'edit /etc/hosts'"
323 cmd="$cmd 'View/Edit $rocknet_config file' 'edit $rocknet_config'"
325 eval "$cmd"
326 do : ; done