Class: Yast::DnsServerHelperFunctionsClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) HandleNsupdate(update_record, operation, current_zone)



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

def HandleNsupdate(update_record, operation, current_zone)
  update_record = deep_copy(update_record)
  if !Builtins.regexpmatch(operation, "^(add|delete)$")
    Builtins.y2error(
      "allowed operation is 'add' or 'delete', not '%1'",
      operation
    )
    return false
  end
  if Ops.get_string(update_record, "type", "") == "" ||
      Ops.get_string(update_record, "key", "") == ""
    Builtins.y2error("$[key:?] and $[type:?] is not defined")
    return false
  end

  __current_zone_ = Ops.add(
    Ops.add(".", Ops.get_string(current_zone.value, "zone", "")),
    Builtins.regexpmatch(
      Ops.get_string(current_zone.value, "zone", ""),
      "\\.$"
    ) ? "" : "."
  )

  # relative hostnames are converted to absolute
  if Builtins.regexpmatch(
      Builtins.tolower(Ops.get_string(update_record, "type", "")),
      "^(a|ns|mx|cname)$"
    )
    if Builtins.regexpmatch(
        Ops.get_string(update_record, "key", ""),
        ".*[^.]$"
      )
      Ops.set(
        update_record,
        "key",
        Ops.add(Ops.get_string(update_record, "key", ""), __current_zone_)
      )
    end
  end
  if Builtins.regexpmatch(
      Builtins.tolower(Ops.get_string(update_record, "type", "")),
      "^(ns|mx|cname)$"
    )
    if Builtins.regexpmatch(
        Ops.get_string(update_record, "value", ""),
        ".*[^.]$"
      )
      Ops.set(
        update_record,
        "value",
        Ops.add(Ops.get_string(update_record, "value", ""), __current_zone_)
      )
    end
  end

  if Builtins.tolower(Ops.get_string(update_record, "type", "")) == "txt"
    Ops.set(
      update_record,
      "value",
      Ops.add(
        Ops.add(
          "\"",
          Builtins.mergestring(
            Builtins.splitstring(
              Ops.get_string(update_record, "value", ""),
              "\""
            ),
            "\\\""
          )
        ),
        "\""
      )
    )
  end

  # If absolute name && doesn't match the current zone...
  # nsupdate would refuse such change
  if Builtins.regexpmatch(Ops.get_string(update_record, "key", ""), "\\.$") &&
      !Builtins.issubstring(
        Ops.get_string(update_record, "key", ""),
        __current_zone_
      ) &&
      Ops.get_string(
        # e.g., NS for zone
        update_record,
        "key",
        ""
      ) !=
        Ops.add(Ops.get_string(current_zone.value, "zone", ""), ".")
    Builtins.y2warning(
      "Wrong record '%1' for zone '%2'",
      Ops.get_string(update_record, "key", ""),
      __current_zone_
    )
    return false
  end

  Ops.set(
    current_zone.value,
    "update_actions",
    Builtins.add(
      Ops.get_list(current_zone.value, "update_actions", []),
      {
        "operation" => operation,
        "type"      => Ops.get_string(update_record, "type", ""),
        "key"       => Ops.get_string(update_record, "key", ""),
        "value"     => Ops.get_string(update_record, "value", "")
      }
    )
  )

  true
end

- (Object) IsInternalZone(zone_name)



46
47
48
49
50
51
52
53
54
# File '../../src/modules/DnsServerHelperFunctions.rb', line 46

def IsInternalZone(zone_name)
  if zone_name == "" || zone_name == nil
    Builtins.y2error("Uknown zone: %1", zone_name)
    return nil
  end

  zone_name == "." || zone_name == "0.0.127.in-addr.arpa" ||
    zone_name == "localhost"
end

- (Boolean) IsReverseZone(zone)

Returns whether zone is a reverse-zone type

Parameters:

  • zone (String)

Returns:

  • (Boolean)

    if zone is reverse type



