* imported qemu/hotfix-glibc-2.41.patch
[t2sde.git] / package / network / rocknet / stone_mod_network.sh
blobb60313147e946d657023b9d165199c14d481f9df
1 # --- T2-COPYRIGHT-NOTE-BEGIN ---
2 # T2 SDE: package/*/rocknet/stone_mod_network.sh
3 # Copyright (C) 2004 - 2024 The T2 SDE Project
4 # Copyright (C) 1998 - 2003 ROCK Linux Project
5 #
6 # This Copyright note is generated by scripts/Create-CopyPatch,
7 # more information can be found in the files COPYING and README.
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License version 2.
11 # --- T2-COPYRIGHT-NOTE-END ---
13 # [MAIN] 20 network Network Configuration
14 # [SETUP] 95 network
16 ### DYNAMIC NEW-STYLE CONFIG ###
18 export rocknet_base="/lib/network" # export needed for subshells ...
19 export rocknet_config="/etc/conf/network"
21 edit() {
22 gui_edit "Edit file $1" "$1"
23 exec $STONE network
26 read_section() {
27 local globals=1
28 local readit=0
29 local i=0
31 unset tags
32 unset interfaces
35 [ "$1" = "" ] && readit=1
36 while read netcmd para
38 if [ -n "$netcmd" ]; then
39 netcmd="${netcmd//-/_}"
40 para="$( echo "$para" | sed 's,[\*\?],\\&,g' )"
41 if [ "$netcmd" = "interface" ]; then
42 prof="$( echo "$para" | sed 's,[(),],_,g' )"
43 [ "$prof" = "$1" ] && readit=1 || readit=0
44 globals=0
45 interfaces="$interfaces $prof"
47 if [ $readit = 1 ]; then
48 tags[$i]="$netcmd $para"
49 i=$((i+1))
52 done < <( sed 's,\(^\|[ \t]\)#.*$,,' < $rocknet_config )
55 write_tags() {
56 for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
57 local netcmd="${tags[$i]}"
58 [ "$netcmd" ] || continue
59 [ $1 = 0 ] && [[ "$netcmd" != interface* ]] && \
60 echo -en "\t"
61 echo "${tags[$i]}"
62 done
65 write_section() {
66 local globals=1
67 local passit=1
68 local dumped=0
70 [ "$1" = "" ] && passit=0
72 echo -n > $rocknet_config.new
74 while read netcmd para; do
75 [ "$netcmd" ] || continue
76 netcmd="${netcmd//-/_}"
77 para="$( echo "$para" | sed 's,[\*\?],\\&,g' )"
79 # when we reached the matching section dump the
80 # mew tags ...
81 if [ $passit = 0 -a $dumped = 0 ]; then
82 write_tags $globals >> $rocknet_config.new
83 dumped=1
86 # if we reached a new interface section maybe change
87 # the state
88 if [ "$netcmd" = "interface" ]; then
89 prof="$( echo "$para" | sed 's,[(),],_,g' )"
90 [ "$prof" = "$1" ] && passit=0 || passit=1
92 # write out a separating newline
93 echo "" >> $rocknet_config.new
95 globals=0
98 # just pass the line thru?
99 if [ $passit = 1 ]; then
100 [ $globals = 0 -a "$netcmd" != "interface" ] && \
101 echo -en "\t" >> $rocknet_config.new
102 echo "$netcmd $para" >> $rocknet_config.new
104 done < <( cat $rocknet_config )
106 # if the config file was empty, for an not yet present or last
107 # we had no change to match the existing position - so write them
108 # out now ...
109 [ $globals = 0 ] && echo "" >> $rocknet_config.new
110 [ "$1" ] && globals=0
111 [ $dumped = 0 ] && write_tags $globals >> $rocknet_config.new
113 mv $rocknet_config{.new,}
116 edit_tag() {
117 tag="${tags[$1]}"
118 name="$tag"
119 gui_input "Set new value for tag '$name'" \
120 "$tag" "tag"
122 tags[$1]="$tag"
125 edit_global_tag() {
126 edit_tag $@
127 write_section ""
130 add_tag() {
131 tta="$@"
132 if [ "$tta" = "" ]; then
133 cmd="gui_menu add_tag 'Add tag of from module'"
135 while read module ; do
136 cmd="$cmd "$module" module='$module'"
137 done < <( cd $rocknet_base/ ; grep public_ * | sed -e \
138 's/\.sh//' -e 's/:.*//' | sort -u )
139 module=""
140 eval $cmd
142 cmd="gui_menu add_tag 'Add tag of type'"
143 while read tag ; do
144 cmd="$cmd "$tag" 'tta=$tag'"
145 done < <( cd $rocknet_base/ ; grep -h public_ $module.sh \
146 | sed -e 's/public_\([^(]*\).*/\1/' | sort )
147 eval "$cmd"
150 if [ "$tta" ]; then
151 tagno=${#tags[@]}
152 tags[$tagno]="$tta"
153 edit_tag $tagno
157 add_global_tag() {
158 add_tag $@
159 write_section ""
162 edit_if() {
163 read_section "$1"
165 quit=0
166 while
167 cmd="gui_menu if_edit 'Configure interface ${1//_/ }'"
168 for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
169 [ "${tags[$i]}" ] || continue
170 cmd="$cmd '${tags[$i]}' 'edit_tag $i'"
171 done
173 cmd="$cmd '' '' 'Add new tag' 'add_tag'"
174 cmd="$cmd 'Delete this interface/profile' 'del_interface $1 && quit=1'"
176 # tiny hack since gui_menu return 0 or 1 depending on the exec
177 # status of e.g. dialog - and thus a del_interface && false
178 # cannot be used ...
180 eval "$cmd" || quit=1
181 [ $quit = 0 ]
182 do : ; done
183 write_section "$1"
186 add_interface() {
187 if="$1"
189 gui_input "The new interface name (and profile)" \
190 "$if" "if"
191 unset tags
192 tags[0]="interface $if"
194 if gui_yesno "Use DHCP to obtain the configuration?"; then
195 add_tag "dhcp"
196 else
197 add_tag "ip 192.168.5.1/24"
198 add_tag "gw 192.168.5.1"
199 add_tag "nameserver 192.168.5.1"
202 write_section "$if"
205 del_interface() {
206 unset tags
207 write_section "$1"
210 ### STATIC OLD-STYLE CONFIG ###
212 set_name() {
213 old1="$HOSTNAME" old2="$HOSTNAME.$DOMAINNAME" old3="$DOMAINNAME"
214 if [ $1 = HOSTNAME ]; then
215 gui_input "Set a new hostname (without domain part)" \
216 "${!1}" "$1"
217 else
218 gui_input "Set a new domainname (without host part)" \
219 "${!1}" "$1"
221 new="$HOSTNAME.$DOMAINNAME $HOSTNAME"
223 echo "$HOSTNAME" > /etc/hostname ; hostname "$HOSTNAME"
225 #ip="`echo $IPADDR | sed 's,[/ ].*,,'`"
226 #if grep -q "^$ip\\b" /etc/hosts; then
227 # tmp="`mktemp`"
228 # sed -e "/^$ip\\b/ s,\\b$old2\\b[ ]*,,g" \
229 # -e "/^$ip\\b/ s,\\b$old1\\b[ ]*,,g" \
230 # -e "/^$ip\\b/ s,[ ]\\+,&$new ," < /etc/hosts > $tmp
231 # cat $tmp > /etc/hosts ; rm -f $tmp
232 #else
233 # echo -e "$ip\\t$new" >> /etc/hosts
236 if [ $1 = DOMAINNAME ]; then
237 tmp="`mktemp`"
238 grep -vx "search $old3" /etc/resolv.conf > $tmp
239 [ -n "$DOMAINNAME" ] && echo "search $DOMAINNAME" >> $tmp
240 cat $tmp > /etc/resolv.conf
241 rm -f $tmp
245 set_dns() {
246 gui_input "Set a new (space seperated) list of DNS Servers" "$DNSSRV" "DNSSRV"
247 DNSSRV="`echo $DNSSRV`" ; [ -z "$DNSSRV" ] && DNSSRV="none"
249 tmp="`mktemp`" ; grep -v '^nameserver\b' /etc/resolv.conf > $tmp
250 for x in $DNSSRV ; do
251 [ "$x" != "none" ] && echo "nameserver $x" >> $tmp
252 done
253 cat $tmp > /etc/resolv.conf
254 rm -f $tmp
257 HOSTNAME="`hostname`"
258 DOMAINNAME="`hostname -d 2> /dev/null`"
260 tmp="`mktemp`"
261 grep '^nameserver ' /etc/resolv.conf | tr '\t' ' ' | tr -s ' ' | \
262 sed 's,^nameserver *\([^ ]*\),DNSSRV="$DNSSRV \1",' > $tmp
263 DNSSRV='' ; . $tmp ; DNSSRV="`echo $DNSSRV`"
264 [ -z "$DNSSRV" ] && DNSSRV="none" ; rm -f $tmp
266 main() {
267 first_run=1
268 while
270 # read global section and interface list ...
271 read_section ""
273 p_interfaces=$(ip -o link |
274 sed -n '/link\/ether/ s/[^:]*: \([^:]*\):.*/\1/p')
276 if [ $first_run = 1 ]; then
277 first_run=0
278 # check if a section for the interface is already present
279 for x in $p_interfaces ; do
280 if [[ $interfaces != *$x* ]]; then
281 if gui_yesno "Unconfigured interface $x detected. \
282 Do you want to create an interface section?"; then
283 add_interface "$x"
286 done
287 read_section ""
290 cmd="gui_menu network 'Network Configuration - Select an item to
291 change the value
293 WARNING: This script tries to adapt /etc/conf/network and /etc/hosts
294 according to your changes. Changes only take affect the next time
295 rocknet is executed.'"
297 cmd="$cmd 'Static hostname: $HOSTNAME' 'set_name HOSTNAME'"
298 cmd="$cmd 'Static domainname: $DOMAINNAME' 'set_name DOMAINNAME'"
299 cmd="$cmd 'Static DNS-Server: $DNSSRV' 'set_dns' '' ''"
301 for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )); do
302 cmd="$cmd '${tags[$i]}' 'edit_global_tag $i'"
303 done
304 cmd="$cmd 'Add new global tag' 'add_global_tag' '' ''"
306 for if in $interfaces; do
307 cmd="$cmd 'Edit interface ${if//_/ }' 'edit_if $if'"
308 done
310 cmd="$cmd 'Add new interface/profile' 'add_interface' '' ''"
312 cmd="$cmd 'Configure runlevels for network service'"
313 cmd="$cmd '$STONE runlevel edit_srv network'"
314 cmd="$cmd '(Re-)Start network init script'"
315 cmd="$cmd '$STONE runlevel restart network'"
316 cmd="$cmd '' ''"
318 cmd="$cmd 'View/Edit /etc/resolv.conf file' 'edit /etc/resolv.conf'"
319 cmd="$cmd 'View/Edit /etc/hosts file' 'edit /etc/hosts'"
320 cmd="$cmd 'View/Edit $rocknet_config file' 'edit $rocknet_config'"
322 eval "$cmd"
323 do : ; done