#!/bin/sh
#
# Gtkdialog box to manage network connexion.
# - SliTaz GNU/Linux 2008.
#
VERSION=20080113

# 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
}

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
}

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 | \
		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;;
	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 dialod.
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 let you manage network connections getting dynamic IP by DHCP
or static IP, and setup servers. Netbox can start or stop networking,
configure network interfaces or directly edit files. Wifi users may
configure System wide / Network."
      </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>
    <text wrap="false">
      <input>date</input>
    </text>
    <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 inetd udhcpd dnsd rsync lighttpd smbd nmbd x11vnc pppd; 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 than /etc/network.conf is seded wehwn an interafce is activate
#
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>

    <frame General>
      <hbox>
        <text use-markup="true">
          <label>"<b>Interface   :</b>"</label>
        </text>
        <entry>
          <input>. /etc/network.conf; echo $INTERFACE</input>
          <variable>INTERFACE</variable>
        </entry>
      </hbox>
    </frame>

    <notebook labels="DHCP|PPPoE|Static IP|System wide|VPN|Servers'
[ -x /sbin/iptables ] && NET_BOX="$NET_BOX|Firewall"
tmp='|Etherwake">

    <frame Udhcpc>
      <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>
        <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>
        </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>
        </button>
      </hbox>
    </frame>

    <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>USER</variable>
        </entry>
      </hbox>
      <hbox>
        <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>

    <frame Configuration>
      <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/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>
        </button>
        <button>
          <label>Stop</label>
          <input file icon="stop"></input>
          <action>echo -n "Stopping interface : $INTERFACE... "</action>
          <action>ifconfig $INTERFACE down; echo "done"</action>
        </button>
      </hbox>
    </frame>

    <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 lo down</action>
          <action>ifconfig $INTERFACE down</action>
          <action>killall -q udhcpc; echo "done"</action>
          <action>/etc/init.d/network.sh</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="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 "dropbear help" -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 ] && NET_BOX="${NET_BOX}${tmp}SSH"   && tmp='|'
[ -x /usr/sbin/inetd    ] && NET_BOX="${NET_BOX}${tmp}INETD" && tmp='|'
[ -x /usr/sbin/udhcpd   ] && 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     ] && NET_BOX="${NET_BOX}${tmp}DNS"   && tmp='|'
[ -x /usr/bin/rsync     ] && NET_BOX="${NET_BOX}${tmp}RSYNC" && tmp='|'
[ -x /usr/sbin/lighttpd ] && 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='|'
. /etc/daemons.conf
set -- $DROPBEAR_OPTIONS
while [ -n "$2" ]; do
  [ "$1" = "-b" ] && DROPBEAR_BANNER="$2" && break
  shift
done
NET_BOX="${NET_BOX}\">"
tmp='<frame Dropbear>
      <hbox>
        <text use-markup="true">
          <label>"<b>DROPBEAR_OPTIONS</b>"</label>
        </text>
        <entry editable="false">'
[ -n "$DROPBEAR_OPTIONS" ] && tmp="$tmp<default>$DROPBEAR_OPTIONS</default>"
tmp2='
          <variable>DROPBEAR_OPTS</variable>
        </entry>
        <button>
          <input file icon="help"></input>
	  <action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x30 -title "dropbear help" -e "dropbear --help ; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad /etc/daemons.conf</action>
        </button>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Banner   :</b>"</label>
        </text>
        <entry editable="false">'
[ -n "$DROPBEAR_BANNER" ] && tmp="$tmp$tmp2<default>$DROPBEAR_BANNER</default>"
tmp2='
          <variable>DROPBEAR_BANNER</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $DROPBEAR_BANNER</action>
        </button>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Remote  :</b>"</label>
        </text>
        <entry>
          <variable>DROPBEAR_PEER</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 "$DROPBEAR_PEER" -e "dbclient -i /etc/dropbear/dropbear_rsa_host_key $DROPBEAR_PEER ; 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 "dropbear help" -e "netbox call sendsshkey $DROPBEAR_PEER; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
      </hbox>
      <hbox>
        <button>
          <label>Start</label>
          <input file icon="forward"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)dropbear \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)\"/RUN_DAEMONS=\"dropbear \1\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/dropbear start</action>
        </button>
        <button>
          <label>Stop</label>
          <input file icon="stop"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)dropbear \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/dropbear stop</action>
        </button>
      </hbox>
     </frame>
'
[ -x /usr/sbin/dropbear ] && NET_BOX="${NET_BOX}${tmp}${tmp2}"
tmp='<frame Inetd>
      <hbox>
        <text use-markup="true">
          <label>"<b>INETD_OPTIONS</b>"</label>
        </text>
        <entry editable="false">'