32
33
34
35
36
37
38
39
40
41
42
43
44
# File '../../src/modules/DnsServerHelperFunctions.rb', line 32

def IsReverseZone(zone)
  if zone == nil || zone == ""
    Builtins.y2error("Wrong zone name: '%1'", zone)
    return nil
  end

  if Builtins.regexpmatch(zone, ".*\\.in-addr\\.arpa\\.?$") ||
      Builtins.regexpmatch(zone, ".*\\.ip6\\.arpa\\.?$")
    return true
  else
    return false
  end
end

- (Object) main



16
17
18
19
20
21
22
23
24
25
26
# File '../../src/modules/DnsServerHelperFunctions.rb', line 16

def main
  textdomain "dns-server"

  Yast.import "DnsServer"
  Yast.import "DnsServerAPI"

  @skip_converting_to_relative = {
    "key"   => [],
    "value" => ["TXT", "SPF", "SRV"]
  }
end

- (Object) MakeFQDNRecord(one_record, from_zone)



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File '../../src/modules/DnsServerHelperFunctions.rb', line 170

def MakeFQDNRecord(one_record, from_zone)
  one_record = deep_copy(one_record)
  # relative name to the zone name
  if !Builtins.regexpmatch(Ops.get_string(one_record, "key", ""), "\\.$")
    Ops.set(
      one_record,
      "key",
      Ops.add(
        Ops.add(Ops.get_string(one_record, "key", ""), "."),
        from_zone.value
      )
    )
  end

  deep_copy(one_record)
end

- (Object) RegenerateReverseZoneFrom(regenerate_zone, from_zone)



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
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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File '../../src/modules/DnsServerHelperFunctions.rb', line 187

