Module: Yast::NetworkServicesDnsInclude

Defined in:
src/include/network/services/dns.rb

Constant Summary collapse

CUSTOM_RESOLV_POLICIES =

CWM wants id-value pairs

{
  "STATIC"          => "STATIC",
  "STATIC_FALLBACK" => "STATIC_FALLBACK"
}.freeze
NONE_LABEL =
"no".freeze
ANY_LABEL =
"any".freeze
NO_CHANGE_LABEL =
"no_change".freeze

Instance Method Summary collapse

Instance Method Details

#DNSMainDialog(_standalone) ⇒ Object

Standalone dialog only - embedded one is handled separately via CWMTab


609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'src/include/network/services/dns.rb', line 609

def DNSMainDialog(_standalone)
  caption = _("Hostname and Name Server Configuration")

  functions = {
    "init"  => fun_ref(method(:InitHnWidget), "void (string)"),
    "store" => fun_ref(method(:StoreHnWidget), "void (string, map)"),
    :abort  => fun_ref(method(:ReallyAbort), "boolean ()")
  }

  Wizard.HideBackButton

  ret = CWM.ShowAndRun(
    "widget_descr"       => @widget_descr_dns,
    "contents"           => @dns_contents,
    # dialog caption
    "caption"            => caption,
    "back_button"        => Label.BackButton,
    "next_button"        => Label.FinishButton,
    "fallback_functions" => functions
  )

  ret
end

#handleModifyResolvPolicy(key, _event) ⇒ Object


551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'src/include/network/services/dns.rb', line 551

def handleModifyResolvPolicy(key, _event)
  Builtins.y2milestone(
    "handleModifyResolvPolicy called: %1",
    UI.QueryWidget(Id("MODIFY_RESOLV"), :Value)
  )

  @resolver_modifiable = UI.QueryWidget(Id("MODIFY_RESOLV"), :Value) != :nomodify

  initPolicy(key)

  Builtins.y2milestone(
    "Exit: resolver_modifiable = %1",
    @resolver_modifiable
  )
  nil
end

#handlePolicy(_key, _event) ⇒ Object


513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'src/include/network/services/dns.rb', line 513

def handlePolicy(_key, _event)
  Builtins.y2milestone("handlePolicy")

  case UI.QueryWidget(Id("MODIFY_RESOLV"), :Value)
  when :custom
    SetHnItem("PLAIN_POLICY", UI.QueryWidget(Id("PLAIN_POLICY"), :Value))
  when :auto
    SetHnItem("PLAIN_POLICY", "auto")
  else
    SetHnItem("PLAIN_POLICY", "")
  end

  nil
end

#HandleResolverData(key, _event) ⇒ Object

Event handler for resolver data (nameservers, searchlist) enable or disable: is DHCP available?


424
425
426
427
428
429
430
431
# File 'src/include/network/services/dns.rb', line 424

def HandleResolverData(key, _event)
  # if this one is disabled, it means NM is in charge (see also initModifyResolvPolicy())
  if Convert.to_boolean(UI.QueryWidget(Id("MODIFY_RESOLV"), :Enabled))
    # thus, we should not re-enable already disabled widgets
    UI.ChangeWidget(Id(key), :Enabled, @resolver_modifiable)
  end
  nil
end

#has_dhcp?Boolean

Checks if any interface is configured to use DHCP


636
637
638
# File 'src/include/network/services/dns.rb', line 636

def has_dhcp?
  !LanItems.find_dhcp_ifaces.empty?
end

#InitDhcpHostname(_key) ⇒ Object

Init handler for DHCP_HOSTNAME


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
# File 'src/include/network/services/dns.rb', line 379

def InitDhcpHostname(_key)
  UI.ChangeWidget(Id("DHCP_HOSTNAME"), :Enabled, has_dhcp? && NetworkService.is_wicked)
  dhcp_hostname = DNS.dhcp_hostname

  items = [
    # translators: no device selected placeholder
    Item(Id(:none), _("no"), dhcp_hostname == :none),
    # translators: placeholder for "set hostname via any DHCP aware device"
    Item(Id(:any), _("yes: any"), dhcp_hostname == :any)
  ]

  config = Yast::Lan.yast_config
  iface_names = config.connections.to_a.select { |c| c.bootproto.dhcp? }.map(&:interface)
  items += iface_names.map do |iface|
    # translators: label is in form yes: <device name>
    Item(Id(iface), format(_("yes: %s"), iface), dhcp_hostname == iface)
  end

  UI.ReplaceWidget(
    Id("dhcp_hostname_method"),
    ComboBox(
      Id("DHCP_HOSTNAME"),
      "",
      items
    )
  )

  log.info("InitDhcpHostname: preselected item = #{dhcp_hostname}")

  nil