[ -n "$INETD_OPTS" ] && tmp="$tmp<default>$INETD_OPTS</default>"
tmp2='
          <variable>INETD_OPTS</variable>
        </entry>
        <button>
          <input file icon="help"></input>
	  <action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 60x14 -title "inetd help" -e "inetd --help ; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad /etc/daemons.conf</action>
        </button>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Configuration :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/inetd.conf</default>
          <variable>INETD_CONF</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $INETD_CONF</action>
        </button>
      </hbox>
      <hbox>
'
tmp="$tmp$tmp2"
for i in $(grep bin /etc/inetd.conf | awk '{ print $6}'); do
  i=$(basename $i)
tmp2="
        <button>
          <input file icon=\"help\"></input>
          <label>$i</label>
	  <action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 60x19 -title \"$i help\" -e \"$i --help ; echo -e \\\"----\nENTER to continue...\\\" && read close\"</action>
        </button>
"
  tmp="$tmp$tmp2"
done
tmp2='
        <button>
          <label>Start</label>
          <input file icon="forward"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)inetd \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)\"/RUN_DAEMONS=\"inetd \1\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/inetd start</action>
        </button>
        <button>
          <label>Stop</label>
          <input file icon="stop"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)inetd \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/inetd stop</action>
        </button>
      </hbox>
     </frame>
'
[ -x /usr/sbin/inetd ] && NET_BOX="${NET_BOX}${tmp}${tmp2}"
tmp='<frame Dhcpd>
      <hbox>
        <text use-markup="true">
          <label>"<b>UDHCPD_OPTIONS</b>"</label>
        </text>
        <entry editable="false">'
[ -n "$UDHCPD_OPTS" ] && tmp="$tmp<default>$UDHCPD_OPTS</default>"
tmp2='
          <variable>UDHCPD_OPTS</variable>
        </entry>
        <button>
          <input file icon="help"></input>
	  <action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 60x14 -title "udhcpd help" -e "udhcpd --help ; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad /etc/daemons.conf</action>
        </button>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Configuration :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/udhcpd.conf</default>
          <variable>UDHCPD_CONF</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $UDHCPD_CONF</action>
        </button>
      </hbox>
      <hbox>
        <button>
          <label>Start</label>
          <input file icon="forward"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)udhcpd \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)\"/RUN_DAEMONS=\"udhcpd \1\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/udhcpd start</action>
        </button>
        <button>
          <label>Stop</label>
          <input file icon="stop"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)udhcpd \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/udhcpd stop</action>
        </button>
      </hbox>
     </frame>
'
[ -x /usr/sbin/udhcpd   ] && NET_BOX="${NET_BOX}$tmp$tmp2"
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="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"
tmp='<frame Dnsd>
      <hbox>
        <text use-markup="true">
          <label>"<b>DNSD_OPTIONS</b>"</label>
        </text>
        <entry editable="false">'
[ -n "$DNSD_OPTIONS" ] && tmp="$tmp<default>$DNSD_OPTIONS</default>"
tmp2='
          <variable>DNSD_OPTS</variable>
        </entry>
        <button>
          <input file icon="help"></input>
	  <action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x15 -title "dnsd help" -e "dnsd --help ; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad /etc/daemons.conf</action>
        </button>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Configuration :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/dnsd.conf</default>
          <variable>DNSD_CONF</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $DNSD_CONF</action>
        </button>
      </hbox>
      <hbox>
        <button>
          <label>Start</label>
          <input file icon="forward"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)dnsd \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)\"/RUN_DAEMONS=\"dnsd \1\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/dnsd start</action>
        </button>
        <button>
          <label>Stop</label>
          <input file icon="stop"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)dnsd \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/dnsd stop</action>
        </button>
      </hbox>
     </frame>
'
[ -x /usr/sbin/dnsd     ] && NET_BOX="${NET_BOX}${tmp}${tmp2}"
tmp='<frame Rsync>
      <hbox>
        <text use-markup="true">
          <label>"<b>RSYNCD_OPTIONS</b>"</label>
        </text>
        <entry editable="false">'
[ -n "${RSYNCD_OPTIONS#* }" ] && tmp="$tmp<default>${RSYNCD_OPTIONS#* }</default>"
tmp2='
          <variable>RSYNC_OPTS</variable>
        </entry>
        <button>
          <input file icon="help"></input>
	  <action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x31 -title "rsync daemon help" -e "rsync --daemon --help ; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad /etc/daemons.conf</action>
        </button>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Configuration :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/rsyncd.conf</default>
          <variable>RSYNCD_CONF</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $RSYNCD_CONF</action>
        </button>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Secrets file     :</b>"</label>
        </text>
        <entry editable="true">
          <default>/etc/rsyncd.secrets</default>
          <variable>RSYNCD_SECRETS</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $RSYNCD_SECRETS</action>
        </button>
      </hbox>
      <hbox>
        <button>
          <label>Start</label>
          <input file icon="forward"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)rsyncd \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)\"/RUN_DAEMONS=\"rsyncd \1\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/rsyncd start</action>
        </button>
        <button>
          <label>Stop</label>
          <input file icon="stop"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)rsyncd \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/rsyncd stop</action>
        </button>
      </hbox>
     </frame>
