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 / Time / NTP.pm next >
Encoding:
Perl POD Document  |  2006-08-14  |  3.6 KB  |  152 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  
  3. # NTP 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 Time::NTP;
  25.  
  26. sub get_config_file ()
  27. {
  28.   my %dist_map =
  29.   (
  30.     "redhat-6.2"      => "redhat-6.2",
  31.     "redhat-7.0"      => "redhat-6.2",
  32.     "redhat-7.1"      => "redhat-6.2",
  33.     "redhat-7.2"      => "redhat-6.2",
  34.     "redhat-7.3"      => "redhat-6.2",
  35.     "redhat-8.0"      => "redhat-6.2",
  36.     "mandrake-9.0"    => "redhat-6.2",
  37.     "debian-3.0"      => "redhat-6.2",
  38.     "suse-9.0"        => "redhat-6.2",
  39.     "slackware-9.1.0" => "redhat-6.2",
  40.     "gentoo"          => "redhat-6.2",
  41.     "pld-1.0"         => "pld-1.0",
  42.     "vine-3.0"        => "redhat-6.2",
  43.     "freebsd-5"       => "redhat-6.2",
  44.   );
  45.  
  46.   my %dist_table =
  47.   (
  48.     "redhat-6.2" => "/etc/ntp.conf",
  49.     "pld-1.0"    => "/etc/ntp/ntp.conf"
  50.   );
  51.  
  52.   my $dist = $dist_map{$Utils::Backend::tool{"platform"}};
  53.   return $dist_table{$dist} if $dist;
  54.  
  55.   &Utils::Report::do_report ("platform_no_table", $$tool{"platform"});
  56.   return undef;
  57. }
  58.  
  59. sub get_ntp_servers
  60. {
  61.   $ntp_conf = &get_config_file ();
  62.  
  63.   return &Utils::Parse::split_all_array_with_pos ($ntp_conf, "server", 0, "[ \t]+", "[ \t]+");
  64. }
  65.  
  66. sub ntp_conf_replace
  67. {
  68.   my ($file, $key, $re, $value) = @_;
  69.   my ($fd, @line, @res);
  70.   my ($buff, $i);
  71.   my ($pre_space, $post_comment);
  72.   my ($line_key, $val, $rest);
  73.   my ($n, $ret);
  74.  
  75.   &Utils::Report::enter ();
  76.   &Utils::Report::do_report ("replace_split", $key, $file);
  77.  
  78.   $buff = &Utils::File::load_buffer ($file);
  79.   
  80.   foreach $i (@$buff)
  81.   {
  82.     $pre_space = $post_comment = "";
  83.  
  84.     chomp $i;
  85.  
  86.     $pre_space    = $1 if $i =~ s/^([ \t]+)//;
  87.     $post_comment = $1 if $i =~ s/([ \t]*\#.*)//;
  88.     
  89.     if ($i ne "")
  90.     {
  91.       @line = split ($re, $i, 3);
  92.       $line_key = shift (@line);
  93.       $val      = shift (@line);
  94.       $rest     = shift (@line);
  95.  
  96.       # found the key?
  97.       if ($line_key eq $key)
  98.       {
  99.         $n = 0;
  100.  
  101.         while (@$value[$n] && (@$value[$n] ne $val))
  102.         {
  103.           $n++;
  104.         }
  105.  
  106.         if (@$value[$n] ne $val)
  107.         {
  108.           $i = "";
  109.           next;
  110.         }
  111.  
  112.         delete @$value[$n];
  113.         chomp $val;
  114.         $i  = &Utils::Replace::set_value ($key, $val, $re) . " " . $rest;
  115.       }
  116.     }
  117.  
  118.     $i = $pre_space . $i . $post_comment . "\n";
  119.   }
  120.  
  121.   foreach $i (@$value)
  122.   {
  123.     push (@$buff, &Utils::Replace::set_value ($key, $i, $re) . "\n") if ($i ne "");
  124.   }
  125.  
  126.   &Utils::File::clean_buffer ($buff);
  127.   $ret = &Utils::File::save_buffer ($buff, $file);
  128.   &Utils::Report::leave ();
  129.   return $ret;
  130. }
  131.  
  132. sub set_ntp_servers
  133. {
  134.   my (@config) = @_;
  135.   my ($ntp_conf);
  136.  
  137.   $ntp_conf = &get_config_file ();
  138.   return &ntp_conf_replace ($ntp_conf, "server", "[ \t]+", @config);
  139. }
  140.  
  141. sub get
  142. {
  143.   return &get_ntp_servers ();
  144. }
  145.  
  146. sub set
  147. {
  148.   return &set_ntp_servers (@_);
  149. }
  150.  
  151. 1;
  152.