Class List Method List File List

# Class: Yast::SudoClass

Inherits:
  Module
  - Object
  - Module
  - Yast::SudoClass
  show all
Defined in:
  ../../src/modules/Sudo.rb

## Instance Method Summary (collapse)

- - (Object) Abort

  end Commands.

- - (Object) GetAliasNames(what)
- - (Object) GetCmndAliases2

  Commands.

- - (Object) GetHostAliases2

  Hosts.

- - (Object) GetItem
- - (Object) GetModified

  Data was modified?.

- - (Object) GetRule(i)
- - (Object) GetRules
- - (Object) GetRunAsAliases2

  RunAs.

- - (Object) GetUserAliases2

  Users.

- - (Object) main
- - (Object) PollAbort

  - Checks whether an Abort button has been pressed.

- - (Object) Read

  Read all sudo settings.

- - (Object) ReadLocalUsers
- - (Object) ReadSudoSettings2
- - (Object) RemoveCmndAlias2(i)
- - (Object) RemoveHostAlias2(i)
- - (Object) RemoveRule(i)
- - (Object) RemoveRunAsAlias2(i)
- - (Object) RemoveUserAlias2(i)
- - (Object) SearchAlias2(name, aliases)
- - (Object) SearchRules(what, key)
- - (Object) SetCmndAlias(i, _alias)
- - (Object) SetHostAlias(i, _alias)
- - (Object) SetItem(i)
- - (Object) SetModified
- - (Object) SetRule(i, spec)
- - (Object) SetRunAsAlias(i, _alias)
- - (Object) SetUserAlias(i, _alias)
- - (Object) SystemRulePopup(m, delete)
- - (Object) Write

  Write all sudo settings.

- - (Object) WriteSudoSettings2

## Instance Method Details

### - (`Object`) Abort

end Commands

    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    # File '../../src/modules/Sudo.rb', line 542

    def Abort
      if GetModified()
        return Popup.YesNo(
          _(
            "All changes will be lost. Really quit sudo configuration without saving?"
          )
        )
      else
        return true
      end
    end

### - (`Object`) GetAliasNames(what)

    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    # File '../../src/modules/Sudo.rb', line 427

    def GetAliasNames(what)
      ret = []
      case what
        when "user"
          ret = Builtins.maplist(@user_aliases2) do |m|
            Ops.get_string(m, "name", "")
          end
        when "run_as"
          ret = Builtins.maplist(@runas_aliases2) do |m|
            Ops.get_string(m, "name", "")
          end
        when "host"
          ret = Builtins.maplist(@host_aliases2) do |m|
            Ops.get_string(m, "name", "")
          end
        when "command"
          ret = Builtins.maplist(@cmnd_aliases2) do |m|
            Ops.get_string(m, "name", "")
          end
        else
          return []
      end
      deep_copy(ret)
    end

### - (`Object`) GetCmndAliases2

Commands

    523
    524
    525
    # File '../../src/modules/Sudo.rb', line 523

    def GetCmndAliases2
      deep_copy(@cmnd_aliases2)
    end

### - (`Object`) GetHostAliases2

Hosts

    484
    485
    486
    # File '../../src/modules/Sudo.rb', line 484

    def GetHostAliases2
      deep_copy(@host_aliases2)
    end

### - (`Object`) GetItem

    348
    349
    350
    # File '../../src/modules/Sudo.rb', line 348

    def GetItem
      @citem
    end

### - (`Object`) GetModified

Data was modified?

Returns:

- true if modified

    74
    75
    76
    # File '../../src/modules/Sudo.rb', line 74

    def GetModified
      @modified
    end

### - (`Object`) GetRule(i)

    362
    363
    364
    # File '../../src/modules/Sudo.rb', line 362

    def GetRule(i)
      Ops.get(@rules, i, {})
    end

### - (`Object`) GetRules

    352
    353
    354
    # File '../../src/modules/Sudo.rb', line 352

    def GetRules
      deep_copy(@rules)
    end

### - (`Object`) GetRunAsAliases2

RunAs

    503
    504
    505
    # File '../../src/modules/Sudo.rb', line 503

    def GetRunAsAliases2
      deep_copy(@runas_aliases2)
    end

