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.

318 lines
8.0 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,#.*,,' < "$rocknet_base"/config )
  54. }
  55. write_section() {
  56. local globals=1
  57. local passit=1
  58. local dumped=0
  59. [ "$1" = "" ] && passit=0
  60. echo -n > $rocknet_base/config.new
  61. while read netcmd para
  62. do
  63. if [ -n "$netcmd" ]; then
  64. netcmd="${netcmd//-/_}"
  65. para="$( echo "$para" | sed 's,[\*\?],\\&,g' )"
  66. if [ "$netcmd" = "interface" ] ; then
  67. prof="$( echo "$para" | sed 's,[(),],_,g' )"
  68. [ "$prof" = "$1" ] && passit=0 || passit=1
  69. globals=0
  70. # write out a separating newline
  71. echo "" >> $rocknet_base/config.new
  72. fi
  73. # when we reached the matching section dump the
  74. # mew tags ...
  75. if [ $passit = 0 -a $dumped = 0 ] ; then
  76. for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
  77. netcmd="${tags[$i]}"
  78. [ $globals = 0 ] && [[ "$netcmd" != interface* ]] && \
  79. echo -en "\t" >> \
  80. $rocknet_base/config.new
  81. echo "${tags[$i]}" >> \
  82. $rocknet_base/config.new
  83. done
  84. dumped=1
  85. fi
  86. if [ $passit = 1 ] ; then
  87. [ $globals = 0 -a \
  88. "$netcmd" != "interface" ] && \
  89. echo -en "\t" >> \
  90. $rocknet_base/config.new
  91. echo "$netcmd $para" >> \
  92. $rocknet_base/config.new
  93. fi
  94. fi
  95. done < <( cat < "$rocknet_base"/config )
  96. mv $rocknet_base/config{.new,}
  97. }
  98. edit_tag() {
  99. tag="${tags[$1]}"
  100. name="$tag"
  101. gui_input "Set new value for tag '$name'" \
  102. "$tag" "tag"
  103. tags[$1]="$tag"
  104. }
  105. edit_global_tag() {
  106. edit_tag $@
  107. write_section ""
  108. }
  109. add_tag() {
  110. tta="$@"
  111. if [ "$tta" = "" ] ; then
  112. cmd="gui_menu add_tag 'Add tag of type'"
  113. while read tag module ; do
  114. cmd="$cmd '`printf "%-12s %s" "$tag" "($module)"`' 'tta=$tag'"
  115. done < <( cd /etc/network/modules/ ; grep public_ * | sed -e \
  116. 's/\([a-zA-Z0-9_-]*\).sh:public_\([a-zA-Z0-9_-]*\).*/\2 \1/' \
  117. | sort +2 +1)
  118. eval "$cmd"
  119. fi
  120. if [ "$tta" ] ; then
  121. tagno=${#tags[@]}
  122. tags[$tagno]="$tta"
  123. edit_tag $tagno
  124. fi
  125. }
  126. add_global_tag() {
  127. add_tag $@
  128. write_section ""
  129. }
  130. edit_if() {
  131. read_section "$1"
  132. quit=0
  133. while
  134. cmd="gui_menu if_edit 'Configure interface ${1//_/ }'"
  135. for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
  136. cmd="$cmd '${tags[$i]}' 'edit_tag $i'"
  137. done
  138. cmd="$cmd '' '' 'Add new tag' 'add_tag'"
  139. cmd="$cmd 'Delete this interface/profile' 'del_interface $1 && quit=1'"
  140. # tiny hack since gui_menu return 0 or 1 depending on the exec
  141. # status of e.g. dialog - and thus a del_interface && false cannot
  142. # be used ...
  143. eval "$cmd" || quit=1
  144. [ $quit = 0 ]
  145. do : ; done
  146. write_section "$1"
  147. }
  148. add_interface() {
  149. if="$1"
  150. gui_input "The new interface name (and profile)" \
  151. "$if" "if"
  152. unset tags
  153. tags[0]="interface $if"
  154. # for now we need to add the interface line into the file
  155. # so the parse finds the right place to add the new tags
  156. echo -e "\ninterface $if\n#added by stone" >> "$rocknet_base"/config
  157. if gui_yesno "Use DHCP to obtain the configuration?" ; then
  158. add_tag "dhcp"
  159. else
  160. add_tag "ip 192.168.5.1"
  161. add_tag "gw 192.168.5.1"
  162. add_tag "nameserver 192.168.5.1"
  163. fi
  164. write_section "$if"
  165. }
  166. del_interface() {
  167. unset tags
  168. write_section "$1"
  169. }
  170. ### STATIC OLD-STYLE CONFIG ###
  171. set_name() {
  172. old1="$HOSTNAME" old2="$HOSTNAME.$DOMAINNAME" old3="$DOMAINNAME"
  173. if [ $1 = HOSTNAME ] ; then
  174. gui_input "Set a new hostname (without domain part)" \
  175. "${!1}" "$1"
  176. else
  177. gui_input "Set a new domainname (without host part)" \
  178. "${!1}" "$1"
  179. fi
  180. new="$HOSTNAME.$DOMAINNAME $HOSTNAME"
  181. echo "$HOSTNAME" > /etc/HOSTNAME ; hostname "$HOSTNAME"
  182. #ip="`echo $IPADDR | sed 's,[/ ].*,,'`"
  183. #if grep -q "^$ip\\b" /etc/hosts ; then
  184. # tmp="`mktemp`"
  185. # sed -e "/^$ip\\b/ s,\\b$old2\\b[ ]*,,g" \
  186. # -e "/^$ip\\b/ s,\\b$old1\\b[ ]*,,g" \
  187. # -e "/^$ip\\b/ s,[ ]\\+,&$new ," < /etc/hosts > $tmp
  188. # cat $tmp > /etc/hosts ; rm -f $tmp
  189. #else
  190. # echo -e "$ip\\t$new" >> /etc/hosts
  191. #fi
  192. if [ $1 = DOMAINNAME ] ; then
  193. tmp="`mktemp`"
  194. grep -vx "search $old3" /etc/resolv.conf > $tmp
  195. [ -n "$DOMAINNAME" ] && echo "search $DOMAINNAME" >> $tmp
  196. cat $tmp > /etc/resolv.conf
  197. rm -f $tmp
  198. fi
  199. }
  200. set_dns() {
  201. gui_input "Set a new (space seperated) list of DNS Servers" "$DNSSRV" "DNSSRV"
  202. DNSSRV="`echo $DNSSRV`" ; [ -z "$DNSSRV" ] && DNSSRV="none"
  203. tmp="`mktemp`" ; grep -v '^nameserver\b' /etc/resolv.conf > $tmp
  204. for x in $DNSSRV ; do
  205. [ "$x" != "none" ] && echo "nameserver $x" >> $tmp
  206. done
  207. cat $tmp > /etc/resolv.conf
  208. rm -f $tmp
  209. }
  210. HOSTNAME="`hostname`"
  211. DOMAINNAME="`hostname -d 2> /dev/null`"
  212. tmp="`mktemp`"
  213. grep '^nameserver ' /etc/resolv.conf | tr '\t' ' ' | tr -s ' ' | \
  214. sed 's,^nameserver *\([^ ]*\),DNSSRV="$DNSSRV \1",' > $tmp
  215. DNSSRV='' ; . $tmp ; DNSSRV="`echo $DNSSRV`"
  216. [ -z "$DNSSRV" ] && DNSSRV="none" ; rm -f $tmp
  217. main() {
  218. first_run=1
  219. while
  220. # read global section and interface list ...
  221. read_section ""
  222. p_interfaces=$(ip link | egrep '[^:]*: .*' | \
  223. sed 's/[^:]*: \([a-z0-9]*\): .*/\1/' | \
  224. grep -v -e lo -e sit)
  225. if [ $first_run = 1 ] ; then
  226. first_run=0
  227. # check if a section for the interface is already present
  228. for x in $p_interfaces ; do
  229. if [[ $interfaces != *$x* ]] ; then
  230. if gui_yesno "Unconfigured interface $x detected. \
  231. Do you want to create an interface section?" ; then
  232. add_interface "$x"
  233. fi
  234. fi
  235. done
  236. read_section ""
  237. fi
  238. cmd="gui_menu network 'Network Configuration - Select an item to
  239. change the value
  240. WARNING: This script tries to adapt /etc/network/config and /etc/hosts
  241. according to your changes. Changes only take affect the next time
  242. rocknet is executed.'"
  243. cmd="$cmd 'Static hostname: $HOSTNAME' 'set_name HOSTNAME'"
  244. cmd="$cmd 'Static domainname: $DOMAINNAME' 'set_name DOMAINNAME'"
  245. cmd="$cmd 'Static DNS-Server: $DNSSRV' 'set_dns' '' ''"
  246. for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
  247. cmd="$cmd '${tags[$i]}' 'edit_global_tag $i'"
  248. done
  249. cmd="$cmd 'Add new global tag' 'add_global_tag' '' ''"
  250. for if in $interfaces ; do
  251. cmd="$cmd 'Edit interface ${if//_/ }' 'edit_if $if'"
  252. done
  253. cmd="$cmd 'Add new interface/profile' 'add_interface' '' ''"
  254. cmd="$cmd 'Configure runlevels for network service'"
  255. cmd="$cmd '$STONE runlevel edit_srv network'"
  256. cmd="$cmd '(Re-)Start network init script'"
  257. cmd="$cmd '$STONE runlevel restart network'"
  258. cmd="$cmd '' ''"
  259. cmd="$cmd 'View/Edit /etc/resolv.conf file' 'edit /etc/resolv.conf'"
  260. cmd="$cmd 'View/Edit /etc/hosts file' 'edit /etc/hosts'"
  261. cmd="$cmd 'View/Edit $rocknet_base/config file' 'edit $rocknet_base/config'"
  262. eval "$cmd"
  263. do : ; done
  264. }