#!/usr/bin/env perl
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-

# Network configurator. Designed to be architecture and distribution independent.
#
# Copyright (C) 2000-2001 Ximian, Inc.
#
# Authors: Hans Petter Jansson <hpj@ximian.com>
#          Michael Vogt <mvo@debian.org> (Debian Support)
#          Arturo Espinosa <arturo@ximian.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

# Best viewed with 100 columns of width.

# Configuration files affected:
#
# /etc/resolv.conf
# /etc/host.conf
# /etc/hosts
# /etc/sysconfig/network
# /etc/rc.config
# /etc/smb.conf

# Running programs affected:
#
# smbd
# nmbd
# ifconfig: check current interfaces and activate/deactivate.


BEGIN {
require "/usr/share/setup-tool-backends/scripts/general.pl";
require "/usr/share/setup-tool-backends/scripts/platform.pl";
require "/usr/share/setup-tool-backends/scripts/util.pl";
require "/usr/share/setup-tool-backends/scripts/file.pl";
require "/usr/share/setup-tool-backends/scripts/xml.pl";
require "/usr/share/setup-tool-backends/scripts/network.pl";
}


# Debug stuff
#$xst_prefix = "/tmp/y";
#open STDERR, ">/tmp/err";

# --- Tool information --- #

$name = "network";
$version = "0.6.1";
@platforms = ("redhat-5.2", "redhat-6.0", "redhat-6.1", "redhat-6.2", "redhat-7.0", "redhat-7.1",
              "mandrake-7.1", "mandrake-7.2",
              "debian-2.2", "debian-woody",
              "suse-7.0", "turbolinux-7.0");

$description =<<"end_of_description;";
       Configures all network parameters and interfaces.
end_of_description;

$progress_max = 10;


# --- XML parsing ---

# Scan XML from standard input to an internal tree.

sub xml_parse
{
  my ($tree, %hash);
  # Scan XML to tree.

  $tree = &xst_xml_scan;

  # Walk the tree recursively and extract configuration parameters.
  # This is the top level - find and enter the "network" tag.

  while (@$tree)
  {
    if ($$tree[0] eq "network") { &xml_parse_network ($$tree[1], \%hash); }

    shift @$tree;
    shift @$tree;
  }

  return(\%hash);
}

# <network>...</network>

sub push_unique
{
  my ($arr, $val) = @_;
  my $i;

  foreach $i (@$arr)
  {
    return if $i eq $val;
  }

  push @$arr, $val;
}

