mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
8.4 KiB

  1. # --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  2. #
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. # Please add additional copyright information _after_ the line containing
  5. # the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  6. # the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  7. #
  8. # ROCK Linux: rock-src/package/base/iproute2/stone_mod_network.sh
  9. # ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version. A copy of the GNU General Public
  15. # License can be found at Documentation/COPYING.
  16. #
  17. # Many people helped and are helping developing ROCK Linux. Please
  18. # have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  19. # file for details.
  20. #
  21. # --- ROCK-COPYRIGHT-NOTE-END ---
  22. #
  23. # [MAIN] 20 network Network Configuration
  24. ### DYNAMIC NEW-STYLE CONFIG ###
  25. rocknet_base="/etc/network"
  26. edit() {
  27. gui_edit "Edit file $1" "$1"
  28. exec $STONE network
  29. }
  30. read_section() {
  31. local globals=1
  32. local readit=0
  33. local i=0
  34. unset tags
  35. unset interfaces
  36. [ "$1" = "" ] && readit=1
  37. while read netcmd para
  38. do
  39. if [ -n "$netcmd" ]; then
  40. netcmd="${netcmd//-/_}"
  41. para="$( echo "$para" | sed 's,[\*\?],\\&,g' )"
  42. if [ "$netcmd" = "interface" ] ; then
  43. prof="$( echo "$para" | sed 's,[(),],_,g' )"
  44. [ "$prof" = "$1" ] && readit=1 || readit=0
  45. globals=0
  46. interfaces="$interfaces $prof"
  47. fi
  48. if [ $readit = 1 ] ; then
  49. tags[$i]="$netcmd $para"
  50. i=$((i+1))
  51. fi
  52. fi
  53. done < <( sed 's,\(^\|[ \t]\)#.*$,,' < "$rocknet_base"/config )
  54. }
  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
  63. }
  64. write_section() {
  65. local globals=1
  66. local passit=1
  67. local dumped=0
  68. [ "$1" = "" ] && passit=0
  69. echo -n > $rocknet_base/config.new
  70. while read netcmd para ; do
  71. [ "$netcmd" ] || continue
  72. netcmd="${netcmd//-/_}"
  73. para="$( echo "$para" | sed 's,[\*\?],\\&,g' )"
  74. # when we reached the matching section dump the
  75. # mew tags ...
  76. if [ $passit = 0 -a $dumped = 0 ] ; then
  77. write_tags $globals >> $rocknet_base/config.new
  78. dumped=1
  79. fi
  80. # if we reached a new interface section maybe change
  81. # the state
  82. if [ "$netcmd" = "interface" ] ; then
  83. prof="$( echo "$para" | sed 's,[(),],_,g' )"
  84. [ "$prof" = "$1" ] && passit=0 || passit=1
  85. # write out a separating newline
  86. echo "" >> $rocknet_base/config.new
  87. globals=0
  88. fi
  89. # just pass the line thru?
  90. if [ $passit = 1 ] ; then
  91. [ $globals = 0 -a "$netcmd" != "interface" ] && \
  92. echo -en "\t" >> $rocknet_base/config.new
  93. echo "$netcmd $para" >> $rocknet_base/config.new
  94. fi
  95. done < <( cat < "$rocknet_base"/config )
  96. # if the config file was empty, for an not yet present or last
  97. # we had no change to match the existing position - so write them
  98. # out now ...
  99. [ $globals = 0 ] && echo "" >> $rocknet_base/config.new
  100. [ "$1" ] && globals=0
  101. [ $dumped = 0 ] && write_tags $globals >> $rocknet_base/config.new
  102. mv $rocknet_base/config{.new,}
  103. }
  104. edit_tag() {
  105. tag="${tags[$1]}"
  106. name="$tag"
  107. gui_input "Set new value for tag '$name'" \
  108. "$tag" "tag"
  109. tags[$1]="$tag"
  110. }
  111. edit_global_tag() {
  112. edit_tag $@
  113. write_section ""
  114. }
  115. add_tag() {
  116. tta="$@"
  117. if [ "$tta" = "" ] ; then
  118. cmd="gui_menu add_tag 'Add tag of from module'"
  119. while read module ; do
  120. cmd="$cmd "$module" module='$module'"
  121. done < <( cd /etc/network/modules/ ; grep public_ * | sed -e \
  122. 's/\.sh//' -e 's/:.*//' | sort -u )
  123. module=""
  124. eval $cmd
  125. cmd="gui_menu add_tag 'Add tag of type'"
  126. while read tag ; do
  127. cmd="$cmd "$tag" 'tta=$tag'"
  128. done < <( cd /etc/network/modules/ ; grep -h public_ $module.sh \
  129. | sed -e 's/public_\([^(]*\).*/\1/' | sort )
  130. eval "$cmd"
  131. fi
  132. if [ "$tta" ] ; then
  133. tagno=${#tags[@]}
  134. tags[$tagno]="$tta"
  135. edit_tag $tagno
  136. fi
  137. }
  138. add_global_tag() {
  139. add_tag $@
  140. write_section ""
  141. }
  142. edit_if() {
  143. read_section "$1"
  144. quit=0
  145. while
  146. cmd="gui_menu if_edit 'Configure interface ${1//_/ }'"
  147. for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
  148. [ "${tags[$i]}" ] || continue
  149. cmd="$cmd '${tags[$i]}' 'edit_tag $i'"
  150. done
  151. cmd="$cmd '' '' 'Add new tag' 'add_tag'"
  152. cmd="$cmd 'Delete this interface/profile' 'del_interface $1 && quit=1'"
  153. # tiny hack since gui_menu return 0 or 1 depending on the exec
  154. # status of e.g. dialog - and thus a del_interface && false
  155. # cannot be used ...
  156. eval "$cmd" || quit=1
  157. [ $quit = 0 ]
  158. do : ; done
  159. write_section "$1"
  160. }
  161. add_interface() {
  162. if="$1"
  163. gui_input "The new interface name (and profile)" \
  164. "$if" "if"
  165. unset tags
  166. tags[0]="interface $if"
  167. if gui_yesno "Use DHCP to obtain the configuration?" ; then
  168. add_tag "dhcp"
  169. else
  170. add_tag "ip 192.168.5.1/24"
  171. add_tag "gw 192.168.5.1"
  172. add_tag "nameserver 192.168.5.1"
  173. fi
  174. write_section "$if"
  175. }
  176. del_interface() {
  177. unset tags
  178. write_section "$1"
  179. }
  180. ### STATIC OLD-STYLE CONFIG ###
  181. set_name() {
  182. old1="$HOSTNAME" old2="$HOSTNAME.$DOMAINNAME" old3="$DOMAINNAME"
  183. if [ $1 = HOSTNAME ] ; then
  184. gui_input "Set a new hostname (without domain part)" \
  185. "${!1}" "$1"
  186. else
  187. gui_input "Set a new domainname (without host part)" \
  188. "${!1}" "$1"
  189. fi
  190. new="$HOSTNAME.$DOMAINNAME $HOSTNAME"
  191. echo "$HOSTNAME" > /etc/HOSTNAME ; hostname "$HOSTNAME"
  192. #ip="`echo $IPADDR | sed 's,[/ ].*,,'`"
  193. #if grep -q "^$ip\\b" /etc/hosts ; then
  194. # tmp="`mktemp`"
  195. # sed -e "/^$ip\\b/ s,\\b$old2\\b[ ]*,,g" \
  196. # -e "/^$ip\\b/ s,\\b$old1\\b[ ]*,,g" \
  197. # -e "/^$ip\\b/ s,[ ]\\+,&$new ," < /etc/hosts > $tmp
  198. # cat $tmp > /etc/hosts ; rm -f $tmp
  199. #else
  200. # echo -e "$ip\\t$new" >> /etc/hosts
  201. #fi
  202. if [ $1 = DOMAINNAME ] ; then
  203. tmp="`mktemp`"
  204. grep -vx "search $old3" /etc/resolv.conf > $tmp
  205. [ -n "$DOMAINNAME" ] && echo "search $DOMAINNAME" >> $tmp
  206. cat $tmp > /etc/resolv.conf
  207. rm -f $tmp
  208. fi
  209. }
  210. set_dns() {
  211. gui_input "Set a new (space seperated) list of DNS Servers" "$DNSSRV" "DNSSRV"
  212. DNSSRV="`echo $DNSSRV`" ; [ -z "$DNSSRV" ] && DNSSRV="none"
  213. tmp="`mktemp`" ; grep -v '^nameserver\b' /etc/resolv.conf > $tmp
  214. for x in $DNSSRV ; do
  215. [ "$x" != "none" ] && echo "nameserver $x" >> $tmp
  216. done
  217. cat $tmp > /etc/resolv.conf
  218. rm -f $tmp
  219. }
  220. HOSTNAME="`hostname`"
  221. DOMAINNAME="`hostname -d 2> /dev/null`"
  222. tmp="`mktemp`"
  223. grep '^nameserver ' /etc/resolv.conf | tr '\t' ' ' | tr -s ' ' | \
  224. sed 's,^nameserver *\([^ ]*\),DNSSRV="$DNSSRV \1",' > $tmp
  225. DNSSRV='' ; . $tmp ; DNSSRV="`echo $DNSSRV`"
  226. [ -z "$DNSSRV" ] && DNSSRV="none" ; rm -f $tmp
  227. main() {
  228. first_run=1
  229. while
  230. # read global section and interface list ...
  231. read_section ""
  232. p_interfaces=$(ip link | egrep '[^:]*: .*' | \
  233. sed 's/[^:]*: \([a-z0-9]*\): .*/\1/' | \
  234. grep -v -e lo -e sit)
  235. if [ $first_run = 1 ] ; then
  236. first_run=0
  237. # check if a section for the interface is already present
  238. for x in $p_interfaces ; do
  239. if [[ $interfaces != *$x* ]] ; then
  240. if gui_yesno "Unconfigured interface $x detected. \
  241. Do you want to create an interface section?" ; then
  242. add_interface "$x"
  243. fi
  244. fi
  245. done
  246. read_section ""
  247. fi
  248. cmd="gui_menu network 'Network Configuration - Select an item to
  249. change the value
  250. WARNING: This script tries to adapt /etc/network/config and /etc/hosts
  251. according to your changes. Changes only take affect the next time
  252. rocknet is executed.'"
  253. cmd="$cmd 'Static hostname: $HOSTNAME' 'set_name HOSTNAME'"
  254. cmd="$cmd 'Static domainname: $DOMAINNAME' 'set_name DOMAINNAME'"
  255. cmd="$cmd 'Static DNS-Server: $DNSSRV' 'set_dns' '' ''"
  256. for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
  257. cmd="$cmd '${tags[$i]}' 'edit_global_tag $i'"
  258. done
  259. cmd="$cmd 'Add new global tag' 'add_global_tag' '' ''"
  260. for if in $interfaces ; do
  261. cmd="$cmd 'Edit interface ${if//_/ }' 'edit_if $if'"
  262. done
  263. cmd="$cmd 'Add new interface/profile' 'add_interface' '' ''"
  264. cmd="$cmd 'Configure runlevels for network service'"
  265. cmd="$cmd '$STONE runlevel edit_srv network'"
  266. cmd="$cmd '(Re-)Start network init script'"
  267. cmd="$cmd '$STONE runlevel restart network'"
  268. cmd="$cmd '' ''"
  269. cmd="$cmd 'View/Edit /etc/resolv.conf file' 'edit /etc/resolv.conf'"
  270. cmd="$cmd 'View/Edit /etc/hosts file' 'edit /etc/hosts'"
  271. cmd="$cmd 'View/Edit $rocknet_base/config file' 'edit $rocknet_base/config'"
  272. eval "$cmd"
  273. do : ; done
  274. }