Class: Yast::DNSClass
- Inherits:
-
Module
- Object
- Module
- Yast::DNSClass
- Includes:
- Logger
- Defined in:
- ../../src/modules/DNS.rb
Constant Summary
- HOSTNAME_FILE =
"hostname"
- HOSTNAME_PATH =
"/etc/" + HOSTNAME_FILE
Instance Method Summary (collapse)
-
- (Object) create_hostname_link
Creates symlink /etc/HOSTNAME -> /etc/hostname to gurantee backward compatibility after changes in bnc#858908.
-
- (Boolean) default_dhcp_hostname
Default value for #dhcp_hostname based on ProductFeatures and Arch.
- - (Object) DefaultWriteHostname
-
- (Object) Export
Dump the DNS settings to a map, for autoinstallation use.
-
- (Object) GetDHCPHostnameIP
Get current hostname and IP Address if these are set by DHCP.
-
- (Object) GetHostnameFromGetent(line)
Handles input as one line of getent output.
-
- (Object) Import(settings)
Get all the DNS configuration from a map.
-
- (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.
- - (Object) main
- - (Object) ProposeHostname
-
- (Object) Read
Reads current DNS and hostname settings Includes Host,NetworkConfig::Read.
-
- (Object) ReadHostDomain(hn, dn)
Use this host and domain name, if they are defined.
- - (Object) ReadHostname
-
- (Object) ReadNameserver(ns)
Use the parameter, coming usually from install.inf, if it is defined.
-
- (Object) ResolveIP(ip)
Resolve IP to canonical hostname.
-
- (Object) Summary
Create DNS text summary.
-
- (Object) Write
Write new DNS and hostname settings Includes Host,NetworkConfig::Write.
Instance Method Details
- (Object) create_hostname_link
Creates symlink /etc/HOSTNAME -> /etc/hostname to gurantee backward compatibility after changes in bnc#858908
661 662 663 664 665 666 667 668 669 670 671 |
# File '../../src/modules/DNS.rb', line 661 def create_hostname_link link_name = "/etc/HOSTNAME" return if FileUtils.IsLink(link_name) log.info "Creating #{link_name} symlink" SCR.Execute(path(".target.bash"), "rm #{link_name}") if FileUtils.Exists(link_name) SCR.Execute(path(".target.bash"), "ln -s #{DNSClass::HOSTNAME_PATH} #{link_name}") nil end |
- (Boolean) default_dhcp_hostname
Default value for #dhcp_hostname based on ProductFeatures and Arch
234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File '../../src/modules/DNS.rb', line 234 def default_dhcp_hostname # ProductFeatures.GetBooleanFeature returns false either if the value is # false or if it's missing, so let's discard the later case calling # ProductFeatures.GetFeature first feature_index = ["globals", "dhclient_set_hostname"] feature = ProductFeatures.GetFeature(*feature_index) # No value for the feature if feature.nil? || (feature.respond_to?(:empty?) && feature.empty?) !Arch.is_laptop else ProductFeatures.GetBooleanFeature(*feature_index) end end |
- (Object) DefaultWriteHostname
220 221 222 223 224 225 226 227 228 |
# File '../../src/modules/DNS.rb', line 220 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.
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 |
# File '../../src/modules/DNS.rb', line 503 def Export expdns = {} # It should be case only for installer (1st stage). When resolver / hostname # was configured via linuxrc, yast needn't to be aware of it. bnc#957377 Read() if !@initialized 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
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 |
# File '../../src/modules/DNS.rb', line 114 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).
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 |
# File '../../src/modules/DNS.rb', line 152 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.
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 |
# File '../../src/modules/DNS.rb', line 451 def Import(settings) settings = deep_copy(settings) @dhcp_hostname = settings.fetch("dhcp_hostname") { default_dhcp_hostname } # 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
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 |
# File '../../src/modules/DNS.rb', line 605 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
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 |
# File '../../src/modules/DNS.rb', line 41 def main Yast.import "UI" textdomain "network" Yast.import "Arch" 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.import "FileUtils" 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
263 264 265 266 267 268 269 270 271 |
# File '../../src/modules/DNS.rb', line 263 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
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 |
# File '../../src/modules/DNS.rb', line 276 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
103 104 105 106 107 108 109 |
# File '../../src/modules/DNS.rb', line 103 def ReadHostDomain(hn, dn) return false if hn == "" || hn.nil? || dn.nil? @hostname = hn @domain = dn # modified = true; true end |
- (Object) ReadHostname
248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File '../../src/modules/DNS.rb', line 248 def ReadHostname # In installation (standard, or AutoYaST one), prefer /etc/install.inf # (because HOSTNAME comes with netcfg.rpm already, #144687) if (Mode.installation || Mode.autoinst) && FileUtils.Exists("/etc/install.inf") fqhostname = read_hostname_from_install_inf end # reads setup from /etc/HOSTNAME, returns a default if nothing found fqhostname = Hostname.CurrentFQ if fqhostname.nil? || fqhostname.empty? @hostname, @domain = *Hostname.SplitFQ(fqhostname) nil end |
- (Object) ReadNameserver(ns)
Use the parameter, coming usually from install.inf, if it is defined. Used when there is nothing better.
92 93 94 95 96 97 |
# File '../../src/modules/DNS.rb', line 92 def ReadNameserver(ns) return false if ns == "" || ns.nil? @nameservers = [ns] # modified = true; true end |
- (Object) ResolveIP(ip)
Resolve IP to canonical hostname
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File '../../src/modules/DNS.rb', line 204 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
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 |
# File '../../src/modules/DNS.rb', line 532 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
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 |
# File '../../src/modules/DNS.rb', line 328 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"), HOSTNAME_PATH, Ops.add(fqhostname, "\n") ) create_hostname_link Builtins.sleep(sl) # Progress step 2/3 ProgressNextStage(_("Updating configuration...")) # Finish him update_mta_config 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 |