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.

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