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