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

# 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 call with args.
case "$1" in
	detect_interfaces|interfaces_List_actions)
		$1
		exit 0 ;;
	*)
		continue ;;
esac

# GUI functions
helpbutton()
{
	local label;
        label="<label>$3</label>"
	[ -n "$3" ] || label=""
	cat << EOT
<button>
	<input file icon="help"></input>$label
	<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry $2 -title "$1 help" -e "$(which $1) --help ; echo -e \\"----\\nENTER to continue...\\" && read close"</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 -fa MiscFixed -fs 11 -bg gray93 -fg black -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"
}

webbutton()
{
	[ -f "/var/lib/tazpkg/installed/$1/receipt" ] && cat << EOT
<button>
	<input file icon="browser"></input>
	<label>web</label>
	<action>. /var/lib/tazpkg/installed/$1/receipt ; firefox \$WEB_SITE &</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 -fa MiscFixed -fs 11 -bg gray93 -fg black -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

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

# 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 dynamic IP by DHCP
or 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>
'

# Interafce status with ifconfig without arguments to show all
# active connections.
#
export IFCONFIG='
<window title="Network status" icon-name="network-wire">
<vbox>
	<notebook labels="Network interfaces'
		[ -x /usr/sbin/iwlist ] && IFCONFIG="$IFCONFIG|Wireless interfaces|Wireless networks"
		tmp='|Routing|Servers">
		<frame Ifconfig>
			<text wrap="false" width-chars="58">
				<input>ifconfig</input>
			</text>
		</frame>'
		IFCONFIG="$IFCONFIG$tmp"
		tmp='
		<frame Iwconfig>
			<text wrap="false" width-chars="58">
				<input>iwconfig</input>
			</text>
		</frame>
		<frame Wireless networks>
			<text wrap="false" width-chars="58">
				<input>iwlist scan</input>
			</text>
		</frame>'
		[ -x /usr/sbin/iwlist ] && IFCONFIG="$IFCONFIG$tmp"
		tmp='
		<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>
		<frame Servers>
			<text wrap="false" width-chars="58">
				<input>for i in dropbear sshd inetd dhcpd dnsd named rsync httpd smbd nmbd x11vnc pppd knock; do ps ww | grep $i | grep -v grep | fold -s; done</input>
			</text>
		</frame>
	</notebook>
	<hbox>
		<button>
			<input file icon="gtk-close"></input>
			<action type="closewindow">IFCONFIG</action>
		</button>
	</hbox>
</vbox>
</window>'
IFCONFIG="$IFCONFIG$tmp"

# The main dialog with notebook, start/stop buttons and all options.
# Note that /etc/network.conf is seded when an interface is activated.
#
NET_BOX='
<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>'

# Notebook tabs.
tmp='<notebook labels="Connections|DHCP|Static IP|PPPoE|PPP|System wide|VPN|Servers'
NET_BOX="$NET_BOX$tmp"
[ -x /sbin/iptables ] && NET_BOX="$NET_BOX|Firewall"
[ -x /usr/bin/knock ] && NET_BOX="$NET_BOX|Knock"
tmp='|Etherwake">'

# Interfaces list from detect_interfaces()
tmp=${tmp}"
	<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>"

# DHCP
tmp=${tmp}'
<frame Udhcpc (busybox)>
	<text width-chars="58">
		<label>
"Ethernet (cable) default connexion.
"
		</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>
		<button>
			<input file icon="help"></input>
			<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x27 -title "udhcpc help" -e "udhcpc --help ; echo -e \"----\nENTER to continue...\" && read close"</action>
		</button>
	</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>
		<button>
			<input file icon="accessories-text-editor"></input>
			<action type="lauch">leafpad $UDHCPC_SCRIPT</action>
		</button>
	</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>'

# Static/fixed IP settings.
tmp=${tmp}'
<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>'

# Static/fixed IP actions (should go in a function).
tmp=${tmp}'
	<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>'

# PPPoe settings.
tmp=${tmp}'
<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>
		<button>
			<input file icon="help"></input>
			<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x30 -title "pppd help (q to quit)" -e "pppd --help 2>&1 | less"</action>
		</button>
		<button>
			<input file icon="browser"></input>
			<label>man</label>
			<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x24 -title "pppd man (q to quit)" -e ". /etc/profile; man 8 pppd"</action>
		</button>
		<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>'

# PPP settings.
tmp=${tmp}'
<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>
		<button>
			<input file icon="help"></input>
			<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x30 -title "pppd help (q to quit)" -e "pppd --help 2>&1 | less"</action>
		</button>
		<button>
			<input file icon="browser"></input>
			<label>man</label>
			<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x24 -title "pppd man (q to quit)" -e ". /etc/profile; man 8 pppd"</action>
		</button>
		<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>'

#
# TODO: Indent with tabs from there
#

# System Wide configuration.
NET_BOX="$NET_BOX$tmp"
tmp='<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>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $HOSTS</action>
        </button>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Host name    :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/hostname</default>
          <variable>HOSTNAME</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $HOSTNAME</action>
        </button>
      </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>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $CONFIG_FILE</action>
        </button>
      </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>

    <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>
        <button>
          <input file icon="browser"></input>
	  <label>Wiki</label>
	  <action>firefox http://wiki.slitaz.org/doku.php?id=quickstart:vpn &</action>
        </button>
        <button>
          <input file icon="forward"></input>
	  <label>Connect</label>
	  <action>netbox call vpnssh $DROPBEAR_PEERVPN $DROPBEAR_LOCAL $DROPBEAR_REMOTE "$DROPBEAR_ROUTE" &</action>
        </button>
        <button>
          <input file icon="stop"></input>
	  <label>Disconnect</label>
	  <action>netbox call killvpnssh</action>
        </button>
        <button>
          <input file icon="go-up"></input>
	  <label>Send key</label>
	  <action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x10 -title "Send key" -e "netbox call sendsshkey $DROPBEAR_PEERVPN; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
      </hbox>
    </frame>
    <notebook labels="'
NET_BOX="$NET_BOX$tmp"
tmp=''
[ -x /usr/sbin/dropbear -o \
  -x /usr/sbin/sshd     ] && NET_BOX="${NET_BOX}${tmp}SSH"   && tmp='|'
[ -x /usr/sbin/inetd    ] && NET_BOX="${NET_BOX}${tmp}Inetd" && tmp='|'
[ -x /sbin/zcip         ] && NET_BOX="${NET_BOX}${tmp}ZeroConf" && tmp='|'
[ -x /usr/sbin/udhcpd -o \
  -x /usr/sbin/dhcpd    ] && NET_BOX="${NET_BOX}${tmp}DHCP"  && tmp='|'
[ -f /usr/share/boot/pxelinux.0.lzma -a -x /usr/bin/tftpd -a \
  -x /usr/sbin/inetd -a -x /usr/sbin/udhcpd ] \
                          && NET_BOX="${NET_BOX}${tmp}PXE"   && tmp='|'
[ -x /usr/sbin/dnsd -o \
  -x /usr/sbin/named    ] && NET_BOX="${NET_BOX}${tmp}DNS"   && tmp='|'
[ -x /usr/bin/rsync     ] && NET_BOX="${NET_BOX}${tmp}Rsync" && tmp='|'
[ -x /usr/sbin/lighttpd -o \
  -x /usr/bin/httpd     ] && NET_BOX="${NET_BOX}${tmp}HTTP"  && tmp='|'
[ -x /usr/sbin/smbd     ] && NET_BOX="${NET_BOX}${tmp}WINS"  && tmp='|'
[ -x /usr/bin/x11vnc    ] && NET_BOX="${NET_BOX}${tmp}VNC"   && tmp='|'
[ -x /usr/sbin/knockd   ] && NET_BOX="${NET_BOX}${tmp}Knock" && tmp='|'
. /etc/daemons.conf
set -- $DROPBEAR_OPTIONS
while [ -n "$2" ]; do
  [ "$1" = "-b" ] && DROPBEAR_BANNER="$2" && break
  shift
done
NET_BOX="${NET_BOX}\">"
[ -x /usr/sbin/sshd -a -x /usr/sbin/dropbear ] && NET_BOX="${NET_BOX}
   <notebook labels=\"Dropbear|OpenSSH\">
"

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 -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x10 -title "send ssh key" -e "netbox call sendsshkey \$$2; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
      </hbox>
EOT
}

