Class: Yast::CWMFirewallInterfacesClass

Inherits:
Module
  • Object
show all
Includes:
Y2Firewall::Helpers::Interfaces, Logger
Defined in:
library/network/src/modules/CWMFirewallInterfaces.rb

Overview

This class provide a set of methods to define a widget for handling with firewall interfaces configuration.

Examples:

Open a service in the firewall


# Obtain the firewalld widget description (a widget with the firewalld
# status, a checkbox for opening the service in all the interfaces and
# a details button to decide per interface where should be opened.

CWMFireallInterfaces.CreateOpenFirewallWidget(["smtp"])

# Require the new firewalld library
require 'y2firewall/firewalld'

# In your Read method call firewalld.read to initializate the firewalld
# object and all of its zones.

Y2Firewall::Firewalld.instance.read

# And finally call firewalld.write to apply the changes done through the
# widget.

Y2Firewall::Firewalld.instance.write

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Y2Firewall::Helpers::Interfaces

#default_interfaces, #default_zone, #interface_zone, #known_interfaces, #unknown_interfaces

Instance Attribute Details

#all_interfacesObject (readonly)

[Array] List of all the system network interfaces


75
76
77
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 75

def all_interfaces
  @all_interfaces
end

#allowed_interfacesObject (readonly)

[Array] List of all interfaces relevant for firewall settings


73
74
75
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 73

def allowed_interfaces
  @allowed_interfaces
end

#configuration_changedObject (readonly)

[Boolean] Information if configuration was changed by user


78
79
80
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 78

def configuration_changed
  @configuration_changed
end

Instance Method Details

#CreateInterfacesWidget(settings) ⇒ Hash

Get the widget description map



Behavior manipulating functions (mandatory)
- "get_allowed_interfaces" : list<string>() -- function that returns
         the list of allowed network interfaces
- "set_allowed_interfaces" : void (list<string>) -- function that sets
         the list of allowed interfaces

Additional settings:
- "help" : string -- help to the whole widget. If not specified, generic help
         is used (button labels are patched correctly)

Parameters:

  • settings (Hash{String => Object})

    a map of all parameters needed to create the widget properly

Returns:

  • (Hash)

    the widget description map


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

def CreateInterfacesWidget(settings)
  settings = deep_copy(settings)
  widget = HBox(
    HSpacing(1),
    VBox(
      HSpacing(48),
      VSpacing(1),
      ReplacePoint(
        Id("_cwm_interface_list_rp"),
        MultiSelectionBox(
          Id("_cwm_interface_list"),
          # translators: selection box title
          _("Network &Interfaces with Open Port in Firewall"),
          []
        )
      ),
      VSpacing(1),
      HBox(
        HStretch(),
        HWeight(
          1,
          PushButton(
            Id("_cwm_interface_select_all"),
            # push button to select all network intefaces for firewall
            _("Select &All")
          )
        ),
        HWeight(
          1,
          PushButton(
            Id("_cwm_interface_select_none"),
            # push button to deselect all network intefaces for firewall
            _("Select &None")
          )
        ),
        HStretch()
      ),
      VSpacing(1)
    ),
    HSpacing(1)
  )

  help = "" # TODO

  help = Ops.get_string(settings, "help", "") if Builtins.haskey(settings, "help")

  ret = Convert.convert(
    Builtins.union(
      settings,
      "widget"            => :custom,
      "custom_widget"     => widget,
      "help"              => help,
      "init"              => fun_ref(
        method(:InterfacesInitWrapper),
        "void (string)"
      ),
      "store"             => fun_ref(
        method(:InterfacesStoreWrapper),
        "void (string, map)"
      ),
      "handle"            => fun_ref(
        method(:InterfacesHandleWrapper),
        "symbol (string, map)"
      ),
      "validate_type"     => :function,
      "validate_function" => fun_ref(
        method(:InterfacesValidateWrapper),
        "boolean (string, map)"
      )
    ),
    from: "map",
    to:   "map <string, any>"
  )

  deep_copy(ret)
end

#CreateOpenFirewallWidget(settings) ⇒ Hash

Get the widget description map of the firewall enablement widget



- "services" : list<string> -- services identifications for the Firewall.ycp
         module
- "display_details" : boolean -- true if the details button should be
         displayed
- "firewall_details_handler" : symbol () -- function to handle the firewall
         details button. If returns something else than nil, dialog is
         exited with the returned symbol as value for wizard sequencer.
         If not specified, but "display_details" is true, common popup
         is used.