### - (`Object`) GetUserAliases2

Users

    464
    465
    466
    # File '../../src/modules/Sudo.rb', line 464

    def GetUserAliases2
      deep_copy(@user_aliases2)
    end

### - (`Object`) main

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

    def main
      Yast.import "UI"
      textdomain "sudo"

      Yast.import "Confirm"
      Yast.import "Map"
      Yast.import "Message"
      Yast.import "Progress"
      Yast.import "Report"
      Yast.import "Popup"
      Yast.import "Service"
      Yast.import "String"
      Yast.import "Summary"
      Yast.import "UsersPasswd"

      # Data was modified?
      @modified = false

      @sl = 10

      @ValidCharsUsername = Ops.add(
        Builtins.deletechars(String.CGraph, "'\""),
        " "
      )

      @settings2 = []
      @host_aliases2 = []
      @user_aliases2 = []
      @cmnd_aliases2 = []
      @runas_aliases2 = []
      @defaults = []
      @rules = []
      @all_users = []

      @citem = -1
    end

### - (`Object`) PollAbort

- Checks whether an Abort button has been pressed.

- If so, calls function to confirm the abort call. :*

- @return boolean true if abort confirmed

    558
    559
    560
    561
    562
    # File '../../src/modules/Sudo.rb', line 558

    def PollAbort
      return Abort() if UI.PollInput == :abort

      false
    end

### - (`Object`) Read

Read all sudo settings

Returns:

- true on success

    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    # File '../../src/modules/Sudo.rb', line 566

    def Read
      return false if !Confirm.MustBeRoot

      Report.Error(Message.CannotReadCurrentSettings) if !ReadSudoSettings2()

      # Error message
      if !ReadLocalUsers()
        Report.Error(_("An error occurred while reading users and groups."))
      end

      # Sudo read dialog caption
      # string caption = _("Initializing sudo Configuration");
      #
      # integer steps = 2;
      #
      # Progress::New( caption, " ", steps, [
      # 	    /* Progress stage 1/2
      # 	    _("Read sudo settings"),
      # 	    /* Progress stage 2/2
      # 	    _("Read local users and groups")
      # 	], [
      # 	    /* Progress step 1/2
      # 	    _("Reading sudo settings..."),
      # 	    /* Progress step 2/2
      # 	    _("Reading local users and groups..."),
      # 	    /* Progress finished
      # 	    Message::Finished()
      # 	],
      # 	""
      # );

      @modified = false
      true
    end

### - (`Object`) ReadLocalUsers

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

    def ReadLocalUsers
      return false if !UsersPasswd.Read({})

      users = Convert.convert(
        Builtins.merge(
          Map.Keys(UsersPasswd.GetUsers("local")),
          Map.Keys(UsersPasswd.GetUsers("system"))
        ),
        :from => "list",
        :to   => "list <string>"
      )

      available_groups = Convert.convert(
        Builtins.merge(
          Map.Keys(UsersPasswd.GetGroups("local")),
          Map.Keys(UsersPasswd.GetGroups("system"))
        ),
        :from => "list",
        :to   => "list <string>"
      )

      available_groups = Builtins.maplist(available_groups) do |group|
        Ops.add("%", group)
      end

      @all_users = Convert.convert(
        Builtins.merge(users, available_groups),
        :from => "list",
        :to   => "list <string>"
      )
      Builtins.y2milestone(
        "Read the following users and groups: %1",
        @all_users
      )

      true
    end

