Module: Yast::NfsServerRoutinesInclude

Defined in:
../../src/include/nfs_server/routines.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) AllowedTableItems(allowed)

Returns a ui table list of items

Examples:

AllowedTableItems ([“*.local.domain(ro)”, “@trusted(rw)”])

Parameters:

  • allowed (Array<String>)

    a list of allowed host specifications

Returns:

  • a ui table list of items



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File '../../src/include/nfs_server/routines.rb', line 59

def AllowedTableItems(allowed)
  allowed = deep_copy(allowed)
  count = 0
  Builtins.maplist(allowed) do |str|
    sp = AllowedToHostsOpts(str)
    it = Item(
      Id(count),
      Ops.add(Ops.get(sp, 0, ""), " "),
      Ops.add(Ops.get(sp, 1, ""), " ")
    )
    count = Ops.add(count, 1)
    deep_copy(it)
  end
end

- ("hosts", "opts") AllowedToHostsOpts(hosts)

Split the allowed host specification

Parameters:

  • hosts (String)

    “hosts(opts)”

Returns:

  • ("hosts", "opts")


43
44
45
46
47
48
49
50
51
52
53
54
# File '../../src/include/nfs_server/routines.rb', line 43

def AllowedToHostsOpts(hosts)
  brpos = Builtins.findfirstof(hosts, "(")
  opts = ""
  if brpos != nil
    opts = Builtins.substring(hosts, Ops.add(brpos, 1))
    hosts = Builtins.substring(hosts, 0, brpos)

    brpos = Builtins.findfirstof(opts, ")")
    opts = Builtins.substring(opts, 0, brpos) if brpos != nil
  end
  [hosts, opts]
end

- (Object) CheckExportOptions(options)

Check for the validity of export options: [A-Za-z0-9=/.:,_-]* If invalid, a message is displayed.

Parameters:

  • options (String)

    spaces and parentheses already removed

Returns:

  • whether valid



348
349
350
351
352
353
354
355
356
357
358
359
360
# File '../../src/include/nfs_server/routines.rb', line 348

def CheckExportOptions(options)
  # colon is allowed for sec= option, see man 5 exports
  if Builtins.regexpmatch(options, "[^A-Za-z0-9=/.:,_-]")
    # error popup message
    Report.Error(
      _(
        "Invalid option.\nOnly letters, digits, and the characters =/.:,_- are allowed."
      )
    )
    return false
  end
  true
end

- (Object) CheckExportOptions_strict(options)

Check for the validity of export options: only those listed in exports(5) are accepted. Unused - to allow not only nfs-utils but also nfs-server. If invalid, a message is displayed.

Parameters:

  • options (String)

    spaces and parentheses already removed

Returns:

  • whether valid



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
# File '../../src/include/nfs_server/routines.rb', line 369

def CheckExportOptions_strict(options)
  o1 = [
    "secure",
    "insecure",
    "rw",
    "ro",
    "sync",
    "async",
    "no_wdelay",
    "wdelay",
    "nohide",
    "hide",
    "no_subtree_check",
    "subtree_check",
    "insecure_locks",
    "secure_locks",
    "no_auth_nlm",
    "auth_nlm",
    "root_squash",
    "no_root_squash",
    "all_squash",
    "no_all_squash"
  ]
  o_value = ["anonuid", "anongid"]
  opts = Builtins.splitstring(options, ",")

  ret = true
  opts = Builtins.filter(opts) { |e| !Builtins.contains(o1, e) }
  Builtins.foreach(opts) do |e|
    opt = Builtins.splitstring(e, "=")
    if !Builtins.contains(o_value, Ops.get(opt, 0, ""))
      # error popup message
      Popup.Error(Builtins.sformat(_("Unknown option: '%1'"), e))
      ret = false
    elsif Builtins.size(opt) != 2 ||
        !Builtins.regexpmatch(Ops.get(opt, 1, ""), "[0-9]+")
      # error popup message
      Popup.Error(Builtins.sformat(_("Invalid option: '%1'"), e))
      ret = false
    end
  end
  ret
end

- (Object) CheckNoSpaces(name)

Check for the validity of client specification: fewer than 70 chars, no blanks. If invalid, a message is displayed.

Parameters:

  • name (String)

    options

Returns:

  • whether valid



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File '../../src/include/nfs_server/routines.rb', line 323

