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

6b3c76
From bde7036b706857aff32a025e82da5bc32473e439 Mon Sep 17 00:00:00 2001
a85e8e
From: Robert Marshall <rmarshall@redhat.com>
a85e8e
Date: Thu, 25 Jun 2015 11:13:11 -0400
6b3c76
Subject: [PATCH 178/261] 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
---
d41074
 .gitignore               |  12 ++---
6b3c76
 Makefile.util.def        |  13 +++++
6b3c76
 configure.ac             |   1 +
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
6b3c76
diff --git a/.gitignore b/.gitignore
6b3c76
index b6c45776a..e49f76b11 100644
6b3c76
--- a/.gitignore
6b3c76
+++ b/.gitignore
6b3c76
@@ -1,11 +1,7 @@
6b3c76
-00_header
6b3c76
-10_*
6b3c76
-20_linux_xen
6b3c76
-30_os-prober
6b3c76
-40_custom
6b3c76
-41_custom
6b3c76
-*.1
6b3c76
-*.8
6b3c76
+[[:digit:]][[:digit:]]_*
6b3c76
+!util/[[:digit:]][[:digit:]]_*.in
6b3c76
+*.[[:digit:]]
6b3c76
+!util/*.[[:digit:]]
6b3c76
 aclocal.m4
6b3c76
 ahci_test
6b3c76
 ascii.bitmaps
a85e8e
diff --git a/Makefile.util.def b/Makefile.util.def
6b3c76
index 3ac757204..e2821a2f3 100644
a85e8e
--- a/Makefile.util.def
a85e8e
+++ b/Makefile.util.def
6b3c76
@@ -438,6 +438,12 @@ script = {
a85e8e
 };
a85e8e
 
6b3c76
 script = {
a85e8e
+  name = '01_users';
a85e8e
+  common = util/grub.d/01_users.in;
a85e8e
+  installdir = grubconf;
a85e8e
+};
a85e8e
+
6b3c76
+script = {
a85e8e
   name = '10_windows';
a85e8e
   common = util/grub.d/10_windows.in;
6b3c76
   installdir = grubconf;
6b3c76
@@ -713,6 +719,13 @@ script = {
a85e8e
 };
a85e8e
 
6b3c76
 script = {
a85e8e
+  name = grub-setpassword;
a85e8e
+  common = util/grub-setpassword.in;
a85e8e
+  mansection = 8;
a85e8e
+  installdir = sbin;
a85e8e
+};
a85e8e
+
6b3c76
+script = {
a85e8e
   name = grub-mkconfig_lib;
a85e8e
   common = util/grub-mkconfig_lib.in;
6b3c76
   installdir = noinst;
6b3c76
diff --git a/configure.ac b/configure.ac
6b3c76
index de17e65c4..273da6cb9 100644
6b3c76
--- a/configure.ac
6b3c76
+++ b/configure.ac
6b3c76
@@ -65,6 +65,7 @@ grub_TRANSFORM([grub-mkrelpath])
6b3c76
 grub_TRANSFORM([grub-mkrescue])
6b3c76
 grub_TRANSFORM([grub-probe])
6b3c76
 grub_TRANSFORM([grub-reboot])
6b3c76
+grub_TRANSFORM([grub-setpassword])
6b3c76
 grub_TRANSFORM([grub-rpm-sort])
6b3c76
 grub_TRANSFORM([grub-script-check])
6b3c76
 grub_TRANSFORM([grub-set-default])
a85e8e
diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in
6b3c76
index bc26e7c10..e029d0d5e 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
6b3c76
index 000000000..5973abef4
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
6b3c76
index 000000000..dd76f00fc
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
6b3c76
index 000000000..facd409e7
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
6b3c76
-- 
6b3c76
2.13.5
6b3c76