Class: Yast::DNSClass

Inherits:
Module
  • Object
show all
Defined in:
../../src/modules/DNS.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) DefaultWriteHostname



214
215
216
217
218
219
220
221
222
# File '../../src/modules/DNS.rb', line 214

def DefaultWriteHostname
  # FaTe#303875: Introduce a switch regarding 127.0.0.2 entry in /etc/hosts
  whth = ProductFeatures.GetBooleanFeature(
    "globals",
    "write_hostname_to_hosts"
  )
  Builtins.y2milestone("write_hostname_to_hosts default value: %1", whth)
  whth
end

- (Object) Export

Dump the DNS settings to a map, for autoinstallation use.

Returns:

  • autoinstallation settings



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File '../../src/modules/DNS.rb', line 522

def Export
  expdns = {}
  if Ops.greater_than(Builtins.size(@hostname), 0)
    Ops.set(expdns, "hostname", @hostname)
  end
  if Ops.greater_than(Builtins.size(@domain), 0)
    Ops.set(expdns, "domain", @domain)
  end
  if Ops.greater_than(Builtins.size(@nameservers), 0)
    Ops.set(expdns, "nameservers", Builtins.eval(@nameservers))
  end
  if Ops.greater_than(Builtins.size(@searchlist), 0)
    Ops.set(expdns, "searchlist", Builtins.eval(@searchlist))
  end
  Ops.set(expdns, "dhcp_hostname", @dhcp_hostname)
  #TODO: test if it really works with empty string
  Ops.set(expdns, "resolv_conf_policy", @resolv_conf_policy)
  #bnc#576495, FaTE#305281 - clone write_hostname, too
  Ops.set(expdns, "write_hostname", @write_hostname)
  deep_copy(expdns)
end

- (Object) GetDHCPHostnameIP

Get current hostname and IP Address if these are set by DHCP

Returns:

  • map with ip, hostname_short and hostname_fq keys



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
# File '../../src/modules/DNS.rb', line 107

def GetDHCPHostnameIP
  ret = {}

  output = Convert.to_map(
    SCR.Execute(path(".target.bash_output"), "hostname -i")
  )
  Ops.set(
    ret,
    "ip",
    Builtins.deletechars(Ops.get_string(output, "stdout", ""), " \n")
  )

  output = Convert.to_map(
    SCR.Execute(path(".target.bash_output"), "hostname")
  )
  Ops.set(
    ret,
    "hostname_short",
    Builtins.deletechars(Ops.get_string(output, "stdout", ""), " \n")
  )

  output = Convert.to_map(
    SCR.Execute(path(".target.bash_output"), "hostname -f")
  )
  Ops.set(
    ret,
    "hostname_fq",
    Builtins.deletechars(Ops.get_string(output, "stdout", ""), " \n")
  )

  deep_copy(ret)
end

- (Object) GetHostnameFromGetent(line)

Handles input as one line of getent output. Returns first hostname found on the line (= canonical hostname).

Parameters:

  • line (String)

    in /etc/hosts format

Returns:

  • canonical hostname from given line



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
# File '../../src/modules/DNS.rb', line 145

def GetHostnameFromGetent(line)
  #  line is expected same format as is used in /etc/hosts without additional
  #  comments (getent removes comments from the end).
  #
  #  /etc/hosts line is formatted this way (man 5 hosts):
  #
  #      <ip address> <canonical hostname> [<alias> ...]
  #
  #  - field separators are at least one space and/or tab.
  #  - <canonical hostname>, in generic it is "a computer's unique name". In case
  #  of DNS world, <canonical hostname> is FQDN ("A" record), then <hostname> is
  #  <canonical hostname> without domain part. For example:
  #
  #      foo.example.com. IN A 1.2.3.4
  #
  #  <canonical hostname> => foo.example.com
  #  <hostname> => foo
  #
  canonical_hostname = Builtins.regexpsub(
    line,
    Builtins.sformat("^[%1]+[[:blank:]]+(.*)", IP.ValidChars),
    "\\1"
  )

  canonical_hostname = String.FirstChunk(canonical_hostname, " \t\n")
  canonical_hostname = String.CutBlanks(canonical_hostname)

  if !Hostname.CheckDomain(canonical_hostname) &&
      !Hostname.Check(canonical_hostname)
    Builtins.y2error(
      "GetHostnameFromGetent: Invalid hostname detected (%1)",
      canonical_hostname
    )
    Builtins.y2error("GetHostnameFromGetent: input params - begin")
    Builtins.y2error("%1", line)
    Builtins.y2error("GetHostnameFromGetent: input params - end")

    return ""
  end

  Builtins.y2milestone(
    "GetHostnameFromGetEnt: canonical hostname => (%1)",
    canonical_hostname
  )

  canonical_hostname