def CheckNoSpaces(name)
  if Ops.less_than(Builtins.size(name), 70) &&
      Builtins.findfirstof(name, " \t") == nil
    return true
  else
    # error popup message
    Report.Message(
      Builtins.sformat(
        _(
          "The wild card or options string is invalid.\n" +
            "It must be shorter than 70 characters and it\n" +
            "must not contain spaces.\n"
        )
      )
    )
  end
  false
end

- (Object) CheckSyntaxErrors(exports)

Check for suspicious allowed lists and warn the user. Like “host(rw, sync)” with the space.



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
446
447
448
449
# File '../../src/include/nfs_server/routines.rb', line 415

def CheckSyntaxErrors(exports)
  exports = deep_copy(exports)
  bad_shares = {}
  Builtins.foreach(exports) do |entry|
    Builtins.foreach(
      Convert.convert(
        Ops.get(entry, "allowed") { ["()"] },
        :from => "any",
        :to   => "list <string>"
      )
    ) do |client|
      if Builtins.search(client, "(") == nil ||
          Builtins.search(client, ")") == nil
        Ops.set(bad_shares, Ops.get_string(entry, "mountpoint", "?"), true)
      end
    end
  end
  bad_shares_l = Builtins.maplist(bad_shares) { |s, d| s }
  bad_shares_s = Builtins.mergestring(bad_shares_l, ", ")
  if bad_shares_s != ""
    # %1 is a list of exported paths
    Report.Warning(
      Builtins.sformat(
        _(
          "There are unbalanced parentheses in export options\n" +
            "for %1.\n" +
            "Likely, there is a spurious whitespace in the configuration file.\n"
        ),
        bad_shares_s
      )
    )
  end

  nil
end

- (Object) CheckUniqueRootForClient(exports, expath, client, eopts)

Report the first error that is encountered while checking for Unique NFSv4 psuedofilesystem root.

Parameters:

  • exports (Array<Hash>)

    list of exports

  • expath (String)

    the exported filesystem path

  • client (String)

    string representing a client (*, *.domain, ip address etc)

  • eopts (String)

    comma separated string of export options

Returns:

  • the first error encountered or nil



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
230
231
232
233
# File '../../src/include/nfs_server/routines.rb', line 164

def CheckUniqueRootForClient(exports, expath, client, eopts)
  exports = deep_copy(exports)
  exportpath = ""
  errorstring = nil
  clientrelation = 0

  if !Builtins.issubstring(eopts, "fsid=0") # Then no need to check for conflict.
    return nil
  end

  Builtins.foreach(
    Convert.convert(
      exports,
      :from => "list <map>",
      :to   => "list <map <string, any>>"
    )
  ) do |entry|
    exportpath = Ops.get_string(entry, "mountpoint", "")
    Builtins.foreach(
      Convert.convert(
        Ops.get(entry, "allowed") { ["()"] },
        :from => "any",
        :to   => "list <string>"
      )
    ) do |hostops|
      opts = ""
      clientexpr = ""
      pos = Builtins.findfirstof(hostops, "(")
      if pos != nil
        opts = Builtins.substring(hostops, Ops.add(pos, 1))
        clientexpr = Builtins.substring(hostops, 0, pos)

        pos = Builtins.findfirstof(opts, ")")
        opts = Builtins.substring(opts, 0, pos) if pos != nil
      end
      clientrelation = ClientRelated(client, clientexpr)
      if clientrelation != 0
        if Builtins.issubstring(opts, "fsid=0")
          if exportpath != expath
            if clientrelation == 1
              errorstring = Builtins.sformat(
                _(
                  "%3 and %4 are both exported with the option fsid=0\nfor the same client '%1' (contained in '%2')"
                ),
                client,
                clientexpr,
                expath,
                exportpath
              )
            else
              errorstring = Builtins.sformat(
                _(
                  "%3 and %4 are both exported with the option fsid=0\nfor the same client '%1' (contained in '%2')"
                ),
                clientexpr,
                client,
                expath,
                exportpath
              )
            end
            raise Break
          end
        end
      end
    end
    raise Break if errorstring != nil
  end

  errorstring
end

- (Object) ClientRelated(clntexpr1, clntexpr2)

Find out whether client representations are related .abc.com (contains)xyz.abc.com and xyz.* and abc.com are independent

