c789522
From 68f21f5ebebc23a6d8512631f2a1d474afcccd43 Mon Sep 17 00:00:00 2001
f4c76c0
From: Robert Marshall <rmarshall@redhat.com>
f4c76c0
Date: Thu, 25 Jun 2015 11:13:11 -0400
63f1a98
Subject: [PATCH 129/250] 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
 configure.ac             |   1 +
ec4acbb
 Makefile.util.def        |  13 +++++
ec4acbb
 .gitignore               |   2 +
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 +++++
bbc6a89
 7 files changed, 180 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
ec4acbb
diff --git a/configure.ac b/configure.ac
ec4acbb
index 49427cbf989..1960840048f 100644
ec4acbb
--- a/configure.ac
ec4acbb
+++ b/configure.ac
ec4acbb
@@ -65,6 +65,7 @@ grub_TRANSFORM([grub-mkrelpath])
ec4acbb
 grub_TRANSFORM([grub-mkrescue])
ec4acbb
 grub_TRANSFORM([grub-probe])
ec4acbb
 grub_TRANSFORM([grub-reboot])
ec4acbb
+grub_TRANSFORM([grub-setpassword])
ec4acbb
 grub_TRANSFORM([grub-rpm-sort])
ec4acbb
 grub_TRANSFORM([grub-script-check])
ec4acbb
 grub_TRANSFORM([grub-set-default])
f4c76c0
diff --git a/Makefile.util.def b/Makefile.util.def
ec4acbb
index d846b81b5f9..226c46b2d79 100644
f4c76c0
--- a/Makefile.util.def
f4c76c0
+++ b/Makefile.util.def
da63b36
@@ -439,6 +439,12 @@ script = {
da63b36
   installdir = grubconf;
f4c76c0
 };
f4c76c0
 
da63b36
+script = {
f4c76c0
+  name = '01_users';
f4c76c0
+  common = util/grub.d/01_users.in;
f4c76c0
+  installdir = grubconf;
f4c76c0
+};
f4c76c0
+
da63b36
 script = {
f4c76c0
   name = '10_windows';
f4c76c0
   common = util/grub.d/10_windows.in;
da63b36
@@ -721,6 +727,13 @@ script = {
da63b36
   installdir = sbin;
f4c76c0
 };
f4c76c0
 
da63b36
+script = {
f4c76c0
+  name = grub-setpassword;
f4c76c0
+  common = util/grub-setpassword.in;
f4c76c0
+  mansection = 8;
f4c76c0
+  installdir = sbin;
f4c76c0
+};
f4c76c0
+
da63b36
 script = {
f4c76c0
   name = grub-mkconfig_lib;
f4c76c0
   common = util/grub-mkconfig_lib.in;
ec4acbb
diff --git a/.gitignore b/.gitignore
ec4acbb
index 1c85371298c..387e3554cd5 100644
ec4acbb
--- a/.gitignore
ec4acbb
+++ b/.gitignore
ec4acbb
@@ -107,6 +107,8 @@ grub-*.tar.*
ec4acbb
 /grub*-script-check.1
ec4acbb
 /grub*-set-default
ec4acbb
 /grub*-set-default.8
ec4acbb
+/grub*-setsetpassword
ec4acbb
+/grub*-setsetpassword.8
ec4acbb
 /grub*-shell
ec4acbb
 /grub*-shell-tester
ec4acbb
 /grub*-sparc64-setup
f4c76c0
diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in
ec4acbb
index 9d595ac058a..fb8724708b5 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
ec4acbb
index 00000000000..49200a848b7
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:
9d15b4d
+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
ec4acbb
index 00000000000..dd76f00fc0e
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
ec4acbb
index 00000000000..db2f44bfb78
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
9d15b4d
+  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
-- 
63f1a98
2.14.3
f4c76c0