end

- (Object) Import(settings)

Get all the DNS configuration from a map. When called by dns_auto (preparing autoinstallation data) the map may be empty.

Parameters:

  • settings (Hash)

    autoinstallation settings

Returns:

  • true if success



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
# File '../../src/modules/DNS.rb', line 470

def Import(settings)
  settings = deep_copy(settings)
  @dhcp_hostname = Ops.get_boolean(settings, "dhcp_hostname", false)
  #if not defined, set to 'auto'
  @resolv_conf_policy = Ops.get_string(
    settings,
    "resolv_conf_policy",
    "auto"
  )

  # user-defined value has higher priority - FaTE#305281
  if Builtins.haskey(settings, "write_hostname")
    @write_hostname = Ops.get_boolean(settings, "write_hostname", false)
  else
    # otherwise, use control.xml default
    @write_hostname = DefaultWriteHostname()
  end

  # user-defined <hostname>
  if Builtins.haskey(settings, "hostname")
    @hostname = Ops.get_string(settings, "hostname", "")
    @domain = Ops.get_string(settings, "domain", "") # empty is not a bug, bnc#677471
  else
    # otherwise, check 1) install.inf 2) /etc/HOSTNAME
    ReadHostname()
    # if nothing is found, generate a random one
    ProposeHostname()
  end

  @nameservers = Builtins.eval(Ops.get_list(settings, "nameservers", []))
  @searchlist = Builtins.eval(Ops.get_list(settings, "searchlist", []))
  @modified = true
  # empty settings means that we're probably resetting the config
  # thus, setup is not initialized anymore
  @initialized = settings != {}

  Builtins.y2milestone("DNS Import:")
  Builtins.y2milestone("nameservers=%1", @nameservers)
  Builtins.y2milestone("searchlist=%1", @searchlist)
  Builtins.y2milestone("hostname=%1", @hostname)
  Builtins.y2milestone("domain=%1", @domain)
  Builtins.y2milestone(
    "dhcp_hostname=%1, write_hostname=%2",
    @dhcp_hostname,
    @write_hostname
  )

  true
end

- (Boolean) IsHostLocal(check_host)