end

#InitHnSettingsObject

Initialize internal state according current system configuration.


296
297
298
299
300
# File 'src/include/network/services/dns.rb', line 296

def InitHnSettings
  @hn_settings = InitSettings()

  nil
end

#InitHnWidget(key) ⇒ Object

Default function to init the value of a widget. Used for push buttons.


352
353
354
355
356
357
# File 'src/include/network/services/dns.rb', line 352

def InitHnWidget(key)
  value = Ops.get(@hn_settings, key)
  UI.ChangeWidget(Id(key), :Value, value)

  nil
end

#initHostnameGlobal(_key) ⇒ Object

Used in GUI mode - initializes widgets according hn_settings


570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'src/include/network/services/dns.rb', line 570

def initHostnameGlobal(_key)
  InitHnSettings()

  Builtins.foreach(
    Convert.convert(
      Map.Keys(@hn_settings),
      from: "list",
      to:   "list <string>"
    )
  ) { |key2| InitHnWidget(key2) }
  # disable those if NM is in charge
  disable_unconfigureable_items(
    ["NAMESERVER_1", "NAMESERVER_2", "NAMESERVER_3", "SEARCHLIST_S"],
    false
  )

  nil
end

#initialize_network_services_dns(include_target) ⇒ Object


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
# File 'src/include/network/services/dns.rb', line 37

