Class: Yast::PortRangesClass

Inherits:
Module
  • Object
show all
Defined in:
library/network/src/modules/PortRanges.rb

Overview

Tools for ranges of network ports, as used by iptables for firewalling.

A Port Range is a string of two numbers separated with a colon: “3000:3010”. The range includes both ends. The numbers are nonnegative integers.

A Valid Port Range is an ascending pair of numbers between 1..65535.

Helpers collapse

Instance Method Summary collapse

Instance Method Details

#CreateNewPortRange(min_pr, max_pr) ⇒ String

Function creates a port range from min and max params. Max must be bigger than min. If something is wrong, it returns an empty string.

Examples:

CreateNewPortRange(10, 20) # => "10:20"
CreateNewPortRange(10, 10) # => "10"
CreateNewPortRange(0,  20) # => ""
CreateNewPortRange(20, 10) # => ""


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
# File 'library/network/src/modules/PortRanges.rb', line 277

def CreateNewPortRange(min_pr, max_pr)
  if min_pr.nil? || min_pr == 0
    Builtins.y2error(
      "Wrong definition of the starting port '%1', it must be between 1 and 65535",
      min_pr
    )
    return ""
  elsif max_pr.nil? || max_pr == 0 || Ops.greater_than(max_pr, 65_535)
    Builtins.y2error(
      "Wrong definition of the ending port '%1', it must be between 1 and 65535",
      max_pr
    )
    return ""
  end

  # max and min are the same, this is not a port range
  if min_pr == max_pr
    Builtins.tostring(min_pr)
  # right port range
  elsif Ops.less_than(min_pr, max_pr)
    Ops.add(
      Ops.add(Builtins.tostring(min_pr), ":"),
      Builtins.tostring(max_pr)
    )
  # min is bigger than max
  else
    Builtins.y2error(
      "Starting port '%1' cannot be bigger than ending port '%2'",
      min_pr,
      max_pr
    )
    ""
  end
end

#DividePortsAndPortRanges(unsorted_ports, with_aliases) ⇒ Hash{String => Array<String>}

Function divides list of ports to the map of ports and port ranges. If with_aliases is 'true' it also returns ports wit their port aliases. Port ranges are not affected with it.



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
# File 'library/network/src/modules/PortRanges.rb', line 229

def DividePortsAndPortRanges(unsorted_ports, with_aliases)
  unsorted_ports = deep_copy(unsorted_ports)
  ret = {}

  Builtins.foreach(unsorted_ports) do |port|
    # port range
    if IsPortRange(port)
      Ops.set(
        ret,
        "port_ranges",
        Builtins.add(Ops.get(ret, "port_ranges", []), port)
      )
    # is a normal port
    # find also aliases
    elsif with_aliases
      Ops.set(
        ret,
        "ports",
        Convert.convert(
          Builtins.union(
            Ops.get(ret, "ports", []),
            PortAliases.GetListOfServiceAliases(port)
          ),
          from: "list",
          to:   "list <string>"
        )
      )
      # only add the port itself
    else
      Ops.set(ret, "ports", Builtins.add(Ops.get(ret, "ports", []), port))
    end
  end

  deep_copy(ret)
end

#FlattenServices(old_list, protocol) ⇒ Array<String>

Function tries to flatten services into the minimal list. If ports are already mentioned inside port ranges, they are dropped.



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
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
500
501
502
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
529
530
531
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
598
599
600
601
602
603
604
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
# File 'library/network/src/modules/PortRanges.rb', line 396