- "open_firewall_checkbox" : string -- label of the check box
- "firewall_details_button" : string -- label of the push button for
         changing firewall details
- "help" : string -- help to the widget. If not specified, generic help
         is used

Parameters:

  • settings (Hash{String => Object})

    a map of all parameters needed to create the widget properly

Returns:

  • (Hash)

    the widget description map


818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 818

def CreateOpenFirewallWidget(settings)
  settings = deep_copy(settings)

  services = adapt_settings_services!(settings)

  if services.empty?
    log.error("Firewall services not specified") if services.empty?
    return { "widget" => :custom, "custom_widget" => VBox(), "help" => "" }
  end

  if !firewalld.installed?
    log.error("Firewall is not installed") if !firewalld.installed?
    return { "widget" => :custom, "custom_widget" => not_installed_widget, "help" => "" }
  end

  if services.any? { |s| !firewalld.current_service_names.include?(s) }
    return {
      "widget"        => :custom,
      "custom_widget" => services_not_defined_widget(services),
      "help"          => ""
    }
  end

  open_firewall_checkbox =
    settings.fetch("open_firewall_checkbox", _("Open Port in &Firewall"))
  firewall_details_button =
    settings.fetch("firewall_details_button", _("Firewall &Details..."))
  display_firewall_details =
    settings.fetch("firewall_details_handler", settings.fetch("display_details", false))

  help = settings.fetch("help", OpenFirewallHelp(display_firewall_details))

  firewall_settings = CheckBox(
    Id("_cwm_open_firewall"),
    Opt(:notify),
    open_firewall_checkbox
  )

  if display_firewall_details
    firewall_settings = HBox(
      firewall_settings,
      HSpacing(2),
      PushButton(Id("_cwm_firewall_details"), firewall_details_button)
    )
  end

  firewall_settings = VBox(
    Frame(
      format(_("Firewall Settings for %{firewall}"), firewall: Y2Firewall::Firewalld::SERVICE),
      VBox(
        Left(firewall_settings),
        Left(
          ReplacePoint(
            Id(:_cwm_firewall_status_rp),
            # label text
            Label(_("Firewall is open"))
          )
        )
      )
    )
  )

  {
    "widget"        => :custom,
    "custom_widget" => firewall_settings,
    "help"          => help,
    "init"          => fun_ref(
      method(:OpenFirewallInitWrapper),
      "void (string)"
    ),
    "store"         => fun_ref(
      method(:OpenFirewallStoreWrapper),
      "void (string, map)"
    ),
    "handle"        => fun_ref(
      method(:OpenFirewallHandleWrapper),
      "symbol (string, map)"
    ),
    "handle_events" => ["_cwm_firewall_details", "_cwm_open_firewall"]
  }.merge(settings)
end

#current_firewall_statusSymbol

Return the current status of the firewall related to the interfaces opened or available

Returns:

  • (Symbol)

    current firewall status


910
911
912
913
914
915
916
917
918
919
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 910

def current_firewall_status
  # bnc #429861
  return :not_installed if Stage.initial || !firewalld.installed?
  return :off unless firewalld.enabled?
  return :no_ifaces if all_interfaces.empty?
  return :open_all if all_interfaces.size == allowed_interfaces.size
  return :closed if allowed_interfaces.empty?

  :custom
end

#DisableOpenFirewallWidgetObject

Disable the whole firewal widget


737
738
739
740
741
742
743
744
745
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 737

def DisableOpenFirewallWidget
  return if !UI.WidgetExists(Id("_cwm_open_firewall"))
  return if !UI.WidgetExists(Id("_cwm_firewall_details"))

  UI.ChangeWidget(Id("_cwm_open_firewall"), :Enabled, false)
  UI.ChangeWidget(Id("_cwm_firewall_details"), :Enabled, false)

  nil
end

#DisplayDetailsPopup(settings) ⇒ Symbol

Display the firewall interfaces selection as a popup

Returns:

  • (Symbol)

    return value of the dialog


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