FIXME This is not too intelligent. Ideally a while loop with matching '' is required. 1) Doesn't look default name domain. 2) Doesn't know how to deal with .abc. ; only single '' please :(

Examples:

1.2.3.4 (is contained in) 1.*.3.4,

Parameters:

  • clntexpr1 (String)

    first client representation to which check is being made

  • clntexpr2 (String)

    another client representatio against which the check is done

Returns:

  • 1, if clntexpr1 is contained in clntexpr2 and -1, if otherway round, and 0 if they are independent



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
# File '../../src/include/nfs_server/routines.rb', line 104

def ClientRelated(clntexpr1, clntexpr2)
  pos = Builtins.findfirstof(clntexpr2, "*")
  len = Builtins.size(clntexpr2)

  clntexpr2 = Builtins.tolower(clntexpr2)
  clntexpr1 = Builtins.tolower(clntexpr1)

  if pos == nil
    pos = Builtins.findfirstof(clntexpr1, "*")
    return 0 if pos == nil # FIXME We must continue investigating with name/ip resolution
    # Both expressions not having *, doesn't mean they are not
    # related.
    return Ops.multiply(-1, ClientRelated(clntexpr2, clntexpr1))
  end

  return 1 if clntexpr2 == "*"
  return 1 if clntexpr1 == clntexpr2

  if pos == Ops.subtract(len, 1) # expressions of type abc.xyz.*
    check = Builtins.substring(clntexpr2, 0, pos)
    matchpos = Builtins.findfirstof(clntexpr1, check)
    return 1 if matchpos == 0
  elsif pos == 0
    check = Builtins.substring(clntexpr2, 1)
    matchpos = Builtins.findfirstof(clntexpr1, check)
    right = Builtins.substring(clntexpr1, matchpos)
    return 1 if check == right
  else
    # expressions of type abc.*.xyz
    checkleft = Builtins.substring(clntexpr2, 0, pos)
    matchpos = Builtins.findfirstof(clntexpr1, checkleft)
    if matchpos == 0
      checkright = Builtins.substring(clntexpr2, Ops.add(pos, 1))
      matchpos2 = Builtins.findfirstof(clntexpr1, checkright)
      right = Builtins.substring(clntexpr1, matchpos2)

      return 1 if checkright == right
    end
  end

  0
end

- (Object) current_export_dir

Returns currently selected directory configured for export via NFS.



300
301
302
# File '../../src/include/nfs_server/routines.rb', line 300

def current_export_dir
  UI.QueryWidget(Id(:exportsbox), :CurrentItem)
end

- (Object) ExportsItems(exports)

Returns a ui table list of mountpoints, id'ed by themselves

Parameters:

  • exports (Array<Hash>)

    list of exports

Returns:

  • a ui table list of mountpoints, id'ed by themselves



238
239
240
241
242
243
244
# File '../../src/include/nfs_server/routines.rb', line 238

def ExportsItems(exports)
  exports = deep_copy(exports)
  Builtins.maplist(exports) do |entry|
    str = Ops.get_string(entry, "mountpoint", "")
    Item(Id(str), Ops.add(str, " "))
  end
end

- (Object) ExportsRows(exports)

Returns a ui table list of mountpoints and the corresponding bindmount targets, if any.

Parameters:

  • exports (Array<Hash>)

    list of exports

Returns:

  • a ui table list of mountpoints and the corresponding bindmount targets, if any.



290
291
292
293
294
295
296
297
# File '../../src/include/nfs_server/routines.rb', line 290

def ExportsRows(exports)
  exports = deep_copy(exports)
  Builtins.maplist(exports) do |entry|
    exportpath = Ops.get_string(entry, "mountpoint", "")
    bindpaths = getbindpaths(entry)
    Item(Id(exportpath), Ops.add(exportpath, " "), bindpaths)
  end
end

- (Object) ExportsSelBox(exports)

Returns a SelectionBox for the mountpoints, id(exportsbox) containing list of exported directory paths.

Parameters:

  • exports (Array<Hash>)

    list of exports

Returns:

  • a SelectionBox for the mountpoints, id(exportsbox) containing list of exported directory paths.



308
309
310
311
312
313
314
315
316
# File '../../src/include/nfs_server/routines.rb', line 308

def ExportsSelBox(exports)
  return SelectionBox(
    Id(:exportsbox),
    Opt(:notify),
    # selection box label
    _("Dire&ctories"),
    ExportsItems(exports)
  )
end

- (Object) FindAllowed(exports, mp)

Find entry in exports according to the mountpoint

Parameters:

  • exports (Array<Hash>)

    list of exports

  • mp (String)

    mount point

Returns:

  • a list of allowed host specifications or nil if not found



78
79
80
81
82
83
84
85
86
87
88
89
90
# File '../../src/include/nfs_server/routines.rb', line 78

def FindAllowed(exports, mp)
  exports = deep_copy(exports)
  flt = Builtins.filter(exports) do |ent|
    Ops.get_string(ent, "mountpoint", "") == mp
  end
  return nil if flt == nil || Builtins.size(flt) == 0

  Convert.convert(
    Ops.get(flt, [0, "allowed"]),
    :from => "any",
    :to   => "list <string>"
  )
end

- (Object) getbindpaths(entry)

Returns a string that has comma-separated list of bind target paths.

Parameters:

  • clients

    list of “host(opts)” strings

Returns:

  • a string that has comma-separated list of bind target paths.



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
# File '../../src/include/nfs_server/routines.rb', line 249

def getbindpaths(entry)
  entry = deep_copy(entry)
  exportpath = Ops.get_string(entry, "mountpoint", "")
  clients = Convert.convert(
    Ops.get(entry, "allowed") { ["()"] },
    :from => "any",
    :to   => "list <string>"
  )
  bindpaths = ""
  paths = []
  Builtins.foreach(clients) do |hostopts|
    pos = Builtins.findfirstof(hostopts, "(")
    opts = Builtins.substring(hostopts, Ops.add(pos, 1))
    clientexpr = Builtins.substring(hostopts, 0, pos)
    bindpath = ""
    pos = Builtins.findfirstof(opts, ")")
    opts = Builtins.substring(opts, 0, pos) if pos != nil
    if opts == ""
      Builtins.y2error(
        "Your /etc/exports file has errors. The export path %1 has no export options specified.",
        exportpath
      )
    end
    pos = Builtins.search(opts, "bind=")
    if pos != nil
      bindpath = Builtins.substring(opts, Ops.add(pos, 5))
      pos = Builtins.findfirstof(bindpath, ",")
      bindpath = Builtins.substring(bindpath, 0, pos) if pos != nil
    end
    paths = Builtins.prepend(paths, bindpath) if bindpath != ""
  end
  bindpaths = Builtins.mergestring(paths, ",") if Builtins.size(paths) != 0


  bindpaths
end

- (Object) GetDefaultOpts(exports, client)

Give out appropriate default options

Parameters:

  • exports (Array<Hash>)

    list of exports

  • client (String)

    some string representation of the client (*, *.domain, ip address)

Returns:

  • a comma separated default options string, that is most appropriate



151
152
153
# File '../../src/include/nfs_server/routines.rb', line 151

def GetDefaultOpts(exports, client)
  return @default_options
end

- (Object) initialize_nfs_server_routines(include_target)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File '../../src/include/nfs_server/routines.rb', line 23

def initialize_nfs_server_routines(include_target)
  textdomain "nfs_server"
  Yast.import "NfsServer"
  Yast.import "Popup"
  Yast.import "Report"


  # nfs-utils-1.0.1 gives a warning
  # if neither of sync, async is specified.
  #
  # no_subtree_check:
  #  http://nfs.sourceforge.net/#faq_c7
  #  nfs-utils-1.1.0, will switch the default from subtree_check
  #  to no_subtree_check (#233709)
  @default_options = "ro,root_squash,sync,no_subtree_check"
end

- (Object) ReplaceInExports(exports, mountpoint, allowed)

Replaces 'allowed' list in exports (for specified mountpoint)

Parameters:

  • exports (Array<Hash{String => Object>})

    exports list

  • mountpoint (String)

    mount point

  • allowed (Array<String>)

    new allowed host list for that mout point

Returns:

  • modified exports list



456
457
458
459
460
461
462
463
464
465
# File '../../src/include/nfs_server/routines.rb', line 456

def ReplaceInExports(exports, mountpoint, allowed)
  exports = deep_copy(exports)
  allowed = deep_copy(allowed)
  Builtins.maplist(exports) do |entry|
    if Ops.get_string(entry, "mountpoint", "") == mountpoint
      entry = Builtins.add(entry, "allowed", allowed)
    end
    deep_copy(entry)
  end
end