Class: Yast::CWMFirewallInterfacesClass

Inherits:
Module
  • Object
show all
Includes:
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

Instance Attribute Details

#all_interfacesObject (readonly)

Array<String>

List of all the system network interfaces



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

def all_interfaces
  @all_interfaces
end

#allowed_interfacesObject (readonly)

Array<String>

List of all interfaces relevant for firewall settings



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

def allowed_interfaces
  @allowed_interfaces
end

#configuration_changedObject (readonly)

Boolean

Information if configuration was changed by user



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

def configuration_changed
  @configuration_changed
end

Instance Method Details

#CreateInterfacesWidget(settings) ⇒ Hash

Get the widget description map <pre>

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)
    

</pre>

Parameters:

  • settings (Hash{String => Object})

    a map of all parameters needed to create the widget properly

Returns:

  • (Hash)

    the widget description map



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

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

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

  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 <pre>

  • “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
    

</pre>

Parameters:

  • settings (Hash{String => Object})

    a map of all parameters needed to create the widget properly

Returns:

  • (Hash)

    the widget description map



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

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.api.service_supported?(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(
      _("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

#DisableOpenFirewallWidgetObject

Disable the whole firewal widget



735
736
737
738
739
740
741
742
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 735

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



186
187
188
189
190
191
192
193
194
195
196
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 186

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) if !common_details_handler.nil?

  nil
end

#EnableOpenFirewallWidgetObject

Enable the whole firewal widget



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

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



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

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

#InitAllInterfacesListObject

Initialize the list of all known interfaces



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

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



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

def InitAllowedInterfaces(services)
  service_status = {}

  services.each { |s| firewalld.find_service(s) }

  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 && default_zone.services.include?(service)
    end
    if service_status[firewalld.default_zone]
      @allowed_interfaces = (allowed_interfaces + default_interfaces).uniq
    end
  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



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 302

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



455
456
457
458
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 455

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



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 275

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



445
446
447
448
449
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 445

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



323
324
325
326
327
328
329
330
331
332
333
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 323

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



463
464
465
466
467
468
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 463

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



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

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.select { |i| !ifaces.include?(i) }
  log.info("added_ifaces: #{added_ifaces}")
  removed_ifaces = ifaces.select { |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



474
475
476
477
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 474

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

#mainObject



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

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



899
900
901
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 899

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



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

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



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

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:



785
786
787
788
789
790
791
792
793
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 785

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



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

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



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

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



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

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
# 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



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

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



747
748
749
750
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 747

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'library/network/src/modules/CWMFirewallInterfaces.rb', line 160

def Selected2Opened(ifaces, _nm_ifaces_have_to_be_selected)
  log.info("Selected ifaces: #{ifaces}")
  zone_names = ifaces.map { |i| interface_zone(i) || default_zone.name }.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



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

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



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

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 |known_interface, a|
      if allowed_interfaces.include?(known_interface["id"])
        zone_name = known_interface["zone"] || default_zone.name
        a << zone_name
      end
    end

  firewalld.zones.map do |zone|
    if zones.include?(zone.name)
      services.map do |service|
        zone.add_service(service) unless zone.services.include?(service)
      end
    else
      services.map 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



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

def UpdateFirewallStatus
  InitAllInterfacesList() if all_interfaces.nil?

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

  SetFirewallLabel(status)
  open = status == :open_all || status == :custom
  UI.ChangeWidget(Id("_cwm_open_firewall"), :Value, open)

  nil
end