Class: Bootloader::Grub2Widget::GrubPasswordWidget

Inherits:
CWM::CustomWidget
  • Object
show all
Includes:
Grub2Helper
Defined in:
src/lib/bootloader/grub2_widgets.rb

Overview

Represents grub password protection widget

Constant Summary collapse

MASKED_PASSWORD =
"**********"

Instance Method Summary collapse

Methods included from Grub2Helper

#grub2, #grub_default, #password, #sections, #stage1

Constructor Details

#initializeGrubPasswordWidget

Returns a new instance of GrubPasswordWidget.



488
489
490
491
492
# File 'src/lib/bootloader/grub2_widgets.rb', line 488

def initialize
  textdomain "bootloader"

  super
end

Instance Method Details

#contentsObject



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'src/lib/bootloader/grub2_widgets.rb', line 496

def contents
  HBox(
    CheckBoxFrame(
      Id(:use_pas),
      _("Prot&ect Boot Loader with Password"),
      true,
      VBox(
        HBox(
          HSpacing(2),
          # TRANSLATORS: checkbox entry
          CheckBox(Id(:unrestricted_pw), _("P&rotect Entry Modification Only")),
          HStretch()
        ),
        HBox(
          HSpacing(2),
          # TRANSLATORS: text entry, please keep it short
          Password(Id(:pw1), Opt(:hstretch), _("&Password for GRUB2 User 'root'")),
          # text entry
          HSpacing(2),
          Password(Id(:pw2), Opt(:hstretch), _("Re&type Password")),
          HStretch()
        )
      )
    )
  )
end

#handle(event) ⇒ Object



556
557
558
559
560
561
562
563
564
565
# File 'src/lib/bootloader/grub2_widgets.rb', line 556

def handle(event)
  return unless event["ID"] == :use_pas

  enabled = Yast::UI.QueryWidget(Id(:use_pas), :Value)
  Yast::UI.ChangeWidget(Id(:unrestricted_pw), :Enabled, enabled)
  Yast::UI.ChangeWidget(Id(:pw1), :Enabled, enabled)
  Yast::UI.ChangeWidget(Id(:pw2), :Enabled, enabled)

  nil
end

#helpObject



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'src/lib/bootloader/grub2_widgets.rb', line 587

def help
  _(
    "<p><b>Protect Boot Loader with Password</b>\n" \
    "at boot time, modifying or even booting any entry will require the" \
    " password. If <b>Protect Entry Modification Only</b> is checked then " \
    "booting any entry is not restricted but modifying entries requires " \
    "the password (which is the way GRUB 1 behaved). As side-effect of " \
    "this option, rd.shell=0 is added to kernel parameters, to prevent " \
    "an unauthorized access to the initrd shell. " \
    "YaST will only accept the password if you repeat it in " \
    "<b>Retype Password</b>. The password applies to the GRUB2 user 'root' " \
    "which is distinct from the Linux 'root'. YaST currently does not support " \
    "other GRUB2 users. If you need them, use a separate GRUB2 script.</p>"
  )
end

#initObject



542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'src/lib/bootloader/grub2_widgets.rb', line 542

def init
  enabled = password.used?
  # read state on disk only if not already set by user (bnc#900026)
  value = (enabled && password.password?) ? MASKED_PASSWORD : ""

  Yast::UI.ChangeWidget(Id(:use_pas), :Value, enabled)
  Yast::UI.ChangeWidget(Id(:pw1), :Enabled, enabled)
  Yast::UI.ChangeWidget(Id(:pw1), :Value, value)
  Yast::UI.ChangeWidget(Id(:pw2), :Enabled, enabled)
  Yast::UI.ChangeWidget(Id(:pw2), :Value, value)
  Yast::UI.ChangeWidget(Id(:unrestricted_pw), :Enabled, enabled)
  Yast::UI.ChangeWidget(Id(:unrestricted_pw), :Value, password.unrestricted?)
end

#storeObject



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'src/lib/bootloader/grub2_widgets.rb', line 567

def store
  usepass = Yast::UI.QueryWidget(Id(:use_pas), :Value)
  matcher = CFA::Matcher.new(key: "rd.shell")
  grub_default.kernel_params.remove_parameter(matcher)
  if !usepass
    password.used = false
    return
  end

  password.used = true

  value = Yast::UI.QueryWidget(Id(:pw1), :Value)
  # special value as we do not know password, so it mean user do not change it
  password.password = value if value != MASKED_PASSWORD

  value = Yast::UI.QueryWidget(Id(:unrestricted_pw), :Value)
  grub_default.kernel_params.add_parameter("rd.shell", "0") if value
  password.unrestricted = value
end

#validateObject



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'src/lib/bootloader/grub2_widgets.rb', line 523

def validate
  return true unless Yast::UI.QueryWidget(Id(:use_pas), :Value)

  if Yast::UI.QueryWidget(Id(:pw1), :Value) == ""
    Yast::Report.Error(_("The password must not be empty."))
    Yast::UI.SetFocus(Id(:pw1))
    return false
  end
  if Yast::UI.QueryWidget(Id(:pw1), :Value) == Yast::UI.QueryWidget(Id(:pw2), :Value)
    return true
  end

  Yast::Report.Error(_(
                       "'Password' and 'Retype password'\ndo not match. Retype the password."
                     ))
  Yast::UI.SetFocus(Id(:pw1))
  false
end