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. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  2. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. #
  4. # Filename: package/.../stone/stone_mod_network.sh
  5. # Copyright (C) 2008 The OpenSDE Project
  6. # Copyright (C) 2004 - 2006 The T2 SDE Project
  7. # Copyright (C) 1998 - 2003 Clifford Wolf
  8. #
  9. # More information can be found in the files COPYING and README.
  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; version 2 of the License. A copy of the
  14. # GNU General Public License can be found in the file COPYING.
  15. # --- SDE-COPYRIGHT-NOTE-END ---
  16. #
  17. # [MAIN] 20 network Network Configuration
  18. ### DYNAMIC NEW-STYLE CONFIG ###
  19. export rocknet_base="/lib/network" # export needed for subshells ...
  20. export rocknet_config="/etc/conf/network"
  21. edit() {
  22. gui_edit "Edit file $1" "$1"
  23. exec $STONE network
  24. }
  25. read_section() {
  26. local globals=1
  27. local readit=0
  28. local i=0
  29. unset tags
  30. unset interfaces
  31. [ "$1" = "" ] && readit=1
  32. while read netcmd para
  33. do
  34. if [ -n "$netcmd" ]; then
  35. netcmd="${netcmd//-/_}"
  36. para="$( echo "$para" | sed 's,[\*\?],\\&,g' )"
  37. if [ "$netcmd" = "interface" ] ; then
  38. prof="$( echo "$para" | sed 's,[(),],_,g' )"
  39. [ "$prof" = "$1" ] && readit=1 || readit=0
  40. globals=0
  41. interfaces="$interfaces $prof"
  42. fi
  43. if [ $readit = 1 ] ; then
  44. tags[$i]="$netcmd $para"
  45. i=$((i+1))
  46. fi
  47. fi
  48. done < <( sed 's,\(^\|[ \t]\)#.*$,,' < $rocknet_config )
  49. }
  50. write_tags() {
  51. for (( i=0 ; $i < ${#tags[@]} ; i=i+1 )) ; do
  52. local netcmd="${tags[$i]}"
  53. [ "$netcmd" ] || continue
  54. [ $1 = 0 ] && [[ "$netcmd" != interface* ]] && \
  55. echo -en "\t"
  56. echo "${tags[$i]}"
  57. done
  58. }
  59. write_section() {
  60. local globals=1
  61. local passit=1
  62. local dumped=0
  63. [ "$1" = "" ] && passit=0
  64. echo -n > $rocknet_config.new
  65. while read netcmd para ; do
  66. [ "$netcmd" ] || continue
  67. netcmd="${netcmd//-/_}"
  68. para="$( echo "$para" | sed 's,[\*\?],\\&,g' )"
  69. # when we reached the matching section dump the
  70. # mew tags ...
  71. if [ $passit = 0 -a $dumped = 0 ] ; then
  72. write_tags $globals >> $rocknet_config.new
  73. dumped=1
  74. fi
  75. # if we reached a new interface section maybe change
  76. # the state
  77. if [ "$netcmd" = "interface" ] ; then
  78. prof="$( echo "$para" | sed 's,[(),],_,g' )"
  79. [ "$prof" = "$1" ] && passit=0 || passit=1
  80. # write out a separating newline
  81. echo "" >> $rocknet_config.new
  82. globals=0
  83. fi
  84. # just pass the line thru?
  85. if [ $passit = 1 ] ; then
  86. [ $globals = 0 -a "$netcmd" != "interface" ] && \
  87. echo -en "\t" >> $rocknet_config.new
  88. echo "$netcmd $para" >> $rocknet_config.new
  89. fi
  90. done < <( cat $rocknet_config )
  91. # if the config file was empty, for an not yet present or last
  92. # we had no change to match the existing position - so write them
  93. # out now ...
  94. [ $globals = 0 ] && echo "" >> $rocknet_config.new
  95. [ "$1" ] && globals=0
  96. [ $dumped = 0 ] && write_tags $globals >> $rocknet_config.new
  97. mv $rocknet_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 $rocknet_base/ ; 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 $rocknet_base/ ; 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. if gui_yesno "Use DHCP to obtain the configuration?" ; then
  163. add_tag "dhcp"
  164. else
  165. add_tag "ip 192.168.5.1/24"
  166. add_tag "gw 192.168.5.1"
  167. add_tag "nameserver 192.168.5.1"
  168. fi
  169. write_section "$if"
  170. }
  171. del_interface() {
  172. unset tags
  173. write_section "$1"
  174. }
  175. ### STATIC OLD-STYLE CONFIG ###
  176. set_name() {
  177. old1="$HOSTNAME" old2="$HOSTNAME.$DOMAINNAME" old3="$DOMAINNAME"
  178. if [ $1 = HOSTNAME ] ; then
  179. gui_input "Set a new hostname (without domain part)" \
  180. "${!1}" "$1"
  181. else
  182. gui_input "Set a new domainname (without host part)" \
  183. "${!1}" "$1"
  184. fi
  185. new="$HOSTNAME.$DOMAINNAME $HOSTNAME"
  186. echo "$HOSTNAME" > /etc/HOSTNAME ; hostname "$HOSTNAME"
  187. #ip="`echo $IPADDR | sed 's,[/ ].*,,'`"
  188. #if grep -q "^$ip\\b" /etc/hosts ; then
  189. # tmp="`mktemp`"
  190. # sed -e "/^$ip\\b/ s,\\b$old2\\b[ ]*,,g" \
  191. # -e "/^$ip\\b/ s,\\b$old1\\b[ ]*,,g" \
  192. # -e "/^$ip\\b/ s,[ ]\\+,&$new ," < /etc/hosts > $tmp
  193. # cat $tmp > /etc/hosts ; rm -f $tmp
  194. #else
  195. # echo -e "$ip\\t$new" >> /etc/hosts
  196. #fi
  197. if [ $1 = DOMAINNAME ] ; then
  198. tmp="`mktemp`"
  199. grep -vx "search $old3" /etc/resolv.conf > $tmp
  200. [ -n "$DOMAINNAME" ] && echo "search $DOMAINNAME" >> $tmp
  201. cat $tmp > /etc/resolv.conf
  202. rm -f $tmp
  203. fi
  204. }
  205. set_dns() {
  206. gui_input "Set a new (space seperated) list of DNS Servers" "$DNSSRV" "DNSSRV"
  207. DNSSRV="`echo $DNSSRV`" ; [ -z "$DNSSRV" ] && DNSSRV="none"
  208. tmp="`mktemp`" ; grep -v '^nameserver\b' /etc/resolv.conf > $tmp
  209. for x in $DNSSRV ; do
  210. [ "$x" != "none" ] && echo "nameserver $x" >> $tmp
  211. done
  212. cat $tmp > /etc/resolv.conf
  213. rm -f $tmp
  214. }
  215. HOSTNAME="`hostname`"
  216. DOMAINNAME="`hostname -d 2> /dev/null`"
  217. tmp="`mktemp`"
  218. grep '^nameserver ' /etc/resolv.conf | tr '\t' ' ' | tr -s ' ' | \
  219. sed 's,^nameserver *\([^ ]*\),DNSSRV="$DNSSRV \1",' > $tmp
  220. DNSSRV= ; . $tmp ; DNSSRV="`echo $DNSSRV`"
  221. [ -z "$DNSSRV" ] && DNSSRV="none" ; rm -f $tmp
  222. main() {
  223. first_run=1
  224. while
  225. # read global section and interface list ...
  226. read_section ""
  227. p_interfaces=$(ip -o link | \
  228. sed -n -e 's|^[0-9]*: \([^:]*\):.*link/ether.*|\1|p' )
  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. }