OpenSDE Packages Database (without history before r20070)
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.

329 lines
8.0 KiB

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