#!/bin/sh
#
# Gtkdialog box to manage network connections and servers.
# Please use tab to indent.
#
# (c) - SliTaz GNU/Linux 2009.
#
VERSION=20090812

# Check if user is root.
check_root()
{
	if test $(id -u) != 0 ; then
		echo -e "
You must be root to run `basename $0`. Please type 'su' and
root password to become super-user.\n"
		exit 0
	fi
}

#
# Functions called by the connection tab to list interfaces.
#
############################################################

interface_status()
{
	if 	ifconfig | grep -A 1 $i | grep -q inet; then
		ip=`ifconfig | grep -A 1 $i | grep inet | awk '{ print $2 }' | cut -d ":" -f 2`
		echo "connected ($ip)"
	else
		echo "-"
	fi
}
# First column is for icon name.
detect_interfaces()
{
	for i in `ls /sys/class/net`
	do
		case $i in
			eth*)
				echo "network-wired | $i | Ethernet | `interface_status`" ;;
			wlan*|ath*|ra*)
				echo "network-wireless | $i | Wireless | `interface_status`" ;;
			lo)
				echo "gtk-network | $i | Loopback | `interface_status`" ;;
			*)
				continue ;;
		esac
		
	done
}

# When users double click on a connection.
interfaces_List_actions()
{
	echo "$INTERFACE_LIST"
}

# Netbox can be called with args.
case "$1" in
	detect_interfaces|interfaces_List_actions)
		$1
		exit 0 ;;
	*)
		continue ;;
esac

#
# GUI functions
#
############################################################

xterm="xterm -fa MiscFixed -fs 11 -bg gray93 -fg black"
helpbutton()
{
	local label;
        label="<label>$3</label>"
	[ -n "$3" ] || label=""
	cat << EOT
<button>
	<input file icon="help"></input>$label
	<action>$xterm -geometry $2 -title "$1 help (q to quit)" -e "( echo ; $(which $1) --help 2>&1 ) | less"</action>
</button>
EOT
}

editbutton()
{
	cat << EOT
<button>
	<input file icon="accessories-text-editor"></input>
	<action type="lauch">leafpad $1</action>
</button>
EOT
}

manbutton()
{
	cat << EOT
<button>
	<input file icon="browser"></input>
	<label>man</label>
	<action>$xterm -geometry 80x24 -title "$2 man (q to quit)" -e ". /etc/profile; man $1 $2"</action>
</button>
EOT
}

helpdaemon()
{
	cat << EOT
<hbox>
	<text use-markup="true">
		<label>"<b>$3</b>"</label>
	</text>
	<entry editable="false">
EOT
	eval tmp=\$$3
	[ -n "$tmp" ] && cat << EOT
		<default>$tmp</default>
EOT
	cat << EOT
		<variable>$4</variable>
	</entry>
EOT
	[ -n "$6" ] || cat << EOT
$(helpbutton $1 $2)
EOT
	[ -n "$5" ] && cat << EOT
$(manbutton $5 $1)
EOT
	cat << EOT
$(editbutton /etc/daemons.conf)
</hbox>
EOT
}

datafile()
{
	cat << EOT
<hbox>
	<text use-markup="true">
		<label>"<b>$3</b>"</label>
	</text>
 	<entry editable="false">
EOT
	[ -n "$1" ] && cat << EOT
		<default>$1</default>
EOT
	cat << EOT
		<variable>$2</variable>
	</entry>
EOT
	[ -n "$4" ] && cat << EOT
$(manbutton $4)
EOT
	cat << EOT
$(editbutton \$$2)
</hbox>
EOT
}

configfile()
{
	datafile "$1" $2 'Configuration:' "$3"
}

BROWSER=''
for i in firefox opera dillo midori netsurf links ; do
	which $i > /dev/null && BROWSER="$i" && break
done
case "$BROWSER" in
links) BROWSER="links -g ";;
esac

webbutton()
{
	[ -f "/var/lib/tazpkg/installed/$1/receipt" ] && 
	[ -n "$BROWSER" ] && cat << EOT
<button>
	<input file icon="browser"></input>
	<label>web</label>
	<action>. /var/lib/tazpkg/installed/$1/receipt ; $BROWSER \$WEB_SITE &</action>
</button>
EOT
}

wikibutton()
{
[ -n "$BROWSER" ] && cat <<EOT
        <button>
          <input file icon="browser"></input>
	  <label>Wiki</label>
	  <action>$BROWSER $1 &</action>
        </button>
EOT
}

startstopdaemon()
{
	local pkg
	pkg=$1
	[ -n "$2" ] && pkg=$2
	webbutton $pkg
	cat << EOT
<button>
	<label>Start</label>
	<input file icon="forward"></input>
	<action>sed -i "s/RUN_DAEMONS=\\"\\(.*\\)$1 \?\\(.*\\)\\"/RUN_DAEMONS=\\"\1\2\\"/" /etc/rcS.conf</action>
	<action>sed -i "s/RUN_DAEMONS=\\"\\(.*\\)\\"/RUN_DAEMONS=\\"$1 \1\\"/" /etc/rcS.conf</action>
	<action>/etc/init.d/$1 start</action>
</button>
<button>
	<label>Stop</label>
	<input file icon="stop"></input>
	<action>sed -i "s/RUN_DAEMONS=\\"\\(.*\\)$1 \?\\(.*\\)\\"/RUN_DAEMONS=\\"\1\2\\"/" /etc/rcS.conf</action>
	<action>/etc/init.d/$1 stop</action>
</button>
EOT
}

installbox()
{
	cat << EOT
<hbox>
	<text wrap="true" use-markup="true">
		<label>"<i>The package <b>$1</b> is not yet installed.</i>"</label>
	</text>
	<button>
		<input file icon="go-jump"></input>
		<label>Install</label>
		<action>$xterm -geometry 80x17 -title "$1 install" -e "yes y | tazpkg get-install $1 ; echo -e \"----\n\nENTER to continue...\" && read close"</action>
		<action type="exit">restart</action>
	</button>
</hbox>
EOT
}

set_ipup()
{
	[ -f /etc/ppp/ip-up ] && return
	cat > /etc/ppp/ip-up <<EOT
#!/bin/sh
exec $0 call ipup \$@
EOT
	chmod +x /etc/ppp/ip-up
}

while true; do

