6f1e3d5
From b970cde9a609dce14c52145aaa61b87299f55086 Mon Sep 17 00:00:00 2001
f4c76c0
From: Robert Marshall <rmarshall@redhat.com>
f4c76c0
Date: Thu, 25 Jun 2015 11:13:11 -0400
6f1e3d5
Subject: [PATCH 130/198] 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
---
bbc6a89
 .gitignore               |   2 +
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 +++++
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
f4c76c0
diff --git a/.gitignore b/.gitignore
bbc6a89
index 1c8537129..387e3554c 100644
f4c76c0
--- a/.gitignore
f4c76c0
+++ b/.gitignore
bbc6a89
@@ -107,6 +107,8 @@ grub-*.tar.*
bbc6a89
 /grub*-script-check.1
bbc6a89
 /grub*-set-default
bbc6a89
 /grub*-set-default.8
bbc6a89
+/grub*-setsetpassword
bbc6a89
+/grub*-setsetpassword.8
bbc6a89
 /grub*-shell
bbc6a89
 /grub*-shell-tester
bbc6a89
 /grub*-sparc64-setup
f4c76c0
diff --git a/Makefile.util.def b/Makefile.util.def
bc092b9
index d846b81b5..226c46b2d 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;
f4c76c0
diff --git a/configure.ac b/configure.ac
6f1e3d5
index 49427cbf9..196084004 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
bc092b9
index 9d595ac05..fb8724708 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
bc092b9
index 000000000..49200a848
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
bc092b9
index 000000000..dd76f00fc
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
bc092b9
index 000000000..db2f44bfb
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
-- 
da63b36
2.14.3
f4c76c0