def FlattenServices(old_list, protocol)
  old_list = deep_copy(old_list)
  if !Builtins.contains(["TCP", "UDP"], protocol)
    message = Builtins.sformat(
      "Protocol %1 doesn't support port ranges, skipping...",
      protocol
    )
    Builtins.y2milestone(message) if ReportOnlyOnce(message)
    return deep_copy(old_list)
  end

  new_list = []
  list_of_ranges = []
  # Using port number, we can remove ports mentioned in port ranges
  ports_to_port_numbers = {}
  # Using this we can remove ports mentioned more than once
  port_numbers_to_port_names = {}

  Builtins.foreach(old_list) do |one_item|
    # Port range
    if IsPortRange(one_item)
      list_of_ranges = Builtins.add(list_of_ranges, one_item)
    else
      port_number = PortAliases.GetPortNumber(one_item)
      # Cannot find port number for this port, it is en error of the configuration
      if port_number.nil?
        Builtins.y2warning(
          "Unknown port %1 but leaving it in the configuration.",
          one_item
        )
        new_list = Builtins.add(new_list, one_item)
        # skip the 'nil' port number
        next
      end
      Ops.set(ports_to_port_numbers, one_item, port_number)
      Ops.set(
        port_numbers_to_port_names,
        port_number,
        Builtins.add(
          Ops.get(port_numbers_to_port_names, port_number, []),
          one_item
        )
      )
    end
  end

  Builtins.foreach(port_numbers_to_port_names) do |port_number, _port_names|
    # Port is not in any defined port range
    if !PortIsInPortranges(Builtins.tostring(port_number), list_of_ranges)
      # Port - 1 IS in some port range
      if PortIsInPortranges(
        Builtins.tostring(Ops.subtract(port_number, 1)),
        list_of_ranges
      )
        # Creating fake port range, to be joined with another one
        list_of_ranges = Builtins.add(
          list_of_ranges,
          CreateNewPortRange(Ops.subtract(port_number, 1), port_number)
        )
        # Port + 1 IS in some port range
      elsif PortIsInPortranges(
        Builtins.tostring(Ops.add(port_number, 1)),
        list_of_ranges
      )
        # Creating fake port range, to be joined with another one
        list_of_ranges = Builtins.add(
          list_of_ranges,
          CreateNewPortRange(port_number, Ops.add(port_number, 1))
        )
        # Port is not in any port range and also it cannot be joined with any one
      else
        # Port names of this port
        used_port_names = Ops.get(
          port_numbers_to_port_names,
          port_number,
          []
        )
        if Ops.greater_than(Builtins.size(used_port_names), 0)
          new_list = Builtins.add(new_list, Ops.get(used_port_names, 0, ""))
        else
          Builtins.y2milestone(
            "No port name for port number %1. Adding %1...",
            port_number
          )
          # There are no port names (hmm?), adding port number
          new_list = Builtins.add(new_list, Builtins.tostring(port_number))
        end
      end
      # Port is in a port range
    else
      Builtins.y2milestone(
        "Removing port %1 mentioned in port ranges %2",
        port_number,
        list_of_ranges
      )
    end
  end

  list_of_ranges = Builtins.toset(list_of_ranges)
  # maximal count of steps
  max_loops = 5000

  # Joining port ranges together
  # this is a bit dangerous!
  Builtins.y2milestone("Joining list of ranges %1", list_of_ranges)
  while Ops.greater_than(max_loops, 0)
    # if something goes wrong
    max_loops = Ops.subtract(max_loops, 1)

    any_change_during_this_loop = false

    try_all_these_ranges = deep_copy(list_of_ranges)
    Builtins.foreach(try_all_these_ranges) do |port_range|
      if !IsValidPortRange(port_range)
        warning = Builtins.sformat(
          "Wrong port-range definition %1, cannot join",
          port_range
        )
        Builtins.y2warning(warning) if ReportOnlyOnce(warning)
        next
      end
      min_pr = Builtins.tointeger(
        Builtins.regexpsub(port_range, "^([0123456789]+):.*$", "\\1")
      )
      max_pr = Builtins.tointeger(
        Builtins.regexpsub(port_range, "^.*:([0123456789]+)$", "\\1")
      )
      if min_pr.nil? || max_pr.nil?
        Builtins.y2error("Not a port range %1", port_range)
        next
      end
      # try to join it with another port ranges
      # -->
      Builtins.foreach(try_all_these_ranges) do |try_this_pr|
        # Exact match means the same port range
        next if try_this_pr == port_range
        this_min = Builtins.regexpsub(
          try_this_pr,
          "^([0123456789]+):.*$",
          "\\1"
        )
        this_max = Builtins.regexpsub(
          try_this_pr,
          "^.*:([0123456789]+)$",
          "\\1"
        )
        if this_min.nil? || this_max.nil?
          Builtins.y2error(
            "Wrong port range %1, %2 > %3",
            port_range,
            this_min,
            this_max
          )
          # skip it
          next
        end
        this_min_pr = Builtins.tointeger(this_min)
        this_max_pr = Builtins.tointeger(this_max)
        # // wrong definition of the port range
        if Ops.less_than(this_min_pr, 1) ||
            Ops.greater_than(this_max_pr, @max_port_number)
          warning = Builtins.sformat(
            "Wrong port-range definition %1, cannot join",
            port_range
          )
          Builtins.y2warning(warning) if ReportOnlyOnce(warning)
          # skip it
          next
        end
        # If new port range should be created
        new_min = nil
        new_max = nil
        # the second one is inside the first one
        if Ops.less_or_equal(min_pr, this_min_pr) &&
            Ops.greater_or_equal(max_pr, this_max_pr)
          # take min_pr & max_pr
          any_change_during_this_loop = true
          new_min = min_pr
          new_max = max_pr
          # the fist one is inside the second one
        elsif Ops.greater_or_equal(min_pr, this_min_pr) &&
            Ops.less_or_equal(max_pr, this_max_pr)
          # take this_min_pr & this_max_pr
          any_change_during_this_loop = true
          new_min = this_min_pr
          new_max = this_max_pr
          # the fist one partly covers the second one (by its right side)
        elsif Ops.less_or_equal(min_pr, this_min_pr) &&
            Ops.greater_or_equal(max_pr, this_min_pr)
          # take min_pr & this_max_pr
          any_change_during_this_loop = true
          new_min = min_pr
          new_max = this_max_pr
          # the second one partly covers the first one (by its left side)
        elsif Ops.greater_or_equal(min_pr, this_min_pr) &&
            Ops.less_or_equal(max_pr, this_max_pr)
          # take this_min_pr & max_pr
          any_change_during_this_loop = true
          new_min = this_min_pr
          new_max = max_pr
          # the first one has the second one just next on the right
        elsif Ops.add(max_pr, 1) == this_min_pr
          # take min_pr & this_max_pr
          any_change_during_this_loop = true
          new_min = min_pr
          new_max = this_max_pr
          # the first one has the second one just next on the left side
        elsif Ops.subtract(min_pr, 1) == this_max_pr
          # take this_min_pr & max_pr
          any_change_during_this_loop = true
          new_min = this_min_pr
          new_max = max_pr
        end
        if any_change_during_this_loop && !new_min.nil? && !new_max.nil?
          new_port_range = CreateNewPortRange(new_min, new_max)
          Builtins.y2milestone(
            "Joining %1 and %2 into %3",
            port_range,
            try_this_pr,
            new_port_range
          )
          # Remove old port ranges
          list_of_ranges = Builtins.filter(list_of_ranges) do |filter_pr|
            filter_pr != port_range && filter_pr != try_this_pr
          end
          # Create a new one
          list_of_ranges = Builtins.add(list_of_ranges, new_port_range)
        end
      end
      # <--

      # renew list of current port ranges, they have changed
      raise Break if any_change_during_this_loop
    end

    break if !any_change_during_this_loop
  end
  Builtins.y2milestone("Result of joining: %1", list_of_ranges)

  new_list = Convert.convert(
    Builtins.union(new_list, list_of_ranges),
    from: "list",
    to:   "list <string>"
  )

  deep_copy(new_list)