### - (`Object`) ReadSudoSettings2

    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
    230
    231
    232
    # File '../../src/modules/Sudo.rb', line 84

    def ReadSudoSettings2
      @settings2 = Convert.convert(
        SCR.Read(path(".sudo")),
        :from => "any",
        :to   => "list <list>"
      )
      Builtins.y2milestone("Sudo settings %1", @settings2)

      Builtins.foreach(@settings2) do |line|
        type = Ops.get_string(line, 1, "")
        case type
          when "Host_Alias"
            lst = Builtins.splitstring(
              Builtins.deletechars(Ops.get_string(line, 3, ""), " \t"),
              ","
            )
            @host_aliases2 = Builtins.add(
              @host_aliases2,
              {
                "c"    => Ops.get_string(line, 0, ""),
                "name" => Ops.get_string(line, 2, ""),
                "mem"  => lst
              }
            )
          when "User_Alias"
            lst = Builtins.splitstring(
              Builtins.deletechars(Ops.get_string(line, 3, ""), " \t"),
              ","
            )
            @user_aliases2 = Builtins.add(
              @user_aliases2,
              {
                "c"    => Ops.get_string(line, 0, ""),
                "name" => Ops.get_string(line, 2, ""),
                "mem"  => lst
              }
            )
          when "Cmnd_Alias"
            lst = Builtins.maplist(
              Builtins.splitstring(Ops.get_string(line, 3, ""), ",")
            ) do |s|
              Builtins.substring(s, Builtins.findfirstnotof(s, " \t"))
            end
            @cmnd_aliases2 = Builtins.add(
              @cmnd_aliases2,
              {
                "c"    => Ops.get_string(line, 0, ""),
                "name" => Ops.get_string(line, 2, ""),
                "mem"  => lst
              }
            )
          when "Runas_Alias"
            lst = Builtins.splitstring(
              Builtins.deletechars(Ops.get_string(line, 3, ""), " \t"),
              ","
            )
            @runas_aliases2 = Builtins.add(
              @runas_aliases2,
              {
                "c"    => Ops.get_string(line, 0, ""),
                "name" => Ops.get_string(line, 2, ""),
                "mem"  => lst
              }
            )
          else
            if Builtins.regexpmatch(type, "^Defaults.*$")
              #do nothing, keep defaults untouched
              @defaults = Builtins.add(@defaults, line)
            else
              m = {}
              cmd = []

              if Builtins.regexpmatch(type, "^.*\\\\.*$")
                type = Builtins.regexpsub(type, "^(.*)\\\\(.*)$", "\\1\\2")
              end
              Ops.set(m, "user", type)

              Ops.set(m, "host", Ops.get_string(line, 2, ""))

              #match "(.*)"
              if Builtins.regexpmatch(Ops.get_string(line, 3, ""), "\\(.*\\)")
                Ops.set(
                  m,
                  "run_as",
                  Builtins.regexpsub(
                    Ops.get_string(line, 3, ""),
                    "(\\(.*\\))",
                    "\\1"
                  )
                )
              end
              if Builtins.regexpmatch(
                  Ops.get_string(line, 3, ""),
                  "NOPASSWD:|SETENV:|NOEXEC:"
                )
                Ops.set(
                  m,
                  "tag",
                  Builtins.regexpsub(
                    Ops.get_string(line, 3, ""),
                    "(NOPASSWD:|SETENV:|NOEXEC:)",
                    "\\1"
                  )
                )
              end

              # if(issubstring(line[3]:"","NOPASSWD:")) {
              # 	m["no_passwd"] = (boolean) true;
              # }
              # else {
              #      m["no_passwd"] = (boolean) false;
              # }

              cmd = Builtins.splitstring(Ops.get_string(line, 3, ""), "):")
              Ops.set(
                m,
                "commands",
                Builtins.splitstring(
                  Ops.get(cmd, Ops.subtract(Builtins.size(cmd), 1), ""),
                  ","
                )
              )
              Ops.set(
                m,
                "commands",
                Builtins.maplist(Ops.get_list(m, "commands", [])) do |s|
                  Builtins.substring(s, Builtins.findfirstnotof(s, " \t"))
                end
              )
              Ops.set(m, "comment", Ops.get_string(line, 0, ""))

              @rules = Builtins.add(@rules, m)
            end
        end
      end

      Builtins.y2milestone(
        "user %1 host %2 runas %3 cmnd %4 def %5 rules %6",
        @user_aliases2,
        @host_aliases2,
        @runas_aliases2,
        @cmnd_aliases2,
        @defaults,
        @rules
      )

      true
    end

### - (`Object`) RemoveCmndAlias2(i)

    534
    535
    536
    537
    538
    # File '../../src/modules/Sudo.rb', line 534

    def RemoveCmndAlias2(i)
      @cmnd_aliases2 = Builtins.remove(@cmnd_aliases2, i)

      nil
    end