def initialize_network_services_dns(include_target)
  textdomain "network"

  Yast.import "CWM"
  Yast.import "DNS"
  Yast.import "Hostname"
  Yast.import "IP"
  Yast.import "Label"
  Yast.import "LanItems"
  Yast.import "Popup"
  Yast.import "Map"
  Yast.import "NetworkService"

  Yast.include include_target, "network/routines.rb"
  Yast.include include_target, "network/widgets.rb"
  Yast.include include_target, "network/lan/help.rb"

  # If there's a process modifying resolv.conf, we warn the user before
  # letting him change things that will be overwritten anyway.
  # See also #61000.
  @resolver_modifiable = false

  # original setup, used to determine whether data have been modified
  @settings_orig = {}

  # CWM buffer for both dialogs.  Note that NAMESERVERS and SEARCHLIST
  # are lists and their widgets are suffixed.
  @hn_settings = {}

  @widget_descr_dns = {
    "HOSTNAME"        => {
      "widget"            => :textentry,
      "label"             => Label.HostName,
      "opt"               => [],
      "help"              => Ops.get_string(@help, "hostname_global", ""),
      "valid_chars"       => Hostname.ValidChars,
      "validate_type"     => :function_no_popup,
      "validate_function" => fun_ref(
        method(:ValidateHostname),
        "boolean (string, map)"
      ),
      # validation error popup
      "validate_help"     => Ops.add(
        _("The hostname is invalid.") + "\n",
        Hostname.ValidHost
      )
    },
    "HOSTNAME_GLOBAL" => {
      "widget" => :empty,
      "init"   => fun_ref(
        method(:initHostnameGlobal),
        "void (string)"
      ),
      "store"  => fun_ref(
        method(:storeHostnameGlobal),
        "void (string, map)"
      )
    },
    "DHCP_HOSTNAME"   => {
      "widget"        => :custom,
      "custom_widget" => HBox(
        Label(_("Set Hostname via DHCP")),
        HSpacing(2),
        ReplacePoint(Id("dhcp_hostname_method"), Empty()),
        ReplacePoint(Id("dh_host_text"), Empty())
      ),
      "init"          => fun_ref(method(:InitDhcpHostname), "void (string)"),
      "store"         => fun_ref(method(:StoreDhcpHostname), "void (string, map)")
    },
    "MODIFY_RESOLV"   => {
      "widget" => :combobox,
      "label"  => _("&Modify DNS Configuration"),
      "opt"    => [:notify],
      "items"  => [
        [:nomodify, _("Only Manually")],
        [:auto, _("Use Default Policy")],
        [:custom, _("Use Custom Policy")]
      ],
      "init"   => fun_ref(method(:initModifyResolvPolicy), "void (string)"),
      "handle" => fun_ref(
        method(:handleModifyResolvPolicy),
        "symbol (string, map)"
      ),
      "help"   => Ops.get_string(@help, "dns_config_policy", "")
    },
    "PLAIN_POLICY"    => {
      "widget" => :combobox,
      "label"  => _("&Custom Policy Rule"),
      "opt"    => [:editable],
      "items"  => CUSTOM_RESOLV_POLICIES.to_a,
      "init"   => fun_ref(method(:initPolicy), "void (string)"),
      "handle" => fun_ref(method(:handlePolicy), "symbol (string, map)"),
      "help"   => ""
    },
    "NAMESERVER_1"    => {
      "widget"            => :textentry,
      "label"             => _("Name Server &1"),
      "opt"               => [],
      "help"              => "",
      # at "SEARCHLIST_S"
      "handle"            => fun_ref(
        method(:HandleResolverData),
        "symbol (string, map)"
      ),
      "valid_chars"       => IP.ValidChars,
      "validate_type"     => :function_no_popup,
      "validate_function" => fun_ref(
        method(:ValidateIP),
        "boolean (string, map)"
      ),
      # validation error popup
      "validate_help"     => _("The IP address of the name server is invalid.") + "\n\n" +
        IP.Valid4 + "\n\n" + IP.Valid6
    },
    # NAMESERVER_2 and NAMESERVER_3 are cloned in the dialog function
    "SEARCHLIST_S"    => {
      "widget"            => :multi_line_edit,
      "label"             => _("Do&main Search"),
      "opt"               => [],
      "help"              => Ops.get_string(@help, "searchlist_s", ""),
      "handle"            => fun_ref(
        method(:HandleResolverData),
        "symbol (string, map)"
      ),
      "validate_type"     => :function,
      "validate_function" => fun_ref(
        method(:ValidateSearchList),
        "boolean (string, map)"
      )
    }
  }

  Ops.set(
    @widget_descr_dns,
    "NAMESERVER_2",
    Ops.get(@widget_descr_dns, "NAMESERVER_1", {})
  )
  Ops.set(
    @widget_descr_dns,
    "NAMESERVER_3",
    Ops.get(@widget_descr_dns, "NAMESERVER_1", {})
  )
  Ops.set(@widget_descr_dns, ["NAMESERVER_2", "label"], _("Name Server &2"))
  Ops.set(@widget_descr_dns, ["NAMESERVER_3", "label"], _("Name Server &3"))

  @dns_contents = VBox(
    VBox(
      HBox(
        "HOSTNAME",
        "HOSTNAME_GLOBAL" # global help, init, store for all dialog
      ),
      VSpacing(0.49),
      VBox(
        Left("DHCP_HOSTNAME")
      )
    ),
    VSpacing(1),
    Left(HBox("MODIFY_RESOLV", HSpacing(1), "PLAIN_POLICY")),
    Frame(
      _("Name Servers and Domain Search List"),
      VBox(
        VSquash(
          HBox(
            HWeight(1, VBox("NAMESERVER_1", "NAMESERVER_2", "NAMESERVER_3")),
            HSpacing(1),
            HWeight(1, "SEARCHLIST_S")
          )
        ),
        VSpacing(0.49)
      )
    ),
    VStretch()
  )

  @dns_td = {
    "resolv" => {
      "header"       => _("Hostname/DNS"),
      "contents"     => @dns_contents,
      "widget_names" => [
        "HOSTNAME",
        "HOSTNAME_GLOBAL",
        "DOMAIN",
        "DHCP_HOSTNAME",
        "MODIFY_RESOLV",
        "PLAIN_POLICY",
        "NAMESERVER_1",
        "NAMESERVER_2",
        "NAMESERVER_3",
        "SEARCHLIST_S"
      ]
    }
  }
end

#initModifyResolvPolicy(_key) ⇒ Object


538
539
540
541
542
543
544
545
546
547
548
549
# File 'src/include/network/services/dns.rb', line 538

def initModifyResolvPolicy(_key)
  Builtins.y2milestone("initModifyResolvPolicy")

  # first initialize correctly
  default = modify_resolv_default

  UI.ChangeWidget(Id("MODIFY_RESOLV"), :Value, default)
  # then disable if needed
  disable_unconfigureable_items(["MODIFY_RESOLV"], false)

  nil
end

#initPolicy(_key) ⇒ Object


492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'src/include/network/services/dns.rb', line 492