# Detect WIFI_INTERFACE and update /etc/network.conf
. /etc/network.conf
if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
	WIFI_INTERFACE=$(for i in /sys/class/net/*/wireless; do \
		[ -d $i ] && echo $(basename $(dirname $i)) || echo wlan0; \
		break; done)
	[ -n "$WIFI_INTERFACE" ] && sed -i "s/^WIFI_INTERFACE=.*/WIFI_INTERFACE=\"$WIFI_INTERFACE\"/" /etc/network.conf
fi

#
# Netbox internal calls
#
#########################################################

if [ "$1" = "call" ]; then
	ppp="pppd local lock notty"
	pppup=""
	sub=$2
	shift 2
	case "$sub" in
	sendsshkey)
		check_root
		( dropbearkey -y -f /etc/dropbear/dropbear_rsa_host_key ;
		  cat /etc/ssh/ssh_host_rsa_key.pub ) 2> /dev/null | \
		grep ^ssh | ssh $1 "mkdir .ssh 2> /dev/null ; \
while read key; do for i in authorized_keys authorized_keys2; do \
grep -q \$key .ssh/\$i || echo \$key >> .ssh/\$i ; done ; done ; \
chmod 700 .ssh ; chmod 600 .ssh/authorized_keys*"
		exit 0;;
# OpenSSH VPN:
#   PermitTunnel=point-to-point   (or yes, ethernet, no)
#   Tunnel="4" => tun4
#   Local Client:
#     # ssh -f -w 0:1 $REMOTE true  => local tun0 [, remote tun1]
#     # ifconfig $TUN $IP1 $IP2 netmask 255.255.255.252
#     # route add $REMOTE_NETWORK $IP2
#   Remote Server:
#     # ifconfig $TUN $IP2 $IP1 netmask 255.255.255.252
#     # route add $LOCAL_NETWORK $IP1
	vpnssh)	check_root
		set_ipup
		ps ww | grep -q "$ppp $2:$3" && exit 1
		pipe="/tmp/ssh$$"
		mkfifo $pipe
		[ -n "$4" ] && pppup="ipparam 'addroutes,$(echo $4 | sed 's/ /,/g')'"
		cat $pipe | dbclient -i /etc/dropbear/dropbear_rsa_host_key \
		$1 "$ppp" | $ppp $2:$3 $pppup > $pipe
		rm -f $pipe
		exit 0;;
	killvpnssh)
		check_root
		kill $(ps x | grep dbclient | grep "$ppp" | awk '{ print $1 }')
		exit 0;;
	ipup)
#    Arg  Name                          Example
#    $1   Interface name                ppp0
#    $2   The tty                       ttyS1
#    $3   The link speed                38400
#    $4   Local IP number               12.34.56.78
#    $5   Peer  IP number               12.34.56.99
#    $6   Optional ``ipparam'' value    foo
		iface=$1
		# skip tty if present
		case "$2" in [0-9]*);; *) shift; esac
		peer=$4
		IFS=","; set -- $(eval echo $5); unset IFS
		set -- $1
		if [ "$1" = "addroutes" ]; then
			while [ -n "$2" ]; do
				eval $(ipcalc -n $2)
				eval $(ipcalc -m $2)
				route add -net $NETWORK netmask $NETMASK \
					gw $peer $iface
				shift
			done
		fi
		exit 0;;
	esac
	echo "call $sub unsupported."
	exit 1
fi

#
# Tab display engine
#
######################################################

