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

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