summaryrefslogtreecommitdiff
path: root/time-conf.in
blob: eda04858c60488fe60edba7f32d5379a6db288bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#!/usr/bin/env perl
#-*-perl-*-

# Time configurator. Designed to be architecture- and distribution independent.
#
# Copyright (C) 2000-2001 Ximian, Inc.
#
# Authors: Hans Petter Jansson <hpj@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/used:
#
# /usr/share/zoneinfo/zone.tab
# /etc/ntp.conf
# /etc/ntp/step-tickers
# /etc/localtime

# Running programs affected/used:
#
# date



require "___scriptsdir___/be.pl";


# --- Tool information --- #

$name = "time";
$version = "0.1.0";

$description =<<"end_of_description;";
       Configures your system clock, timezone and time server list.
end_of_description;

$progress_max = 365;


# --- System config file locations --- #

# We list each config file type with as many alternate locations as possible.
# They are tried in array order. First found = used.

@ntp_conf_names =         ( "/etc/ntp.conf" );
@ntp_step_tickers_names = ( "/etc/ntp/step-tickers" );
@zoneinfo_db_names =      ( "/usr/share/zoneinfo/zone.tab", "/usr/local/share/zoneinfo/zone.tab" );


# --- Internal configuration variables --- #

# Configuration is parsed/read to, and printed/written from, these temporary variables.

$cf_year = 0;
$cf_month = 0;
$cf_mday = 0;
$cf_hour = 0;
$cf_minute = 0;
$cf_second = 0;

$cf_timezone = "";

$cf_sync_active = 0;
@cf_servers = ();


# --- Configuration file manipulation --- #

# xntpd style /etc/ntp.conf
#
# <filtered lines>
# server <server>
# <filtered lines>
#
# Exists: Red Hat 6.x
#
# Absent:

sub read_ntp_conf
{
  my $ifh;
  local *FILE;
  
  # Find the file.
  
  $ifh = be_open_read_from_names(@ntp_conf_names);
  if (not $ifh) { return; }
  *FILE = $ifh;

  # Parse the file.
  
  be_report_info(13, "Reading time server list");
  
  while (<FILE>)
  {
    @line = split(/[ \n\r\t]+/, $_);
    if ($line[0] eq "") { shift(@line); }  # Leading whitespace.
    
    if ($line[0] eq "server" && $line[1] ne "127.127.1.0")  # Disregard loopback.
    { be_push_unique(\@cf_servers, $line[1]); }
  }
}


sub write_ntp_conf
{
  my ($ifh, $ofh);
  local (*INFILE, *OUTFILE);
  my $wrote_servers = 0;

  # Find the file.

  ($ifh, $ofh) = be_open_filter_write_from_names(@ntp_conf_names);
  if (not $ofh) { return; }  # No point if we can't write.
  *INFILE = $ifh; *OUTFILE = $ofh;

  # Write the file, preserving as much as possible from INFILE.

  be_report_info(11, "Saving time server list");

  while (<INFILE>)
  {
    @line = split(/[ \n\r\t]+/, $_);
    if ($line[0] eq "") { shift(@line); }  # Leading whitespace.

    if ($line[0] eq "server")
    {
      if ($line[1] eq "127.127.1.0") { print OUTFILE; }
      elsif (!$wrote_servers)
      {
        for $elem (@cf_servers) { print OUTFILE "server $elem\n"; }
        $wrote_servers = 1;
      }
    }
    else { print OUTFILE; }
  }
  
  if (!$wrote_servers)
  {
    for $elem (@cf_servers) { print OUTFILE "server $elem\n"; }
    $wrote_servers = 1;
  }
  
  close(OUTFILE);
  if (*INFILE) { close(INFILE); }
}


# Red Hat style /etc/ntp/step-tickers
#
# <server>
# <server>
# ...
#
# This file is used as source for servers from which to sync the system
# clock before xntpd is started. In Red Hat, this is done because xntpd
# refuses to sync the clock if it is too much off.
#
# Exists: Red Hat 6.x
#
# Absent:

sub write_ntp_step_tickers
{
  my $ofh;
  local *FILE;

  # Find the file (it might very well not exist beforehand).

  $ofh = be_open_write_from_names(@ntp_step_tickers_names);
  if (not $ofh) { return; }
  *FILE = $ofh;

  # Write the file.

  be_report_info(12, "Saving time server preload list");

  for $elem (@cf_servers) { print FILE "$elem\n"; }

  close(FILE);
}