use_tab()
{
local tmp=''
case "$2" in
header|body)
	$1 | while read exe name call; do
	  case "$exe" in
	  \#*) continue;;
	  /*)  [ -e $exe ] || continue;;
	  *)   which $exe > /dev/null || continue
	  esac
	  if [ "$2" == "header" ]; then
	    echo -n "$tmp$name"
	    tmp='|'
	  else
	    $call
	  fi
	done
	;;
*)
	tmp=$(use_tab $1 header)
	echo "$tmp" | grep -q \| && cat <<EOT
<notebook labels="$tmp">
EOT
	[ -n "$tmp" ] && cat <<EOT
$(use_tab $1 body)
EOT
	echo "$tmp" | grep -q \| && cat <<EOT
</notebook>
EOT
	;;
esac
}

#
# Status wire interfaces
#
tab_status_iface()
{
local eth
eth="$( (cd /sys/class/net ; [ -d eth0 ] && ls -d eth* ) )"
which ethtool > /dev/null || eth=''
if [ -n "$eth" ]; then
    cat <<EOT
<notebook labels="Ifconfig|$(echo $eth | sed 's/ /|/g')">
EOT
fi
cat <<EOT
<frame Ifconfig>
	<text wrap="false" width-chars="58">
		<input>ifconfig</input>
	</text>
</frame>
EOT
if [ -n "$eth" ]; then
    local i
    for i in $eth ; do
        cat <<EOT
<frame $i>
	<text wrap="false" width-chars="58">
		<input>ethtool $i</input>
	</text>
</frame>
EOT
    done
    cat <<EOT
</notebook>
EOT
fi
}

#
# Status wifi interfaces
#
tab_status_wifi_iface()
{
cat <<EOT
<frame Iwconfig>
	<text wrap="false" width-chars="58">
		<input>iwconfig</input>
	</text>
</frame>
EOT
}

#
# Status wifi network
#
tab_status_wifi_net()
{
cat <<EOT
<frame Wireless networks>
	<text wrap="false" width-chars="58">
		<input>iwlist scan</input>
	</text>
</frame>
EOT
}

#
# Status routing
#
tab_status_route()
{
cat <<EOT
<frame Routing>
	<frame Nameservers>
		<text wrap="false" width-chars="58">
			<input>cat /etc/resolv.conf</input>
		</text>
	</frame>
	<frame Routing table>
		<text wrap="false" width-chars="58">
			<input>route</input>
		</text>
	</frame>
	<frame Arp table>
		<text wrap="false" width-chars="58">
			<input>arp</input>
		</text>
	</frame>
</frame>
EOT
}

#
# Status servers
#
tab_status_servers()
{
local servers
servers="dropbear sshd inetd dhcpd dnsd named rsync httpd mbd vnc pppd knock"
cat <<EOT
<frame Servers>
	<text wrap="false" width-chars="58">
		<input>for i in $servers; do ps ww | grep \$i | grep -v grep | fold -s; done</input>
	</text>
</frame>
EOT
}

data_status()
{
cat <<EOT
ifconfig    Network\ interfaces   tab_status_iface
iwconfig    Wireless\ interfaces  tab_status_wifi_iface
iwlist      Wireless\ networks    tab_status_wifi_net
route       Routing               tab_status_route
ps          Servers               tab_status_servers
EOT
}

#
# Status
#
tab_status()
{
use_tab data_status
}

#
# Interfaces list from detect_interfaces()
#
tab_connections()
{
cat <<EOT
	<vbox>
		<tree>
			<width>520</width><height>120</height>
			<variable>INTERFACE_LIST</variable>
			<label>Interface|Type|Status</label>
			<input icon_column="0">$0 detect_interfaces</input>
			<action>refresh:INTERFACE_LIST</action>
		</tree>
	</vbox>
EOT
}

#
# DHCP
#
tab_udhcpc()
{
cat <<EOT
<frame Udhcpc (busybox)>
	<text width-chars="58">
		<label> "Ethernet (cable) default connection." </label>
	</text>
	<hbox>
		<text use-markup="true">
			<label>"<b>Interface:</b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$INTERFACE"</input>
			<variable>INTERFACE</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Options:  </b>"</label>
		</text>
		<entry>
			<default>-b</default>
			<variable>UDHCPC_OPTS</variable>
		</entry>
$(helpbutton udhcpc 80x30)
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Script:     </b>"</label>
		</text>
		<entry editable="false">
			<default>/usr/share/udhcpc/default.script</default>
			<variable>UDHCPC_SCRIPT</variable>
		</entry>
$(editbutton \$UDHCPC_SCRIPT)
	</hbox>
	<hbox>
		<button>
			<label>Start</label>
			<input file icon="forward"></input>
			<action>sed -i s/`cat /etc/network.conf | grep ^INTERFACE=`/INTERFACE="\$INTERFACE"/ /etc/network.conf</action>
			<action>sed -i s/DHCP="no"/DHCP="yes"/ /etc/network.conf</action>
			<action>sed -i s/STATIC="yes"/STATIC="no"/ /etc/network.conf</action>
			<action>udhcpc \$UDHCPC_OPTS -i \$INTERFACE -p /var/run/udhcpc.\$INTERFACE.pid</action>
			<action>refresh:INTERFACE_LIST</action>
		</button>
		<button>
			<label>Stop</label>
			<input file icon="stop"></input>
			<action>echo -n "Stopping interface : \$INTERFACE... "</action>
			<action>ifconfig \$INTERFACE down</action>
			<action>killall -q udhcpc; echo "done"</action>
			<action>refresh:INTERFACE_LIST</action>
		</button>
	</hbox>
</frame>
EOT
}

#
# Static/fixed IP settings.
#
tab_fixed_ip()
{
cat <<EOT
<frame Ethernet fixed IP>
	<hbox>
		<text use-markup="true">
			<label>"<b>Interface:   </b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$INTERFACE"</input>
			<variable>INTERFACE</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>IP:                </b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$IP"</input>
			<variable>IP</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Netmask:    </b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$NETMASK"</input>
			<variable>NETMASK</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Gateway:    </b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$GATEWAY"</input>
			<variable>GATEWAY</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>DNS server: </b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$DNS_SERVER"</input>
			<variable>DNS_SERVER</variable>
		</entry>
	</hbox>
	<hbox>
		<button>
		<label>Start</label>
			<input file icon="forward"></input>
			<action>ifconfig lo down</action>
			<action>ifconfig \$INTERFACE down</action>
			<action>sed -i s/`cat /etc/network.conf | grep ^INTERFACE=`/INTERFACE="\$INTERFACE"/ /etc/network.conf</action>
			<action>sed -i s/DHCP="yes"/DHCP="no"/ /etc/network.conf</action>
			<action>sed -i s/WIFI="yes"/WIFI="no"/ /etc/network.conf</action>
			<action>sed -i s/STATIC="no"/STATIC="yes"/ /etc/network.conf</action>
			<action>sed -i s/`cat /etc/network.conf | grep ^IP=`/IP="\$IP"/ /etc/network.conf</action>
			<action>sed -i s/`cat /etc/network.conf | grep ^NETMASK=`/NETMASK="\$NETMASK"/ /etc/network.conf</action>
			<action>sed -i s/`cat /etc/network.conf | grep ^GATEWAY=`/GATEWAY="\$GATEWAY"/ /etc/network.conf</action>
			<action>sed -i s/`cat /etc/network.conf | grep ^DNS_SERVER=`/DNS_SERVER="\$DNS_SERVER"/ /etc/network.conf</action>
			<action>/etc/init.d/network.sh</action>
			<action>refresh:INTERFACE_LIST</action>
		</button>
		<button>
			<label>Stop</label>
			<input file icon="stop"></input>
			<action>ifconfig \$INTERFACE down</action>
			<action>refresh:INTERFACE_LIST</action>
		</button>
	</hbox>
</frame>
EOT
}

#
# PPPoe settings.
#
tab_pppoe()
{
cat <<EOT
<frame PPPoE>
	<hbox>
		<text use-markup="true">
			<label>"<b>Name:       </b>"</label>
		</text>
		<entry>
			<input>NAME=\$(grep -s ^name /etc/ppp/options); echo "\${NAME#* }"</input>
			<variable>NAME</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Username:</b>"</label>
		</text>
		<entry>
			<variable>USER</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Password:</b>"</label>
		</text>
		<entry>
			<variable>PASS</variable>
		</entry>
	</hbox>
	<hbox>
$(helpbutton pppd 80x30)
$(manbutton 8 pppd)
$(webbutton ppp)
		<button>
			<input file icon="accessories-text-editor"></input>
			<label>Tune</label>
			<action>[ -n "\$NAME" ] && sed -i "s/^name .*/name \$NAME/" /etc/ppp/options</action>
			<action type="lauch">leafpad /etc/ppp/options</action>
		</button>
		<button>
			<label>Start</label>
			<input file icon="forward"></input>
			<action>[ -n "\$USER" ] && grep -qs "^\"\$USER\"" /etc/ppp/pap-secrets
			&& echo "\"\$USER\"	*	\"\$PASS\"" >> /etc/ppp/pap-secrets</action>
			<action>[ -n "\$USER" ] && grep -qs "^\"\$USER\"" /etc/ppp/chap-secrets
			&& echo "\"\$USER\"	*	\"\$PASS\"" >> /etc/ppp/chap-secrets</action>
			<action>[ -n "\$NAME" ] && sed -i "s/^name .*/name \$NAME/" /etc/ppp/options</action>
			<action>killall udhcpc</action>
			<action>sed -i "s/DHCP=\"yes\"/DHCP=\"no\"/" /etc/network.conf</action>
			<action>sed -i "s/PPPOE=\"no\"/PPPOE=\"yes\"/" /etc/network.conf</action>
			<action>pppd \$INTERFACE &</action>
		</button>
		<button>
			<label>Stop</label>
			<input file icon="stop"></input>
			<action>sed -i "s/PPPOE=\"yes\"/PPPOE=\"no\"/" /etc/network.conf</action>
			<action>killall pppd</action>
		</button>
	</hbox>
</frame>
EOT
}