[ -x /usr/sbin/dropbear ] && NET_BOX="${NET_BOX}
     <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>
"
[ -x /usr/sbin/sshd ] && NET_BOX="${NET_BOX}
    <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>
"
[ -x /usr/sbin/sshd -a -x /usr/sbin/dropbear ] && NET_BOX="${NET_BOX}
   </notebook>
"
tmp="<frame Inetd (busybox)>
$(helpdaemon inetd 60x14 INETD_OPTIONS INETD_OPTS)
$(configfile /etc/inetd.conf INETD_CONF)
      <hbox>
"
for i in $(grep bin /etc/inetd.conf | awk '{ print $6}'); do
  i=$(basename $i)
  tmp="$tmp$(helpbutton $i 60x19 $i)"
done
[ -x /usr/sbin/inetd ] && NET_BOX="${NET_BOX}${tmp}
	$(startstopdaemon inetd)
      </hbox>
     </frame>
"
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
[ -x /sbin/zcip ] && NET_BOX="${NET_BOX}
     <frame Zcip (busybox)>
$(helpdaemon zcip 60x14 ZCIP_OPTIONS ZCIP_OPTS)
$(datafile /etc/zcip.script CZIP_SCRIPT 'Script :')
      <hbox>
	$(startstopdaemon zcip)
      </hbox>
     </frame>
