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

d41074
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
a85e8e
From: Robert Marshall <rmarshall@redhat.com>
a85e8e
Date: Thu, 25 Jun 2015 11:13:11 -0400
d41074
Subject: [PATCH] Add friendly grub2 password config tool (#985962)
a85e8e
a85e8e
Provided a tool for users to reset the grub2 root user password
a85e8e
without having to alter the grub.cfg. The hashed password now
a85e8e
lives in a root-only-readable configuration file.
a85e8e
a85e8e
Resolves: rhbz#985962
a85e8e
---
a85e8e
 configure.ac             |   1 +
d41074
 Makefile.util.def        |  13 +++++
d41074
 .gitignore               |  12 ++---
a85e8e
 util/grub-mkconfig.in    |   2 +
a85e8e
 util/grub-setpassword.8  |  28 +++++++++++
a85e8e
 util/grub-setpassword.in | 123 +++++++++++++++++++++++++++++++++++++++++++++++
a85e8e
 util/grub.d/01_users.in  |  11 +++++
a85e8e
 7 files changed, 182 insertions(+), 8 deletions(-)
a85e8e
 create mode 100644 util/grub-setpassword.8
a85e8e
 create mode 100755 util/grub-setpassword.in
a85e8e
 create mode 100644 util/grub.d/01_users.in
a85e8e
d41074
diff --git a/configure.ac b/configure.ac
d41074
index de17e65c4b7..273da6cb906 100644
d41074
--- a/configure.ac
d41074
+++ b/configure.ac
d41074
@@ -65,6 +65,7 @@ grub_TRANSFORM([grub-mkrelpath])
d41074
 grub_TRANSFORM([grub-mkrescue])
d41074
 grub_TRANSFORM([grub-probe])
d41074
 grub_TRANSFORM([grub-reboot])
d41074
+grub_TRANSFORM([grub-setpassword])
d41074
 grub_TRANSFORM([grub-rpm-sort])
d41074
 grub_TRANSFORM([grub-script-check])
d41074
 grub_TRANSFORM([grub-set-default])
a85e8e
diff --git a/Makefile.util.def b/Makefile.util.def
d41074
index 3ac75720466..e2821a2f3a5 100644
a85e8e
--- a/Makefile.util.def
a85e8e
+++ b/Makefile.util.def
d41074
@@ -437,6 +437,12 @@ script = {
d41074
   installdir = grubconf;
a85e8e
 };
a85e8e
 
d41074
+script = {
a85e8e
+  name = '01_users';
a85e8e
+  common = util/grub.d/01_users.in;
a85e8e
+  installdir = grubconf;
a85e8e
+};
a85e8e
+
d41074
 script = {
a85e8e
   name = '10_windows';
a85e8e
   common = util/grub.d/10_windows.in;
d41074
@@ -712,6 +718,13 @@ script = {
d41074
   installdir = sbin;
a85e8e
 };
a85e8e
 
d41074
+script = {
a85e8e
+  name = grub-setpassword;
a85e8e
+  common = util/grub-setpassword.in;
a85e8e
+  mansection = 8;
a85e8e
+  installdir = sbin;
a85e8e
+};
a85e8e
+
d41074
 script = {
a85e8e
   name = grub-mkconfig_lib;
a85e8e
   common = util/grub-mkconfig_lib.in;
d41074
diff --git a/.gitignore b/.gitignore
d41074
index b6c45776a4d..e49f76b114a 100644
d41074
--- a/.gitignore
d41074
+++ b/.gitignore
d41074
@@ -1,11 +1,7 @@
d41074
-00_header
d41074
-10_*
d41074
-20_linux_xen
d41074
-30_os-prober
d41074
-40_custom
d41074
-41_custom
d41074
-*.1
d41074
-*.8
d41074
+[[:digit:]][[:digit:]]_*
d41074
+!util/[[:digit:]][[:digit:]]_*.in
d41074
+*.[[:digit:]]
d41074
+!util/*.[[:digit:]]
d41074
 aclocal.m4
d41074
 ahci_test
d41074
 ascii.bitmaps
a85e8e
diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in
d41074
index bc26e7c109e..e029d0d5ea9 100644
a85e8e
--- a/util/grub-mkconfig.in
a85e8e
+++ b/util/grub-mkconfig.in
a85e8e
@@ -260,6 +260,8 @@ for i in "${grub_mkconfig_dir}"/* ; do
a85e8e
     *~) ;;
a85e8e
     # emacsen autosave files. FIXME: support other editors
a85e8e
     */\#*\#) ;;
a85e8e
+    # rpm config files of yore.
a85e8e
+    *.rpmsave|*.rpmnew|*.rpmorig) ;;
a85e8e
     *)
a85e8e
       if grub_file_is_not_garbage "$i" && test -x "$i" ; then
a85e8e
         echo
a85e8e
diff --git a/util/grub-setpassword.8 b/util/grub-setpassword.8
a85e8e
new file mode 100644
d41074
index 00000000000..5973abef4ab
a85e8e
--- /dev/null
a85e8e
+++ b/util/grub-setpassword.8
a85e8e
@@ -0,0 +1,28 @@
a85e8e
+.TH GRUB-SETPASSWORD 3 "Thu Jun 25 2015"
a85e8e
+.SH NAME
a85e8e
+\fBgrub-setpassword\fR \(em Generate the user.cfg file containing the hashed grub bootloader password.
a85e8e
+
a85e8e
+.SH SYNOPSIS
a85e8e
+\fBgrub-setpassword\fR [OPTION]
a85e8e
+
a85e8e
+.SH DESCRIPTION
a85e8e
+\fBgrub-setpassword\fR outputs the user.cfg file which contains the hashed GRUB bootloader password. This utility only supports configurations where there is a single root user.
a85e8e
+
a85e8e
+The file has the format:
a85e8e
+GRUB_2PASSWORD=<\fIhashed password\fR>.
a85e8e
+
a85e8e
+.SH OPTIONS
a85e8e
+.TP
a85e8e
+-h, --help
a85e8e
+Display program usage and exit.
a85e8e
+.TP
a85e8e
+-v, --version
a85e8e
+Display the current version.
a85e8e
+.TP
a85e8e
+-o, --output[=\fIDIRECTORY PATH\fR]
a85e8e
+Choose the file path to which user.cfg will be written.
a85e8e
+
a85e8e
+.SH SEE ALSO
a85e8e
+.BR "info grub"
a85e8e
+
a85e8e
+.BR "info grub2-mkpasswd-pbkdf2"
a85e8e
diff --git a/util/grub-setpassword.in b/util/grub-setpassword.in
a85e8e
new file mode 100755
d41074
index 00000000000..dd76f00fc0e
a85e8e
--- /dev/null
a85e8e
+++ b/util/grub-setpassword.in
a85e8e
@@ -0,0 +1,123 @@
a85e8e
+#!/bin/sh -e
a85e8e
+
a85e8e
+if [ -d /sys/firmware/efi/efivars/ ]; then
a85e8e
+    grubdir=`echo "/@bootdirname@/efi/EFI/redhat/" | sed 's,//*,/,g'`
a85e8e
+else
a85e8e
+    grubdir=`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`
a85e8e
+fi
a85e8e
+
a85e8e
+PACKAGE_VERSION="@PACKAGE_VERSION@"
a85e8e
+PACKAGE_NAME="@PACKAGE_NAME@"
a85e8e
+self=`basename $0`
a85e8e
+bindir="@bindir@"
a85e8e
+grub_mkpasswd="${bindir}/@grub_mkpasswd_pbkdf2@"
a85e8e
+
a85e8e
+# Usage: usage
a85e8e
+# Print the usage.
a85e8e
+usage () {
a85e8e
+    cat <
a85e8e
+Usage: $0 [OPTION] [SOURCE]
a85e8e
+Run GRUB script in a Qemu instance.
a85e8e
+
a85e8e
+  -h, --help              print this message and exit
a85e8e
+  -v, --version           print the version information and exit
a85e8e
+  -o, --output_path       choose a custom output path for user.cfg
a85e8e
+
a85e8e
+$0 prompts the user to set a password on the grub bootloader. The password
a85e8e
+is written to a file named user.cfg.
a85e8e
+
a85e8e
+Report bugs at https://bugzilla.redhat.com.
a85e8e
+EOF
a85e8e
+}
a85e8e
+
a85e8e
+argument () {
a85e8e
+    opt=$1
a85e8e
+    shift
a85e8e
+
a85e8e
+    if test $# -eq 0; then
a85e8e
+        gettext_printf "%s: option requires an argument -- \`%s'\n" "$self" "$opt" 1>&2
a85e8e
+        exit 1
a85e8e
+    fi
a85e8e
+    echo $1
a85e8e
+}
a85e8e
+
a85e8e
+# Ensure that it's the root user running this script
a85e8e
+if [ "${EUID}" -ne 0 ]; then
a85e8e
+    echo "The grub bootloader password may only be set by root."
a85e8e
+    usage
a85e8e
+    exit 2
a85e8e
+fi
a85e8e
+
a85e8e
+# Check the arguments.
a85e8e
+while test $# -gt 0
a85e8e
+do
a85e8e
+    option=$1
a85e8e
+    shift
a85e8e
+
a85e8e
+    case "$option" in
a85e8e
+    -h | --help)
a85e8e
+	usage
a85e8e
+	exit 0 ;;
a85e8e
+    -v | --version)
a85e8e
+	echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
a85e8e
+	exit 0 ;;
a85e8e
+    -o | --output)
a85e8e
+        OUTPUT_PATH=`argument $option "$@"`; shift ;;
a85e8e
+    --output=*)
a85e8e
+        OUTPUT_PATH=`echo "$option" | sed 's/--output=//'` ;;
a85e8e
+    -o=*)
a85e8e
+        OUTPUT_PATH=`echo "$option" | sed 's/-o=//'` ;;
a85e8e
+    esac
a85e8e
+done
a85e8e
+
a85e8e
+# set user input or default path for user.cfg file
a85e8e
+if [ -z "${OUTPUT_PATH}" ]; then
a85e8e
+    OUTPUT_PATH="${grubdir}"
a85e8e
+fi
a85e8e
+
a85e8e
+if [ ! -d "${OUTPUT_PATH}" ]; then
a85e8e
+    echo "${OUTPUT_PATH} does not exist."
a85e8e
+    usage
a85e8e
+    exit 2;
a85e8e
+fi
a85e8e
+
a85e8e
+ttyopt=$(stty -g)
a85e8e
+fixtty() {
a85e8e
+      stty ${ttyopt}
a85e8e
+}
a85e8e
+
a85e8e
+trap fixtty EXIT
a85e8e
+stty -echo
a85e8e
+
a85e8e
+# prompt & confirm new grub2 root user password
a85e8e
+echo -n "Enter password: "
a85e8e
+read PASSWORD
a85e8e
+echo
a85e8e
+echo -n "Confirm password: "
a85e8e
+read PASSWORD_CONFIRM
a85e8e
+echo
a85e8e
+stty ${ttyopt}
a85e8e
+
a85e8e
+getpass() {
a85e8e
+    local P0
a85e8e
+    local P1
a85e8e
+    P0="$1" && shift
a85e8e
+    P1="$1" && shift
a85e8e
+
a85e8e
+    ( echo ${P0} ; echo ${P1} ) | \
a85e8e
+        ${grub_mkpasswd} | \
a85e8e
+        grep -v '[eE]nter password:' | \
a85e8e
+        sed -e "s/PBKDF2 hash of your password is //"
a85e8e
+}
a85e8e
+
a85e8e
+MYPASS="$(getpass "${PASSWORD}" "${PASSWORD_CONFIRM}")"
a85e8e
+if [ -z "${MYPASS}" ]; then
a85e8e
+      echo "${self}: error: empty password" 1>&2
a85e8e
+      exit 1
a85e8e
+fi
a85e8e
+
a85e8e
+# on the ESP, these will fail to set the permissions, but it's okay because
a85e8e
+# the directory is protected.
a85e8e
+install -m 0600 /dev/null "${grubdir}/user.cfg" 2>/dev/null || :
a85e8e
+chmod 0600 "${grubdir}/user.cfg" 2>/dev/null || :
a85e8e
+echo "GRUB2_PASSWORD=${MYPASS}" > "${grubdir}/user.cfg"
a85e8e
diff --git a/util/grub.d/01_users.in b/util/grub.d/01_users.in
a85e8e
new file mode 100644
d41074
index 00000000000..facd409e722
a85e8e
--- /dev/null
a85e8e
+++ b/util/grub.d/01_users.in
a85e8e
@@ -0,0 +1,11 @@
a85e8e
+#!/bin/sh -e
a85e8e
+cat << EOF
a85e8e
+if [ -f \${prefix}/user.cfg ]; then
a85e8e
+  source \${prefix}/user.cfg
a85e8e
+  if [ -n \${GRUB2_PASSWORD} ]; then
a85e8e
+    set superusers="root"
a85e8e
+    export superusers
a85e8e
+    password_pbkdf2 root \${GRUB2_PASSWORD}
a85e8e
+  fi
a85e8e
+fi
a85e8e
+EOF