#
# PPP settings.
#
tab_ppp()
{
cat <<EOT
<frame PPP>
	<hbox>
		<text use-markup="true">
			<label>"<b>Username: </b>"</label>
		</text>
		<entry>
			<input>USER=\$(grep -s ^ACCOUNT= /etc/ppp/scripts/ppp-on | cut -f1); echo "\${USER#*=}"</input>
			<variable>USER</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Password: </b>"</label>
		</text>
		<entry>
			<input>PASS=\$(grep -s ^PASSWORD= /etc/ppp/scripts/ppp-on | cut -f1); echo "\${PASS#*=}"</input>
			<variable>PASS</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Telephone:</b>"</label>
		</text>
		<entry>
			<input>PHONE=\$(grep -s ^TELEPHONE= /etc/ppp/scripts/ppp-on | cut -f1); echo "\${PHONE#*=}"</input>
			<variable>PHONE</variable>
		</entry>
	</hbox>
	<hbox>
$(helpbutton pppd 80x30)
$(manbutton 8 pppd)
$(webbutton ppp)
		<button>
			<input file icon="accessories-text-editor"></input>
			<label>Tune</label>
			<action>[ -n "\$NAME" ] && sed -i "s/^ACCOUNT=.*/ACCOUNT=\$NAME/" /etc/ppp/scripts/ppp-on</action>
			<action>[ -n "\$PASS" ] && sed -i "s/^PASSWORD=.*/PASSWORD=\$PASS/" /etc/ppp/scripts/ppp-on</action>
			<action>[ -n "\$PHONE" ] && sed -i "s/^TELEPHONE=.*/TELEPHONE=\$PHONE/" /etc/ppp/scripts/ppp-on</action>
			<action type="lauch">leafpad /etc/ppp/scripts/ppp-on</action>
		</button>
		<button>
			<label>Start</label>
			<input file icon="forward"></input>
			<action>[ -n "\$USER" ] && grep -qs "^\"\$USER\"" /etc/ppp/pap-secrets
			&& echo "\"\$USER\"	*	\"\$PASS\"" >> /etc/ppp/pap-secrets</action>
			<action>[ -n "\$USER" ] && grep -qs "^\"\$USER\"" /etc/ppp/chap-secrets
			&& echo "\"\$USER\"	*	\"\$PASS\"" >> /etc/ppp/chap-secrets</action>
			<action>[ -n "\$NAME" ] && sed -i "s/^name .*/name \$NAME/" /etc/ppp/options</action>
			<action>/etc/ppp/scripts/ppp-off</action>
			<action>/etc/ppp/scripts/ppp-on &</action>
		</button>
		<button>
			<label>Stop</label>
			<input file icon="stop"></input>
			<action>/etc/ppp/scripts/ppp-off</action>
		</button>
	</hbox>
</frame>
EOT
}

#
# System Wide configuration.
#
tab_system()
{
cat <<EOT
<frame Configuration files>
      <hbox>
        <text use-markup="true">
          <label>"<b>Hosts            :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/hosts</default>
          <variable>HOSTS</variable>
        </entry>
$(editbutton \$HOSTS)
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Host name    :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/hostname</default>
          <variable>HOSTNAME</variable>
        </entry>
$(editbutton \$HOSTNAME)
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Network       :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/network.conf</default>
          <variable>CONFIG_FILE</variable>
        </entry>
$(editbutton \$CONFIG_FILE)
      </hbox>
      <hbox>
        <button>
          <label>Restart</label>
          <input file icon="reload"></input>
          <action>echo -n "Stopping interface : \$INTERFACE... "</action>
          <action>ifconfig \$INTERFACE down</action>
          <action>killall -q udhcpc; echo "done"</action>
          <action>/etc/init.d/network.sh restart</action>
        </button>
      </hbox>
</frame>
EOT
}

