Skip to content

Commit

Permalink
Compatibility with BSD date (#1)
Browse files Browse the repository at this point in the history
* Add compatibility with BSD date.
* Require Bash >= 4.0 for Shell Parameter Expansion.
---------
Co-authored-by: Esa Jokinen <[email protected]>
  • Loading branch information
gamma authored Mar 31, 2024
1 parent ac19068 commit 39a8018
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ This command line tool (Bash script) helps building RFC 9116 compliant, valid
& well formatted security.txt files as well as automating the PGP signing &
expire date updating process.

Requires Bash >= 4.0. Compatible with both GNU coreutils & BSD `date`.

### Usage

```bash
Expand Down
57 changes: 46 additions & 11 deletions securitytxt-signer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ required_command() {

UNMET=0

if ((BASH_VERSINFO[0] < 4)); then
echo -e "\033[0;31mThis script requires bash >= 4.0\033[0m" >&2
echo -e "\033[0;31mYour bash ${BASH_VERSION} is unsupported.\033[0m" >&2
((UNMET=UNMET+1))
fi

required_command "sed"
required_command "awk"
required_command "grep"
Expand All @@ -67,6 +73,15 @@ if [ "$UNMET" -gt 0 ]; then
exit 1
fi

# Compatibility for non-GNU date

if date --version >/dev/null 2>&1; then
GNU_DATE=1
else
echo -e "\033[0;33mIn compatibility mode for non-GNU date command.\033[0m" >&2
GNU_DATE=0
fi

shopt -s nocasematch

# Validate the PGP key.
Expand All @@ -77,14 +92,25 @@ if ! [[ "$KEY" =~ ^0x[a-fA-F0-9]{8,40}$ ]]; then
else
KEY_INFO=$(gpg --list-secret-keys "$KEY" 2> >(sed $'s,.*,\e[33m&\e[m,'>&2))

KEY_EXPIRES=$(
echo "$KEY_INFO" \
| grep "sec" \
| grep -Eo 'expires:\ [0-9\-]+' \
| awk '{ print $2}' \
| date -Iseconds -u -f - \
| sed -e 's/+00:00$/Z/'
)
if (( GNU_DATE )); then
KEY_EXPIRES=$(
echo "$KEY_INFO" \
| grep "sec" \
| grep -Eo 'expires:\ [0-9\-]+' \
| awk '{ print $2}' \
| date -Iseconds -u -f - \
| sed -e 's/+00:00$/Z/'
)
else
KEY_EXPIRES=$(
echo "$KEY_INFO" \
| grep "sec" \
| grep -Eo 'expires:\ [0-9\-]+' \
| awk '{ print $2}' \
| date -Iseconds -u \
| sed -e 's/+00:00$/Z/'
)
fi
echo

if [[ "$KEY_EXPIRES" = "" ]]; then
Expand Down Expand Up @@ -115,13 +141,22 @@ fi
# Set expire date.
# If the key expires before the DAYS_MAX, use the key expiration date instead.

EXPIRES=$(date -Iseconds -u -d "${DAYS_MAX} days" | sed -e 's/+00:00$/Z/')
if (( GNU_DATE )); then
EXPIRES=$(date -Iseconds -u -d "${DAYS_MAX} days" | sed -e 's/+00:00$/Z/')
else
EXPIRES=$(date -Iseconds -u -v+${DAYS_MAX}d | sed -e 's/+00:00$/Z/')
fi

if [ -z ${KEY_EXPIRES+x} ]; then
echo -e "\033[0;33mUSING EXPIRE (max ${DAYS_MAX} days): $EXPIRES\033[0;0m"
else
EXPIRES_COMPARABLE=$(date -d "$EXPIRES" +%s)
KEY_EXPIRES_COMPARABLE=$(date -d "$KEY_EXPIRES" +%s)
if (( GNU_DATE )); then
EXPIRES_COMPARABLE=$(date -d "$EXPIRES" +%s)
KEY_EXPIRES_COMPARABLE=$(date -d "$KEY_EXPIRES" +%s)
else
EXPIRES_COMPARABLE=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$EXPIRES" +%s)
KEY_EXPIRES_COMPARABLE=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$KEY_EXPIRES" +%s)
fi

echo -e "\033[0;33mComparing ${EXPIRES} to key expr ${KEY_EXPIRES}.\033[0;0m"

Expand Down

0 comments on commit 39a8018

Please sign in to comment.