nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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