def DisplayDetailsPopup(settings)
  settings = deep_copy(settings)
  # FIXME: breaks help if run in dialog with Tab!!!!!!
  # settings stack must be created in CWM::Run
  w = CWM.CreateWidgets(
    ["firewall_ifaces"],
    "firewall_ifaces" => CreateInterfacesWidget(settings)
  )
  contents = VBox(
    "firewall_ifaces",
    ButtonBox(
      PushButton(Id(:ok), Opt(:okButton, :key_F10), Label.OKButton),
      PushButton(
        Id(:cancel),
        Opt(:cancelButton, :key_F9),
        Label.CancelButton
      )
    )
  )
  contents = CWM.PrepareDialog(contents, w)
  UI.OpenDialog(contents)
  ret = CWM.Run(w, {})
  UI.CloseDialog
  ret
end

#DisplayFirewallDetailsPopupHandler(widget) ⇒ Object

Display popup with firewall settings details


194
195
196
197
198
199
200
201
202
203
204
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 194

def DisplayFirewallDetailsPopupHandler(widget)
  widget = deep_copy(widget)
  common_details_handler = Convert.convert(
    Ops.get(widget, "common_details_handler"),
    from: "any",
    to:   "void (map <string, any>)"
  )
  common_details_handler&.call(widget)

  nil
end

#EnableOpenFirewallWidgetObject

Enable the whole firewal widget


726
727
728
729
730
731
732
733
734
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 726

def EnableOpenFirewallWidget
  return if !UI.WidgetExists(Id("_cwm_open_firewall"))
  return if !UI.WidgetExists(Id("_cwm_firewall_details"))

  UI.ChangeWidget(Id("_cwm_open_firewall"), :Enabled, true)
  EnableOrDisableFirewallDetails()

  nil
end

#EnableOrDisableFirewallDetailsObject

Enable or disable the firewall details widget according to the status of "open firewall" checkbox


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 107

def EnableOrDisableFirewallDetails
  return if !UI.WidgetExists(Id("_cwm_open_firewall"))
  return if !UI.WidgetExists(Id("_cwm_firewall_details"))

  enabled = Convert.to_boolean(
    UI.QueryWidget(Id("_cwm_open_firewall"), :Value)
  )
  enabled = false if enabled.nil? || all_interfaces.empty?

  UI.ChangeWidget(Id("_cwm_firewall_details"), :Enabled, enabled)

  nil
end

#firewall_status_label(status) ⇒ String

Return the firewall status label for the given status

Parameters:

  • status (Symbol)

    current status

Returns:

  • (String)

    current firewall status label


925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 925

def firewall_status_label(status)
  case status
  when :not_installed
    # bnc #429861
    if Stage.initial
      # label
      _(
        "Firewall cannot be adjusted during first stage installation."
      )
    else
      # label
      _("Firewall package is not installed.")
    end
  when :off
    # label
    _("Firewall is disabled")
  when :closed
    # label
    _("Firewall port is closed")
  when :open_all
    # label
    _("Firewall port is open on all interfaces")
  when :custom
    # label
    _("Firewall port is open on selected interfaces")
  when :no_ifaces
    # label
    _("No network interfaces are configured")
  end
end

#InitAllInterfacesListObject

Initialize the list of all known interfaces


130
131
132
133
134
135
136
137
138
139
140
141
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 130

def InitAllInterfacesList
  # Do not read NetworkInterfaces when they are already read
  if !Mode.config && !Mode.installation && !Mode.update
    log.info("Reading NetworkInterfaces...")
    NetworkInterfaces.Read
  end

  @all_interfaces = NetworkInterfaces.List("").reject { |i| i == "lo" }
  @interface_items = all_interfaces.map { |i| Item(Id(i), interface_label(i)) }

  nil
end

#InitAllowedInterfaces(services) ⇒ Object

Initialize the list of allowed interfaces Changes the internal variables

Parameters:

  • services (Array<String>)

    a list of services


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

def InitAllowedInterfaces(services)
  service_status = {}

  zone_services(services).each do |_s, status|
    status.each do |iface, en|
      service_status[iface] = service_status.fetch(iface, true) && en
    end
  end

  service_status.select! { |_iface, en| en == true }
  log.info("Status: #{service_status}")
  @allowed_interfaces = service_status.keys

  log.info "Default zone name: #{default_zone.name}"
  log.info "Default interfaces: #{default_interfaces}"
  log.info "Default_zone services: #{default_zone.services}"

  if !default_interfaces.empty?
    services.each do |service|
      service_status[firewalld.default_zone] =
        default_zone&.services&.include?(service)
    end
    @allowed_interfaces = (allowed_interfaces + default_interfaces).uniq if service_status[firewalld.default_zone]
  end

  log.info "Allowed interfaces: #{allowed_interfaces}"

  @configuration_changed = false

  nil