"
[ -x /usr/sbin/dhcpd -a -x /usr/sbin/udhcpd ] && NET_BOX="${NET_BOX}
   <notebook labels=\"Dhcpd|Udhcpd\">
"
[ -x /usr/sbin/dhcpd   ] && NET_BOX="${NET_BOX}
    <frame Dhcpd (ISC)>
$(helpdaemon dhcpd 60x14 DHCPD_OPTIONS DHCPD_OPTS)
$(configfile /etc/dhcpd.conf DHCPD_CONF)
      <hbox>
	$(startstopdaemon dhcpd dhcp)
      </hbox>
     </frame>
"
[ -x /usr/sbin/udhcpd   ] && NET_BOX="${NET_BOX}
    <frame Udhcpd (busybox)>
$(helpdaemon udhcpd 60x14 UDHCPD_OPTIONS UDHCPD_OPTS)
$(configfile /etc/udhcpd.conf UDHCPD_CONF)
      <hbox>
	$(startstopdaemon udhcpd)
      </hbox>
     </frame>
"
[ -x /usr/sbin/dhcpd -a -x /usr/sbin/udhcpd ] && NET_BOX="${NET_BOX}
   </notebook>
"
tmp='<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>'
tmp="$tmp$(grep bin/tftpd /etc/inetd.conf | awk '{ print $NF }')"
tmp2='/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>grep -q "^boot_file" $UDHCPD_CONF || 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>
        <button>
          <input file icon="browser"></input>
	  <label>Wiki</label>
	  <action>firefox http://wiki.slitaz.org/doku.php?id=quickstart:pxe &</action>
        </button>
        <button>
          <input file icon="system-file-manager"></input>
	  <label>Network boot files</label>
	  <action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 100x25 -title "Network boot files (q to quit)" -e "find $(dirname $(dirname $PXE_CONF)) -exec ls -ld {} \; | less"</action>
        </button>
      </hbox>
     </frame>
'
[ -f /usr/share/boot/pxelinux.0.lzma -a -x /usr/bin/tftpd -a \
  -x /usr/sbin/inetd -a -x /usr/sbin/udhcpd ] && NET_BOX="$NET_BOX$tmp$tmp2"