def RegenerateReverseZoneFrom(regenerate_zone, from_zone)
  Builtins.y2milestone(
    "Regenerating zone %1 from zone %2",
    regenerate_zone,
    from_zone
  )

  current_zone = {}

  if !IsReverseZone(regenerate_zone)
    Builtins.y2error("Not a reverse zone: %1", regenerate_zone)
    return
  end

  if IsReverseZone(from_zone)
    Builtins.y2error("Not a forward zone: %1", from_zone)
    return
  end

  # Fetch the original zone records
  # to generate reverse records later
  index = DnsServer.FindZone(from_zone)
  if index == nil || Ops.less_than(index, 0)
    Builtins.y2error("Cannot find zone: %1", from_zone)
    return
  end

  DnsServer.SelectZone(index)
  current_zone = DnsServer.FetchCurrentZone
  original_zone_records = Ops.get_list(current_zone, "records", [])

  # Fetch the zone to edit
  index = DnsServer.FindZone(regenerate_zone)
  if index == nil || Ops.less_than(index, 0)
    Builtins.y2error("Cannot find zone: %1", regenerate_zone)
    return
  end

  DnsServer.SelectZone(index)
  current_zone = DnsServer.FetchCurrentZone

  # we leave these records untouched
  leave_these_records = ["SOA", "NS"]

  # Delete all current records from zone
  counter = -1
  zone_records = Ops.get_list(current_zone, "records", [])

  # run through copy of the list of records
  Builtins.foreach(zone_records) do |one_record|
    counter = Ops.add(counter, 1)
    if Builtins.contains(
        leave_these_records,
        Builtins.tostring(Ops.get_string(one_record, "type", ""))
      )
      next
    end
    # change it to nil and remove later
    Ops.set(current_zone, ["records", counter], nil)
    # ORIGIN is a special case, handled by bind itself if zone is dynamic
    if Builtins.tostring(Ops.get_string(one_record, "type", "")) != "ORIGIN"
      # dynamic zone
      current_zone_ref = arg_ref(current_zone)
      HandleNsupdate(one_record, "delete", current_zone_ref)
      current_zone = current_zone_ref.value
    end
  end

  # remove nil records
  Ops.set(
    current_zone,
    "records",
    Builtins.filter(Ops.get_list(current_zone, "records", [])) do |one_record|
      one_record != nil
    end
  )

  # Creating FQDN
  if !Builtins.regexpmatch(regenerate_zone, "\\.$")
    regenerate_zone = Ops.add(regenerate_zone, ".")
  end
  if !Builtins.regexpmatch(from_zone, "\\.$")
    from_zone = Ops.add(from_zone, ".")
  end

  new_record = {}

  # Go through all A, resp. AAAA, and add them in reverse order
  # IPv4 or IPv6?
  if Builtins.regexpmatch(regenerate_zone, "\\.ip6\\.arpa\\.?$")
    Builtins.foreach(original_zone_records) do |one_record|
      if Ops.get_string(one_record, "type", "") == "AAAA"
        # FIXME: make a PTR
        one_record = (
          from_zone_ref = arg_ref(from_zone);
          _MakeFQDNRecord_result = MakeFQDNRecord(one_record, from_zone_ref);
          from_zone = from_zone_ref.value;
          _MakeFQDNRecord_result
        )
        new_record = {
          "key"   => DnsServerAPI.GetReverseIPforIPv6(
            Ops.get_string(one_record, "value", "")
          ),
          "type"  => "PTR",
          "value" => Ops.get_string(one_record, "key", "")
        }
        # FIXME: something more offective?
        Ops.set(
          current_zone,
          "records",
          Builtins.add(
            Ops.get_list(current_zone, "records", []),
            new_record
          )
        )
        current_zone_ref = arg_ref(current_zone)
        HandleNsupdate(new_record, "add", current_zone_ref)
        current_zone = current_zone_ref.value
      end
    end
  else
    Builtins.foreach(original_zone_records) do |one_record|
      if Ops.get_string(one_record, "type", "") == "A"
        one_record = (
          from_zone_ref = arg_ref(from_zone);
          _MakeFQDNRecord_result = MakeFQDNRecord(one_record, from_zone_ref);
          from_zone = from_zone_ref.value;
          _MakeFQDNRecord_result
        )
        new_record = {
          "key"   => DnsServerAPI.GetReverseIPforIPv4(
            Ops.get_string(one_record, "value", "")
          ),
          "type"  => "PTR",
          "value" => Ops.get_string(one_record, "key", "")
        }
        # FIXME: something more offective?
        Ops.set(
          current_zone,
          "records",
          Builtins.add(
            Ops.get_list(current_zone, "records", []),
            new_record
          )
        )
        current_zone_ref = arg_ref(current_zone)
        HandleNsupdate(new_record, "add", current_zone_ref)
        current_zone = current_zone_ref.value
      end
    end
  end

  DnsServer.StoreCurrentZone(current_zone)
  DnsServer.StoreZone

  nil
end

- (Object) RRToRelativeName(absolute_name, zone_name, record_type, key_or_value)



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

def RRToRelativeName(absolute_name, zone_name, record_type, key_or_value)
  if absolute_name == nil || zone_name == nil
    Builtins.y2error(
      "Erroneous record: %1/%2/%3/%4",
      absolute_name,
      zone_name,
      record_type,
      key_or_value
    )
    return nil
  end

  if key_or_value != "key" && key_or_value != "value"
    Builtins.y2error("Unknown key/value %1", key_or_value)
    return absolute_name
  end

  # these records are not wanted to be converted to a relative name
  if Builtins.contains(
      Ops.get(@skip_converting_to_relative, key_or_value, []),
      Builtins.toupper(record_type)
    )
    Builtins.y2debug("Not converting %1/%2", key_or_value, record_type)
    return absolute_name
  end

  remove_this_to_be_relative = Ops.add(Ops.add("\\.", zone_name), "\\.$")
  relative_name = Builtins.regexpsub(
    absolute_name,
    Ops.add("([^ \t]*)", remove_this_to_be_relative),
    "\\1"
  )
  return relative_name if relative_name != nil

  absolute_name
end