end

#InterfacesHandle(_widget, _key, event) ⇒ Symbol

Handle function of the widget

Parameters:

  • _widget (Hash<String, Object>)

    a widget description map

  • _key (String)

    strnig the widget key

  • event (Hash)

    map event to be handled

Returns:

  • (Symbol)

    for wizard sequencer or nil


306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 306

def InterfacesHandle(_widget, _key, event)
  event_id = Ops.get(event, "ID")
  if event_id == "_cwm_interface_select_all"
    UI.ChangeWidget(
      Id("_cwm_interface_list"),
      :SelectedItems,
      all_interfaces
    )
    return nil
  end
  if event_id == "_cwm_interface_select_none"
    UI.ChangeWidget(Id("_cwm_interface_list"), :SelectedItems, [])
    return nil
  end
  nil
end

#InterfacesHandleWrapper(key, event) ⇒ Symbol

Handle function of the widget

Parameters:

  • key (String)

    strnig the widget key

  • event (Hash)

    map event to be handled

Returns:

  • (Symbol)

    for wizard sequencer or nil


459
460
461
462
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 459

def InterfacesHandleWrapper(key, event)
  event = deep_copy(event)
  InterfacesHandle(CWM.GetProcessedWidget, key, event)
end

#InterfacesInit(_widget, _key) ⇒ Object

Init function of the widget

Parameters:

  • _widget (Hash<String, Object>)

    a widget description map

  • _key (String)

    strnig the widget key


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 279

def InterfacesInit(_widget, _key)
  # set the list of ifaces
  InitAllInterfacesList() if all_interfaces.nil?
  UI.ReplaceWidget(
    Id("_cwm_interface_list_rp"),
    MultiSelectionBox(
      Id("_cwm_interface_list"),
      # transaltors: selection box title
      _("&Network Interfaces with Open Port in Firewall"),
      @interface_items
    )
  )
  # mark open ifaces as open
  UI.ChangeWidget(
    Id("_cwm_interface_list"),
    :SelectedItems,
    allowed_interfaces
  )

  nil
end

#InterfacesInitWrapper(key) ⇒ Object

Init function of the widget

Parameters:

  • key (String)

    strnig the widget key


449
450
451
452
453
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 449

def InterfacesInitWrapper(key)
  InterfacesInit(CWM.GetProcessedWidget, key)

  nil
end

#InterfacesStore(_widget, _key, _event) ⇒ Object

Store function of the widget

Parameters:

  • _widget (Hash<String, Object>)

    a widget description map

  • _key (String)

    strnig the widget key

  • _event (Hash)

    map that caused widget data storing


327
328
329
330
331
332
333
334
335
336
337
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 327

def InterfacesStore(_widget, _key, _event)
  allowed_interfaces = Convert.convert(
    UI.QueryWidget(Id("_cwm_interface_list"), :SelectedItems),
    from: "any",
    to:   "list <string>"
  )
  @allowed_interfaces = Selected2Opened(allowed_interfaces, false)
  @configuration_changed = true

  nil
end

#InterfacesStoreWrapper(key, event) ⇒ Object

Store function of the widget

Parameters:

  • key (String)

    strnig the widget key

  • event (Hash)

    map that caused widget data storing


467
468
469
470
471
472
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 467

def InterfacesStoreWrapper(key, event)
  event = deep_copy(event)
  InterfacesStore(CWM.GetProcessedWidget, key, event)

  nil
end

#InterfacesValidate(_widget, _key, _event) ⇒ Object

Validate function of the widget

Parameters:

  • _widget (Hash<String, Object>)

    a widget description map

  • _key (String)

    strnig the widget key

  • _event (Hash)

    map event that caused the validation

Returns:

  • true if validation succeeded, false otherwise


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