# Red Hat (?) style /etc/localtime
#
# Copied from /usr/share/zoneinfo/Etc/<match>, if found.
# TODO: Otherwise, we generate it ourselves.
#
# Exists: Red Hat 6.x
#
# Absent:

sub write_localtime
{
  my ($tz, $name);
  my $zonebase = "/usr/share/zoneinfo/";

  be_report_info(10, "Setting timezone");

  $tz = "$zonebase$cf_timezone";

  if (stat($tz) ne "")
  {
    # Badly needs fixing & porting.

    $name = "/etc/localtime";

    (my $fullname = "$prefix/$name") =~ tr/\//\//s;  # '//' -> '/'
    be_report_info(1, "Writing timezone configuration to \"$fullname\"");

    ($name = "$prefix/$name") =~ tr/\//\//s;  # '//' -> '/'
    be_create_path($name);

    # Make a backup if the file already exists - if the user specified a prefix,
    # it might not.

    if (stat($name))
    {
      # NOTE: Might not work everywhere. Might be unsafe if the user is allowed
      # to specify a $name list somehow, in the future.

      be_run("cp $name $name.confsave");
    }

    # Replace the timezone info file.

    unlink $name;  # Important, since it might be a symlink.
    be_run("cp $tz $name");
  }
}


# --- XML parsing --- #


# Scan XML from standard input to an internal tree.

sub xml_parse
{
  # Scan XML to tree.

  $tree = &be_xml_scan;

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

  while (@$tree)
  {
    if ($$tree[0] eq "time") { xml_parse_time($$tree[1]); }

    shift @$tree;
    shift @$tree;
  }

  return($tree);
}


# <networking>...</networking>

sub xml_parse_time
{
  my $tree = $_[0];

  shift @$tree;  # Skip attributes.

  while (@$tree)
  {
    if    ($$tree[0] eq "year")     { $cf_year = be_xml_get_word($$tree[1]); }
    elsif ($$tree[0] eq "month")    { $cf_month = be_xml_get_word($$tree[1]); }
    elsif ($$tree[0] eq "monthday") { $cf_mday = be_xml_get_word($$tree[1]); }
    elsif ($$tree[0] eq "hour")     { $cf_hour = be_xml_get_word($$tree[1]); }
    elsif ($$tree[0] eq "minute")   { $cf_minute = be_xml_get_word($$tree[1]); }
    elsif ($$tree[0] eq "second")   { $cf_second = be_xml_get_word($$tree[1]); }
    elsif ($$tree[0] eq "timezone") { $cf_timezone = be_xml_get_word($$tree[1]); }
    elsif ($$tree[0] eq "synchronization") { xml_parse_synchronization($$tree[1]); }

    shift @$tree;
    shift @$tree;
  }
}


sub xml_parse_synchronization
{
  my $tree = $_[0];

  $cf_sync_active = be_read_boolean($$tree[0]->{active});
  shift @$tree;

  while (@$tree)
  {
    if ($$tree[0] eq "server")  { be_push_unique(\@cf_servers, be_xml_get_word($$tree[1])); }
    
    shift @$tree;
    shift @$tree;
  }
}


# --- XML printing --- #


sub xml_print
{
  print "<?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?>\n";
  print "<!DOCTYPE time []>\n\n";
  print "<time>\n";
  be_xml_enter ();

  be_xml_vspace ();
  be_xml_print ("<!-- Configuration starts here -->\n");
  be_xml_vspace ();

  be_xml_print ("<year>$cf_year</year>\n");
  be_xml_print ("<month>$cf_month</month>\n");
  be_xml_print ("<monthday>$cf_mday</monthday>\n");
  
  be_xml_vspace ();
  
  be_xml_print ("<hour>$cf_hour</hour>\n");
  be_xml_print ("<minute>$cf_minute</minute>\n");
  be_xml_print ("<second>$cf_second</second>\n");

  be_xml_vspace ();
  be_xml_print ("<timezone>$cf_timezone</timezone>\n");
  be_xml_vspace ();
  
  be_xml_vspace ();
  be_xml_print ("<synchronization active='", be_print_boolean_yesno($cf_sync_active), "'>\n");
  
  be_xml_enter ();
  
  for $server (@cf_servers)
  {
    be_xml_print ("<server>$server</server>\n");
  }
  
  be_xml_leave ();
  
  be_xml_print ("</synchronization>\n");
  be_xml_vspace ();
  
  be_xml_print ("<!-- End of configuration -->\n");
  be_xml_vspace ();

  be_xml_leave ();
  print "</time>\n";
}


