dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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