def InterfacesValidate(_widget, _key, _event)
  trusted_zone = firewalld.find_zone("trusted")

  ifaces = Convert.convert(
    UI.QueryWidget(Id("_cwm_interface_list"), :SelectedItems),
    from: "any",
    to:   "list <string>"
  ) || []

  log.info("Selected ifaces: #{ifaces}")

  trusted_interfaces = trusted_zone ? trusted_zone.interfaces : []

  if !trusted_interfaces.empty?
    int_not_selected = []
    trusted_interfaces.each do |interface|
      int_not_selected << interface unless ifaces.include?(interface)
    end

    if !int_not_selected.empty?
      log.warn("Unprotected internal interfaces not selected: #{int_not_selected}")

      Report.Message(
        Builtins.sformat(
          _(
            "These network interfaces assigned to internal network cannot be deselected:\n%1\n"
          ),
          int_not_selected.join("\n")
        )
      )

      ifaces = Convert.convert(
        Builtins.union(ifaces, int_not_selected),
        from: "list",
        to:   "list <string>"
      )
      log.info("Selected interfaces: #{ifaces}")
      UI.ChangeWidget(Id("_cwm_interface_list"), :SelectedItems, ifaces)
      return false
    end
  end

  if ifaces.empty?
    # question popup
    if !Popup.YesNo(
      _(
        "No interface is selected. Service will not\n" \
          "be available for other computers.\n" \
          "\n" \
          "Continue?"
      )
    )
      return false
    end
  end

  firewall_ifaces = Selected2Opened(ifaces, false)
  log.info("firewall_ifaces: #{firewall_ifaces}")
  added_ifaces = firewall_ifaces.reject { |i| ifaces.include?(i) }
  log.info("added_ifaces: #{added_ifaces}")
  removed_ifaces = ifaces.reject { |i| firewall_ifaces.include?(i) }
  log.info("removed_ifaces: #{removed_ifaces}")

  # to hide that special string
  if !added_ifaces.empty?
    if !Popup.YesNo(
      Builtins.sformat(
        # yes-no popup
        _(
          "Because of firewalld settings, the port\n" \
            "on the following interfaces will additionally be open:\n" \
            "%1\n" \
            "\n" \
            "Continue?"
        ),
        added_ifaces.join("\n")
      )
    )
      return false
    end
  end

  # to hide that special string
  if !removed_ifaces.empty?
    if !Popup.YesNo(
      Builtins.sformat(
        # yes-no popup
        _(
          "Because of firewalld settings, the port\n" \
            "on the following interfaces cannot be opened:\n" \
            "%1\n" \
            "\n" \
            "Continue?"
        ),
        removed_ifaces.join("\n")
      )
    )
      return false
    end
  end
  true
end

#InterfacesValidateWrapper(key, event) ⇒ Object

Validate function of the widget

Parameters:

  • key (String)

    strnig the widget key

  • event (Hash)

    map event that caused the validation

Returns:

  • true if validation succeeded, false otherwise


478
479
480
481
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 478

def InterfacesValidateWrapper(key, event)
  event = deep_copy(event)
  InterfacesValidate(CWM.GetProcessedWidget, key, event)
end

#mainObject


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 80

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

  Yast.import "CWM"
  Yast.import "Label"
  Yast.import "Mode"
  Yast.import "NetworkInterfaces"
  Yast.import "Popup"
  Yast.import "Report"
  Yast.import "Stage"
  Yast.import "String"

  # private variables

  # List of all items of interfaces to the selection box
  @interface_items = nil

  @configuration_changed = false

  @buggy_ifaces = []
end

#ModifiedBoolean

Check if settings were modified by the user

Returns:

  • (Boolean)

    true if settings were modified


902
903
904
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 902

def Modified
  firewalld.modified?
end

#OpenFirewallHandle(widget, _key, event) ⇒ Object

Handle the immediate start and stop of the service

Parameters:

  • widget (Hash<String, Object>)

    a map describing the widget

  • _key (String)

    strnig the widget key

  • event (Hash<String, Object>)

    event to handle

Returns:

  • always nil


645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 645

def OpenFirewallHandle(widget, _key, event)
  widget = deep_copy(widget)
  event = deep_copy(event)
  event_id = Ops.get(event, "ID")
  if event_id == "_cwm_firewall_details"
    handle_firewall_details = Convert.convert(
      Ops.get(widget, "firewall_details_handler"),
      from: "any",
      to:   "symbol ()"
    )
    Builtins.y2milestone("FD: %1", handle_firewall_details)
    ret = nil
    Builtins.y2milestone("RT: %1", ret)
    if !handle_firewall_details.nil?
      ret = handle_firewall_details.call
    else
      w = Builtins.filter(widget) { |k, _v| "services" == k }
      DisplayDetailsPopup(w)
    end
    UpdateFirewallStatus()
    EnableOrDisableFirewallDetails()
    return ret
  end
  if event_id == "_cwm_open_firewall"
    value = Convert.to_boolean(
      UI.QueryWidget(Id("_cwm_open_firewall"), :Value)
    )
    Builtins.y2milestone("OF: %1", value)
    @allowed_interfaces = value ? deep_copy(all_interfaces) : []

    @buggy_ifaces = []

    # Filtering out buggy ifaces
    Builtins.foreach(@buggy_ifaces) do |one_iface|
      @allowed_interfaces = Builtins.filter(allowed_interfaces) do |one_allowed|
        one_allowed != one_iface
      end
    end

    UpdateFirewallStatus()
    EnableOrDisableFirewallDetails()
    @configuration_changed = true
  end
  nil
