/
NtpClient.rb
1404 lines (1200 loc) · 44 KB
/
NtpClient.rb
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
# File: modules/NtpClient.ycp
# Package: Configuration of ntp-client
# Summary: Data for configuration of ntp-client, input and output functions.
# Authors: Jiri Srain <jsrain@suse.cz>
#
# $Id$
#
# Representation of the configuration of ntp-client.
# Input and output routines.
require "yast"
require "yaml"
require "cfa/ntp_conf"
require "yast2/target_file" # required to cfa work on changed scr
module Yast
class NtpClientClass < Module
include Logger
# the default synchronization interval in minutes when running in the manual
# sync mode ("Synchronize without Daemon" option, ntp started from cron)
# Note: the UI field currently uses maximum of 60 minutes
DEFAULT_SYNC_INTERVAL = 5
# List of servers defined by the pool.ntp.org to get random ntp servers
#
# @see #http://www.pool.ntp.org/
RANDOM_POOL_NTP_SERVERS = ["0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org"].freeze
# Different kinds of records which the server can syncronize with and
# reference clock record
#
# @see http://doc.ntp.org/4.1.0/confopt.htm
# @see http://doc.ntp.org/4.1.0/clockopt.htm
SYNC_RECORDS = ["server", "__clock", "peer", "broadcast", "broadcastclient"].freeze
NTP_FILE = "/etc/chrony.conf".freeze
def main
Yast.import "UI"
textdomain "ntp-client"
Yast.import "Directory"
Yast.import "FileUtils"
Yast.import "Language"
Yast.import "Message"
Yast.import "Mode"
Yast.import "NetworkInterfaces"
Yast.import "PackageSystem"
Yast.import "Popup"
Yast.import "Progress"
Yast.import "Report"
Yast.import "Service"
Yast.import "SLPAPI"
Yast.import "Stage"
Yast.import "String"
Yast.import "Summary"
Yast.import "SuSEFirewall"
Yast.import "FileChanges"
# Abort function
# return boolean return true if abort
@AbortFunction = nil
# Data was modified?
@modified = false
# Write only, used during autoinstallation.
# Don't run services and SuSEconfig, it's all done at one place.
@write_only = false
# Read all ntp-client settings
# @return true on success
@ntp_records = []
@restrict_map = {}
# Should the daemon be started when system boots?
@run_service = true
# Should the time synchronized periodicaly?
@synchronize_time = false
# The interval of synchronization in minutes.
@sync_interval = DEFAULT_SYNC_INTERVAL
# The cron file name for the synchronization.
@cron_file = "/etc/cron.d/novell.ntp-synchronize"
# Service name of the NTP daemon
@service_name = "chronyd"
# Should the daemon be started in chroot environment?
@run_chroot = false
# Netconfig policy: for merging and prioritizing static and DHCP config.
# FIXME: get a public URL
# https://svn.suse.de/svn/sysconfig/branches/mt/dhcp6-netconfig/netconfig/doc/README
@ntp_policy = "auto"
# Index of the currently sellected item
@selected_index = -1
# The currently sellected item
@selected_record = {}
# Active Directory controller
@ad_controller = ""
# Should the firewall settings be changed?
@change_firewall = false
# Required packages
@required_packages = ["chrony"]
# ports in firewall to open
@firewall_services = ["service:ntp"]
# List of known NTP servers
# server address -> information
# address: the key repeated
# country: CC (uppercase)
# location: for displaying
# ...: (others are unused)
@ntp_servers = nil
# Mapping between country codes and country names ("CZ" -> "Czech Republic")
@country_names = nil
@simple_dialog = false
@config_has_been_read = false
@ntp_selected = false
# for lazy loading
@countries_already_read = false
@known_countries = {}
@random_pool_servers = RANDOM_POOL_NTP_SERVERS
@deleted_records = []
end
def add_to_deleted_records(records)
records.each do |record|
cfa = record["cfa_record"]
cfa_fudge = record["cfa_fudge_record"]
@deleted_records << cfa if cfa
@deleted_records << cfa_fudge if cfa_fudge
end
end
def PolicyIsAuto
@ntp_policy == "auto" || @ntp_policy == "STATIC *"
end
def PolicyIsNomodify
@ntp_policy == ""
end
def PolicyIsNonstatic
@ntp_policy != "" && @ntp_policy != "STATIC"
end
# Abort function
# @return blah blah lahjk
def Abort
@AbortFunction.nil? ? false : @AbortFunction.call == true
end
def go_next
return false if Abort()
Progress.NextStage if progress?
true
end
def progress?
Mode.normal
end
# Reads and returns all known countries with their country codes
#
# @return [Hash{String => String}] of known contries
#
# **Structure:**
#
# $[
# "CL" : "Chile",
# "FR" : "France",
# ...
# ]
def GetAllKnownCountries
# first point of dependence on yast2-country-data
if !@countries_already_read
@known_countries = Convert.convert(
Builtins.eval(
SCR.Read(
path(".target.ycp"),
Directory.find_data_file("country.ycp")
)
),
from: "any",
to: "map <string, string>"
)
@countries_already_read = true
@known_countries = {} if @known_countries.nil?
end
# workaround bug #241054: servers in United Kingdom are in domain .uk
# domain .gb does not exist - add UK to the list of known countries
if Builtins.haskey(@known_countries, "GB")
Ops.set(@known_countries, "UK", Ops.get(@known_countries, "GB", ""))
@known_countries = Builtins.remove(@known_countries, "GB")
end
deep_copy(@known_countries)
end
# Read current language (RC_LANG from sysconfig)
# @return two-letter language code (cs_CZ.UTF-8 -> CZ)
def GetCurrentLanguageCode
lang = Convert.to_string(SCR.Read(path(".sysconfig.language.RC_LANG")))
# second point of dependence on yast2-country-data
Language.GetGivenLanguageCountry(lang)
end
# Given a country code and a location returns a hash with pool
# ntp address for given country, country code and location
# @return [Hash{String => String}] ntp pool address for given country
def MakePoolRecord(country_code, location)
mycc = country_code.downcase
# There is no gb.pool.ntp.org only uk.pool.ntp.org
mycc = "uk" if mycc == "gb"
{
"address" => "#{mycc}.pool.ntp.org",
"country" => country_code,
"location" => location
}
end
# Get the list of known NTP servers
# @return a list of known NTP servers
def GetNtpServers
update_ntp_servers! if @ntp_servers.nil?
deep_copy(@ntp_servers)
end
# Get the mapping between country codea and names ("CZ" -> "Czech Republic")
# @return a map the country codes and names mapping
def GetCountryNames
if @country_names.nil?
@country_names = Convert.convert(
Builtins.eval(SCR.Read(path(".target.yast2"), "country.ycp")),
from: "any",
to: "map <string, string>"
)
end
if @country_names.nil?
Builtins.y2error("Failed to read country names")
@country_names = {}
end
deep_copy(@country_names)
end
# Get list of public NTP servers for a country
# @param [String] country two-letter country code
# @param [Boolean] terse_output display additional data (location etc.)
# @return [Array] of servers (usable as combo-box items)
def GetNtpServersByCountry(country, terse_output)
country_names = {}
servers = GetNtpServers()
if country.to_s != ""
servers.select! { |_server, attrs| attrs["country"] == country }
# bnc#458917 add country, in case data/country.ycp does not have it
pool_country_record = MakePoolRecord(country, "")
servers[pool_country_record["address"]] = pool_country_record
else
country_names = GetCountryNames()
end
default = false
servers.map do |server, attrs|
# Select the first occurrence of pool.ntp.org as the default option (bnc#940881)
selected = default ? false : default = server.end_with?("pool.ntp.org")
next Item(Id(server), server, selected) if terse_output
country_label = country.empty? ? country_names[attrs["country"]] || attrs["country"] : ""
label = server + country_server_label(attrs["location"].to_s, country_label.to_s)
Item(Id(server), label, selected)
end
end
def read_ntp_conf
if !FileUtils.Exists(NTP_FILE)
log.error("File #{NTP_FILE} does not exist")
return false
end
begin
ntp_conf.load
rescue StandardError => e
log.error("Failed to read #{NTP_FILE}: #{e.message}")
return false
end
load_ntp_records
log.info("Raw ntp conf #{ntp_conf.raw}")
true
end
def load_ntp_records
@ntp_records = ntp_conf.records.map do |record|
{
"type" => record.type,
"address" => record.value,
"options" => record.raw_options,
"comment" => record.comment.to_s,
"cfa_record" => record
}
end
end
# Read and parse /etc/ntp.conf
# @return true on success
def ProcessNtpConf
if @config_has_been_read
log.info "Configuration has been read already, skipping."
return false
end
return false unless read_ntp_conf
@config_has_been_read = true
fudge_records = Builtins.filter(@ntp_records) do |m|
Ops.get_string(m, "type", "") == "fudge"
end
fudge_map = Convert.convert(
Builtins.listmap(fudge_records) do |m|
key = Ops.get_string(m, "address", "")
{ key => m }
end,
from: "map <string, map>",
to: "map <string, map <string, any>>"
)
restrict_records = Builtins.filter(@ntp_records) do |m|
Ops.get_string(m, "type", "") == "restrict"
end
@restrict_map = Convert.convert(
Builtins.listmap(restrict_records) do |m|
key = Ops.get_string(m, "address", "")
value2 = {}
opts = Builtins.splitstring(
String.CutBlanks(Ops.get_string(m, "options", "")),
" \t"
)
if Ops.get(opts, 0, "") == "mask"
Ops.set(value2, "mask", Ops.get(opts, 1, ""))
Ops.set(opts, 0, "")
Ops.set(opts, 1, "")
else
Ops.set(value2, "mask", "")
end
Ops.set(
value2,
"options",
String.CutBlanks(Builtins.mergestring(opts, " "))
)
Ops.set(value2, "comment", Ops.get_string(m, "comment", ""))
value2["cfa_record"] = m["cfa_record"]
{ key => value2 }
end,
from: "map <string, map>",
to: "map <string, map <string, any>>"
)
@ntp_records = Builtins.filter(@ntp_records) do |m|
Ops.get_string(m, "type", "") != "fudge"
end
@ntp_records = Builtins.filter(@ntp_records) do |m|
Ops.get_string(m, "type", "") != "restrict"
end
@ntp_records = Convert.convert(
Builtins.maplist(@ntp_records) do |m|
if Builtins.haskey(fudge_map, Ops.get_string(m, "address", ""))
Ops.set(
m,
"fudge_options",
Ops.get_string(
fudge_map,
[Ops.get_string(m, "address", ""), "options"],
""
)
)
Ops.set(
m,
"fudge_comment",
Ops.get_string(
fudge_map,
[Ops.get_string(m, "address", ""), "comment"],
""
)
)
m["cfa_fudge_record"] = ntp_conf.records.find do |record|
record.type == "fudge" && record.value == m["address"]
end
end
m
end,
from: "list <map>",
to: "list <map <string, any>>"
)
# mark local clock to be local clock and not real servers
@ntp_records = Builtins.maplist(@ntp_records) do |p|
if Ops.get_string(p, "type", "") == "server" &&
Builtins.regexpmatch(
Ops.get_string(p, "address", ""),
"^127.127.[0-9]+.[0-9]+$"
)
Ops.set(p, "type", "__clock")
end
p
end
true
end
# Read the synchronization status, fill
# synchronize_time and sync_interval variables
# Return updated value of synchronize_time
def ReadSynchronization
crontab = SCR.Read(path(".cron"), @cron_file, "")
log.info("NTP Synchronization crontab entry: #{crontab}")
cron_entry = (crontab || []).fetch(0, {}).fetch("events", []).fetch(0, {})
@synchronize_time = cron_entry["active"] == "1"
sync_interval_entry = cron_entry.fetch("minute", "*/#{DEFAULT_SYNC_INTERVAL}")
log.info("MINUTE #{sync_interval_entry}")
@sync_interval = sync_interval_entry.tr("^[0-9]", "").to_i
log.info("SYNC_INTERVAL #{@sync_interval}")
@synchronize_time
end
# Read all ntp-client settings
# @return true on success
def Read
return true if @config_has_been_read
sl = 500
# We do not set help text here, because it was set outside
new_read_progress if progress?
# read network configuration
return false if !go_next
progress_orig = Progress.set(false)
NetworkInterfaces.Read
Progress.set(progress_orig)
read_policy!
GetNtpServers()
GetCountryNames()
# read current settings
return false if !go_next
if !Mode.installation && !PackageSystem.CheckAndInstallPackagesInteractive(["chrony"])
log.info("PackageSystem::CheckAndInstallPackagesInteractive failed")
return false
end
@run_service = Service.Enabled(@service_name)
# Poke to /var/lib/YaST if there is Active Directory controller address dumped in .ycp file
read_ad_address!
# Stay away if the user may have made changes which we cannot parse.
# But bnc#456553, no pop-ups for CLI.
failed = !Mode.commandline && !FileChanges.CheckFiles(["/etc/ntp.conf"])
ProcessNtpConf()
ReadSynchronization()
failed = true unless read_chroot_config!
if failed
# While calling "yast clone_system" it is possible that
# the ntp server has not already been installed at that time.
# (This would be done if yast2-ntp-client will be called in the UI)
# In that case the error popup will not be shown. (bnc#889557)
Report.Error(Message.CannotReadCurrentSettings) unless Mode.config
end
progress_orig2 = Progress.set(false)
SuSEFirewall.Read
Progress.set(progress_orig2)
return false if !go_next
Progress.Title(_("Finished")) if progress?
Builtins.sleep(sl)
return false if Abort()
@modified = false
true
end
# Function returns list of NTP servers used in the configuration.
#
# @return [Array<String>] of servers
def GetUsedNtpServers
used_servers = []
@ntp_records.each do |record|
used_servers << record["address"] if record["type"] == "server"
end
used_servers
end
# Checks whether all servers listed in the random_pool_servers list
# are used in the configuration.
#
# @return [Boolean] true if enabled
def IsRandomServersServiceEnabled
used_servers = GetUsedNtpServers()
RANDOM_POOL_NTP_SERVERS.all? { |s| used_servers.include? s }
end
# Removes all servers contained in the random_pool_servers list
# from the current configuration.
def DeActivateRandomPoolServersFunction
deleted_records, @ntp_records = @ntp_records.partition do |record|
record["type"] == "server" && RANDOM_POOL_NTP_SERVERS.include?(record["address"])
end
add_to_deleted_records(deleted_records)
nil
end
# Add servers needed for random_pool_servers function
# into the current configuration.
def ActivateRandomPoolServersFunction
# leave the current configuration if any
store_current_options = {}
Builtins.foreach(@ntp_records) do |one_record|
if Ops.get_string(one_record, "type", "") == "server" &&
Ops.get_string(one_record, "address", "") != ""
one_address = Ops.get_string(one_record, "address", "")
Ops.set(store_current_options, one_address, {})
Ops.set(
store_current_options,
[one_address, "options"],
Ops.get_string(one_record, "options", "")
)
end
end
# remove all old ones
DeActivateRandomPoolServersFunction()
deleted_records, @ntp_records = @ntp_records.partition do |record|
record["type"] == "server"
end
add_to_deleted_records(deleted_records)
Builtins.foreach(@random_pool_servers) do |one_server|
one_options = ""
if Builtins.haskey(store_current_options, one_server)
one_options = Ops.get_string(
store_current_options,
[one_server, "options"],
""
)
Builtins.y2milestone(
"Leaving current configuration for server '%1', options '%2'",
one_server,
one_options
)
end
@ntp_records <<
{
"address" => one_server,
"comment" => "# Random pool server, see http://www.pool.ntp.org/ " \
"for more information",
"options" => one_options,
"type" => "server"
}
end
nil
end
# Write all ntp-client settings
# @return true on success
def Write
# We do not set help text here, because it was set outside
new_write_progress if progress?
# write settings
return false if !go_next
# Restrict map records are written first to not mangle the config file
# (bsc#983486)
@ntp_records = restrict_map_records + @ntp_records
log.info "Writing settings #{@ntp_records}"
Report.Error(Message.CannotWriteSettingsTo("/etc/ntp.conf")) if !write_ntp_conf
write_and_update_policy
write_chroot_config
# restart daemon
return false if !go_next
# SuSEFirewall::Write checks on its own whether there are pending
# changes, so call it always. bnc#476951
progress_orig = Progress.set(false)
SuSEFirewall.Write
Progress.set(progress_orig)
check_service
update_cron_settings
return false if !go_next
Progress.Title(_("Finished")) if progress?
!Abort()
end
# Get all ntp-client settings from the first parameter
# (For use by autoinstallation.)
# @param [Hash] settings The YCP structure to be imported.
# @return [Boolean] True on success
def Import(settings)
settings = deep_copy(settings)
@synchronize_time = Ops.get_boolean(settings, "synchronize_time", false)
@sync_interval = Ops.get_integer(settings, "sync_interval", DEFAULT_SYNC_INTERVAL)
@run_service = Ops.get_boolean(settings, "start_at_boot", false)
@run_chroot = Ops.get_boolean(settings, "start_in_chroot", true)
# compatibility: configure_dhcp:true translates to ntp_policy:auto
config_dhcp = Ops.get_boolean(settings, "configure_dhcp", false)
@ntp_policy = Ops.get_string(
settings,
"ntp_policy",
config_dhcp ? "auto" : ""
)
@ntp_records = Ops.get_list(settings, "peers", [])
@ntp_records = Builtins.maplist(@ntp_records) do |p|
if Builtins.haskey(p, "key") && Builtins.haskey(p, "value")
Ops.set(p, "type", Ops.get_string(p, "key", ""))
Ops.set(p, "address", Ops.get_string(p, "value", ""))
if Builtins.haskey(p, "param")
Ops.set(p, "options", Ops.get_string(p, "param", ""))
end
end
next deep_copy(p)
end
# sanitize records
@ntp_records = @ntp_records.map { |r| sanitize_record(r) }
# restricts is a list of entries whereas restrict_map
# is a map with target key (ip, ipv4-tag, ipv6-tag,...).
restricts = settings["restricts"] || []
@restrict_map = {}
restricts.each do |entry|
target = entry.delete("target").strip
@restrict_map[target] = sanitize_record(entry)
end
@modified = true
true
end
# Dump the ntp-client settings to a single map
# (For use by autoinstallation.)
# @return [Hash] Dumped settings (later acceptable by Import ())
def Export
# restrict_map is a map with the key ip,ipv4-tag or ipv6-tag.
# This will be converted into a list in order to use it in
# autoyast XML file properly.
restricts = @restrict_map.collect do |target, values|
# cfa_record not needed for export
export_values = values.dup
export_values.delete("cfa_record")
export_values["target"] = target
export_values
end
peers = @ntp_records.dup
peers.each do |peer|
peer.delete("cfa_record")
peer.delete("cfa_fudge_record")
end
{
"synchronize_time" => @synchronize_time,
"sync_interval" => @sync_interval,
"start_at_boot" => @run_service,
"start_in_chroot" => @run_chroot,
"ntp_policy" => @ntp_policy,
"peers" => peers,
"restricts" => restricts
}
end
# Create a textual summary and a list of unconfigured cards
# @return [String] summary of the current configuration
def Summary
summary = ""
if @run_service
# summary string
summary = Summary.AddLine(
summary,
_("The NTP daemon starts when starting the system.")
)
else
# summary string
summary = Summary.AddLine(
summary,
_("The NTP daemon does not start automatically.")
)
end
types = {
# summary string, %1 is list of addresses
"server" => _(
"Servers: %1"
),
# summary string, %1 is list of addresses
"__clock" => _(
"Radio Clocks: %1"
),
# summary string, %1 is list of addresses
"peer" => _(
"Peers: %1"
),
# summary string, %1 is list of addresses
"broadcast" => _(
"Broadcast time information to: %1"
),
# summary string, %1 is list of addresses
"broadcastclient" => _(
"Accept broadcasted time information from: %1"
)
}
# if (config_dhcp)
# {
# summary = Summary::AddLine (summary,
# // summary string
# _("Configure NTP daemon via DHCP."));
# return summary;
# }
# netconfig policy
if PolicyIsAuto()
# summary string, FIXME
summary = Summary.AddLine(
summary,
_("Combine static and DHCP configuration.")
)
elsif PolicyIsNomodify()
# summary string, FIXME
summary = Summary.AddLine(summary, _("Static configuration only."))
else
# summary string, FIXME: too generic!
summary = Summary.AddLine(summary, _("Custom configuration policy."))
end
SYNC_RECORDS.each do |t|
type_records = @ntp_records.select { |r| r["type"] == t }
names = type_records.map { |r| r["address"].to_s }.select { |n| n != "" }
summary = Summary.AddLine(summary, "#{types[t]}#{names.join(", ")}") if !names.empty?
end
summary
end
# Test if a specified NTP server is reachable by IPv4 or IPv6 (bsc#74076),
# Firewall could have been blocked IPv6
# @param [String] server string host name or IP address of the NTP server
# @return [Boolean] true if NTP server answers properly
def reachable_ntp_server?(server)
ntp_test(server) || ntp_test(server, 6)
end
# Test NTP server answer for a given IP version.
# @param [String] server string host name or IP address of the NTP server
# @param [Fixnum] integer ip version to use (4 or 6)
# @return [Boolean] true if stderr does not include lookup error and exit
# code is 0
def ntp_test(server, ip_version = 4)
output = SCR.Execute(
path(".target.bash_output"),
# -t : seconds of timeout
# -Q: print only offset, if failed exit is non-zero
"LANG=C /usr/sbin/chronyd -#{ip_version} -t 30 -Q 'pool #{server} iburst'"
)
Builtins.y2milestone("chronyd test response: #{output}")
output["exit"] == 0
end
# Handle UI of NTP server test answers
# @param [String] server string host name or IP address of the NTP server
# @param [Symbol] verbosity `no_ui: ..., `transient_popup: pop up while scanning,
# `result_popup: also final pop up about the result
# @return [Boolean] true if NTP server answers properly
def TestNtpServer(server, verbosity)
return reachable_ntp_server?(server) if verbosity == :no_ui
ok = false
Yast::Popup.Feedback(_("Testing the NTP server..."), Message.takes_a_while) do
log.info("Testing reachability of server #{server}")
ok = reachable_ntp_server?(server)
end
if verbosity == :result_popup
if ok
# message report - result of test of connection to NTP server
Popup.Notify(_("Server is reachable and responds properly."))
else
# error message - result of test of connection to NTP server
# report error instead of simple message (#306018)
Report.Error(_("Server is unreachable or does not respond properly."))
end
end
ok
end
# Detect NTP servers present in the local network
# @param [Symbol] method symbol method of the detection (only `slp suported ATM)
# @return a list of found NTP servers
def DetectNtpServers(method)
if method == :slp
required_package = "yast2-slp"
# if package is not installed (in the inst-sys, it is: bnc#399659)
if !Stage.initial && !PackageSystem.Installed(required_package)
if !PackageSystem.CheckAndInstallPackages([required_package])
Report.Error(
Builtins.sformat(
_(
"Cannot search for NTP server in local network\nwithout package %1 installed.\n"
),
required_package
)
)
Builtins.y2warning("Not searching for local NTP servers via SLP")
return []
else
SCR.RegisterAgent(path(".slp"), term(:ag_slp, term(:SlpAgent)))
end
end
servers = SLPAPI.FindSrvs("service:ntp", "")
server_names = Builtins.maplist(servers) do |m|
Ops.get_string(m, "pcHost", "")
end
server_names = Builtins.filter(server_names) { |s| s != "" }
return deep_copy(server_names)
end
Builtins.y2error("Unknown detection method: %1", method)
[]
end
# Get the list of synchronization-related records
# @return a list of maps with keys type (eg. "server"), address and index.
def getSyncRecords
index = -1
@ntp_records.each_with_object([]) do |record, ret|
index += 1
type = record["type"]
next if !sync_record?(type)
ret << {
"type" => type,
"index" => index,
"address" => record["address"].to_s,
"device" => record["device"].to_s
}
end
end
# Select synchronization record
# @param [Fixnum] index integer, -1 for creating a new record
# @return [Boolean] true on success
def selectSyncRecord(index)
ret = true
unless (-1..@ntp_records.size - 1).cover?(index)
log.error("Record with index #{index} doesn't exist, creating new")
index = -1
ret = false
end
@selected_record = index == -1 ? {} : @ntp_records[index]
@selected_index = index
ret
end
# Find index of synchronization record
# @param [String] type string record type
# @param [String] address string address
# @return [Fixnum] index of the record if found, -1 otherwise
def findSyncRecord(type, address)
index = -1
ret = -1
Builtins.foreach(@ntp_records) do |m|
index = Ops.add(index, 1)
if type == Ops.get_string(m, "type", "") &&
address == Ops.get_string(m, "address", "")
ret = index
end
end
ret
end
# Store currently sellected synchronization record
# @return [Boolean] true on success
def storeSyncRecord
if @selected_index == -1
@ntp_records = Builtins.add(@ntp_records, @selected_record)
else
Ops.set(@ntp_records, @selected_index, @selected_record)
end
@modified = true
true
end
# Delete specified synchronization record
# @param [Fixnum] index integer index of record to delete
# @return [Boolean] true on success
def deleteSyncRecord(index)
unless (0..@ntp_records.size - 1).cover?(index)
log.error("Record with index #{index} doesn't exist")
return false
end
add_to_deleted_records([@ntp_records[index]])
@ntp_records.delete_at(index)
@modified = true
end
# Ensure that selected_record["options"] contains the option.
# (A set operation in a string)
def enableOptionInSyncRecord(option)
# careful, "burst" != "iburst"
old = Ops.get_string(@selected_record, "options", "")
old_l = Builtins.splitstring(old, " \t")
old_l = Builtins.add(old_l, option) if !Builtins.contains(old_l, option)
Ops.set(@selected_record, "options", Builtins.mergestring(old_l, " "))
nil
end
# Return required packages for auto-installation
# @return [Hash] of packages to be installed and to be removed
def AutoPackages
{ "install" => @required_packages, "remove" => [] }
end
private
# Remove blank spaces in values
#
# @note to avoid augeas parsing errors, comments should be sanitized by
# removing blank spaces at the beginning and adding line break.
def sanitize_record(record)
sanitized = record.dup
sanitized.each do |key, value|
if key.include?("comment")
value.sub!(/^ */, "")
value << "\n" unless value.include?("\n")
elsif value.respond_to?(:strip!)
value.strip!
end
end
sanitized
end