-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch-log4j.sh
More file actions
executable file
·48 lines (42 loc) · 1.81 KB
/
Copy pathpatch-log4j.sh
File metadata and controls
executable file
·48 lines (42 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env sh
# patch-log4j.sh - replace old log4j files in a target directory with new ones from a log4j download
# Copyright (c) 2021 Peter Willis
set -e -u
[ "${DEBUG:-0}" = "1" ] && set -x
DRYRUN=0 # Set to 1 to prevent removing/copying files
# You can replace CHECKSUM_TOOL with anything that outputs a checksum
# as the first word of output, like md5sum
CHECKSUM_TOOL="${CHECKSUM_TOOL:-sha256sum}"
if [ $# -lt 2 ] ; then
cat <<EOUSAGE
Usage: $0 LOG4J_DIR TARGET_DIR
LOG4J_DIR must be a directory with the patched version of Log4j downloaded.
TARGET_DIR should be the directory of a Java project with log4j jars.
EOUSAGE
exit 1
fi
LOG4J_DIR="$1"
TARGET_DIR="$2"
TARGET_JARS="$(find "$TARGET_DIR" -type f -iname '*log4j*.jar')"
for jar in $TARGET_JARS ; do
target_dn="$(dirname "$jar")"
target_bn="$(basename "$jar" .jar)"
libname="$(echo "$target_bn" | sed -e 's/^\(.*\)-\([0-9]\+\.[0-9]\+\.[0-9]\+\)/\1/')"
libver="$(echo "$target_bn" | sed -e 's/^\(.*\)-\([0-9]\+\.[0-9]\+\.[0-9]\+\)/\2/')"
echo "$0: Found target jar $libname v$libver ($jar)"
detected_jar="$(find "$LOG4J_DIR/" -type f -iname '*.jar' | grep -e "\/\?$libname-[0-9]\+\.[0-9]\+\.[0-9]\+\.jar$")"
if [ -z "$detected_jar" ] || [ "$(echo "$detected_jar" | wc -l)" -gt 1 ] ; then
echo "$0: ERROR: Could not detect log4j jar file matching name '$libname'"
else
echo "$0: Detected log4j jar file '$detected_jar'"
if [ "$($CHECKSUM_TOOL "$jar"|awk '{print $1}')" = "$($CHECKSUM_TOOL "$detected_jar"|awk '{print $1}')" ] ; then
echo "$0: File already updated"
else
echo "$0: Replacing jar '$jar' with '$detected_jar'"
if [ "$DRYRUN" = "0" ] ; then
rm -f "$jar"
cp -f "$detected_jar" "$target_dn/"
fi
fi
fi
done