nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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