def initPolicy(_key)
  # first initialize correctly
  Builtins.y2milestone(
    "initPolicy: %1",
    UI.QueryWidget(Id("MODIFY_RESOLV"), :Value)
  )
  if UI.QueryWidget(Id("MODIFY_RESOLV"), :Value) == :custom
    UI.ChangeWidget(Id("PLAIN_POLICY"), :Enabled, true)
    if UI.QueryWidget(Id("PLAIN_POLICY"), :Value) == ""
      UI.ChangeWidget(Id("PLAIN_POLICY"), :Value, DNS.resolv_conf_policy)
    end
  else
    UI.ChangeWidget(Id("PLAIN_POLICY"), :Value, "")
    UI.ChangeWidget(Id("PLAIN_POLICY"), :Enabled, false)
  end
  # then disable if needed
  disable_unconfigureable_items(["PLAIN_POLICY"], false)

  nil
end

#InitSettingsObject


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'src/include/network/services/dns.rb', line 239

def InitSettings
  settings = {
    "HOSTNAME"     => DNS.hostname,
    "PLAIN_POLICY" => DNS.resolv_conf_policy
  }
  # the rest is not so straightforward,
  # because we have list variables but non-list widgets

  # domain search
  searchstring = Builtins.mergestring(DNS.searchlist, "\n")
  # #49094: populate the search list
  # #437759: discard 'site', nobody really wants that pre-set
  if searchstring == "" && Ops.get_string(settings, "DOMAIN", "") != "site"
    searchstring = Ops.get_string(settings, "DOMAIN", "")
  end
  Ops.set(settings, "SEARCHLIST_S", searchstring)
  Ops.set(settings, "NAMESERVER_1", DNS.nameservers[0].to_s)
  Ops.set(settings, "NAMESERVER_2", DNS.nameservers[1].to_s)
  Ops.set(settings, "NAMESERVER_3", DNS.nameservers[2].to_s)

  @settings_orig = deep_copy(settings)

  deep_copy(settings)
end

#modify_resolv_defaultObject


528
529
530
531
532
533
534
535
536
# File 'src/include/network/services/dns.rb', line 528

def modify_resolv_default
  if DNS.resolv_conf_policy.nil? || DNS.resolv_conf_policy == ""
    Id(:nomodify)
  elsif DNS.resolv_conf_policy == "auto" || DNS.resolv_conf_policy == "STATIC *"
    Id(:auto)
  else
    Id(:custom)
  end
end

#NonEmpty(l) ⇒ Object


233
234
235
236
# File 'src/include/network/services/dns.rb', line 233

def NonEmpty(l)
  l = deep_copy(l)
  Builtins.filter(l) { |s| s != "" }
end

#ReallyAbortInstObject


603
604
605
# File 'src/include/network/services/dns.rb', line 603

def ReallyAbortInst
  Popup.ConfirmAbort(:incomplete)
end

#SetHnItem(key, value) ⇒ Object

Function for updating actual hostname settings.


305
306
307
308
309
310
311
312
313
314
315
# File 'src/include/network/services/dns.rb', line 305

def SetHnItem(key, value)
  Builtins.y2milestone(
    "hn_settings[ \"%1\"] changes '%2' -> '%3'",
    key,
    @hn_settings[key].to_s,
    value
  )
  @hn_settings[key] = value

  nil
end

#SetHostname(value) ⇒ Object

Function for updating actual hostname.


318
319
320
321
322
323
# File 'src/include/network/services/dns.rb', line 318

def SetHostname(value)
  value = deep_copy(value)
  SetHnItem("HOSTNAME", value)

  nil
end

#SetNameserver1(value) ⇒ Object

Function for updating ip address of first nameserver.


326
327
328
329
330
331
# File 'src/include/network/services/dns.rb', line 326

def SetNameserver1(value)
  value = deep_copy(value)
  SetHnItem("NAMESERVER_1", value)

  nil
end

#SetNameserver2(value) ⇒ Object

Function for updating ip address of second nameserver.


334
335
336
337
338
339
# File 'src/include/network/services/dns.rb', line 334

def SetNameserver2(value)
  value = deep_copy(value)
  SetHnItem("NAMESERVER_2", value)

  nil
end

#SetNameserver3(value) ⇒ Object

Function for updating ip address of third nameserver.


342
343
344
345
346
347
# File 'src/include/network/services/dns.rb', line 342

def SetNameserver3(value)
  value = deep_copy(value)
  SetHnItem("NAMESERVER_3", value)

  nil
end

#StoreDhcpHostname(_key, _event) ⇒ Object

Store handler for DHCP_HOSTNAME


412
413
414
415
416
417
# File 'src/include/network/services/dns.rb', line 412