### - (`Object`) RemoveHostAlias2(i)

    488
    489
    490
    491
    492
    # File '../../src/modules/Sudo.rb', line 488

    def RemoveHostAlias2(i)
      @host_aliases2 = Builtins.remove(@host_aliases2, i)

      nil
    end

### - (`Object`) RemoveRule(i)

    356
    357
    358
    359
    360
    # File '../../src/modules/Sudo.rb', line 356

    def RemoveRule(i)
      @rules = Builtins.remove(@rules, i)

      nil
    end

### - (`Object`) RemoveRunAsAlias2(i)

    507
    508
    509
    510
    511
    # File '../../src/modules/Sudo.rb', line 507

    def RemoveRunAsAlias2(i)
      @runas_aliases2 = Builtins.remove(@runas_aliases2, i)

      nil
    end

### - (`Object`) RemoveUserAlias2(i)

    475
    476
    477
    478
    479
    # File '../../src/modules/Sudo.rb', line 475

    def RemoveUserAlias2(i)
      @user_aliases2 = Builtins.remove(@user_aliases2, i)

      nil
    end

### - (`Object`) SearchAlias2(name, aliases)

    452
    453
    454
    455
    456
    457
    458
    459
    460
    # File '../../src/modules/Sudo.rb', line 452

    def SearchAlias2(name, aliases)
      aliases = deep_copy(aliases)
      tmp = []
      tmp = Builtins.filter(aliases) do |m|
        Ops.get_string(m, "name", "") == name
      end

      Ops.greater_than(Builtins.size(tmp), 0)
    end

### - (`Object`) SearchRules(what, key)

    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    # File '../../src/modules/Sudo.rb', line 373

    def SearchRules(what, key)
      found = false

      Builtins.foreach(@rules) do |m|
        if Builtins.haskey(m, what)
          if what == "commands"
            if Builtins.contains(Ops.get_list(m, what, []), key)
              found = true
              raise Break
            end
          else
            if Ops.get_string(m, what, "") == key
              found = true
              raise Break
            end
          end
        end
      end

      found
    end

### - (`Object`) SetCmndAlias(i, _alias)

    527
    528
    529
    530
    531
    532
    # File '../../src/modules/Sudo.rb', line 527

    def SetCmndAlias(i, _alias)
      _alias = deep_copy(_alias)
      Ops.set(@cmnd_aliases2, i, _alias)

      nil
    end

### - (`Object`) SetHostAlias(i, _alias)

    494
    495
    496
    497
    498
    499
    # File '../../src/modules/Sudo.rb', line 494

    def SetHostAlias(i, _alias)
      _alias = deep_copy(_alias)
      Ops.set(@host_aliases2, i, _alias)

      nil
    end

### - (`Object`) SetItem(i)

    341
    342
    343
    344
    345
    346
    # File '../../src/modules/Sudo.rb', line 341

    def SetItem(i)
      Builtins.y2internal("Current item %1", i)
      @citem = i

      nil
    end

### - (`Object`) SetModified

    78
    79
    80
    81
    82
    # File '../../src/modules/Sudo.rb', line 78

    def SetModified
      @modified = true

      nil
    end

### - (`Object`) SetRule(i, spec)

    366
    367
    368
    369
    370
    371
    # File '../../src/modules/Sudo.rb', line 366

    def SetRule(i, spec)
      spec = deep_copy(spec)
      Ops.set(@rules, i, spec)

      nil
    end

### - (`Object`) SetRunAsAlias(i, _alias)

    513
    514
    515
    516
    517
    518
    # File '../../src/modules/Sudo.rb', line 513

    def SetRunAsAlias(i, _alias)
      _alias = deep_copy(_alias)
      Ops.set(@runas_aliases2, i, _alias)

      nil
    end

### - (`Object`) SetUserAlias(i, _alias)

    468
    469
    470
    471
    472
    473
    # File '../../src/modules/Sudo.rb', line 468

    def SetUserAlias(i, _alias)
      _alias = deep_copy(_alias)
      Ops.set(@user_aliases2, i, _alias)

      nil
    end

