Class: Yast::DNSClass

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

Constant Summary collapse

HOSTNAME_FILE =
"hostname".freeze
HOSTNAME_PATH =
"/etc/" + HOSTNAME_FILE

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_dns_config_method(name) ⇒ Object

Defines a proxy method to DNS configuration

The idea is to keep DNS.hostname, DNS.nameservers, etc. methods so they can still being used in the UI. This mechanism should be removed in the future, when the widgets are adapted to the new API.

Parameters:

  • name (Symbol)

    Public method's name



48
49
50
51
52
53
54
55
56
# File 'src/modules/DNS.rb', line 48

def self.define_dns_config_method(name)
  define_method(name) do
    yast_dns_config.public_send(name)
  end

  define_method("#{name}=") do |value|
    yast_dns_config.public_send("#{name}=", value)
  end
end

.define_hostname_config_method(name) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'src/modules/DNS.rb', line 58

def self.define_hostname_config_method(name)
  define_method(name) do
    yast_hostname_config.public_send(name)
  end

  define_method("#{name}=") do |value|
    yast_hostname_config.public_send("#{name}=", value)
  end
end

Instance Method Details

#default_dhcp_hostnameBoolean

Default value for #dhcp_hostname based on ProductFeatures and Arch

Returns:

  • (Boolean)

    value set in features or, if none is set, false just for laptops



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'src/modules/DNS.rb', line 181

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

#GetHostnameFromGetent(line) ⇒ Object

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



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

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

#IsHostLocal(check_host) ⇒ Boolean

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 NOTE: used in yast2-nis-server, yast2-samba-server, yast2-dhcp-server

Parameters:

  • check_host (String)

    string hostname or IP address to check

Returns:

  • (Boolean)

    true if hostname is local host



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

def IsHostLocal(check_host)
  Read()
  current_dhcp_data = {}
  connections = Yast::Lan.yast_config.connections

  if connections.any?(:dhcp?) || dhcp_hostname
    current_dhcp_data = dhcp_data
    log.info("Got DHCP-configured data: #{current_dhcp_data.inspect}")
  end

  # FIXME: May not work properly in following situations:
  #   - multiple addresses per interface
  #     - aliases in /etc/hosts
  #   - IPADDR=IP/24

  # loopback interface or localhost hostname
  return true if ["127.0.0.1", "::1", "localhost", "localhost.localdomain"].include?(check_host)

  ip_addresses = connections.each_with_object([]) do |conn, res|
    conn.all_ips.each_with_object(res) do |ip, ips|
      address = ip&.address&.address.to_s
      ips << address if !address.empty? && !ips.include?(address)
    end
  end

  # IPv4 address
  if IP.Check4(check_host)
    return true if ip_addresses.include?(check_host) || current_dhcp_data["ip"] == check_host
  # IPv6 address
  elsif IP.Check6(check_host)
    log.debug("TODO make it similar to IPv4 after other code adapted to IPv6")
  # short hostname or FQDN
  elsif check_host.downcase == hostname.to_s.downcase ||
      current_dhcp_data["hostname_short"] == check_host ||
      current_dhcp_data["hostname_fq"] == check_host

    return true
  end
  false
end

#mainObject



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

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

  Yast.import "Lan"
  Yast.import "Arch"
  Yast.import "Hostname"
  Yast.import "IP"
  Yast.import "ProductFeatures"
  Yast.import "Progress"
  Yast.import "Service"
  Yast.import "String"
  Yast.import "FileUtils"
  Yast.import "Stage"
  Yast.import "Report"

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

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

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

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

#modifiedBoolean

Determines whether the DNS configuration has been modified

Returns:

  • (Boolean)


267
268
269
# File 'src/modules/DNS.rb', line 267

def modified
  system_dns_config.nil? || yast_dns_config != system_dns_config
end

#ReadObject

Note:

It reads all network settings, including DNS ones.

Reads DNS settings



198
199
200
# File 'src/modules/DNS.rb', line 198

def Read
  Yast::Lan.Read(:cache)
end

#ResolveIP(ip) ⇒ Object

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.



164
165
166
167
168
169
170
171
172
173
174
175
# File 'src/modules/DNS.rb', line 164

def ResolveIP(ip)
  getent = SCR.Execute(path(".target.bash_output"), "/usr/bin/getent hosts #{ip.shellescape}")
  exit_code = Ops.get_integer(getent, "exit", -1)

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

    return ""
  end

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

#Write(netconfig_update: true) ⇒ Object

TODO:

Update GUI

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

Parameters:

  • netconfig_update (Boolean) (defaults to: true)

    Whether 'netconfig update' should be called after writing the DNS configuration or not

Returns:

  • true if success



208
209
210
211
212
213
214
# File 'src/modules/DNS.rb', line 208

def Write(netconfig_update: true)
  writer = Y2Network::ConfigWriters::DNSWriter.new
  writer.write(Yast::Lan.yast_config.dns,
    Yast::Lan.system_config.dns,
    netconfig_update: netconfig_update)
  true
end