end

#OpenFirewallHandleWrapper(key, event) ⇒ Object

Handle the immediate start and stop of the service

Parameters:

  • key (String)

    strnig the widget key

  • event (Hash<String, Object>)

    to handle

Returns:

  • always nil


713
714
715
716
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 713

def OpenFirewallHandleWrapper(key, event)
  event = deep_copy(event)
  OpenFirewallHandle(CWM.GetProcessedWidget, key, event)
end

#OpenFirewallHelp(restart_displayed) ⇒ String

Get the help text to the firewall opening widget

Parameters:

  • restart_displayed (Boolean)

    shold be true if "Save and restart" is displayed

Returns:


788
789
790
791
792
793
794
795
796
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 788

def OpenFirewallHelp(restart_displayed)
  Builtins.sformat(
    OpenFirewallHelpTemplate(restart_displayed),
    # part of help text - check box label, NO SHORTCUT!!!
    _("Open Port in Firewall"),
    # part of help text - push button label, NO SHORTCUT!!!
    _("Firewall Details")
  )
end

#OpenFirewallHelpTemplate(restart_displayed) ⇒ String

Get the template for the help text to the firewall opening widget

Parameters:

  • restart_displayed (Boolean)

    shold be true if "Save and restart" is displayed

Returns:

  • (String)

    help text template with %1 and %2 placeholders


758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 758

def OpenFirewallHelpTemplate(restart_displayed)
  # help text for firewall settings widget 1/3,
  # %1 is check box label, eg. "Open Port in Firewall" (without quotes)
  help = _(
    "<p><b><big>Firewall Settings</big></b><br>\n" \
      "To open the firewall to allow access to the service from remote computers,\n" \
      "set <b>%1</b>.<br>"
  )
  if restart_displayed
    # help text for firewall port openning widget 2/3, optional
    # %1 is push button label, eg. "Firewall &Details" (without quotes)
    # note: %2 is correct, do not replace with %1!!!
    help = Ops.add(
      help,
      _(
        "To select interfaces on which to open the port,\nclick <b>%2</b>.<br>"
      )
    )
  end
  # help text for firewall settings widget 3/3,
  help = Ops.add(
    help,
    _("This option is available only if the firewall\nis enabled.</p>")
  )
  help
end

#OpenFirewallInit(widget, _key) ⇒ Object

Initialize the open firewall widget

Parameters:

  • widget (Hash{String => Object})

    a map describing the whole widget


607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 607

def OpenFirewallInit(widget, _key)
  return unless open_firewall_widget?

  services = widget.fetch("services", [])

  InitAllInterfacesList()
  InitAllowedInterfaces(services)

  open_firewall = !allowed_interfaces.empty?
  firewall_enabled = firewalld.enabled? && !all_interfaces.empty?
  if !firewall_enabled
    open_firewall = false
    UI.ChangeWidget(Id("_cwm_open_firewall"), :Enabled, false)
  end
  UI.ChangeWidget(Id("_cwm_open_firewall"), :Value, open_firewall)
  UpdateFirewallStatus()
  EnableOrDisableFirewallDetails()

  nil
end

#OpenFirewallInitWrapper(key) ⇒ Object

Init function of the widget

Parameters:

  • key (String)

    strnig the widget key


693
694
695
696
697
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 693

def OpenFirewallInitWrapper(key)
  OpenFirewallInit(CWM.GetProcessedWidget, key)

  nil
end

#OpenFirewallModified(_key) ⇒ Boolean

Check if the widget was modified

Parameters:

  • _key (String)

    the widget key

Returns:

  • (Boolean)

    true if widget was modified


721
722
723
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 721

