Class: Yast::TftpServerClass

Inherits:
Module
  • Object
show all
Includes:
Logger
Defined in:
../../src/modules/TftpServer.rb

Constant Summary

SERVICE_NAME =
"xinetd"

Instance Method Summary (collapse)

Instance Method Details

- (Hash) AutoPackages

Return needed packages and packages to be removed during autoinstallation.

Returns:

  • (Hash)

    of lists.



320
321
322
323
324
# File '../../src/modules/TftpServer.rb', line 320

def AutoPackages
  install_pkgs = deep_copy(@required_packages)
  remove_pkgs = []
  { "install" => install_pkgs, "remove" => remove_pkgs }
end

- (Hash) Export

Dump the tftp-server settings to a single map (For use by autoinstallation.)

Returns:

  • (Hash)

    Dumped settings (later acceptable by Import ())



291
292
293
294
295
# File '../../src/modules/TftpServer.rb', line 291

def Export
  settings = { "start_tftpd" => @start }
  Ops.set(settings, "tftp_directory", @directory) if @start
  deep_copy(settings)
end

- (Object) ForeignServersError

Return error string to be used in WriteOnly (for autoinst) or before the edit dialog.

Returns:

  • error string



141
142
143
144
145
146
147
148
149
150
151
152
# File '../../src/modules/TftpServer.rb', line 141

def ForeignServersError
  # error popup
  # %1 is a command name (or a comma (, ) separated list of them)
  Builtins.sformat(
    _(
      "This module can only use xinetd to set up TFTP.\n" +
        "However, another program is serving TFTP: %1.\n" +
        "Exiting.\n"
    ),
    @foreign_servers
  )
end

- (Object) GetModified

Returns true if the settings were modified

Returns:

  • settings were modified



58
59
60
# File '../../src/modules/TftpServer.rb', line 58

def GetModified
  @modified
end

- (Boolean) Import(settings)

Get all tftp-server settings from the first parameter (For use by autoinstallation.)

Parameters:

  • settings (Hash)

    The YCP structure to be imported.

Returns:

  • (Boolean)

    True on success



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File '../../src/modules/TftpServer.rb', line 270

def Import(settings)
  settings = deep_copy(settings)
  if settings != {}
    s = "start_tftpd"
    d = "tftp_directory"
    if !Builtins.haskey(settings, s)
      Builtins.y2error("Missing at Import: '%1'.", s)
      return false
    end
    if Ops.get_boolean(settings, s, false) && !Builtins.haskey(settings, d)
      Builtins.y2error("Missing at Import: '%1'.", d)
      return false
    end
  end
  Set(settings)
  true
end

- (Object) main



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
# File '../../src/modules/TftpServer.rb', line 21

def main
  textdomain "tftp-server"

  Yast.import "Progress"
  Yast.import "Report"
  Yast.import "Service"
  Yast.import "Summary"
  Yast.import "SuSEFirewall"

  # Any settings modified?
  # As we have only a single dialog which handles it by itself,
  # it is used only by autoinst cloning.
  @modified = false

  # Required packages for operation
  @required_packages = ["xinetd", "tftp"]

  # Start tftpd via xinetd?
  @start = false

  # Image directory, last argument of in.tftpd
  @directory = ""

  # Other arguments to in.tftpd, ie. not including -s or /dir
  @other_args = ""


  # Detect who is serving tftp:
  # Inetd may be running, it is the default. But it is ok unless it is
  # serving tftp. So we detect who is serving tftp and warn if it is
  # not xinetd or in.tftpd.
  # If nonempty, the user is notified and the module gives up.
  @foreign_servers = ""
end

- (Object) ParseServerArgs(server_args)

Extract the directory and other arguments. global to make testing easier

Parameters:

  • server_args (String)

    server_args from xinetd.conf



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File '../../src/modules/TftpServer.rb', line 74

def ParseServerArgs(server_args)
  # extract the last argument and kick "-s".
  server_args_l = Builtins.filter(Builtins.splitstring(server_args, " \t")) do |s|
    s != ""
  end
  sz = Builtins.size(server_args_l)
  i = 0
  other_args_l = Builtins.filter(server_args_l) do |s|
    i = Ops.add(i, 1)
    s != "-s" && i != sz
  end
  @directory = Ops.get(server_args_l, Ops.subtract(sz, 1), "")
  @other_args = Builtins.mergestring(other_args_l, " ")

  nil
end

- (Object) Read

Read all tftp-server settings

Returns:

  • true on success



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
# File '../../src/modules/TftpServer.rb', line 93

def Read
  # foreign_servers:
  # get command names via lsof, filter out xinetd and in.tftpd
  out = Convert.to_map(
    SCR.Execute(path(".target.bash_output"), "/usr/bin/lsof -i :tftp -Fc")
  )
  lines = Builtins.splitstring(Ops.get_string(out, "stdout", ""), "\n")
  # the command is field 'c'
  lines = Builtins.filter(lines) { |l| Builtins.substring(l, 0, 1) == "c" }
  # strip the c
  lines = Builtins.maplist(lines) do |l|
    Builtins.substring(l, 1, Ops.subtract(Builtins.size(l), 1))
  end
  # filter out our servers
  lines = Builtins.filter(lines) { |l| l != "xinetd" && l != "in.tftpd" }
  @foreign_servers = Builtins.mergestring(lines, ", ")

  xinetd_start = Service.Enabled(SERVICE_NAME)

  # is the config file there at all?
  sections = SCR.Dir(path(".etc.xinetd_d.tftp.section"))
  disable = Convert.to_string(
    SCR.Read(path(".etc.xinetd_d.tftp.value.tftp.disable"))
  )
  @start = xinetd_start && sections != [] && disable != "yes"

  server_args = Convert.to_string(
    SCR.Read(path(".etc.xinetd_d.tftp.value.tftp.server_args"))
  )
  if server_args == nil
    # default
    #	server_args = "-s /tftpboot";
    server_args = ""
  end

  ParseServerArgs(server_args)

  # TODO only when we have our own Progress
  #boolean progress_orig = Progress::set (false);
  SuSEFirewall.Read
  #Progress::set (progress_orig);

  true