# --- Get (read) config --- #


sub get_zone
{
  local *TZLIST;
  my $zone;
  my $diff_tool;
  my $size_search;
  my $size_test;

  *TZLIST = be_open_read_from_names(@zoneinfo_db_names);
  if (not *TZLIST) { return; }

  $diff_tool = be_locate_tool("diff");

  be_report_info(8, "Scanning timezones");

  # Get the filesize for /etc/localtime so that we don't have to execute
  # a diff for every file, only for file with the correct size. This speeds
  # up loading 
  $size_search = (stat("/etc/localtime"))[7];
  
  while (<TZLIST>)
  {
    if (/#/) { next; }                     # Skip comments.
    ($d, $d, $zone) = split /[\t ]+/, $_;  # Get 3rd column.
    chomp $zone;                           # Remove linefeeds.


    # See if this zone file matches the installed one.
    be_report_info(9, "Scanning timezones : $zone");

   $size_test = (stat("/usr/share/zoneinfo/$zone"))[7];
   if ($size_test eq $size_search)
   {
      if (!system "$diff_tool /usr/share/zoneinfo/$zone /etc/localtime >/dev/null 2>/dev/null")
      {
        # Found a match.
        last;
      }
    }
    
    $zone = "";
    be_print_progress();
  }
  
  $cf_timezone = $zone;
  close (TZLIST);
}


sub get_time
{
  my $datetext;
  
  be_report_info(7, "Querying system clock");
  
  $datetext = `date +%Y.%m.%d.%H:%M.%S.%z`;
  
  ($cf_year, $cf_month, $cf_mday, $cf_hour, $cf_minute, $cf_second) =
    ($datetext =~ /^([0-9]+).([0-9]+).([0-9]+).([0-9]+):([0-9]+).([0-9]+)/);

  # TODO: We should fall back to internal Perl functions here.
}


sub check_servers
{
  if (-f "/etc/rc.d/init.d/xntpd")
  {
    if (!system "/etc/rc.d/init.d/xntpd status >/dev/null 2>/dev/null")
    {
      $cf_sync_active = 1;
      be_report_info(2, "Found XNTPD enabled");
    }
    else
    {
      $cf_sync_active = 0;
      be_report_info(3, "Found XNTPD disabled");
    }
  }
  else
  {
    be_report_warning(1, "Could not find a way to check XNTPD status");
  }
}


sub get
{
  get_zone ();
  read_ntp_conf (); be_print_progress ();
  get_time (); be_print_progress ();
  check_servers (); be_print_progress ();

  be_end();
  xml_print ();
}


# --- Set (write) config --- #


sub set_time
{
  be_report_info(6, "Setting system clock");

  be_run(sprintf("date %02d%02d%02d%02d%04d.%02d >/dev/null 2>/dev/null",
                 $cf_month, $cf_mday, $cf_hour, $cf_minute, $cf_year, $cf_second));
}


sub restart_servers
{
  if ($cf_sync_active)
  {
    be_report_info(4, "Restarting XNTPD with new configuration");
    be_service_restart(81, "", "xntpd");
  }
  else
  {
    be_report_info(5, "Stopping XNTPD");
    be_service_disable(15, "", "xntpd");
  }
}


sub set
{
  xml_parse ();

  write_ntp_conf (); be_progress (10);
  write_ntp_step_tickers (); be_progress (20);
  write_localtime (); be_progress (30);

  if ($be_do_immediate)
  {
    set_time; be_progress (50);
    restart_servers; be_progress (80);
  }
  
  be_end();
}


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


sub filter
{
  xml_parse ();
  be_end();
  xml_print ();
}


# --- Main --- #

be_init($name, $version, $description, @ARGV);

# Do our thing.

if    ($be_operation eq "get")    { get; }
elsif ($be_operation eq "set")    { set; }
elsif ($be_operation eq "filter") { filter; }