Skip to content

Commit

Permalink
issuegen: clean up code (variables, comments)
Browse files Browse the repository at this point in the history
Clean up code by minimizing the number of variables and shortening
the variable names. Remove unneeded comments, and add a few more
clarifying comments.

Separate code into clearer blocks for different operations
(generating SSH key information, udev data, and the final output
issue file).
  • Loading branch information
Robert Fairley committed Apr 20, 2020
1 parent 32c8fab commit 81458f2
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions usr/libexec/console-login-helper-messages/issuegen
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,45 @@
set -e

PKG_NAME=console-login-helper-messages
ISSUE_DIR_PUBLIC=issue.d
ISSUE_DIR_PRIVATE="${PKG_NAME}/issue.d"
SSH_DIR=/etc/ssh
ISSUE_SNIPPETS_PATH=${PKG_NAME}/issue.d
ETC_SNIPPETS="/etc/${ISSUE_SNIPPETS_PATH}"
RUN_SNIPPETS="/run/${ISSUE_SNIPPETS_PATH}"
USR_LIB_SNIPPETS="/usr/lib/${ISSUE_SNIPPETS_PATH}"

# The public directories are to be read by higher-level programs to display
# the issue on login, e.g. agetty.
# The private directories are to be read only by this script.
mkdir -p "${SSH_DIR}"
mkdir -p "/run/${ISSUE_DIR_PUBLIC}"
mkdir -p "/run/${ISSUE_DIR_PRIVATE}"
# Parts of this script write to the `${RUN_SNIPPETS}` directory,
# make sure it is created upfront.
mkdir -p "${RUN_SNIPPETS}"

# Provide key fingerprints via issue

# Provide key fingerprints via issue.
SSH_DIR=/etc/ssh
# Ensure `${SSH_DIR}` is created and can be searched without error.
mkdir -p "${SSH_DIR}"
SSH_KEY_OUTDIR="${RUN_SNIPPETS}"
for KEY_FILE in $(find "${SSH_DIR}" -name 'ssh_host_*_key') ; do
ssh-keygen -l -f "${KEY_FILE}"
done | awk '{print "SSH host key: " $2 " " $4}' > "/run/${ISSUE_DIR_PRIVATE}/21_ssh_host_keys.issue"
done | awk '{print "SSH host key: " $2 " " $4}' > "${SSH_KEY_OUTDIR}/21_ssh_host_keys.issue"


# Data from udev rules
# Add/remove data from udev rules.
UDEV_IF_OUTDIR="${RUN_SNIPPETS}"
case "$1" in
add)
echo "${2}: \\4{${2}} \\6{${2}}" > "/run/${ISSUE_DIR_PRIVATE}/22_${2}.issue"
echo "${2}: \\4{${2}} \\6{${2}}" > "${UDEV_IF_OUTDIR}/22_${2}.issue"
;;
remove)
rm -f "/run/${ISSUE_DIR_PRIVATE}/22_${2}.issue"
rm -f "${UDEV_IF_OUTDIR}/22_${2}.issue"
;;
esac

# TODO: it would be nice to have /run/issue.d be an official directory,
# see https://github.com/karelzak/util-linux/commit/1fc82a1360305f696dc1be6105c9c56a9ea03f52#commitcomment-27949895
# until then, $GENERATED_ISSUE writes to the privately scoped directory (not in /run/issue.d)
#
# Pick 40 as an index as other files can order around it easily.

# Generate a final issue message from compiling the snippets.
# Pick 40 as a prefix as other files can order around it easily.
generated_file="/run/${PKG_NAME}/40_${PKG_NAME}.issue"
generated_string=''
# Hack around files potentially not existing in the below paths with `|| true`.
generated_string+=$(cat /etc/${ISSUE_DIR_PRIVATE}/* 2>/dev/null || true)
generated_string+=$(cat /run/${ISSUE_DIR_PRIVATE}/* 2>/dev/null || true)
generated_string+=$(cat /usr/lib/${ISSUE_DIR_PRIVATE}/* 2>/dev/null || true)
generated_string+=$(cat ${ETC_SNIPPETS}/* 2>/dev/null || true)
generated_string+=$(cat ${RUN_SNIPPETS}/* 2>/dev/null || true)
generated_string+=$(cat ${USR_LIB_SNIPPETS}/* 2>/dev/null || true)

echo "${generated_string}" > "${generated_file}"

0 comments on commit 81458f2

Please sign in to comment.