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_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.


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

def self.define_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

Instance Method Details

#default_dhcp_hostnameBoolean

Default value for #dhcp_hostname based on ProductFeatures and Arch


169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'src/modules/DNS.rb', line 169

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).


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

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


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

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

  if Ops.greater_than(
    Builtins.size(NetworkInterfaces.Locate("BOOTPROTO", "dhcp")),
    0
  ) || dhcp_hostname
    dhcp_data = dhcp_data()
    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
  elsif Builtins.tolower(check_host) ==
      Builtins.tolower(Ops.add(Ops.add(@hostname, "."), @domain)) ||
      Ops.get(dhcp_data, "hostname_fq", "") == check_host
    return true
  end
  false
end

#mainObject


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

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

  Yast.import "Arch"
  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.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


262
263
264
# File 'src/modules/DNS.rb', line 262

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


186
187
188
# File 'src/modules/DNS.rb', line 186

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

#ResolveIP(ip) ⇒ Object

Resolve IP to canonical hostname


152
153
154
155
156
157
158
159
160
161
162
163
# File 'src/modules/DNS.rb', line 152

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(_gui: true) ⇒ Object

TODO:

Update GUI

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


194
195
196
197
198
# File 'src/modules/DNS.rb', line 194

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