nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0033-Add-friendly-grub2-password-config-tool-985962.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Robert Marshall <rmarshall@redhat.com>
8e15ce
Date: Thu, 25 Jun 2015 11:13:11 -0400
8e15ce
Subject: [PATCH] Add friendly grub2 password config tool (#985962)
8e15ce
8e15ce
Provided a tool for users to reset the grub2 root user password
8e15ce
without having to alter the grub.cfg. The hashed password now
8e15ce
lives in a root-only-readable configuration file.
8e15ce
8e15ce
Resolves: rhbz#985962
8e15ce
8e15ce
Signed-off-by: Robert Marshall <rmarshall@redhat.com>
8e15ce
[pjones: fix the efidir in grub-setpassword and rename tool]
8e15ce
Signed-off-by: Peter Jones <pjones@redhat.com>
8e15ce
[luto: fix grub-setpassword -o's output path]
b35c50
Signed-off-by: Andy Lutomirski <luto@kernel.org>
b35c50
[rharwood: migrate man page to h2m, context]
b35c50
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
8e15ce
---
b35c50
 configure.ac                   |   1 +
b35c50
 Makefile.util.def              |  13 +++++
b35c50
 docs/man/grub-set-password.h2m |   2 +
b35c50
 util/grub-mkconfig.in          |   2 +
b35c50
 util/grub-set-password.in      | 128 +++++++++++++++++++++++++++++++++++++++++
b35c50
 util/grub.d/01_users.in        |  11 ++++
b35c50
 6 files changed, 157 insertions(+)
b35c50
 create mode 100644 docs/man/grub-set-password.h2m
8e15ce
 create mode 100644 util/grub-set-password.in
8e15ce
 create mode 100644 util/grub.d/01_users.in
8e15ce
8e15ce
diff --git a/configure.ac b/configure.ac
b35c50
index 8331f95b64..7f59ad788f 100644
8e15ce
--- a/configure.ac
8e15ce
+++ b/configure.ac
8e15ce
@@ -72,6 +72,7 @@ grub_TRANSFORM([grub-mkrelpath])
8e15ce
 grub_TRANSFORM([grub-mkrescue])
8e15ce
 grub_TRANSFORM([grub-probe])
8e15ce
 grub_TRANSFORM([grub-reboot])
8e15ce
+grub_TRANSFORM([grub-set-password])
8e15ce
 grub_TRANSFORM([grub-script-check])
8e15ce
 grub_TRANSFORM([grub-set-default])
b35c50
 grub_TRANSFORM([grub-sparc64-setup])
8e15ce
diff --git a/Makefile.util.def b/Makefile.util.def
b35c50
index 2c9b283a23..4ee22c5daa 100644
8e15ce
--- a/Makefile.util.def
8e15ce
+++ b/Makefile.util.def
8e15ce
@@ -452,6 +452,12 @@ script = {
8e15ce
   installdir = grubconf;
8e15ce
 };
8e15ce
 
8e15ce
+script = {
8e15ce
+  name = '01_users';
8e15ce
+  common = util/grub.d/01_users.in;
8e15ce
+  installdir = grubconf;
8e15ce
+};
8e15ce
+
8e15ce
 script = {
8e15ce
   name = '10_windows';
8e15ce
   common = util/grub.d/10_windows.in;
b35c50
@@ -724,6 +730,13 @@ script = {
8e15ce
   installdir = sbin;
8e15ce
 };
8e15ce
 
8e15ce
+script = {
8e15ce
+  name = grub-set-password;
8e15ce
+  common = util/grub-set-password.in;
8e15ce
+  mansection = 8;
8e15ce
+  installdir = sbin;
8e15ce
+};
8e15ce
+
8e15ce
 script = {
8e15ce
   name = grub-mkconfig_lib;
8e15ce
   common = util/grub-mkconfig_lib.in;
b35c50
diff --git a/docs/man/grub-set-password.h2m b/docs/man/grub-set-password.h2m
b35c50
new file mode 100644
b35c50
index 0000000000..10ee82f4d5
b35c50
--- /dev/null
b35c50
+++ b/docs/man/grub-set-password.h2m
b35c50
@@ -0,0 +1,2 @@
b35c50
+[NAME]
b35c50
+grub-set-password \- generate the user.cfg file containing the hashed grub bootloader password
8e15ce
diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in
b35c50
index 8ea2315ebc..ba14cf6261 100644
8e15ce
--- a/util/grub-mkconfig.in
8e15ce
+++ b/util/grub-mkconfig.in
8e15ce
@@ -276,6 +276,8 @@ for i in "${grub_mkconfig_dir}"/* ; do
8e15ce
     *~) ;;
8e15ce
     # emacsen autosave files. FIXME: support other editors
8e15ce
     */\#*\#) ;;
8e15ce
+    # rpm config files of yore.
8e15ce
+    *.rpmsave|*.rpmnew|*.rpmorig) ;;
8e15ce
     *)
8e15ce
       if grub_file_is_not_garbage "$i" && test -x "$i" ; then
8e15ce
         echo
8e15ce
diff --git a/util/grub-set-password.in b/util/grub-set-password.in
8e15ce
new file mode 100644
b35c50
index 0000000000..5ebf50576d
8e15ce
--- /dev/null
8e15ce
+++ b/util/grub-set-password.in
8e15ce
@@ -0,0 +1,128 @@
8e15ce
+#!/bin/sh -e
8e15ce
+
8e15ce
+EFIDIR=$(grep ^ID= /etc/os-release | sed -e 's/^ID=//' -e 's/rhel/redhat/')
8e15ce
+if [ -d /sys/firmware/efi/efivars/ ]; then
8e15ce
+    grubdir=`echo "/@bootdirname@/efi/EFI/${EFIDIR}/" | sed 's,//*,/,g'`
8e15ce
+else
8e15ce
+    grubdir=`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`
8e15ce
+fi
8e15ce
+
8e15ce
+PACKAGE_VERSION="@PACKAGE_VERSION@"
8e15ce
+PACKAGE_NAME="@PACKAGE_NAME@"
8e15ce
+self=`basename $0`
8e15ce
+bindir="@bindir@"
8e15ce
+grub_mkpasswd="${bindir}/@grub_mkpasswd_pbkdf2@"
8e15ce
+
8e15ce
+# Usage: usage
8e15ce
+# Print the usage.
8e15ce
+usage () {
8e15ce
+    cat <
8e15ce
+Usage: $0 [OPTION]
8e15ce
+$0 prompts the user to set a password on the grub bootloader. The password
8e15ce
+is written to a file named user.cfg which lives in the GRUB directory
8e15ce
+located by default at ${grubdir}.
8e15ce
+
8e15ce
+  -h, --help                     print this message and exit
8e15ce
+  -v, --version                  print the version information and exit
8e15ce
+  -o, --output_path <DIRECTORY>  put user.cfg in a user-selected directory
8e15ce
+
8e15ce
+Report bugs at https://bugzilla.redhat.com.
8e15ce
+EOF
8e15ce
+}
8e15ce
+
8e15ce
+argument () {
8e15ce
+    opt=$1
8e15ce
+    shift
8e15ce
+
8e15ce
+    if test $# -eq 0; then
8e15ce
+        gettext_printf "%s: option requires an argument -- \`%s'\n" "$self" "$opt" 1>&2
8e15ce
+        exit 1
8e15ce
+    fi
8e15ce
+    echo $1
8e15ce
+}
8e15ce
+
8e15ce
+# Ensure that it's the root user running this script
8e15ce
+if [ "${EUID}" -ne 0 ]; then
8e15ce
+    echo "The grub bootloader password may only be set by root."
8e15ce
+    usage
8e15ce
+    exit 2
8e15ce
+fi
8e15ce
+
8e15ce
+# Check the arguments.
8e15ce
+while test $# -gt 0
8e15ce
+do
8e15ce
+    option=$1
8e15ce
+    shift
8e15ce
+
8e15ce
+    case "$option" in
8e15ce
+    -h | --help)
8e15ce
+	usage
8e15ce
+	exit 0 ;;
8e15ce
+    -v | --version)
8e15ce
+	echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
8e15ce
+	exit 0 ;;
8e15ce
+    -o | --output)
8e15ce
+        OUTPUT_PATH=`argument $option "$@"`; shift ;;
8e15ce
+    --output=*)
8e15ce
+        OUTPUT_PATH=`echo "$option" | sed 's/--output=//'` ;;
8e15ce
+    -o=*)
8e15ce
+        OUTPUT_PATH=`echo "$option" | sed 's/-o=//'` ;;
8e15ce
+    esac
8e15ce
+done
8e15ce
+
8e15ce
+# set user input or default path for user.cfg file
8e15ce
+if [ -z "${OUTPUT_PATH}" ]; then
8e15ce
+    OUTPUT_PATH="${grubdir}"
8e15ce
+fi
8e15ce
+
8e15ce
+if [ ! -d "${OUTPUT_PATH}" ]; then
8e15ce
+    echo "${OUTPUT_PATH} does not exist."
8e15ce
+    usage
8e15ce
+    exit 2;
8e15ce
+fi
8e15ce
+
8e15ce
+ttyopt=$(stty -g)
8e15ce
+fixtty() {
8e15ce
+      stty ${ttyopt}
8e15ce
+}
8e15ce
+
8e15ce
+trap fixtty EXIT
8e15ce
+stty -echo
8e15ce
+
8e15ce
+# prompt & confirm new grub2 root user password
8e15ce
+echo -n "Enter password: "
8e15ce
+read PASSWORD
8e15ce
+echo
8e15ce
+echo -n "Confirm password: "
8e15ce
+read PASSWORD_CONFIRM
8e15ce
+echo
8e15ce
+stty ${ttyopt}
8e15ce
+
8e15ce
+getpass() {
8e15ce
+    local P0
8e15ce
+    local P1
8e15ce
+    P0="$1" && shift
8e15ce
+    P1="$1" && shift
8e15ce
+
8e15ce
+    ( echo ${P0} ; echo ${P1} ) | \
8e15ce
+        LC_ALL=C ${grub_mkpasswd} | \
8e15ce
+        grep -v '[eE]nter password:' | \
8e15ce
+        sed -e "s/PBKDF2 hash of your password is //"
8e15ce
+}
8e15ce
+
8e15ce
+MYPASS="$(getpass "${PASSWORD}" "${PASSWORD_CONFIRM}")"
8e15ce
+if [ -z "${MYPASS}" ]; then
8e15ce
+      echo "${self}: error: empty password" 1>&2
8e15ce
+      exit 1
8e15ce
+fi
8e15ce
+
8e15ce
+# on the ESP, these will fail to set the permissions, but it's okay because
8e15ce
+# the directory is protected.
8e15ce
+install -m 0600 /dev/null "${OUTPUT_PATH}/user.cfg" 2>/dev/null || :
8e15ce
+chmod 0600 "${OUTPUT_PATH}/user.cfg" 2>/dev/null || :
8e15ce
+echo "GRUB2_PASSWORD=${MYPASS}" > "${OUTPUT_PATH}/user.cfg"
8e15ce
+
8e15ce
+if ! grep -q "^### BEGIN /etc/grub.d/01_users ###$" "${OUTPUT_PATH}/grub.cfg"; then
8e15ce
+    echo "WARNING: The current configuration lacks password support!"
8e15ce
+    echo "Update your configuration with @grub_mkconfig@ to support this feature."
8e15ce
+fi
8e15ce
diff --git a/util/grub.d/01_users.in b/util/grub.d/01_users.in
8e15ce
new file mode 100644
b35c50
index 0000000000..db2f44bfb7
8e15ce
--- /dev/null
8e15ce
+++ b/util/grub.d/01_users.in
8e15ce
@@ -0,0 +1,11 @@
8e15ce
+#!/bin/sh -e
8e15ce
+cat << EOF
8e15ce
+if [ -f \${prefix}/user.cfg ]; then
8e15ce
+  source \${prefix}/user.cfg
8e15ce
+  if [ -n "\${GRUB2_PASSWORD}" ]; then
8e15ce
+    set superusers="root"
8e15ce
+    export superusers
8e15ce
+    password_pbkdf2 root \${GRUB2_PASSWORD}
8e15ce
+  fi
8e15ce
+fi
8e15ce
+EOF