6b2dd0
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f4c76c
From: Robert Marshall <rmarshall@redhat.com>
f4c76c
Date: Thu, 25 Jun 2015 11:13:11 -0400
31cddd
Subject: [PATCH] Add friendly grub2 password config tool (#985962)
f4c76c
f4c76c
Provided a tool for users to reset the grub2 root user password
f4c76c
without having to alter the grub.cfg. The hashed password now
f4c76c
lives in a root-only-readable configuration file.
f4c76c
f4c76c
Resolves: rhbz#985962
7e98da
7e98da
Signed-off-by: Robert Marshall <rmarshall@redhat.com>
7e98da
[pjones: fix the efidir in grub-setpassword and rename tool]
7e98da
Signed-off-by: Peter Jones <pjones@redhat.com>
7e98da
[luto: fix grub-setpassword -o's output path]
7e98da
Andy Lutomirski <luto@kernel.org>
f4c76c
---
7e98da
 configure.ac              |   1 +
7e98da
 Makefile.util.def         |  13 +++++
7e98da
 .gitignore                |   2 +
7e98da
 util/grub-mkconfig.in     |   2 +
7e98da
 util/grub-set-password.8  |  28 ++++++++++
7e98da
 util/grub-set-password.in | 128 ++++++++++++++++++++++++++++++++++++++++++++++
7e98da
 util/grub.d/01_users.in   |  11 ++++
7e98da
 7 files changed, 185 insertions(+)
7e98da
 create mode 100644 util/grub-set-password.8
7e98da
 create mode 100644 util/grub-set-password.in
f4c76c
 create mode 100644 util/grub.d/01_users.in
f4c76c
ec4acb
diff --git a/configure.ac b/configure.ac
7e98da
index 00f1db29b1a..1cb6a9615db 100644
ec4acb
--- a/configure.ac
ec4acb
+++ b/configure.ac
ec4acb
@@ -65,6 +65,7 @@ grub_TRANSFORM([grub-mkrelpath])
ec4acb
 grub_TRANSFORM([grub-mkrescue])
ec4acb
 grub_TRANSFORM([grub-probe])
ec4acb
 grub_TRANSFORM([grub-reboot])
7e98da
+grub_TRANSFORM([grub-set-password])
ec4acb
 grub_TRANSFORM([grub-rpm-sort])
ec4acb
 grub_TRANSFORM([grub-script-check])
ec4acb
 grub_TRANSFORM([grub-set-default])
f4c76c
diff --git a/Makefile.util.def b/Makefile.util.def
7e98da
index bb21c87c8ed..7729e65c607 100644
f4c76c
--- a/Makefile.util.def
f4c76c
+++ b/Makefile.util.def
7e98da
@@ -442,6 +442,12 @@ script = {
da63b3
   installdir = grubconf;
f4c76c
 };
f4c76c
 
da63b3
+script = {
f4c76c
+  name = '01_users';
f4c76c
+  common = util/grub.d/01_users.in;
f4c76c
+  installdir = grubconf;
f4c76c
+};
f4c76c
+
da63b3
 script = {
f4c76c
   name = '10_windows';
f4c76c
   common = util/grub.d/10_windows.in;
7e98da
@@ -724,6 +730,13 @@ script = {
da63b3
   installdir = sbin;
f4c76c
 };
f4c76c
 
da63b3
+script = {
7e98da
+  name = grub-set-password;
7e98da
+  common = util/grub-set-password.in;
f4c76c
+  mansection = 8;
f4c76c
+  installdir = sbin;
f4c76c
+};
f4c76c
+
da63b3
 script = {
f4c76c
   name = grub-mkconfig_lib;
f4c76c
   common = util/grub-mkconfig_lib.in;
ec4acb
diff --git a/.gitignore b/.gitignore
7e98da
index fa2e5b609b1..141684867d1 100644
ec4acb
--- a/.gitignore
ec4acb
+++ b/.gitignore
3e07ee
@@ -111,6 +111,8 @@ grub-*.tar.*
ec4acb
 /grub*-script-check.1
ec4acb
 /grub*-set-default
ec4acb
 /grub*-set-default.8
7e98da
+/grub*-set-password
7e98da
+/grub*-set-password.8
ec4acb
 /grub*-shell
ec4acb
 /grub*-shell-tester
ec4acb
 /grub*-sparc64-setup
f4c76c
diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in
752ceb
index f68d4925ee6..bdb9982aefb 100644
f4c76c
--- a/util/grub-mkconfig.in
f4c76c
+++ b/util/grub-mkconfig.in
752ceb
@@ -282,6 +282,8 @@ for i in "${grub_mkconfig_dir}"/* ; do
f4c76c
     *~) ;;
f4c76c
     # emacsen autosave files. FIXME: support other editors
f4c76c
     */\#*\#) ;;
f4c76c
+    # rpm config files of yore.
f4c76c
+    *.rpmsave|*.rpmnew|*.rpmorig) ;;
f4c76c
     *)
f4c76c
       if grub_file_is_not_garbage "$i" && test -x "$i" ; then
f4c76c
         echo
7e98da
diff --git a/util/grub-set-password.8 b/util/grub-set-password.8
f4c76c
new file mode 100644
7e98da
index 00000000000..9646546e43d
f4c76c
--- /dev/null
7e98da
+++ b/util/grub-set-password.8
f4c76c
@@ -0,0 +1,28 @@
7e98da
+.TH GRUB-SET-PASSWORD 3 "Thu Jun 25 2015"
f4c76c
+.SH NAME
7e98da
+\fBgrub-set-password\fR \(em Generate the user.cfg file containing the hashed grub bootloader password.
f4c76c
+
f4c76c
+.SH SYNOPSIS
7e98da
+\fBgrub-set-password\fR [OPTION]
f4c76c
+
f4c76c
+.SH DESCRIPTION
7e98da
+\fBgrub-set-password\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.
f4c76c
+
f4c76c
+The file has the format:
9d15b4
+GRUB2_PASSWORD=<\fIhashed password\fR>.
f4c76c
+
f4c76c
+.SH OPTIONS
f4c76c
+.TP
f4c76c
+-h, --help
f4c76c
+Display program usage and exit.
f4c76c
+.TP
f4c76c
+-v, --version
f4c76c
+Display the current version.
f4c76c
+.TP
7e98da
+-o, --output=<\fIDIRECTORY\fR>
f4c76c
+Choose the file path to which user.cfg will be written.
f4c76c
+
f4c76c
+.SH SEE ALSO
f4c76c
+.BR "info grub"
f4c76c
+
f4c76c
+.BR "info grub2-mkpasswd-pbkdf2"
7e98da
diff --git a/util/grub-set-password.in b/util/grub-set-password.in
f4c76c
new file mode 100644
7e98da
index 00000000000..5ebf50576d6
f4c76c
--- /dev/null
7e98da
+++ b/util/grub-set-password.in
7e98da
@@ -0,0 +1,128 @@
f4c76c
+#!/bin/sh -e
f4c76c
+
7e98da
+EFIDIR=$(grep ^ID= /etc/os-release | sed -e 's/^ID=//' -e 's/rhel/redhat/')
f4c76c
+if [ -d /sys/firmware/efi/efivars/ ]; then
7e98da
+    grubdir=`echo "/@bootdirname@/efi/EFI/${EFIDIR}/" | sed 's,//*,/,g'`
f4c76c
+else
f4c76c
+    grubdir=`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`
f4c76c
+fi
f4c76c
+
f4c76c
+PACKAGE_VERSION="@PACKAGE_VERSION@"
f4c76c
+PACKAGE_NAME="@PACKAGE_NAME@"
f4c76c
+self=`basename $0`
f4c76c
+bindir="@bindir@"
f4c76c
+grub_mkpasswd="${bindir}/@grub_mkpasswd_pbkdf2@"
f4c76c
+
f4c76c
+# Usage: usage
f4c76c
+# Print the usage.
f4c76c
+usage () {
f4c76c
+    cat <
7e98da
+Usage: $0 [OPTION]
f4c76c
+$0 prompts the user to set a password on the grub bootloader. The password
7e98da
+is written to a file named user.cfg which lives in the GRUB directory
7e98da
+located by default at ${grubdir}.
7e98da
+
7e98da
+  -h, --help                     print this message and exit
7e98da
+  -v, --version                  print the version information and exit
7e98da
+  -o, --output_path <directory>  put user.cfg in a user-selected directory
f4c76c
+
f4c76c
+Report bugs at https://bugzilla.redhat.com.
f4c76c
+EOF
f4c76c
+}
f4c76c
+
f4c76c
+argument () {
f4c76c
+    opt=$1
f4c76c
+    shift
f4c76c
+
f4c76c
+    if test $# -eq 0; then
f4c76c
+        gettext_printf "%s: option requires an argument -- \`%s'\n" "$self" "$opt" 1>&2
f4c76c
+        exit 1
f4c76c
+    fi
f4c76c
+    echo $1
f4c76c
+}
f4c76c
+
f4c76c
+# Ensure that it's the root user running this script
f4c76c
+if [ "${EUID}" -ne 0 ]; then
f4c76c
+    echo "The grub bootloader password may only be set by root."
f4c76c
+    usage
f4c76c
+    exit 2
f4c76c
+fi
f4c76c
+
f4c76c
+# Check the arguments.
f4c76c
+while test $# -gt 0
f4c76c
+do
f4c76c
+    option=$1
f4c76c
+    shift
f4c76c
+
f4c76c
+    case "$option" in
f4c76c
+    -h | --help)
f4c76c
+	usage
f4c76c
+	exit 0 ;;
f4c76c
+    -v | --version)
f4c76c
+	echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
f4c76c
+	exit 0 ;;
f4c76c
+    -o | --output)
f4c76c
+        OUTPUT_PATH=`argument $option "$@"`; shift ;;
f4c76c
+    --output=*)
f4c76c
+        OUTPUT_PATH=`echo "$option" | sed 's/--output=//'` ;;
f4c76c
+    -o=*)
f4c76c
+        OUTPUT_PATH=`echo "$option" | sed 's/-o=//'` ;;
f4c76c
+    esac
f4c76c
+done
f4c76c
+
f4c76c
+# set user input or default path for user.cfg file
f4c76c
+if [ -z "${OUTPUT_PATH}" ]; then
f4c76c
+    OUTPUT_PATH="${grubdir}"
f4c76c
+fi
f4c76c
+
f4c76c
+if [ ! -d "${OUTPUT_PATH}" ]; then
f4c76c
+    echo "${OUTPUT_PATH} does not exist."
f4c76c
+    usage
f4c76c
+    exit 2;
f4c76c
+fi
f4c76c
+
f4c76c
+ttyopt=$(stty -g)
f4c76c
+fixtty() {
f4c76c
+      stty ${ttyopt}
f4c76c
+}
f4c76c
+
f4c76c
+trap fixtty EXIT
f4c76c
+stty -echo
f4c76c
+
f4c76c
+# prompt & confirm new grub2 root user password
f4c76c
+echo -n "Enter password: "
f4c76c
+read PASSWORD
f4c76c
+echo
f4c76c
+echo -n "Confirm password: "
f4c76c
+read PASSWORD_CONFIRM
f4c76c
+echo
f4c76c
+stty ${ttyopt}
f4c76c
+
f4c76c
+getpass() {
f4c76c
+    local P0
f4c76c
+    local P1
f4c76c
+    P0="$1" && shift
f4c76c
+    P1="$1" && shift
f4c76c
+
f4c76c
+    ( echo ${P0} ; echo ${P1} ) | \
7e98da
+        LC_ALL=C ${grub_mkpasswd} | \
f4c76c
+        grep -v '[eE]nter password:' | \
f4c76c
+        sed -e "s/PBKDF2 hash of your password is //"
f4c76c
+}
f4c76c
+
f4c76c
+MYPASS="$(getpass "${PASSWORD}" "${PASSWORD_CONFIRM}")"
f4c76c
+if [ -z "${MYPASS}" ]; then
f4c76c
+      echo "${self}: error: empty password" 1>&2
f4c76c
+      exit 1
f4c76c
+fi
f4c76c
+
f4c76c
+# on the ESP, these will fail to set the permissions, but it's okay because
f4c76c
+# the directory is protected.
7e98da
+install -m 0600 /dev/null "${OUTPUT_PATH}/user.cfg" 2>/dev/null || :
7e98da
+chmod 0600 "${OUTPUT_PATH}/user.cfg" 2>/dev/null || :
7e98da
+echo "GRUB2_PASSWORD=${MYPASS}" > "${OUTPUT_PATH}/user.cfg"
7e98da
+
7e98da
+if ! grep -q "^### BEGIN /etc/grub.d/01_users ###$" "${OUTPUT_PATH}/grub.cfg"; then
7e98da
+    echo "WARNING: The current configuration lacks password support!"
7e98da
+    echo "Update your configuration with @grub_mkconfig@ to support this feature."
7e98da
+fi
f4c76c
diff --git a/util/grub.d/01_users.in b/util/grub.d/01_users.in
f4c76c
new file mode 100644
ec4acb
index 00000000000..db2f44bfb78
f4c76c
--- /dev/null
f4c76c
+++ b/util/grub.d/01_users.in
f4c76c
@@ -0,0 +1,11 @@
f4c76c
+#!/bin/sh -e
f4c76c
+cat << EOF
f4c76c
+if [ -f \${prefix}/user.cfg ]; then
f4c76c
+  source \${prefix}/user.cfg
9d15b4
+  if [ -n "\${GRUB2_PASSWORD}" ]; then
f4c76c
+    set superusers="root"
f4c76c
+    export superusers
f4c76c
+    password_pbkdf2 root \${GRUB2_PASSWORD}
f4c76c
+  fi
f4c76c
+fi
f4c76c
+EOF