Class: Yast::PortAliasesClass

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

Instance Method Summary collapse

Instance Method Details

#AllowedPortNameOrNumberString

Function returns string describing allowed port name or number.



134
135
136
137
138
139
140
141
# File 'library/network/src/modules/PortAliases.rb', line 134

def AllowedPortNameOrNumber
  # TRANSLATORS: popup informing message, allowed characters for port-names
  _(
    "A port name may consist of the characters 'a-z', 'A-Z', '0-9', and '*+._-'.\n" \
      "A port number may be a number from 0 to 65535.\n" \
      "No spaces are allowed.\n"
  )
end

#GetListOfServiceAliases(port) ⇒ Array

Function returns list of aliases (port-names and port-numbers) for requested port-number or port-name. Also the requested name or port is returned.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'library/network/src/modules/PortAliases.rb', line 232

def GetListOfServiceAliases(port)
  service_aliases = [port]
  port_number = nil

  # service is a port number
  if Builtins.regexpmatch(port, "^[0123456789]+$")
    port_number = Builtins.tointeger(port)

    sport_name = Ops.get(@SERVICE_PORT_TO_NAME, port_number) do
      LoadAndReturnPortToName(port_number)
    end

    if !sport_name.nil?
      service_aliases = Convert.convert(
        Builtins.union(
          service_aliases,
          Builtins.splitstring(sport_name, " ")
        ),
        from: "list",
        to:   "list <string>"
      )
    end
    # service is a port name, any space isn't allowed
  elsif IsAllowedPortName(port)
    found_alias_port = Ops.get(@SERVICE_NAME_TO_PORT, port) do
      LoadAndReturnNameToPort(port)
    end
    if !found_alias_port.nil?
      service_aliases = Builtins.add(
        service_aliases,
        Builtins.tostring(found_alias_port)
      )

      # search for another port-name aliases when port-number found
      service_aliases = Convert.convert(
        Builtins.union(
          service_aliases,
          Builtins.splitstring(
            Ops.get(@SERVICE_PORT_TO_NAME, found_alias_port) do
              LoadAndReturnPortToName(found_alias_port)
            end,
            " "
          )
        ),
        from: "list",
        to:   "list <string>"
      )
    end
  else
    if !Builtins.contains(@cache_not_allowed_ports, port)
      @cache_not_allowed_ports = Builtins.add(
        @cache_not_allowed_ports,
        port
      )
      Builtins.y2error("Port name '%1' is not allowed", port)
    else
      Builtins.y2debug("Port name '%1' is not allowed", port)
    end
    return [port]
  end

  Builtins.toset(service_aliases)
end

#GetPortNumber(port_name) ⇒ Object

Function returns a port number for the port name alias



313
314
315
316
317
318
319
320
321
322
323
324
# File 'library/network/src/modules/PortAliases.rb', line 313

def GetPortNumber(port_name)
  return Builtins.tointeger(port_name) if Builtins.regexpmatch(port_name, "^[0123456789]+$")

  port_number = Ops.get(@SERVICE_NAME_TO_PORT, port_name) do
    LoadAndReturnNameToPort(port_name)
  end

  # not a known port
  return nil if port_number.nil?

  Builtins.tointeger(port_number)
end

#IsAllowedPortName(port_name) ⇒ Boolean

Function returns if the port name is allowed port name (or number).



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'library/network/src/modules/PortAliases.rb', line 115

def IsAllowedPortName(port_name)
  if port_name.nil?
    Builtins.y2error("Invalid port name: %1", port_name)
    return false
    # port is number
  elsif Builtins.regexpmatch(port_name, "^[0123456789]+$")
    port_number = Builtins.tointeger(port_name)
    # checking range
    return Ops.greater_or_equal(port_number, 0) &&
        Ops.less_or_equal(port_number, 65_535)
    # port is name
  else
    return Builtins.regexpmatch(port_name, @allowed_service_regexp)
  end
end

#IsKnownPortName(port_name) ⇒ Boolean

Function returns if the requested port-name is known port. Known port have an IANA alias.



301
302
303
304
305
306
307
# File 'library/network/src/modules/PortAliases.rb', line 301

def IsKnownPortName(port_name)
  # function returns the requested port and aliases if exists
  if Ops.greater_than(Builtins.size(GetListOfServiceAliases(port_name)), 1)
    return true
  end
  false
end

#LoadAndReturnNameToPort(port_name) ⇒ Object

Internal function for loading unknown ports into memory and returning them as integer



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

