-
Notifications
You must be signed in to change notification settings - Fork 8
/
zone.rb
229 lines (202 loc) · 6.87 KB
/
zone.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# ------------------------------------------------------------------------------
# Copyright (c) 2018 SUSE LLC
#
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, contact SUSE.
#
# To contact SUSE about this file by physical or electronic mail, you may find
# current contact information at www.suse.com.
# ------------------------------------------------------------------------------
require "yast"
require "cwm/common_widgets"
require "cwm/page"
require "cwm/table"
require "cwm/tabs"
require "y2firewall/widgets/allowed_services"
module Y2Firewall
module Widgets
module Pages
# A page for a firewall zone
class Zone < CWM::Page
# Constructor
#
# @param zone [Y2Firewall::Firewalld::Zone]
# @param pager [CWM::TreePager]
def initialize(zone, pager)
super()
Yast.import "Popup"
textdomain "firewall"
@zone = zone
@pager = pager
self.widget_id = "z:" + zone.name
end
# @macro seeAbstractWidget
def label
@zone.name
end
# @macro seeCustomWidget
def contents
VBox(
CWM::Tabs.new(
ServicesTab.new(@zone, @pager),
PortsTab.new(@zone)
)
)
end
end
# A Tab for ports in a firewall zone
class PortsTab < CWM::Tab
# Constructor
#
# @param zone [Y2Firewall::Firewalld::Zone]
def initialize(zone)
super()
textdomain "firewall"
@zone = zone
end
def label
_("Ports")
end
def contents
VBox(
PortsForProtocols.new(@zone),
VStretch()
)
end
# A group of InputFields to specify the open TCP, UDP, SCTP and DCCP ports.
class PortsForProtocols < CWM::CustomWidget
extend Yast::I18n
PROTOCOLS = {
# TRANSLATORS: TCP is the Transmission Control Protocol
tcp: N_("TCP Ports"),
# TRANSLATORS: UDP is the User Datagram Protocol
udp: N_("UDP Ports"),
# TRANSLATORS: SCTP is the Stream Control Transmission Protocol
sctp: N_("SCTP Ports"),
# TRANSLATORS: DCCP is the Datagram Congestion Control Protocol
dccp: N_("DCCP Ports")
}.freeze
def initialize(zone)
super()
textdomain "firewall"
@zone = zone
end
def contents
fields = PROTOCOLS.map do |proto_sym, label|
InputField(Id(proto_sym), Opt(:hstretch), _(label))
end
VBox(* fields)
end
def valid_port_description
format(
_("Enter ports or port ranges, separated by spaces and/or commas.\n" \
"A port is an integer.\n" \
"A port range is port-dash-port (with no spaces).\n" \
"For example:\n" \
"%s"),
"16001-16009, 18080"
)
end
def help
valid_port_description.gsub("\n", "<br>\n")
end
def init
by_proto = ports_from_array(@zone.ports)
PROTOCOLS.each do |proto_sym, _label|
s = by_proto.fetch(proto_sym, []).join(", ")
Yast::UI.ChangeWidget(Id(proto_sym), :Value, s)
end
end
def store
@zone.ports = ports_to_array(values_by_proto.to_h)
end
def validate
values_by_proto.each do |proto_sym, ranges|
invalid_range = ranges.find { |r| !valid_range?(r) }
next unless invalid_range
Yast::UI.SetFocus(Id(proto_sym))
err_msg = format(_("Invalid port range: %s"), invalid_range)
Yast::Popup.Error(err_msg + "\n" + valid_port_description)
return false
end
true
end
private
# @return [Hash{Symbol => Array<String>}]
def values_by_proto
PROTOCOLS.map do |proto_sym, _label|
line = Yast::UI.QueryWidget(Id(proto_sym), :Value)
[proto_sym, items_from_ui(line)]
end
end
def valid_range?(range)
range =~ /\d+/ || range =~ /\d+-\d+/
end
def items_from_ui(str)
# the separator is at least one comma or space, surrounded by optional spaces
str.split(/ *[, ] */)
end
# @param hash [Hash{Symbol => Array<String>}] ports specification
# categorized by protocol
# @return [Array] ports specification
# as array for {Y2Firewall::Firewalld::Zone#ports}
# @example
# h = { tcp: ["55555-55666", "44444"], udp: ["33333"] }
# a = ["55555-55666/tcp", "44444/tcp", "33333/udp"]
# ports_to_array(h) # => a
def ports_to_array(hash)
hash.map { |sym, ports| ports.map { |p| "#{p}/#{sym}" } }.flatten
end
# @param ports_list [Array] ports specification
# as array from {Y2Firewall::Firewalld::Zone#ports}
# @return [Hash{Symbol => Array<String>}] ports specification
# categorized by protocol
# @example
# a = ["55555-55666/tcp", "44444/tcp", "33333/udp"]
# h = { tcp: ["55555-55666", "44444"], udp: ["33333"] }
# ports_from_array(a) # => h
def ports_from_array(ports_list)
ports_list
.map { |p| p.split("/") }
.each_with_object({}) do |i, acc|
ports, proto = *i
proto = proto.to_sym
acc[proto] ||= []
acc[proto] << ports
end
end
end
end
# A Tab for services in a firewall zone
class ServicesTab < CWM::Tab
# Constructor
#
# @param zone [Y2Firewall::Firewalld::Zone]
# @param _pager [CWM::TreePager]
def initialize(zone, _pager)
super()
textdomain "firewall"
@zone = zone
@allowed_services_widget = Y2Firewall::Widgets::AllowedServices.new(zone)
self.widget_id = "st:" + zone.name
end
def label
_("Services")
end
# @macro seeCustomWidget
def contents
VBox(@allowed_services_widget)
end
end
end
end
end