[ -x /usr/sbin/dnsd -a -x /usr/sbin/named ] && NET_BOX="${NET_BOX}
   <notebook labels=\"Named|Dnsd\">
"
[ -x /usr/sbin/named     ] && NET_BOX="${NET_BOX}
    <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>
"
[ -x /usr/sbin/dnsd     ] && NET_BOX="${NET_BOX}
    <frame Dnsd (busybox)>
$(helpdaemon dnsd 80x15 DNSD_OPTIONS DNSD_OPTS)
$(configfile /etc/dnsd.conf DNSD_CONF)
      <hbox>
	$(startstopdaemon dnsd)
      </hbox>
     </frame>
"
[ -x /usr/sbin/dnsd -a -x /usr/sbin/named ] && NET_BOX="${NET_BOX}
   </notebook>
"
[ -x /usr/bin/rsync    ] && NET_BOX="${NET_BOX}
     <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>
"
[ -x /usr/sbin/lighttpd -a -x /usr/bin/httpd ] && NET_BOX="${NET_BOX}
   <notebook labels=\"Apache|Lighttpd\">
"
tmp="<frame PHP>
$(configfile /etc/php.ini PHP_CONF)
      </frame>
"
[ -f /etc/php.ini ] || tmp=""
[ -x /usr/bin/httpd ] && NET_BOX="${NET_BOX}<frame Apache>${tmp}
$(configfile /etc/apache/httpd.conf HTTPD_CONF)
      <hbox>
	$(startstopdaemon apache)
      </hbox>
     </frame>
"
[ -x /usr/sbin/lighttpd ] && NET_BOX="${NET_BOX}<frame Lighttpd>${tmp}
$(configfile /etc/lighttpd/lighttpd.conf LIGHTTPD_CONF)
      <hbox>
	$(startstopdaemon lighttpd)
      </hbox>
     </frame>
"
[ -x /usr/sbin/lighttpd -a -x /usr/bin/httpd ] && NET_BOX="${NET_BOX}
   </notebook>
"
tmp="<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>"
[ -x /usr/sbin/smbd     ] && NET_BOX="${NET_BOX}${tmp}
	$(startstopdaemon samba)
      </hbox>
     </frame>
"
[ -x /usr/bin/x11vnc    ] && NET_BOX="${NET_BOX}
     <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>
"
[ -x /usr/sbin/knockd    ] && NET_BOX="${NET_BOX}
    <frame Knockd: trigger backdoors>
$(helpdaemon knockd 80x15 KNOCK_OPTIONS KNOCK_OPTS)
$(configfile /etc/knockd.conf KNOCKD_CONF)
      <hbox>
	$(startstopdaemon knockd knock)
      </hbox>
     </frame>
"
NET_BOX="${NET_BOX}
    </notebook>
"
tmp="<frame Iptables>
$(configfile /etc/iptables.conf IPTABLES_CONF)
      <hbox>
        <button>
          <input file icon=\"browser\"></input>
	  <label>man</label>
	  <action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x24 -title \"iptables man (q to quit)\" -e \". /etc/profile; man 8 iptables\"</action>
        </button>
        <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>
"
[ -x /sbin/iptables ] && NET_BOX="$NET_BOX${tmp}"
tmp='<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>'
[ -x /usr/bin/knock ] && NET_BOX="$NET_BOX${tmp}
$(helpbutton knock 80x12)
      <button>
        <label>Start</label>
        <input file icon=\"forward\"></input>
        <action>knock \$KNOCK_OPTS</action>
      </button>
    </hbox>
    </frame>
"
tmp='
    <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>'
NET_BOX="${NET_BOX}${tmp}$(helpbutton ether-wake 80x15)"
tmp='
					<button>
						<label>Start</label>
						<input file icon="forward"></input>
						<action>ether-wake $ETHERWAKE_OPTS</action>
					</button>
				</hbox>
			</frame>
		</notebook>

		<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="${NET_BOX}${tmp}"

export NET_BOX

# TODO:  Modules(Network kernel modules) VPN(OpenVPN)

# 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