Check if hostname or IP address is local computer Used to determine if LDAP server is local (and it should be checked if required schemes are included Calls Read () function before querying any data

Parameters:

  • check_host (String)

    string hostname or IP address to check

Returns:

  • (Boolean)

    true if hostname is local host



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
# File '../../src/modules/DNS.rb', line 620

def IsHostLocal(check_host)
  Read()
  NetworkInterfaces.Read
  dhcp_data = {}

  if Ops.greater_than(
      Builtins.size(NetworkInterfaces.Locate("BOOTPROTO", "dhcp")),
      0
    ) || @dhcp_hostname
    dhcp_data = GetDHCPHostnameIP()
    Builtins.y2milestone("Got DHCP-configured data: %1", dhcp_data)
  end
  # FIXME: May not work properly in following situations:
  # 	- multiple addresses per interface
  #     - aliases in /etc/hosts
  # 	- IPADDR=IP/24

  # loopback interface
  return true if check_host == "127.0.0.1" || check_host == "::1"
  # localhost hostname
  if check_host == "localhost" || check_host == "localhost.localdomain"
    return true
  end

  # IPv4 address
  if IP.Check4(check_host)
    if Ops.greater_than(
        Builtins.size(NetworkInterfaces.Locate("IPADDR", check_host)),
        0
      ) ||
        Ops.get(dhcp_data, "ip", "") == check_host
      return true
    end
  # IPv6 address
  elsif IP.Check6(check_host)
    Builtins.y2debug(
      "TODO make it similar to IPv4 after other code adapted to IPv6"
    )
  # short hostname
  elsif Builtins.findfirstof(check_host, ".") == nil
    if Builtins.tolower(check_host) == Builtins.tolower(@hostname) ||
        Ops.get(dhcp_data, "hostname_short", "") == check_host
      return true
    end
  else
    if Builtins.tolower(check_host) ==
        Builtins.tolower(Ops.add(Ops.add(@hostname, "."), @domain)) ||
        Ops.get(dhcp_data, "hostname_fq", "") == check_host
      return true
    end
  end
  false
end

- (Object) main



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
# File '../../src/modules/DNS.rb', line 36

def main
  Yast.import "UI"
  textdomain "network"

  Yast.import "NetHwDetection"
  Yast.import "Hostname"
  Yast.import "IP"
  Yast.import "NetworkInterfaces"
  Yast.import "ProductFeatures"
  Yast.import "Progress"
  Yast.import "Service"
  Yast.import "String"

  Yast.include self, "network/routines.rb"
  Yast.include self, "network/runtime.rb"

  # Should the hostname be proposed? #152218
  @proposal_valid = false

  # Short Hostname
  @hostname = ""

  # Domain Name (not including the host part)
  @domain = ""

  @nameservers = []
  @searchlist = []

  @dhcp_hostname = false
  @write_hostname = false
  @resolv_conf_policy = ""

  # fully qualified
  @oldhostname = ""

  # Data was modified?
  @modified = false

  # resolver config file location
  @resolv_conf = "/etc/resolv.conf"

  # True if DNS is already read
  @initialized = false
end

- (Object) ProposeHostname



283
284
285
286
287
288
289
290
291
# File '../../src/modules/DNS.rb', line 283

def ProposeHostname
  if @hostname == "linux"
    Builtins.srandom
    @hostname = Ops.add("linux-", String.Random(4)) # #157107
    @modified = true
  end

  nil
end

- (Object) Read

Reads current DNS and hostname settings Includes Host,NetworkConfig::Read

Returns:

  • true if success



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
# File '../../src/modules/DNS.rb', line 296

def Read
  return true if @initialized == true

  tmp1 = Convert.to_string(
    SCR.Read(path(".sysconfig.network.dhcp.DHCLIENT_SET_HOSTNAME"))
  )
  @dhcp_hostname = tmp1 == "yes"
  tmp2 = Convert.to_string(
    SCR.Read(path(".sysconfig.network.dhcp.WRITE_HOSTNAME_TO_HOSTS"))
  )
  @write_hostname = tmp2 == "yes"

  @resolv_conf_policy = Convert.to_string(
    SCR.Read(path(".sysconfig.network.config.NETCONFIG_DNS_POLICY"))
  )
  resolvlist = Builtins.splitstring(
    Convert.to_string(
      SCR.Read(
        path(".sysconfig.network.config.NETCONFIG_DNS_STATIC_SERVERS")
      )
    ),
    " "
  )
  if Ops.greater_than(Builtins.size(resolvlist), 0)
    @nameservers = deep_copy(resolvlist)
  end

  @searchlist = Builtins.splitstring(
    Convert.to_string(
      SCR.Read(
        path(".sysconfig.network.config.NETCONFIG_DNS_STATIC_SEARCHLIST")
      )
    ),
    " "
  )

  # hostname and domain
  ReadHostname()
  @oldhostname = Hostname.MergeFQ(@hostname, @domain)

  Builtins.y2milestone("nameservers=%1", @nameservers)
  Builtins.y2milestone("searchlist=%1", @searchlist)
  Builtins.y2milestone("hostname=%1", @hostname)
  Builtins.y2milestone("domain=%1", @domain)

  @initialized = true
  true
end

- (Object) ReadHostDomain(hn, dn)

Use this host and domain name, if they are defined

Parameters:

  • hn (String)

    hostname

  • dn (String)

    domain name

Returns:

  • true if the hostname has been assigned



96
97
98
99
100
101
102
# File '../../src/modules/DNS.rb', line 96

def ReadHostDomain(hn, dn)
  return false if hn == "" || hn == nil || dn == nil
  @hostname = hn
  @domain = dn
  #modified = true;
  true
end

- (Object) ReadHostname



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
# File '../../src/modules/DNS.rb', line 224

def ReadHostname
  fqhostname = ""
  # In installation (standard, or AutoYaST one), prefer /etc/install.inf
  # (because HOSTNAME comes with netcfg.rpm already, #144687)
  if (Mode.installation || Mode.autoinst) &&
      Ops.greater_than(
        SCR.Read(path(".target.size"), "/etc/install.inf"),
        0
      )
    install_inf_hostname = Convert.to_string(
      SCR.Read(path(".etc.install_inf.Hostname"))
    )
    Builtins.y2milestone("Got %1 from install.inf", install_inf_hostname)

    if Ops.greater_than(Builtins.size(install_inf_hostname), 0)
      #if the name is actually IP, try to resolve it (bnc#556613, bnc#435649)
      if IP.Check(install_inf_hostname)
        fqhostname = ResolveIP(install_inf_hostname)
        Builtins.y2milestone(
          "Got %1 after resolving IP from install.inf",
          fqhostname
        )
      else
        fqhostname = install_inf_hostname
      end
    end
  end

  # We have non-empty hostname by now => we must set DNS modified flag
  # in order to get the setting actually written (bnc#588938)
  @modified = true if Ops.greater_than(Builtins.size(fqhostname), 0)


  # /etc/HOSTNAME
  # the usual location
  if fqhostname == ""
    if Ops.greater_than(SCR.Read(path(".target.size"), "/etc/HOSTNAME"), 0)
      fqhostname = Convert.to_string(
        SCR.Read(path(".target.string"), "/etc/HOSTNAME")
      )
      #avoid passing nil argument when we get non-\n-terminated string (#445531)
      fqhostname = String.FirstChunk(fqhostname, "\n")
      Builtins.y2milestone("Got %1 from /etc/HOSTNAME", fqhostname)
    end
  end

  split = Hostname.SplitFQ(fqhostname)
  @hostname = Ops.get(split, 0, "")
  @domain = Ops.get(split, 1, "")

  # last resort
  if @hostname == ""
    @hostname = "linux"
    @domain = "site"
  end

  nil
end

- (Object) ReadNameserver(ns)

Use the parameter, coming usually from install.inf, if it is defined. Used when there is nothing better.

Parameters:

  • ns (String)

    ip of the nameserver

Returns:

  • true if success



85
86
87
88
89
90
# File '../../src/modules/DNS.rb', line 85

def ReadNameserver(ns)
  return false if ns == "" || ns == nil
  @nameservers = [ns]
  #modified = true;
  true
end

- (Object) ResolveIP(ip)

Resolve IP to canonical hostname

Parameters:

  • ip (String)

    given IP address

Returns:

  • resolved canonical hostname (FQDN) for given IP or empty string in case of failure.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File '../../src/modules/DNS.rb', line 197

def ResolveIP(ip)
  command = "/usr/bin/getent hosts \"%1\""
  getent = Convert.to_map(
    SCR.Execute(path(".target.bash_output"), Builtins.sformat(command, ip))
  )
  exit_code = Ops.get_integer(getent, "exit", -1)

  if exit_code != 0
    Builtins.y2error("ResolveIP: getent call failed (%1)", exit_code)

    return ""
  end

  GetHostnameFromGetent(Ops.get_string(getent, "stdout", ""))
end

- (Object) Summary

Create DNS text summary

Returns:

  • summary text



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
# File '../../src/modules/DNS.rb', line 546

def Summary
  Yast.import "Summary"
  summary = ""

  has_dhcp = Ops.greater_than(
    Builtins.size(NetworkInterfaces.Locate("BOOTPROTO", "dhcp")),
    0
  )

  if has_dhcp && @dhcp_hostname
    # Summary text
    summary = Summary.AddListItem(summary, _("Hostname: Set by DHCP"))
  elsif Ops.greater_than(Builtins.size(@hostname), 0)
    # Summary text
    summary = Summary.AddListItem(
      summary,
      Builtins.sformat(
        _("Hostname: %1"),
        Hostname.MergeFQ(@hostname, @domain)
      )
    )
  end
  if !@write_hostname
    summary = Summary.AddListItem(
      summary,
      _("Hostname will not be written to /etc/hosts")
    )
  end


  #if (has_dhcp && NetworkConfig::DHCP["DHCLIENT_MODIFY_RESOLV_CONF"]:false) {
  # Summary text
  #summary = Summary::AddListItem(summary, _("Name Servers: Set by DHCP"));
  # Summary text
  #summary = Summary::AddListItem(summary, _("Search List: Set by DHCP"));
  #}
  #else {
  nslist = Builtins.maplist(@nameservers) do |ns|
    nss = NetHwDetection.ResolveIP(ns)
    nss == "" ? ns : Ops.add(Ops.add(Ops.add(ns, " ("), nss), ")")
  end

  if Ops.greater_than(Builtins.size(nslist), 0)
    # Summary text
    summary = Summary.AddListItem(
      summary,
      Builtins.sformat(
        _("Name Servers: %1"),
        Builtins.mergestring(nslist, ", ")
      )
    )
  end
  if Ops.greater_than(Builtins.size(@searchlist), 0)
    # Summary text
    summary = Summary.AddListItem(
      summary,
      Builtins.sformat(
        _("Search List: %1"),
        Builtins.mergestring(@searchlist, ", ")
      )
    )
  end
  #}

  return "" if Ops.less_than(Builtins.size(summary), 1)
  Ops.add(Ops.add("<ul>", summary), "</ul>")
end

- (Object) Write

Write new DNS and hostname settings Includes Host,NetworkConfig::Write

Returns:

  • true if success



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
# File '../../src/modules/DNS.rb', line 348

def Write
  # build FQ hostname
  fqhostname = Hostname.MergeFQ(@hostname, @domain)

  #We do not collect static IP addresses here, as hostnames
  #are defined for each static IP separately in address dialog
  #FaTE #2202

  @oldhostname = fqhostname # #49634

  # ensure that nothing is saved in case old values are the same, as it makes
  # rcnetwork reload restart all interfaces (even 'touch /etc/sysconfig/network/dhcp'
  # is sufficient)
  tmp = SCR.Read(path(".sysconfig.network.dhcp.DHCLIENT_SET_HOSTNAME"))
  old_dhcp_hostname = tmp == "yes"

  tmp = SCR.Read(path(".sysconfig.network.dhcp.WRITE_HOSTNAME_TO_HOSTS"))
  old_write_hostname = tmp == "yes"

  if old_dhcp_hostname != dhcp_hostname || old_write_hostname != write_hostname
    SCR.Write(
      path(".sysconfig.network.dhcp.DHCLIENT_SET_HOSTNAME"),
      @dhcp_hostname ? "yes" : "no"
    )
    SCR.Write(
      path(".sysconfig.network.dhcp.WRITE_HOSTNAME_TO_HOSTS"),
      @write_hostname ? "yes" : "no"
    )
    SCR.Write(path(".sysconfig.network.dhcp"), nil)
  end

  Builtins.y2milestone("Writing configuration")
  if !@modified
    Builtins.y2milestone("No changes to DNS -> nothing to write")
    return true
  end

  Builtins.y2milestone("nameservers=%1", @nameservers)
  Builtins.y2milestone("searchlist=%1", @searchlist)
  Builtins.y2milestone("hostname=%1", @hostname)
  Builtins.y2milestone("domain=%1", @domain)
  Builtins.y2milestone(
    "dhcp_hostname=%1, write_hostname=%2",
    @dhcp_hostname,
    @write_hostname
  )


  steps = [
    # Progress stage 1
    _("Write hostname"),
    # Progress stage 2
    _("Update configuration"),
    # Progress stage 3
    _("Update /etc/resolv.conf")
  ]

  # Write dialog caption
  caption = _("Saving Hostname and DNS Configuration")
  sl = 0 #100; for testing

  Progress.New(caption, " ", Builtins.size(steps), steps, [], "")

  # Allow to set hostname even if it's modified by DHCP (#13427)
  # if(NetworkConfig::DHCP["DHCLIENT_SET_HOSTNAME"]:false != true) {

  # Progress step 1/3
  ProgressNextStage(_("Writing hostname..."))

  # change the hostname
  SCR.Execute(path(".target.bash"), Ops.add("/bin/hostname ", @hostname))

  # write hostname
  SCR.Write(
    path(".target.string"),
    "/etc/HOSTNAME",
    Ops.add(fqhostname, "\n")
  )
  Builtins.sleep(sl)

  # Progress step 2/3
  ProgressNextStage(_("Updating configuration..."))

  # Finish him
  RunSuSEconfig()
  Builtins.sleep(sl)

  #     if(SCR::Read(.target.size, resolv_conf) < 0)
  # SCR::Write(.target.string, resolv_conf, "");


  # Progress step 3/3
  ProgressNextStage(_("Updating /etc/resolv.conf ..."))

  SCR.Write(
    path(".sysconfig.network.config.NETCONFIG_DNS_POLICY"),
    @resolv_conf_policy
  )
  SCR.Write(
    path(".sysconfig.network.config.NETCONFIG_DNS_STATIC_SEARCHLIST"),
    Builtins.mergestring(@searchlist, " ")
  )
  SCR.Write(
    path(".sysconfig.network.config.NETCONFIG_DNS_STATIC_SERVERS"),
    Builtins.mergestring(@nameservers, " ")
  )
  SCR.Write(path(".sysconfig.network.config"), nil)

  SCR.Execute(path(".target.bash"), "/sbin/netconfig update")

  Builtins.sleep(sl)

  Progress.NextStage
  @modified = false
  true
end