end

#IsPortRange(check_this) ⇒ Boolean

Function returns where the string parameter is a port range. Port ranges are defined by the syntax “min_port_number:max_port_number”. Port range means that these maximum and minimum ports define the range of currency in Firewall. Ports defining the range are included in it. This function doesn't check whether the port range is valid or not.

Examples:

IsPortRange("34:38")      -> true
IsPortRange("0:38")       -> true
IsPortRange("port-range") -> false
IsPortRange("19-22")      -> false

See Also:

  • #IsValidPortRange()


95
96
97
98
99
100
# File 'library/network/src/modules/PortRanges.rb', line 95

def IsPortRange(check_this)
  if Builtins.regexpmatch(check_this, "^[0123456789]+:[0123456789]+$")
    return true
  end
  false
end

#IsValidPortRange(port_range) ⇒ Boolean

Checks whether the port range is valid.

Examples:

IsValidPortRange("54:135") -> true  // valid
IsValidPortRange("135:54") -> false // reverse order
IsValidPortRange("0:135")  -> false // cannot be from 0
IsValidPortRange("135")    -> false // cannot be one number
IsValidPortRange("54-135") -> false // wrong separator

See Also:

  • #IsPortRange()


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
169
170
# File 'library/network/src/modules/PortRanges.rb', line 115