def LoadAndReturnNameToPort(port_name)
  if !IsAllowedPortName(port_name)
    Builtins.y2error("Disallwed port-name '%1'", port_name)
    return nil
  end

  command = Ops.add(
    Ops.add("grep --perl-regexp \"^", QuoteString(port_name)),
    "[ \\t]\" /etc/services | sed \"s/[^ \\t]*[ \\t]*\\([^/ \\t]*\\).*/\\1/\""
  )
  found = Convert.to_map(SCR.Execute(path(".target.bash_output"), command))
  alias_found = nil

  if found["exit"] == 0
    found["stdout"].split("\n").each do |alias_|
      next if alias_.empty?
      alias_found = Builtins.tointeger(alias_)
    end
  else
    Builtins.y2error(
      "Services Command: %1 -> %2",
      command,
      Ops.get_string(found, "stderr", "")
    )
    return nil
  end

  # store results for later requests
  Ops.set(@SERVICE_NAME_TO_PORT, port_name, alias_found)

  alias_found
end

#LoadAndReturnPortToName(port_number) ⇒ Object

Internal function for loading unknown ports into memory and returning them as list



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

def LoadAndReturnPortToName(port_number)
  command = Ops.add(
    Ops.add("grep \"^[^#].*[ \\t]", port_number),
    "/\" /etc/services | sed \"s/\\([^ \\t]*\\)[ \\t]*.*/\\1/\""
  )
  found = Convert.to_map(SCR.Execute(path(".target.bash_output"), command))
  aliases = []

  if found["exit"] == 0
    found["stdout"].split("\n").each do |alias_|
      next if alias_.empty?
      aliases = Builtins.add(aliases, alias_)
    end
  else
    Builtins.y2error(
      "Services Command: %1 -> %2",
      command,
      Ops.get_string(found, "stderr", "")
    )
    return nil
  end

  # store results for later requests
  Ops.set(
    @SERVICE_PORT_TO_NAME,
    port_number,
    Builtins.mergestring(Builtins.toset(aliases), " ")
  )

  Ops.get(@SERVICE_PORT_TO_NAME, port_number, "")
end

#mainObject



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

def main
  textdomain "base"

  # an internal service aliases map for port-numbers pointing to port-names,
  # 	aliases are separated by space
  @SERVICE_PORT_TO_NAME = {
    22   => "ssh",
    25   => "smtp",
    53   => "domain",
    67   => "bootps",
    68   => "bootpc",
    69   => "tftp",
    80   => "http www www-http",
    110  => "pop3",
    111  => "sunrpc",
    123  => "ntp",
    137  => "netbios-ns",
    138  => "netbios-dgm",
    139  => "netbios-ssn",
    143  => "imap",
    389  => "ldap",
    443  => "https",
    445  => "microsoft-ds",
    500  => "isakmp",
    631  => "ipp",
    636  => "ldaps",
    873  => "rsync",
    993  => "imaps",
    995  => "pop3s",
    3128 => "ndl-aas",
    4500 => "ipsec-nat-t",
    8080 => "http-alt"
  }

  # an internal service aliases map for port-names pointing to port-numbers
  @SERVICE_NAME_TO_PORT = {
    "ssh"          => 22,
    "smtp"         => 25,
    "domain"       => 53,
    "bootps"       => 67,
    "bootpc"       => 68,
    "tftp"         => 69,
    "http"         => 80,
    "www"          => 80,
    "www-http"     => 80,
    "pop3"         => 110,
    "sunrpc"       => 111,
    "ntp"          => 123,
    "netbios-ns"   => 137,
    "netbios-dgm"  => 138,
    "netbios-ssn"  => 139,
    "imap"         => 143,
    "ldap"         => 389,
    "https"        => 443,
    "microsoft-ds" => 445,
    "isakmp"       => 500,
    "ipp"          => 631,
    "ldaps"        => 636,
    "rsync"        => 873,
    "imaps"        => 993,
    "pop3s"        => 995,
    "ndl-aas"      => 3128,
    "ipsec-nat-t"  => 4500,
    "http-alt"     => 8080
  }

  # This variable contains characters allowed in port-names, backslashed for regexpmatch()
  @allowed_service_regexp = "^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*/+._-]*$"

  @cache_not_allowed_ports = []
end

#QuoteString(port_name) ⇒ Object

Internal function for preparing string for grep command



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

def QuoteString(port_name)
  port_name = Builtins.mergestring(
    Builtins.splitstring(port_name, "\""),
    "\\\""
  )
  port_name = Builtins.mergestring(
    Builtins.splitstring(port_name, "*"),
    "\\*"
  )
  port_name = Builtins.mergestring(
    Builtins.splitstring(port_name, "."),
    "\\."
  )
  port_name
end