end

- (void) Set(settings)

This method returns an undefined value.

Set module data, without validity checking

Parameters:

  • settings (Hash)

    may be empty for reset



256
257
258
259
260
261
262
263
# File '../../src/modules/TftpServer.rb', line 256

def Set(settings)
  settings = deep_copy(settings)
  @start = Ops.get_boolean(settings, "start_tftpd", false)
  @directory = Ops.get_string(settings, "tftp_directory", "")
  @other_args = ""

  nil
end

- (Object) SetModified

Function sets an internal variable indicating that any settings were modified to “true”. Used for autoinst cloning.



65
66
67
68
69
# File '../../src/modules/TftpServer.rb', line 65

def SetModified
  @modified = true

  nil
end

- (Object) Summary

Returns Html formatted configuration summary

Returns:

  • Html formatted configuration summary



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File '../../src/modules/TftpServer.rb', line 298

def Summary
  summary = ""
  nc = Summary.NotConfigured

  # summary header
  summary = Summary.AddHeader(summary, _("TFTP Server Enabled:"))
  # summary item: an option is turned on
  summary = Summary.AddLine(summary, @start ? _("Yes") : nc)

  # summary header
  summary = Summary.AddHeader(summary, _("Boot Image Directory:"))
  summary = Summary.AddLine(summary, @directory != "" ? @directory : nc)

  summary
end

- (Object) Write

Write all tftp-server settings

Returns:

  • true on success



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
244
245
246
247
248
249
250
251
# File '../../src/modules/TftpServer.rb', line 217

def Write
  # write the config file
  # image dir: if does not exist, create with root:root rwxr-xr-x
  # firewall??
  # enable and (re)start xinetd

  return false if !WriteOnly()

  # enable and (re)start xinetd

  # in.tftpd will linger around for 15 minutes waiting for a new connection
  # so we must kill it otherwise it will be using the old parameters
  SCR.Execute(path(".target.bash"), "/usr/bin/killall in.tftpd")

  if @start
    Service.Restart(SERVICE_NAME)
  else
    # xinetd may be needed for other services so we never turn it
    # off. It will exit anyway if no services are configured.
    # If it is running, restart it.
    service = SystemdService.find(SERVICE_NAME)
    if service.nil?
      log.error("Could not find service #{SERVICE_NAME}")
      Report.Error(_("Cannot reload service %{name}") % { :name => SERVICE_NAME })
    end
    service.try_restart
  end

  # TODO only when we have our own Progress
  #boolean progress_orig = Progress::set (false);
  SuSEFirewall.ActivateConfiguration
  #Progress::set (progress_orig);

  true
end

- (Object) WriteOnly

Write all tftp-server settings without actually (re)starting the service

Returns:

  • true on success



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
# File '../../src/modules/TftpServer.rb', line 157

def WriteOnly
  Builtins.y2milestone("Writing")

  # give up if tftp is served by someone else
  if @foreign_servers != ""
    Report.Error(ForeignServersError())
    return false
  end

  # write the config file
  #
  #  create it if it does not exist
  #  could be a normal situation at initial setup
  #  or a broken setup, ok if we fix it when writing
  #  but that means messing up with other parameters
  #  lets touch just the basics
  #  the first "item" is the brace following the section start
  SCR.Write(path(".etc.xinetd_d.tftp.value.tftp.\"{\""), "")
  SCR.Write(path(".etc.xinetd_d.tftp.value_type.tftp.\"{\""), 1)
  SCR.Write(
    path(".etc.xinetd_d.tftp.value.tftp.disable"),
    @start ? "no" : "yes"
  )
  if @start
    SCR.Write(path(".etc.xinetd_d.tftp.value.tftp.socket_type"), "dgram")
    SCR.Write(path(".etc.xinetd_d.tftp.value.tftp.protocol"), "udp")
    SCR.Write(path(".etc.xinetd_d.tftp.value.tftp.wait"), "yes")
    SCR.Write(path(".etc.xinetd_d.tftp.value.tftp.user"), "root")
    SCR.Write(
      path(".etc.xinetd_d.tftp.value.tftp.server"),
      "/usr/sbin/in.tftpd"
    )
    server_args = Builtins.sformat("%1 -s %2", @other_args, @directory)
    SCR.Write(
      path(".etc.xinetd_d.tftp.value.tftp.server_args"),
      server_args
    )
  end

  # flush
  SCR.Write(path(".etc.xinetd_d.tftp"), nil)

  # image dir: if does not exist, create with root:root rwxr-xr-x
  SCR.Execute(path(".target.mkdir"), @directory)

  # firewall??

  # enable and (re)start xinetd
  Service.Enable(SERVICE_NAME) if @start

  # TODO only when we have our own Progress
  #boolean progress_orig = Progress::set (false);
  SuSEFirewall.WriteOnly
  #Progress::set (progress_orig);

  true
end