sub xml_parse_network
{
  my $tree = $_[0];
  my $hash = $_[1];
  my (@searchdomain, @nameserver, @order, %statichost, %interface, %dialing);

  shift @$tree;  # Skip attributes.

  while (@$tree)
  {
       if ($$tree[0] eq "auto")            { $$hash{"auto"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "hostname")        { $$hash{"hostname"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "gateway")         { $$hash{"gateway"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "gatewaydev")      { $$hash{"gatewaydev"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "domain")          { $$hash{"domain"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "nameserver")      { &push_unique (\@nameserver, &xst_xml_get_pcdata ($$tree[1])); }
    elsif ($$tree[0] eq "searchdomain")    { &push_unique (\@searchdomain, &xst_xml_get_pcdata ($$tree[1])); }
    elsif ($$tree[0] eq "order")           { push (@order, &xst_xml_get_pcdata ($$tree[1])); }
    elsif ($$tree[0] eq "hostmatch")       { $$hash{"hostmatch"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "statichost")      { &xml_parse_statichost ($$tree[1], \%statichost); }
    elsif ($$tree[0] eq "workgroup")       { $$hash{"workgroup"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "description")     { $$hash{"description"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "winsserver")      { $$hash{"winsserver"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "winsuse")         { $$hash{"winsuse"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "smbuse")          { $$hash{"smbuse"} = &xst_xml_get_pcdata ($$tree[1]); }
    elsif ($$tree[0] eq "interface")       { &xml_parse_interface ($$tree[1], \%interface); }
    elsif ($$tree[0] eq "dialing")         { &xml_parse_dialing ($$tree[1], \%dialing); }

    shift @$tree;
    shift @$tree;
  }

  $$hash{"order"} = \@order unless $#order < 0;
  $$hash{"searchdomain"} = \@searchdomain unless $#searchdomain < 0;
  $$hash{"nameserver"} = \@nameserver unless $#nameserver < 0;
  $$hash{"statichost"} = \%statichost unless scalar keys %statichost == 0;
  $$hash{"interface"} = \%interface unless scalar keys %interface == 0;
  $$hash{"dialing"} = \%dialing unless scalar keys %dialing == 0;
}

# <network><statichost>...</statichost></network>

sub xml_parse_statichost
{
  my $tree = $_[0];
  my $statichost = $_[1];
  my $ip;
  my @alias;

  shift @$tree;

  while (@$tree)
  {
    if    ($$tree[0] eq "ip")    { $ip = &xst_xml_get_pcdata($$tree[1]); }
    elsif ($$tree[0] eq "alias") { push(@alias, &xst_xml_get_pcdata($$tree[1])); }

    shift @$tree;
    shift @$tree;
  }

  if ($ip =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/)
  {
    $$statichost{$ip} = \@alias;
  }
}

# <interface>...</interface>

sub xml_parse_interface
{
  my $tree = $_[0];
  my $interface = $_[1];
  my %hash;
  my $dev;

  shift @$tree;
  
  while (@$tree)
  {
    $hash{$$tree[0]} = &xst_xml_get_pcdata ($$tree[1]);

    shift @$tree;
    shift @$tree;
  }

  $hash{"file"} = &xst_network_get_file ($hash{"dev"}) if !exists $hash{"file"};
  $dev = $hash{"file"};
  $$interface{$dev} = \%hash;
}

sub xml_parse_dialing
{
  my $tree = $_[0];
  my $dialing = $_[1];
  my %hash;
  my $name;

  shift @$tree;
  
  while (@$tree)
  {
    $hash{$$tree[0]} = &xst_xml_get_pcdata ($$tree[1]);

    shift @$tree;
    shift @$tree;
  }

  $name = $hash{"name"};
  $$dialing{$name} = \%hash;
}

# --- XML printing --- #

sub xml_print_statichost
{
  my ($h) = $_[0];
  my ($statichost, $i, $j, $val);
  
  &xst_xml_print_vspace ();
  foreach $i (keys %{$$h{"statichost"}})
  {
    $statichost = $ {$$h{"statichost"}}{$i};
    &xst_xml_print_line ("<statichost>\n");
    &xst_xml_enter ();
    $val = &xst_xml_quote ($i);
    &xst_xml_print_line ("<ip>$val</ip>\n");
    foreach $j (@$statichost)
    {
      $val = &xst_xml_quote ($j);
      &xst_xml_print_line ("<alias>$val</alias>\n");
    }
    &xst_xml_leave ();
    &xst_xml_print_line ("</statichost>\n");
  }
}

sub xml_print
{
  my $h = $_[0];
  my @scalar_keys =
      qw(auto hostname gateway gatewaydev gwdevunsup domain 
         hostmatch workgroup description winsserver winsuse smbuse
         smartdhcpcd smbinstalled dialinstalled);
  my @array_keys =
      qw(nameserver searchdomain order);

  &xst_xml_print_begin ();

  # Hostname, domain, search domains, nameservers.

  &xst_xml_print_scalars ($h, @scalar_keys);
  &xst_xml_print_arrays ($h, @array_keys);
  &xml_print_statichost ($h);

  &xst_xml_print_hash_hash ($$h{"interface"}, "interface");
  &xst_xml_print_hash_hash ($$h{"dialing"}, "dialing");

  &xst_xml_print_end ();
}


# Top-level actions.


sub get
{
  my $hash;

  # network interface stuff
  $hash = &xst_network_conf_get ();
  &xst_network_ensure_loopback ($hash);

  &xst_end();
  &xml_print ($hash);
}


sub set
{
  my $hash;
  
  $hash = &xml_parse ();

  # check the success result of this.
  &xst_network_conf_set ($hash);
#  &be_ensure_local_host_entry ($$hash{"hostname"});

  &xst_end ();
}


# --- Filter config: XML in, XML out --- #


sub filter
{
  my $hash;
  
  $hash = &xml_parse ();
  &xst_end ();
  &xml_print ($hash);
}

sub enable_iface
{
    my ($tool, $iface, $enabled) = @_;
    my (%dist_attrib, $iface_set);
    my %hash = ("file" => $iface);
    
    %dist_attrib = &xst_network_get_interface_replace_table ();
    $iface_set = $dist_attrib{"iface_set"};
    &$iface_set (\%hash, undef, $enabled, 1);
    # Don't forget to do xst_end when the reports are over!
    &xst_end ();
    # XML output would come here, but this directive returns no XML.
}


# --- Main --- #

# get, set and filter are special cases that don't need more parameters than a ref to their function.
# Read general.pl.in:xst_run_directive to know about the format of this hash.

$directives = {
  "get"          => [ \&get,          [], "" ],
  "set"          => [ \&set,          [], "" ],
  "filter"       => [ \&filter,       [], "" ],
  "enable_iface" => [ \&enable_iface, [ "interface", "enabled" ],
                      "Immediatly enable or disable a given interface. " .
                      "interface is the file tag value, enabled is 1 or 0." ]
    };

$tool = &xst_init ($name, $version, $description, $directives, @ARGV);
&xst_platform_ensure_supported ($tool, @platforms);
&xst_run ($tool);