'
[ -x /usr/bin/rsync    ] && NET_BOX="${NET_BOX}${tmp}${tmp2}"
tmp='<frame Lighttpd>'
tmp2='<frame PHP>
       <hbox>
        <text use-markup="true">
          <label>"<b>Configuration :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/php.ini</default>
          <variable>PHP_CONF</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $PHP_CONF</action>
        </button>
       </hbox>
      </frame>
'
[ -f /etc/php.ini ] && tmp="${tmp}${tmp2}"
tmp2='<hbox>
        <text use-markup="true">
          <label>"<b>Configuration :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/lighttpd/lighttpd.conf</default>
          <variable>LIGHTTPD_CONF</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $LIGHTTPD_CONF</action>
        </button>
      </hbox>
      <hbox>
        <button>
          <label>Start</label>
          <input file icon="forward"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)lighttpd \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)\"/RUN_DAEMONS=\"lighttpd \1\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/lighttpd start</action>
        </button>
        <button>
          <label>Stop</label>
          <input file icon="stop"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)lighttpd \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/lighttpd stop</action>
        </button>
      </hbox>
     </frame>
'
[ -x /usr/sbin/lighttpd ] && NET_BOX="${NET_BOX}${tmp}${tmp2}"
tmp='<frame Samba: smbd & nmbd>
      <hbox>
        <text use-markup="true">
          <label>"<b>Configuration :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/samba/smb.conf</default>
          <variable>SMBD_CONF</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $SMBD_CONF</action>
        </button>
      </hbox>
      <hbox>
        <button>
          <label>Reload</label>
          <input file icon="reload"></input>
          <action>/etc/init.d/samba reload</action>
        </button>
        <button>
          <label>Start</label>
          <input file icon="forward"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)samba \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)\"/RUN_DAEMONS=\"samba \1\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/samba start</action>
        </button>
        <button>
          <label>Stop</label>
          <input file icon="stop"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)samba \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/samba stop</action>
        </button>
      </hbox>
     </frame>
'
[ -x /usr/sbin/smbd     ] && NET_BOX="${NET_BOX}${tmp}"
tmp='<frame x11vnc>
      <hbox>
        <text use-markup="true">
          <label>"<b>X11VNC_OPTIONS</b>"</label>
        </text>
        <entry editable="false">'
[ -n "$X11VNC_OPTIONS" ] && tmp="$tmp<default>$X11VNC_OPTIONS</default>"
tmp2='
          <variable>X11VNC_OPTS</variable>
        </entry>
        <button>
          <input file icon="help"></input>
	  <action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x30 -title "x11vnc daemon help (q to quit)" -e "x11vnc --help | less"</action>
        </button>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad /etc/daemons.conf</action>
        </button>
      </hbox>
      <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>
        <button>
          <label>Start</label>
          <input file icon="forward"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)x11vnc \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)\"/RUN_DAEMONS=\"x11vnc \1\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/x11vnc start</action>
        </button>
        <button>
          <label>Stop</label>
          <input file icon="stop"></input>
          <action>sed -i "s/RUN_DAEMONS=\"\(.*\)x11vnc \?\(.*\)\"/RUN_DAEMONS=\"\1\2\"/" /etc/rcS.conf</action>
          <action>/etc/init.d/x11vnc stop</action>
        </button>
      </hbox>
     </frame>
'
[ -x /usr/bin/x11vnc    ] && NET_BOX="${NET_BOX}${tmp}${tmp2}"
NET_BOX="${NET_BOX}
    </notebook>
"
tmp='<frame Iptables>
      <hbox>
        <text use-markup="true">
          <label>"<b>Configuration :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/iptables.conf</default>
          <variable>IPTABLES_CONF</variable>
        </entry>
        <button>
          <input file icon="accessories-text-editor"></input>
          <action type="lauch">leafpad $IPTABLES_CONF</action>
        </button>
      </hbox>
      <hbox>
        <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 Ether-wake>
    <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="false">
        <variable>ETHERWAKE_OPTS</variable>
      </entry>
      <button>
        <input file icon="help"></input>
	<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x15 -title "dnsd help" -e "ether-wake --help ; echo -e \"----\nENTER to continue...\" && read close"</action>
      </button>
      <button>
        <label>Start</label>
        <input file icon="forward"></input>
        <action>ether-wake $ETHERWAKE_OPTS</action>
      </button>
    </hbox>
    </frame>
    </notebook>

    <hbox>
      <button>
        <label>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

exit 0