def OpenFirewallModified(_key)
  @configuration_changed
end

#OpenFirewallStore(widget, _key, _event) ⇒ Object

Store function of the widget

Parameters:

  • widget (Hash)

    widget description

  • _key (String)

    strnig the widget key

  • _event (Hash)

    map that caused widget data storing


632
633
634
635
636
637
638
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 632

def OpenFirewallStore(widget, _key, _event)
  return unless open_firewall_widget?

  services = widget.fetch("services", [])
  StoreAllowedInterfaces(services)
  nil
end

#OpenFirewallStoreWrapper(key, event) ⇒ Object

Store function of the widget

Parameters:

  • key (String)

    strnig the widget key

  • event (Hash)

    map that caused widget data storing


702
703
704
705
706
707
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 702

def OpenFirewallStoreWrapper(key, event)
  event = deep_copy(event)
  OpenFirewallStore(CWM.GetProcessedWidget, key, event)

  nil
end

#OpenFirewallWidgetExistsBoolean

Check whether the whole firewall widget ( open port checkbox and fw details button) exists

Returns:

  • (Boolean)

    true if both widgets exist


750
751
752
753
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 750

def OpenFirewallWidgetExists
  UI.WidgetExists(Id("_cwm_open_firewall")) &&
    UI.WidgetExists(Id("_cwm_firewall_details"))
end

#Selected2Opened(ifaces, _nm_ifaces_have_to_be_selected) ⇒ Object

Get the list of all interfaces that will be selected

Parameters:

  • ifaces (Array<String>)

    a list of interfaces selected by the user

  • _nm_ifaces_have_to_be_selected (Boolean)

    defines whether also NetworkManager have to be selected too

Returns:

  • a list of interfaces that will be opened


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

def Selected2Opened(ifaces, _nm_ifaces_have_to_be_selected)
  log.info("Selected ifaces: #{ifaces}")
  zone_names = ifaces.map do |name|
    zone = interface_zone(name)
    zone ? zone.name : default_zone.name
  end
  zone_names.uniq!
  log.info("Ifaces zone names: #{zone_names}")

  zone_ifaces =
    zone_names.map do |zone_name|
      zone = firewalld.find_zone(zone_name)
      next [] unless zone

      interfaces = zone.interfaces

      next(interfaces) unless zone_name == default_zone.name

      interfaces += default_interfaces

      left_explicitly = interfaces.select { |i| ifaces.include?(i) }.uniq
      log.info("Ifaces left in zone: #{left_explicitly}")

      next [] if left_explicitly.empty?

      interfaces
    end

  zone_ifaces.flatten.uniq
end

#SetFirewallLabel(status) ⇒ Object

Set the firewall status label

Parameters:

  • status (Symbol)

    symbol one of off,closed, open_all,custom, `not_installed


123
124
125
126
127
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 123

def SetFirewallLabel(status)
  UI.ReplaceWidget(Id(:_cwm_firewall_status_rp), Label(firewall_status_label(status)))

  nil
end

#StoreAllowedInterfaces(services) ⇒ Object

Store the list of allowed interfaces Users the internal variables

Parameters:

  • services (Array<String>)

    a list of services


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

def StoreAllowedInterfaces(services)
  services = deep_copy(services)
  # do not save anything if configuration didn't change
  return if !configuration_changed

  zones =
    known_interfaces.each_with_object([]) do |iface, memo|
      if allowed_interfaces.include?(iface.name)
        zone_name = iface.zone ? iface.zone.name : firewalld.default_zone
        memo << zone_name
      end
    end

  firewalld.zones.each do |zone|
    if zones.include?(zone.name)
      services.each do |service|
        zone.add_service(service) unless zone.services.include?(service)
      end
    else
      services.each do |service|
        zone.remove_service(service) if zone.services.include?(service)
      end
    end
  end

  nil
end

#UpdateFirewallStatusObject

Update the firewall status label according to the current status


144
145
146
147
148
149
150
151
152
153
154
155
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 144

def UpdateFirewallStatus
  InitAllInterfacesList() if all_interfaces.nil?

  status = current_firewall_status
  log.info("Status: #{status}, All: #{all_interfaces}, Allowed: #{allowed_interfaces}")

  SetFirewallLabel(status)
  open = [:open_all, :custom].include?(status)
  UI.ChangeWidget(Id("_cwm_open_firewall"), :Value, open)

  nil
end