home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Network / Hosts.pm next >
Encoding:
Perl POD Document  |  2006-08-14  |  10.6 KB  |  427 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  
  3. # Hosts Configuration handling
  4. #
  5. # Copyright (C) 2000-2001 Ximian, Inc.
  6. #
  7. # Authors: Hans Petter Jansson <hpj@ximian.com>
  8. #          Carlos Garnacho     <carlosg@gnome.org>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU Library General Public License as published
  12. # by the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. # GNU Library General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Library General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  23.  
  24. package Network::Hosts;
  25.  
  26. sub run_hostname
  27. {
  28.   my ($hostname) = @_;
  29.  
  30.   &Utils::Report::enter ();
  31.   &Utils::Report::do_report ("network_hostname_set", $hostname);
  32.   &Utils::File::run ("hostname $hostname");
  33.   &Utils::Report::leave ();
  34. }
  35.  
  36. sub get_fqdn_dist
  37. {
  38.   my %dist_map =
  39.      (
  40.     "redhat-6.2"      => "redhat-6.2",
  41.     "redhat-7.0"      => "redhat-6.2",
  42.     "redhat-7.1"      => "redhat-6.2",
  43.     "redhat-7.2"      => "redhat-7.2",
  44.     "redhat-8.0"      => "redhat-7.2",
  45.     "mandrake-9.0"    => "redhat-6.2",
  46.     "yoper-2.2"       => "redhat-6.2",
  47.     "conectiva-9"     => "redhat-6.2", 
  48.     "debian-3.0"      => "debian-2.2",
  49.     "suse-9.0"        => "suse-9.0",
  50.     "pld-1.0"         => "redhat-6.2",
  51.     "vine-3.0"        => "redhat-6.2",
  52.     "ark"             => "redhat-6.2",
  53.     "slackware-9.1.0" => "suse-9.0",
  54.     "gentoo"          => "gentoo",
  55.     "freebsd-5"       => "freebsd-5",
  56.       );
  57.  
  58.   return $dist_map{$Utils::Backend::tool{"platform"}};
  59. }
  60.  
  61. sub get_fqdn_parse_table
  62. {
  63.   my %dist_tables =
  64.     (
  65.      "redhat-6.2" =>
  66.      {
  67.        fn =>
  68.        {
  69.          SYSCONFIG_NW => "/etc/sysconfig/network",
  70.          RESOLV_CONF  => "/etc/resolv.conf"
  71.        },
  72.        table =>
  73.        [
  74.         [ "hostname", \&Utils::Parse::get_sh, SYSCONFIG_NW, HOSTNAME ],
  75.         [ "domain",   \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ]
  76.        ]
  77.      },
  78.  
  79.      "redhat-7.2" =>
  80.      {
  81.        fn =>
  82.        {
  83.          SYSCONFIG_NW => ["/etc/sysconfig/networking/profiles/default/network",
  84.                           "/etc/sysconfig/networking/network",
  85.                           "/etc/sysconfig/network"],
  86.          RESOLV_CONF  => ["/etc/sysconfig/networking/profiles/default/resolv.conf",
  87.                           "/etc/resolv.conf"],
  88.        },
  89.        table =>
  90.        [
  91.             [ "hostname", \&Utils::Parse::get_sh, SYSCONFIG_NW, HOSTNAME ],
  92.             [ "domain",   \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  93.            ]
  94.      },
  95.  
  96.      "debian-2.2" =>
  97.      {
  98.        fn =>
  99.        {
  100.          RESOLV_CONF => "/etc/resolv.conf",
  101.          HOSTNAME    => "/etc/hostname",
  102.        },
  103.        table =>
  104.        [
  105.         [ "hostname", \&Utils::Parse::get_first_line, HOSTNAME ],
  106.         [ "domain",    \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ]
  107.        ]
  108.      },
  109.  
  110.      "suse-9.0" =>
  111.      {
  112.        fn =>
  113.        {
  114.          RESOLV_CONF  => "/etc/resolv.conf",
  115.          HOSTNAME     => "/etc/HOSTNAME",
  116.        },
  117.        table =>
  118.        [
  119.         [ "hostname", \&Utils::Parse::get_fq_hostname, HOSTNAME ],
  120.         [ "domain", \&Utils::Parse::get_fq_domain, HOSTNAME ],
  121.         [ "domain", \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  122.        ]
  123.      },
  124.     
  125.      "gentoo" =>
  126.      {
  127.        fn =>
  128.        {
  129.          HOSTNAME    => "/etc/conf.d/hostname",
  130.          DOMAINNAME  => "/etc/conf.d/domainname",
  131.          RESOLV_CONF => "/etc/resolv.conf",
  132.        },
  133.        table =>
  134.        [
  135.         [ "hostname", \&Utils::Parse::get_sh, HOSTNAME, HOSTNAME ],
  136.         [ "domain", \&Utils::Parse::get_sh, DOMAINNAME, DNSDOMAIN ],
  137.         [ "domain", \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  138.        ]
  139.      },
  140.  
  141.      "freebsd-5" =>
  142.      {
  143.        fn =>
  144.        {
  145.          RC_CONF     => "/etc/rc.conf",
  146.          RESOLV_CONF => "/etc/resolv.conf",
  147.        },
  148.        table =>
  149.        [
  150.         [ "hostname", \&Utils::Parse::get_sh_re, RC_CONF, hostname, "^([^\.]*)\." ],
  151.         [ "domain", \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  152.        ]
  153.      },
  154.    );
  155.  
  156.   my $dist = &get_fqdn_dist ();
  157.   return %{$dist_tables{$dist}} if $dist;
  158.  
  159.   &Utils::Report::do_report ("platform_no_table", $Utils::Backend::tool{"platform"});
  160.   return undef;
  161. }
  162.  
  163. sub get_fqdn_replace_table
  164. {
  165.   my %dist_tables =
  166.     (
  167.      "redhat-6.2" =>
  168.      {
  169.        fn =>
  170.        {
  171.          SYSCONFIG_NW => "/etc/sysconfig/network",
  172.          RESOLV_CONF  => "/etc/resolv.conf"
  173.        },
  174.        table =>
  175.        [
  176.         [ "hostname", \&Utils::Replace::set_sh, SYSCONFIG_NW, HOSTNAME ],
  177.         [ "hostname", \&run_hostname ],
  178.         [ "domain", \&Utils::Replace::set_sh, SYSCONFIG_NW, DOMAIN ],
  179.         [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ]
  180.        ]
  181.      },
  182.  
  183.      "redhat-7.2" =>
  184.      {
  185.        fn =>
  186.        {
  187.          SYSCONFIG_NW => ["/etc/sysconfig/networking/profiles/default/network",
  188.                           "/etc/sysconfig/networking/network",
  189.                           "/etc/sysconfig/network"],
  190.          RESOLV_CONF  => ["/etc/sysconfig/networking/profiles/default/resolv.conf",
  191.                           "/etc/resolv.conf"],
  192.        },
  193.        table =>
  194.        [
  195.             [ "hostname", \&Utils::Replace::set_sh, SYSCONFIG_NW, HOSTNAME ],
  196.         [ "hostname", \&run_hostname ],
  197.         [ "domain", \&Utils::Replace::set_sh, SYSCONFIG_NW, DOMAIN ],
  198.             [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  199.            ]
  200.      },
  201.  
  202.      "debian-2.2" =>
  203.      {
  204.        fn =>
  205.        {
  206.          RESOLV_CONF => "/etc/resolv.conf",
  207.          HOSTNAME    => "/etc/hostname",
  208.        },
  209.        table =>
  210.        [
  211.         [ "hostname", \&Utils::Replace::set_first_line, HOSTNAME ],
  212.         [ "hostname", \&run_hostname ],
  213.         [ "domain",    \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ]
  214.        ]
  215.      },
  216.  
  217.      "suse-9.0" =>
  218.      {
  219.        fn =>
  220.        {
  221.          RESOLV_CONF  => "/etc/resolv.conf",
  222.          HOSTNAME     => "/etc/HOSTNAME",
  223.        },
  224.        table =>
  225.        [
  226.         [ "hostname", \&Utils::Replace::set_fq_hostname, HOSTNAME, "%hostname%", "%domain%" ],
  227.         [ "hostname", \&run_hostname ],
  228.         [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  229.        ]
  230.      },
  231.     
  232.      "gentoo" =>
  233.      {
  234.        fn =>
  235.        {
  236.          HOSTNAME    => "/etc/conf.d/hostname",
  237.          DOMAINNAME  => "/etc/conf.d/domainname",
  238.          RESOLV_CONF => "/etc/resolv.conf",
  239.        },
  240.        table =>
  241.        [
  242.         [ "hostname", \&Utils::Replace::set_sh, HOSTNAME, HOSTNAME ],
  243.         [ "hostname", \&run_hostname ],
  244.         [ "domain", \&Utils::Replace::set_sh, DOMAINNAME, DNSDOMAIN ],
  245.         [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  246.        ]
  247.      },
  248.  
  249.      "freebsd-5" =>
  250.      {
  251.        fn =>
  252.        {
  253.          RC_CONF     => "/etc/rc.conf",
  254.          RESOLV_CONF => "/etc/resolv.conf",
  255.        },
  256.        table =>
  257.        [
  258.         [ "hostname", \&Utils::Replace::set_sh, RC_CONF, hostname, "%hostname%.%domain%" ],
  259.         [ "hostname", \&run_hostname, "%hostname%.%domain%" ],
  260.         [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  261.        ]
  262.      },
  263.    );
  264.  
  265.   my $dist = &get_fqdn_dist ();
  266.   return %{$dist_tables{$dist}} if $dist;
  267.  
  268.   &Utils::Report::do_report ("platform_no_table", $Utils::Backend::tool{"platform"});
  269.   return undef;
  270. }
  271.  
  272. sub add_statichost_alias
  273. {
  274.   my ($localhost, $alias) = @_;
  275.   my $i;
  276.  
  277.   foreach $i (@$localhost)
  278.   {
  279.     return if ($i eq $alias);
  280.   }
  281.   
  282.   push @$localhost, $alias;
  283. }
  284.  
  285. sub remove_statichost_alias
  286. {
  287.   my ($localhost, $alias) = @_;
  288.   my $i;
  289.  
  290.   for ($i = 0; $i < @$localhost; $i++) {
  291.     if ($$localhost[$i] eq $alias)
  292.     {
  293.       delete $$localhost[$i];
  294.       return;
  295.     }
  296.   }
  297. }
  298.   
  299. sub ensure_loopback_statichost
  300. {
  301.   my ($statichost, $hostname, $old_hostname, $lo_ip) = @_;
  302.   my $i;
  303.  
  304.   if (exists $$statichost{$lo_ip})
  305.   {
  306.     my $localhost = $$statichost{$lo_ip};
  307.     &remove_statichost_alias ($localhost, $old_hostname) if ($old_hostname);
  308.     &add_statichost_alias ($localhost, $hostname);
  309.   }
  310.   else
  311.   {
  312.     $$statichost{$lo_ip} = [ ("localhost", "localhost.localdomain", $hostname) ];
  313.   }
  314. }
  315.  
  316. sub get_fqdn
  317. {
  318.   my %dist_attrib;
  319.   my $hash;
  320.  
  321.   %dist_attrib = &get_fqdn_parse_table ();
  322.  
  323.   $hash = &Utils::Parse::get_from_table ($dist_attrib{"fn"},
  324.                                          $dist_attrib{"table"});
  325.  
  326.   return ($$hash {"hostname"}, $$hash{"domain"});
  327. }
  328.  
  329. sub get_hosts
  330. {
  331.   my ($statichosts, @arr);
  332.  
  333.   $statichosts = &Utils::Parse::split_hash ("/etc/hosts", "[ \t]+", "[ \t]+");
  334.  
  335.   foreach $i (sort keys %$statichosts)
  336.   {
  337.     push @arr, [$i, $$statichosts{$i}];
  338.   }
  339.  
  340.   return \@arr;
  341. }
  342.  
  343. sub get_dns
  344. {
  345.   my (@dns);
  346.  
  347.   @dns = &Utils::Parse::split_all_unique_hash_comment ("/etc/resolv.conf", "nameserver", "[ \t]+");
  348.  
  349.   return @dns;
  350. }
  351.  
  352. sub get_search_domains
  353. {
  354.   my (@search_domains);
  355.  
  356.   @search_domains = &Utils::Parse::split_first_array_unique ("/etc/resolv.conf", "search", "[ \t]+", "[ \t]+");
  357.  
  358.   return @search_domains;
  359. }
  360.  
  361. sub set_fqdn
  362. {
  363.   my ($hostname, $domain) = @_;
  364.   my (%dist_attrib, %hash, %old_hash);
  365.  
  366.   $hash{"hostname"} = $hostname;
  367.   $hash{"domain"} = $domain;
  368.  
  369.   ($old_hash{"hostname"}, $old_hash{"domain"}) = &get_fqdn ();
  370.  
  371.   %dist_attrib = &get_fqdn_replace_table ();
  372.   &Utils::Replace::set_from_table ($dist_attrib{"fn"}, $dist_attrib{"table"},
  373.                                    \%hash, \%old_hash);
  374. }
  375.  
  376. sub ensure_hostname
  377. {
  378.   my ($hosts, $hostname, $domain, $old_hostname, $olddomain) = @_;
  379.   my ($fqdn, $old_fqdn, $i);
  380.  
  381.   $fqdn  = $hostname;
  382.   $fqdn .= ".$domain" if ($domain);
  383.  
  384.   $old_fqdn  = $old_hostname;
  385.   $old_fqdn .= ".$old_domain" if ($old_domain);
  386.  
  387.   foreach $i (@$hosts)
  388.   {
  389.     if ($i eq $old_fqdn)
  390.     {
  391.       $i = $fqdn;
  392.     }
  393.   }
  394. }
  395.  
  396. sub set_hosts
  397. {
  398.   my ($config, $hostname, $domain) = @_;
  399.   my ($old_hostname, $old_domain) = &get_fqdn ();
  400.   my ($i, %hash);
  401.  
  402.   foreach $i (@$config)
  403.   {
  404.     &ensure_hostname ($$i[1], $hostname, $domain, $old_hostname, $old_domain);
  405.     $hash{$$i[0]} = $$i[1];
  406.   }
  407.  
  408.   &Utils::Replace::join_hash ("/etc/hosts", "[ \t]+", "[ \t]+", \%hash);
  409. }
  410.  
  411. sub set_dns
  412. {
  413.   my ($dns) = @_;
  414.  
  415.   &Utils::Replace::join_all ("/etc/resolv.conf", "nameserver", "[ \t]+", $dns);
  416. }
  417.  
  418. sub set_search_domains
  419. {
  420.   my ($search_domains) = @_;
  421.  
  422.   &Utils::Replace::join_first_array ("/etc/resolv.conf", "search",
  423.                                      "[ \t]+", "[ \t]+", $search_domains);
  424. }
  425.  
  426. 1;
  427.