def IsValidPortRange(port_range)
  # not a port range
  if !IsPortRange(port_range)
    warning = Builtins.sformat("Not a port-range %1", port_range)
    Builtins.y2milestone(warning) if ReportOnlyOnce(warning)

    return false
  end

  min_pr = Builtins.tointeger(
    Builtins.regexpsub(port_range, "^([0123456789]+):.*$", "\\1")
  )
  max_pr = Builtins.tointeger(
    Builtins.regexpsub(port_range, "^.*:([0123456789]+)$", "\\1")
  )

  # couldn't extract two integers
  if min_pr.nil? && max_pr.nil?
    warning = Builtins.sformat(
      "Wrong port-range: '%1':'%2'",
      min_pr,
      max_pr
    )
    Builtins.y2warning(warning) if ReportOnlyOnce(warning)

    return false
  end

  # Checking the minimal port number in the port-range
  # wrong range
  if Ops.less_than(min_pr, 1) || Ops.greater_than(min_pr, @max_port_number)
    warning = Builtins.sformat("Wrong port-range definition %1", port_range)
    Builtins.y2warning(warning) if ReportOnlyOnce(warning)

    return false
  end

  # Checking the maximal port number in the port-range
  # wrong range
  if Ops.less_than(max_pr, 1) || Ops.greater_than(max_pr, @max_port_number)
    warning = Builtins.sformat("Wrong port-range definition %1", port_range)
    Builtins.y2warning(warning) if ReportOnlyOnce(warning)

    return false
  end

  # wrong range
  if Ops.greater_or_equal(min_pr, max_pr)
    warning = Builtins.sformat("Wrong port-range definition %1", port_range)
    Builtins.y2warning(warning) if ReportOnlyOnce(warning)

    return false
  end

  true
end

#mainObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'library/network/src/modules/PortRanges.rb', line 43

def main
  textdomain "base"

  Yast.import "PortAliases"

  # Variable for ReportOnlyOnce() function
  @report_only_once = []

  # Maximal number of port number, they are in the interval 1-65535 included.
  # The very same value should appear in SuSEFirewall::max_port_number.
  @max_port_number = 65_535
end

#PortIsInPortranges(port, port_ranges) ⇒ Boolean

Function returns where the port name or port number is included in the list of port ranges. Port ranges must be defined as a string with format “min_port_number:max_port_number”.

Examples:

PortIsInPortranges ("130",  ["100:150","10:30"]) -> true
PortIsInPortranges ("30",   ["100:150","10:20"]) -> false
PortIsInPortranges ("pop3", ["100:150","10:30"]) -> true
PortIsInPortranges ("http", ["100:150","10:20"]) -> false


185
186
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
# File 'library/network/src/modules/PortRanges.rb', line 185

