#!/usr/bin/perl # Drakwizard # Copyright (C) 2002, 2003 Mandrakesoft # # Authors: Arnaud Desmons # Florent Villard # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, 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 General Public License for more details. # # You should have received a copy of the GNU 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. package MDK::Wizard::Ntp; use strict; use common; use services; use MDK::Wizard::Wizcommon; use MDK::Wizard::Wizcommon_gtk3; use timezone; my $wiz = new MDK::Wizard::Wizcommon; my $in = interactive->vnew; my $o = { name => N("Time wizard"), var => { varserver1 => '', varserver2 => '', varserver3 => '', wiz_timezone => '' }, needed_rpm => [ 'ntp' ], defaultimage => "/usr/share/mcc/themes/default/time-mdk.png", }; my $ntp_servers = timezone::ntp_servers(); my %actions = ( try_again => N("Try again"), save_quit => N("Save config without test") ); my %country; my @country; if (-d '/usr/share/zoneinfo') { opendir my $dir, '/usr/share/zoneinfo'; foreach my $z (grep { /^[A-Z]/ } readdir $dir) { if (-d "/usr/share/zoneinfo/$z") { opendir my $dir2, "/usr/share/zoneinfo/$z"; push @{$country{$z}}, map { $_ } grep { /^[A-Z]/ } readdir $dir2; closedir $dir2; } else { $country{$z} = 1; } } closedir $dir; } $o->{pages} = { welcome => { name => N("This wizard will help you to set the time of your server synchronized with an external time server.") . "\n\n" . N("Thus your server will be the local time server for your network.") . "\n\n" . N("press next to begin, or cancel to leave this wizard"), no_back => 1, next => 'config' }, config => { name => N("Time servers") . "\n" . N("Select a primary a secondary and a third server from the list.") . "\n" . N("(we recommend using the server pool.ntp.org twice as this server randomly points to available time servers)"), pre => sub { $o->{var}{varserver1} ||= 'pool.ntp.org'; $o->{var}{varserver2} ||= 'pool.ntp.org'; $o->{var}{varserver3} ||= 'pool.ntp.org'; }, data => [ { label => N("Primary time server:"), val => \$o->{var}{varserver1}, list => [ keys %$ntp_servers ] }, #format => sub { $ntp_servers{$_[0]} } }, { label => N("Secondary time server:"), val => \$o->{var}{varserver2}, list => [ keys %$ntp_servers ] }, # format => sub { $ntp_servers{$_[0]} } }, { label => N("Third time server:"), val => \$o->{var}{varserver3}, list => [ keys %$ntp_servers ] }, # format => sub { $ntp_servers{$_[0]} } }, ], next => 'choose_region' }, choose_region => { name => N("Choose a timezone"), pre => sub { my (undef, $region, $country) = get_timezone(); $o->{var}{wiz_region} ||= $region; $o->{var}{wiz_country} ||= $country; }, data => [ { label => N("Choose a region:"), val => \$o->{var}{wiz_region}, list => [ keys %country ] }, ], post => sub { return 'choose_country' if ref $country{$o->{var}{wiz_region}}; }, next => 'test_server' }, choose_country => { name => N("Choose a timezone"), pre => sub { @country = ref $country{$o->{var}{wiz_region}} ? @{$country{$o->{var}{wiz_region}}} : (); }, data => [ { label => N("Choose a city:"), val => \$o->{var}{wiz_country}, list_ref => \@country }, ], next => 'test_server' }, test_server => { name => N("Testing the time servers availability") . "\n\n" . N("If the time server is not immediately available (network or other reason), there will be about a 30 second delay.") . "\n\n" . N("Press next to start the time servers test."), pre => sub { $o->{var}{wiz_timezone} = $o->{var}{wiz_region} . (ref $country{$o->{var}{wiz_region}} ? "/$o->{var}{wiz_country}" : ""); }, post => \&test, data => [ { label => N("Primary time server:"), val_ref => \$o->{var}{varserver1} }, { label => N("Secondary time server:"), val_ref => \$o->{var}{varserver2} }, { label => N("Third time server:"), val_ref => \$o->{var}{varserver3} }, { label => N("Time zone:"), val_ref => \$o->{var}{wiz_timezone} }, ], next => 'end' }, warning => { name => N("Warning."), data => [ { label => '' } ], ignore => 1, next => 'summary' }, server_not_responding => { name => join("\n", N("Warning."), N("The time servers are not responding. The causes could be:"), N("- non existent time servers"), N("- no outside network"), N("- other reasons..."), N("- You can try again to contact time servers, or save configuration without actually setting time."), ) , post => sub { if ($o->{var}{action} eq 'save_quit') { do_it(); 'end'; } else { 'test_server'; } }, data => [ { val => \$o->{var}{action}, list => [ keys %actions ], format => sub { $actions{$_[0]} } }, ], next => 'config', }, end => { name => N("Time server configuration saved") . "\n\n" . N("Your server can now act as a time server for your local network."), end => 1, no_back => 1, }, }; sub new { my ($class) = @_; bless $o, $class; } sub get_timezone { -f "/etc/sysconfig/clock" or return; my %conf = getVarsFromSh("/etc/sysconfig/clock"); my ($c, $r) = split "/", $conf{ZONE}; return $conf{ZONE}, $c, $r; } sub test { if ($::testing) { do_it(); return 'end'; } my $command = "/usr/sbin/ntpdate -q $o->{var}{varserver1}"; # $o->{var}{varserver2}"; my $_o = MDK::Wizard::Wizcommon_gtk3::gtk_log($command, "Test NTP server Responding..."); # system("/usr/sbin/ntpdate -q $o->{var}{varserver1} $o->{var}{varserver2}"); if (!($? >> 8)) { do_it(); return 'end'; } return 'server_not_responding'; } sub do_it { if ($::testing) { print "Would have written\nUTC=true\nZONE=$o->{var}{wiz_timezone}\nARC=false\n to /etc/sysconfig/clock\n\n"; print "Would have copied /usr/share/zoneinfo/$o->{var}{wiz_timezone} to /etc/localtime\n"; return; } my $file = "/etc/sysconfig/clock"; !-f "$file.orig" and cp_af($file, $file . ".orig"); local *NEW; output($file, qq(UTC=true ZONE=$o->{var}{wiz_timezone} ARC=false )); -f "/usr/share/zoneinfo/$o->{var}{wiz_timezone}" and cp_af("/usr/share/zoneinfo/$o->{var}{wiz_timezone}", "/etc/localtime"); -f "/etc/ntp/step-tickers" and cp_af("/etc/ntp/step-tickers", "/etc/ntp/step-tickers.orig"); output('/etc/ntp/step-tickers', join("\n", $o->{var}{varserver1}, $o->{var}{varserver2}, $o->{var}{varserver3}, undef)); substInFile { s/(# server clock.via.net)/$1\nserver $o->{var}{varserver1}/ } '/etc/ntp.conf' if -f '/etc/ntp.conf'; standalone::explanations("Wrote /etc/ntp/step-tickers, starting services"); my $command = "service crond stop; service atd stop; service ntpd stop; service ntpd start; service atd start; service crond start; /sbin/hwclock --systohc --utc"; MDK::Wizard::Wizcommon_gtk3::gtk_log($command, "Configuring NTP, ATD and CROND services..."); } 1;