Files
server/opt/drweb/scripts/include/key.sh
cutemeli 0bfc6c8425 Initial
2025-12-22 10:32:59 +00:00

178 lines
5.4 KiB
Bash

AGENT_CONF=$CONFDIR/agent.conf
DRWEB32_INI=$CONFDIR/drweb32.ini
ICAPD_INI=$CONFDIR/drweb-icapd.ini
DEFAULT_LICENSE_FILE=$PREFIX/drweb32.key
KEY_SCRIPT_NAME=`basename $0`
# $1 - config file
# $2 - section name
# $3 - key name
DeleteAbsentLicenseFiles() {
if [ ! -f "$1" ]; then
echo "*** WARNING: $1 : No such file!"
echo
return 1
fi
# Retrieve all license file names from config.
files=`IniKeyValues $1 $2 $3`
config=$1
config_new=`MakeTmpFile`
cp -p -f $config $config_new
# Iterate over them, check if they present,
# don't include them into config copy if they aren't.
for i in $files ; do
if [ ! -f "$i" ]; then
IniSectProc $config_new $2 "next if m|^\\s*$3\\s*=\\s*$i\\s*\$|;"
fi
done
if diff $config $config_new >/dev/null; then
true
else
echo
echo "Some keys specified in $config are not found!"
echo "These keys will be removed from the configuration file."
AskUpdateConfig $config $config_new dont_ask
fi
}
AddLicenseFile() {
# Add what
license_file=$1
# Add where
config_file=$2
section_name=$3
key_name=$4
if [ ! -f "$config_file" ]; then
return 1
fi
config_file_new=`MakeTmpFile`
cp -p -f $config_file $config_file_new
# Retrieve all license file names from config.
files=`IniKeyValues $config_file_new $section_name $key_name`
# Iterate over them and diff them with the new license file.
added_already=
for i in $files ; do
[ -f "$i" ] || continue
diff "$i" "$license_file" 1>/dev/null && added_already=1 && break
done
# If not added yet, copy license file (if needed) and add it to the config.
license_file_copy=
if [ -z "$added_already" ]; then
# Look if this key is in confdir already...
keys_in_confdir=`ls $CONFDIR/drweb32-*.key 2>/dev/null`
for i in $keys_in_confdir ; do
if diff "$i" "$license_file" 1>/dev/null ; then
license_file_copy=$i
break
fi
done
# ...Copy it if not.
if [ -z "$license_file_copy" ]; then
license_file_copy="$CONFDIR/drweb32-`env LANG=C date +%Y%m%d%H%M%S`.key"
cp -p -f $license_file $license_file_copy
fi
if [ -n "$files" ]; then
# There's some key in the section, add the new one before it.
IniSectProc $config_file_new $section_name "if (/^\\s*$key_name\\s*=\\s*(.*)/) { print \"$key_name = $license_file_copy\n\"; last }"
else
# There are no keys in the section, add the first one.
IniSectProc $config_file_new $section_name "if (!\$added) { \$_=\"# Added by Dr.Web's $KEY_SCRIPT_NAME\n$key_name = $license_file_copy\n\n\".\$_; \$added=1 }"
fi
echo
echo "Adding $license_file_copy into [$section_name] of $config_file ."
AskUpdateConfig $config_file $config_file_new dont_ask
else
echo
echo "Already added to [$section_name] of $config_file ."
fi
}
SetKey() {
IsKeyValid && return
# Ask user for path to the key.
#
while true ; do
printf "Enter path to the Dr.Web license key file or '0' to skip: "
read file
echo
[ -z "$file" ] && continue
[ "$file" = "0" ] && break
if [ -f "$file" ] ; then
# Check the key.
if IsKeyValid "$file" ; then
true
else
echo "Wrong key!"
echo
continue
fi
# Check for clean install (default license file is absent on disk).
if [ -f "$DEFAULT_LICENSE_FILE" ]; then
# Try to add license file to all configs.
AddLicenseFile "$file" $AGENT_CONF StandaloneMode LicenseFile
AddLicenseFile "$file" $DRWEB32_INI Daemon Key
AddLicenseFile "$file" $DRWEB32_INI Scanner Key
if [ "$PRODUCT_NAME_SHORT" = "drweb-internet-gateways" ]; then
# Try to add license file to $ICAPD_INI also.
AddLicenseFile "$file" $ICAPD_INI Icapd Key
fi
# Delete entries of all keys which are absent on disk.
DeleteAbsentLicenseFiles $AGENT_CONF StandaloneMode LicenseFile
DeleteAbsentLicenseFiles $DRWEB32_INI Daemon Key
DeleteAbsentLicenseFiles $DRWEB32_INI Scanner Key
if [ "$PRODUCT_NAME_SHORT" = "drweb-internet-gateways" ]; then
# Delete dead keys from $ICAPD_INI also.
DeleteAbsentLicenseFiles $ICAPD_INI Icapd Key
fi
else
# Add this license file under default name.
cp -p -f "$file" $DEFAULT_LICENSE_FILE
chmod 600 $DEFAULT_LICENSE_FILE
chown drweb:drweb $DEFAULT_LICENSE_FILE
fi
break
else
echo "`eval_gettext \"Specified path to key file is invalid. Try again, please.\"`"
fi
done
}
IsKeyValid() {
retval=0
if [ -n "$1" ] ; then
license_file=$1
else
license_file="$DEFAULT_LICENSE_FILE"
fi
if [ -f "$license_file" ] ; then
if [ -x "$PREFIX/read_signed" ] ; then
if $PREFIX/read_signed key "$license_file" ; then
retval=0
else
retval=1
fi
fi
else
retval=1
fi
return $retval
}