def PortIsInPortranges(port, port_ranges)
  port_ranges = deep_copy(port_ranges)
  return false if Builtins.size(port_ranges) == 0

  ret = false

  port_number = PortAliases.GetPortNumber(port)

  Builtins.foreach(port_ranges) do |port_range|
    # is portrange really a port range?
    if IsValidPortRange(port_range)
      min_pr = Builtins.tointeger(
        Builtins.regexpsub(port_range, "^([0123456789]+):.*$", "\\1")
      )
      max_pr = Builtins.tointeger(
        Builtins.regexpsub(port_range, "^.*:([0123456789]+)$", "\\1")
      )

      # is the port inside?
      if Ops.less_or_equal(min_pr, max_pr) &&
          Ops.less_or_equal(min_pr, port_number) &&
          Ops.less_or_equal(port_number, max_pr)
        ret = true

        raise Break # break the loop, match found
      end
    end
  end if !port_number.nil?

  ret
end

#RemovePortFromPortRanges(port_number, port_ranges) ⇒ Array<String>

Function removes port number from all port ranges. Port must be in its numeric form. A port range may be a single port, that's OK. Or a non-port, then it will be kept.

Examples:

RemovePortFromPortRanges(25, ["19:88", "152:160"]) -> ["19:24", "26:88", "152:160"]

See Also:

  • Yast::PortRangesClass#PortAliases#PortAliases::GetPortNumber()


324
325
326
327
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
# File 'library/network/src/modules/PortRanges.rb', line 324

def RemovePortFromPortRanges(port_number, port_ranges)
  port_ranges = deep_copy(port_ranges)
  # Checking necessarity of filtering and params
  return deep_copy(port_ranges) if port_ranges.nil? || port_ranges == []
  return deep_copy(port_ranges) if port_number.nil? || port_number == 0

  Builtins.y2milestone(
    "Removing port %1 from port ranges %2",
    port_number,
    port_ranges
  )

  ret = []
  # Checking every port range alone
  Builtins.foreach(port_ranges) do |port_range|
    # Port range might be now only "port"
    if !IsPortRange(port_range)
      # If the port doesn't match the ~port_range...
      if Builtins.tostring(port_number) != port_range
        ret = Builtins.add(ret, port_range)
      end
      # If matches, it isn't added (it is filtered)
      # Modify the port range when the port is included
    elsif PortIsInPortranges(Builtins.tostring(port_number), [port_range])
      min_pr = Builtins.tointeger(
        Builtins.regexpsub(port_range, "^([0123456789]+):.*$", "\\1")
      )
      max_pr = Builtins.tointeger(
        Builtins.regexpsub(port_range, "^.*:([0123456789]+)$", "\\1")
      )

      # Port matches the min. value of port range
      if port_number == min_pr
        ret = Builtins.add(
          ret,
          CreateNewPortRange(Ops.add(port_number, 1), max_pr)
        )
        # Port matches the max. value of port range
      elsif port_number == max_pr
        ret = Builtins.add(
          ret,
          CreateNewPortRange(min_pr, Ops.subtract(port_number, 1))
        )
        # Port is inside the port range, split it up
      else
        ret = Builtins.add(
          ret,
          CreateNewPortRange(Ops.add(port_number, 1), max_pr)
        )
        ret = Builtins.add(
          ret,
          CreateNewPortRange(min_pr, Ops.subtract(port_number, 1))
        )
      end
      # Port isn't in the port range, adding the current port range
    else
      ret = Builtins.add(ret, port_range)
    end
  end

  Builtins.y2milestone("Result: %1", ret)

  deep_copy(ret)
end

#ReportOnlyOnce(what_to_report) ⇒ Boolean

Report the error, warning, message only once. Stores the error, warning, message in memory. This is just a helper function that could avoid from filling y2log up with a lot of the very same messages - 'foreach()' is a very powerful builtin.

string error = sformat(“Port number %1 is invalid.”, port_nr); if (ReportOnlyOnce(error)) y2error(error);



69
70
71
72
73
74
# File 'library/network/src/modules/PortRanges.rb', line 69

def ReportOnlyOnce(what_to_report)
  return false if Builtins.contains(@report_only_once, what_to_report)

  @report_only_once = Builtins.add(@report_only_once, what_to_report)
  true
end