def StoreDhcpHostname(_key, _event)
  return if !UI.QueryWidget(Id("DHCP_HOSTNAME"), :Enabled)
  DNS.dhcp_hostname = UI.QueryWidget(Id("DHCP_HOSTNAME"), :Value)

  nil
end

#StoreHnSettingsObject

Stores actual hostname settings.


289
290
291
292
293
# File 'src/include/network/services/dns.rb', line 289

def StoreHnSettings
  StoreSettings(@hn_settings)

  nil
end

#StoreHnWidget(key, _event) ⇒ Object

Default function to store the value of a widget.


362
363
364
365
366
367
# File 'src/include/network/services/dns.rb', line 362

def StoreHnWidget(key, _event)
  value = UI.QueryWidget(Id(key), :Value)
  SetHnItem(key, value)

  nil
end

#storeHostnameGlobal(_key, event) ⇒ Object

Used in GUI mode - updates and stores actuall hostname settings according dialog widgets content. It calls store handler for every widget from hn_settings with event as an option.


593
594
595
596
597
598
599
600
601
# File 'src/include/network/services/dns.rb', line 593

def storeHostnameGlobal(_key, event)
  @hn_settings.keys.each do |key2|
    StoreHnWidget(key2, event) if UI.QueryWidget(Id(key2), :Enabled)
  end

  StoreHnSettings()

  nil
end

#StoreSettings(settings) ⇒ Object


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'src/include/network/services/dns.rb', line 265

def StoreSettings(settings)
  settings = deep_copy(settings)
  nameservers = [
    Ops.get_string(settings, "NAMESERVER_1", ""),
    Ops.get_string(settings, "NAMESERVER_2", ""),
    Ops.get_string(settings, "NAMESERVER_3", "")
  ]
  searchlist = Builtins.splitstring(
    Ops.get_string(settings, "SEARCHLIST_S", ""),
    " ,\n\t"
  )

  DNS.hostname = Ops.get_string(settings, "HOSTNAME", "")
  valid_nameservers = NonEmpty(nameservers).each_with_object([]) do |ip_str, all|
    all << IPAddr.new(ip_str) if IP.Check(ip_str)
  end
  DNS.nameservers = valid_nameservers
  DNS.searchlist = NonEmpty(searchlist)
  DNS.resolv_conf_policy = settings["PLAIN_POLICY"]

  nil
end

#use_dhcp_hostname?Boolean

Checks whether setting hostname via DHCP is allowed


374
375
376
# File 'src/include/network/services/dns.rb', line 374

def use_dhcp_hostname?
  UI.QueryWidget(Id("DHCP_HOSTNAME"), :Value) != NONE_LABEL
end

#ValidateHostname(key, _event) ⇒ Object

Validator for hostname, no_popup


437
438
439
440
441
442
443
444
445
446
# File 'src/include/network/services/dns.rb', line 437

def ValidateHostname(key, _event)
  dhn = has_dhcp? && use_dhcp_hostname?
  # If the names are set by dhcp, the user may enter backup values
  # here - N#28427. That is, host and domain name are optional then.
  # For static config, they are mandatory.
  value = Convert.to_string(UI.QueryWidget(Id(key), :Value))

  return Hostname.Check(value) if !dhn || value != ""
  true
end

#ValidateSearchList(key, _event) ⇒ Object

Validator for the search list


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
# File 'src/include/network/services/dns.rb', line 452

def ValidateSearchList(key, _event)
  value = Convert.to_string(UI.QueryWidget(Id(key), :Value))
  sl = NonEmpty(Builtins.splitstring(value, " ,\n\t"))
  error = ""

  if Ops.greater_than(Builtins.size(sl), 6)
    # Popup::Error text
    error = Builtins.sformat(
      _("The search list can have at most %1 domains."),
      6
    )
  elsif Ops.greater_than(Builtins.size(Builtins.mergestring(sl, " ")), 256)
    # Popup::Error text
    error = Builtins.sformat(
      _("The search list can have at most %1 characters."),
      256
    )
  end
  Builtins.foreach(sl) do |s|
    if !Hostname.CheckDomain(s)
      # Popup::Error text
      error = Ops.add(
        Ops.add(
          Builtins.sformat(_("The search domain '%1' is invalid."), s),
          "\n"
        ),
        Hostname.ValidDomain
      )
      break
    end
  end

  if error != ""
    UI.SetFocus(Id(key))
    Popup.Error(error)
    return false
  end
  true
end