### - (`Object`) SystemRulePopup(m, delete)

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

    def SystemRulePopup(m, delete)
      m = deep_copy(m)
      if Ops.get_string(m, "user", "") == "ALL" &&
          Ops.get_string(m, "host", "") == "ALL" &&
          Ops.get_string(m, "run_as", "") == "(ALL)"
        text = _(
          "This rule is a system rule necessary for correct functionality of sudo.\n"
        )

        if delete
          text = Ops.add(
            text,
            _(
              "After deleting it, some applications may no longer work.\nReally delete?"
            )
          )
        else
          text = Ops.add(
            text,
            _(
              "If you change it, some applications may no longer work.\nReally edit? "
            )
          )
        end

        return Popup.YesNo(text)
      else
        return true
      end
    end

### - (`Object`) Write

Write all sudo settings

Returns:

- true on success

    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    # File '../../src/modules/Sudo.rb', line 603

    def Write
      # Sudo read dialog caption
      caption = _("Saving sudo Configuration")

      steps = 1

      sl = 500

      ret = true

      # We do not set help text here, because it was set outside
      Progress.New(
        caption,
        " ",
        steps,
        [
          # Progress stage 1/1
          _("Write the settings")
        ],
        [
          # Progress step 1/1
          _("Writing the settings..."),
          # Progress finished
          Message.Finished
        ],
        ""
      )

      Builtins.sleep(sl)

      # write settings
      return false if PollAbort()
      Progress.NextStage
      # Error message
      if !WriteSudoSettings2()
        Report.Error(_("Cannot write settings."))
        ret = false
      end
      Builtins.sleep(sl)

      Progress.NextStage
      Builtins.sleep(sl)

      ret
    end

### - (`Object`) WriteSudoSettings2

    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    # File '../../src/modules/Sudo.rb', line 273

    def WriteSudoSettings2
      set = []

      Builtins.foreach(@host_aliases2) do |a|
        line = [
          Ops.get_string(a, "c", ""),
          "Host_Alias",
          Ops.get_string(a, "name", ""),
          Builtins.mergestring(Ops.get_list(a, "mem", []), ",")
        ]
        set = Builtins.add(set, line)
      end
      Builtins.foreach(@user_aliases2) do |a|
        line = [
          Ops.get_string(a, "c", ""),
          "User_Alias",
          Ops.get_string(a, "name", ""),
          Builtins.mergestring(Ops.get_list(a, "mem", []), ",")
        ]
        set = Builtins.add(set, line)
      end
      Builtins.foreach(@cmnd_aliases2) do |a|
        line = [
          Ops.get_string(a, "c", ""),
          "Cmnd_Alias",
          Ops.get_string(a, "name", ""),
          Builtins.mergestring(Ops.get_list(a, "mem", []), ",")
        ]
        set = Builtins.add(set, line)
      end

      set = Convert.convert(
        Builtins.merge(set, @defaults),
        :from => "list",
        :to   => "list <list>"
      )

      Builtins.foreach(@runas_aliases2) do |a|
        line = [
          Ops.get_string(a, "c", ""),
          "Runas_Alias",
          Ops.get_string(a, "name", ""),
          Builtins.mergestring(Ops.get_list(a, "mem", []), ",")
        ]
        set = Builtins.add(set, line)
      end

      Builtins.foreach(@rules) do |m|
        user = Ops.get_string(m, "user", "")
        user = Builtins.mergestring(Builtins.splitstring(user, "\\"), "\\\\")
        host = Ops.get_string(m, "host", "")
        comment = Ops.get_string(m, "comment", "")
        rest = Ops.add(
          Ops.add(
            Ops.add(Ops.get_string(m, "run_as", ""), " "),
            Ops.get_string(m, "tag", "")
          ),
          Builtins.mergestring(Ops.get_list(m, "commands", []), ",")
        )
        set = Builtins.add(set, [comment, user, host, rest])
      end

      Builtins.y2milestone("Sudo settings %1", set)

      return SCR.Write(path(".sudo"), nil) if SCR.Write(path(".sudo"), set)
      true
    end