#
# ssh/ppp
#
tab_sshppp()
{
cat <<EOT
    <frame Virtual Private Network with PPP/SSH>
      <hbox>
        <text use-markup="true">
          <label>"<b>Peer          :</b>"</label>
        </text>
        <entry>
          <variable>DROPBEAR_PEERVPN</variable>
	  <default>user@elsewhere</default>
        </entry>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Local IP     :</b>"</label>
        </text>
        <entry>
          <variable>DROPBEAR_LOCAL</variable>
	  <default>192.168.254.1</default>
        </entry>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Remote IP :</b>"</label>
        </text>
        <entry>
          <variable>DROPBEAR_REMOTE</variable>
	  <default>192.168.254.2</default>
        </entry>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Route(s)    :</b>"</label>
        </text>
        <entry>
          <variable>DROPBEAR_ROUTE</variable>
	  <default>192.168.10.0/24 192.168.20.0/28</default>
        </entry>
      </hbox>
      <hbox>
$(wikibutton http://wiki.slitaz.org/doku.php?id=quickstart:vpn)
        <button>
          <input file icon="forward"></input>
	  <label>Connect</label>
	  <action>$0 call vpnssh \$DROPBEAR_PEERVPN \$DROPBEAR_LOCAL \$DROPBEAR_REMOTE "\$DROPBEAR_ROUTE" &</action>
        </button>
        <button>
          <input file icon="stop"></input>
	  <label>Disconnect</label>
	  <action>$0 call killvpnssh</action>
        </button>
        <button>
          <input file icon="go-up"></input>
	  <label>Send key</label>
	  <action>$xterm -geometry 80x10 -title "Send key" -e "$0 call sendsshkey \$DROPBEAR_PEERVPN; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
      </hbox>
    </frame>
EOT
}

#
# PPTP client
#
#FIXME
#cat > /etc/ppp/peers/$TUNNEL <<EOT
#pty "pptp $SERVER --nolaunchpppd"
#lock
#noauth
#nobsdcomp
#nodeflate
#name $DOMAIN\\$USERNAME
#remotename $TUNNEL
#ipparam $TUNNEL
#$(encryption && echo "require-mppe-128")
#EOT
#pppd call $TUNNEL updetach

tab_pptp()
{
[ -f /etc/ppp/options.pptp ] || cat >> /etc/ppp/options.pptp <<EOT
lock noauth nobsdcomp nodeflate remotename PPTP
EOT
[ -f /etc/ppp/pptp-servers ] || cat >> /etc/ppp/pptp-servers <<EOT
# PPTP servers list
EOT
cat <<EOT
    <frame Virtual Private Network with PPTP>
      <hbox>
        <text use-markup="true">
          <label>"<b>Server:</b>"</label>
        </text>
        <combobox>
          <variable>PPTP_SERVER</variable>
EOT
grep -v ^\# /etc/ppp/pptp-servers | \
awk '{ printf "          <item>%s</item>\n",$1 }'
cat <<EOT
          <item>SERVER</item>
        </combobox>
$(editbutton /etc/ppp/pptp-servers)
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>   User:</b>"</label>
        </text>
        <combobox>
          <variable>PPTP_USER</variable>
EOT
grep '\\' /etc/ppp/chap-secrets | grep -v ^\# | \
awk '{ printf "          <item>%s</item>\n",$1 }'
cat <<EOT
          <item>DOMAIN\USERNAME</item>
        </combobox>
$(editbutton /etc/ppp/chap-secrets)
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Password:</b>"</label>
        </text>
        <entry>
          <variable>PPTP_PASS</variable>
        </entry>
      </hbox>
      <hbox>
      	<checkbox>
	  <label>Encryption</label>
	  <variable>PPTP_ENC</variable>
	  <default>true</default>
      	</checkbox>
$(helpbutton pptp 80x35)
	<button>
	  <input file icon="accessories-text-editor"></input>
	  <label>Options</label>
	  <action type="lauch">leafpad /etc/ppp/options.pptp</action>
	</button>
$(webbutton pptpclient)
        <button>
          <input file icon="forward"></input>
	  <label>Connect</label>
	  <action>grep -qs ^\$PPTP_USER /etc/ppp/chap-secrets || \
echo "\$PPTP_USER PPTP \"$PPTP_PASS\" *" >> /etc/ppp/chap-secrets</action>
	  <action>grep -qs ^\$PPTP_SERVER /etc/ppp/pptp-servers || \
echo "\$PPTP_SERVER" >> /etc/ppp/pptp-servers</action>
	  <action>PASS=""; [ -n "\$PPTP_PASS" ] && PASS="password \$PASS"; \
ENC=""; [ x\$PPTP_ENC == xtrue ] && ENC="require-mppe-128"; \
/usr/sbin/pptp \$PPTP_SERVER file /etc/ppp/options.pptp \$ENC user \$PPTP_USER \$PASS &
	  </action>
        </button>
        <button>
          <input file icon="stop"></input>
	  <label>Disconnect</label>
	  <action>killall pptp</action>
        </button>
      </hbox>
    </frame>
EOT
}

#
# Cisco EasyVPN
#
tab_easyvpn()
{
cat <<EOT
    <frame Cisco EasyVPN>
<hbox>
	<text use-markup="true">
		<label>"<b>VPNC_OPTIONS</b>"</label>
	</text>
	<entry editable="false">
EOT
[ -n "$VPNC_OPTIONS" ] && cat << EOT
		<default>$tmp</default>
EOT
	cat << EOT
		<variable>VPNC_OPTS</variable>
	</entry>
$(helpbutton vpnc 80x30)
<button>
	<input file icon="help"></input>
	<label>more</label>
	<action>$xterm -geometry 80x40 -title "vpnc help (q to quit)" -e "$(which vpnc) --long-help 2>&1 | less"</action>
</button>
$(editbutton /etc/daemons.conf)
</hbox>
$(configfile /etc/vpnc/default.conf VPNC_CONF)
$(datafile "/etc/vpnc/vpnc-script" VPNC_SCRIPT 'Script:')
      <hbox>
	$(startstopdaemon vpnc)
      </hbox>
     </frame>
EOT
}

#
# OpenVPN
#
tab_openvpn()
{
local i
local n
cat <<EOT
<frame OpenVPN>
EOT
n=1
for i in /etc/openvpn/*.conf ; do
	[ -f $i ] || continue
	configfile $i OPENVPN_CONF$n
	n=$(($n + 1))
done
cat <<EOT
      <hbox>
	$(helpbutton openvpn 80x40)
	$(startstopdaemon openvpn)
      </hbox>
</frame>
EOT
}

data_vpn()
{
cat <<EOT
$([ -x /usr/sbin/dropbear -o -x /usr/sbin/sshd ] && echo "pppd" ||
echo "#")   PPP/SSH       tab_sshppp
pptp        PPTP          tab_pptp
vpnc        EasyVPN       tab_easyvpn
openvpn     OpenVPN       tab_openvpn
EOT
}

#
# VPN
#
tab_vpn()
{
use_tab data_vpn
}

sshremote()
{
	cat << EOT
      <hbox>
        <text use-markup="true">
          <label>"<b>Remote  :</b>"</label>
        </text>
        <entry>
          <variable>$2</variable>
	  <default>user@elsewhere</default>
        </entry>
        <button>
          <input file icon="utilities-terminal"></input>
	  <label>Connect</label>
	  <action>xterm -fa MiscFixed -fs 11 -bg black -fg white -geometry 80x25 -title "$1" -e "$1 \$$2 ; echo -e \"----\nENTER to continue...\" && read close" &</action>
        </button>
        <button>
          <input file icon="go-up"></input>
	  <label>Send key</label>
	  <action>$xterm -geometry 80x10 -title "send ssh key" -e "$0 call sendsshkey \$$2; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
      </hbox>
EOT
}

#
# Dropbear client & server
#
tab_dropbear()
{
. /etc/daemons.conf
set -- $DROPBEAR_OPTIONS
while [ -n "$2" ]; do
  [ "$1" = "-b" ] && DROPBEAR_BANNER="$2" && break
  shift
done

cat <<EOT
     <frame Dropbear>
$(helpdaemon dropbear 80x30 DROPBEAR_OPTIONS DROPBEAR_OPTS)
$(datafile /etc/dropbear/banner DROPBEAR_BANNER 'Banner   :')
$(sshremote DROPBEAR_PEER 'dbclient -i /etc/dropbear/dropbear_rsa_host_key')
      <hbox>
	$(startstopdaemon dropbear)
      </hbox>
     </frame>
EOT
}

#
# OpenSSH client & server
#
tab_openssh()
{
cat <<EOT
    <frame OpenSSH>
$(helpdaemon sshd 80x30 OPENSSH_OPTIONS OPENSSH_OPTS 8 nohelp)
$(configfile /etc/ssh/sshd_config OPENSSH_CONF '5 sshd_config')
$(sshremote OPENSSH_PEER 'ssh -i /etc/ssh/ssh_host_rsa_key')
      <hbox>
	$(startstopdaemon openssh)
      </hbox>
     </frame>
EOT
}

data_sshd()
{
cat <<EOT
dropbear    Dropbear      tab_dropbear
sshd        OpenSSH       tab_openssh
EOT
}

#
# SSH tab
#
tab_sshd()
{
use_tab data_sshd
}

#
# Busybox Inetd
#
tab_inetd()
{
cat <<EOT
<frame Inetd (busybox)>
$(helpdaemon inetd 60x14 INETD_OPTIONS INETD_OPTS)
$(configfile /etc/inetd.conf INETD_CONF)
      <hbox>
EOT
for i in $(grep bin /etc/inetd.conf | awk '{ print $6}'); do
  i=$(basename $i)
  helpbutton $i 60x19 $i
done
cat <<EOT
	$(startstopdaemon inetd)
      </hbox>
     </frame>
EOT
}

#
# ZeroConf
#
tab_zcip()
{
if [ -x /sbin/zcip -a -z "$ZCIP_OPTIONS" ]; then
	ZCIP_OPTIONS="eth0 /etc/zcip.script"
	cat >> /etc/daemons.conf <<EOT
# ZeroConf options
ZCIP_OPTIONS="$ZCIP_OPTIONS"

EOT
fi
cat <<EOT
     <frame Zcip (busybox)>
$(helpdaemon zcip 60x14 ZCIP_OPTIONS ZCIP_OPTS)
$(datafile /etc/zcip.script CZIP_SCRIPT 'Script :')
      <hbox>
	$(startstopdaemon zcip)
      </hbox>
     </frame>
EOT
}

#
# Dhcpd ISC
#
tab_isc_dhcpd()
{
cat <<EOT
    <frame Dhcpd (ISC)>
$(helpdaemon dhcpd 60x14 DHCPD_OPTIONS DHCPD_OPTS)
$(configfile /etc/dhcpd.conf DHCPD_CONF)
      <hbox>
	$(startstopdaemon dhcpd dhcp)
      </hbox>
     </frame>
EOT
}

#
# Dhcpd Busybox
#
tab_udhcpd()
{
cat <<EOT
    <frame Udhcpd (busybox)>
$(helpdaemon udhcpd 60x14 UDHCPD_OPTIONS UDHCPD_OPTS)
$(configfile /etc/udhcpd.conf UDHCPD_CONF)
      <hbox>
	$(startstopdaemon udhcpd busybox)
      </hbox>
     </frame>
EOT
}

data_dhcpd()
{
cat <<EOT
udhcpd      Udhcpd        tab_udhcpd
dhcpd       Dhcpd         tab_isc_dhcpd
dnsmasq     DNSmasq       tab_dnsmasq
EOT
}

#
# dhcpd tab
#
tab_dhcpd()
{
use_tab data_dhcpd
}

#
# PXE forwarder
#
tab_gpxe()
{
cat <<EOT
<frame PXE forwarder>
      <hbox>
        <text use-markup="true">
          <label>"<b>WEB boot URL(s) :</b>"</label>
        </text>
      </hbox>
      <hbox>
        <entry editable="true">
          <default>
"$(dd bs=1 skip=5 count=255 if=/usr/share/boot/gpxe.pxe 2> /dev/null | strings)"
          </default>
          <variable>WEB_PXE</variable>
        </entry>
      </hbox>
      <hbox>
$(wikibutton http://wiki.slitaz.org/doku.php?id=quickstart:pxe)
        <button>
          <input file icon="go-jump"></input>
	  <label>Install</label>
	  <action>
	echo -n "\$WEB_PXE" | cat - /dev/zero | \
	dd conv=notrunc bs=1 seek=5 count=255 of=/usr/share/boot/gpxe.pxe
	  </action>
	  <action>
	sed -i "s|tftpd -r .*\$|tftpd -r /usr/share/boot|" /etc/inetd.conf
	  </action>
	  <action> sed -i "/^boot_file/d" \$UDHCPD_CONF </action>
	  <action> echo "boot_file gpxe.pxe" >> \$UDHCPD_CONF </action>
	  <action> /etc/init.d/inetd restart </action>
	  <action> /etc/init.d/udhcpd restart </action>
	<action>sed -i "s/RUN_DAEMONS=\\"\\(.*\\)inetd \?\\(.*\\)\\"/RUN_DAEMONS=\\"\1\2\\"/" /etc/rcS.conf</action>
	<action>sed -i "s/RUN_DAEMONS=\\"\\(.*\\)udhcpd \?\\(.*\\)\\"/RUN_DAEMONS=\\"\1\2\\"/" /etc/rcS.conf</action>
	<action>sed -i "s/RUN_DAEMONS=\\"\\(.*\\)\\"/RUN_DAEMONS=\\"udhcpd inetd \1\\"/" /etc/rcS.conf</action>
        </button>
      </hbox>
</frame>
EOT
}

#
# PXE server
#
tab_pxelinux()
{
cat <<EOT
<frame PXElinux>
      <hbox>
        <text wrap="true">
          <label>
"Launch Dhcpd and Inetd with Tftpd to start the PXE service."
          </label>
        </text>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Configuration :</b>"</label>
        </text>
        <entry editable="false">
          <default>
$(grep bin/tftpd /etc/inetd.conf | awk '{ print $NF }')/pxelinux.cfg/default
          </default>
          <variable>PXE_CONF</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
	  <action>dir=\$(dirname \$PXE_CONF); [ -d \$dir ] || mkdir -p \$dir</action>
	  <action>lzma d /usr/share/boot/pxelinux.0.lzma \$(dirname \$PXE_CONF)/../pxelinux.0</action>
	  <action>sed -i "/^boot_file/d" \$UDHCPD_CONF</action>
	  <action>echo "boot_file pxelinux.0" >> \$UDHCPD_CONF</action>
	  <action>grep -q "^siaddr" \$UDHCPD_CONF || echo "siaddr \$(ifconfig \$INTERFACE | grep inet.ad | cut -d: -f2 | cut -d\  -f1)" >> \$UDHCPD_CONF</action>
	  <action>[ -f \$PXE_CONF ] || echo -e "label linux\n\tkernel bzImage\n\tappend initrd=rootfs.gz rw root=/dev/null vga=normal" > \$PXE_CONF</action>
          <action type="lauch">leafpad \$PXE_CONF</action>
        </button>
      </hbox>
      <hbox>
$(wikibutton http://wiki.slitaz.org/doku.php?id=quickstart:pxe)
        <button>
          <input file icon="system-file-manager"></input>
	  <label>Network boot files</label>
	  <action>$xterm -geometry 100x25 -title "Network boot files (q to quit)" -e "find \$(dirname \$(dirname \$PXE_CONF)) -exec ls -ld {} \; | less"</action>
        </button>
      </hbox>
     </frame>
EOT
}

data_pxe()
{
[ -f /usr/share/boot/pxelinux.0.lzma ] && cat <<EOT
true        PXElinux          tab_pxelinux
EOT
[ -f /usr/share/boot/gpxe.pxe ] && cat <<EOT
true        PXE\ forwarder    tab_gpxe
EOT
}

#
# PXE
#
tab_pxe()
{
use_tab data_pxe
}

#
# Busybox dnsd
#
tab_dnsd()
{
cat <<EOT
    <frame Dnsd (busybox master dns)>
$(helpdaemon dnsd 80x15 DNSD_OPTIONS DNSD_OPTS)
$(configfile /etc/dnsd.conf DNSD_CONF)
      <hbox>
	$(startstopdaemon dnsd busybox)
      </hbox>
     </frame>
EOT
}

#
# ISC Bind
#
tab_named()
{
cat <<EOT
    <frame Named (ISC bind)>
$(helpdaemon named 80x10 NAMED_OPTIONS NAMED_OPTS)
$(configfile /etc/bind/named.conf.local NAMED_CONF)
      <hbox>
	$(startstopdaemon named bind)
      </hbox>
     </frame>
EOT
}

#
# DNSmasq 
#
tab_dnsmasq()
{
cat <<EOT
    <frame DNSmasq (DNS/DHCP/TFTP)>
$(helpdaemon dnsmasq 100x34 DNSMASQ_OPTIONS DNSMASQ_OPTS)
$(configfile /etc/dnsmasq.conf DNSMASQ_CONF)
      <hbox>
	$(startstopdaemon dnsmasq)
      </hbox>
     </frame>
EOT
}

data_dns()
{
cat <<EOT
dnsd        Dnsd          tab_dnsd
named       Named         tab_named
dnsmasq     DNSmasq       tab_dnsmasq
#TODO sheerdns    Sheerdns      tab_sheerdns
EOT
}

#
# dns tab
#
tab_dns()
{
use_tab data_dns
}

#
# rsync server
#
tab_rsync()
{
cat <<EOT
     <frame Rsync>
$(helpdaemon rsync 80x30 RSYNCD_OPTIONS RSYNCD_OPTS 1)
$(configfile /etc/rsyncd.conf RSYNCD_CONF)
$(datafile /etc/rsyncd.secrets RSYNCD_SECRETS 'Secrets file     :')
      <hbox>
	$(startstopdaemon rsyncd rsync)
      </hbox>
     </frame>
EOT
}

php_ini()
{
[ -f /etc/php.ini ] && cat <<EOT
      <frame PHP>
$(configfile /etc/php.ini PHP_CONF)
      </frame>
EOT
}

#
# Apache server
#
tab_apache()
{
cat <<EOT
     <frame Apache>
$(configfile /etc/apache/httpd.conf HTTPD_CONF)
$(php_ini)
      <hbox>
	$(startstopdaemon apache)
      </hbox>
     </frame>
EOT
}

#
# Lighttpd server
#
tab_lighttpd()
{
cat <<EOT
     <frame Lighttpd>
$(helpdaemon lighttpd 80x15 LIGHTTPD_OPTIONS LIGHTTPD_OPTS)
$(configfile /etc/lighttpd/lighttpd.conf LIGHTTPD_CONF)
$(php_ini)
      <hbox>
	$(startstopdaemon lighttpd)
      </hbox>
     </frame>
EOT
}

#
# Busybox httpd server
#
tab_httpd()
{
cat <<EOT
     <frame httpd (busybox)>
$(helpdaemon httpd 80x30 HTTPD_OPTIONS HTTPD_OPTS)
$(configfile /etc/httpd.conf HTTPD_CONF)
$(php_ini)
      <hbox>
	$(startstopdaemon httpd busybox)
      </hbox>
     </frame>
EOT
}

data_http()
{
cat <<EOT
/etc/apache/httpd.conf  Apache          tab_apache
lighttpd                Lighttpd        tab_lighttpd
/etc/httpd.conf         Busybox\ httpd  tab_httpd
EOT
}

#
# http tab
#
tab_http()
{
use_tab data_http
}

#
# Samba servers
#
tab_wins()
{
cat <<EOT
     <frame Samba: smbd & nmbd>
$(configfile /etc/samba/smb.conf SMBD_CONF)
      <hbox>
        <button>
          <label>Reload</label>
          <input file icon="reload"></input>
          <action>/etc/init.d/samba reload</action>
        </button>
	$(startstopdaemon samba)
      </hbox>
     </frame>
EOT
}

#
# x11vnc
#
tab_vnc()
{
cat <<EOT
     <frame x11vnc>
$(helpdaemon x11vnc 80x30 X11VNC_OPTIONS X11VNC_OPTS)
      <hbox>
        <text use-markup="true">
          <label>"<b>New password</b>"</label>
        </text>
        <entry>
          <variable>X11VNC_PASSWD</variable>
        </entry>
        <button>
          <input file icon="reload"></input>
          <label>Update</label>
          <action>x11vnc -storepasswd \$X11VNC_PASSWD /etc/vnc.secret</action>
        </button>
      </hbox>
      <hbox>
	$(startstopdaemon x11vnc)
      </hbox>
     </frame>
EOT
}

#
# knock server
#
tab_knockd()
{
cat <<EOT
    <frame Knockd: trigger backdoors>
$(helpdaemon knockd 80x15 KNOCK_OPTIONS KNOCK_OPTS)
$(configfile /etc/knockd.conf KNOCKD_CONF)
      <hbox>
	$(startstopdaemon knockd knock)
      </hbox>
     </frame>
EOT
}

data_servers()
{
local $pxe
pxe="#"
[ -x /usr/bin/tftpd -a -x /usr/sbin/udhcpd ] && pxe="inetd"
[ -x /usr/sbin/dnsmasq ] && pxe="true"
[ -f /usr/share/boot/pxelinux.0.lzma -o /usr/share/boot/gpxe.pxe ] || pxe="#"
cat <<EOT
$([ -x /usr/sbin/dropbear -o -x /usr/sbin/sshd ] && echo "true" ||
echo "#")   SSH           tab_sshd
inetd       Inetd         tab_inetd
zcip        ZeroConf      tab_zcip
$([ -x /usr/sbin/udhcpd -o -x /usr/sbin/dhcpd -o -x /usr/sbin/dnsmasq ] && 
echo "true" ||
echo "#")   DHCP          tab_dhcpd
$pxe        PXE           tab_pxe
dnsmasq     tftpd         tab_dnsmasq
$([ -x /usr/sbin/dnsd -o -x /usr/sbin/named -o -x /usr/sbin/dnsmasq ] && 
echo "true" ||
echo "#")   DNS           tab_dns
rsync       Rsync         tab_rsync
$([ -x /usr/sbin/lighttpd -o -x /usr/bin/httpd ] && echo "true" || 
echo "#")   HTTP          tab_http
smbd        WINS          tab_wins
x11vnc      VNC           tab_vnc 
knockd      Knock         tab_knockd
#TODO+callback pppd        Pppd          tab_pppd
#TODO pptpd       PoPToP        tab_poptop
#TODO squid *sql openldap 
EOT
}

#
# Servers tab
#
tab_servers()
{
use_tab data_servers
}

#
# Firewall tab
#
tab_firewall()
{
cat <<EOT
    <frame Iptables>
$(configfile /etc/iptables.conf IPTABLES_CONF)
      <hbox>
$(helpbutton iptables 80x30)
$(manbutton 8 iptables)
$(webbutton iptables)
        <button>
          <label>Load</label>
          <input file icon="reload"></input>
          <action>cat \$IPTABLES_CONF | /sbin/iptables-restore</action>
        </button>
        <button>
          <label>Save</label>
          <input file icon="go-jump"></input>
          <action>/sbin/iptables-save > \$IPTABLES_CONF</action>
        </button>
      </hbox>
     </frame>
EOT
}

#
# knock client
#
tab_knock()
{
cat <<EOT
    <frame Knock>
    <hbox>
      <text use-markup="true">
        <label>"<b>Options : </b>"</label>
      </text>
      <entry editable="true">
        <variable>KNOCK_OPTS</variable>
	<default>myserver.example.com 123:tcp 456:udp 789:tcp</default>
      </entry>
    </hbox>
    <hbox>
$(helpbutton knock 80x12)
      <button>
        <label>Start</label>
        <input file icon="forward"></input>
        <action>knock \$KNOCK_OPTS</action>
      </button>
    </hbox>
    </frame>
EOT
}

#
# ether-wake
#
tab_etherwake()
{
cat <<EOT
    <frame Ether-wake (busybox)>
    <hbox>
      <text use-markup="true">
        <label>"<b>Machines :</b>"</label>
      </text>
      <entry editable="false">
        <default>/etc/ethers</default>
        <variable>ETHERS</variable>
      </entry>
      <button>
        <input file icon="accessories-text-editor"></input>
        <action>[ -s \$ETHERS ] || echo "#00:01:02:03:04:05 mystation" >\$ETHERS</action>
        <action type="lauch">leafpad \$ETHERS</action>
      </button>
    </hbox>
    <hbox>
      <text use-markup="true">
        <label>"<b>Options : </b>"</label>
      </text>
      <entry editable="true">
        <variable>ETHERWAKE_OPTS</variable>
      </entry>
$(helpbutton ether-wake 80x15)
	<button>
		<label>Start</label>
		<input file icon="forward"></input>
		<action>ether-wake \$ETHERWAKE_OPTS</action>
	</button>
    </hbox>
    <frame>
    <hbox>
EOT
if which ethtool > /dev/null; then
cat <<EOT
        <text use-markup="true">
          <label>"<b>Interface:</b>"</label>
        </text>
        <combobox>
          <variable>ETHERWAKE_ETH</variable>
EOT
(cd /sys/class/net ; [ -d eth0 ] && ls -d eth* ) | \
awk '{ printf "          <item>%s</item>\n",$1 }'
cat <<EOT
        </combobox>
	<button>
		<label>Enable Wake On LAN</label>
		<input file icon="forward"></input>
		<action>ethtool -s \$ETHERWAKE_ETH wok g</action>
	</button>
EOT
else
cat <<EOT
    <vbox>
	<text wrap="true" width-chars="58">
			<label>
"Ethtool utility can enable Wake-on-lan 
feature on many Ethernet cards.
"
			</label>
	</text>
$(installbox ethtool)
    </vbox>
EOT
fi
cat <<EOT
    </hbox>
    </frame>
    </frame>
EOT
}

data_main()
{
cat <<EOT
#program    tab name      function
ifconfig    Connections   tab_connections
udhcpc      DHCP          tab_udhcpc
ifconfig    Static\ IP    tab_fixed_ip
pppoe       PPPoE         tab_pppoe
pppd        PPP           tab_ppp
true        System\ wide  tab_system
true        VPN           tab_vpn
true        Servers       tab_servers
iptables    Firewall      tab_firewall
knock       Knock         tab_knock
ether-wake  Etherwake     tab_etherwake
EOT
}

tab_main()
{
use_tab data_main
}

######################################################
#
# Netbox GUI
#
######################################################

# English/French help dialog.
export HELP='
<window title="Network status" icon-name="network-wire">
<vbox>
	<text use-markup="true">
		<label>
"
<b>SliTaz - Netbox</b>"
		</label>
	</text>
	<frame English>
		<text wrap="true" width-chars="58">
			<label>
"Netbox lets you manage network connections by getting a dynamic IP by 
DHCP or a static IP and setup servers. Netbox can start or stop
networking, configure network interfaces or directly edit files."
			</label>
		</text>
	</frame>
	<frame Francais>
		<text wrap="true" width-chars="58">
			<label>
"Netbox vous permet de gerer les connexions reseau avec une IP
statique ou en obtenant une IP dynamique par DHCP, et de parametrer
les serveurs. Netbox peut demarrer ou arreter le reseau, configurer
les interfaces reseau ou editer directement les fichiers."
			</label>
		</text>
	</frame>
</vbox>
</window>
'

# Interface status with ifconfig without arguments to show all
# active connections.
#
export IFCONFIG="
<window title=\"Network status\" icon-name=\"network-wire\">
<vbox>
$(tab_status)
	<hbox>
		<button>
			<input file icon=\"gtk-close\"></input>
			<action type=\"closewindow\">IFCONFIG</action>
		</button>
	</hbox>
</vbox>
</window>"

# The main dialog with notebook, start/stop buttons and all options.
# Note that /etc/network.conf is seded when an interface is activated.
#
head='
<window title="SliTaz Netbox Manager" icon-name="network-wired">
	<vbox>

		<hbox>
			<text use-markup="true">
				<label>"<b>Network/Server Manager</b>"</label>
			</text>
			<pixmap>
				<input file>/usr/share/pixmaps/netbox.png</input>
			</pixmap>
		</hbox>
'
bottom='
		<hbox>
			<button>
				<label>Wireless manager</label>
				<input file icon="network-wireless"></input>
				<action>wifibox &</action>
			</button>
			<button>
				<label>Refresh list</label>
				<input file icon="reload"></input>
				<action>refresh:INTERFACE_LIST</action>
			</button>
			<button>
			<label>Full status</label>
				<input file icon="dialog-information"></input>
				<action type="launch">IFCONFIG</action>
			</button>
			<button help>
				<label>Help</label>
				<action type="launch">HELP</action>
			</button>
			<button>
				<label>Quit</label>
				<input file icon="exit"></input>
				<action type="exit">Exit</action>
			</button>
		</hbox>

	</vbox>
</window>
'

NET_BOX="${head}$(tab_main)${bottom}"

export NET_BOX

# TODO:  Modules(Network kernel modules)

# Only root can configure network.
check_root

# Configure and connect if button Connect was pressed.
if ! grep -qs ^name /etc/ppp/options ; then
	# Generate /etc/ppp/options
	cat > /etc/ppp/options << _EOT_
plugin rp-pppoe.so
name provider-ID
noipdefault
defaultroute
mtu 1492
mru 1492
lock
_EOT_
	# Generate /etc/ppp/pap-secrets
	cat > /etc/ppp/pap-secrets << _EOT_
# Secrets for authentication using PAP
# client	server	secret			IP addresses
_EOT_
	# Generate /etc/ppp/chap-secrets
	cat > /etc/ppp/chap-secrets << _EOT_
# Secrets for authentication using CHAP
# client	server	secret			IP addresses
_EOT_
fi
gtkdialog --center --program=NET_BOX | grep -a 'EXIT="restart"' && continue
exit 0
done
